Ejemplo n.º 1
0
        private void _AddInstances(IVisualNodeContainer dst, Schema2SceneBuilder context)
        {
            if (_Children.Count < _GpuMinCount)
            {
                foreach (var srcChild in _Children)
                {
                    if (srcChild.Content is SCHEMA2NODE srcOperator)
                    {
                        var dstNode = dst.CreateNode();
                        dstNode.Name           = srcChild.Name;
                        dstNode.Extras         = srcChild.Extras.DeepClone();
                        dstNode.LocalTransform = srcChild.ChildTransform;

                        srcOperator.ApplyTo(dstNode, context);

                        System.Diagnostics.Debug.Assert(dstNode.WorldMatrix == srcChild.GetPoseWorldMatrix(), "Transform mismatch!");
                    }
                }
            }
            else
            {
                if (_Children[0].Content is SCHEMA2NODE srcOperator)
                {
                    var xforms = _Children
                                 .Select(item => item.ChildTransform)
                                 .ToList();

                    if (!(dst is Node dstNode))
                    {
                        dstNode = dst.CreateNode();
                    }

                    System.Diagnostics.Debug.Assert(dstNode.Mesh == null);
                    System.Diagnostics.Debug.Assert(dstNode.Skin == null);
                    System.Diagnostics.Debug.Assert(dstNode.GetGpuInstancing() == null);

                    srcOperator.ApplyTo(dstNode, context);

                    dstNode
                    .UseGpuInstancing()
                    .WithInstanceAccessors(xforms);

                    #if DEBUG
                    var dstInstances = dstNode.GetGpuInstancing();
                    for (int i = 0; i < _Children.Count; ++i)
                    {
                        var srcXform = _Children[i].GetPoseWorldMatrix();
                        var dstXform = dstInstances.GetWorldMatrix(i);

                        System.Diagnostics.Debug.Assert(srcXform == dstXform, "Transform mismatch!");
                    }
                    #endif
                }
            }
        }
Ejemplo n.º 2
0
        void SCHEMA2NODE.Setup(Node dstNode, Schema2SceneBuilder context)
        {
            if (!(_Target is SCHEMA2NODE schema2Target))
            {
                return;
            }

            schema2Target.Setup(dstNode, context);

            // setup morphs here!
        }
Ejemplo n.º 3
0
        void SCHEMA2NODE.ApplyTo(Node dstNode, Schema2SceneBuilder context)
        {
            // we try to assign our mesh to the target node.
            // but if the target node already has a mesh, we need to create
            // a child node that will contain our mesh.

            if (dstNode.Mesh != null)
            {
                dstNode = dstNode.CreateNode();
            }
            dstNode.Mesh = context.GetMesh(_Mesh);
        }
Ejemplo n.º 4
0
        void SCHEMA2SCENE.ApplyTo(Scene dstScene, Schema2SceneBuilder context)
        {
            if (!(Content is SCHEMA2NODE schema2Target))
            {
                return;
            }

            // a skinned mesh is controlled indirectly by its bones,
            // so we need to create a dummy container for it:
            var skinnedMeshNode = dstScene.CreateNode();

            skinnedMeshNode.Name   = _NodeName;
            skinnedMeshNode.Extras = _NodeExtras;

            if (_MeshPoseWorldTransform.HasValue)
            {
                var dstNodes = new Node[_Joints.Count];

                for (int i = 0; i < dstNodes.Length; ++i)
                {
                    var(joints, inverseBindMatrix) = _Joints[i];

                    System.Diagnostics.Debug.Assert(!inverseBindMatrix.HasValue);

                    dstNodes[i] = context.GetNode(joints);
                }

                #if DEBUG
                for (int i = 0; i < dstNodes.Length; ++i)
                {
                    var(joints, inverseBindMatrix) = _Joints[i];
                    System.Diagnostics.Debug.Assert(dstNodes[i].WorldMatrix == joints.WorldMatrix);
                }
                #endif

                skinnedMeshNode.WithSkinBinding(_MeshPoseWorldTransform.Value.Matrix, dstNodes);
            }
            else
            {
                var skinnedJoints = _Joints
                                    .Select(j => (context.GetNode(j.Joint), j.InverseBindMatrix.Value))
                                    .ToArray();

                skinnedMeshNode.WithSkinBinding(skinnedJoints);
            }

            // set skeleton
            // var root = _Joints[0].Joint.Root;
            // skinnedMeshNode.Skin.Skeleton = context.GetNode(root);

            schema2Target.ApplyTo(skinnedMeshNode, context);
            Schema2SceneBuilder.SetMorphAnimation(skinnedMeshNode, this.Morphings);
        }
Ejemplo n.º 5
0
        void SCHEMA2SCENE.ApplyTo(Scene dstScene, Schema2SceneBuilder context)
        {
            if (Content is SCHEMA2NODE schema2Target)
            {
                var dstNode = context.GetNode(_Node);

                schema2Target.ApplyTo(dstNode, context);

                Schema2SceneBuilder.SetMorphAnimation(dstNode, this.Morphings);

                System.Diagnostics.Debug.Assert(dstNode.WorldMatrix == this.GetPoseWorldMatrix(), "Transform mismatch!");
            }
        }
Ejemplo n.º 6
0
        void SCHEMA2SCENE.Setup(Scene dstScene, Schema2SceneBuilder context)
        {
            if (!(Content is SCHEMA2NODE schema2Target))
            {
                return;
            }

            var dstNode = context.GetNode(_Node);

            schema2Target.Setup(dstNode, context);

            context.SetMorphAnimation(dstNode, this.Morphings);
        }
Ejemplo n.º 7
0
        void SCHEMA2SCENE.Setup(Scene dstScene, Schema2SceneBuilder context)
        {
            if (!(Content is SCHEMA2NODE schema2Target))
            {
                return;
            }

            var dstNode = dstScene.CreateNode();

            dstNode.LocalMatrix = _WorldTransform;

            schema2Target.Setup(dstNode, context);
        }
Ejemplo n.º 8
0
        void SCHEMA2SCENE.Setup(Scene dstScene, Schema2SceneBuilder context)
        {
            if (!(Content is SCHEMA2NODE schema2Target))
            {
                return;
            }

            var skinnedMeshNode = dstScene.CreateNode();

            skinnedMeshNode.Name = _NodeName;

            if (_TargetBindMatrix.HasValue)
            {
                var dstNodes = new Node[_Joints.Count];

                for (int i = 0; i < dstNodes.Length; ++i)
                {
                    var(joints, inverseBindMatrix) = _Joints[i];

                    System.Diagnostics.Debug.Assert(!inverseBindMatrix.HasValue);

                    dstNodes[i] = context.GetNode(joints);
                }

                #if DEBUG
                for (int i = 0; i < dstNodes.Length; ++i)
                {
                    var(joints, inverseBindMatrix) = _Joints[i];
                    System.Diagnostics.Debug.Assert(dstNodes[i].WorldMatrix == joints.WorldMatrix);
                }
                #endif

                skinnedMeshNode.WithSkinBinding(_TargetBindMatrix.Value, dstNodes);
            }
            else
            {
                var skinnedJoints = _Joints
                                    .Select(j => (context.GetNode(j.Joints), j.InverseBindMatrix.Value))
                                    .ToArray();

                skinnedMeshNode.WithSkinBinding(skinnedJoints);
            }

            // set skeleton
            // var root = _Joints[0].Joint.Root;
            // skinnedMeshNode.Skin.Skeleton = context.GetNode(root);

            schema2Target.Setup(skinnedMeshNode, context);

            context.SetMorphAnimation(skinnedMeshNode, this.Morphings);
        }
Ejemplo n.º 9
0
        void SCHEMA2SCENE.Setup(Scene dstScene, Schema2SceneBuilder context)
        {
            if (!(Content is SCHEMA2NODE schema2Target))
            {
                return;
            }

            var dstNode = dstScene.CreateNode();

            dstNode.Name        = _NodeName;
            dstNode.LocalMatrix = _WorldTransform;

            schema2Target.Setup(dstNode, context);

            context.SetMorphAnimation(dstNode, this.Morphings);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Converts this <see cref="SceneBuilder"/> instance into a <see cref="ModelRoot"/> instance.
        /// </summary>
        /// <param name="useStridedBuffers">True to generate strided vertex buffers whenever possible.</param>
        /// <returns>A new <see cref="ModelRoot"/> instance.</returns>
        public ModelRoot ToSchema2(bool useStridedBuffers = true)
        {
            var context = new Schema2SceneBuilder();

            var dstModel = ModelRoot.CreateModel();

            context.AddGeometryResources(dstModel, new[] { this }, useStridedBuffers);

            var dstScene = dstModel.UseScene(0);

            dstScene.Name = this.Name;

            context.AddScene(dstScene, this);

            return(dstModel);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Converts this <see cref="SceneBuilder"/> instance into a <see cref="ModelRoot"/> instance.
        /// </summary>
        /// <param name="useStridedBuffers">True to generate strided vertex buffers whenever possible.</param>
        /// <returns>A new <see cref="ModelRoot"/> instance.</returns>
        public ModelRoot ToGltf2(SceneBuilderSchema2Settings settings)
        {
            var context = new Schema2SceneBuilder();

            var dstModel = ModelRoot.CreateModel();

            context.AddGeometryResources(dstModel, new[] { this }, settings);

            var dstScene = dstModel.UseScene(0);

            dstScene.Name = this.Name;

            context.AddScene(dstScene, this);

            dstModel.DefaultScene = dstScene;

            return(dstModel);
        }
Ejemplo n.º 12
0
        public void ApplyTo(Node dstNode, Schema2SceneBuilder context)
        {
            if (_Camera is CameraBuilder.Orthographic ortho)
            {
                if (dstNode.Camera != null)
                {
                    dstNode = dstNode.CreateNode();
                }
                dstNode.WithOrthographicCamera(ortho.XMag, ortho.YMag, ortho.ZNear, ortho.ZFar);
            }

            if (_Camera is CameraBuilder.Perspective persp)
            {
                if (dstNode.Camera != null)
                {
                    dstNode = dstNode.CreateNode();
                }
                dstNode.WithPerspectiveCamera(persp.AspectRatio, persp.VerticalFOV, persp.ZNear, persp.ZFar);
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Convertes a collection of <see cref="SceneBuilder"/> instances to a single <see cref="ModelRoot"/> instance.
        /// </summary>
        /// <param name="srcScenes">A collection of scenes</param>
        /// <param name="useStridedBuffers">True to generate strided vertex buffers whenever possible.</param>
        /// <returns>A new <see cref="ModelRoot"/> instance.</returns>
        public static ModelRoot ToSchema2(IEnumerable <SceneBuilder> srcScenes, SceneBuilderSchema2Settings settings)
        {
            Guard.NotNull(srcScenes, nameof(srcScenes));

            var context = new Schema2SceneBuilder();

            var dstModel = ModelRoot.CreateModel();

            context.AddGeometryResources(dstModel, srcScenes, settings);

            foreach (var srcScene in srcScenes)
            {
                var dstScene = dstModel.UseScene(dstModel.LogicalScenes.Count);

                dstScene.Name = srcScene.Name;

                context.AddScene(dstScene, srcScene);
            }

            return(dstModel);
        }
Ejemplo n.º 14
0
        public void ApplyTo(Scene dstScene, Schema2SceneBuilder context)
        {
            System.Diagnostics.Debug.Assert(_Children.Count > 0);

            if (_ParentNode == null)
            {
                _AddInstances(dstScene, context);
                return;
            }
            else
            {
                var dstNode = context.GetNode(_ParentNode);

                if (Schema2SceneBuilder.HasContent(dstNode))
                {
                    dstNode = dstNode.CreateNode();
                }

                _AddInstances(dstNode, context);
                return;
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Converts a collection of <see cref="SceneBuilder"/> instances to a single <see cref="ModelRoot"/> instance.
        /// </summary>
        /// <param name="srcScenes">A collection of scenes</param>
        /// <param name="settings">Conversion settings.</param>
        /// <returns>A new <see cref="ModelRoot"/> instance.</returns>
        public static ModelRoot ToGltf2(IEnumerable <SceneBuilder> srcScenes, SceneBuilderSchema2Settings settings)
        {
            Guard.NotNull(srcScenes, nameof(srcScenes));

            var context = new Schema2SceneBuilder();

            var dstModel = ModelRoot.CreateModel();

            context.AddGeometryResources(dstModel, srcScenes, settings);

            foreach (var srcScene in srcScenes)
            {
                var dstScene = dstModel.UseScene(dstModel.LogicalScenes.Count);
                srcScene.TryCopyNameAndExtrasTo(dstScene);

                context.AddScene(dstScene, srcScene);
            }

            dstModel.DefaultScene = dstModel.LogicalScenes[0];

            return(dstModel);
        }
Ejemplo n.º 16
0
        public void ApplyTo(Node dstNode, Schema2SceneBuilder context)
        {
            if (_Light is LightBuilder.Directional directional)
            {
                if (dstNode.Camera != null)
                {
                    dstNode = dstNode.CreateNode();
                }
                dstNode.PunctualLight           = dstNode.LogicalParent.CreatePunctualLight(PunctualLightType.Directional);
                dstNode.PunctualLight.Color     = directional.Color;
                dstNode.PunctualLight.Intensity = directional.Intensity;
            }

            if (_Light is LightBuilder.Point point)
            {
                if (dstNode.Camera != null)
                {
                    dstNode = dstNode.CreateNode();
                }
                dstNode.PunctualLight           = dstNode.LogicalParent.CreatePunctualLight(PunctualLightType.Point);
                dstNode.PunctualLight.Color     = point.Color;
                dstNode.PunctualLight.Intensity = point.Intensity;
                dstNode.PunctualLight.Range     = point.Range;
            }

            if (_Light is LightBuilder.Spot spot)
            {
                if (dstNode.Camera != null)
                {
                    dstNode = dstNode.CreateNode();
                }
                dstNode.PunctualLight           = dstNode.LogicalParent.CreatePunctualLight(PunctualLightType.Spot);
                dstNode.PunctualLight.Color     = spot.Color;
                dstNode.PunctualLight.Intensity = spot.Intensity;
                dstNode.PunctualLight.Range     = spot.Range;
                dstNode.PunctualLight.SetSpotCone(spot.InnerConeAngle, spot.OuterConeAngle);
            }
        }
Ejemplo n.º 17
0
 void SCHEMA2NODE.Setup(Node dstNode, Schema2SceneBuilder context)
 {
     dstNode.WithPerspectiveCamera(_AspectRatio, _FovY, _ZNear, _ZFar);
 }
Ejemplo n.º 18
0
 void SCHEMA2NODE.Setup(Node dstNode, Schema2SceneBuilder context)
 {
     dstNode.Mesh = context.GetMesh(_Mesh);
 }
Ejemplo n.º 19
0
 void SCHEMA2NODE.Setup(Node dstNode, Schema2SceneBuilder context)
 {
     dstNode.WithOrthographicCamera(_XMag, _YMag, _ZNear, _ZFar);
 }