예제 #1
0
        public static GLTFAnimation Deserialize(GLTFRoot root, JsonReader reader)
        {
            var animation = new GLTFAnimation();

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

                switch (curProp)
                {
                case "channels":
                    animation.Channels = reader.ReadList(() => AnimationChannel.Deserialize(root, reader));
                    break;

                case "samplers":
                    animation.Samplers = reader.ReadList(() => AnimationSampler.Deserialize(root, reader));
                    break;

                default:
                    animation.DefaultPropertyDeserializer(root, reader);
                    break;
                }
            }

            return(animation);
        }
예제 #2
0
        public static AnimationChannel Deserialize(GLTFRoot root, JsonReader reader)
        {
            var animationChannel = new AnimationChannel();

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

                switch (curProp)
                {
                case "sampler":
                    animationChannel.Sampler = SamplerId.Deserialize(root, reader);
                    break;

                case "target":
                    animationChannel.Target = AnimationChannelTarget.Deserialize(root, reader);
                    break;

                default:
                    animationChannel.DefaultPropertyDeserializer(root, reader);
                    break;
                }
            }

            return(animationChannel);
        }
예제 #3
0
        public AnimationChannel(AnimationChannel animationChannel, GLTFRoot root) : base(animationChannel)
        {
            if (animationChannel == null)
            {
                return;
            }

            Sampler = new SamplerId(animationChannel.Sampler, root);
            Target  = new AnimationChannelTarget(animationChannel.Target, root);
        }
        private void addGLTFChannelDataToClip(GLTF.Schema.AnimationChannel channel, AnimationClip clip)
        {
            int animatedNodeIndex = channel.Target.Node.Id;

            if (!_importedObjects.ContainsKey(animatedNodeIndex))
            {
                Debug.Log("Node '" + animatedNodeIndex + "' found for animation, aborting.");
            }

            Transform animatedNode = _importedObjects[animatedNodeIndex].transform;
            string    nodePath     = AnimationUtility.CalculateTransformPath(animatedNode, _sceneObject.transform);

            bool isStepInterpolation = channel.Sampler.Value.Interpolation != InterpolationType.LINEAR;

            byte[]  timeBufferData = _assetCache.BufferCache[channel.Sampler.Value.Output.Value.BufferView.Value.Buffer.Id];
            float[] times          = GLTFHelpers.ParseKeyframeTimes(channel.Sampler.Value.Input.Value, timeBufferData);

            if (channel.Target.Path == GLTFAnimationChannelPath.translation || channel.Target.Path == GLTFAnimationChannelPath.scale)
            {
                byte[] bufferData             = _assetCache.BufferCache[channel.Sampler.Value.Output.Value.BufferView.Value.Buffer.Id];
                GLTF.Math.Vector3[] keyValues = GLTFHelpers.ParseVector3Keyframes(channel.Sampler.Value.Output.Value, bufferData);
                if (keyValues == null)
                {
                    return;
                }

                Vector3[]        values        = keyValues.ToUnityVector3();
                AnimationCurve[] vector3Curves = GLTFUtils.createCurvesFromArrays(times, values, isStepInterpolation, channel.Target.Path == GLTFAnimationChannelPath.translation);

                if (channel.Target.Path == GLTFAnimationChannelPath.translation)
                {
                    GLTFUtils.addTranslationCurvesToClip(vector3Curves, nodePath, ref clip);
                }
                else
                {
                    GLTFUtils.addScaleCurvesToClip(vector3Curves, nodePath, ref clip);
                }
            }
            else if (channel.Target.Path == GLTFAnimationChannelPath.rotation)
            {
                byte[]           bufferData     = _assetCache.BufferCache[channel.Sampler.Value.Output.Value.BufferView.Value.Buffer.Id];
                Vector4[]        values         = GLTFHelpers.ParseRotationKeyframes(channel.Sampler.Value.Output.Value, bufferData).ToUnityVector4();
                AnimationCurve[] rotationCurves = GLTFUtils.createCurvesFromArrays(times, values, isStepInterpolation);

                GLTFUtils.addRotationCurvesToClip(rotationCurves, nodePath, ref clip);
            }
            else if (channel.Target.Path == GLTFAnimationChannelPath.weights)
            {
                List <string> morphTargets = new List <string>();
                int           meshIndex    = _root.Nodes[animatedNodeIndex].Mesh.Id;
                for (int i = 0; i < _root.Meshes[meshIndex].Primitives[0].Targets.Count; ++i)
                {
                    morphTargets.Add(GLTFUtils.buildBlendShapeName(meshIndex, i));
                }

                byte[]           bufferData  = _assetCache.BufferCache[channel.Sampler.Value.Output.Value.BufferView.Value.Buffer.Id];
                float[]          values      = GLTFHelpers.ParseKeyframeTimes(channel.Sampler.Value.Output.Value, bufferData);
                AnimationCurve[] morphCurves = GLTFUtils.buildMorphAnimationCurves(times, values, morphTargets.Count);

                GLTFUtils.addMorphAnimationCurvesToClip(morphCurves, nodePath, morphTargets.ToArray(), ref clip);
            }
            else
            {
                Debug.Log("Unsupported animation channel target: " + channel.Target.Path);
            }
        }