generated from nathanfranke/gdextension
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a Mesh Normals propset with a dropdown to select from three opt…
…ions for always-pixel, always-vertex, or pick-by-distance (the default). Added defines to main.glsl so when normals are calculated always-vertex, it completely bypasses the call to get_normal and normalizes v_normal inline. Added a define to get_normal so always-vertex or by-distance will both check for region edges, but by-dstance also checks for range. Added support for calculating noise height on the cpu.
- Loading branch information
1 parent
ea1db16
commit 998ffd9
Showing
9 changed files
with
294 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
project/addons/terrain_3d/shader/vertex/t3d_bg_world_noise_type1_funcs.gdshaderinc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// WORLD_NOISE1 | ||
|
||
//float hashv2(vec2 v) { | ||
// return fract(1e4 * sin(17.0 * v.x + v.y * 0.1) * (0.1 + abs(sin(v.y * 13.0 + v.x)))); } | ||
|
||
// Explicitly Optimized - changes made should have (mostly) already been inlined by the compiler but why take chances. | ||
float ihashv2(ivec2 iv) { | ||
vec2 v = sin( vec2( iv * ivec2(17, 13) ) + vec2( float(iv.y) * 0.1, float(iv.x) ) ); | ||
v.y = ( abs(v.y) + 0.1 ); | ||
return fract( 1e4 * v.x * v.y ); } | ||
|
||
// alternate optimization, unclear which performs better between this and above. | ||
//float hashv2(ivec2 iv) { | ||
// vec2 v = vec2(iv); | ||
// v = sin( vec2( | ||
// fma( 17., v.x, v.y * 0.1 ), | ||
// fma( 13., v.y, v.x ) ) ); | ||
// return fract( 1e4 * v.x * ( 0.1 + abs(v.y) ) ); } | ||
|
||
/* vec3 orig_noise2D(vec2 x) { | ||
vec2 f = fract(x); | ||
// Quintic Hermine Curve. Similar to SmoothStep() | ||
vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); | ||
vec2 du = 30.0*f*f*((f*f)-(f*2.0)+1.0); | ||
// vec2 du = 30.0*f*f*(f*(f-2.0)+1.0); // Original | ||
|
||
vec2 p = floor(x); | ||
|
||
// Four corners in 2D of a tile | ||
float a = hashv2( p+vec2(0,0) ); | ||
float b = hashv2( p+vec2(1,0) ); | ||
float c = hashv2( p+vec2(0,1) ); | ||
float d = hashv2( p+vec2(1,1) ); | ||
|
||
// Mix 4 corner percentages | ||
float k0 = a; | ||
float k1 = b - a; | ||
float k2 = c - a; | ||
float k3 = a - b - c + d; | ||
return vec3( k0 + k1 * u.x + k2 * u.y + k3 * u.x * u.y, | ||
du * ( vec2(k1, k2) + k3 * u.yx) ); } */ | ||
|
||
// https://iquilezles.org/articles/morenoise/ | ||
// Explicitly Optimized - changes made should already be in effect from compiler optimization but that's not always the case. | ||
vec3 noise2D(vec2 x) { | ||
const ivec2 i01 = ivec2(0,1); | ||
|
||
vec2 f = fract(x); | ||
// Quintic Hermine Curve. Similar to SmoothStep() | ||
vec2 f2 = f*f; | ||
vec4 udu = f2.xyxy * vec4( | ||
f * (fma(f, (fma(f, vec2(6.0), -vec2(15.0))), vec2(10.0))), | ||
30.0 * (fma (f, (f - 2.0), vec2(1.0)))); | ||
ivec2 p = ivec2(floor(x)); | ||
|
||
// Four corners in 2D of a tile | ||
vec4 abcd = vec4( ihashv2(p), ihashv2(p + i01.yx), ihashv2(p + i01.xy), ihashv2(p + 1) ); | ||
|
||
// Mix 4 corner percentages | ||
vec3 k123 = abcd.yzx - abcd.xxy; | ||
k123.z = k123.z - abcd.z + abcd.w; | ||
udu.zw *= fma(udu.yx, k123.zz, k123.xy); | ||
k123 *= udu.xyx; | ||
return vec3( fma(k123.z, udu.y, abcd.x + k123.x + k123.y), udu.zw ); } | ||
|
||
float bg_world(vec2 p) { | ||
float a = 0.0; | ||
float b = 1.0; | ||
vec2 d = vec2(0.0); | ||
int octaves = int( clamp( | ||
float(_bg_world_max_octaves) - floor(v_vertex_xz_dist/(_bg_world_lod_distance)), | ||
float(_bg_world_min_octaves), float(_bg_world_max_octaves)) ); | ||
for( int i=0; i < octaves; i++ ) { | ||
vec3 n = noise2D(p); | ||
d += n.yz; | ||
a += b * n.x / (1.0 + dot(d,d)); | ||
b *= 0.5; | ||
p = mat2( vec2(0.8, -0.6), vec2(0.6, 0.8) ) * p * 2.0; } | ||
return a; } | ||
|
||
// World Noise end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#pragma once | ||
|
||
#ifndef TERRAIN3D_NORMALS_H | ||
#define TERRAIN3D_NORMALS_H | ||
|
||
//Code Templates for Terrain3D Managed GLSL/Godot NORMALS Properties | ||
|
||
// ################################################################################ | ||
#pragma region _NORMALS_ | ||
|
||
// ******************* | ||
// ** NORMALS ** | ||
// ******************* | ||
|
||
#pragma region _HELP_ | ||
#define HELP_NORMS()\ | ||
ADD_HELP_TOPIC_DECL(normals, R"( | ||
A mesh normal is the outward facing direction of a surface at any point. | ||
3D graphics smoothly shifts that normal over the entire surface. How | ||
that normal is calculated impacts quality and speed inversely, with | ||
per-pixel providing the highest quality, but per-vertex having the fastest | ||
speed. A third option is available, where beyond a certain distance | ||
it uses per-pixel, because the mesh density is much lower there and it | ||
looks better per-pixel. But up-close where the mesh density is very high, | ||
per-pixel is less necessary. It still looks better but it's harder to tell | ||
and in many situations it might be good enough, and offer faster speeds. | ||
|
||
The Distance setting lets you adjust the vertex/pixel range if the By_Distance | ||
option is selected. | ||
)") | ||
#pragma endregion _HELP_ | ||
|
||
#pragma region _normals_helper_macros_ | ||
#define GETR_NORMS(m_bind_id, m_type) GSTR( normals_##m_bind_id, m_type ) | ||
#define VAR_NORMS(m_bind_id, m_type, m_value) VARR( normals_##m_bind_id, m_type##, m_value ) | ||
#define PROP_NORMS(m_member_id, m_var_type, ...) __PROP_GENGRP( normals, m_member_id, m_var_type, __VA_ARGS__ ) | ||
#define BIND_NORMS(m_bind_id, m_param_label) ADD_BIND( normals_##m_bind_id, m_param_label ) | ||
#define SETUPD_NORMS(m_bind_id, m_type, s_log_desc) SETUPD__GRP( normals, normals_##m_bind_id, m_type, s_log_desc ) | ||
#define _SETPR_NORMS(m_bind_id, m_type, s_log_desc) __SETPR_GRP( normals, normals_##m_bind_id, m_type, s_log_desc ) | ||
#define _UPDSH_NORMS(m_member_id) UPDATE_SHADER( normals_##m_member_id ) | ||
#pragma endregion _normals_helper_macros_ | ||
|
||
#define UPDATE_NORMALS() UPDATE_GROUP(normals) | ||
|
||
#define PRIV_NORMALS_VARS()\ | ||
GROUP_VARS(normals)\ | ||
VAR_NORMS(quality, NormalCalculation, BY_DISTANCE)\ | ||
VAR_NORMS(distance, float, 128.0f) | ||
|
||
#define PUBLIC_NORMALS_FUNCS()\ | ||
GETR_NORMS(quality, NormalCalculation)\ | ||
GETR_NORMS(distance, float) | ||
|
||
#define BIND_NORMALS_VARS() \ | ||
BIND_NORMS(quality, strategy)\ | ||
BIND_NORMS(distance, range) | ||
|
||
#define PROPS_NORMALS()\ | ||
ADD_GROUP("Mesh Normals", "normals_");\ | ||
PROP_NORMS(quality, INT, ENUM, "Pixel,Vertex,By_Distance")\ | ||
PROP_NORMS(distance, FLOAT, RANGE, "0.0,1024.0,1., or_greater")\ | ||
ADD_INLINE_HELP(normals, Mesh Normals, About Mesh Normals) | ||
|
||
#define MAKE_NORMALS_FUNCTIONS() \ | ||
UPDATE_GRP_START_CON(normals, true)\ | ||
_UPDSH_NORMS(distance)\ | ||
UPDATE_GRP_END()\ | ||
SETUPD_NORMS(quality, NormalCalculation, "Quality: ")\ | ||
_SETPR_NORMS(distance, float, "Distance: ") | ||
|
||
#pragma endregion _NORMALS_ | ||
// ################################################################################ | ||
#endif |
Oops, something went wrong.