|
|
|
|
|
|
RenderLoop.renderLoopDelegate -= Render; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Main entry point for our scriptable render loop
|
|
|
|
bool Render (Camera[] cameras, RenderLoop loop) |
|
|
|
{ |
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Setup lighting variables for shader to use
|
|
|
|
// We only support up to 8 visible lights here. More complex approaches would
|
|
|
|
// be doing some sort of per-object light setups, but here we go for simplest possible
|
|
|
|
// approach.
|
|
|
|
// Just take first 8 lights. Possible improvements: sort lights by intensity or distance
|
|
|
|
// to the viewer, so that "most important" lights in the scene are picked, and not the 8
|
|
|
|
// that happened to be first.
|
|
|
|
// x - light count
|
|
|
|
// y - zero (needed by d3d9 VS loop instruction; initial loop value)
|
|
|
|
// z - one (needed by d3d9 VS loop instruction; loop increment)
|
|
|
|
// w - unused
|
|
|
|
Vector4 lightCountVector = new Vector4 (lightCount, 0, 1, 0); |
|
|
|
// Prepare light data
|
|
|
|
Vector4[] lightColors = new Vector4[kMaxLights]; |
|
|
|
Vector4[] lightPositions = new Vector4[kMaxLights]; |
|
|
|
Vector4[] lightSpotDirections = new Vector4[kMaxLights]; |
|
|
|
|
|
|
lightColors[i] = light.finalColor; |
|
|
|
if (light.lightType == LightType.Directional) |
|
|
|
{ |
|
|
|
// light position for directional lights is: (-direction, 0)
|
|
|
|
// light position for point/spot lights is: (position, 1)
|
|
|
|
// float atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
|
|
|
|
// float atten = 1.0 / (1.0 + lengthSq * LightAtten[i].z);
|
|
|
|
// float rho = max (0, dot(normalize(toLight), unity_SpotDirection[i].xyz));
|
|
|
|
// float spotAtt = (rho - unity_LightAtten[i].x) * unity_LightAtten[i].y;
|
|
|
|
// float rho = max (0, dot(normalize(toLight), SpotDirection[i].xyz));
|
|
|
|
// float spotAtt = (rho - LightAtten[i].x) * LightAtten[i].y;
|
|
|
|
// spotAtt = saturate(spotAtt);
|
|
|
|
// and the above works for all light types, i.e. spot light code works out
|
|
|
|
// to correct math for point & directional lights as well.
|
|
|
|
|
|
|
float quadAtten; |
|
|
|
if (light.lightType == LightType.Directional) |
|
|
|
{ |
|
|
|
quadAtten = 0.0f; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
quadAtten = 25.0f / rangeSq; |
|
|
|
} |
|
|
|
float quadAtten = (light.lightType == LightType.Directional) ? 0.0f : 25.0f / rangeSq; |
|
|
|
|
|
|
|
// spot direction & attenuation
|
|
|
|
if (light.lightType == LightType.Spot) |
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// ambient lighting spherical harmonics values
|
|
|
|
|
|
|
|
// setup global shader variables to contain all the data computed above
|
|
|
|
cmd.SetGlobalVector ("globalLightCount", lightCountVector); |
|
|
|
cmd.SetGlobalVector ("globalLightCount", new Vector4 (lightCount, 0, 0, 0)); |
|
|
|
|
|
|
|
// Prepare L2 spherical harmonics values for efficient evaluation in a shader
|
|
|
|
static void GetShaderConstantsFromNormalizedSH (ref SphericalHarmonicsL2 ambientProbe, Vector4[] outCoefficients) |
|
|
|
{ |
|
|
|
for (int channelIdx = 0; channelIdx < 3; ++channelIdx) |
|
|
|