<p>I have the following .fx file for my shader:</p>
<pre><code>// Parameters that should be set from the program
float4x4 World; // World Matrix
float4x4 View; // View Matrix
float4x4 Projection; // Projection Matrix
float3 LightPosition; // in world space
float3 CameraPosition; // in world space
float Shininess; // scalar value
float3 AmbientColor;
float3 DiffuseColor;
float3 SpecularColor;
texture DiffuseTexture;

sampler DiffuseSampler = sampler_state
{
    Texture = &lt;DiffuseTexture&gt;;
    AddressU = Wrap;
    AddressV = Wrap;
};
// We create structs to help us manage the inputs/outputs
// to vertex and pixel shaders
struct VertexInput
{
    float4 Position : POSITION0; // Here, POSITION0 and NORMAL0
    float3 Normal : NORMAL0; // are called mnemonics
    float2 UV: TEXCOORD0;
};
// --- Per Vertex technique - Gouraud
// We will create an output struct for this technique
struct GouraudVertexOutput
{
    float4 Position : POSITION0;
    float3 Color : COLOR0;
};
// The Vertex shader
// We will perform all the lighting calculations here
GouraudVertexOutput GouraudVertex(VertexInput input)
{
    GouraudVertexOutput output; // create the output struct
    // First, do the transformations
    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
    // Now do the lighting computation. First calculate the directions
    float3 lightDirection = normalize(LightPosition - worldPosition.xyz);
    float3 viewDirection = normalize(CameraPosition - worldPosition.xyz);
    float3 worldNormal = mul(input.Normal, World);
    float3 reflectDirection = -reflect(lightDirection, worldNormal);
    // Now compute the light components
    float diffuse = max(dot(lightDirection, worldNormal), 0);
    float specular = pow(max(dot(reflectDirection, viewDirection), 0), Shininess);
    // Finally, save the color to the output struct
    output.Color = AmbientColor + diffuse * DiffuseColor + specular * SpecularColor;
    return output;
}
// The Pixel Shader
// All this function does is to pass through the color through 
float4 GouraudPixel(GouraudVertexOutput input) : COLOR0
{
    return float4(input.Color, 1);
}

// --- Per Pixel techniques - Phong, Blinn, Schlick
// We will need a different output struct for this
struct PhongVertexOutput
{
    float4 Position : POSITION0;
    float2 UV: TEXCOORD0;
    float4 WorldPosition : TEXCOORD1;
    float3 WorldNormal : TEXCOORD2;
};
// A common vertex shader for all the different techniques
PhongVertexOutput PhongVertex(VertexInput input)
{
    PhongVertexOutput output;
    // Do the transformations as before
    // Save the world position for use in the pixel shader
    output.WorldPosition = mul(input.Position, World);
    float4 viewPosition = mul(output.WorldPosition, View);
    output.Position = mul(viewPosition, Projection);
    // as well as the normal in world space
    output.WorldNormal = mul(input.Normal, World);
    output.UV = input.UV * 10;
    return output;
}
// The pixel shader performs the lighting
float4 PhongPixel(PhongVertexOutput input) : COLOR0
{
    // The lighting operation, same as in the Gouraud vertex method
    float3 lightDirection = normalize(LightPosition - input.WorldPosition.xyz);
    float3 viewDirection = normalize(CameraPosition - input.WorldPosition.xyz);
    // Need to normalize my incoming normal, length could be less than 1
    input.WorldNormal = normalize(input.WorldNormal);
    float3 reflectDirection = -reflect(lightDirection, input.WorldNormal);
    // Now, compute the lighint components
    float diffuse = max(dot(lightDirection, input.WorldNormal), 0) * tex2D(DiffuseSampler, input.UV);
    float specular = pow(max(dot(reflectDirection, viewDirection), 0), Shininess);
    return float4(AmbientColor + diffuse * DiffuseColor + specular * SpecularColor, 1);
}

// Now the different techniques
technique Gouraud
{
    pass Pass1
    {
        VertexShader = compile vs_4_0 GouraudVertex();
        PixelShader = compile ps_4_0 GouraudPixel();
    }
}
technique Phong
{
    pass Pass1
    {
        VertexShader = compile vs_4_0 PhongVertex();
        PixelShader = compile ps_4_0 PhongPixel();
    }
}
</code></pre>
<p>What I'm trying to do is change this file so that the 3D objects I apply it to are green in color. What parts of the above code do I need to change in order to do this? I am using the Monogame software in Visual Studio (c# coding language). Any help would be greatly appreciated!</p>

CC BY-SA 4.0
2020-12-01T06:30:40.063Z
Judging by your question, you don't know all too much about shaders. If you want to get into shader programming, I suggest using a few tutorials, as they explain the basics nicely.

That being said, you probably want your object to be greenn but shaded / lighted. So rather than changing your shader, you need to change the input to your shader. In your PhongPixel method, there is the `return` line at the end, which contains `AmbientColor`, `DiffuseColor` and `SpecularColor`. These are inputs to the shader, so wherever they are set, you need to change the inputs to your green color.
Tare
CC BY-SA 4.0
2020-12-01T06:33:29.727Z
Also note that in the very beginning of your code it says `// Parameters that should be set from the program` and then lists a bunch of variables to do this for. In case I misunderstood and you want everything to turn green, you need to multiply the current result of your `return` line with something like `float4(1.0, 0.0, 0.0,1 .0)`.
Tare
<3d><shader><shading><3dtexture>
Changing shader color
Alexander Mills
# Changing shader color

(Asked by: username_0 on 2020-11-30)

I have the following .fx file for my shader:

```
// Parameters that should be set from the program
float4x4 World; // World Matrix
float4x4 View; // View Matrix
float4x4 Projection; // Projection Matrix
float3 LightPosition; // in world space
float3 CameraPosition; // in world space
float Shininess; // scalar value
float3 AmbientColor;
float3 DiffuseColor;
float3 SpecularColor;
texture DiffuseTexture;

sampler DiffuseSampler = sampler_state
{
    Texture = <DiffuseTexture>;
    AddressU = Wrap;
    AddressV = Wrap;
};
// We create structs to help us manage the inputs/outputs
// to vertex and pixel shaders
struct VertexInput
{
    float4 Position : POSITION0; // Here, POSITION0 and NORMAL0
    float3 Normal : NORMAL0; // are called mnemonics
    float2 UV: TEXCOORD0;
};
// --- Per Vertex technique - Gouraud
// We will create an output struct for this technique
struct GouraudVertexOutput
{
    float4 Position : POSITION0;
    float3 Color : COLOR0;
};
// The Vertex shader
// We will perform all the lighting calculations here
GouraudVertexOutput GouraudVertex(VertexInput input)
{
    GouraudVertexOutput output; // create the output struct
    // First, do the transformations
    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
    // Now do the lighting computation. First calculate the directions
    float3 lightDirection = normalize(LightPosition - worldPosition.xyz);
    float3 viewDirection = normalize(CameraPosition - worldPosition.xyz);
    float3 worldNormal = mul(input.Normal, World);
    float3 reflectDirection = -reflect(lightDirection, worldNormal);
    // Now compute the light components
    float diffuse = max(dot(lightDirection, worldNormal), 0);
    float specular = pow(max(dot(reflectDirection, viewDirection), 0), Shininess);
    // Finally, save the color to the output struct
    output.Color = AmbientColor + diffuse * DiffuseColor + specular * SpecularColor;
    return output;
}
// The Pixel Shader
// All this function does is to pass through the color through 
float4 GouraudPixel(GouraudVertexOutput input) : COLOR0
{
    return float4(input.Color, 1);
}

// --- Per Pixel techniques - Phong, Blinn, Schlick
// We will need a different output struct for this
struct PhongVertexOutput
{
    float4 Position : POSITION0;
    float2 UV: TEXCOORD0;
    float4 WorldPosition : TEXCOORD1;
    float3 WorldNormal : TEXCOORD2;
};
// A common vertex shader for all the different techniques
PhongVertexOutput PhongVertex(VertexInput input)
{
    PhongVertexOutput output;
    // Do the transformations as before
    // Save the world position for use in the pixel shader
    output.WorldPosition = mul(input.Position, World);
    float4 viewPosition = mul(output.WorldPosition, View);
    output.Position = mul(viewPosition, Projection);
    // as well as the normal in world space
    output.WorldNormal = mul(input.Normal, World);
    output.UV = input.UV * 10;
    return output;
}
// The pixel shader performs the lighting
float4 PhongPixel(PhongVertexOutput input) : COLOR0
{
    // The lighting operation, same as in the Gouraud vertex method
    float3 lightDirection = normalize(LightPosition - input.WorldPosition.xyz);
    float3 viewDirection = normalize(CameraPosition - input.WorldPosition.xyz);
    // Need to normalize my incoming normal, length could be less than 1
    input.WorldNormal = normalize(input.WorldNormal);
    float3 reflectDirection = -reflect(lightDirection, input.WorldNormal);
    // Now, compute the lighint components
    float diffuse = max(dot(lightDirection, input.WorldNormal), 0) * tex2D(DiffuseSampler, input.UV);
    float specular = pow(max(dot(reflectDirection, viewDirection), 0), Shininess);
    return float4(AmbientColor + diffuse * DiffuseColor + specular * SpecularColor, 1);
}

// Now the different techniques
technique Gouraud
{
    pass Pass1
    {
        VertexShader = compile vs_4_0 GouraudVertex();
        PixelShader = compile ps_4_0 GouraudPixel();
    }
}
technique Phong
{
    pass Pass1
    {
        VertexShader = compile vs_4_0 PhongVertex();
        PixelShader = compile ps_4_0 PhongPixel();
    }
}

```

What I'm trying to do is change this file so that the 3D objects I apply it to are green in color. What parts of the above code do I need to change in order to do this? I am using the Monogame software in Visual Studio (c# coding language). Any help would be greatly appreciated!

**Tags:** 3d, shader, shading, 3dtexture

> **Comments:**
> **username_1**: Judging by your question, you don't know all too much about shaders. If you want to get into shader programming, I suggest using a few tutorials, as they explain the basics nicely.  That being said, you probably want your object to be greenn but shaded / lighted. So rather than changing your shader, you need to change the input to your shader. In your PhongPixel method, there is the `return` line at the end, which contains `AmbientColor`, `DiffuseColor` and `SpecularColor`. These are inputs to the shader, so wherever they are set, you need to change the inputs to your green color.  
> **username_1**: Also note that in the very beginning of your code it says `// Parameters that should be set from the program` and then lists a bunch of variables to do this for. In case I misunderstood and you want everything to turn green, you need to multiply the current result of your `return` line with something like `float4(1.0, 0.0, 0.0,1 .0)`.  

---

## 0 answers

