• Advertisement
Sign in to follow this  

Shader does not render Textures

This topic is 1242 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having a problem with my Shader, basically, all Textures are rendered solid black. My knowledge of HLSL is, well, expandable, so maybe you can see errors that I just overlook.

 

Here's the code:

float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 WorldInverseTranspose;
 
float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;
 
float3 DiffuseLightDirection = float3(1, 0, 0);
float4 DiffuseColor = float4(1, 1, 1, 1);
float DiffuseIntensity = 1.0;
 
float Shininess = 200;
float4 SpecularColor = float4(1, 1, 1, 1);
float SpecularIntensity = 1;
float3 ViewVector = float3(1, 0, 0);
 
Texture2D ModelTexture;

SamplerState textureSampler = sampler_state {
    Texture2D = (ModelTexture);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};
 
struct VertexShaderInput
{
    float4 Position : SV_POSITION0;
    float4 Normal : NORMAL0;
    float2 TextureCoordinate : TEXCOORD0;
};
 
struct VertexShaderOutput
{
    float4 Position : SV_POSITION0;
    float4 Color : COLOR0;
    float3 Normal : TEXCOORD0;
    float2 TextureCoordinate : TEXCOORD1;
};
 
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;
 
    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
 
    float4 normal = normalize(mul(input.Normal, WorldInverseTranspose));
    float lightIntensity = dot(normal, DiffuseLightDirection);
    output.Color = saturate(DiffuseColor * DiffuseIntensity * lightIntensity);
 
    output.Normal = normal;
 
    output.TextureCoordinate = input.TextureCoordinate;
    return output;
}
 
float4 PixelShaderFunction(VertexShaderOutput input) : SV_TARGET0
{
    float3 light = normalize(DiffuseLightDirection);
    float3 normal = normalize(input.Normal);
    float3 r = normalize(2 * dot(light, normal) * normal - light);
    float3 v = normalize(mul(normalize(ViewVector), World));
    float dotProduct = dot(r, v);
 
    float4 specular = SpecularIntensity * SpecularColor * max(pow(abs(dotProduct), Shininess), 0) * length(input.Color);
 
    float4 textureColor = ModelTexture.Sample(textureSampler, input.TextureCoordinate);
    textureColor.a = 1;
 
    return saturate(textureColor * (input.Color) + AmbientColor * AmbientIntensity + specular);
}
 
technique Textured
{
    pass Pass1
    {
		AlphaBlendEnable = TRUE;
        DestBlend = INVSRCALPHA;
        SrcBlend = SRCALPHA;
        VertexShader = compile vs_4_0 VertexShaderFunction();
        PixelShader = compile ps_4_0 PixelShaderFunction();
    }
}

I'm currently using SharpDX's BasicEffect class for rendering because of this.

Share this post


Link to post
Share on other sites
Advertisement

Have you tried only using the sampled texels from the texture ?

float4 PixelShaderFunction(...)
{
     // Snip...
     return textureColor;
}

Share this post


Link to post
Share on other sites

*facepalm* That did it, textures are showing.

Now on to tracking down the real issue. Gonna get into the graphics debugger

 

EDIT: Found it :D Had to kick the worldInverseTranspose out of the equation

Edited by Lordadmiral Drake

Share this post


Link to post
Share on other sites

Let me guess : input.Color is black

 

ie. DiffuseColor * DiffuseIntensity * lightIntensity one of those components is 0 or maybe negative.

 

Cheers!

Share this post


Link to post
Share on other sites

Yep, because float4 normal = normalize(mul(input.Normal, WorldInverseTranspose));

always returns 0 which in turn caused lightIntensity to be 0 and so on.

Now I modified the above line to look like this:

 

float4 normal = normalize(input.Normal);

 

And its working =)

 

I didn't find this problem before because I was always looking in the Pixel Shader only.

Edited by Lordadmiral Drake

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement