rj-action-library/Runtime/Shading/Library/Normals.gdshaderinc

56 lines
1.6 KiB
Plaintext
Raw Normal View History

// #include "res://addons/rokojori_action_library/Runtime/Shading/Library/Normals.gdshaderinc"
2025-09-17 08:25:03 +00:00
vec3 computeNormalFromHeightMap( sampler2D _heightMap, vec2 _uv, float size, float strength )
{
float l = texture( _heightMap, _uv + vec2( -size, 0 ) ).r * strength;
float r = texture( _heightMap, _uv + vec2( size, 0 ) ).r * strength;
float d = texture( _heightMap, _uv + vec2( 0, -size ) ).r * strength;
float u = texture( _heightMap, _uv + vec2( 0, size ) ).r * strength;
return normalize( vec3( l - r, 2.0 * size, d - u ) );
}
vec3 scaleNormalMap( vec3 normal, float scale )
{
normal -= vec3( 0.5 );
normal *= scale;
normal += vec3( 0.5 );
return normal;
}
vec3 addNormalMaps( vec3 a, vec3 b )
{
a -= vec3( 0.5 );
b -= vec3( 0.5 );
vec3 result = a + b;
return result + 0.5;
2025-08-31 06:05:39 +00:00
}
vec3 getNormal( sampler2D _normalTexture, vec2 screenUV )
{
return textureLod( _normalTexture, screenUV, 0.0 ).rgb * 2.0 - vec3( 1.0 );
}
vec3 getNormalAtViewPosition( sampler2D _normalTexture, vec3 viewPosition, mat4 _PROJECTION_MATRIX )
{
vec2 screenPosition = viewToScreen( viewPosition, _PROJECTION_MATRIX );
return getNormal( _normalTexture, screenPosition );
}
vec4 getNormalRoughness( sampler2D _normalTexture, vec2 screenUV )
{
vec4 sample = textureLod( _normalTexture, screenUV, 0.0 );
return vec4( sample.rgb * 2.0 - vec3( 1.0 ), sample.a );
}
vec4 getNormalRoughnessAtViewPosition( sampler2D _normalTexture, vec3 viewPosition, mat4 _PROJECTION_MATRIX )
{
vec2 screenPosition = viewToScreen( viewPosition, _PROJECTION_MATRIX );
return getNormalRoughness( _normalTexture, screenPosition );
}