Vertex Animation

Vertex offset achieves the wind animation of tree leaves. By specifying the vertex color, vertex shader can use lerp to filter the animation on a certain part of the mesh. For example, only leaves will wave by painting all leaves’ color red.

URP

Sample Texture

https://forum.unity.com/threads/writing-shaders-urp-texture2d-sampler.1000782/

TEXTURE2D()translates the texture to the target API’s native type. GLES2 is sampler2D and others are Texture2D.

Maintenance

pow() vs. sqrt()

After compile pow(a, b) in shader, the code will become exp2(log2(a) * b )

Universal Render Pipeline

main light shadow macro define: https://forum.unity.com/threads/shadow-cascades-weird-since-7-2-0.828453/ https://forum.unity.com/threads/lwrp-questions.769868/

  • _MAIN_LIGHT_SHADOWS_CASCADE and _MAIN_LIGHT_SHADOWS

In Shadow.hlsl:

#if !defined(_RECEIVE_SHADOWS_OFF)
    #if defined(_MAIN_LIGHT_SHADOWS)
        #define MAIN_LIGHT_CALCULATE_SHADOWS

        #if !defined(_MAIN_LIGHT_SHADOWS_CASCADE)
            #define REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR
        #endif
    #endif

    #if defined(_ADDITIONAL_LIGHT_SHADOWS)
        #define ADDITIONAL_LIGHT_CALCULATE_SHADOWS
    #endif
#endif

If the object is receiving shadow, and the main light cast shadow, URP will define MAIN_LIGHT_CALCULATE_SHADOWS.

If shadow cascade was not defined, then vertex shadow coord interpolator is required.

if additional lights cast shadow, shader relies on these macros to calculate additional shadows too.

  • _ADDITIONAL_LIGHTS_VERTEX and _ADDITIONAL_LIGHTS

If URP Assets’ lighting section has enabled additional lights, these keywords will be defined to indicate to calculate per vertex or per pixel

  • _ADDITIONAL_LIGHT_SHADOWS

Whether additional lights cast shadows?

  • _SHADOWS_SOFT

Is the shadow soft?

  • LIGHTMAP_SHADOW_MIXING and SHADOWS_SHADOWMASK

When the Light’s mode is mixed, and Mixed Lighting is either subtractive or shadowmask, the 2 keywords would be defined. It usually works with light map.

Parallax Effect

parallax effect: https://smartslider3.com/blog/parallax-effect/ Harry Alisavakis, Parallax Effect https://halisavakis.com/my-take-on-shaders-parallax-effect-part-i/

It creates an illusion of depth by adding layers moving by view direction. It is commonly used in 2D platformer games' background. Or website design. In shader, we could use it to add interesting visual effect, such as details beneath the glass(https://twitter.com/SchaefferAustin/status/996896783765852160?s=20) or cracking glacier (https://www.youtube.com/watch?v=rlGNbq5p5CQ).

The key is to sample the texture with uv offset, which is calculated by view direction in tangent space.

View Direction in Tangent Space

// if UV's derivative in V is negative, tangent.w is -1
// https://forum.unity.com/threads/question-about-tangents-in-shader.171448/
bitangent = cross(tangent, normal) * tangent.w;
float3x3 tanToWorldMatrix = flaot3x3(tangent, bitangent, normal);
float3 viewDirTS = mul(tanToWorldMatrix, viewDirWS);

Then we use it to calculate offset

uvOffset = viewDirTS * _Offset
tex2D(_Texture, uv + uvOffset)