hlsl

float frac(float v)
{
    return v - floor(v);
}

fmod(x, y) returns the floating-point remainder of the x parameter divided by the y parameter

float remap(float v, float2 inMinMax, float2 outMinMax)
{
	return outMinMax.x + (v - inMinMax.x) * (outMinMax.y - outMinMax.x) / (inMinMax.y - inMinMax.x);
}
float fresnelEffect(float3 normal, float3 viewDir, float power)
{
	return pow(1.0 - saturate(dot(normalize(normal), normalize(viewDir))), power)
}

Shader

Posterization / Posterisation is the conversion of a continuous gradation of tone to several regions of fewer tones, with abrupt changes from one to another.

void Unity_Posterize_float4(float4 In, float4 steps, out float4 Out)
{
	Out = floor( In/(1/Steps)) * (1/Steps);
}

Shader Graph: Random Range Node It returns a pseudo-random number between min and max based on the seed. The same seed always gives the same result.

void Unity_RandomRange_float(float2 Seed, float Min, float Max, out float Out)
{
    float randomno =  frac(sin(dot(Seed, float2(12.9898, 78.233)))*43758.5453);
    Out = lerp(Min, Max, randomno);
}

Toggle and keyword

[Toggle(_EXAMPLE_KEYWORD] _ExampleKeyword("Keyword", Float) = 1

#pragma shader_feature_loca _EXAMPLE_KEYWORD

The above statements work together to enable and disable the keyword.

Sprite shaders

To access the sprite, we either declare a [PerRendererData] _MainTex in ShaderLab or create a texture property with a reference named by _MainTex.

Glitch Effect

To create the glitch effect, we can simply move around the vertex (3D models) or UV (2D sprites). The random offset happens in every time interval. We can add another layer of randomness to decide if this glitch effect would appear. To add some animation to the shader, like vertex displacement or uv flow, we usually use the _Time, because it keeps changing. We can use it directly or plug in it to some equations, like sine or cosine wave.

Color changing is also part of the glitch.

To control the effect’s visibility or intensity, from a bit of vertex offset to a strongly distorted model, we can create another float value to multiply the glitch result, or plug it in functions like lerp() to change the max or min value.

Multiplication can mix two textures or two colors, but the result will be black if one of the tex/color is black. Black is 0, and anything multiplied by 0 is 0.

Shader Properties Attributes

Add certain attributes to the properties can help us unexpectedly.

Examples:

[NoScaleOffset] removes the tilling and offset.

[Toggle] works like true and false (int). We can add a #pragma shader_feature to create branches.

[enum] and [keywordEnum] gives a menu for single option

[IntRange] makes Range(0,10) assign integers only.

Source:

  1. 【UnityShader】ShaderLabのプロパティ属性 【1】#77, https://soramamenatan.hatenablog.com/entry/2020/11/01/110554
  2. 【UnityShader】ShaderLabのプロパティ属性 【2】#78, https://soramamenatan.hatenablog.com/entry/2020/11/08/094204