Chapter VIII of the more complex lighting (3)

@

Unity light attenuation

In the foregoing, we mentioned using a Unity texel light attenuation as a lookup table is calculated by the fragment shader. This has the advantage that calculate the attenuation does not depend on the complexity of the mathematical formula, we just use a parameter value to the texture sampling can be. But using texture lookups to calculate the attenuation also has some drawbacks.
● need to get pre-sampling texture, texture and size will also affect the attenuation accuracy
● intuitive, while not easy, so once the data is stored in a lookup table, we will not be able to use other mathematical formulas to calculate the attenuation
but since this ways to improve the performance to a certain extent, but the effect obtained is good in most cases, so this is to use the default Unity texture look-by-pixel manner to calculate a point light source and a spotlight attenuation.

1.1 for light attenuation texture

Unity texture used internally to calculate a _LightTexture0 called decay light. Note that, if we use a cookie to the light source, then the decay texture lookup is _LightTextureB0, but here not to discuss this case. We usually focus on only the texture color values _LightTexture0 diagonal, these values indicate the attenuation value of a point light source in different positions in space. For example, (0,0) indicates an attenuation value coincides with the point light source position, and (1,1) indicate the point farthest point in the attenuation of light in the space of interest.
In order to obtain a point light source to the attenuation value of _LightTexture0 texture sample, we first need to obtain the position of the point source in the space, which is obtained by the transformation matrix _LightMatrix0. We have already know _LightMatrix0 can transform vertices from world space to light space. Thus, we only need to _LightMatrix0 vertex coordinates and world space can be obtained by multiplying the corresponding position in the light space.

float3 lightCoord=mul(_LightMatrix0,float4(i.worldPosition,1)).xyz;

Then, we can use this squared coordinates mode attenuation texture sampling, to obtain attenuation values:

fixed atten = tex2D(_LightTexture0,dot(lightCoord,lightCoord).rr).UNITY_ATTEN_CHANNEL;

Can be found in the above code, we used a light source vertex distance squared space (obtained by dot function) texture sampling, there is no reason to use the sampling distance value since this method avoids prescribing operation. Finally, we use a macro UNITY_ATTEN_CHANNEL to get the texture component attenuation attenuation values ​​are located, in order to get the final attenuation values.

1.2 uses a mathematical formula to calculate

Although the method of attenuating texture complexity in calculating the attenuation can be reduced, but sometimes we hope can be calculated using the formula in the attenuation of the light source code. For example, the following code can be calculated linear attenuation of the light source.

float distance = length(_WorldSpaceLightPos.xyz-i.worldPosition.xyz);
atten=1.0/distance;//linear attenuation

Unfortunately, Unity is not given instructions to calculate the attenuation of the built-in document. Sometimes Although we can still use some mathematical formula to calculate the attenuation in the fragment shader, but since we can not pass through the built-in variable range of the light source obtained Shader, spotlight toward the opening angle information, and therefore the effect obtained is not people are intended to make, in particular, the mutation occurs when illumination range of the light source away from the object (this is because, if the object is not within the illumination range of the light source, does not Unity object to perform an Additional Pass). Of course, we can use scripts to pass information source for Shader, but this flexibility is very low. We can only look forward to future versions of Unity can improve the document and develop more parameters to the developers.

Guess you like

Origin www.cnblogs.com/xiegaosen/p/12004777.html