|
|
|
|
|
|
public static int _AdditionalLightCount; |
|
|
|
public static int _AdditionalLightPosition; |
|
|
|
public static int _AdditionalLightColor; |
|
|
|
public static int _AdditionalLightDistanceAttenuation; |
|
|
|
public static int _AdditionalLightAttenuation; |
|
|
|
public static int _AdditionalLightSpotAttenuation; |
|
|
|
|
|
|
|
public static int _LightIndexBuffer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Vector4 k_DefaultLightPosition = new Vector4(0.0f, 0.0f, 1.0f, 0.0f); |
|
|
|
Vector4 k_DefaultLightColor = Color.black; |
|
|
|
Vector4 k_DefaultLightAttenuation = new Vector4(0.0f, 1.0f, 0.0f, 1.0f); |
|
|
|
Vector4 k_DefaultLightAttenuation = new Vector4(1.0f, 0.0f, 0.0f, 1.0f); |
|
|
|
Vector4 k_DefaultLightSpotAttenuation = new Vector4(0.0f, 1.0f, 0.0f, 0.0f); |
|
|
|
Vector4[] m_LightDistanceAttenuations; |
|
|
|
Vector4[] m_LightAttenuations; |
|
|
|
Vector4[] m_LightSpotAttenuations; |
|
|
|
|
|
|
|
private int maxVisibleLocalLights { get; set; } |
|
|
|
private ComputeBuffer perObjectLightIndices { get; set; } |
|
|
|
|
|
|
PerCameraBuffer._AdditionalLightCount = Shader.PropertyToID("_AdditionalLightCount"); |
|
|
|
PerCameraBuffer._AdditionalLightPosition = Shader.PropertyToID("_AdditionalLightPosition"); |
|
|
|
PerCameraBuffer._AdditionalLightColor = Shader.PropertyToID("_AdditionalLightColor"); |
|
|
|
PerCameraBuffer._AdditionalLightDistanceAttenuation = Shader.PropertyToID("_AdditionalLightDistanceAttenuation"); |
|
|
|
PerCameraBuffer._AdditionalLightAttenuation = Shader.PropertyToID("_AdditionalLightAttenuation"); |
|
|
|
PerCameraBuffer._AdditionalLightSpotAttenuation = Shader.PropertyToID("_AdditionalLightSpotAttenuation"); |
|
|
|
m_LightDistanceAttenuations = new Vector4[0]; |
|
|
|
m_LightAttenuations = new Vector4[0]; |
|
|
|
m_LightSpotAttenuations = new Vector4[0]; |
|
|
|
} |
|
|
|
|
|
|
|
public void Setup(int maxVisibleLocalLights, ComputeBuffer perObjectLightIndices) |
|
|
|
|
|
|
{ |
|
|
|
m_LightPositions = new Vector4[maxVisibleLocalLights]; |
|
|
|
m_LightColors = new Vector4[maxVisibleLocalLights]; |
|
|
|
m_LightDistanceAttenuations = new Vector4[maxVisibleLocalLights]; |
|
|
|
m_LightAttenuations = new Vector4[maxVisibleLocalLights]; |
|
|
|
m_LightSpotAttenuations = new Vector4[maxVisibleLocalLights]; |
|
|
|
void InitializeLightConstants(List<VisibleLight> lights, int lightIndex, out Vector4 lightPos, out Vector4 lightColor, out Vector4 lightDistanceAttenuation, out Vector4 lightSpotDir, |
|
|
|
out Vector4 lightSpotAttenuation) |
|
|
|
void InitializeLightConstants(List<VisibleLight> lights, int lightIndex, out Vector4 lightPos, out Vector4 lightColor, out Vector4 lightAttenuation, out Vector4 lightSpotDir) |
|
|
|
lightDistanceAttenuation = k_DefaultLightSpotAttenuation; |
|
|
|
lightAttenuation = k_DefaultLightAttenuation; |
|
|
|
lightSpotAttenuation = k_DefaultLightAttenuation; |
|
|
|
|
|
|
|
// When no lights are visible, main light will be set to -1.
|
|
|
|
// In this case we initialize it to default values and return
|
|
|
|
|
|
|
|
|
|
|
// VisibleLight.finalColor already returns color in active color space
|
|
|
|
lightColor = lightData.finalColor; |
|
|
|
lightColor.w = 0.0f; |
|
|
|
|
|
|
|
// Directional Light attenuation is initialize so distance attenuation always be 1.0
|
|
|
|
if (lightData.lightType != LightType.Directional) |
|
|
|
|
|
|
float fadeRangeSqr = (fadeStartDistanceSqr - lightRangeSqr); |
|
|
|
float oneOverFadeRangeSqr = 1.0f / fadeRangeSqr; |
|
|
|
float lightRangeSqrOverFadeRangeSqr = -lightRangeSqr / fadeRangeSqr; |
|
|
|
float quadAtten = 25.0f / lightRangeSqr; |
|
|
|
lightDistanceAttenuation = new Vector4(quadAtten, oneOverFadeRangeSqr, lightRangeSqrOverFadeRangeSqr, 1.0f); |
|
|
|
lightAttenuation.x = oneOverFadeRangeSqr; |
|
|
|
lightAttenuation.y = lightRangeSqrOverFadeRangeSqr; |
|
|
|
lightColor.w = 1.0f; |
|
|
|
} |
|
|
|
|
|
|
|
if (lightData.lightType == LightType.Spot) |
|
|
|
|
|
|
float smoothAngleRange = Mathf.Max(0.001f, cosInnerAngle - cosOuterAngle); |
|
|
|
float invAngleRange = 1.0f / smoothAngleRange; |
|
|
|
float add = -cosOuterAngle * invAngleRange; |
|
|
|
lightSpotAttenuation = new Vector4(invAngleRange, add, 0.0f); |
|
|
|
lightAttenuation.z = invAngleRange; |
|
|
|
lightAttenuation.w = add; |
|
|
|
} |
|
|
|
|
|
|
|
Light light = lightData.light; |
|
|
|
|
|
|
if (m_MixedLightingSetup == MixedLightingSetup.None && lightData.light.shadows != LightShadows.None) |
|
|
|
{ |
|
|
|
m_MixedLightingSetup = MixedLightingSetup.Subtractive; |
|
|
|
lightDistanceAttenuation.w = 0.0f; |
|
|
|
lightColor.w = 0.0f; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
for (int i = 0; i < maxVisibleLocalLights; ++i) |
|
|
|
InitializeLightConstants(lightData.visibleLights, -1, out m_LightPositions[i], |
|
|
|
out m_LightColors[i], |
|
|
|
out m_LightDistanceAttenuations[i], |
|
|
|
out m_LightSpotDirections[i], |
|
|
|
out m_LightSpotAttenuations[i]); |
|
|
|
out m_LightAttenuations[i], |
|
|
|
out m_LightSpotDirections[i]); |
|
|
|
|
|
|
|
m_MixedLightingSetup = MixedLightingSetup.None; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SetupMainLightConstants(CommandBuffer cmd, ref LightData lightData) |
|
|
|
{ |
|
|
|
Vector4 lightPos, lightColor, lightDistanceAttenuation, lightSpotDir, lightSpotAttenuation; |
|
|
|
Vector4 lightPos, lightColor, lightAttenuation, lightSpotDir; |
|
|
|
InitializeLightConstants(lightData.visibleLights, lightData.mainLightIndex, out lightPos, out lightColor, out lightDistanceAttenuation, out lightSpotDir, out lightSpotAttenuation); |
|
|
|
InitializeLightConstants(lightData.visibleLights, lightData.mainLightIndex, out lightPos, out lightColor, out lightAttenuation, out lightSpotDir); |
|
|
|
|
|
|
|
if (lightData.mainLightIndex >= 0) |
|
|
|
{ |
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
cmd.SetGlobalVector(PerCameraBuffer._MainLightPosition, new Vector4(lightPos.x, lightPos.y, lightPos.z, lightDistanceAttenuation.w)); |
|
|
|
cmd.SetGlobalVector(PerCameraBuffer._MainLightPosition, new Vector4(lightPos.x, lightPos.y, lightPos.z, 1.0f)); // TODO: lightDistanceAttenuation.w));
|
|
|
|
cmd.SetGlobalVector(PerCameraBuffer._MainLightColor, lightColor); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
InitializeLightConstants(lights, i, out m_LightPositions[localLightsCount], |
|
|
|
out m_LightColors[localLightsCount], |
|
|
|
out m_LightDistanceAttenuations[localLightsCount], |
|
|
|
out m_LightSpotDirections[localLightsCount], |
|
|
|
out m_LightSpotAttenuations[localLightsCount]); |
|
|
|
out m_LightAttenuations[localLightsCount], |
|
|
|
out m_LightSpotDirections[localLightsCount]); |
|
|
|
localLightsCount++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
cmd.SetGlobalVectorArray(PerCameraBuffer._AdditionalLightPosition, m_LightPositions); |
|
|
|
cmd.SetGlobalVectorArray(PerCameraBuffer._AdditionalLightColor, m_LightColors); |
|
|
|
cmd.SetGlobalVectorArray(PerCameraBuffer._AdditionalLightDistanceAttenuation, m_LightDistanceAttenuations); |
|
|
|
cmd.SetGlobalVectorArray(PerCameraBuffer._AdditionalLightAttenuation, m_LightAttenuations); |
|
|
|
cmd.SetGlobalVectorArray(PerCameraBuffer._AdditionalLightSpotAttenuation, m_LightSpotAttenuations); |
|
|
|
} |
|
|
|
|
|
|
|
void SetShaderKeywords(CommandBuffer cmd, ref CameraData cameraData, ref LightData lightData, ref ShadowData shadowData) |
|
|
|