# HLSL Textured Light: outputs only a specific part of the texture (new problem)

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

input.Position3D.w = 1.0f;
output.Position = mul(viewPosition, Projection);


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;

{
//left out a lot of code here
output.Position = mul(mul(worldPosition, View2), Projection2);
}


Full Code

• What happens if instead of going through the branch you immediately return tex2D(SpotlightTextureSampler, TextureCoordinate);? If you still see a black circle then your texture could be empty or not being sampled correctly. – TheWanderer Aug 14 '13 at 19:33
• @AvengerDr if I do that I get a completely black model, thats why I think the problem lies indeed in the sampler (or texture), but I have no clue here exactly the problem lies in my code or how to solve it – Hasse Iona Aug 14 '13 at 19:43
• My assumption is that either the texture is not being sampled correctly as AvengerDr said or your TextureCoordinates are wrong and constantly fetch a black Pixel. – Luis W Aug 14 '13 at 21:18
• Try replacing the texture with a 1x1 magenta (or some other highly visible color) to see if it's coming through at all. Also try replacing the TextureCoordinate with a constant like float2(0.5, 0.5) and see if you get the color from the center of your texture. – Nathan Reed Aug 14 '13 at 21:54
• @LuisW I tried exchanging the content of the sampler I use for my greyscale with this one, to see if my texture then would somehow show up on the greyscale or something else, as to find out whether the problem might be the TextureCoordinates. To my surprise the greyscaleshader kept working flawlessly, litterally nothing changed. It seems as if the samplers dont do anything or at least if the line Texture = <SpotlightTexture>; does completely nothing. Does maybe the problem lie here: sampler SpotlightTextureSampler = sampler_state? The part after the equation is the same for both samplers. – Hasse Iona Aug 14 '13 at 21:58