Confuse on the doc's Your second 3D Shader (water) tutorial
Asked Answered
M

8

0

I have done Your first 3D Shader but failed at the your second 3D Shader tutorial because I don't know where to plug in those wave codes. (they don't show the final code).

Can anyone follow that tutorial and complete code for me so I can study them.

Thank you very much

Murat answered 1/6, 2022 at 10:42 Comment(0)
S
0

You mean this?

void vertex() {
  vec2 pos = VERTEX.xz;
  float k = height(pos, TIME);
  VERTEX.y = k;
}

Maybe you were just scrolling right past it.

Starstudded answered 1/6, 2022 at 12:43 Comment(0)
M
0

Forgot to mention I'm using GD4 so not sure if it makes a difference (so far It works for the 1st tutorial). The second tutorial, however,:

-I couldn't even get

void fragment() {
  ALBEDO = vec3(0.1, 0.3, 0.5);
}

to make it smoother water surface like tutorial shows.

-adding ROUGHNESS gives me this looks

-I also got error such as "too many argument for height()"

It's just too many errors and it would be nice if I could have the complete correct code so I can go back and compare what I did wrong

Murat answered 2/6, 2022 at 2:58 Comment(0)
S
0

If you are using 4.0 beta you should probably be browsing the latest(unstable) documentation and not stable: https://docs.godotengine.org/en/latest/tutorials/shaders/your_first_shader/your_second_3d_shader.html

Starstudded answered 2/6, 2022 at 3:17 Comment(0)
M
0

I did and it still gives out same errors. This is my code:

shader_type spatial;
render_mode specular_toon

uniform float height_scale = 0.5;
uniform sampler2D noise;
uniform sampler2D normalmap;
varying vec2 tex_position;

void vertex() {
	vec2 pos = VERTEX.xz;
	float k = height(pos, TIME);
	VERTEX.y = k;
	NORMAL = normalize(vec3(k - height(pos + vec2(0.1, 0.0), TIME), 0.1, k - height(pos + vec2(0.0, 0.1), TIME)));
}

void fragment() {
  float fresnel = sqrt(1.0 - dot(NORMAL, VIEW));
  RIM = 0.2;
  METALLIC = 0.0;
  ROUGHNESS = 0.01 * (1.0 - fresnel);
  ALBEDO = vec3(0.01, 0.03, 0.05) + (0.1 * fresnel);
}

float height(vec2 position, float time) {
  vec2 offset = 0.01 * cos(position + time);
  return texture(noise, (position / 10.0) - offset).x;
}
Murat answered 2/6, 2022 at 3:35 Comment(0)
M
0

OK so after moving abunch of codes around I finally got it somewhat working with this:

shader_type spatial;
render_mode specular_toon;

uniform sampler2D noise;
uniform float height_scale = 0.5;
uniform sampler2D normalmap;
varying vec2 tex_position;

float wave(vec2 position){
  position += texture(noise, position / 10.0).x * 2.0 -1.0;
  vec2 wv = 1.0 - abs(sin(position));
  return pow(1.0 - pow(wv.x * wv.y, 0.65), 4.0);
}

float height(vec2 position, float time) {
  float d = wave((position + time) * 0.4) * 0.3;
  d += wave((position - time) * 0.3) * 0.3;
  d += wave((position + time) * 0.5) * 0.2;
  d += wave((position - time) * 0.6) * 0.2;
  return d;
}

void vertex() {
	vec2 pos = VERTEX.xz;
	float k = height(pos, TIME);
	VERTEX.y = k;
	NORMAL = normalize(vec3(k - height(pos + vec2(0.1, 0.0), TIME), 0.1, k - height(pos + vec2(0.0, 0.1), TIME)));
}

void fragment() {
  float fresnel = sqrt(1.0 - dot(NORMAL, VIEW));
  RIM = 0.2;
  METALLIC = 0.0;
  ROUGHNESS = 0.01 * (1.0 - fresnel);
  ALBEDO = vec3(0.01, 0.03, 0.05) + (0.1 * fresnel);
}

So what I learn is that each function have to be at the right place (eg. if you put float height() above float wave() it will fail).

Murat answered 2/6, 2022 at 16:22 Comment(0)
S
0

Yes, order of operations matters, sometimes seemingly significantly more than others, but in truth always. It's more about execution order/code flow.

Starstudded answered 2/6, 2022 at 19:31 Comment(0)
G
0

Murat OK so after moving abunch of codes around I finally got it somewhat working with this:

I got the same code and the result is far from what is presented as expected. The resulting picture is completely different from the one presented in the tutorial as a reference.

Gough answered 7/10, 2022 at 20:6 Comment(0)
M
0

Gough Yes I remember mine being jaggy and looks plastic. I wonder if they increased vertices and improved environment lightings for image display.

Murat answered 9/10, 2022 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.