| 7 | mat4 model; |
| 8 | mat4 projection; |
| 9 | mat4 inverseModel; |
| 10 | vec4 parameters; // time, refraction, brightness, sparkles |
| 11 | vec4 viewport; // width, height, optical plane count, unused |
| 12 | vec4 sparkleShape; // main layer scale, contour morph, core scale, star glow scale |
| 13 | vec4 sparkleHalo; // circular glow scale, main face rotation, horizontal correction, visibility |
| 14 | vec4 crownGradient; |
| 15 | vec4 pavilionGradient; |
| 16 | vec4 lightSweep; // diagonal position, environment phase, transmission, reserved |
| 17 | vec4 facetProjection; // reference pitch cosine/sine, source units, projected top |
| 18 | vec4 crownSweep; |
| 19 | vec4 rightCrownSweep; |
| 20 | vec4 leftCrownSweep; |
| 21 | vec4 pavilionSweep; |
| 22 | vec4 rightPavilionSweep; |
| 23 | vec4 leftPavilionSweep; |
| 24 | vec4 appearance; // palette identifier, reserved |
| 25 | vec4 referenceCrownFlash; |
| 26 | vec4 referencePavilionFlash; |
| 27 | }; |
| 28 | struct Raster { |
| 29 | vec4 position ; |
| 30 | vec3 localPosition; |
| 31 | vec3 normal; |
| 32 | vec3 worldPosition; |
| 33 | vec3 facetWeights; // table, crown, pavilion; interpolated across rounded edges |
| 34 | }; |
| 35 | struct SparkleVertex { vec4 contours; vec4 material; }; |
| 36 | struct SparkleRaster { |
| 37 | vec4 position ; |
| 38 | vec2 sourcePoint; |
| 39 | float strength ; |
| 40 | uint layer ; |
| 41 | }; |
| 42 | struct SparkleAnchor { vec4 position; vec4 normal; }; |
| 43 | uniform Uniforms u; |
| 44 | // Read the optical hull directly from uniforms. Passing this array by value |
| 45 | // makes mobile drivers materialize private copies for each ray query. |
| 46 | uniform vec4 planes[17]; |
| 47 | uniform SparkleAnchor anchors[8]; |
| 48 | in vec3 v_localPosition; |
| 49 | in vec3 v_normal; |
| 50 | in vec3 v_worldPosition; |
| 51 | in vec3 v_facetWeights; |
| 52 | vec3 referenceCrown(vec2 p, vec4 gradient) { |
| 53 | const float stops[7] =float[7](0.0, 0.162, 0.324, 0.501, 0.667, 0.833, 1.0); |
| 54 | const vec3 colors[7] =vec3[7](vec3(0.145,0.792,1.0), vec3(0.373,0.875,1.0), |
| 55 | vec3(0.6,0.957,1.0), vec3(0.3,0.798,1.0), vec3(0.0,0.639,1.0), |
| 56 | vec3(0.0,0.545,1.0), vec3(0.0,0.451,1.0)); |
| 57 | vec2 start = gradient.xy, direction = gradient.zw - start; |
| 58 | float t = saturate(dot(p-start, direction) / dot(direction,direction)); |
| 59 | for (uint i = 1u; i < 7u; ++i) { |
| 60 | if (t <= stops[i]) { return mix(colors[i-1u], colors[i], (t-stops[i-1u])/(stops[i]-stops[i-1u])); } |
| 61 | } |
| 62 | return colors[6]; |
| 63 | } |
| 64 |
| 65 | vec3 lateralDepth(vec3 color, vec3 normal, float y, vec3 facetWeights, |
| 66 | float opticalLuminance) { |
| 67 | float horizontalNormal = length(normal.xz); |
| 68 | float side = smoothstep(0.22, 0.80, abs(normal.x) / max(horizontalNormal, 0.001)); |
| 69 | float shoulder = smoothstep(0.06, 0.57, y); |
| 70 | float lower = smoothstep(0.12, 0.90, -y); |
| 71 | float reflectedLight = smoothstep(0.28, 0.78, color.g); |
| 72 | // A restrained blue-violet shadow keeps the sides distinct without |
| 73 | // swallowing their cyan gradients and moving reflections. |
| 74 | vec3 deepBlue = mix(vec3(0.017, 0.035, 0.90), |
| 75 | vec3(0.006, 0.11, 1.0), reflectedLight); |
| 76 | vec3 sideColor = mix(deepBlue, vec3(0.22, 0.86, 1.0), shoulder * 0.69); |
| 77 | sideColor = mix(sideColor, vec3(0.018, 0.39, 1.0), lower * 0.52); |
| 78 | float reflectionDetail = 0.105 + opticalLuminance * 0.14 |
| 79 | + smoothstep(0.70, 0.95, color.g) * 0.26; |
| 80 | sideColor = mix(sideColor, color, reflectionDetail); |
| 81 | float weight = side * 0.96 * (1.0 - facetWeights.x); |
| 82 | return mix(color, sideColor, weight); |
| 83 | } |
| 84 |
| 85 | vec3 facetBarycentric(vec2 p, vec2 a, vec2 b, vec2 c) { |
| 86 | vec2 v = b-a, w = c-a, q = p-a; |
| 87 | float determinant = v.x*w.y - v.y*w.x; |
| 88 | float y = (q.x*w.y - q.y*w.x) / determinant; |
| 89 | float z = (v.x*q.y - v.y*q.x) / determinant; |
| 90 | return vec3(1.0-y-z, y, z); |
| 91 | } |
| 92 |
| 93 | float facetCoverage(vec3 barycentric) { |
| 94 | float edge = min(barycentric.x, min(barycentric.y, barycentric.z)); |
| 95 | float aa = max(fwidth(edge), 0.004); |
| 96 | return smoothstep(-aa, aa, edge); |
| 97 | } |
| 98 |
| 99 | vec2 sourceFacetPoint(vec3 localPosition, vec2 outward, Uniforms u) { |
| 100 | float across = dot(localPosition.xz, vec2(outward.y,-outward.x)); |
| 101 | float depth = dot(localPosition.xz,outward); |
| 102 | float c = u.facetProjection.x, s = u.facetProjection.y; |
| 103 | float y = c*localPosition.y-s*depth; |
| 104 | float z = s*localPosition.y+c*depth; |
| 105 | float w = u.projection[2].w*z+u.projection[3].w; |
| 106 | return vec2(257.6+across/w*u.facetProjection.z, |
| 107 | 103.8+(u.facetProjection.w-y/w)*u.facetProjection.z); |
| 108 | } |
| 109 |
| 110 | vec3 illustratedFacets(vec3 color, Raster inputData, Uniforms u) { |
| 111 | vec3 localNormal = normalize((u.inverseModel * vec4(normalize(inputData.normal), 0.0)).xyz); |
| 112 | // Fixed material coordinates on each broad face. The same reference camera |
| 113 | // maps the authored facets and the sparkle anchors onto the current cut. |
| 114 | vec2 outward = abs(localNormal.x) > abs(localNormal.z) |
| 115 | ? vec2(sign(localNormal.x),0.0) : vec2(0.0,sign(localNormal.z)); |
| 116 | vec3 tangent = vec3(outward.y,0.0,-outward.x); |
| 117 | vec2 p = sourceFacetPoint(inputData.localPosition,outward,u); |
| 118 | // Fade the drawing before the diagonal facets, where the next face's |
| 119 | // coordinate system takes over. No duplicated motifs along rounded edges. |
| 120 | vec2 normalXZ = abs(localNormal.xz); |
| 121 | float broad = 1.0-smoothstep(0.20,0.65,min(normalXZ.x,normalXZ.y)/max(max(normalXZ.x,normalXZ.y),0.0001)); |
| 122 | vec3 worldTangent = (u.model * vec4(tangent, 0.0)).xyz; |
| 123 | vec3 n = normalize(inputData.normal); |
| 124 | vec3 light = normalize(vec3(0.6*sin(u.lightSweep.y), 0.5, 1.0)); |
| 125 | float leftLight = pow(saturate(dot(normalize(n - worldTangent*0.38), light)), 5.0); |
| 126 | float rightLight = pow(saturate(dot(normalize(n + worldTangent*0.38), light)), 5.0); |
| 127 | if (inputData.facetWeights.y > 0.0) { |
| 128 | // Both crown triangles meet at the same upper junction. |
| 129 | const vec2 crownJunction = vec2(258.8,128.9); |
| 130 | // Authored lower corners now share the same projection as the mesh. |
| 131 | const vec2 crownBaseLeft = vec2(108.8,240.8); |
| 132 | const vec2 crownBaseRight = vec2(406.8,240.8); |
| 133 | vec3 left = facetBarycentric(p, vec2(142.1,106.3), crownJunction, crownBaseLeft); |
| 134 | float leftStrength = facetCoverage(left) * (0.18 + 0.40*leftLight) * saturate(1.0-left.y); |
| 135 | color = mix(color, vec3(0.79,0.988,1.0), leftStrength * inputData.facetWeights.y * broad); |
| 136 | vec3 right = facetBarycentric(p, crownBaseRight, crownJunction, vec2(378.5,108.2)); |
| 137 | // The source is light at the left junction and dark at the right edge. |
| 138 | // Use horizontal facet coordinates so the gradient turns with the gem. |
| 139 | float gradient = smoothstep(crownJunction.x,crownBaseRight.x,p.x); |
| 140 | vec3 tint = mix(vec3(0.28,0.83,1.0),vec3(0.025,0.43,1.0),gradient); |
| 141 | tint = mix(tint,vec3(0.57,0.95,1.0),rightLight*0.24); |
| 142 | color = mix(color, tint, facetCoverage(right) * 0.90 * inputData.facetWeights.y * broad); |
| 143 | } |
| 144 | if (inputData.facetWeights.z > 0.0) { |
| 145 | // A translucent kite belongs to each broad pavilion face. Material-space |
| 146 | // edges stay attached to the surface throughout a complete rotation. |
| 147 | vec2 kite = vec2(dot(inputData.localPosition,tangent),inputData.localPosition.y); |
| 148 | const float halfWidth = 0.245, top = -0.61, shoulder = -0.70, bottom = -1.075; |
| 149 | const float upperHeight = top-shoulder, lowerHeight = shoulder-bottom; |
| 150 | float upperEdge = ((top-kite.y)*halfWidth-abs(kite.x)*upperHeight) |
| 151 | / length(vec2(halfWidth,upperHeight)); |
| 152 | float lowerEdge = ((kite.y-bottom)*halfWidth-abs(kite.x)*lowerHeight) |
| 153 | / length(vec2(halfWidth,lowerHeight)); |
| 154 | float edge = min(upperEdge,lowerEdge); |
| 155 | float aa = max(fwidth(edge),0.0015); |
| 156 | float coverage = smoothstep(-aa,aa,edge); |
| 157 | float illumination = saturate(dot(n,normalize(vec3(-0.65,0.5,1.0)))); |
| 158 | vec3 tint = mix(vec3(0.48,0.96,1.0),vec3(0.70,1.0,1.0),illumination); |
| 159 | float across = saturate(0.5+kite.x/(2.0*halfWidth)); |
| 160 | tint *= mix(vec3(1.0),vec3(0.88,0.97,1.0),across); |
| 161 | color = mix(color,tint,coverage*0.28*inputData.facetWeights.z*broad); |
| 162 | } |
| 163 |
| 164 | return color; |
| 165 | } |
| 166 |
| 167 | vec3 studio(vec3 d, float phase) { |
| 168 | float angle = 0.85 * sin(phase); |
| 169 | d.xz = vec2(cos(angle)*d.x - sin(angle)*d.z, sin(angle)*d.x + cos(angle)*d.z); |
| 170 | float up = smoothstep(-0.65, 0.85, d.y); |
| 171 | vec3 c = mix(vec3(0.002, 0.075, 0.88), vec3(0.19, 0.78, 1.0), up); |
| 172 | float key = pow(saturate(dot(d, normalize(vec3(-0.6, 0.8, 0.7)))), 10.0); |
| 173 | float stripPosition = 0.12 + 0.48 * sin(phase); |
| 174 | float strip = exp(-pow((d.x + d.y * 0.42 - stripPosition) * 9.0, 2.0)); |
| 175 | float side = pow(saturate(dot(d, normalize(vec3(0.8, 0.2, -0.5)))), 18.0); |
| 176 | c = mix(c, vec3(0.68, 0.98, 1.0), key * 0.94); |
| 177 | c = mix(c, vec3(0.80, 0.99, 1.0), strip * 0.64); |
| 178 | c += side * vec3(0.1, 0.3, 0.38); |
| 179 | return c; |
| 180 | } |
| 181 |
| 182 | vec2 facetEdgeRoll(vec2 p, vec2 a, vec2 b) { |
| 183 | vec2 segment = b-a; |
| 184 | float edgeLength = length(segment); |
| 185 | vec2 along = segment/edgeLength; |
| 186 | vec2 across = vec2(-along.y,along.x); |
| 187 | float distance = dot(p-a,across); |
| 188 | float progress = dot(p-a,along); |
| 189 | float width = max(1.6,fwidth(distance)); |
| 190 | float q = distance/width; |
| 191 | float ends = smoothstep(0.0,5.0,progress)*(1.0-smoothstep(edgeLength-5.0,edgeLength,progress)); |
| 192 | return across*(q*exp(-q*q)*0.22*ends); |
| 193 | } |
| 194 |
| 195 | vec3 polishedNormal(Raster inputData, vec3 localNormal, Uniforms u) { |
| 196 | // Only the two surface crown diagonals get this tiny optical fillet. |
| 197 | // The actual outline and the internal optical hull stay unchanged. |
| 198 | if (inputData.facetWeights.y <= 0.0) { return normalize(inputData.normal); } |
| 199 | vec2 outward = abs(localNormal.x) > abs(localNormal.z) |
| 200 | ? vec2(sign(localNormal.x),0.0) : vec2(0.0,sign(localNormal.z)); |
| 201 | vec2 normalXZ = abs(localNormal.xz); |
| 202 | float broad = 1.0-smoothstep(0.20,0.65,min(normalXZ.x,normalXZ.y)/max(max(normalXZ.x,normalXZ.y),0.0001)); |
| 203 | vec2 p = sourceFacetPoint(inputData.localPosition,outward,u); |
| 204 | vec2 roll = facetEdgeRoll(p,vec2(258.8,128.9),vec2(108.8,240.8)) |
| 205 | + facetEdgeRoll(p,vec2(258.8,128.9),vec2(406.8,240.8)); |
| 206 | vec3 across = vec3(outward.y,0.0,-outward.x); |
| 207 | vec3 up = normalize(cross(localNormal,across)); |
| 208 | vec3 normal = normalize(localNormal-(across*roll.x-up*roll.y)*broad*inputData.facetWeights.y); |
| 209 | return normalize((u.model*vec4(normal,0.0)).xyz); |
| 210 | } |
| 211 |
| 212 | float studioPanel(vec3 reflected, vec3 direction, vec2 size) { |
| 213 | float alignment = dot(reflected,direction); |
| 214 | vec3 horizontal = normalize(cross(vec3(0.0,1.0,0.0),direction)); |
| 215 | vec3 vertical = cross(direction,horizontal); |
| 216 | vec2 point = vec2(dot(reflected,horizontal),dot(reflected,vertical))/max(alignment,0.15); |
| 217 | // Filter the narrow reflection at small sizes and grazing angles. |
| 218 | vec2 dx = dFdx(point), dy = dFdy(point); |
| 219 | vec2 variance = size*size+dx*dx+dy*dy; |
| 220 | float energy = size.x*size.y/sqrt(variance.x*variance.y); |
| 221 | return exp(-dot(point*point,1.0/variance))*energy*smoothstep(0.15,0.40,alignment); |
| 222 | } |
| 223 |
| 224 | vec3 surfaceFinish(vec3 color, vec3 normal, vec3 view, Uniforms u) { |
| 225 | vec3 reflected = reflect(-view,normal); |
| 226 | float phase = u.lightSweep.y; |
| 227 | float key = studioPanel(reflected,normalize(vec3(-0.36+0.14*sin(phase),0.90,0.72)),vec2(0.16,0.48)); |
| 228 | float rim = studioPanel(reflected,normalize(vec3(0.72,-0.52+0.10*cos(phase),0.18)),vec2(0.10,0.60)); |
| 229 | float fresnel = pow(1.0-saturate(dot(normal,view)),4.0); |
| 230 | float strength = key*(0.35+0.35*fresnel)+rim*(0.16+0.30*fresnel); |
| 231 | return mix(color,vec3(0.87,0.99,1.0),strength); |
| 232 | } |
| 233 |
| 234 | // The optical hull always has 17 planes. Constant indices avoid the expensive |
| 235 | // dynamically indexed uniform loop on mobile GPUs, without changing ray math. |
| 236 |
| 237 | void exitPlane(vec4 plane, vec3 origin, vec3 ray, inout float nearest, |
| 238 | inout float second, inout vec3 normal, inout vec3 secondNormal) { |
| 239 | float denominator = dot(plane.xyz, ray); |
| 240 | if (denominator > 0.0001) { |
| 241 | float t = -(dot(plane.xyz, origin) + plane.w) / denominator; |
| 242 | if (t > 0.001 && t < nearest) { |
| 243 | second = nearest; secondNormal = normal; |
| 244 | nearest = t; normal = plane.xyz; |
| 245 | } else if (t > 0.001 && t < second) { |
| 246 | second = t; secondNormal = plane.xyz; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 |
| 251 | float nearestExit(vec3 origin, vec3 ray, inout vec3 normal) { |
| 252 | float nearest = 1e5; |
| 253 | float second = 1e5; |
| 254 | vec3 secondNormal = normal; |
| 255 | #define TEST_EXIT(I) exitPlane(planes[I], origin, ray, nearest, second, normal, secondNormal); |
| 256 | TEST_EXIT(0u) TEST_EXIT(1u) TEST_EXIT(2u) TEST_EXIT(3u) TEST_EXIT(4u) TEST_EXIT(5u) |
| 257 | TEST_EXIT(6u) TEST_EXIT(7u) TEST_EXIT(8u) TEST_EXIT(9u) TEST_EXIT(10u) TEST_EXIT(11u) |
| 258 | TEST_EXIT(12u) TEST_EXIT(13u) TEST_EXIT(14u) TEST_EXIT(15u) TEST_EXIT(16u) |
| 259 | #undef TEST_EXIT |
| 260 | if (second < 1e5) { |
| 261 | // The optical hull inherits a tiny edge fillet. This also filters its |
| 262 | // reflected boundaries when they become thinner than a screen pixel. |
| 263 | float width = max(0.008,min(0.035,fwidth(second-nearest))); |
| 264 | float blend = 0.5*(1.0-smoothstep(0.0,width,second-nearest)); |
| 265 | normal = normalize(mix(normal,secondNormal,blend)); |
| 266 | } |
| 267 | return nearest; |
| 268 | } |
| 269 |
| 270 | vec4 oppositeFacets(vec3 p, vec3 ray, Uniforms u) { |
| 271 | vec3 origin = p + ray*0.004; |
| 272 | vec3 normal = vec3(0.0,1.0,0.0); |
| 273 | float distance = nearestExit(origin,ray,normal); |
| 274 | if (distance > 100.0 || normal.y > 0.95) { return vec4(0.0); } |
| 275 | vec3 hit = origin + ray*distance; |
| 276 | vec3 worldNormal = (u.model*vec4(normal,0.0)).xyz; |
| 277 | vec3 tangent = normalize(vec3(normal.z+0.00001,0.0,-normal.x)); |
| 278 | float across = dot(hit,tangent); |
| 279 | vec2 authored = vec2(across*225.8, (normal.y > 0.0 ? 0.3065-hit.y : -0.54-hit.y)*239.0); |
| 280 | vec3 color; |
| 281 | if (normal.y > 0.0) { |
| 282 | color = referenceCrown(authored,u.crownGradient); |
| 283 | float response = pow(saturate(dot(worldNormal, normalize(vec3(0.7*sin(u.lightSweep.y),0.55,-1.0)))),4.0); |
| 284 | color = mix(color*vec3(0.45,0.70,0.98), vec3(0.60,0.94,1.0), response*0.55); |
| 285 | } else { |
| 286 | // The pavilion is one connected eight-facet hull. Looking through its |
| 287 | // front reveals the actual opposite facet, without view-switched motifs. |
| 288 | vec3 reflected = normalize((u.model*vec4(reflect(ray,normal),0.0)).xyz); |
| 289 | vec3 environment = studio(reflected,u.lightSweep.y); |
| 290 | float response = pow(saturate(dot(worldNormal,normalize(vec3(-0.4,-0.55,-1.0)))),2.0); |
| 291 | color = mix(vec3(0.012,0.38,1.0),vec3(0.27,0.94,1.0),response*0.65+environment.g*0.35); |
| 292 | color = mix(color,environment,0.28); |
| 293 | float panel = studioPanel(reflected,normalize(vec3(-0.36,0.90,0.72)),vec2(0.20,0.52)); |
| 294 | color = mix(color,vec3(0.72,0.98,1.0),panel*0.48); |
| 295 | } |
| 296 | float coverage = smoothstep(0.03,0.35,distance) * exp(-distance*0.16); |
| 297 | return vec4(color,coverage); |
| 298 | } |
| 299 |
| 300 | float sourceBand(vec2 p, vec4 gradient) { |
| 301 | vec2 direction = gradient.zw-gradient.xy; |
| 302 | float t = dot(p-gradient.xy,direction)/dot(direction,direction); |
| 303 | return saturate(1.0-abs(t-0.49)/0.49); |
| 304 | } |
| 305 |
| 306 | float facetSweep(Raster inputData, vec3 localNormal, Uniforms u) { |
| 307 | vec3 tangent = normalize(vec3(localNormal.z+0.00001,0.0,-localNormal.x)); |
| 308 | float across = dot(inputData.localPosition,tangent)*225.8; |
| 309 | float side = smoothstep(0.20,0.50,abs(normalize(inputData.normal).x)); |
| 310 | bool right = inputData.normal.x > 0.0; |
| 311 | vec4 crown = mix(u.crownSweep, right ? u.rightCrownSweep : u.leftCrownSweep, side); |
| 312 | vec4 pavilion = mix(u.pavilionSweep, right ? u.rightPavilionSweep : u.leftPavilionSweep, side); |
| 313 | return sourceBand(vec2(across,(0.3065-inputData.localPosition.y)*239.0),crown)*inputData.facetWeights.y |
| 314 | + sourceBand(vec2(across,(-0.54-inputData.localPosition.y)*235.0),pavilion)*inputData.facetWeights.z; |
| 315 | } |
| 316 |
| 317 | vec3 internalEnvironment(vec3 position, vec3 direction, float pavilion, Uniforms u) { |
| 318 | vec3 worldPosition = (u.model*vec4(position,1.0)).xyz; |
| 319 | vec3 worldDirection = normalize((u.model*vec4(direction,0.0)).xyz); |
| 320 | // A finite studio gives each reflected facet a spatial gradient, rather |
| 321 | // than a flat swatch. The lights and the optical hull share one 3D space. |
| 322 | float along = dot(worldPosition,worldDirection); |
| 323 | float distance = -along+sqrt(max(0.0,along*along+3.2*3.2-dot(worldPosition,worldPosition))); |
| 324 | vec3 sampleValue = normalize(worldPosition+worldDirection*distance); |
| 325 | if (pavilion <= 0.0) { return studio(sampleValue,u.lightSweep.y); } |
| 326 | // Keep a uniform blue surround below the crown. A bright upper hemisphere |
| 327 | // reflected inputData the pavilion reads as a filled tip with a horizontal meniscus. |
| 328 | // Narrow studio panels retain moving reflections without filling whole facets. |
| 329 | float phase = u.lightSweep.y; |
| 330 | float key = studioPanel(sampleValue,normalize(vec3(-0.12+0.035*sin(phase),-0.60,0.79)),vec2(0.055,0.52)); |
| 331 | float rim = studioPanel(sampleValue,normalize(vec3(0.12,0.75+0.035*cos(phase),-0.65)),vec2(0.055,0.50)); |
| 332 | vec3 lower = mix(vec3(0.015,0.41,1.0),vec3(0.58,0.94,1.0),key*0.95); |
| 333 | lower = mix(lower,vec3(0.24,0.78,1.0),rim*0.82); |
| 334 | return pavilion < 1.0 ? mix(studio(sampleValue,u.lightSweep.y),lower,pavilion) : lower; |
| 335 | } |
| 336 |
| 337 | float reflectionTriangle(vec3 origin, vec3 ray, vec3 a, vec3 b, vec3 c, |
| 338 | out vec3 coordinates) { |
| 339 | vec3 ab = b-a, ac = c-a, crossRay = cross(ray,ac); |
| 340 | float determinant = dot(ab,crossRay); |
| 341 | if (abs(determinant) < 0.00001) { return 1e5; } |
| 342 | vec3 offset = origin-a; |
| 343 | float u = dot(offset,crossRay)/determinant; |
| 344 | vec3 q = cross(offset,ab); |
| 345 | float v = dot(ray,q)/determinant; |
| 346 | float t = dot(ac,q)/determinant; |
| 347 | coordinates = vec3(1.0-u-v,u,v); |
| 348 | return min(min(coordinates.x,coordinates.y),coordinates.z) >= 0.0 && t > 0.001 ? t : 1e5; |
| 349 | } |
| 350 |
| 351 | vec3 pavilionReflections(vec3 color, vec3 origin, vec3 ray, Uniforms u) { |
| 352 | if (u.parameters.y <= 0.0) { return color; } |
| 353 | const vec2 ring[8] =vec2[8](vec2(0.0,1.0),vec2(0.70710678,0.70710678), |
| 354 | vec2(1.0,0.0),vec2(0.70710678,-0.70710678),vec2(0.0,-1.0), |
| 355 | vec2(-0.70710678,-0.70710678),vec2(-1.0,0.0),vec2(-0.70710678,0.70710678)); |
| 356 | // One convex fan of reflected facets, shared by every camera orientation. |
| 357 | // Clip the whole volume so adjoining facets never acquire dark seams. |
| 358 | const vec3 top = vec3(0.0,-0.28,0.0), tip = vec3(0.0,-1.06,0.0); |
| 359 | float entry = 0.0, exit = 1e5; |
| 360 | vec3 entryNormal = vec3(0.0,1.0,0.0); |
| 361 | vec3 worldRay = normalize((u.model*vec4(ray,0.0)).xyz); |
| 362 | vec3 illumination = normalize(vec3(-0.55,0.65,1.0)); |
| 363 | vec3 sideColor = vec3(0.0); |
| 364 | float sideWeight = 0.0; |
| 365 | vec3 veilColor = vec3(0.0); |
| 366 | float veilWeight = 0.0; |
| 367 | for (uint i = 0u; i < 8u; ++i) { |
| 368 | vec2 current = ring[i], next = ring[(i+1u)%8u]; |
| 369 | vec3 a = vec3(current.x*0.28,-0.68,current.y*0.28); |
| 370 | vec3 b = vec3(next.x*0.28,-0.68,next.y*0.28); |
| 371 | for (uint part = 0u; part < 2u; ++part) { |
| 372 | vec3 peak = part == 0u ? top : tip; |
| 373 | vec3 n = normalize(cross(a-peak,b-peak)); |
| 374 | if (dot(n,(a+b+peak)/3.0-vec3(0.0,-0.68,0.0)) < 0.0) { n = -n; } |
| 375 | float denominator = dot(n,ray); |
| 376 | float side = dot(n,origin-peak); |
| 377 | if (abs(denominator) < 0.00001) { |
| 378 | if (side > 0.0) { exit = -1.0; } |
| 379 | } else { |
| 380 | float t = -side/denominator; |
| 381 | if (denominator < 0.0 && t > entry) { entry = t; entryNormal = n; } |
| 382 | if (denominator > 0.0) { exit = min(exit,t); } |
| 383 | } |
| 384 | } |
| 385 | vec3 radial = vec3(current.x,0.0,current.y); |
| 386 | vec3 tangent = vec3(current.y,0.0,-current.x); |
| 387 | // Broad, low-contrast echoes of the crown stretch through the volume |
| 388 | // towards the same lower junction. Adjacent planes meet without dark gaps. |
| 389 | // Continue the same planes above the girdle so their top fade stays |
| 390 | // hidden behind the crown, including the deeper, opposite reflections. |
| 391 | const float upperY = 0.20; |
| 392 | const float extension = (upperY+0.12)/0.65; |
| 393 | const float upperRadius = 0.64+(0.64-0.19)*extension; |
| 394 | const float upperWidth = 0.265097+(0.265097-0.078701)*extension; |
| 395 | vec3 upperLeft = radial*upperRadius - tangent*upperWidth + vec3(0.0,upperY,0.0); |
| 396 | vec3 upperRight = radial*upperRadius + tangent*upperWidth + vec3(0.0,upperY,0.0); |
| 397 | vec3 lowerLeft = radial*0.19 - tangent*0.078701 + vec3(0.0,-0.77,0.0); |
| 398 | vec3 lowerRight = radial*0.19 + tangent*0.078701 + vec3(0.0,-0.77,0.0); |
| 399 | vec3 ribbonCoordinates; |
| 400 | float ribbon = reflectionTriangle(origin,ray,upperLeft,upperRight,lowerLeft,ribbonCoordinates); |
| 401 | if (ribbon > 100.0) { |
| 402 | ribbon = reflectionTriangle(origin,ray,upperRight,lowerRight,lowerLeft,ribbonCoordinates); |
| 403 | } |
| 404 | if (ribbon < 100.0) { |
| 405 | vec3 hit = origin+ray*ribbon; |
| 406 | float depth = (-0.12-hit.y)/0.65; |
| 407 | float across = dot(hit,tangent); |
| 408 | float right = mix(0.265097,0.078701,depth), left = -right; |
| 409 | vec3 worldRadial = normalize((u.model*vec4(radial,0.0)).xyz); |
| 410 | float facing = saturate(-dot(worldRadial,worldRay)); |
| 411 | float fade = smoothstep(0.0,0.065,upperY-hit.y)*(1.0-smoothstep(0.70,1.0,depth)); |
| 412 | float opacity = fade*(0.16+0.84*facing); |
| 413 | float light = saturate(0.5+0.5*dot(worldRadial,illumination)); |
| 414 | // Broad reflected facets fill the middle with restrained diagonal |
| 415 | // changes inputData tone, expressed inputData the fixed plane's own coordinates. |
| 416 | float acrossPlane = saturate((across-left)/(right-left)); |
| 417 | float diagonal = smoothstep(-0.045,0.045,acrossPlane-0.35-depth*0.55); |
| 418 | float middle = smoothstep(0.12,0.24,depth)*(1.0-smoothstep(0.50,0.64,depth)); |
| 419 | vec3 tint = mix(vec3(0.015,0.56,1.0),vec3(0.33,0.97,1.0),light); |
| 420 | tint = mix(tint,vec3(0.42,0.97,1.0),middle*(0.32-0.20*diagonal)); |
| 421 | tint = mix(tint,vec3(0.015,0.46,1.0),diagonal*0.24); |
| 422 | veilColor += tint*opacity; |
| 423 | veilWeight += opacity; |
| 424 | } |
| 425 | vec3 aSide = radial*0.72 + tangent*0.055 + vec3(0.0,-0.30,0.0); |
| 426 | vec3 bSide = radial*0.49 - tangent*0.105 + vec3(0.0,-0.60,0.0); |
| 427 | vec3 cSide = radial*0.15 + tangent*0.020 + vec3(0.0,-0.93,0.0); |
| 428 | vec3 coordinates; |
| 429 | float t = reflectionTriangle(origin,ray,aSide,bSide,cSide,coordinates); |
| 430 | if (t < 100.0) { |
| 431 | vec3 worldRadial = normalize((u.model*vec4(radial,0.0)).xyz); |
| 432 | // Long glints read at the sides; a front-facing sector must not |
| 433 | // become an opaque needle inputData the middle of the stone. |
| 434 | float sideFacing = 1.0-abs(dot(worldRadial,worldRay)); |
| 435 | float edge = min(coordinates.y,coordinates.z); |
| 436 | float opacity = smoothstep(0.20,0.75,sideFacing) |
| 437 | * smoothstep(0.0,0.085,edge) * smoothstep(0.0,0.035,coordinates.x); |
| 438 | float light = 0.5+0.5*dot(worldRadial,illumination); |
| 439 | vec3 tint = mix(vec3(0.16,0.80,1.0),vec3(0.85,1.0,1.0),light); |
| 440 | sideColor += tint*opacity; |
| 441 | sideWeight += opacity; |
| 442 | } |
| 443 | } |
| 444 | float strength = saturate(u.parameters.y/0.72); |
| 445 | if (veilWeight > 0.0) { |
| 446 | color = mix(color,veilColor/veilWeight,(1.0-exp(-veilWeight*0.46))*strength); |
| 447 | } |
| 448 | if (sideWeight > 0.0) { |
| 449 | color = mix(color,sideColor/sideWeight,(1.0-exp(-sideWeight*1.4))*strength); |
| 450 | } |
| 451 | { |
| 452 | // studioPanel uses screen derivatives. Evaluate it on both sides of the |
| 453 | // hull boundary; divergent execution produces dotted edges on Adreno. |
| 454 | vec3 hit = origin+ray*entry; |
| 455 | vec3 worldNormal = normalize((u.model*vec4(entryNormal,0.0)).xyz); |
| 456 | float light = saturate(dot(worldNormal,normalize(vec3(-0.65,0.15,1.0)))); |
| 457 | float gleam = studioPanel(reflect(worldRay,worldNormal), |
| 458 | normalize(vec3(-0.35+0.15*sin(u.lightSweep.y),0.60,0.75)),vec2(0.25,0.65)); |
| 459 | vec3 tint = mix(vec3(0.025,0.54,1.0),vec3(0.40,0.92,1.0),light); |
| 460 | tint = mix(tint,vec3(0.72,0.99,1.0),gleam*0.16); |
| 461 | float thickness = smoothstep(0.0,0.18,exit-entry); |
| 462 | float fade = smoothstep(-1.07,-0.93,hit.y); |
| 463 | color = mix(color,tint,0.28*thickness*fade*strength*(entry < exit ? 1.0 : 0.0)); |
| 464 | } |
| 465 | return color; |
| 466 | } |
| 467 |
| 468 | vec3 interior(vec3 p, vec3 direction, float pavilion, Uniforms u) { |
| 469 | vec3 accumulated = vec3(0.0); |
| 470 | float weight = 0.60; |
| 471 | vec3 origin = p + direction * 0.004; |
| 472 | for (uint bounce = 0u; bounce < 2u; ++bounce) { |
| 473 | vec3 n = vec3(0.0, 1.0, 0.0); |
| 474 | float distance = nearestExit(origin, direction, n); |
| 475 | if (distance > 100.0) { break; } |
| 476 | vec3 hit = origin + direction * distance; |
| 477 | vec3 outgoing = refract(direction, -n, 1.62); |
| 478 | vec3 reflection = reflect(direction, n); |
| 479 | vec3 color = internalEnvironment(hit,reflection,pavilion,u); |
| 480 | if (dot(outgoing,outgoing) > 0.01) { |
| 481 | // Fresnel approaches total internal reflection continuously; a hard |
| 482 | // switch between the two rays made whole patches change abruptly. |
| 483 | float incident = saturate(dot(direction,n)); |
| 484 | float transmitted = saturate(dot(outgoing,n)); |
| 485 | float rs = (1.62*incident-transmitted)/(1.62*incident+transmitted); |
| 486 | float rp = (incident-1.62*transmitted)/(incident+1.62*transmitted); |
| 487 | float reflectance = 0.5*(rs*rs+rp*rp); |
| 488 | color = mix(internalEnvironment(hit,outgoing,pavilion,u),color,reflectance); |
| 489 | } |
| 490 | color *= exp(-vec3(0.30, 0.07, 0.006) * distance); |
| 491 | accumulated += weight * color; |
| 492 | weight *= 0.52; |
| 493 | direction = reflection; |
| 494 | origin = hit + direction * 0.004; |
| 495 | } |
| 496 | return accumulated; |
| 497 | } |
| 498 |
| 499 | vec3 materialPalette(vec3 color, vec3 facetWeights, uint appearance) { |
| 500 | switch (appearance) { |
| 501 | case 1u: { |
| 502 | // Keep the authored icy blue inputData the middle values: interpolating |
| 503 | // directly to white desaturates the crown into a cold grey. |
| 504 | float tone = smoothstep(0.0,0.92,saturate(dot(color.rg,vec2(0.22,0.78)))); |
| 505 | vec3 shadow = mix(vec3(0.63,0.77,0.95),vec3(0.733,0.859,1.0),facetWeights.y*0.25); |
| 506 | const vec3 ice = vec3(207.0,241.0,253.0) / 255.0; // #cff1fd |
| 507 | vec3 upper = mix(shadow,ice,smoothstep(0.0,0.62,tone)); |
| 508 | upper = mix(upper,vec3(1.0),smoothstep(0.58,1.0,tone)*0.92); |
| 509 | // Lift the pavilion while retaining a distinct shadow range below |
| 510 | // the white crown, with a little more blue inputData its lighter facets. |
| 511 | vec3 lower = mix(vec3(0.59,0.71,0.865),vec3(0.96,0.985,1.0),pow(tone,1.10)); |
| 512 | return mix(upper,lower,facetWeights.z); |
| 513 | } |
| 514 | case 2u: { |
| 515 | float tone = smoothstep(0.035,0.90,saturate(dot(color.rg,vec2(0.20,0.80)))); |
| 516 | vec3 cool = mix(vec3(0.025,0.29,0.83),vec3(0.38,0.78,1.0),smoothstep(0.0,0.65,tone)); |
| 517 | vec3 upper = mix(cool,vec3(0.94,0.99,1.0),smoothstep(0.45,1.0,tone)); |
| 518 | vec3 lower = mix(vec3(0.025,0.34,0.92),vec3(0.90,0.99,1.0),pow(tone,2.35)); |
| 519 | return mix(upper,lower,facetWeights.z); |
| 520 | } |
| 521 | default: return color; |
| 522 | } |
| 523 | } |
| 524 |
| 525 | void glowPlane(vec4 plane, vec3 position, vec3 ray, vec3 localNormal, |
| 526 | inout float planeAlignment, inout float thickness) { |
| 527 | planeAlignment = max(planeAlignment,dot(plane.xyz,localNormal)); |
| 528 | float denominator = dot(plane.xyz,ray); |
| 529 | if (denominator > 0.0001) { |
| 530 | float exit = -(dot(plane.xyz,position)+plane.w) / denominator; |
| 531 | thickness = min(thickness,max(0.0,exit)); |
| 532 | } |
| 533 | } |
| 534 |
| 535 | vec3 coolInnerGlow(vec3 color, vec3 position, vec3 ray, vec3 normal, vec3 localNormal, vec3 view, |
| 536 | Uniforms u) { |
| 537 | // Thin parts transmit a white light band into the stone. The optical chord |
| 538 | // follows the 3D cut under every rotation, without expanding the silhouette. |
| 539 | float thickness = 1e5; |
| 540 | float planeAlignment = 0.0; |
| 541 | #define TEST_GLOW(I) glowPlane(planes[I], position, ray, localNormal, planeAlignment, thickness); |
| 542 | TEST_GLOW(0u) TEST_GLOW(1u) TEST_GLOW(2u) TEST_GLOW(3u) TEST_GLOW(4u) TEST_GLOW(5u) |
| 543 | TEST_GLOW(6u) TEST_GLOW(7u) TEST_GLOW(8u) TEST_GLOW(9u) TEST_GLOW(10u) TEST_GLOW(11u) |
| 544 | TEST_GLOW(12u) TEST_GLOW(13u) TEST_GLOW(14u) TEST_GLOW(15u) TEST_GLOW(16u) |
| 545 | #undef TEST_GLOW |
| 546 | // Scale the penetration with the taper: the tip gets a narrow rim, |
| 547 | // rather than filling with a solid white pool as its whole depth shrinks. |
| 548 | float crossSection = clamp((position.y+1.09)/0.98,0.07,1.0); |
| 549 | float inner = 1.0-smoothstep(0.01,0.80*crossSection,thickness); |
| 550 | float grazing = 1.0-saturate(dot(normal,view)); |
| 551 | float roundedEdge = smoothstep(0.0015,0.028,1.0-planeAlignment); |
| 552 | float bevel = pow(grazing,3.0); |
| 553 | float glow = saturate(inner*0.85 + bevel*0.85 + roundedEdge*smoothstep(0.18,0.80,grazing)); |
| 554 | return mix(color,vec3(0.96,1.0,1.0),glow); |
| 555 | } |
| 556 |
| 557 | vec4 diamondSurface(Raster inputData, Uniforms u) { |
| 558 | vec3 n = normalize(inputData.normal); |
| 559 | float cameraDistance = -u.projection[3].w / u.projection[2].w; |
| 560 | vec3 view = normalize(vec3(0.0, 0.0, cameraDistance) - inputData.worldPosition); |
| 561 | vec3 localView = (u.inverseModel * vec4(-view, 0.0)).xyz; |
| 562 | vec3 localNormal = normalize((u.inverseModel * vec4(n, 0.0)).xyz); |
| 563 | vec3 transmitted = refract(localView, localNormal, 1.0 / 1.62); |
| 564 | // Zero internal bounces on every device; retain the existing material mapping. |
| 565 | vec3 optical = vec3(0.0); |
| 566 | vec3 reflection = studio(reflect(-view, n), u.lightSweep.y); |
| 567 | float fresnel = 0.08 + 0.46 * pow(1.0 - saturate(dot(n, view)), 4.0); |
| 568 |
| 569 | float height = saturate((inputData.localPosition.y + 1.05) / 1.65); |
| 570 | float key = saturate(dot(n, normalize(vec3(-0.65, 0.85, 1.0)))); |
| 571 | float left = saturate(0.52 - inputData.worldPosition.x * 0.43); |
| 572 | vec3 blue = vec3(0.008, 0.22, 1.0); |
| 573 | vec3 cyan = vec3(0.29, 0.87, 1.0); |
| 574 | vec3 body = mix(blue, cyan, saturate(key * 0.60 + left * 0.38)); |
| 575 | float verticalBand = mix(0.5 + 0.5 * sin(height * 18.0 + n.x * 3.0),1.0,inputData.facetWeights.z); |
| 576 | body *= mix(vec3(0.24, 0.48, 0.96), vec3(1.0), verticalBand * 0.5 + 0.5); |
| 577 | float opticalLuminance = smoothstep(0.25, 0.85, optical.g); |
| 578 | // Give the pavilion a cyan body tone without changing reflection contrast |
| 579 | // or the subdued lower motif. Crown and table keep their existing palette. |
| 580 | vec3 opticalShadow = mix(vec3(0.005,0.13,1.0),vec3(0.015,0.36,1.0),inputData.facetWeights.z); |
| 581 | vec3 opticalLight = mix(vec3(0.58,0.97,1.0),vec3(0.45,0.99,1.0),inputData.facetWeights.z); |
| 582 | optical = mix(opticalShadow,opticalLight,opticalLuminance); |
| 583 | float opticalWeight = u.parameters.y * mix(0.90, 0.08, inputData.facetWeights.y); |
| 584 | vec3 color = mix(body, optical, opticalWeight); |
| 585 | color = mix(color, reflection, fresnel); |
| 586 |
| 587 | float crownGlow = smoothstep(0.10, 0.60, inputData.localPosition.y) * left; |
| 588 | color = mix(color, vec3(0.70, 0.99, 1.0), crownGlow * 0.58); |
| 589 | float crownLight = pow(saturate(dot(n, normalize(vec3(-0.38, 0.55, 1.0)))), 9.0); |
| 590 | crownLight *= smoothstep(-0.03, 0.28, inputData.localPosition.y); |
| 591 | color = mix(color, vec3(0.65, 0.98, 1.0), crownLight * 0.35); |
| 592 | vec3 facetBase = color; |
| 593 | if (inputData.facetWeights.y > 0.0) { |
| 594 | float softbox = exp(-pow((inputData.worldPosition.x + 0.42) * 1.6, 2.0) |
| 595 | -pow((inputData.worldPosition.y - 0.32) * 2.2, 2.0)); |
| 596 | color = mix(color, vec3(0.71, 0.99, 1.0), softbox * crownLight * 0.36); |
| 597 | float shadow = pow(saturate(1.0 - key), 1.4); |
| 598 | color = mix(color, vec3(0.015, 0.08, 1.0), shadow * 0.7); |
| 599 | vec3 tangent = normalize(vec3(localNormal.z, 0.0, -localNormal.x)); |
| 600 | vec2 authoredPosition = vec2(dot(inputData.localPosition, tangent) * 225.8 + n.x * 65.0, |
| 601 | (0.3065 - inputData.localPosition.y) * 244.0); |
| 602 | color = mix(referenceCrown(authoredPosition, u.crownGradient), color, 0.28); |
| 603 | color = mix(facetBase, color, inputData.facetWeights.y); |
| 604 | } |
| 605 | if (inputData.facetWeights.z > 0.0) { |
| 606 | color = mix(color,mix(vec3(0.008,0.32,1.0),optical,0.86),inputData.facetWeights.z*u.parameters.y); |
| 607 | } |
| 608 | float facetFlash = smoothstep(0.20, 0.78, opticalLuminance); |
| 609 | float flashWeight = mix(0.24,0.08,inputData.facetWeights.y); |
| 610 | color *= mix(vec3(1.0),mix(vec3(0.76, 0.82, 0.99), vec3(1.0), facetFlash),flashWeight); |
| 611 | color = mix(color, vec3(0.66, 0.98, 1.0), pow(facetFlash, 2.0) * 0.24 * flashWeight); |
| 612 | color = lateralDepth(color, n, inputData.localPosition.y, inputData.facetWeights, opticalLuminance); |
| 613 | color = mix(color,optical,inputData.facetWeights.z*u.parameters.y*0.48); |
| 614 | color = mix(color,body,inputData.facetWeights.z*pow(saturate(n.z),2.0)*0.32); |
| 615 | vec3 rearRay = normalize(mix(localView,transmitted,mix(0.68,0.18,inputData.facetWeights.y))); |
| 616 | // Disable the opposite-face ray independently of the lower-facet reflections. |
| 617 | vec4 rear = vec4(0.0); |
| 618 | float transmission = mix(0.34,0.12+u.lightSweep.z*0.65,inputData.facetWeights.y) * u.parameters.y; |
| 619 | float facing = smoothstep(0.12,0.65,n.z); |
| 620 | color = mix(color,rear.rgb,rear.a*transmission*facing*mix(1.0,0.12,inputData.facetWeights.y)); |
| 621 | if (inputData.facetWeights.z > 0.0) { |
| 622 | vec3 detailRay = normalize(mix(localView,transmitted,0.16)); |
| 623 | color = mix(color,pavilionReflections(color,inputData.localPosition,detailRay,u),inputData.facetWeights.z); |
| 624 | } |
| 625 | float tipGlow = pow(saturate((-inputData.localPosition.y - 0.55) / 0.50), 2.0); |
| 626 | color = mix(color, vec3(0.57, 0.96, 1.0), tipGlow * 0.20); |
| 627 | // A shallow, translucent image of the table, like Layer 36.0 inputData Lottie. |
| 628 | // Its optical footprint is smaller than the rounded outer shoulder. |
| 629 | vec3 tableRay = localView; |
| 630 | tableRay.y *= 0.72; |
| 631 | if (tableRay.y > 0.001) { |
| 632 | float tableHeight = -planes[0].w / planes[0].y; |
| 633 | float distance = (tableHeight-inputData.localPosition.y)/tableRay.y; |
| 634 | vec2 hit = (inputData.localPosition + tableRay*distance).xz; |
| 635 | const float extent = 0.632; |
| 636 | const float corner = 0.285; |
| 637 | vec2 q = abs(hit); |
| 638 | float edge = max(max(q.x,q.y)-extent, (q.x+q.y-extent-corner)*0.70710678); |
| 639 | float aa = max(fwidth(edge),0.001); |
| 640 | float coverage = 1.0-smoothstep(-aa,aa,edge); |
| 641 | vec2 direction = tableRay.xz / max(length(tableRay.xz),0.0001); |
| 642 | float depth = saturate(0.5+dot(hit,direction)/(2.0*extent)); |
| 643 | // Source opacity: 80.0% group opacity times a 49...65% fill gradient. |
| 644 | float opacity = mix(0.39,0.52,depth); |
| 645 | color = mix(color,vec3(0.765,0.988,1.0),coverage*opacity |
| 646 | *saturate(u.parameters.y/0.72)*inputData.facetWeights.y); |
| 647 | } |
| 648 | color = illustratedFacets(color, inputData, u); |
| 649 | color = mix(color, vec3(0.63, 0.96, 1.0), 0.58 * inputData.facetWeights.x); |
| 650 | float sweep = facetSweep(inputData,localNormal,u); |
| 651 | float sweepCore = smoothstep(0.35,1.0,sweep); |
| 652 | color = mix(color,vec3(0.592,0.953,1.0),sweep*0.20+sweepCore*sweepCore*0.62); |
| 653 | color = surfaceFinish(color,polishedNormal(inputData,localNormal,u),view,u); |
| 654 | color = materialPalette(color,inputData.facetWeights,uint(u.appearance.x)); |
| 655 | if (uint(u.appearance.x) == 2u) { |
| 656 | color = coolInnerGlow(color,inputData.localPosition,localView,n,localNormal,view,u); |
| 657 | } |
| 658 | if (uint(u.appearance.x) == 1u) { |
| 659 | // Source facet flashes stay on the physical front/side planes, including |
| 660 | // their rounded transitions. They never become screen-space overlays. |
| 661 | vec2 direction = normalize(localNormal.xz + vec2(0.0,0.00001)); |
| 662 | float side = smoothstep(0.18,0.65,abs(direction.x)); |
| 663 | float front = smoothstep(0.02,0.32,direction.y); |
| 664 | vec3 regions = vec3(1.0-side, side*step(0.0,direction.x), side*(1.0-step(0.0,direction.x))) * front; |
| 665 | float alpha = dot(regions,u.referenceCrownFlash.xyz)*inputData.facetWeights.y |
| 666 | + dot(regions,u.referencePavilionFlash.xyz)*inputData.facetWeights.z; |
| 667 | float referenceFacing = smoothstep(0.0,0.2,(u.model * vec4(0.0,0.0,1.0,0.0)).z); |
| 668 | color = mix(color,vec3(1.0),alpha*referenceFacing); |
| 669 | } |
| 670 | float whiten = u.appearance.y; |
| 671 | if (whiten > 0.0) { |
| 672 | float w = pow(whiten, 0.7); |
| 673 | float luma = dot(color, vec3(0.2126, 0.7152, 0.0722)); |
| 674 | float lift = smoothstep(0.32, 0.62, luma); |
| 675 | vec3 glass = mix(vec3(0.07, 0.33, 0.95), vec3(1.0), lift); |
| 676 | float rim = pow(1.0 - saturate(abs(n.z)), 2.2); |
| 677 | glass = mix(glass, vec3(1.0), rim * 0.8); |
| 678 | float seam = saturate(length(fwidth(n)) * 5.5); |
| 679 | glass = mix(glass, vec3(1.0), seam * 0.85); |
| 680 | glass = max(glass, vec3(smoothstep(0.62, 0.95, luma))); |
| 681 | color = mix(color, glass, w); |
| 682 | float alpha = mix(1.0, mix(0.34, 0.94, max(lift, max(rim, seam))), w); |
| 683 | return vec4(saturate(color * u.parameters.z) * alpha, alpha); |
| 684 | } |
| 685 | return vec4(saturate(color * u.parameters.z), 1.0); |
| 686 | } |
| 687 |
| 688 | out vec4 fragColor; |
| 689 |
| 690 | void main(){ |
| 691 | Raster inputData; |
| 692 | inputData.localPosition=v_localPosition; |
| 693 | inputData.normal=v_normal; |
| 694 | inputData.worldPosition=v_worldPosition; |
| 695 | inputData.facetWeights=v_facetWeights; |
| 696 |
| 697 | fragColor = diamondSurface(inputData, u); |
| 698 |
| 699 | } |
| 700 |