Weird Shader Issue
Asked Answered
T

16

0

I wanted to get a cool water shader for my game so I went to godotshaders.com and found this one added it to the game and did some modifications to add triplaner mapping to the texturing,

shader_type spatial;
render_mode blend_mix, specular_phong;

uniform float speed : hint_range(-1,1) = 0.0;

//colors
uniform sampler2D noise1; //add Godot noise here
uniform sampler2D noise2; //add Godot noise here
uniform sampler2D normalmap : hint_normal; //add Godot noise here, enable as_normalmap
uniform vec4 color : hint_color;
uniform vec4 edge_color : hint_color;

//foam
uniform float edge_scale = 0.25;
uniform float near = 0.1;
uniform float far = 100.0;

//waves
uniform vec2 wave_strengh = vec2(0.5, 0.25);
uniform vec2 wave_frequency = vec2(12.0, 12.0);
uniform vec2 time_factor = vec2(1.0, 2.0);

varying vec3 uv1_triplanar_pos;
varying vec3 uv1_power_normal;
uniform vec3 uv1_scale = vec3(1.0,1.0,1.0);
uniform vec3 uv1_offset = vec3(0.0,0.0,0.0);

float rim(float depth){
	depth = 2.0 * depth - 1.0;
	return near * far / (far + depth * (near - far));
}

float waves(vec2 pos, float time){
	return (wave_strengh.y * sin(pos.y * wave_frequency.y + time * time_factor.y)) + (wave_strengh.x * sin(pos.x * wave_frequency.x + time * time_factor.x));
}

vec4 tri_map (sampler2D p_sampler,vec3 p_triplanar_pos) {
	vec4 samp=vec4(0.0);
	samp+= texture(p_sampler,p_triplanar_pos.xy) * uv1_power_normal.z;
	samp+= texture(p_sampler,p_triplanar_pos.xz) * uv1_power_normal.y;
	samp+= texture(p_sampler,p_triplanar_pos.zy * vec2(-1.0,1.0)) * uv1_power_normal.x;
	return samp;
}

void vertex(){
	VERTEX.y += waves(VERTEX.xy, TIME);
	
	TANGENT = vec3(0.0,0.0,-1.0) * abs(NORMAL.x);
	TANGENT+= vec3(1.0,0.0,0.0) * abs(NORMAL.y);
	TANGENT+= vec3(1.0,0.0,0.0) * abs(NORMAL.z);
	TANGENT = normalize(TANGENT);
	BINORMAL = vec3(0.0,1.0,0.0) * abs(NORMAL.x);
	BINORMAL+= vec3(0.0,0.0,-1.0) * abs(NORMAL.y);
	BINORMAL+= vec3(0.0,1.0,0.0) * abs(NORMAL.z);
	BINORMAL = normalize(BINORMAL);
	uv1_power_normal=pow(abs(NORMAL),vec3(1.0));
	uv1_power_normal/=dot(uv1_power_normal,vec3(1.0));
	uv1_triplanar_pos = VERTEX * uv1_scale + (uv1_offset + vec3(0.0,sin(TIME*speed*4.0),0.0));
	uv1_triplanar_pos *= vec3(1.0,-1.0, 1.0);
}

void fragment(){
	float time = TIME * speed;
	vec3 n1 = tri_map(noise1, uv1_triplanar_pos + time).rgb;
	vec3 n2 = tri_map(noise2, uv1_triplanar_pos - time * 0.2).rgb;
	
	vec2 uv_movement = UV * 4.0;
	uv_movement += TIME * speed * 4.0;
	
	float sum = (n1.r + n2.r) - 1.0;
	
	float z_depth = rim(texture(DEPTH_TEXTURE, SCREEN_UV).x);
	float z_pos = rim(FRAGCOORD.z);
	float diff = z_depth - z_pos;
	
	vec2 displacement = vec2(sum * 0.05);
	diff += displacement.x * 50.0;
	
	vec4 col = mix(edge_color, color, step(edge_scale, diff));
	
	vec4 alpha = texture(SCREEN_TEXTURE, SCREEN_UV + displacement);
	
	float fin = 0.0;
	if (sum > 0.0 && sum < 0.4) fin = 0.1;
	if (sum > 0.4 && sum < 0.8) fin = 0.0;
	if (sum > 0.8) fin = 1.0;
	
	ALBEDO = vec3(fin) + mix(alpha.rgb, col.rgb, color.a);
	
	NORMALMAP = tri_map(normalmap, uv1_triplanar_pos).rgb;
	ROUGHNESS = 0.1;
	SPECULAR = 1.0;
}

up till this point I had been testing the shader on a cube, when I added the finished shader to my actual mesh it made a weird effect of having the closest side be pushed inwards to the back of the mesh


And for refrence heres how the mesh should be (not talking about the colours just where the vertexs are)

after a bunch of investing I coulden`t find a cause to this.

For a bit I thought it was on line 72 which had it take the depth texture but that wasent it and tried making the fragment shader not set the albedo or colours but it didnt do it either.

It`d be a great help for any advise on this situation or how to fix it.

Link to project file:
https://drive.google.com/drive/folders/1t9A5aZUKrdPq4e9qP4OdJOdJp_TRh3j9?usp=share_link
(thanks Megalomniak)

Transport answered 9/11, 2022 at 17:41 Comment(0)
T
0

Ive started redoing the shader and noticed the odd behaviour again and it seems that reading from some values or textures cause behavious close to this, as when I made it read the DEPTH value insted of the DEPTH_TEXTURE it started flickering like the snow on tvs.

but it gets even weirder when I uncomment the z_pos declaration (which reads from the FRAGCOORD value) it makes the whole cube black with it occasionally flicker to defualt white or just dessapiering outright.

Keep in mind that we don`t even use the values we get from the DEPTH or FRAGCOORD or even change the COLOR values or any other values.
Final note when I uncomment the alpha declaration (which reads from the SCREEN_TEXTURE) it makes it go all the way back to the original issue with causing it to project into itself.

and when unshaded it isnt all just black but does this?

Transport answered 12/11, 2022 at 11:42 Comment(0)
D
0

There's vertex displacement in the shader. The very first line within vertex(). The shader is clearly meant for planar grid mesh.

Deipnosophist answered 9/11, 2022 at 20:20 Comment(0)
T
0

Deipnosophist that just moves it up and down, the effect I tried to show was how the mesh went into itself somehow

Transport answered 9/11, 2022 at 21:48 Comment(0)
D
0

It does so based on time derived noise. Plus you are applying VERTEX to your triplanar after this displacement noise is already applied to the VERTEX value. Perhaps it will result in what you want/expect if you shuffle contents around within vertex(). I'm not sure tho.

To be clear with noise I'm not referring to the Sampler2D noise noise.

Deipnosophist answered 9/11, 2022 at 22:9 Comment(0)
T
0

Deipnosophist that sounds as it would work. I will try it as soon as I can and give back results

Transport answered 9/11, 2022 at 22:22 Comment(0)
T
0

Deipnosophist sorry for taking a while to get back, but I did what you described I could do and it seemed do end up with the same effect. but something intresting I noticed is that meshes inside the water are affected by it.

it could be that its somehow projecting itself onto itself thus creating the room like effect and it projecting atop the sphere, how its doing this Im unsure.

heres a second picture as in the first one it was a bit hard to see the sphere I was talking about.

Transport answered 10/11, 2022 at 23:11 Comment(0)
D
0

And what happens if you temporarily hide this mesh and apply the shader to a planar grid meshinstance? Also what version of godot?

Deipnosophist answered 11/11, 2022 at 7:38 Comment(0)
M
0

This first line is only applying the wave to the Y axis up position.

VERTEX.y += waves(VERTEX.xy, TIME);

If you are using a mesh (not a flat plane) you probably want to adjust it so the wave moves along the vertex normal.

For a flat plane on the ground, Y up is the assumed normal (what this shader it written for). But on an arbitrary mesh you would need the real vertex normal.

Marcin answered 11/11, 2022 at 10:2 Comment(0)
T
0

Deipnosophist I`m using godot 3.5
and heres how it looks on a plane mesh
plus how it looks underneath

and turns out it does the effect I desire but only on one side of the mesh

on any other side it does the undesired effect

Transport answered 11/11, 2022 at 16:9 Comment(0)
T
0

Marcin that isnt the problem though as it bobbing up and down is part of the effect Im going for, the problem is that it`s projecting itself into itself.

Transport answered 11/11, 2022 at 16:13 Comment(0)
D
0

So yeah, as I already said earlier it looks like this shader was designed for a planar mesh and the waves work along a single axis.

Deipnosophist The shader is clearly meant for planar grid mesh.

Deipnosophist answered 11/11, 2022 at 20:15 Comment(0)
T
0

Deipnosophist thats not the issue though. Im trying to figure out which part of this shader causes it to go into itself. The Vertex function dosen`t seem to be doing this as its a visual effect causing the effect of it going into itself

Transport answered 11/11, 2022 at 23:29 Comment(0)
M
0

The vertex shader is the only thing that changed the geometry, so it must be in there.

Marcin answered 12/11, 2022 at 2:47 Comment(0)
D
0

Transport I was implying but should have just outright said that it's likely easier to write a shader from scratch than try and fix it. And should you look at parts of this one as reference while doing so you might even stumble upon where exactly the issue stems from.

BTW, if you want more help - particularly more useful feedback - your best bet is to share your scene/minimal project where you are experiencing this issue. Without it we are all just guessing here.

Deipnosophist answered 12/11, 2022 at 7:51 Comment(0)
T
0

Deipnosophist When setting up the post I thought I couldent upload the minima project files and just left it with the shader and images. I again tried uploading the project file but as a zip and dosent seem to show up and uploading the tscn file itself dosent work as it says its not allowed to upload these types of files.

Transport answered 12/11, 2022 at 11:18 Comment(0)
T
0

Ive started redoing the shader and noticed the odd behaviour again and it seems that reading from some values or textures cause behavious close to this, as when I made it read the DEPTH value insted of the DEPTH_TEXTURE it started flickering like the snow on tvs.

but it gets even weirder when I uncomment the z_pos declaration (which reads from the FRAGCOORD value) it makes the whole cube black with it occasionally flicker to defualt white or just dessapiering outright.

Keep in mind that we don`t even use the values we get from the DEPTH or FRAGCOORD or even change the COLOR values or any other values.
Final note when I uncomment the alpha declaration (which reads from the SCREEN_TEXTURE) it makes it go all the way back to the original issue with causing it to project into itself.

and when unshaded it isnt all just black but does this?

Transport answered 12/11, 2022 at 11:42 Comment(0)
D
0

Transport When setting up the post I thought I couldent upload the minima project files and just left it with the shader and images.

Aye, it's best to upload to say google drive or dropbox or such. Then just paste a link here for the share URL on a new line and it should auto embed. Worst case it'll be a link to the download.

edit: it's also worth uploading because it might well be related to a bug either in godot or in your specific graphics drivers perhaps. If it's a godot bug then others might be able to reproduce on their HW too, while if it is specific to your graphics drivers then only those with the same gpu and driver version might be able to reproduce it.

Deipnosophist answered 12/11, 2022 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.