Esempio n. 1
0
        internal ModelInstance(Model model, GraphicsDevice graphicsDevice)
        {
            Model = model;

            _graphicsDevice = graphicsDevice;

            ModelBoneInstances = new ModelBoneInstance[model.Bones.Length];
            for (var i = 0; i < model.Bones.Length; i++)
            {
                ModelBoneInstances[i] = new ModelBoneInstance(model.Bones[i]);
            }

            RelativeBoneTransforms = new Matrix4x4[model.Bones.Length];
            AbsoluteBoneTransforms = new Matrix4x4[model.Bones.Length];

            BoneVisibilities = new bool[model.Bones.Length];
            for (var i = 0; i < model.Bones.Length; i++)
            {
                BoneVisibilities[i] = true;
            }

            _hasSkinnedMeshes = model.Meshes.Any(x => x.Skinned);

            if (_hasSkinnedMeshes)
            {
                SkinningBuffer = AddDisposable(graphicsDevice.ResourceFactory.CreateBuffer(
                                                   new BufferDescription(
                                                       (uint)(Matrix4x3.SizeInBytes * model.Bones.Length),
                                                       BufferUsage.StructuredBufferReadOnly | BufferUsage.Dynamic,
                                                       Matrix4x3.SizeInBytes)));

                _skinningBones = new Matrix4x3[model.Bones.Length];
            }

            AnimationInstances = new List <AnimationInstance>();
            foreach (var animation in model.Animations)
            {
                AnimationInstances.Add(new AnimationInstance(this, animation));
            }
        }
Esempio n. 2
0
        internal ModelInstance(Model model, GraphicsDevice graphicsDevice)
        {
            Model = model;

            _graphicsDevice = graphicsDevice;

            ModelBoneInstances = new ModelBoneInstance[model.BoneHierarchy.Bones.Length];
            for (var i = 0; i < model.BoneHierarchy.Bones.Length; i++)
            {
                ModelBoneInstances[i] = new ModelBoneInstance(model.BoneHierarchy.Bones[i]);
            }

            RelativeBoneTransforms = new Matrix4x4[model.BoneHierarchy.Bones.Length];
            AbsoluteBoneTransforms = new Matrix4x4[model.BoneHierarchy.Bones.Length];

            BoneVisibilities = new bool[model.BoneHierarchy.Bones.Length];
            for (var i = 0; i < model.BoneHierarchy.Bones.Length; i++)
            {
                BoneVisibilities[i] = true;
            }

            _hasSkinnedMeshes = model.SubObjects.Any(x => x.RenderObject.Skinned);

            if (_hasSkinnedMeshes)
            {
                SkinningBuffer = AddDisposable(graphicsDevice.ResourceFactory.CreateBuffer(
                                                   new BufferDescription(
                                                       (uint)(64 * model.BoneHierarchy.Bones.Length),
                                                       BufferUsage.StructuredBufferReadOnly | BufferUsage.Dynamic,
                                                       64,
                                                       true)));

                _skinningBones = new Matrix4x4[model.BoneHierarchy.Bones.Length];
            }

            AnimationInstances = new List <AnimationInstance>();
        }
Esempio n. 3
0
        internal ModelInstance(Model model, AssetLoadContext loadContext)
        {
            Model = model;

            _graphicsDevice = loadContext.GraphicsDevice;

            ModelBoneInstances = new ModelBoneInstance[model.BoneHierarchy.Bones.Length];
            for (var i = 0; i < model.BoneHierarchy.Bones.Length; i++)
            {
                ModelBoneInstances[i] = new ModelBoneInstance(model.BoneHierarchy.Bones[i]);
            }

            RelativeBoneTransforms = new Matrix4x4[model.BoneHierarchy.Bones.Length];
            AbsoluteBoneTransforms = new Matrix4x4[model.BoneHierarchy.Bones.Length];

            BoneVisibilities      = new bool[model.BoneHierarchy.Bones.Length];
            BoneFrameVisibilities = new bool[model.BoneHierarchy.Bones.Length];

            for (var i = 0; i < model.BoneHierarchy.Bones.Length; i++)
            {
                BoneVisibilities[i]      = true;
                BoneFrameVisibilities[i] = true;
            }

            _hasSkinnedMeshes = model.SubObjects.Any(x => x.RenderObject.Skinned);

            DeviceBuffer skinningBuffer;

            if (_hasSkinnedMeshes)
            {
                _skinningBuffer = skinningBuffer = AddDisposable(_graphicsDevice.ResourceFactory.CreateBuffer(
                                                                     new BufferDescription(
                                                                         (uint)(64 * model.BoneHierarchy.Bones.Length),
                                                                         BufferUsage.StructuredBufferReadOnly | BufferUsage.Dynamic,
                                                                         64,
                                                                         true)));

                _skinningBones = new Matrix4x4[model.BoneHierarchy.Bones.Length];
            }
            else
            {
                skinningBuffer = loadContext.StandardGraphicsResources.GetNullStructuredBuffer(64);
            }

            SkinningBufferResourceSet = AddDisposable(loadContext.ShaderResources.Mesh.CreateSkinningResourceSet(skinningBuffer));

            AnimationInstances = new List <AnimationInstance>();

            BeforeRenderDelegates      = new BeforeRenderDelegate[model.SubObjects.Length][];
            BeforeRenderDelegatesDepth = new BeforeRenderDelegate[model.SubObjects.Length][];

            for (var i = 0; i < model.SubObjects.Length; i++)
            {
                var mesh = model.SubObjects[i].RenderObject;

                BeforeRenderDelegates[i]      = new BeforeRenderDelegate[mesh.MeshParts.Count];
                BeforeRenderDelegatesDepth[i] = new BeforeRenderDelegate[mesh.MeshParts.Count];

                for (var j = 0; j < mesh.MeshParts.Count; j++)
                {
                    var meshBeforeRender      = mesh.BeforeRenderDelegates[j];
                    var meshBeforeRenderDepth = mesh.BeforeRenderDelegatesDepth[j];

                    BeforeRenderDelegates[i][j] = (cl, context) =>
                    {
                        cl.SetGraphicsResourceSet(8, SkinningBufferResourceSet);
                        meshBeforeRender(cl, context);
                    };

                    BeforeRenderDelegatesDepth[i][j] = (cl, context) =>
                    {
                        cl.SetGraphicsResourceSet(3, SkinningBufferResourceSet);
                        meshBeforeRenderDepth(cl, context);
                    };
                }
            }
        }
Esempio n. 4
0
        internal ModelInstance(Model model, AssetLoadContext loadContext)
        {
            Model = model;

            _graphicsDevice = loadContext.GraphicsDevice;

            ModelBoneInstances = new ModelBoneInstance[model.BoneHierarchy.Bones.Length];
            for (var i = 0; i < model.BoneHierarchy.Bones.Length; i++)
            {
                ModelBoneInstances[i] = new ModelBoneInstance(model.BoneHierarchy.Bones[i]);
            }

            RelativeBoneTransforms = new Matrix4x4[model.BoneHierarchy.Bones.Length];
            AbsoluteBoneTransforms = new Matrix4x4[model.BoneHierarchy.Bones.Length];

            BoneVisibilities      = new bool[model.BoneHierarchy.Bones.Length];
            BoneFrameVisibilities = new bool[model.BoneHierarchy.Bones.Length];

            for (var i = 0; i < model.BoneHierarchy.Bones.Length; i++)
            {
                BoneVisibilities[i]      = true;
                BoneFrameVisibilities[i] = true;
            }

            if (model.HasSkinnedMeshes)
            {
                _skinningBuffer = SkinningBuffer = AddDisposable(_graphicsDevice.ResourceFactory.CreateBuffer(
                                                                     new BufferDescription(
                                                                         (uint)(64 * model.BoneHierarchy.Bones.Length),
                                                                         BufferUsage.StructuredBufferReadOnly | BufferUsage.Dynamic,
                                                                         64,
                                                                         true)));

                _skinningBones = new Matrix4x4[model.BoneHierarchy.Bones.Length];
            }
            else
            {
                SkinningBuffer = loadContext.StandardGraphicsResources.GetNullStructuredBuffer(64);
            }

            AnimationInstances = new List <AnimationInstance>();

            RenderItemConstantsBufferPS = AddDisposable(new ConstantBuffer <MeshShaderResources.RenderItemConstantsPS>(_graphicsDevice, "RenderItemConstantsPS"));

            RenderItemConstantsBufferPS.Value = new MeshShaderResources.RenderItemConstantsPS
            {
                HouseColor = Vector3.One,
                Opacity    = 1.0f,
                TintColor  = Vector3.One
            };
            RenderItemConstantsBufferPS.Update(_graphicsDevice);

            MeshInstances = new ModelMeshInstance[model.SubObjects.Length];

            for (var i = 0; i < model.SubObjects.Length; i++)
            {
                var renderObject = model.SubObjects[i].RenderObject;

                if (!(renderObject is ModelMesh mesh))
                {
                    continue;
                }

                MeshInstances[i] = AddDisposable(
                    new ModelMeshInstance(
                        mesh,
                        this,
                        loadContext));
            }

            UnknownBools = new bool[model.SubObjects.Length];
        }