Lit Shader - Light Variables

Hi dear devs,

Currently I’m trying to implement subsurface scattering effect into PlayCanvas just by changing refractionDynamicPS shader chunk. In process, I’ve encountered some difficulties about lights. Currently, lit-shader.js appends each lights code into fragment shader and each lights’ parameters are hard coded like light0_direction (also see below)
backend.append(" dAtten *= getSpotEffect(light" + i + "_direction,

I’m having a hard time reaching every lights variables while developing shader. I tried to set them in an array in lightDirPointPS currently. But light0_color is unreachable. (which means another patch in getFalloffLinear())

#define LIGHTS_AVAILABLE
int pointLightCount = 0;
vec3 lightPointsPos[255];

void getLightDirPoint(vec3 lightPosW) {
    dLightDirW = vPositionW - lightPosW;
    dLightDirNormW = normalize(dLightDirW);
    dLightPosW = lightPosW;
    lightPointsPos[pointLightCount] = dLightPosW;
    pointLightCount = pointLightCount + 1;
}

I think it would be nice to have lights values as arrays.

Btw, I wanna make PR about it but I’m stuck in this process. You can see effects results here: https://twitter.com/defektu_/status/1646825602530385920

Currently it has same light radius and intensity for each light. It would be cool to implement PlayCanvas’ default lights automatically.

3 Likes

We have lights in an “array” when using clustered lighting (lights are stored in the texture). But accessing those is not very easy. I’m not sure how have a better suggestion apart from what you’re doing already.

1 Like

Thanks for the answer @mvaligursky

If I need to use Cluster Lighting, new definition like subsurfaceScatteringPS must be defined after clusteredLightPS to use decodeClusterLightCore(clusterLightData, lightIndex); to retrieve light properties from clusterLightData struct. Because refractionDynamicPS chunk is appended before clusteredLightPS struct and is out of scope. But I got the point about that matter.

Or I might add each SSS in each light (like in evaluateLight() & getLightDiffuse() and add it to dDiffuseLight). But I don’t know if it is performance wise. Any suggestions might help.

Reachable light struct in standart lighting might be helpful for developing shaders I guess.
Also customPS appended after everything like for doing material postFX may be good for easier access to shader chunks. (extensionPS & extensionVS is present in engine but never used.)