Dinomaly Procedural Wood texture

A simple procedural wood shader I made for my point and click horror game. Built off-of Procedural Grain Wood Shader for Godot Engine

 

The shader generates soft, wavy growth rings from layered value noise and sine-wave distortion, then adds fine directional grain to break up the larger pattern. Two colour controls define the light and dark areas of the resulting wood.

I also added a small emissive ambient tint, to try and retain the flat, constant fill lighting associated with older pre-rendered CGI environments.

 

Usage: Create a ShaderMaterial in Godot, paste in the shader, and apply the material to a UV-mapped mesh. Adjust light_color, dark_color, ring_size, wave_scale, random_scale, grain_amount, and bump_strength to change the species, scale, and intensity of the wood grain.

Shader code
shader_type spatial;

// Two ends of wood gradient
uniform vec3 light_color : source_color = vec3(0.9, 0.9, 0.75); // pale wood color
uniform vec3 dark_color  : source_color = vec3(0.75, 0.6, 0.5); // dark growth rings

// flat ambient tint since old textures did not have bounce light, shadows are a constant fill color
uniform vec3 ambient : source_color = vec3(0.15, 0.12, 0.1);

// ------------------------ Pattern Controls ------------------------- //
uniform float ring_size     = 4.4; // how many rings scattered across the plank ( higher = tighter rings )
uniform float wave_scale    = 8.5;
uniform float random_scale  = 4.6;
uniform float grain_amount  = 0.08;
uniform float bump_strength = 1.5;
uniform float shine : hint_range(1.0, 256.0) = 48.0;
uniform float specular_strength : hint_range(0.0, 1.0) = 0.4;


// ------------------------ Random Hash --------------------------- //
// Turns a 2D position into two pseudo-random numbers between 0 and 1
// The dot() products mix x and y into one number, sin() scrambles it,
// and multiplying by a huge number then taking fract() throws away everything except the chaotic fractional digits.
vec2 random(vec2 pos) {
	return fract(sin(vec2(
		dot(pos, vec2(12.9898, 78.233)),
		dot(pos, vec2(-148.998, -65.233))
	)) * 43758.5453);
}
// ---------------------------------------------------------------------- //


// ------------------------- Value Noise -------------------------- //
// Smooth noise built from the random hash

float value_noise(vec2 pos) {
	vec2 p = floor(pos);   // which grid cell we're in
	vec2 f = fract(pos);   // where inside that cell (0..1)
	
	// random value at each of the cell's four corners
	float v00 = random(p).x;
	float v10 = random(p + vec2(1.0, 0.0)).x;
	float v01 = random(p + vec2(0.0, 1.0)).x;
	float v11 = random(p + vec2(1.0, 1.0)).x;
	
	// smoothstep curve so blending eases in and out (no visible grid lines)
	vec2 u = f * f * (3.0 - 2.0 * f);
	
	//bilinear blend of the four corners using the eased weights
	return mix(mix(v00, v10, u.x), mix(v01, v11, u.x), u.y);
}
// ---------------------------------------------------------------------- //


// ------------------------------ Wood Pattern --------------------------- //
// Returns 0..1, where 0 = light wood and 1 = dark ring.
float wood(vec2 uv) {
	uv = uv.yx;
	vec2 warped_uv = uv;
	
	warped_uv.x += value_noise(uv * random_scale);
	float ring_coords = warped_uv.x + sin(warped_uv.y * wave_scale);
	float grain = value_noise(vec2(uv.x * 90.0, uv.y * 3.0));
	
	float ring_phase = ring_coords * ring_size + grain * grain_amount;
	
	// fract(v) is a sawtooth (hard edge each ring). Remapping to
	// abs(fract*2-1) turns it into a triangle wave (light > dark > light),
	// and smoothstep rounds the corners. Result: soft rings, no hard seam.
	return smoothstep(0.0, 1.0, abs(fract(ring_phase) * 2.0 - 1.0));
}
// ---------------------------------------------------------------------- //


void fragment() {
	float wood_mask = wood(UV);                       
	
	ALBEDO = mix(light_color, dark_color, wood_mask); 
	EMISSION = ALBEDO * ambient;              
	
	// Fake bump mapping
	float e = 0.002;                          
	float wood_mask_x = wood(UV + vec2(e, 0.0));
	float wood_mask_y = wood(UV + vec2(0.0, e));
	
	NORMAL_MAP = normalize(vec3(
		(wood_mask - wood_mask_x) * bump_strength * 20.0,
		(wood_mask - wood_mask_y) * bump_strength * 20.0, 1.0)
	) * 0.5 + 0.5;
}
Tags
Data/Generation, Procedural, wood
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Comments 0

No comments yet.