all files / src/ Mesh.js

79.17% Statements 19/24
33.33% Branches 3/9
75% Functions 3/4
79.17% Lines 19/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                1×                                         32× 32×         297×       112×   112×                                                                             112×                                 1× 1× 1× 1× 1× 1× 1×   1× 1× 1× 1× 1×      
import Renderable from './Renderable';
import glenum from './core/glenum';
import Texture2D from './Texture2D';
 
/**
 * @constructor clay.Mesh
 * @extends clay.Renderable
 */
var Mesh = Renderable.extend(
/** @lends clay.Mesh# */
{
    /**
     * Used when it is a skinned mesh
     * @type {clay.Skeleton}
     */
    skeleton: null,
    /**
     * Joints indices Meshes can share the one skeleton instance and each mesh can use one part of joints. Joints indices indicate the index of joint in the skeleton instance
     * @type {number[]}
     */
    joints: null,
 
    /**
     * If store the skin matrices in vertex texture
     * @type {bool}
     */
    useSkinMatricesTexture: false
 
}, function () {
    Eif (!this.joints) {
        this.joints = [];
    }
}, {
 
    isSkinnedMesh: function () {
        return !!(this.skeleton && this.joints && this.joints.length > 0);
    },
 
    render: function (renderer, shader, program) {
        var _gl = renderer.gl;
        // Set pose matrices of skinned mesh
        Iif (this.skeleton) {
            // TODO Multiple mesh share same skeleton
            this.skeleton.update();
 
            var skinMatricesArray = this.skeleton.getSubSkinMatrices(this.__uid__, this.joints);
 
            // if (this.useSkinMatricesTexture) {
            //     var size;
            //     var numJoints = this.joints.length;
            //     if (numJoints > 256) {
            //         size = 64;
            //     }
            //     else if (numJoints > 64) {
            //         size = 32;
            //     }
            //     else if (numJoints > 16) {
            //         size = 16;
            //     }
            //     else {
            //         size = 8;
            //     }
 
            //     var texture = this.getSkinMatricesTexture();
            //     texture.width = size;
            //     texture.height = size;
 
            //     if (!texture.pixels || texture.pixels.length !== size * size * 4) {
            //         texture.pixels = new Float32Array(size * size * 4);
            //     }
            //     texture.pixels.set(skinMatricesArray);
            //     texture.dirty();
 
            //     shader.setUniform(_gl, '1f', 'skinMatricesTextureSize', size);
            // }
            // else {
            program.setUniformOfSemantic(_gl, 'SKIN_MATRIX', skinMatricesArray);
            // }
        }
 
        return Renderable.prototype.render.call(this, renderer, shader, program);
    },
 
    getSkinMatricesTexture: function () {
        this._skinMatricesTexture = this._skinMatricesTexture || new Texture2D({
            type: glenum.FLOAT,
            minFilter: glenum.NEAREST,
            magFilter: glenum.NEAREST,
            useMipmap: false,
            flipY: false
        });
 
        return this._skinMatricesTexture;
    }
});
 
// Enums
Mesh.POINTS = glenum.POINTS;
Mesh.LINES = glenum.LINES;
Mesh.LINE_LOOP = glenum.LINE_LOOP;
Mesh.LINE_STRIP = glenum.LINE_STRIP;
Mesh.TRIANGLES = glenum.TRIANGLES;
Mesh.TRIANGLE_STRIP = glenum.TRIANGLE_STRIP;
Mesh.TRIANGLE_FAN = glenum.TRIANGLE_FAN;
 
Mesh.BACK = glenum.BACK;
Mesh.FRONT = glenum.FRONT;
Mesh.FRONT_AND_BACK = glenum.FRONT_AND_BACK;
Mesh.CW = glenum.CW;
Mesh.CCW = glenum.CCW;
 
export default Mesh;