Beispiel #1
0
            // hkyoo
            public ImportTask(GLTFSkin skin, GLTFAccessor.ImportTask accessorTask) : base(accessorTask)
            {
                task = new Task(() => {
                    if (skin == null)
                    {
                        return;
                    }

                    Result = new ImportResult[1];
                    for (int i = 0; i < Result.Length; i++)
                    {
                        Result[i] = skin.Import(accessorTask.Result);
                    }
                });
            }
Beispiel #2
0
        /// <summary> Set up various components defined in the node. Call after all transforms have been set up </summary>
        public void SetupComponents()
        {
            if (Transform == null)
            {
                Debug.LogWarning("Transform is null. Call CreateTransform before calling SetupComponents");
                return;
            }
            if (this.mesh != -1)
            {
                GLTFMesh glTFMesh = glTFObject.meshes[this.mesh];
                Mesh     mesh     = glTFMesh.GetMesh();
                Renderer renderer;
                if (skin != -1)
                {
                    SkinnedMeshRenderer smr      = Transform.gameObject.AddComponent <SkinnedMeshRenderer>();
                    GLTFSkin            glTFSkin = glTFObject.skins[skin];
                    Transform[]         bones    = new Transform[glTFSkin.joints.Length];
                    for (int i = 0; i < bones.Length; i++)
                    {
                        int      jointNodeIndex = glTFSkin.joints[i];
                        GLTFNode jointNode      = glTFObject.nodes[jointNodeIndex];
                        bones[i] = jointNode.Transform;
                    }
                    smr.bones    = bones;
                    smr.rootBone = bones[0];
                    renderer     = smr;

                    // Bindposes
                    if (glTFSkin.inverseBindMatrices != -1)
                    {
                        Matrix4x4   m         = glTFObject.nodes[0].Transform.localToWorldMatrix;
                        Matrix4x4[] bindPoses = new Matrix4x4[glTFSkin.joints.Length];
                        for (int i = 0; i < glTFSkin.joints.Length; i++)
                        {
                            bindPoses[i] = glTFObject.nodes[glTFSkin.joints[i]].Transform.worldToLocalMatrix * m;
                        }
                        mesh.bindposes = bindPoses;
                    }
                    smr.sharedMesh = mesh;
                }
                else
                {
                    MeshRenderer mr = Transform.gameObject.AddComponent <MeshRenderer>();
                    MeshFilter   mf = Transform.gameObject.AddComponent <MeshFilter>();
                    renderer      = mr;
                    mf.sharedMesh = mesh;
                }

                //Materials
                Material[] materials = new Material[glTFMesh.primitives.Count];
                for (int i = 0; i < glTFMesh.primitives.Count; i++)
                {
                    GLTFPrimitive primitive = glTFMesh.primitives[i];
                    // Create material if id is positive or 0
                    if (primitive.material != -1)
                    {
                        materials[i] = glTFObject.materials[primitive.material].GetMaterial();
                    }
                }
                renderer.materials = materials;
            }
        }
Beispiel #3
0
 protected override bool OnLoad()
 {
     // Name
     if (string.IsNullOrEmpty(name))
     {
         if (IsJoint())
         {
             Name = "joint" + glTFObject.nodes.IndexOf(this);
         }
         else
         {
             Name = "node" + glTFObject.nodes.IndexOf(this);
         }
     }
     else
     {
         Name = name;
     }
     // References
     if (skin != -1)
     {
         Skin = glTFObject.skins[skin];
     }
     // Transform
     if (matrix != null)
     {
         Matrix4x4 trs = new Matrix4x4(
             new Vector4(matrix[0], matrix[1], matrix[2], matrix[3]),
             new Vector4(matrix[4], matrix[5], matrix[6], matrix[7]),
             new Vector4(matrix[8], matrix[9], matrix[10], matrix[11]),
             new Vector4(matrix[12], matrix[13], matrix[14], matrix[15])
             );
         Vector3 pos = trs.GetColumn(3);
         pos.z = -pos.z;
         Quaternion rot = trs.rotation;
         rot           = new Quaternion(rot.x, rot.y, -rot.z, -rot.w);
         LocalPosition = pos;
         LocalRotation = rot;
         LocalScale    = trs.lossyScale;
     }
     else
     {
         if (translation != null)
         {
             LocalPosition = new Vector3(translation[0], translation[1], -translation[2]);
         }
         else
         {
             LocalPosition = Vector3.zero;
         }
         if (rotation != null)
         {
             LocalRotation = new Quaternion(rotation[0], rotation[1], -rotation[2], -rotation[3]);
         }
         else
         {
             LocalRotation = new Quaternion(0, 0, 0, 1);
         }
         if (scale != null)
         {
             LocalScale = new Vector3(scale[0], scale[1], scale[2]);
         }
         else
         {
             LocalScale = Vector3.one;
         }
     }
     return(true);
 }