Problem 1
I am trying to create a spotlight that instead of giving the circle within the spotlight a simple color, applies a texture to it. However, where the texture is supposed to be is only a black circle: image.
Solution: the position I used for my texture wasnt correct.
old and faulty code:
float4 TexturedSpotlightPixelShader(VertexShaderOutput input, float2 TextureCoordinate : TEXCOORD0) : COLOR0
{
//I left out all the other code in the shader, full code is at the bottom of the thread
color = tex2D(SpotlightTextureSampler, TextureCoordinate);
}
better code that solved my 'black problem (but created a new one):
float4 TexturedSpotlightPixelShader(VertexShaderOutput input) : COLOR0
{
//I left out all the other code in the shader, full code is at the bottom of the thread
color = tex2D(SpotlightTextureSampler, input.Position);
}
where at that point input.Position was set to the worldPosition in the vertexshader (which turned out was the reason for problem 2)
Problem 2
My spotlight now applies the texture, however the texture is repeated multiple times in miniature, as such: image
Solution: I added these lines in the vertex shader:
input.Position3D.w = 1.0f;
output.Position = mul(viewPosition, Projection);
and by adding/changing these lines in the pixelshader:
float2 TextureCoordinates;
TextureCoordinates.x = input.Position.x / input.Position.w / 2.0f + 0.5f;
TextureCoordinates.y = -input.Position.y / input.Position.w / 2.0f + 0.5f;
color += tex2D(SpotlightTextureSampler, TextureCoordinates);
however, there was still a third problem.
Problem 3
The third and probably (I hope :P) the last problem, my spotlight now only shows a part of my texture and manually resizing the image (in the image file, not in my code) doesnt change what part of the texture is shown, as such: image
Solution: In my vertexshader I used the 2D positions (viewmatrix multiplied with the projection matrix) where I should have used a viewmatrix not based from my camera but from my spotlights point of view (and a projection matrix also for my spotlight's point of view)
My new code:
float4x4 View2, Projection2;
VertexShaderOutput SimpleVertexShader(VertexShaderInput input)
{
//left out a lot of code here
output.Position = mul(mul(worldPosition, View2), Projection2);
}
Full Code
Link to the shader code and the link to the xna/c# code.
return tex2D(SpotlightTextureSampler, TextureCoordinate);
? If you still see a black circle then your texture could be empty or not being sampled correctly. \$\endgroup\$TextureCoordinate
with a constant likefloat2(0.5, 0.5)
and see if you get the color from the center of your texture. \$\endgroup\$