Hey everyone, stupid question, but I’ve been trying for an hour and haven’t been able to figure it out on my own.
Anyone know how to properly get the light power and distance of additional lights?
I’ve got a really simple eye scelera shader that I just want to grab the light colour and distance, ignoring angles and shadows and everything. It should effectively be treating all additional lights as if they were point lights with shadows disabled (and also completely disregard shadows all together)
However, I haven’t been able to get my shader to get any additional lights at all. Here’s where it’s at at the moment:
Shader "Custom/EyeScelera"
{
Properties
{
_BaseTex("Base Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" "UniversalMaterialType" = "Lit"}
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
// Declare all material properties in a single CBUFFER for SRP Batcher compatibility
CBUFFER_START(UnityPerMaterial)
TEXTURE2D(_BaseTex);
SAMPLER(sampler_BaseTex);
CBUFFER_END
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
half3 lightAmount : TEXCOORD2;
};
Varyings vert(Attributes IN)
{
Varyings OUT;
// Transform object space to clip space
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
// Calculate lighting
Light mainLight = GetMainLight();
// Just get the brightness of the light, don't bother calculating normals or whatever
OUT.lightAmount = mainLight.color;
uint totalAdditional = GetAdditionalLightsCount();
float3 positionWs = GetVertexPositionInputs(IN.positionOS.xyz).positionWS;
LIGHT_LOOP_BEGIN(totalAdditional)
Light light = GetAdditionalLight(lightIndex, positionWs);
OUT.lightAmount += light.color * light.distanceAttenuation;
LIGHT_LOOP_END
// Pass UV for texture sampling
OUT.uv = IN.uv;
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
// Sample the base texture
half3 baseColor = SAMPLE_TEXTURE2D(_BaseTex, sampler_BaseTex, IN.uv).rgb;
// Combine the base texture with lighting
half3 finalColor = baseColor * IN.lightAmount;
return half4(finalColor, 1.0);
}
ENDHLSL
}
}
}
Here’s what’s actually happening in my scene, though:
I know my code should have a lot more #ifdefs and stuff, I just tried to strip away all the possible culprits and really nail down what is going on.
If I manually specified GetAdditionalLight() and just hard coded indexes, it would successfully get those additional lights (in no particular order), and I can confirm the main issue here seems to be the that GetAdditionalLightsCount is consistently returning 0 even though there’s undoubtedly additional lights there.
I’d also appreciate any tips on how to get ambient light (like light probes, and the environmental lighting) data, cos I could’ve really find anything concrete on that either.
Anyways, it’s really late here so I gotta give up for the night. If anybody more experienced than me can see the obvious thing I must be doing wrong, please let me know