Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Created July 27, 2024 19:44
Show Gist options
  • Save partybusiness/53848600a741fc8de5f18963b3cc4f98 to your computer and use it in GitHub Desktop.
Save partybusiness/53848600a741fc8de5f18963b3cc4f98 to your computer and use it in GitHub Desktop.
Some screen-space gradients and texture shaders that account for position of mesh
shader_type spatial;
render_mode unshaded;
//renders vertical gradient that centres on node's position
uniform vec4 _TopColour:source_color = vec4(1.0,1.0,1.0,1.0);
uniform vec4 _BottomColour:source_color = vec4(0.0,0.0,0.0,1.0);
uniform float _Height = 1.0;
uniform float _exp = 1.0;
void fragment() {
float ypos = VERTEX.y/VERTEX.z - NODE_POSITION_VIEW.y/NODE_POSITION_VIEW.z;
ypos = ypos / (_Height / -NODE_POSITION_VIEW.z) + 0.5;
ypos = pow(ypos, _exp);
ALBEDO = mix (_TopColour, _BottomColour, clamp(ypos,0.0,1.0)).rgb;
}
shader_type spatial;
render_mode unshaded;
//renders gradient that centres on node's position
//uses vertical direction of mesh to set direction of gradient on screen
// so it can rotate to directions other than straight vertical
uniform vec4 _TopColour:source_color = vec4(1.0,1.0,1.0,1.0);
uniform vec4 _BottomColour:source_color = vec4(0.0,0.0,0.0,1.0);
uniform float _Height = 1.0;
uniform float _exp = 1.0;
varying vec2 gradient_dir;
void vertex() {
vec4 top_calc = (MODELVIEW_MATRIX * vec4(0.0,1.0,0.0,1.0));
vec2 top_point = vec2(top_calc.x,top_calc.y)/top_calc.z;
vec2 bottom_point = NODE_POSITION_VIEW.xy/NODE_POSITION_VIEW.z;
gradient_dir = normalize(top_point-bottom_point);
}
void fragment() {
vec2 vuv = vec2(VERTEX.x, VERTEX.y)/VERTEX.z - NODE_POSITION_VIEW.xy/NODE_POSITION_VIEW.z; //unscaled, centred on
float par = dot(vuv, gradient_dir) / (_Height / -NODE_POSITION_VIEW.z) / 2.0 + 0.5;
par = pow(par, _exp);
ALBEDO = mix (_TopColour, _BottomColour, clamp(par,0.0,1.0)).rgb;
}
shader_type spatial;
render_mode unshaded;
//displays a screen space texture on a mesh, that's centred on the position of the node
uniform float _scale = 1.0;
uniform sampler2D _texture:source_color;
void fragment() {
vec2 vuv = vec2(VERTEX.x, VERTEX.y)/VERTEX.z - NODE_POSITION_VIEW.xy/NODE_POSITION_VIEW.z;
ALBEDO.rgb = texture(_texture, vuv/_scale - 0.5).rgb;
}
shader_type spatial;
render_mode unshaded;
//displays a screen space texture on a mesh, that's centred on the position of the node
//the size of the texture on-screen scales with the distance of the mesh from the camera
uniform float _scale = 1.0;
uniform sampler2D _texture:source_color;
void fragment() {
vec2 vuv = vec2(VERTEX.x, VERTEX.y)/VERTEX.z - NODE_POSITION_VIEW.xy/NODE_POSITION_VIEW.z;
ALBEDO.rgb = texture(_texture, vuv/(_scale/-NODE_POSITION_VIEW.z) - 0.5).rgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment