private void BuildSkinnedMesh(GameObject nodeObj, GLTF.Schema.Skin skin, int meshIndex, int primitiveIndex)
        {
            if (skin.InverseBindMatrices.Value.Count == 0)
            {
                return;
            }

            SkinnedMeshRenderer skinMesh = nodeObj.AddComponent <SkinnedMeshRenderer>();

            skinMesh.sharedMesh     = _assetManager.getMesh(meshIndex, primitiveIndex);
            skinMesh.sharedMaterial = _assetManager.getMaterial(meshIndex, primitiveIndex);

            byte[]           bufferData       = _assetCache.BufferCache[skin.InverseBindMatrices.Value.BufferView.Value.Buffer.Id];
            NumericArray     content          = new NumericArray();
            List <Matrix4x4> bindPoseMatrices = new List <Matrix4x4>();

            GLTF.Math.Matrix4x4[] inverseBindMatrices = skin.InverseBindMatrices.Value.AsMatrixArray(ref content, bufferData);
            foreach (GLTF.Math.Matrix4x4 mat in inverseBindMatrices)
            {
                bindPoseMatrices.Add(mat.ToUnityMatrix().switchHandedness());
            }

            skinMesh.sharedMesh.bindposes = bindPoseMatrices.ToArray();
            if (skin.Skeleton != null && _importedObjects.ContainsKey(skin.Skeleton.Id))
            {
                skinMesh.rootBone = skin.Skeleton != null ? _importedObjects[skin.Skeleton.Id].transform : null;
            }
        }
        private void LoadSkin(GLTF.Schema.Skin skin, int index)
        {
            Transform[] boneList = new Transform[skin.Joints.Count];
            for (int i = 0; i < skin.Joints.Count; ++i)
            {
                boneList[i] = _importedObjects[skin.Joints[i].Id].transform;
            }

            foreach (SkinnedMeshRenderer skinMesh in _skinIndexToGameObjects[index])
            {
                skinMesh.bones = boneList;
            }
        }
Ejemplo n.º 3
0
        public static GLTFRoot Deserialize(TextReader textReader)
        {
            var jsonReader = new JsonTextReader(textReader);
            var root       = new GLTFRoot();

            if (jsonReader.Read() && jsonReader.TokenType != JsonToken.StartObject)
            {
                throw new Exception("gltf json must be an object");
            }

            while (jsonReader.Read() && jsonReader.TokenType == JsonToken.PropertyName)
            {
                var curProp = jsonReader.Value.ToString();

                switch (curProp)
                {
                case "extensionsUsed":
                    root.ExtensionsUsed = jsonReader.ReadStringList();
                    break;

                case "extensionsRequired":
                    root.ExtensionsRequired = jsonReader.ReadStringList();
                    break;

                case "accessors":
                    root.Accessors = jsonReader.ReadList(() => Accessor.Deserialize(root, jsonReader));
                    break;

                case "animations":
                    root.Animations = jsonReader.ReadList(() => Animation.Deserialize(root, jsonReader));
                    break;

                case "asset":
                    root.Asset = Asset.Deserialize(root, jsonReader);
                    break;

                case "buffers":
                    root.Buffers = jsonReader.ReadList(() => Buffer.Deserialize(root, jsonReader));
                    break;

                case "bufferViews":
                    root.BufferViews = jsonReader.ReadList(() => BufferView.Deserialize(root, jsonReader));
                    break;

                case "cameras":
                    root.Cameras = jsonReader.ReadList(() => Camera.Deserialize(root, jsonReader));
                    break;

                case "images":
                    root.Images = jsonReader.ReadList(() => Image.Deserialize(root, jsonReader));
                    break;

                case "materials":
                    root.Materials = jsonReader.ReadList(() => Material.Deserialize(root, jsonReader));
                    break;

                case "meshes":
                    root.Meshes = jsonReader.ReadList(() => Mesh.Deserialize(root, jsonReader));
                    break;

                case "nodes":
                    root.Nodes = jsonReader.ReadList(() => Node.Deserialize(root, jsonReader));
                    break;

                case "samplers":
                    root.Samplers = jsonReader.ReadList(() => Sampler.Deserialize(root, jsonReader));
                    break;

                case "scene":
                    root.Scene = SceneId.Deserialize(root, jsonReader);
                    break;

                case "scenes":
                    root.Scenes = jsonReader.ReadList(() => GLTF.Schema.Scene.Deserialize(root, jsonReader));
                    break;

                case "skins":
                    root.Skins = jsonReader.ReadList(() => Skin.Deserialize(root, jsonReader));
                    break;

                case "textures":
                    root.Textures = jsonReader.ReadList(() => Texture.Deserialize(root, jsonReader));
                    break;

                default:
                    root.DefaultPropertyDeserializer(root, jsonReader);
                    break;
                }
            }

            return(root);
        }