示例#1
0
        public void Draw(Camera camera, MainLogic mainLogic, Vector3 modelPosition, Matrix modelRotation, GameTime gameTime)
        {
            float scale = (float)Math.Pow(10, GameSettings.m_size);

            CheckRenderChanges(scale);

            UpdateViewProjection(camera);

            AnimatedModelShader.EffectPasses pass = AnimatedModelShader.EffectPasses.Unskinned;

            _graphics.BlendState        = BlendState.Opaque;
            _graphics.DepthStencilState = DepthStencilState.Default;

            float meshsize = 0.5f;

            object        loadedModel = mainLogic.modelLoader.LoadedObject;
            AnimatedModel usedModel   = loadedModel != null ? (AnimatedModel)loadedModel : null;

            if (usedModel != null)
            {
                meshsize = 1 / usedModel.Model.Meshes[0].BoundingSphere.Radius;
            }

            {
                object    loadedMaterial = mainLogic.albedoLoader.LoadedObject;
                Texture2D loadedAlbedo   = loadedMaterial != null ? (Texture2D)loadedMaterial : null;
                _animatedModelShader.AlbedoMap = loadedAlbedo;
            }

            {
                object    loadedMaterial = mainLogic.normalLoader.LoadedObject;
                Texture2D loadedNormal   = loadedMaterial != null ? (Texture2D)loadedMaterial : null;
                _animatedModelShader.NormalMap = loadedNormal;

                if (loadedNormal != null)
                {
                    pass = AnimatedModelShader.EffectPasses.UnskinnedNormalMapped;
                }
            }

            {
                object    loadedMaterial = mainLogic.roughnessLoader.LoadedObject;
                Texture2D loadedAlbedo   = loadedMaterial != null ? (Texture2D)loadedMaterial : null;
                _animatedModelShader.RoughnessMap = loadedAlbedo;
            }

            {
                object    loadedMaterial = mainLogic.metallicLoader.LoadedObject;
                Texture2D loadedAlbedo   = loadedMaterial != null ? (Texture2D)loadedMaterial : null;
                _animatedModelShader.MetallicMap = loadedAlbedo;
            }

            {
                object    loadedMaterial = mainLogic.bumpLoader.LoadedObject;
                Texture2D loadedAlbedo   = loadedMaterial != null ? (Texture2D)loadedMaterial : null;
                _animatedModelShader.HeightMap = loadedAlbedo;

                if (loadedAlbedo != null && GameSettings.r_UsePOM)
                {
                    pass = AnimatedModelShader.EffectPasses.UnskinnedNormalMapped;
                }
            }

            Matrix size      = Matrix.CreateScale(scale * meshsize);
            Matrix meshscale = Matrix.CreateScale(meshsize);

            Matrix world = /*meshscale * */ (GameSettings.m_orientationy ? YupOrientation : Matrix.Identity) * modelRotation * Matrix.CreateTranslation(/*-usedModel.Meshes[0].BoundingSphere.Center*/ -modelPosition / (scale * meshsize)) * size;

            _animatedModelShader.AlbedoColor = GameSettings.bgColor;
            _animatedModelShader.Roughness   = GameSettings.m_roughness;
            _animatedModelShader.Metallic    = GameSettings.m_metallic;
            _animatedModelShader.UseLinear   = GameSettings.r_UseLinear;

            if (loadedModel != null)
            {
                if (GameSettings.ao_Enable)
                {
                    //Draw to depth buffer first!
                    _graphics.SetRenderTarget(_linearDepthTarget);
                    _graphics.Clear(Color.White);

                    _graphics.RasterizerState = RasterizerState.CullCounterClockwise;
                    _graphics.BlendState      = BlendState.Opaque;

                    usedModel.Draw(world, _view, _viewProjection, camera.Position, _animatedModelShader,
                                   AnimatedModelShader.EffectPasses.UnskinnedDepth, true);

                    //Draw to the AO Map
                    _graphics.SetRenderTarget(_ambientOcclusionTarget);
                    _graphics.Clear(Color.White);

                    //We draw the depth again onto our ao target, but without color and use only the pixels affected
                    if (GameSettings.ao_UseStencil)
                    {
                        _graphics.BlendState        = _blendNoColorWrite;
                        _graphics.DepthStencilState = _stencilWriteOnly;

                        usedModel.Draw(world, _view, _viewProjection, camera.Position, _animatedModelShader,
                                       AnimatedModelShader.EffectPasses.UnskinnedDepth, true);

                        _graphics.DepthStencilState = _stencilReadOnly;
                    }
                    else
                    {
                        _graphics.DepthStencilState = DepthStencilState.None;
                    }

                    _graphics.BlendState = BlendState.Opaque;
                    _ambientOcclusionShader.DrawAmbientOcclusion();

                    if (GameSettings.ao_UseBlur)
                    {
                        _ambientOcclusionShader.BlurAmbientOcclusion(_ambientOcclusionTarget, _ambientOcclusionBlur0, _ambientOcclusionBlur1);
                    }
                }

                _graphics.SetRenderTarget(null);
                _graphics.DepthStencilState = DepthStencilState.Default;//_stencilWriteOnly;
                usedModel.Draw(world, _view, _viewProjection, camera.Position, _animatedModelShader, pass, !GameSettings.ao_Enable);

                if (usedModel.HasAnimation())
                {
                    if (usedModel.Clips.Count > 0 && GameSettings.m_updateAnimation)
                    {
                        usedModel.Update(gameTime);
                    }

                    if (GameSettings.m_startClip)
                    {
                        usedModel.PlayClip(usedModel.Clips[0], true);
                        GameSettings.m_startClip = false;
                    }
                }
            }
            else
            {
                if (GameSettings.ao_Enable)
                {
                    _graphics.SetRenderTarget(GameSettings.ao_UseBlur ? _ambientOcclusionBlur1 : _ambientOcclusionTarget);
                    _graphics.Clear(Color.White);
                }

                _graphics.SetRenderTarget(null);
                _graphics.BlendState = BlendState.Opaque;
                _graphics.Clear(Color.White);
                _animatedModelShader.DrawMesh(model, world, _view, _viewProjection, camera.Position, pass);
            }

            _graphics.BlendState        = BlendState.Opaque;
            _graphics.RasterizerState   = RasterizerState.CullClockwise;
            _graphics.DepthStencilState = DepthStencilState.Default;
            _skyboxRenderModule.Draw(Matrix.CreateTranslation(camera.Position) * _viewProjection, Vector3.Zero, 300);

            if (GameSettings.r_DrawDepthMap)
            {
                _spriteBatch.Begin();
                _spriteBatch.Draw(_linearDepthTarget, new Rectangle(0, 0, GameSettings.g_ScreenWidth, GameSettings.g_ScreenHeight), Color.White);
                _spriteBatch.End();
            }
            if (GameSettings.r_DrawAoMap)
            {
                _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp);
                _spriteBatch.Draw(GameSettings.ao_UseBlur ? _ambientOcclusionBlur0 : _ambientOcclusionTarget, new Rectangle(0, 0, GameSettings.g_ScreenWidth, GameSettings.g_ScreenHeight), Color.White);
                _spriteBatch.End();
            }

            DrawInteractivityAnimation(gameTime);
        }
示例#2
0
        /// <summary>
        /// Draw the model
        /// </summary>
        public void Draw(Matrix world, Matrix view, Matrix viewProjection, Vector3 cameraPosition, AnimatedModelShader _skinnedShader, AnimatedModelShader.EffectPasses pass, bool computeTransform)
        {
            if (model == null)
            {
                return;
            }

            //
            // Compute all of the bone absolute transforms
            //
            if (modelExtra != null && hasSkinnedVertexType && hasNormals && hasTexCoords)
            {
                if (computeTransform)
                {
                    for (int i = 0; i < bones.Count; i++)
                    {
                        Bone bone = bones[i];
                        bone.ComputeAbsoluteTransform();

                        boneTransforms[i] = bone.AbsoluteTransform;
                    }

                    for (int s = 0; s < modelExtra.Skeleton.Count; s++)
                    {
                        Bone bone = bones[modelExtra.Skeleton[s]];
                        skeleton[s] = bone.SkinTransform * bone.AbsoluteTransform;
                    }
                }

                if (bones.Count > 1)
                {
                    switch (pass)
                    {
                    case AnimatedModelShader.EffectPasses.Unskinned:
                        pass = AnimatedModelShader.EffectPasses.Skinned;
                        break;

                    case AnimatedModelShader.EffectPasses.UnskinnedNormalMapped:
                        pass = AnimatedModelShader.EffectPasses.SkinnedNormalMapped;
                        break;

                    case AnimatedModelShader.EffectPasses.UnskinnedDepth:
                        pass = AnimatedModelShader.EffectPasses.SkinnedDepth;
                        break;
                    }
                }
                _skinnedShader.DrawMesh(model, world, view, viewProjection, cameraPosition, pass, skeleton);
            }
            else
            {
                if (!hasNormals)
                {
                    pass = AnimatedModelShader.EffectPasses.NoNormalUnskinned;
                }

                if (!hasTexCoords)
                {
                    pass = AnimatedModelShader.EffectPasses.NoNormalNoTexUnskinned;
                }

                _skinnedShader.DrawMesh(model, world, view, viewProjection, cameraPosition, pass, null);
            }

            //// Draw the model.
            //foreach (ModelMesh modelMesh in model.Meshes)
            //{
            //    foreach (Effect effect in modelMesh.Effects)
            //    {
            //        if (effect is BasicEffect)
            //        {
            //            BasicEffect beffect = effect as BasicEffect;
            //            beffect.World = boneTransforms[modelMesh.ParentBone.Index] * world;
            //            beffect.View = camera.View;
            //            beffect.Projection = camera.Projection;
            //            beffect.EnableDefaultLighting();
            //            beffect.PreferPerPixelLighting = true;
            //        }

            //        if (effect is SkinnedEffect)
            //        {
            //            SkinnedEffect seffect = effect as SkinnedEffect;
            //            seffect.World = boneTransforms[modelMesh.ParentBone.Index] * world;
            //            seffect.View = camera.View;
            //            seffect.Projection = camera.Projection;
            //            seffect.EnableDefaultLighting();
            //            seffect.PreferPerPixelLighting = true;
            //            seffect.SetBoneTransforms(skeleton);
            //        }
            //    }

            //    modelMesh.Draw();
            //}
        }