# HLSL how to put 2 lights in my shader ??

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

## Recommended Posts

hi guys i am beginner in using HLSL and i want to put 2 lights in my .fx file but i need some help in it , so can anybody help me with that ??? i tried to make multiple passes in my shader but if i have a big mesh the FPS will become too low and 1 light is passed not 2 lights (2 passes but only 1 light is lighting my scene), so i think i shouldn't make 2 passes for the same type of lighting and i should add the lights inside the shader . i looked at the www.mdxinfo.com tutorials but i didn't find any tutorial that is using more than 1 light so any help would be gr8 thanks alot guys. 1 more thing is using HLSL affect the performance of the game ?? does it decrease the FPS ??

##### Share on other sites
All you want to do is to perform the same lighting equations twice, but just add the results. In my shaders, I can do up to 3 or 4 lights per pass (directional, point, and spot) in this manner. If you want something simple, you can do it like (psuedocode):

float4 lightOneDir;float4 lightTwoDir;VertexShader(){   // Blah blah blah, do all your stuff   OUT.diffuseColor = (float4)0;   // Perform lighting calculations for lightOne   OUT.diffuseColor += lightOneResult;   // Perform lighting calculations for lightTwo   OUT.diffuseColor += lightTwoResult;}

It would kinda go something like that.

##### Share on other sites
can u help me with this code i am using in my shader

float4x4 worldMatrix : WORLD;  // World matrix for objectfloat4x4 worldViewProjection : WORLDVIEWPROJECTION;    // World * View * Projection matrixfloat4 lightDirection;  // Direction of the lightfloat4 eyeVector; // Vector for the eye locationtexture SceneTexture;sampler SceneSampler =sampler_state{    Texture = <SceneTexture>;};//--------------------------------------------------------------------------------------// Transformation with single directional light plus per pixel specular//--------------------------------------------------------------------------------------struct PER_PIXEL_OUT{    float4 Position : POSITION;    float2 TexCoords : TEXCOORD0;    float3 LightDirection : TEXCOORD1;    float3 Normal : TEXCOORD2;    float3 EyeWorld : TEXCOORD3;};PER_PIXEL_OUT TransformSpecularPerPixel(float4 pos : POSITION, float3 normal : NORMAL, float2 uv : TEXCOORD0){    PER_PIXEL_OUT Output = (PER_PIXEL_OUT)0;        // Transform position    Output.Position = mul(pos, worldViewProjection);    // Store uv coords    Output.TexCoords = uv;        // Store the light direction    Output.LightDirection = lightDirection;            // Transform the normals into the world matrix and normalize them    Output.Normal = normalize(mul(normal, worldMatrix));    // Transform the world position of the vertex    float3 worldPosition = normalize(mul(pos, worldMatrix));    // Store the eye vector    Output.EyeWorld = normalize(eyeVector - worldPosition);        // Return the data    return Output;}float4 TextureColorPerPixel( float2 uvCoords : TEXCOORD0, float3 lightDirection : TEXCOORD1, float3 normal : TEXCOORD2, float3 eye : TEXCOORD3) : COLOR0{    // Normalize our vectors    float3 normalized = normalize(normal);    float3 light = normalize(lightDirection);    float3 eyeDirection = normalize(eye);    // Store our diffuse component    float4 diffuse = saturate(dot(light, normalized));        // Calculate specular component    float3 reflection = normalize(2 * diffuse * normalized - light);    float4 specular = pow(saturate(dot(reflection, eyeDirection)), 8);    float4 textureColorFromSampler = tex2D(SceneSampler, uvCoords);    // Return the combined color     return textureColorFromSampler * diffuse + specular;};//--------------------------------------------------------------------------------------// Techniques//--------------------------------------------------------------------------------------technique RenderSceneSpecularPerPixel{    pass P0    {                  VertexShader = compile vs_1_1 TransformSpecularPerPixel();        PixelShader  = compile ps_2_0 TextureColorPerPixel();    } pass P1    {                  VertexShader = compile vs_1_1 TransformSpecularPerPixel();        PixelShader  = compile ps_2_0 TextureColorPerPixel();    }}

##### Share on other sites
Please allow for 24hrs or until a thread falls off the first page of listings before bumping - thanks! [smile]

It's pretty much exactly as Dustin posted [wink]

To modify your shader, add an additional entry to the PER_PIXEL_OUT structure so you have LightDirection1 and LightDirection2 (for example). Make sure that the vertex shader passes both directions through - it needs do no other computation.

Then, in the pixel shader, duplicate all code except for the last tex2D() part. End up with something like:

return textureColorFromSampler * diffuse1 + textureFromSampler * diffuse2 + specular1 + specular2;

hth
Jack

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631375
• Total Posts
2999659
×