Ejemplo n.º 1
0
  /**
   * Set the camera's viewport.
   *
   * @param {vec4} viewport
   */
  viewport(viewport) {
    vec4.copy(this.rect, viewport);

    this.aspect = viewport[2] / viewport[3];

    this.dirty = true;
  }
Ejemplo n.º 2
0
    update (deltaTime, cameraPos, invPlacementMat) {
        if (!this.m2Geom) return;
        //if (!this.materialArray) return;

        var animation = this.currentAnimation;
        animation = this.checkCurrentAnimation(animation, this.currentTime + deltaTime);
        var animationTime = this.currentTime + deltaTime - this.currentAnimationStart;

        var subMeshColors = this.getSubMeshColor(animation, animationTime);
        this.subMeshColors = subMeshColors;

        var transperencies = this.getTransperencies(animation, animationTime);
        this.transperencies = transperencies;

        this.calcBones(animation, animationTime, cameraPos, invPlacementMat);
        this.calcAnimMatrixes(animation, animationTime);

        var skinData = this.skinGeom.skinFile.header;

        var cameraInlocalPos = vec4.create();
        vec4.copy(cameraInlocalPos, cameraPos);
        vec4.transformMat4(cameraInlocalPos, cameraInlocalPos, invPlacementMat);

        this.materialArray.sort(function(a, b) {
            var result = a.layer - b.layer;
            if (result == 0) {
                var mesh1Pos = skinData.subMeshes[a.meshIndex].pos;
                var mesh2Pos = skinData.subMeshes[b.meshIndex].pos;

                var mesh1Vec = vec3.create();
                vec3.subtract(mesh1Vec, [mesh1Pos.x, mesh1Pos.y, mesh1Pos.z], cameraInlocalPos);

                var mesh2Vec = vec3.create();
                vec3.subtract(mesh2Vec, [mesh2Pos.x, mesh2Pos.y, mesh2Pos.z], cameraInlocalPos);

                var distMesh1 = vec3.length(mesh1Vec)
                var distMesh2 = vec3.length(mesh2Vec);

                result = distMesh2 - distMesh1;
            }

            return result;
        });

        this.currentTime += deltaTime;
    }
Ejemplo n.º 3
0
    calcBoneMatrix (index, bone, animation, time, cameraPos, invPlacementMat){
        if (bone.isCalculated) return;
        var boneDefinition = this.m2Geom.m2File.bones[index];
        var parentBone = boneDefinition.parent_bone;
        var animationRecord = this.m2Geom.m2File.animations[animation];

        //2. Calc current transformation matrix

        var tranformMat = mat4.create();
        tranformMat = mat4.identity(tranformMat);

        if (parentBone>=0) {
            this.calcBoneMatrix(parentBone, this.bones[parentBone], animation, time, cameraPos, invPlacementMat);
            mat4.multiply(tranformMat, tranformMat, this.bones[parentBone].tranformMat);
        }

        mat4.translate(tranformMat, tranformMat, [
            boneDefinition.pivot.x,
            boneDefinition.pivot.y,
            boneDefinition.pivot.z,
            0
        ]);

        if (boneDefinition.translation.valuesPerAnimation.length > 0) {
            var transVec = this.getTimedValue(
                0,
                time,
                animationRecord.length,
                animation,
                boneDefinition.translation);

            if (transVec) {
                transVec = mat4.translate(tranformMat, tranformMat, [
                    transVec[0],
                    transVec[1],
                    transVec[2],
                    0
                ]);
                this.isAnimated = true;
            }
        }

        if (((boneDefinition.flags & 0x8) > 0) || ((boneDefinition.flags & 0x40) > 0)) {
            //From http://gamedev.stackexchange.com/questions/112270/calculating-rotation-matrix-for-an-object-relative-to-a-planets-surface-in-monog
            var modelForward = vec3.create();
            var cameraInlocalPos = vec4.create();

            vec4.copy(cameraInlocalPos, cameraPos);
            vec4.transformMat4(cameraInlocalPos, cameraInlocalPos, invPlacementMat);

            if (parentBone>=0) {
                vec4.transformMat4(cameraInlocalPos, cameraInlocalPos, this.bones[parentBone].inverTransforMat);
            }
            vec4.subtract(cameraInlocalPos, cameraInlocalPos, [
                boneDefinition.pivot.x,
                boneDefinition.pivot.y,
                boneDefinition.pivot.z,
                0
            ]);

            vec3.normalize(modelForward, cameraInlocalPos);

            if ((boneDefinition.flags & 0x40) > 0) {
                //Cylindrical billboard

                var modelUp = vec3.fromValues(0,0,1);

                var modelRight = vec3.create();
                vec3.cross(modelRight, modelUp, modelForward);
                vec3.normalize(modelRight, modelRight);

                vec3.cross(modelForward, modelRight, modelUp);
                vec3.normalize(modelForward, modelForward);

                vec3.cross(modelRight, modelUp, modelForward);
                vec3.normalize(modelRight, modelRight);

            } else {
                //Spherical billboard
                var modelRight = vec3.create();
                vec3.cross(modelRight, [0, 0, 1], modelForward);
                vec3.normalize(modelRight, modelRight);

                var modelUp = vec3.create();
                vec3.cross(modelUp, modelForward, modelRight);
                vec3.normalize(modelUp, modelUp);
            }


            mat4.multiply(tranformMat, tranformMat,
                [
                    modelForward[0],modelForward[1],modelForward[2],0,
                    modelRight[0],modelRight[1],modelRight[2],0,
                    modelUp[0],modelUp[1],modelUp[2],0,
                    0,0,0,1
                ]);
            this.isAnimated = true;
        } else if (boneDefinition.rotation.valuesPerAnimation.length > 0) {

            var quaternionVec4 = this.getTimedValue(
                1,
                time,
                animationRecord.length,
                animation,
                boneDefinition.rotation);

            if (quaternionVec4) {
                var orientMatrix = mat4.create();
                mat4.fromQuat(orientMatrix, quaternionVec4 );
                mat4.multiply(tranformMat, tranformMat, orientMatrix);
                this.isAnimated = true;
            }
        }

        if (boneDefinition.scale.valuesPerAnimation.length > 0) {

            var scaleVec3 = this.getTimedValue(
                0,
                time,
                animationRecord.length,
                animation,
                boneDefinition.scale);

            if (scaleVec3) {
                mat4.scale(tranformMat, tranformMat, [
                        scaleVec3[0],
                        scaleVec3[1],
                        scaleVec3[2]
                    ]
                );
            }
            this.isAnimated = true;
        }

        mat4.translate(tranformMat, tranformMat, [
            -boneDefinition.pivot.x,
            -boneDefinition.pivot.y,
            -boneDefinition.pivot.z,
            0
        ]);

        var invertTransformMat = mat4.create();
        mat4.invert(invertTransformMat, tranformMat);
        bone.tranformMat = tranformMat;
        bone.inverTransforMat = invertTransformMat;
    }