Light shaders
Asked Answered
O

12

0

I want to write a custom light shader for water(so that light reflects off of the surface in little specks), but I haven't been able to find anything on light shaders. All I have is this:

DIFFUSE_LIGHT += dot(NORMAL, LIGHT) * ATTENUATION * ALBEDO;

The default light shader would be a good place to start. Does anyone know where to learn about light shaders, or where I can find the default light shader? Thanks.

Obtain answered 6/5, 2020 at 14:17 Comment(0)
C
0

Along with what @Megalomaniak has suggested, you can inspiration directly from Godot's shader code. More specifically here. https://github.com/godotengine/godot/blob/7b56111c297f24304eb911fe75082d8cdc3d4141/drivers/gles3/shaders/scene.glsl#L995

Coefficient answered 6/5, 2020 at 16:46 Comment(0)
O
0

I don't know if I've seen the second page, very useful. I do have some questions: LIGHT is a vector. Is it the direction of the light? ATTENUATION is also a vector, what does that mean? Is NORMAL just the normal, or normal + normal map? How might specular light be calculated?

Obtain answered 6/5, 2020 at 16:49 Comment(0)
O
0

In the shader code @SIsilicon28 linked, I found the part where specular light is calculated(blinn):

	float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
	float blinn = pow(cNdotH, shininess) * cNdotL;
	blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
	float intensity = blinn;

	specular_light += light_color * intensity * specular_blob_intensity * attenuation;
Obtain answered 6/5, 2020 at 17:3 Comment(0)
C
0

Judging from a glance, I believe that's phong based specular.

Coefficient answered 6/5, 2020 at 18:5 Comment(0)
V
0

Yes, Blinn adapted the Phong shading from a purely vertex shader into a pixel shader if I recall my CG history correctly.

Vocalism answered 6/5, 2020 at 18:54 Comment(0)
O
0

I did some research on specular shaders, and I got this:

void light() {
	DIFFUSE_LIGHT += dot(NORMAL, LIGHT) * ATTENUATION * ALBEDO;
	vec3 H = normalize(VIEW + LIGHT);
	float NdotH = max(0, dot(NORMAL, H));
	float specular = pow(NdotH, specular_power) * specular_strength * ATTENUATION.x;
	SPECULAR_LIGHT += specular * specular_color.rgb;
}

I don't know why ATTENUATION is a vec3, so I used ATTENUATION.x.

If I set specular_power to a high value(10,000) and specular color to a high value(5,5,5), then I get closer to the effect I want: I want it to look glittery, kind of the same way water glitters in the real world. Tell me if you have any suggestions.

Obtain answered 21/5, 2020 at 20:15 Comment(0)
O
0

Actually, I think it looks a little better with specular disabled entirely. I think it will look good enough if I make it a little glossier. I found

float fresnel = sqrt(1.0 - dot(NORMAL, VIEW));

can be used for a fresnel effect. Is there a way to adapt this to work with normal maps?

Obtain answered 21/5, 2020 at 20:51 Comment(0)
O
0

Ah, this is helpful: https://origin.80.lv/articles/cartoon-water-shader-in-ue4/

Obtain answered 21/5, 2020 at 21:13 Comment(0)
O
0

Additive bloom gives the correct result:

I like additive bloom better anyway, it just needs to be way softer than softlight. Fire shaders can look really nice with additive bloom, too.

Obtain answered 1/6, 2020 at 13:25 Comment(0)
O
0

I learned two more things about light shaders:

void light() {
    DIFFUSE_LIGHT += max(dot(NORMAL, LIGHT),0.0) * ATTENUATION * ALBEDO * LIGHT_COLOR;
    vec3 H = normalize(VIEW + LIGHT);
    float NdotH = max(0, dot(NORMAL, H));
    float specular = pow(NdotH, specular_power) * specular_strength * ATTENUATION.x;
    SPECULAR_LIGHT += specular*LIGHT_COLOR;
}

First, I multiply both diffuse and specular light by LIGHT_COLOR. Also,

dot(NORMAL, LIGHT)

should be

max(dot(NORMAL, LIGHT),0.0)

otherwise, if a light is behind the surface, it will get darker.

Obtain answered 2/6, 2020 at 12:53 Comment(0)
U
0

Hey @Dschoonmaker ! Thanks for all the updates you made here about what you learned. I was having trouble getting a handle on light shaders and this has helped me greatly.

Underbred answered 5/1, 2022 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.