示例#1
0
        protected virtual void WriteDataJson(JsonWriter writer)
        {
            writer.WritePropertyName(nameof(Name));
            writer.WriteValue(Name);

            writer.WritePropertyName(nameof(DiffuseColor));
            DiffuseColor.WriteJson(writer);

            writer.WritePropertyName(nameof(AmbientColor));
            AmbientColor.WriteJson(writer);

            writer.WritePropertyName(nameof(SpecularColor));
            SpecularColor.WriteJson(writer);

            writer.WritePropertyName(nameof(EmissiveColor));
            EmissiveColor.WriteJson(writer);

            writer.WritePropertyName(nameof(Opacity));
            writer.WriteRawValue(Opacity.ToRoundTripString());

            writer.WritePropertyName(nameof(SpecularExponent));
            writer.WriteRawValue(SpecularExponent.ToRoundTripString());

            writer.WritePropertyName(nameof(FaceMap));
            writer.WriteValue(FaceMap);

            writer.WritePropertyName(nameof(TwoSided));
            writer.WriteValue(TwoSided);
        }
示例#2
0
        /// <summary>
        /// Prepare a BasicEffect for rendering
        /// </summary>
        /// <param name="effect"></param>
        protected void PrepareEffect(BasicEffect effect)
        {
            // Do we have a texture? Set the effect as required
            if (ObjectTexture == null)
            {
                // No testure so disable texturing
                effect.TextureEnabled = false;
            }
            else
            {
                // Enable texturing and set the texture into the effect
                effect.TextureEnabled = true;
                if (ObjectTexture != effect.Texture)
                {
                    effect.Texture = ObjectTexture;
                }
            }

            // Set the color and alpha
            effect.DiffuseColor  = ObjectColor.ToVector3();
            effect.SpecularColor = SpecularColor.ToVector3();
            effect.SpecularPower = SpecularPower;
            effect.EmissiveColor = EmissiveColor.ToVector3();
            effect.Alpha         = (float)ObjectColor.A / 255.0f;

            // Apply the transformation matrix
            effect.World = Transformation;

            // Now the effect is ready for the derived class to actually draw the object
        }
示例#3
0
        private void Update(EvaluationContext context)
        {
            // Parameters
            var parameterBufferContent = new PbrMaterialParams
            {
                BaseColor     = BaseColor.GetValue(context),
                EmissiveColor = EmissiveColor.GetValue(context),
                Roughness     = Roughness.GetValue(context),
                Specular      = Specular.GetValue(context),
                Metal         = Metal.GetValue(context)
            };

            ResourceManager.Instance().SetupConstBuffer(parameterBufferContent, ref _parameterBuffer);

            // Textures
            var resourceManager = ResourceManager.Instance();
            var device          = resourceManager.Device;

            Utilities.Dispose(ref _baseColorMapSrv);
            var tex = BaseColorMap.GetValue(context) ?? PbrContextSettings.WhitePixelTexture;

            _baseColorMapSrv = new ShaderResourceView(device, tex);
            context.PbrMaterialTextures.AlbedoColorMap = _baseColorMapSrv;

            Utilities.Dispose(ref _normalMapSrv);
            var tex2 = NormalMap.GetValue(context) ?? PbrContextSettings.NormalFallbackTexture;

            _normalMapSrv = new ShaderResourceView(device, tex2);
            context.PbrMaterialTextures.NormalMap = _normalMapSrv;

            Utilities.Dispose(ref _rsmoMapSrv);
            var tex3 = RoughnessSpecularMetallicOcclusionMap.GetValue(context) ?? PbrContextSettings.RsmoFallbackTexture;

            _rsmoMapSrv = new ShaderResourceView(device, tex3);
            context.PbrMaterialTextures.RoughnessSpecularMetallicOcclusionMap = _rsmoMapSrv;

            Utilities.Dispose(ref _emissiveColorMapSrv);
            var tex4 = EmissiveColorMap.GetValue(context) ?? PbrContextSettings.WhitePixelTexture;

            _emissiveColorMapSrv = new ShaderResourceView(device, tex4);
            context.PbrMaterialTextures.EmissiveColorMap = _emissiveColorMapSrv;

            var previousParameters = context.PbrMaterialParams;

            context.PbrMaterialParams = _parameterBuffer;


            SubTree.GetValue(context);
            context.PbrMaterialParams = previousParameters;
        }
        /// <summary>
        /// Prepare a BasicEffect for rendering
        /// </summary>
        /// <param name="effect"></param>
        protected void PrepareEffect(BasicEffect effect)
        {
            SetEffectTexture(effect, ObjectTexture);

            // Set the color and alpha
            effect.DiffuseColor  = ObjectColor.ToVector3();
            effect.SpecularColor = SpecularColor.ToVector3();
            effect.SpecularPower = SpecularPower;
            effect.EmissiveColor = EmissiveColor.ToVector3();
            effect.Alpha         = (float)ObjectColor.A / 255.0f;

            // Apply the transformation matrix
            effect.World = Transformation;

            // Now the effect is ready for the derived class to actually draw the object
        }
示例#5
0
        protected virtual void ReadDataJson(JsonReader reader, string propName)
        {
            switch (propName)
            {
            case nameof(Name):
                Name = reader.ReadAsString();
                break;

            case nameof(DiffuseColor):
                DiffuseColor.ReadJson(reader);
                break;

            case nameof(AmbientColor):
                AmbientColor.ReadJson(reader);
                break;

            case nameof(SpecularColor):
                SpecularColor.ReadJson(reader);
                break;

            case nameof(EmissiveColor):
                EmissiveColor.ReadJson(reader);
                break;

            case nameof(Opacity):
                Opacity = (float)reader.ReadAsDouble();
                break;

            case nameof(SpecularExponent):
                SpecularExponent = (float)reader.ReadAsDouble();
                break;

            case nameof(FaceMap):
                FaceMap = reader.ReadAsBoolean().Value;
                break;

            case nameof(TwoSided):
                TwoSided = reader.ReadAsBoolean().Value;
                break;

            default:
                break;
                throw new Exception("Unexpected property name!");
            }
        }
        /// <summary>
        /// Prepare a BasicEffect for rendering
        /// </summary>
        /// <param name="effect"></param>
        protected void PrepareEffect(EnvironmentMapEffect effect)
        {
            // Apply the texture if we have one and it differs from the active texture
            if (effect.Texture != ObjectTexture && ObjectTexture != null)
            {
                effect.Texture = ObjectTexture;
            }

            // Set the color and alpha
            effect.DiffuseColor  = ObjectColor.ToVector3();
            effect.EmissiveColor = EmissiveColor.ToVector3();
            effect.Alpha         = (float)ObjectColor.A / 255.0f;

            // Set other effect properties
            // Do we have a valid texture?
            if (effect.Texture != null)
            {
                // Yes, so apply the other environment map properties as normal
                effect.EnvironmentMapAmount   = EnvironmentMapAmount;
                effect.EnvironmentMapSpecular = EnvironmentMapSpecular.ToVector3();
                effect.FresnelFactor          = FresnelFactor;
            }
            else
            {
                // No, so make the object completely reflective.
                // Any texture set by other objects will remain but will be entirely hidden by the environment map
                effect.EnvironmentMapAmount   = 1.0f;
                effect.EnvironmentMapSpecular = Vector3.Zero;
                effect.FresnelFactor          = 0.0f;
            }

            // Apply the transformation matrix
            effect.World = Transformation;

            // Now the effect is ready for the derived class to actually draw the object
        }
示例#7
0
        public override void Draw(GameTime gameTime)
        {
            if (Content != null && Camera != null && Enabled && Visible)
            {
                // Copy any parent transforms.
                Matrix[] transforms = new Matrix[Content.Bones.Count];
                Content.CopyAbsoluteBoneTransformsTo(transforms);

                foreach (ModelMesh mesh in Content.Meshes)
                {
                    // Update our World Matrix for this Model
                    World = transforms[mesh.ParentBone.Index] *
                            Matrix.CreateRotationX(Rotation.X) *
                            Matrix.CreateRotationY(Rotation.Y) *
                            Matrix.CreateRotationZ(Rotation.Z) *
                            Matrix.CreateScale(Scale) *
                            Matrix.CreateTranslation(Position);

                    if (CustomEffect != null)
                    {
                        #region Custom HLSL Effect Rendering

                        /*********************************************************************************************************
                        *                                                                   -- Please do not f**k with this to much haha unless ya ask me (AR-50 LOCKED AND LOADED!) --
                        *********************************************************************************************************/

                        // Pass our meshes Index buffer to our graphics device
                        //GraphicsDevice.Indices = mesh.IndexBuffer;

                        // Loop thru the Custom Effect's passes
                        foreach (EffectPass pass in CustomEffect.CurrentTechnique.Passes)
                        {
                            // Begin our First pass
                            pass.Apply();

                            // Render this pass over the parts inside the mesh
                            foreach (ModelMeshPart part in mesh.MeshParts)
                            {
                                // Pass our mesh part's Vertex declaration to our graphics device
                                //GraphicsDevice.VertexDeclaration = part.VertexDeclaration;

                                // Manually pass our vertices to our graphics device
                                //GraphicsDevice.Vertices[0].SetSource(mesh.VertexBuffer, part.StreamOffset, part.VertexStride);

                                // TODO: Need to rework this .. the double casts will kill performance! *Refactor OUT*

                                // Check and see if the part has an attached texture
                                if (((BasicEffect)part.Effect).Texture != null)
                                {
                                    // If it does pass the texture our graphics device instead of manually in our shader
                                    GraphicsDevice.Textures[0] = ((BasicEffect)part.Effect).Texture;
                                }

                                // Finally Draw our Index Primitives (Triangles) for this part of our mesh
                                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.NumVertices, 0, part.NumVertices,
                                                                     part.StartIndex, part.PrimitiveCount);
                            }
                        }

                        #endregion
                    }
                    else
                    {
                        #region Basic Effect Rendering Loop

                        // Setup culling / fill
                        RasterizerState rasterizerState1 = new RasterizerState();
                        rasterizerState1.CullMode      = CullMode;
                        rasterizerState1.FillMode      = FillMode;
                        GraphicsDevice.RasterizerState = rasterizerState1;

                        // Set our alpha blending states if enabled
                        if (EnableAlphaBlending)
                        {
                            GraphicsDevice.BlendState = BlendState.AlphaBlend;

                            //GraphicsDevice.RenderState.AlphaBlendEnable = true;
                            //GraphicsDevice.RenderState.AlphaTestEnable = true;

                            //GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Greater;
                            //GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
                            //GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
                            //GraphicsDevice.RenderState.BlendFunction = BlendFunction.Load;
                        }

                        foreach (BasicEffect effect in mesh.Effects)
                        {
                            effect.EnableDefaultLighting();

                            effect.AmbientLightColor = AmbientColor.ToVector3();
                            effect.DiffuseColor      = DiffuseColor.ToVector3();
                            effect.EmissiveColor     = EmissiveColor.ToVector3();
                            effect.SpecularColor     = SpecularColor.ToVector3();

                            effect.World      = World;
                            effect.View       = Camera.View;
                            effect.Projection = Camera.Projection;
                        }

                        mesh.Draw();

                        // Reset our alpha blending states
                        if (EnableAlphaBlending)
                        {
                            //GraphicsDevice.RenderState.AlphaBlendEnable = false;
                            //GraphicsDevice.RenderState.AlphaTestEnable = false;
                            //GraphicsDevice.RenderState.SourceBlend = Blend.Zero;
                            //GraphicsDevice.RenderState.DestinationBlend = Blend.Zero;
                        }

                        #endregion
                    }
                    base.Draw(gameTime);
                }
            }
        }
示例#8
0
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            World = Matrix.CreateScale(Scale) *
                    Matrix.CreateRotationY(Rotation.Y) *
                    Matrix.CreateRotationX(Rotation.X) *
                    Matrix.CreateRotationX(Rotation.Z) *
                    Matrix.CreateTranslation(Position);

            if (VertexBuffer != null && IndexBuffer != null)
            {
                // Pass our Index Buffer and our Vertex Buffer to the Graphics Device
                GraphicsDevice.Indices = IndexBuffer;
                GraphicsDevice.SetVertexBuffer(VertexBuffer);

                if (CustomEffect != null)
                {
                    // Setup our Shader
                    if (SetShaderParameters != null)
                    {
                        SetShaderParameters.Invoke(gameTime);
                    }

                    foreach (EffectPass p in CustomEffect.CurrentTechnique.Passes)
                    {
                        p.Apply();
                        GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _width * _height, 0, TriangleCount);
                    }
                }
                else
                {
                    RasterizerState state = new RasterizerState();

                    state.FillMode = FillMode;
                    state.CullMode = CullMode;

                    GraphicsDevice.RasterizerState = state;

                    // Pass in our Lighting Colors
                    SimpleEffect.AmbientLightColor = AmbientColor.ToVector3();
                    SimpleEffect.DiffuseColor      = DiffuseColor.ToVector3();
                    SimpleEffect.EmissiveColor     = EmissiveColor.ToVector3();
                    SimpleEffect.SpecularColor     = SpecularColor.ToVector3();
                    SimpleEffect.SpecularPower     = SpecularPower;

                    // Update the world, view and projection matrices on the basic effect
                    SimpleEffect.World      = World;
                    SimpleEffect.View       = Camera.View;
                    SimpleEffect.Projection = Camera.Projection;

                    // Pass our texture to our graphics device for rendering
                    if (Texture != null)
                    {
                        SimpleEffect.Texture        = Texture;
                        SimpleEffect.TextureEnabled = true;
                    }

                    foreach (EffectPass pass in SimpleEffect.CurrentTechnique.Passes)
                    {
                        pass.Apply();
                        GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _width * _height, 0, TriangleCount);
                    }
                }
            }
        }