Posted By: Dooley
Waving Grass with Dynamic Light - 03/11/18 21:58
I am looking at the animTree.fx shader, and I am wondering if I can make it work with dynamic lighting. Right now, it works with sunlight, but if I turn on the character's flashlight (a dynamic light) all the trees/grass around me turn dark.
This page explains how to add dynamic lights to a shader: http://opserver.de/twik11/index.php?title=Shader_hints
However, if I add anything to it, it stops the waving animation. This is the part I'm trying to add:
I don't know much about shaders, so maybe I'm adding this code to the wrong section or in the wrong way. I tried the waving grass shader on the wiki, but that one looks even worse.
Has anyone made a waving grass shader that reacts properly to dynamic lights? Or would anyone know how to add this lighting effect to the existing animTree.fx shader?
This page explains how to add dynamic lights to a shader: http://opserver.de/twik11/index.php?title=Shader_hints
However, if I add anything to it, it stops the waving animation. This is the part I'm trying to add:
Quote:
float4 vecLightPos[8];
float4 vecLightColor[8];
float3 vecFalloff = float3(0.f, 0.f, 2.f);
...
float4 DoPointLight(float3 P, float3 N, int i)
{
// calculate the light ray pointing from the light to the surface
float3 D = (float3)vecLightPos[i]-P;
// calculate the angle between surface and light ray
float NdotL = dot(N,normalize(D));
// modulate the light by the surface angle
float4 Color = vecLightColor[i] * NdotL;
// calculate the light attenuation factor, DX uses a really strange formula here
float fac = 0.f;
if (NdotL >= 0.f && vecLightPos[i].w > 0.f)
{
// get the distance factor
float LD = length(D)/vecLightPos[i].w;
#ifdef DXLIGHTING
if (LD < 1.3f)
fac = 1.f/(vecFalloff.x + vecFalloff.y*LD + vecFalloff.z*LD*LD);
#else // linear Lighting
if (LD < 1.f)
fac = 1.f - LD;
#endif
}
return Color * fac;
}
float4 vecLightColor[8];
float3 vecFalloff = float3(0.f, 0.f, 2.f);
...
float4 DoPointLight(float3 P, float3 N, int i)
{
// calculate the light ray pointing from the light to the surface
float3 D = (float3)vecLightPos[i]-P;
// calculate the angle between surface and light ray
float NdotL = dot(N,normalize(D));
// modulate the light by the surface angle
float4 Color = vecLightColor[i] * NdotL;
// calculate the light attenuation factor, DX uses a really strange formula here
float fac = 0.f;
if (NdotL >= 0.f && vecLightPos[i].w > 0.f)
{
// get the distance factor
float LD = length(D)/vecLightPos[i].w;
#ifdef DXLIGHTING
if (LD < 1.3f)
fac = 1.f/(vecFalloff.x + vecFalloff.y*LD + vecFalloff.z*LD*LD);
#else // linear Lighting
if (LD < 1.f)
fac = 1.f - LD;
#endif
}
return Color * fac;
}
I don't know much about shaders, so maybe I'm adding this code to the wrong section or in the wrong way. I tried the waving grass shader on the wiki, but that one looks even worse.
Has anyone made a waving grass shader that reacts properly to dynamic lights? Or would anyone know how to add this lighting effect to the existing animTree.fx shader?