Beispiel #1
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 != null)
                {
                    renderer = Skin.SetupSkinnedRenderer(Transform.gameObject, 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 #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;
            }
        }