diff --git a/client/puppet.gdshader b/client/puppet.gdshader index 657d776..5802ebe 100644 --- a/client/puppet.gdshader +++ b/client/puppet.gdshader @@ -1,5 +1,6 @@ shader_type spatial; render_mode cull_back; +#include "res://studio.gdshaderinc" // The puppet is one mesh that the CPU skins and shapes every frame (PuppetSkin), so the shader draws what it is given and moves // nothing: VERTEX is already where the body is, and the colour, depth and shadow passes all draw those positions. @@ -20,9 +21,20 @@ render_mode cull_back; // The width is on the screen: bevel_screen is a fraction of the viewport's height and the distance across one pixel (metres per pixel, // from the derivatives of the distance to the edge) turns it into metres; bevel_min floors it, and half the triangle's smallest altitude // caps it so the bands of one triangle never meet. A world width is bevel_screen 0 and bevel_min the width. +// +// The puppet is glazed porcelain. The bisque is the PBR surface (roughness, specular, backlight) lit by Look's studio lights. The glaze +// is analytic: Fresnel times the studio (studio.gdshaderinc) in the direction the shading normal reflects the view, added as emission, so +// it needs no radiance map and draws the same under gl_compatibility, whose clearcoat sees punctual lights only. The bevel's tilted +// normal feeds it, so the chamfers glint. Where two facets meet in a concave crease the glaze pools: the albedo goes darker and cooler, +// and warmer over a convex bevel. Every number comes from Look.Glaze. uniform vec3 albedo : source_color = vec3(0.9, 0.9, 0.88); uniform float bevel_screen = 0.0; uniform float bevel_min = 0.0; +uniform float roughness = 1.0; +uniform float specular = 0.0; +uniform vec3 backlight = vec3(0.0); +uniform float glaze = 0.0; // 0 is the bisque alone +uniform float glaze_pool = 0.0; uniform bool show_normals = false; // RenderCheck's view of the shading normal: the normal in view space as emission, rgb = n * 0.5 + 0.5. varying vec4 edge; @@ -77,8 +89,26 @@ void fragment() { n = normalize(mix(n, normalize(vertex_normal), corner)); } NORMAL = n; - ALBEDO = show_normals ? vec3(0.0) : albedo; + // The creases within 6 mm of an edge: concave tints the albedo cooler and darker, convex warmer. + float convex = 0.0, concave = 0.0; + for (int k = 0; k < 3; k++) { + float near = 1.0 - smoothstep(0.0, 0.006, edge[k]); + convex = max(convex, near * clamp(crease[k] * 2.0, 0.0, 1.0)); + concave = max(concave, near * clamp(-crease[k] * 2.0, 0.0, 1.0)); + } + vec3 a = albedo; + a = mix(a, a * vec3(0.80, 0.82, 0.86), glaze_pool * concave); + a = mix(a, a * vec3(1.02, 0.99, 0.94), glaze_pool * convex); + ALBEDO = show_normals ? vec3(0.0) : a; + // RenderCheck's normal view is the emission alone: no light, however shiny the bisque, may add to it. + ROUGHNESS = show_normals ? 1.0 : roughness; + SPECULAR = show_normals ? 0.0 : specular; + BACKLIGHT = show_normals ? vec3(0.0) : backlight; EMISSION = show_normals ? n * 0.5 + 0.5 : vec3(0.0); - ROUGHNESS = 1.0; - SPECULAR = 0.0; + if (!show_normals && glaze > 0.0) { + vec3 r = reflect(-VIEW, n); + if (!studio_in_view) r = mat3(INV_VIEW_MATRIX) * r; + float f = 0.04 + 0.96 * pow(1.0 - clamp(dot(n, VIEW), 0.0, 1.0), 5.0); + EMISSION += glaze * f * studio(r); + } } diff --git a/client/studio.gdshaderinc b/client/studio.gdshaderinc new file mode 100644 index 0000000..0b6b3d9 --- /dev/null +++ b/client/studio.gdshaderinc @@ -0,0 +1,36 @@ +// The studio the glaze reflects: a dark room, one large soft box (key), two tall strip lights (rims) and a dim floor. Look owns the +// numbers and feeds the same ones to the punctual lights, so the lights and the reflections are one studio. The directions are the +// directions the light comes from, in view space when studio_in_view (the studio turns with the camera, as a photographer's lights do), +// else in world space. Radiances are in the render's linear units and are meant to clip where the glaze catches them. +uniform vec3 studio_room = vec3(0.0); +uniform vec3 studio_key_dir = vec3(0.0, 1.0, 0.0); +uniform float studio_key_size = 0.0; +uniform float studio_key_radiance = 0.0; +uniform vec3 studio_rim_a = vec3(0.0, 1.0, 0.0); +uniform vec3 studio_rim_b = vec3(0.0, 1.0, 0.0); +uniform vec2 studio_rim_size = vec2(0.0); +uniform vec3 studio_rim_tint = vec3(1.0); +uniform float studio_rim_radiance = 0.0; +uniform float studio_floor = 0.0; +uniform bool studio_in_view = true; + +// A rectangular emitter seen from the origin along c, w by h on the plane at unit distance, soft at its edge. +float studio_box(vec3 d, vec3 c, vec2 half_size) { + vec3 up = abs(c.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0); + vec3 u = normalize(cross(up, c)); + vec3 v = cross(c, u); + float f = dot(d, c); + if (f <= 0.0) return 0.0; + vec2 q = vec2(dot(d, u), dot(d, v)) / f; + vec2 e = smoothstep(half_size, half_size * 0.8, abs(q)); + return e.x * e.y; +} + +vec3 studio(vec3 d) { + vec3 c = studio_room; + c += studio_key_radiance * studio_box(d, normalize(studio_key_dir), vec2(studio_key_size, studio_key_size * 0.7)) * vec3(1.0, 0.98, 0.95); + c += studio_rim_radiance * studio_box(d, normalize(studio_rim_a), studio_rim_size) * studio_rim_tint; + c += studio_rim_radiance * studio_box(d, normalize(studio_rim_b), studio_rim_size) * studio_rim_tint; + c += studio_floor * smoothstep(0.0, -0.3, d.y) * vec3(1.0); + return c; +}