Shadow Gradient

(Google translator)
Simple shader that applies a gradient, using “multiply,” to the model’s texture from the initial height (relative to the model’s size) to a final height.

In the image, the model’s origin is located in the center and at the bottom of the model.

In this case, the render_mode is set to “diffuse toon.”

Parameters:

  • Textura: The model’s texture.
  • Color Arriba: The color applied to the top of the model.
  • Color Abajo: The color applied to the bottom of the model.
  • Altura mínima: The height from the model’s origin from which the gradient is created (from the bottom of the model).
  • Altura máxima: The height of the model up to which the gradient is created.
Shader code
shader_type spatial;
render_mode diffuse_toon;

uniform sampler2D textura;

uniform vec3 color_arriba: source_color = vec3(0.0, 1.0, 0.0);
uniform vec3 color_abajo: source_color = vec3(1.0, 0.0, 0.0);
// Altura mínima (relativa al modelo, 0 es la base) desde la que empieza el degradado
uniform float altura_minima = 0.0;
// Altura maxima (relativa al modelo, 0 es la base) termina el degradado
uniform float altura_maxima = 6.0;

varying vec3 world_pos;

void vertex() {
	// Posición en el mundo del fragmento para el fragment
	world_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
}

void fragment() {
	// Color de la sombra, degradado desde altura_maxima a altura mínima
	float altura = world_pos.y - NODE_POSITION_WORLD.y;
	float altura_normalizada = clamp((altura - altura_minima) / (altura_maxima - altura_minima), 0.0, 1.0);
	vec3 color_degradado = mix(color_abajo, color_arriba, altura_normalizada);

	// Color de la textura
	vec3 color = texture(textura, UV).rgb;

	// Hacemos un multiply:
	ALBEDO = color * color_degradado;
}
Live Preview
Tags
gradient, shadow
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments