So here is my shader. In my shader I tried to render the lights from loading it into sky material. In my scene, I had a skybox with meshes and loaidng texture material. If I had nothing wrong, the shader that I loaded by sky texture will render just on the surface of that texture right. But I want to loadthe lights into world space to light up the scene, I do not need any shadow for the objects in the scene, I just want to make lights in the scene and control all the lights from shader. The game engine that I had, It is already load the light into the scene, but it is default light effect system that ogre3d rendered and could not change into light or dark effect for scene.
can you guys help me to check, fix,or give me some Ideas to make It work. I really appreciate you guys.
Code: Select all
float4x4 World;
float4x4 View;
float4x4 Projection;
float4 CameraPosition;
// Light 1 properties
float4 Light1Position = float4(0.0, 10.0, 0.0, 1.0); // World position of Light 1
float4 Light1Diffuse = float4(0.921569, 0.639216, 0.211765, 1.0); // Diffuse color of Light 1
float4 Light1Specular = float4(0.184314, 0.368627, 0.360784, 1.0); // Specular color of Light 1
float Light1Attenuation = 0.1; // Attenuation factor of Light 1
// Light 2 properties
float4 Light2Position = float4(5.0, 10.0, 0.0, 1.0); // World position of Light 2
float4 Light2Diffuse = float4(0.203922, 0.156863, 0.647059, 1.0); // Diffuse color of Light 2
float4 Light2Specular = float4(0.184314, 0.368627, 0.360784, 1.0); // Specular color of Light 2
float Light2Attenuation = 0.2; // Attenuation factor of Light 2
// Light 3 properties
float4 Light3Position = float4(-5.0, 10.0, 0.0, 1.0); // World position of Light 3
float4 Light3Diffuse = float4(0.5, 0.8, 0.3, 1.0); // Diffuse color of Light 3
float4 Light3Specular = float4(0.2, 0.6, 0.8, 1.0); // Specular color of Light 3
float Light3Attenuation = 0.15; // Attenuation factor of Light 3
// Light 4 properties
float4 Light4Position = float4(0.0, 15.0, 5.0, 1.0); // World position of Light 4
float4 Light4Diffuse = float4(0.7, 0.1, 0.2, 1.0); // Diffuse color of Light 4
float4 Light4Specular = float4(0.9, 0.3, 0.4, 1.0); // Specular color of Light 4
float Light4Attenuation = 0.05; // Attenuation factor of Light 4
// Light 5 properties
float4 Light5Position = float4(-5.0, 15.0, 5.0, 1.0); // World position of Light 5
float4 Light5Diffuse = float4(0.2, 0.5, 0.7, 1.0); // Diffuse color of Light 5
float4 Light5Specular = float4(0.4, 0.2, 0.5, 1.0); // Specular color of Light 5
float Light5Attenuation = 0.25; // Attenuation factor of Light 5
sampler2D baseTexture : register(s0);
struct VS_IN
{
float4 inPos : POSITION;
float3 inNormal : NORMAL;
};
struct VS_OUT
{
float4 Position : POSITION; // Transformed position
float3 WorldPos : TEXCOORD0; // World space position
float3 Normal : TEXCOORD1; // World space normal
};
// Function to calculate the lighting contribution from a single light
float3 calculateLight(float3 normal, float3 lightPos, float3 lightDiffuse, float3 lightSpecular, float3 fragPos, float3 viewDir, float attenuation)
{
float3 lightDir = normalize(lightPos - fragPos);
float distance = length(lightPos - fragPos);
float attenuationFactor = 1.0 / (1.0 + attenuation * distance * distance);
// Diffuse shading
float diffuseFactor = max(dot(normal, lightDir), 0.0);
float3 diffuse = lightDiffuse * diffuseFactor * attenuationFactor;
// Specular shading
float3 reflectDir = reflect(-lightDir, normal);
float specularFactor = pow(max(dot(viewDir, reflectDir), 0.0), 32);
float3 specular = lightSpecular * specularFactor * attenuationFactor;
return diffuse + specular;
}
VS_OUT mainVS(VS_IN input)
{
VS_OUT output;
// Transform vertex position to world space
float4 worldPos = mul(World, input.inPos);
output.WorldPos = worldPos.xyz;
// Transform normal to world space
output.Normal = mul((float3x3)World, input.inNormal);
// Transform position to clip space
output.Position = mul(mul(View, Projection), worldPos);
return output;
}
float4 mainPS(VS_OUT input) : COLOR
{
// Normalize normal vector
float3 normal = normalize(input.Normal);
// Calculate view direction
float3 viewDir = normalize(CameraPosition.xyz - input.WorldPos);
// Accumulate lighting contributions from all 5 lights
float3 lighting = float3(0.0, 0.0, 0.0);
lighting += calculateLight(normal, Light1Position.xyz, Light1Diffuse.xyz, Light1Specular.xyz, input.WorldPos, viewDir, Light1Attenuation);
lighting += calculateLight(normal, Light2Position.xyz, Light2Diffuse.xyz, Light2Specular.xyz, input.WorldPos, viewDir, Light2Attenuation);
lighting += calculateLight(normal, Light3Position.xyz, Light3Diffuse.xyz, Light3Specular.xyz, input.WorldPos, viewDir, Light3Attenuation);
lighting += calculateLight(normal, Light4Position.xyz, Light4Diffuse.xyz, Light4Specular.xyz, input.WorldPos, viewDir, Light4Attenuation);
lighting += calculateLight(normal, Light5Position.xyz, Light5Diffuse.xyz, Light5Specular.xyz, input.WorldPos, viewDir, Light5Attenuation);
// Add ambient light
float4 ambient = tex2D(baseTexture, float2(0.5, 0.5)) * 0.1;
float4 finalColor = float4(lighting, 1.0) + ambient;
return finalColor;
}