Unity Shader Graph Custom Function Nodes Collection

On this page you can find all of the Unity Shader Graph Custom Function nodes that are used trough out the different Unity Shader Graph Tutorials on this website, as well as some other custom functions that may be of use.


The downloadable package contains all the nodes:

If you want to create the nodes yourself using the .hlsl include files only, then the best way to use nodes is to create a sub-graph for each custom function node, so that you can re-use them easily in different Shader Graphs.

More information on how to create your own Shader Graph Custom Function nodes: https://docs.unity3d.com/Packages/com.unity.shadergraph@6.7/manual/Custom-Function-Node.html

Coordinate Conversions

Cartesian to Spherical Coordinates
Cartesian to spherical coordinates conversion Unity Shader Graph Custom Function node.

// CartesianToSpherical.hlsl

// Cartesian to spherical coordinates conversion.
void CartesianToSpherical_float(float Longitude, float Latitude, out float3 Out){
    float3 p;
    p.y = sin(Latitude * PI);
    p.x = p.y * sin(Longitude * (PI * -2.0));
    p.z = p.y * sin((Longitude + 0.25) * (PI * 2.0));
    p.y = sin((Latitude - 0.5) * PI);
    Out = p;
}

Cartesian to Toroidal Coordinates
Cartesian to toroidal coordinates conversion Unity Shader Graph Custom Function node

// CartesianToToroidal.hlsl

// Cartesian to toroidal coordinates conversion
// where R and r are Major and minor radius and 
// A, B are the angles from 0 to 2*pi.
//
// x = (R-r*CosB)CosA
// y = (R-r*CosB)SinA
// z = r*SinB

void CartesianToToroidal_float(float Longitude, float Latitude,float R, float r, out float3 Out)
{ 
    float A = Longitude * TWO_PI;
    float B = Latitude * TWO_PI;
            
    float cosA = cos(A);
    float sinA = sin(A);
    float cosB = cos(B);
    float sinB = sin(B);

    float x = (R-r*cosB)*cosA;
    float y = (R-r*cosB)*sinA;
    float z = r*sinB;

    float3 toroid = float3(x,y,z);
    Out = toroid;
}

Procedural

Simplex Noise 3D / Simplex Noise 3D Layered
3D Simplex Noise and layered 3D Simplex noise Unity Shader Graph Custom Function nodes.

// SimplexNoise3D.hlsl

inline float3 mod289_3(float3 x){
    return x - floor(x / 289.0) * 289.0;
}
inline float4 mod289_4(float4 x){
    return x - floor(x / 289.0) * 289.0;
}
inline float4 permute(float4 x){
    return mod289_4((x * 34.0 + 1.0) * x);
}
inline float4 taylorInvSqrt(float4 r){
    return 1.79284291400159 - r * 0.85373472095314;
}

inline float4 snoise_grad(float3 v){
    float2 C = float2(1.0 / 6.0, 1.0 / 3.0);
    // First corner
    float3 i  = floor(v + dot(v, C.yyy));
    float3 x0 = v   - i + dot(i, C.xxx);
    // Other corners
    float3 g = step(x0.yzx, x0.xyz);
    float3 l = 1.0 - g;
    float3 i1 = min(g.xyz, l.zxy);
    float3 i2 = max(g.xyz, l.zxy);

    float3 x1 = x0 - i1 + C.xxx;
    float3 x2 = x0 - i2 + C.yyy;
    float3 x3 = x0 - 0.5;
    // Permutations
    i = mod289_3(i); // Avoid truncation effects in permutation
    float4 p =
        permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0))
                              + i.y + float4(0.0, i1.y, i2.y, 1.0))
                              + i.x + float4(0.0, i1.x, i2.x, 1.0));
    // Gradients: 7x7 points over a square, mapped onto an octahedron.
    // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
    float4 j = p - 49.0 * floor(p / 49.0);  // mod(p,7*7)
    float4 x_ = floor(j / 7.0);
    float4 y_ = floor(j - 7.0 * x_);  // mod(j,N)
    float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0;
    float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0;
    float4 h = 1.0 - abs(x) - abs(y);
    float4 b0 = float4(x.xy, y.xy);
    float4 b1 = float4(x.zw, y.zw);
    //float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0;
    //float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0;
    float4 s0 = floor(b0) * 2.0 + 1.0;
    float4 s1 = floor(b1) * 2.0 + 1.0;
    float4 sh = -step(h, 0.0);
    float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
    float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
    float3 g0 = float3(a0.xy, h.x);
    float3 g1 = float3(a0.zw, h.y);
    float3 g2 = float3(a1.xy, h.z);
    float3 g3 = float3(a1.zw, h.w);
    // Normalise gradients
    float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3)));
    g0 *= norm.x;
    g1 *= norm.y;
    g2 *= norm.z;
    g3 *= norm.w;
    // Compute noise and gradient at P
    float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
    float4 m2 = m * m;
    float4 m3 = m2 * m;
    float4 m4 = m2 * m2;
    float3 grad =
        -6.0 * m3.x * x0 * dot(x0, g0) + m4.x * g0 +
        -6.0 * m3.y * x1 * dot(x1, g1) + m4.y * g1 +
        -6.0 * m3.z * x2 * dot(x2, g2) + m4.z * g2 +
        -6.0 * m3.w * x3 * dot(x3, g3) + m4.w * g3;
    float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3));
    return 42.0 * float4(grad, dot(m4, px));
}

float4 EvaluateLayeredNoise(float3 p,float3 offset, float strength, int octaves, float baseRoughness, float roughness,float persistence){
    float4 noiseVector = 0.0;
    float frequency = baseRoughness;
    float amplitude  = 1.0;

    for(int i=0;i<octaves;i++){
        float4 n = snoise_grad(p * frequency + offset);
        noiseVector += (n+1.0) * 0.5 * amplitude;
        frequency *= roughness;
        amplitude *= persistence;
    }
    return noiseVector * strength;
}

void SimplexNoise3D_float(float3 In, out float Noise, out float3 Gradient){
    float4 noise_vector;
    noise_vector = snoise_grad(In);        
    Noise = noise_vector.w;
    Gradient = noise_vector.xyz;
}

void SimplexNoise3DLayered_float(float3 In,float3 Offset,float Strength,int Octaves,float BaseRoughness,float Roughness,float Persistence, out float Noise, out float3 Gradient){
    float4 noise_vector;
    noise_vector = EvaluateLayeredNoise(In,Offset,Strength,Octaves,BaseRoughness, Roughness, Persistence);
    Noise = noise_vector.w;
    Gradient = noise_vector.xyz;
}

Cellular Noise 3D / Cellular Noise 3D Layered
3D Cellular Noise and layered 3D Cellular noise Unity Shader Graph Custom Function nodes.

// CellularNoise3D.hlsl

// Cellular noise ("Worley noise") in 3D in GLSL.
// Copyright (c) Stefan Gustavson 2011-04-19. All rights reserved.
// This code is released under the conditions of the MIT license.
// See LICENSE file for details.
// https://github.com/stegu/webgl-noise

// Modulo 289 without a division (only multiplications)
float3 mod289(float3 x){
  return x - floor(x * (1.0 / 289.0)) * 289.0;
}
// Modulo 7 without a division
float3 mod7(float3 x){
  return x - floor(x * (1.0 / 7.0)) * 7.0;
}
// Permutation polynomial: (34x^2 + x) mod 289
float3 permute(float3 x){
  return mod289((34.0 * x + 1.0) * x);
}

// Cellular noise, returning F1 and F2 in a float2.
// 3x3x3 search region for good F2 everywhere, but a lot
// slower than the 2x2x2 version.
// The code below is a bit scary even to its author,
// but it has at least half decent performance on a
// modern GPU. In any case, it beats any software
// implementation of Worley noise hands down.

float2 cellular(float3 P){
#define K 0.142857142857 // 1/7
#define Ko 0.428571428571 // 1/2-K/2
#define K2 0.020408163265306 // 1/(7*7)
#define Kz 0.166666666667 // 1/6
#define Kzo 0.416666666667 // 1/2-1/6*2
#define jitter 1.0 // smaller jitter gives more regular pattern

	float3 Pi = mod289(floor(P));
 	float3 Pf = frac(P) - 0.5;

	float3 Pfx = Pf.x + float3(1.0, 0.0, -1.0);
	float3 Pfy = Pf.y + float3(1.0, 0.0, -1.0);
	float3 Pfz = Pf.z + float3(1.0, 0.0, -1.0);

	float3 p = permute(Pi.x + float3(-1.0, 0.0, 1.0));
	float3 p1 = permute(p + Pi.y - 1.0);
	float3 p2 = permute(p + Pi.y);
	float3 p3 = permute(p + Pi.y + 1.0);

	float3 p11 = permute(p1 + Pi.z - 1.0);
	float3 p12 = permute(p1 + Pi.z);
	float3 p13 = permute(p1 + Pi.z + 1.0);

	float3 p21 = permute(p2 + Pi.z - 1.0);
	float3 p22 = permute(p2 + Pi.z);
	float3 p23 = permute(p2 + Pi.z + 1.0);

	float3 p31 = permute(p3 + Pi.z - 1.0);
	float3 p32 = permute(p3 + Pi.z);
	float3 p33 = permute(p3 + Pi.z + 1.0);

	float3 ox11 = frac(p11*K) - Ko;
	float3 oy11 = mod7(floor(p11*K))*K - Ko;
	float3 oz11 = floor(p11*K2)*Kz - Kzo; // p11 < 289 guaranteed

	float3 ox12 = frac(p12*K) - Ko;
	float3 oy12 = mod7(floor(p12*K))*K - Ko;
	float3 oz12 = floor(p12*K2)*Kz - Kzo;

	float3 ox13 = frac(p13*K) - Ko;
	float3 oy13 = mod7(floor(p13*K))*K - Ko;
	float3 oz13 = floor(p13*K2)*Kz - Kzo;

	float3 ox21 = frac(p21*K) - Ko;
	float3 oy21 = mod7(floor(p21*K))*K - Ko;
	float3 oz21 = floor(p21*K2)*Kz - Kzo;

	float3 ox22 = frac(p22*K) - Ko;
	float3 oy22 = mod7(floor(p22*K))*K - Ko;
	float3 oz22 = floor(p22*K2)*Kz - Kzo;

	float3 ox23 = frac(p23*K) - Ko;
	float3 oy23 = mod7(floor(p23*K))*K - Ko;
	float3 oz23 = floor(p23*K2)*Kz - Kzo;

	float3 ox31 = frac(p31*K) - Ko;
	float3 oy31 = mod7(floor(p31*K))*K - Ko;
	float3 oz31 = floor(p31*K2)*Kz - Kzo;

	float3 ox32 = frac(p32*K) - Ko;
	float3 oy32 = mod7(floor(p32*K))*K - Ko;
	float3 oz32 = floor(p32*K2)*Kz - Kzo;

	float3 ox33 = frac(p33*K) - Ko;
	float3 oy33 = mod7(floor(p33*K))*K - Ko;
	float3 oz33 = floor(p33*K2)*Kz - Kzo;

	float3 dx11 = Pfx + jitter*ox11;
	float3 dy11 = Pfy.x + jitter*oy11;
	float3 dz11 = Pfz.x + jitter*oz11;

	float3 dx12 = Pfx + jitter*ox12;
	float3 dy12 = Pfy.x + jitter*oy12;
	float3 dz12 = Pfz.y + jitter*oz12;

	float3 dx13 = Pfx + jitter*ox13;
	float3 dy13 = Pfy.x + jitter*oy13;
	float3 dz13 = Pfz.z + jitter*oz13;

	float3 dx21 = Pfx + jitter*ox21;
	float3 dy21 = Pfy.y + jitter*oy21;
	float3 dz21 = Pfz.x + jitter*oz21;

	float3 dx22 = Pfx + jitter*ox22;
	float3 dy22 = Pfy.y + jitter*oy22;
	float3 dz22 = Pfz.y + jitter*oz22;

	float3 dx23 = Pfx + jitter*ox23;
	float3 dy23 = Pfy.y + jitter*oy23;
	float3 dz23 = Pfz.z + jitter*oz23;

	float3 dx31 = Pfx + jitter*ox31;
	float3 dy31 = Pfy.z + jitter*oy31;
	float3 dz31 = Pfz.x + jitter*oz31;

	float3 dx32 = Pfx + jitter*ox32;
	float3 dy32 = Pfy.z + jitter*oy32;
	float3 dz32 = Pfz.y + jitter*oz32;

	float3 dx33 = Pfx + jitter*ox33;
	float3 dy33 = Pfy.z + jitter*oy33;
	float3 dz33 = Pfz.z + jitter*oz33;

	float3 d11 = dx11 * dx11 + dy11 * dy11 + dz11 * dz11;
	float3 d12 = dx12 * dx12 + dy12 * dy12 + dz12 * dz12;
	float3 d13 = dx13 * dx13 + dy13 * dy13 + dz13 * dz13;
	float3 d21 = dx21 * dx21 + dy21 * dy21 + dz21 * dz21;
	float3 d22 = dx22 * dx22 + dy22 * dy22 + dz22 * dz22;
	float3 d23 = dx23 * dx23 + dy23 * dy23 + dz23 * dz23;
	float3 d31 = dx31 * dx31 + dy31 * dy31 + dz31 * dz31;
	float3 d32 = dx32 * dx32 + dy32 * dy32 + dz32 * dz32;
	float3 d33 = dx33 * dx33 + dy33 * dy33 + dz33 * dz33;

	// Sort out the two smallest distances (F1, F2)
#if 0
	// Cheat and sort out only F1
	float3 d1 = min(min(d11,d12), d13);
	float3 d2 = min(min(d21,d22), d23);
	float3 d3 = min(min(d31,d32), d33);
	float3 d = min(min(d1,d2), d3);
	d.x = min(min(d.x,d.y),d.z);
	return float2(sqrt(d.x)); // F1 duplicated, no F2 computed
#else
	// Do it right and sort out both F1 and F2
	float3 d1a = min(d11, d12);
	d12 = max(d11, d12);
	d11 = min(d1a, d13); // Smallest now not in d12 or d13
	d13 = max(d1a, d13);
	d12 = min(d12, d13); // 2nd smallest now not in d13
	float3 d2a = min(d21, d22);
	d22 = max(d21, d22);
	d21 = min(d2a, d23); // Smallest now not in d22 or d23
	d23 = max(d2a, d23);
	d22 = min(d22, d23); // 2nd smallest now not in d23
	float3 d3a = min(d31, d32);
	d32 = max(d31, d32);
	d31 = min(d3a, d33); // Smallest now not in d32 or d33
	d33 = max(d3a, d33);
	d32 = min(d32, d33); // 2nd smallest now not in d33
	float3 da = min(d11, d21);
	d21 = max(d11, d21);
	d11 = min(da, d31); // Smallest now in d11
	d31 = max(da, d31); // 2nd smallest now not in d31
	d11.xy = (d11.x < d11.y) ? d11.xy : d11.yx;
	d11.xz = (d11.x < d11.z) ? d11.xz : d11.zx; // d11.x now smallest
	d12 = min(d12, d21); // 2nd smallest now not in d21
	d12 = min(d12, d22); // nor in d22
	d12 = min(d12, d31); // nor in d31
	d12 = min(d12, d32); // nor in d32
	d11.yz = min(d11.yz,d12.xy); // nor in d12.yz
	d11.y = min(d11.y,d12.z); // Only two more to go
	d11.y = min(d11.y,d11.z); // Done! (Phew!)
	return sqrt(d11.xy); // F1, F2
#endif
}

float2 EvaluateLayeredCellularNoise(float3 p,float3 offset, float strength, int octaves, float baseRoughness, float roughness,float persistence){
    float2 noiseVector = 0.0;
    float frequency = baseRoughness;
    float amplitude  = 1.0;

    for(int i=0;i<octaves;i++){
        float2 n = cellular(p * frequency + offset);
        noiseVector += (n+1.0) * 0.5 * amplitude;
        frequency *= roughness;
        amplitude *= persistence;
    }

    return noiseVector * strength;
}

void CellularNoise3D_float(float3 In, out float F1,out float F2){
    float2 noise;
    noise = cellular(In);
    F1 = noise.x;
    F2 = noise.y;
}

void CellularNoise3DLayered_float(float3 In,float3 Offset,float Strength,int Octaves,float BaseRoughness,float Roughness,float Persistence,out float F1,out float F2){
    float2 noise;
    noise = EvaluateLayeredCellularNoise(In,Offset,Strength,Octaves,BaseRoughness, Roughness, Persistence);
    F1 = noise.x;
    F2 = noise.y;
}

Checkerboard 3D
3D Checkerboard pattern with different interpolation methods.

// CheckerBoard3D.hlsl

// Interpolation functions.
float NearestNeighbour(float a,float b,float t){
	return t < 0.5 ? a : b;
}
// float lerp(float a,float b,float t) {
// 	return a + t * (b - a);
// }
float Cosine(float a,float b,float t){
	t = (1 - cos(t * PI)) * 0.5;
	return a + t * (b - a);
}

void Checkerboard3D_float(float3 Position, float Size, out float Out){
	float size = Size;
	float isize = 1.0 / Size;
	
    float x0 = floor(Position.x * isize);
    float y0 = floor(Position.y * isize);
    float z0 = floor(Position.z * isize);
    
    float x0v = x0 % 2;
    float y0v = y0 % 2;
    float z0v = z0 % 2;
    
    float value = (x0v + y0v + z0v) % 2 ? 1 : -1;

    Out = value;
}

void Checkerboard3DLinear_float(float3 Position, float Size, out float Out){
	float size = Size;
	float isize = 1 / Size;
	
    float x0 = floor(Position.x * isize);
    float y0 = floor(Position.y * isize);
    float z0 = floor(Position.z * isize);
    
    float x0v = x0 % 2;
    float y0v = y0 % 2;
    float z0v = z0 % 2;
    
    float value = (x0v + y0v + z0v) % 2 ? 1 : -1;
    
    float tx = (Position.x - x0 * size) * isize;
    float ty = (Position.y - y0 * size) * isize;
    float tz = (Position.z - z0 * size) * isize;
    
    float lx = lerp(value, -value, tx);
    float ly = lerp(lx, -lx, ty);
    float lz = lerp(ly, -ly, tz);
    
    Out = lz;
}

void Checkerboard3DNearest_float(float3 Position, float Size, out float Out){
	float size = Size;
	float isize = 1.0 / Size;
	
    float x0 = floor(Position.x * isize);
    float y0 = floor(Position.y * isize);
    float z0 = floor(Position.z * isize);
    
    float x0v = x0 % 2;
    float y0v = y0 % 2;
    float z0v = z0 % 2;
    
    float value = (x0v + y0v + z0v) % 2 ? 1 : -1;
    
    float tx = (Position.x - x0 * size) * isize;
    float ty = (Position.y - y0 * size) * isize;
    float tz = (Position.z - z0 * size) * isize;
    
    float lx = NearestNeighbour(value, -value, tx);
    float ly = NearestNeighbour(lx, -lx, ty);
    float lz = NearestNeighbour(ly, -ly, tz);
    
    Out = lz;
}

void Checkerboard3DCosine_float(float3 Position, float Size, out float Out){
	float size = Size;
	float isize = 1.0 / Size;
	
    float x0 = floor(Position.x * isize);
    float y0 = floor(Position.y * isize);
    float z0 = floor(Position.z * isize);
    
    float x0v = x0 % 2;
    float y0v = y0 % 2;
    float z0v = z0 % 2;
    
    float value = (x0v + y0v + z0v) % 2 ? 1 : -1;
    
    float tx = (Position.x - x0 * size) * isize;
    float ty = (Position.y - y0 * size) * isize;
    float tz = (Position.z - z0 * size) * isize;
    
    float lx = Cosine(value, -value, tx);
    float ly = Cosine(lx, -lx, ty);
    float lz = Cosine(ly, -ly, tz);
    
    Out = lz;
}

Input

Lighting

MainLight
Main scene light input Unity Shader Graph Custom Function node.

// CustomLighting.hlsl

#ifndef CUSTOM_LIGHTING_INCLUDED
#define CUSTOM_LIGHTING_INCLUDED

void MainLight_float(float3 WorldPos, out float3 Direction, out float3 Color, out float DistanceAtten, out float ShadowAtten){
#if SHADERGRAPH_PREVIEW
    Direction = float3(0.5, 0.5, 0);
    Color = 1;
    DistanceAtten = 1;
    ShadowAtten = 1;
#else
    #if SHADOWS_SCREEN
        float4 clipPos = TransformWorldToHClip(WorldPos);
        float4 shadowCoord = ComputeScreenPos(clipPos);
    #else
        float4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    #endif
    Light mainLight = GetMainLight(shadowCoord);
    Direction = mainLight.direction;
    Color = mainLight.color;
    DistanceAtten = mainLight.distanceAttenuation;
    ShadowAtten = mainLight.shadowAttenuation;
#endif
}

void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten){
#if SHADERGRAPH_PREVIEW
    Direction = half3(0.5, 0.5, 0);
    Color = 1;
    DistanceAtten = 1;
    ShadowAtten = 1;
#else
    #if SHADOWS_SCREEN
        half4 clipPos = TransformWorldToHClip(WorldPos);
        half4 shadowCoord = ComputeScreenPos(clipPos);
    #else
        half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    #endif
    Light mainLight = GetMainLight(shadowCoord);
    Direction = mainLight.direction;
    Color = mainLight.color;
    DistanceAtten = mainLight.distanceAttenuation;
    ShadowAtten = mainLight.shadowAttenuation;
#endif
}

void DirectSpecular_float(float3 Specular, float Smoothness, float3 Direction, float3 Color, float3 WorldNormal, float3 WorldView, out float3 Out){
#if SHADERGRAPH_PREVIEW
    Out = 0;
#else
    Smoothness = exp2(10 * Smoothness + 1);
    WorldNormal = normalize(WorldNormal);
    WorldView = SafeNormalize(WorldView);
    Out = LightingSpecular(Color, Direction, WorldNormal, WorldView, float4(Specular, 0), Smoothness);
#endif
}

void DirectSpecular_half(half3 Specular, half Smoothness, half3 Direction, half3 Color, half3 WorldNormal, half3 WorldView, out half3 Out){
#if SHADERGRAPH_PREVIEW
    Out = 0;
#else
    Smoothness = exp2(10 * Smoothness + 1);
    WorldNormal = normalize(WorldNormal);
    WorldView = SafeNormalize(WorldView);
    Out = LightingSpecular(Color, Direction, WorldNormal, WorldView,half4(Specular, 0), Smoothness);
#endif
}

void AdditionalLights_float(float3 SpecColor, float Smoothness, float3 WorldPosition, float3 WorldNormal, float3 WorldView, out float3 Diffuse, out float3 Specular){
    float3 diffuseColor = 0;
    float3 specularColor = 0;

#ifndef SHADERGRAPH_PREVIEW
    Smoothness = exp2(10 * Smoothness + 1);
    WorldNormal = normalize(WorldNormal);
    WorldView = SafeNormalize(WorldView);
    int pixelLightCount = GetAdditionalLightsCount();
    for (int i = 0; i < pixelLightCount; ++i){
        Light light = GetAdditionalLight(i, WorldPosition);
        half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
        diffuseColor += LightingLambert(attenuatedLightColor, light.direction, WorldNormal);
        specularColor += LightingSpecular(attenuatedLightColor, light.direction, WorldNormal, WorldView, float4(SpecColor, 0), Smoothness);
    }
#endif

    Diffuse = diffuseColor;
    Specular = specularColor;
}

void AdditionalLights_half(half3 SpecColor, half Smoothness, half3 WorldPosition, half3 WorldNormal, half3 WorldView, out half3 Diffuse, out half3 Specular)
{
    half3 diffuseColor = 0;
    half3 specularColor = 0;

#ifndef SHADERGRAPH_PREVIEW
    Smoothness = exp2(10 * Smoothness + 1);
    WorldNormal = normalize(WorldNormal);
    WorldView = SafeNormalize(WorldView);
    int pixelLightCount = GetAdditionalLightsCount();
    for (int i = 0; i < pixelLightCount; ++i)
    {
        Light light = GetAdditionalLight(i, WorldPosition);
        half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
        diffuseColor += LightingLambert(attenuatedLightColor, light.direction, WorldNormal);
        specularColor += LightingSpecular(attenuatedLightColor, light.direction, WorldNormal, WorldView, half4(SpecColor, 0), Smoothness);
    }
#endif

    Diffuse = diffuseColor;
    Specular = specularColor;
}

#endif

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.