How do I make the flag shader rendering both sides?
Asked Answered
O

3

0
shader_type spatial;

uniform float wave_size = 1.0;
uniform float face_distortion = 0.5;
uniform vec2 time_scale = vec2(0.3, 0.0);
uniform vec2 uv_offset_scale = vec2(-0.2, -0.1);

uniform sampler2D uv_offset_texture : hint_black; 

void vertex() {
	// Sample Noise
	vec2 base_uv_offset = UV * uv_offset_scale;
	base_uv_offset += TIME * time_scale;
	float noise = texture(uv_offset_texture, base_uv_offset).r;
	
	// Calculate offset and convert from 0.0 <=> 1.0 to -1.0 <=> 1.0
	float texture_based_offset = noise * 2.0 - 1.0;
	// Apply amplitude and dampening
	texture_based_offset *= wave_size;
	texture_based_offset *= UV.x;
	
	VERTEX.y += texture_based_offset;
	// Distort the face to give impression of conserving shape
	VERTEX.z += texture_based_offset * face_distortion;
	VERTEX.x += texture_based_offset * -face_distortion;
}

void fragment() {
	vec2 base_uv_offset = UV * uv_offset_scale;
	base_uv_offset += TIME * time_scale;
	float noise = texture(uv_offset_texture, base_uv_offset).r;
	// Display noise. Blue for valleys, green for peaks
	ALBEDO = vec3(0.0,noise, 2.0 - noise);
	// Display dampening. Red is full dampening, blue is none
	//ALBEDO = vec3(1.0 - UV.x, 0.0, UV.x);
}

If you walk around the flag in a 3D scene the other side is empty because a plane only has front face. I tried a cube mesh but with no success.

Oconner answered 17/8, 2022 at 15:20 Comment(0)
S
0

For double sided faces.

shader_type spatial;
render_mode cull_disabled;

or

shader_type spatial;
render_mode cull_front;

for culling front faces only and naturally you'd use cull_back to only cull back faces.

Sonni answered 17/8, 2022 at 15:25 Comment(0)
O
0

That fixed it, thanks a bunch.

PS: I don't know what's going on with the code formatting, looks like a forum issue

Oconner answered 17/8, 2022 at 15:32 Comment(0)
S
0

You used inline code tagging for code block as so ` instead of ```

Sonni answered 17/8, 2022 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.