Exemple #1
0
        /// <summary>
        /// Apply this material.
        /// </summary>
        override protected void MaterialSpecificApply(bool wasLastMaterial)
        {
            // set world matrix
            if (IsDirty(MaterialDirtyFlags.World))
            {
                _effect.World = World;
            }

            // if it was last material used, stop here - no need for the following settings
            if (wasLastMaterial)
            {
                return;
            }

            // set all effect params
            if (IsDirty(MaterialDirtyFlags.TextureParams))
            {
                _effect.Texture = Texture;
            }
            if (IsDirty(MaterialDirtyFlags.Alpha))
            {
                _effect.Alpha = Alpha;
            }
            if (IsDirty(MaterialDirtyFlags.MaterialColors))
            {
                _effect.DiffuseColor = DiffuseColor.ToVector3();
            }
            if (IsDirty(MaterialDirtyFlags.AlphaTest))
            {
                _effect.AlphaFunction  = AlphaFunction;
                _effect.ReferenceAlpha = ReferenceAlpha;
            }
        }
Exemple #2
0
        /// <summary>
        /// Apply this material.
        /// </summary>
        override protected void MaterialSpecificApply(bool wasLastMaterial)
        {
            // set world matrix
            _effectParams["WorldViewProjection"].SetValue(World * ViewProjection);

            // set world matrix
            if (IsDirty(MaterialDirtyFlags.World))
            {
                _worldParam.SetValue(World);
                if (_transposeParam != null)
                {
                    _transposeParam.SetValue(Matrix.Invert(Matrix.Transpose(World)));
                }
            }

            // set all effect params
            if (IsDirty(MaterialDirtyFlags.TextureParams))
            {
                var textureParam = _effectParams["MainTexture"];
                if (textureParam != null)
                {
                    _effectParams["TextureEnabled"].SetValue(Texture != null);
                    textureParam.SetValue(Texture);
                }
            }
            if (IsDirty(MaterialDirtyFlags.Alpha))
            {
                _effectParams["Alpha"].SetValue(Alpha);
            }
            if (IsDirty(MaterialDirtyFlags.MaterialColors))
            {
                _effectParams["DiffuseColor"].SetValue(DiffuseColor.ToVector3());
                _effectParams["MaxLightIntensity"].SetValue(MaxLightIntensity);
            }
        }
Exemple #3
0
 public override void SetEffectParameters(Effect effect)
 {
     if (effect.Parameters["Position"] != null)
     {
         effect.Parameters["Position"].SetValue(Position);
     }
     if (effect.Parameters["LightColor"] != null)
     {
         effect.Parameters["LightColor"].SetValue(LightColor);
     }
     if (effect.Parameters["Attenuation"] != null)
     {
         effect.Parameters["Attenuation"].SetValue(Attenuation);
     }
     if (effect.Parameters["DiffuseColor"] != null)
     {
         effect.Parameters["DiffuseColor"].SetValue(DiffuseColor.ToVector3());
     }
     if (effect.Parameters["AmbientColor"] != null)
     {
         effect.Parameters["AmbientColor"].SetValue(AmbientColor.ToVector3());
     }
     if (effect.Parameters["Falloff"] != null)
     {
         effect.Parameters["Falloff"].SetValue(FallOff);
     }
     if (effect.Parameters["SpecularColor"] != null)
     {
         effect.Parameters["SpecularColor"].SetValue(SpecularColor);
     }
     base.SetEffectParameters(effect);
 }
        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);
        }
Exemple #5
0
        public byte[] GetBytes()
        {
            List <byte> result = new List <byte>();

            result.AddRange(ByteConverter.GetBytes(DiffuseColor.ToArgb()));
            result.AddRange(ByteConverter.GetBytes(SpecularColor.ToArgb()));
            result.AddRange(ByteConverter.GetBytes(Exponent));
            result.AddRange(ByteConverter.GetBytes(TextureID));
            result.AddRange(ByteConverter.GetBytes(Flags));
            return(result.ToArray());
        }
Exemple #6
0
        public override void SetEffectParameters(Effect effect)
        {
            effect.Parameters["LightColor"]?.SetValue(LightColor.ToVector3());
            effect.Parameters["AmbientColor"]?.SetValue(AmbientColor.ToVector3());
            effect.Parameters["DiffuseColor"]?.SetValue(DiffuseColor.ToVector3());
            effect.Parameters["Position"]?.SetValue(Position);
            effect.Parameters["ModelTexture"]?.SetValue(Texture);
            effect.Parameters["NormalTexture"]?.SetValue(Normal);
            effect.Parameters["Attenuation"]?.SetValue(Attenuation);

            base.SetEffectParameters(effect);
        }
Exemple #7
0
        public override void SetEffectParameters(Effect effect)
        {
            effect.Parameters["LightColor"]?.SetValue(LightColor.ToVector3());
            effect.Parameters["AmbientColor"]?.SetValue(AmbientColor.ToVector3());
            effect.Parameters["DiffuseColor"]?.SetValue(DiffuseColor.ToVector3());
            effect.Parameters["SpecularColor"]?.SetValue(SpecularColor.ToVector3());
            effect.Parameters["SpecularPower"]?.SetValue(SpecularPower);
            effect.Parameters["Position"]?.SetValue(Position);
            effect.Parameters["ModelTexture"]?.SetValue(Texture);
            effect.Parameters["NormalTexture"]?.SetValue(Normal);
            effect.Parameters["SpecularTexture"]?.SetValue(Specular);
            effect.Parameters["CameraPosition"]?.SetValue(GameRoot.MainCamera.Position);
            effect.Parameters["Attenuation"]?.SetValue(Attenuation);

            base.SetEffectParameters(effect);
        }
Exemple #8
0
        bool UpdateMaterialElement(XElement materialElement, CharacterAttachmentMaterial material)
        {
            Debug.LogAlways("Updating material element for attachment {0} in slot {1}", material.Attachment.Name, material.Attachment.Slot.Name);
            var modifiedMaterial = false;

            var genMaskAttribute = materialElement.Attribute("StringGenMask");

            if (genMaskAttribute != null && genMaskAttribute.Value.Contains("%COLORMASKING"))
            {
                Debug.LogAlways("Writing color mask");
                var publicParamsElement = materialElement.Element("PublicParams");

                publicParamsElement.SetAttributeValue("ColorMaskR", material.ColorRed.ToString());
                publicParamsElement.SetAttributeValue("ColorMaskG", material.ColorGreen.ToString());
                publicParamsElement.SetAttributeValue("ColorMaskB", material.ColorBlue.ToString());
                publicParamsElement.SetAttributeValue("ColorMaskA", material.ColorAlpha.ToString());

                modifiedMaterial = true;
            }

            var texturesElement = materialElement.Element("Textures");

            if (texturesElement != null)
            {
                if (WriteTexture(texturesElement, "Diffuse", material.DiffuseTexture) ||
                    WriteTexture(texturesElement, "Specular", material.SpecularTexture) ||
                    WriteTexture(texturesElement, "Bumpmap", material.BumpmapTexture) ||
                    WriteTexture(texturesElement, "Custom", material.CustomTexture))
                {
                    modifiedMaterial = true;
                }
            }

            if (!UnusedMarker.IsUnused(DiffuseColor))
            {
                materialElement.SetAttributeValue("Diffuse", DiffuseColor.ToString());
            }

            if (!UnusedMarker.IsUnused(SpecularColor))
            {
                materialElement.SetAttributeValue("Specular", SpecularColor.ToString());
            }

            return(modifiedMaterial);
        }
        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!");
            }
        }
Exemple #10
0
        /// <summary>
        /// Apply this material.
        /// </summary>
        override protected void MaterialSpecificApply(bool wasLastMaterial)
        {
            // set world matrix
            if (IsDirty(MaterialDirtyFlags.World))
            {
                _effect.World = World;
            }

            // if it was last material used, stop here - no need for the following settings
            if (wasLastMaterial)
            {
                return;
            }

            // set all effect params
            if (IsDirty(MaterialDirtyFlags.TextureParams))
            {
                _effect.Texture = Texture;
            }
            if (IsDirty(MaterialDirtyFlags.Alpha))
            {
                _effect.Alpha = Alpha;
            }
            if (IsDirty(MaterialDirtyFlags.AmbientLight))
            {
                _effect.AmbientLightColor = AmbientLight.ToVector3();
            }
            if (IsDirty(MaterialDirtyFlags.EmissiveLight))
            {
                _effect.EmissiveColor = EmissiveLight.ToVector3();
            }
            if (IsDirty(MaterialDirtyFlags.MaterialColors))
            {
                _effect.DiffuseColor  = DiffuseColor.ToVector3();
                _effect.SpecularColor = SpecularColor.ToVector3();
                _effect.SpecularPower = SpecularPower;
            }
            if (_currBones != null && (IsDirty(MaterialDirtyFlags.Bones)))
            {
                _effect.SetBoneTransforms(_currBones);
            }
        }
Exemple #11
0
 public void Apply(int pass)
 {
     PrepareShaderProgram();
     shaderParams.Set(shaderParamKeys.World, Renderer.World);
     shaderParams.Set(shaderParamKeys.WorldView, Renderer.WorldView);
     shaderParams.Set(shaderParamKeys.WorldViewProj, Renderer.WorldViewProjection);
     shaderParams.Set(shaderParamKeys.ColorFactor, Renderer.ColorFactor.ToVector4());
     shaderParams.Set(shaderParamKeys.DiffuseColor, DiffuseColor.ToVector4());
     shaderParams.Set(shaderParamKeys.FogColor, FogColor.ToVector4());
     shaderParams.Set(shaderParamKeys.FogStart, FogStart);
     shaderParams.Set(shaderParamKeys.FogEnd, FogEnd);
     shaderParams.Set(shaderParamKeys.FogDensity, FogDensity);
     if (skinEnabled)
     {
         shaderParams.Set(shaderParamKeys.Bones, boneTransforms, boneCount);
     }
     PlatformRenderer.SetBlendState(Blending.GetBlendState());
     PlatformRenderer.SetTextureLegacy(CommonMaterialProgram.DiffuseTextureStage, diffuseTexture);
     PlatformRenderer.SetShaderProgram(program);
     PlatformRenderer.SetShaderParams(shaderParamsArray);
 }
Exemple #12
0
        public string ToStruct(string[] textures = null)
        {
            if (DiffuseColor == Color.Empty && SpecularColor == Color.Empty && Exponent == 0 && TextureID == 0 && Flags == 0)
            {
                return("{ 0 }");
            }
            StringBuilder result = new StringBuilder("{ ");

            result.Append(DiffuseColor.ToStruct());
            result.Append(", ");
            result.Append(SpecularColor.ToStruct());
            result.Append(", ");
            result.Append(Exponent.ToC());
            result.Append(", ");
            int callback = (int)(TextureID & 0xC0000000);
            int texid    = (int)(TextureID & ~0xC0000000);

            if (callback != 0)
            {
                result.Append(((StructEnums.NJD_CALLBACK)callback).ToString().Replace(", ", " | ") + " | ");
            }
            if (textures == null || texid >= textures.Length)
            {
                result.Append(texid);
            }
            else
            {
                result.Append(textures[texid].MakeIdentifier());
            }
            result.Append(", ");
            result.Append(((StructEnums.MaterialFlags)(Flags & ~0x7F)).ToString().Replace(", ", " | "));
            if (UserFlags != 0)
            {
                result.Append(" | 0x" + UserFlags.ToString("X"));
            }
            result.Append(" }");
            return(result.ToString());
        }
Exemple #13
0
 public override int GetHashCode()
 {
     return(AmbientColor.GetHashCode() ^ DiffuseColor.GetHashCode() ^ SpecularColor.GetHashCode() ^ Exponent.GetHashCode() ^
            TextureID.GetHashCode() ^ Flags.GetHashCode());
 }
Exemple #14
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);
                }
            }
        }
Exemple #15
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);
                    }
                }
            }
        }