Light Shader

Started by
39 comments, last by belfegor 10 years, 10 months ago

I'm looking for a shader example that I can use to setup any type of light in any position in the scene.

Advertisement

That's quite something to ask for. huh.png

Do you have any problems in developing the shader?

You know we might be able to help you. wink.png

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

I don't even know how to write shader for lights, so I'm looking for any example out that can be used with FFP to create any type of light anywhere in the scene.

Assuming that you know the basic concepts, such as: Normals and Vectors. huh.png

One simple type of lighting you could try to implement is directional lighting, such as the sun (Assuming that the rays are directional):

Vertex Shader:

  • Multiply the mesh's normal by its rotation mesh to get the world normal (Position and Scaling does not change the normal, at least not in the simple cases).
  • In the 2nd line we calculate how visible this normal is in comparison to the light direction/vector, by using the dot function with the light vector and the world normal. The saturate function makes sure that the value is in between 0 and 1.
  • Now send the color to the pixel shader, the variable "color" is defined in the input layout for the pixel shader, which is returned by the vertex shader, and that has to be done by you.

Code (VS):


float4 n = normalize(mul(rotationMatrix, normal));
float diffusebrightness = saturate(dot(n, lDir));
output.color = lColor * diffusebrightness;

Pixel Shader:

  • Well if you don't have anything other to do, just return the color, as directional lighting only affects faces.

Code (PS):


return input.color;

Input Layout:


An example of an input layout:


struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

Shader If Needed, click below...

[spoiler]

This shader is for DirectX 11, but the principles should be the same, I think! wacko.png


cbuffer ConstantObjectBuffer : register(c1)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;

    matrix rotationMatrix;
}

cbuffer ConstantFrameBuffer : register(c0)
{
    float4 lDir;
    float4 lColor;
}

struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

VOut VShader(float4 position : POSITION, float4 normal : NORMAL)
{
    VOut output;
    output.position = mul(position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);
	
    float4 n = normalize(mul(rotationMatrix, normal));
    float diffusebrightness = saturate(dot(n, lDir));
    output.color = lColor * diffusebrightness;

    return output;
}

float4 PShader(VOut input) : SV_TARGET
{
    return input.color;
} 

PS. That shader has NOT been tested, so some few errors may be present, but I can't spot some right now.

[/spoiler]

This should get you going. wink.png

If any further issues, please reply.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

If you want to use FFP, have a look at

http://www.e-reading-lib.org/bookreader.php/143437/Pike_-_DirectX_8_Programming_Tutorial.html

Everything is described for DX8, but there're not much changes to DX9. When you want to use many lights than I would prefer to have a look at deferred rendering and use real self-made shaders.

@Auskennfuchs: I actually want to use FFP for lights but I can't get it to work because I'm rendering the meshes with shaders.

@Migi0027: How about point light and spot light? Do I have to use a shader file for the whole game light? Sometimes I'm using shaders file to render certain meshes, how do I use both the current shader file and the light shader file?

EDIT: As Steve_Segreto pointed out, it is possible to combine FFP and vertex shaders when rendering in multiple passes. I didn't know it. And I still cannot imagine how would that work if you want to "use FFP for lights" and "render meshes with shaders", it's probably rather for "render some lights using FFP and some using shaders".

And there are problems with FFP+shader combinations anyway, so it does not invalidate my post too much ;)

You want to use the fixed-function pipeline for lights (device->SetLight(), device->LightEnable()), while rendering your meshes with shaders? Sorry but that won't be possible. If you want to use FFP for lighting, you have to use the quite simple FFP materials.

You cannot really divide the process into "rendering a mesh" and "lighting the mesh" and use FFP for one of those and shaders for the second. How a mesh sufrace looks when rendered is given by how the light interacts with the material, there's a tight connection. The output of your shader must represent the final look of the mesh, you cannot really say to the FFP something like "here's my material shader, please light it up for me with your lights".

In fact you need shaders with all possible combinations of lights and material. There are many ways how to achieve it, from the most flexible (branching using if conditions in the shader) to really having separate shader files (or effect techniques) for every combination. Btw, now I'm talking only about TYPES of lights (directional, point etc.) and materials (textured with diffuse lighting, normal-mapped, light-mapped etc.), details like material and light colors (ambient, diffuse, specular), material texture etc. can be set via shader variables and thus the shader file will of course be the same for a red material and a blue material.

http://aras-p.info/texts/VertexShaderTnL.html

@Tom KQT: Sometimes I will be using a shader file to terrain texture splatting, do I have to modify it for light? If I already made 500 shader files for different effects, do I have to modify the 500 shader file just to make light work on all meshes?

Can I use a separate shader file for light?

You can do multiple passes if you wan't to, and then blend the results together carefully.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

This topic is closed to new replies.

Advertisement