@aura3d/rendering 1.0.6 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/dist/ForwardPass.js +18 -13
  2. package/dist/ForwardPass.js.map +1 -1
  3. package/dist/FrameVisualMetrics.d.ts +19 -0
  4. package/dist/FrameVisualMetrics.d.ts.map +1 -1
  5. package/dist/FrameVisualMetrics.js +57 -0
  6. package/dist/FrameVisualMetrics.js.map +1 -1
  7. package/dist/MorphTargetPlan.d.ts +42 -0
  8. package/dist/MorphTargetPlan.d.ts.map +1 -0
  9. package/dist/MorphTargetPlan.js +129 -0
  10. package/dist/MorphTargetPlan.js.map +1 -0
  11. package/dist/MorphUnlitMaterial.d.ts.map +1 -1
  12. package/dist/MorphUnlitMaterial.js +7 -1
  13. package/dist/MorphUnlitMaterial.js.map +1 -1
  14. package/dist/ShaderLibrary.d.ts.map +1 -1
  15. package/dist/ShaderLibrary.js +34 -6
  16. package/dist/ShaderLibrary.js.map +1 -1
  17. package/dist/WebGPUDevice.d.ts +2 -0
  18. package/dist/WebGPUDevice.d.ts.map +1 -1
  19. package/dist/WebGPUDevice.js +69 -12
  20. package/dist/WebGPUDevice.js.map +1 -1
  21. package/dist/animation/AnimationMaterialStyle.d.ts +19 -0
  22. package/dist/animation/AnimationMaterialStyle.d.ts.map +1 -0
  23. package/dist/animation/AnimationMaterialStyle.js +13 -0
  24. package/dist/animation/AnimationMaterialStyle.js.map +1 -0
  25. package/dist/animation/AnimationRenderPreset.d.ts +36 -0
  26. package/dist/animation/AnimationRenderPreset.d.ts.map +1 -0
  27. package/dist/animation/AnimationRenderPreset.js +23 -0
  28. package/dist/animation/AnimationRenderPreset.js.map +1 -0
  29. package/dist/animation/AnimationToonMaterial.d.ts +130 -0
  30. package/dist/animation/AnimationToonMaterial.d.ts.map +1 -0
  31. package/dist/animation/AnimationToonMaterial.js +303 -0
  32. package/dist/animation/AnimationToonMaterial.js.map +1 -0
  33. package/dist/animation/AnimationVisualQuality.d.ts +28 -0
  34. package/dist/animation/AnimationVisualQuality.d.ts.map +1 -0
  35. package/dist/animation/AnimationVisualQuality.js +39 -0
  36. package/dist/animation/AnimationVisualQuality.js.map +1 -0
  37. package/dist/animation/applyAnimationRenderPreset.d.ts +79 -0
  38. package/dist/animation/applyAnimationRenderPreset.d.ts.map +1 -0
  39. package/dist/animation/applyAnimationRenderPreset.js +88 -0
  40. package/dist/animation/applyAnimationRenderPreset.js.map +1 -0
  41. package/dist/animation/index.d.ts +6 -0
  42. package/dist/animation/index.d.ts.map +1 -0
  43. package/dist/animation/index.js +6 -0
  44. package/dist/animation/index.js.map +1 -0
  45. package/dist/cartoon/CartoonMaterialStyle.d.ts +19 -0
  46. package/dist/cartoon/CartoonMaterialStyle.d.ts.map +1 -0
  47. package/dist/cartoon/CartoonMaterialStyle.js +13 -0
  48. package/dist/cartoon/CartoonMaterialStyle.js.map +1 -0
  49. package/dist/cartoon/CartoonRenderPreset.d.ts +36 -0
  50. package/dist/cartoon/CartoonRenderPreset.d.ts.map +1 -0
  51. package/dist/cartoon/CartoonRenderPreset.js +23 -0
  52. package/dist/cartoon/CartoonRenderPreset.js.map +1 -0
  53. package/dist/cartoon/CartoonToonMaterial.d.ts +130 -0
  54. package/dist/cartoon/CartoonToonMaterial.d.ts.map +1 -0
  55. package/dist/cartoon/CartoonToonMaterial.js +303 -0
  56. package/dist/cartoon/CartoonToonMaterial.js.map +1 -0
  57. package/dist/cartoon/CartoonVisualQuality.d.ts +28 -0
  58. package/dist/cartoon/CartoonVisualQuality.d.ts.map +1 -0
  59. package/dist/cartoon/CartoonVisualQuality.js +39 -0
  60. package/dist/cartoon/CartoonVisualQuality.js.map +1 -0
  61. package/dist/cartoon/applyCartoonRenderPreset.d.ts +79 -0
  62. package/dist/cartoon/applyCartoonRenderPreset.d.ts.map +1 -0
  63. package/dist/cartoon/applyCartoonRenderPreset.js +88 -0
  64. package/dist/cartoon/applyCartoonRenderPreset.js.map +1 -0
  65. package/dist/cartoon/index.d.ts +6 -0
  66. package/dist/cartoon/index.d.ts.map +1 -0
  67. package/dist/cartoon/index.js +6 -0
  68. package/dist/cartoon/index.js.map +1 -0
  69. package/dist/index.d.ts +6 -3
  70. package/dist/index.d.ts.map +1 -1
  71. package/dist/index.js +4 -2
  72. package/dist/index.js.map +1 -1
  73. package/package.json +4 -4
  74. package/src/ForwardPass.ts +18 -13
  75. package/src/FrameVisualMetrics.ts +83 -0
  76. package/src/MorphTargetPlan.ts +178 -0
  77. package/src/MorphUnlitMaterial.ts +7 -1
  78. package/src/ShaderLibrary.ts +34 -6
  79. package/src/WebGPUDevice.ts +83 -12
  80. package/src/animation/AnimationMaterialStyle.ts +32 -0
  81. package/src/animation/AnimationRenderPreset.ts +45 -0
  82. package/src/animation/AnimationToonMaterial.ts +387 -0
  83. package/src/animation/AnimationVisualQuality.ts +71 -0
  84. package/src/animation/applyAnimationRenderPreset.ts +180 -0
  85. package/src/animation/index.ts +5 -0
  86. package/src/index.ts +13 -2
@@ -360,6 +360,9 @@ class WebGPURenderTarget implements RenderTarget {
360
360
  }
361
361
  }
362
362
 
363
+ /** Joint-palette capacity of the WebGPU skinning path — parity with the WebGL2 `u_jointMatrices[96]`. */
364
+ export const MAX_WEBGPU_SKINNING_JOINTS = 96;
365
+
363
366
  export class WebGPUDevice implements RenderDevice {
364
367
  public readonly kind = "webgpu";
365
368
  public readonly info: RenderDeviceInfo;
@@ -1162,6 +1165,17 @@ export class WebGPUDevice implements RenderDevice {
1162
1165
  const modelMatrices = uniformRasterModelMatrices(command.uniforms, command.instanceCount ?? 1);
1163
1166
  const depthTest = command.renderState?.depthTest !== false && this.activeRenderTarget.depthPixels !== null;
1164
1167
  const depthWrite = command.renderState?.depthWrite !== false;
1168
+ // Full joint-palette skinning (up to 96 joints, WebGL2 parity). readLocal applies morph then skin.
1169
+ const jointPalette = jointPaletteFor(command.uniforms);
1170
+ const format = command.vertexFormat;
1171
+ const readLocal = (index: number): readonly [number, number, number] =>
1172
+ skinLocalPosition(
1173
+ readMorphedPosition(vertexBuffer.bytes, format.stride, position.offset, index, command.uniforms),
1174
+ vertexBuffer.bytes,
1175
+ format,
1176
+ index,
1177
+ jointPalette
1178
+ );
1165
1179
  for (let instanceIndex = 0; instanceIndex < instanceMatrices.length; instanceIndex += 1) {
1166
1180
  const matrix = instanceMatrices[instanceIndex] ?? identityMatrix();
1167
1181
  const modelMatrix = modelMatrices[instanceIndex] ?? identityMatrix();
@@ -1170,9 +1184,9 @@ export class WebGPUDevice implements RenderDevice {
1170
1184
  const indexA = indices[offset]!;
1171
1185
  const indexB = indices[offset + 1]!;
1172
1186
  const indexC = indices[offset + 2]!;
1173
- const localA = readMorphedPosition(vertexBuffer.bytes, command.vertexFormat.stride, position.offset, indexA, command.uniforms);
1174
- const localB = readMorphedPosition(vertexBuffer.bytes, command.vertexFormat.stride, position.offset, indexB, command.uniforms);
1175
- const localC = readMorphedPosition(vertexBuffer.bytes, command.vertexFormat.stride, position.offset, indexC, command.uniforms);
1187
+ const localA = readLocal(indexA);
1188
+ const localB = readLocal(indexB);
1189
+ const localC = readLocal(indexC);
1176
1190
  const a = transformPosition(localA, matrix);
1177
1191
  const b = transformPosition(localB, matrix);
1178
1192
  const c = transformPosition(localC, matrix);
@@ -1191,15 +1205,15 @@ export class WebGPUDevice implements RenderDevice {
1191
1205
  for (let offset = 0; offset + 1 < indices.length; offset += 2) {
1192
1206
  const indexA = indices[offset]!;
1193
1207
  const indexB = indices[offset + 1]!;
1194
- const a = transformPosition(readMorphedPosition(vertexBuffer.bytes, command.vertexFormat.stride, position.offset, indexA, command.uniforms), matrix);
1195
- const b = transformPosition(readMorphedPosition(vertexBuffer.bytes, command.vertexFormat.stride, position.offset, indexB, command.uniforms), matrix);
1208
+ const a = transformPosition(readLocal(indexA), matrix);
1209
+ const b = transformPosition(readLocal(indexB), matrix);
1196
1210
  const colorA = readVertexColor(vertexBuffer.bytes, command.vertexFormat.stride, colorAttribute?.offset, indexA);
1197
1211
  const colorB = readVertexColor(vertexBuffer.bytes, command.vertexFormat.stride, colorAttribute?.offset, indexB);
1198
1212
  rasterizeLine(this.activeRenderTarget, a, b, color, colorA, colorB, depthTest, depthWrite);
1199
1213
  }
1200
1214
  } else {
1201
1215
  for (const index of indices) {
1202
- const point = transformPosition(readMorphedPosition(vertexBuffer.bytes, command.vertexFormat.stride, position.offset, index, command.uniforms), matrix);
1216
+ const point = transformPosition(readLocal(index), matrix);
1203
1217
  const vertexColor = readVertexColor(vertexBuffer.bytes, command.vertexFormat.stride, colorAttribute?.offset, index);
1204
1218
  rasterizePoint(this.activeRenderTarget, point, multiplyColor(color, vertexColor), depthTest, depthWrite);
1205
1219
  }
@@ -1531,7 +1545,8 @@ export class WebGPUDevice implements RenderDevice {
1531
1545
 
1532
1546
  private createNativeDrawUniformBuffer(command: DrawCommand): WebGPUBufferLike | null {
1533
1547
  if (!this.device.createBindGroup) return null;
1534
- const data = new Float32Array(188);
1548
+ // 188 floats for the scalar/matrix fields + 96 joint matrices (16 floats each) appended at 188.
1549
+ const data = new Float32Array(188 + MAX_WEBGPU_SKINNING_JOINTS * 16);
1535
1550
  const baseColorBinding = uniformBaseColorTextureBinding(command.uniforms);
1536
1551
  const environmentBinding = uniformTextureBinding(command.uniforms, "u_environmentMapTexture");
1537
1552
  const normalBinding = uniformTextureBinding(command.uniforms, "u_normalTexture");
@@ -1573,9 +1588,13 @@ export class WebGPUDevice implements RenderDevice {
1573
1588
  for (let index = 0; index < 4; index += 1) {
1574
1589
  data.set(instanceMatrices[index] ?? (index === 0 ? modelMatrix : identityMatrix()), 32 + index * 16);
1575
1590
  }
1576
- const jointMatrices = uniformMat4Array(command.uniforms?.get("u_jointMatrices"), 2);
1577
- for (let index = 0; index < 2; index += 1) {
1578
- data.set(jointMatrices[index] ?? identityMatrix(), 96 + index * 16);
1591
+ // Legacy 2-slot joint fields (offsets 96/112) kept as identity for layout stability.
1592
+ data.set(identityMatrix(), 96);
1593
+ data.set(identityMatrix(), 112);
1594
+ // Full 96-joint palette (WebGL2 parity), appended after normalMatrix at offset 188.
1595
+ const jointMatrices = uniformMat4Array(command.uniforms?.get("u_jointMatrices"), MAX_WEBGPU_SKINNING_JOINTS);
1596
+ for (let index = 0; index < MAX_WEBGPU_SKINNING_JOINTS; index += 1) {
1597
+ data.set(jointMatrices[index] ?? identityMatrix(), 188 + index * 16);
1579
1598
  }
1580
1599
  const morphDeltas = command.uniforms?.get("u_morphPositionDeltas");
1581
1600
  const morphNumbers = morphDeltas instanceof Float32Array || Array.isArray(morphDeltas) ? Array.from(morphDeltas).slice(0, 32) : [];
@@ -2097,6 +2116,54 @@ function identityMatrix(): readonly number[] {
2097
2116
  return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
2098
2117
  }
2099
2118
 
2119
+ function readVec4Attribute(bytes: Uint8Array, stride: number, offset: number, vertexIndex: number): readonly [number, number, number, number] {
2120
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2121
+ const base = vertexIndex * stride + offset;
2122
+ return [
2123
+ view.getFloat32(base, true),
2124
+ view.getFloat32(base + 4, true),
2125
+ view.getFloat32(base + 8, true),
2126
+ view.getFloat32(base + 12, true)
2127
+ ];
2128
+ }
2129
+
2130
+ // CPU skinning for the WebGPU emulation rasterizer: blend the joint palette (up to the full
2131
+ // WebGL2-parity 96-joint count) by the vertex's 4 joint indices + weights, then transform the local
2132
+ // position by the blended matrix. Mirrors the WebGL2 skinning path so a skinned character deforms
2133
+ // identically on both backends. `palette` may hold up to 96 matrices (parity with WebGL2).
2134
+ function skinLocalPosition(
2135
+ local: readonly [number, number, number],
2136
+ bytes: Uint8Array,
2137
+ format: VertexFormat,
2138
+ vertexIndex: number,
2139
+ palette: readonly (readonly number[])[]
2140
+ ): readonly [number, number, number] {
2141
+ if (palette.length === 0 || !format.hasAttribute("joints") || !format.hasAttribute("weights")) {
2142
+ return local;
2143
+ }
2144
+ const joints = readVec4Attribute(bytes, format.stride, format.getAttribute("joints").offset, vertexIndex);
2145
+ const weights = readVec4Attribute(bytes, format.stride, format.getAttribute("weights").offset, vertexIndex);
2146
+ const weightSum = weights[0] + weights[1] + weights[2] + weights[3];
2147
+ if (weightSum <= 1e-4) return local;
2148
+ const blended = new Array<number>(16).fill(0);
2149
+ const maxJoint = palette.length - 1;
2150
+ for (let influence = 0; influence < 4; influence += 1) {
2151
+ const weight = weights[influence] ?? 0;
2152
+ if (weight === 0) continue;
2153
+ const jointIndex = Math.max(0, Math.min(maxJoint, Math.round(joints[influence] ?? 0)));
2154
+ const jointMatrix = palette[jointIndex] ?? identityMatrix();
2155
+ for (let k = 0; k < 16; k += 1) blended[k] += (jointMatrix[k] ?? 0) * weight;
2156
+ }
2157
+ return transformPosition(local, blended);
2158
+ }
2159
+
2160
+ function jointPaletteFor(uniforms: DrawCommand["uniforms"]): readonly (readonly number[])[] {
2161
+ const value = uniforms?.get("u_jointMatrices");
2162
+ const length = value instanceof Float32Array || Array.isArray(value) ? (value as ArrayLike<number>).length : 0;
2163
+ const count = Math.min(MAX_WEBGPU_SKINNING_JOINTS, Math.floor(length / 16));
2164
+ return count > 0 ? uniformMat4Array(value, count) : [];
2165
+ }
2166
+
2100
2167
  function readVertexColor(bytes: Uint8Array, stride: number, offset: number | undefined, vertexIndex: number): readonly [number, number, number, number] {
2101
2168
  if (offset === undefined) return [1, 1, 1, 1];
2102
2169
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
@@ -2385,6 +2452,7 @@ function nativeUniformStruct(): string {
2385
2452
  joint0: mat4x4<f32>,
2386
2453
  joint1: mat4x4<f32>,
2387
2454
  morph0: vec4<f32>,
2455
+ // (joint palette appended at struct end below as \`joints\` for 96-joint WebGL2 parity)
2388
2456
  morph1: vec4<f32>,
2389
2457
  morph2: vec4<f32>,
2390
2458
  morph3: vec4<f32>,
@@ -2396,6 +2464,9 @@ function nativeUniformStruct(): string {
2396
2464
  camera: vec4<f32>,
2397
2465
  materialFlags: vec4<f32>,
2398
2466
  normalMatrix: mat4x4<f32>,
2467
+ // 96-joint skinning palette (WebGL2 \`u_jointMatrices[96]\` parity). Appended at the struct end so
2468
+ // the existing field offsets are unchanged.
2469
+ joints: array<mat4x4<f32>, 96>,
2399
2470
  };
2400
2471
 
2401
2472
  @group(0) @binding(0) var<uniform> u_draw: DrawUniforms;
@@ -2717,8 +2788,8 @@ struct VertexOutput {
2717
2788
  };
2718
2789
 
2719
2790
  fn jointMatrix(index: f32) -> mat4x4<f32> {
2720
- if (index > 0.5) { return u_draw.joint1; }
2721
- return u_draw.joint0;
2791
+ let i = clamp(i32(index + 0.5), 0, 95);
2792
+ return u_draw.joints[i];
2722
2793
  }
2723
2794
 
2724
2795
  @vertex
@@ -0,0 +1,32 @@
1
+ export type AnimationMaterialTreatment = "preserve-pbr" | "soft-toon" | "cel" | "flat-readable";
2
+
3
+ export interface AnimationMaterialStyleOptions {
4
+ readonly treatment?: AnimationMaterialTreatment | undefined;
5
+ readonly outline?: boolean | undefined;
6
+ readonly rampSteps?: number | undefined;
7
+ readonly saturationBoost?: number | undefined;
8
+ readonly roughnessFloor?: number | undefined;
9
+ }
10
+
11
+ export interface AnimationMaterialStyle {
12
+ readonly kind: "animation-material-style";
13
+ readonly treatment: AnimationMaterialTreatment;
14
+ readonly outline: boolean;
15
+ readonly rampSteps: number;
16
+ readonly saturationBoost: number;
17
+ readonly roughnessFloor: number;
18
+ readonly assetOverrideMetadata: readonly string[];
19
+ }
20
+
21
+ export function createAnimationMaterialStyle(options: AnimationMaterialStyleOptions = {}): AnimationMaterialStyle {
22
+ const treatment = options.treatment ?? "soft-toon";
23
+ return {
24
+ kind: "animation-material-style",
25
+ treatment,
26
+ outline: options.outline ?? treatment === "cel",
27
+ rampSteps: options.rampSteps ?? (treatment === "cel" ? 4 : 7),
28
+ saturationBoost: options.saturationBoost ?? 0.08,
29
+ roughnessFloor: options.roughnessFloor ?? 0.48,
30
+ assetOverrideMetadata: ["animationMaterialTreatment", "toonRampSteps", "outlineEligible", "preserveSkinning"]
31
+ };
32
+ }
@@ -0,0 +1,45 @@
1
+ import { createAnimationMaterialStyle, type AnimationMaterialStyle, type AnimationMaterialStyleOptions } from "./AnimationMaterialStyle.js";
2
+
3
+ export interface AnimationRenderPresetOptions {
4
+ readonly name?: string | undefined;
5
+ readonly resolution?: { readonly width: number; readonly height: number } | undefined;
6
+ readonly materialStyle?: AnimationMaterialStyleOptions | undefined;
7
+ readonly reducedMotion?: boolean | undefined;
8
+ readonly reducedFlash?: boolean | undefined;
9
+ }
10
+
11
+ export interface AnimationRenderPresetEvidence {
12
+ readonly kind: "animation-render-preset-evidence";
13
+ readonly name: string;
14
+ readonly resolution: { readonly width: number; readonly height: number };
15
+ readonly lights: readonly string[];
16
+ readonly shadows: { readonly soft: boolean; readonly contact: boolean };
17
+ readonly postprocess: { readonly bloom: number; readonly colorGrade: string; readonly fogDepthCue: boolean };
18
+ readonly materialStyle: AnimationMaterialStyle;
19
+ readonly frameBudgetMs: number;
20
+ readonly debugOverlaysAllowedInExport: false;
21
+ readonly reducedMotion: boolean;
22
+ readonly reducedFlash: boolean;
23
+ }
24
+
25
+ export function createAnimationRenderPreset(options: AnimationRenderPresetOptions = {}): AnimationRenderPresetEvidence {
26
+ const reducedMotion = options.reducedMotion ?? false;
27
+ const reducedFlash = options.reducedFlash ?? false;
28
+ return {
29
+ kind: "animation-render-preset-evidence",
30
+ name: options.name ?? "moon-garden-animation",
31
+ resolution: options.resolution ?? { width: 1280, height: 720 },
32
+ lights: ["soft-key", "cool-rim", "set-fill", "emissive-practicals"],
33
+ shadows: { soft: true, contact: true },
34
+ postprocess: {
35
+ bloom: reducedFlash ? 0.08 : 0.18,
36
+ colorGrade: "storybook-night",
37
+ fogDepthCue: true
38
+ },
39
+ materialStyle: createAnimationMaterialStyle(options.materialStyle),
40
+ frameBudgetMs: 16.7,
41
+ debugOverlaysAllowedInExport: false,
42
+ reducedMotion,
43
+ reducedFlash
44
+ };
45
+ }
@@ -0,0 +1,387 @@
1
+ import { Material, type RenderState } from "../Material.js";
2
+
3
+ /**
4
+ * AnimationToonMaterial — a REAL toon / cel material for the Aura3D rendering engine.
5
+ *
6
+ * Unlike `AnimationMaterialStyle` (which is descriptive metadata consumed by nothing),
7
+ * this module defines an actual material that drives a dedicated GLSL program:
8
+ *
9
+ * - It quantizes the diffuse lighting term (N·L) into a small number of discrete
10
+ * bands ("ramp steps") instead of smoothly shading — the defining trait of cel
11
+ * shading.
12
+ * - It adds a Fresnel-style rim light that brightens silhouette edges.
13
+ * - It is wired into the engine the same way `PBRMaterial` / `UnlitMaterial` are:
14
+ * it extends `Material`, references a `shaderKey`, declares a `uniformSchema`, and
15
+ * seeds `parameters`. The matching shader source is registered into the default
16
+ * `ShaderLibrary` via `registerAnimationToonShader` (called from
17
+ * `createDefaultShaderLibrary`), so any renderer that resolves materials through
18
+ * the shader library can compile and bind this program.
19
+ *
20
+ * WHAT IS WIRED TO PIXELS:
21
+ * - The TS ramp/quantize math (`quantizeToonBand`, `toonDiffuseRamp`, `toonRimTerm`,
22
+ * `toonShadeColor`) is pure, tested, and is the exact same math the GLSL fragment
23
+ * shader performs (mirrored line-for-line). Any renderer that runs the
24
+ * `aura3d/animation-toon` program gets banded lighting + rim light on real fragments.
25
+ * - The material's uniforms (band count, rim color/power, light direction, colors)
26
+ * flow through the standard `Material` parameter system, so they reach the GPU
27
+ * through whatever uniform-binding path the active `RenderDevice` uses for other
28
+ * materials.
29
+ *
30
+ * WHAT IS NOT (yet) WIRED:
31
+ * - The animation shader is a single-directional-light forward program. It does NOT
32
+ * consume the full `u_lightData[]` array / shadow maps / IBL that the PBR shader
33
+ * does. It is intentionally a focused NPR program, not a PBR superset.
34
+ * - Outlines are NOT drawn by this material. Outlines come from the existing Sobel
35
+ * `outlinePixels()` post-pass, which `applyAnimationRenderPreset` enables separately.
36
+ */
37
+
38
+ export const ANIMATION_TOON_SHADER_NAME = "aura3d/animation-toon";
39
+ export const ANIMATION_TOON_SHADER_MARKER = "@aura3d-shader:animation-toon";
40
+
41
+ export const ANIMATION_TOON_MIN_BANDS = 2;
42
+ export const ANIMATION_TOON_MAX_BANDS = 16;
43
+
44
+ export interface AnimationToonMaterialOptions {
45
+ readonly name?: string;
46
+ /** Base albedo (RGBA), each channel in [0, 1]. */
47
+ readonly baseColor?: readonly [number, number, number, number];
48
+ /** Number of discrete lighting bands. Integer in [2, 16]. */
49
+ readonly bands?: number;
50
+ /**
51
+ * Ambient floor in [0, 1] — the darkest band is never fully black, so cel art
52
+ * keeps readable shapes in shadow. Multiplies base color in the lowest band.
53
+ */
54
+ readonly shadowFloor?: number;
55
+ /** Direction the key light travels FROM the surface toward the light (world space). */
56
+ readonly lightDirection?: readonly [number, number, number];
57
+ /** Key light color (RGB), each channel in [0, 1]. */
58
+ readonly lightColor?: readonly [number, number, number];
59
+ /** Rim light color (RGB), each channel in [0, 1]. */
60
+ readonly rimColor?: readonly [number, number, number];
61
+ /** Rim falloff exponent (higher = tighter rim). Finite and >= 0. */
62
+ readonly rimPower?: number;
63
+ /** Rim contribution strength in [0, 1]. */
64
+ readonly rimIntensity?: number;
65
+ /** Alpha discard threshold in [0, 1]. */
66
+ readonly alphaCutoff?: number;
67
+ readonly renderState?: Partial<RenderState>;
68
+ }
69
+
70
+ /* ------------------------------------------------------------------------- *
71
+ * Pure ramp / quantize math.
72
+ *
73
+ * These functions are the single source of truth for the toon lighting model.
74
+ * The GLSL fragment shader below performs the identical computation, so testing
75
+ * these functions validates the actual pixel math (modulo float precision).
76
+ * ------------------------------------------------------------------------- */
77
+
78
+ function clamp01(value: number): number {
79
+ return value < 0 ? 0 : value > 1 ? 1 : value;
80
+ }
81
+
82
+ /**
83
+ * Quantize a continuous lighting term `t` in [0, 1] into one of `bands` discrete
84
+ * levels, returning the band's representative value in [0, 1].
85
+ *
86
+ * With `bands = 4` the output can only be 0, 1/3, 2/3, or 1 — hard stepped shading.
87
+ * This mirrors GLSL `floor(t * bands) / (bands - 1)` clamped so the brightest band
88
+ * reaches full strength.
89
+ */
90
+ export function quantizeToonBand(t: number, bands: number): number {
91
+ const steps = Math.max(ANIMATION_TOON_MIN_BANDS, Math.min(ANIMATION_TOON_MAX_BANDS, Math.floor(bands)));
92
+ const clamped = clamp01(t);
93
+ // floor into [0, steps-1], then normalize so the top band maps to exactly 1.
94
+ const level = Math.min(steps - 1, Math.floor(clamped * steps));
95
+ return level / (steps - 1);
96
+ }
97
+
98
+ /**
99
+ * Toon diffuse ramp: take raw N·L (already clamped to [0, 1]), quantize it into
100
+ * bands, then lift the darkest band off zero by `shadowFloor` so shadows stay
101
+ * readable. Returns a multiplier in [shadowFloor, 1].
102
+ */
103
+ export function toonDiffuseRamp(ndotl: number, bands: number, shadowFloor: number): number {
104
+ const floor = clamp01(shadowFloor);
105
+ const banded = quantizeToonBand(clamp01(ndotl), bands);
106
+ return floor + (1 - floor) * banded;
107
+ }
108
+
109
+ /**
110
+ * Fresnel-style rim term: bright at grazing angles (N·V small), dark facing the
111
+ * camera. `ndotv` is clamped N·V. Returns rim strength in [0, rimIntensity].
112
+ */
113
+ export function toonRimTerm(ndotv: number, rimPower: number, rimIntensity: number): number {
114
+ const power = Number.isFinite(rimPower) && rimPower >= 0 ? rimPower : 2;
115
+ const intensity = clamp01(rimIntensity);
116
+ const fresnel = Math.pow(1 - clamp01(ndotv), power);
117
+ return clamp01(fresnel) * intensity;
118
+ }
119
+
120
+ export interface ToonShadeInputs {
121
+ readonly baseColor: readonly [number, number, number];
122
+ readonly ndotl: number;
123
+ readonly ndotv: number;
124
+ readonly bands: number;
125
+ readonly shadowFloor: number;
126
+ readonly lightColor: readonly [number, number, number];
127
+ readonly rimColor: readonly [number, number, number];
128
+ readonly rimPower: number;
129
+ readonly rimIntensity: number;
130
+ }
131
+
132
+ /**
133
+ * Full toon shade for one fragment: banded diffuse * base * light, plus additive
134
+ * rim. Returns linear RGB (unclamped channels are clamped to [0, 1] for output
135
+ * parity with the shader's final clamp). This is exactly what the GLSL `main()`
136
+ * computes per pixel.
137
+ */
138
+ export function toonShadeColor(inputs: ToonShadeInputs): [number, number, number] {
139
+ const ramp = toonDiffuseRamp(inputs.ndotl, inputs.bands, inputs.shadowFloor);
140
+ const rim = toonRimTerm(inputs.ndotv, inputs.rimPower, inputs.rimIntensity);
141
+ const out: [number, number, number] = [0, 0, 0];
142
+ for (let i = 0; i < 3; i += 1) {
143
+ const lit = inputs.baseColor[i]! * ramp * inputs.lightColor[i]!;
144
+ const rimContribution = inputs.rimColor[i]! * rim;
145
+ out[i] = clamp01(lit + rimContribution);
146
+ }
147
+ return out;
148
+ }
149
+
150
+ /* ------------------------------------------------------------------------- *
151
+ * GLSL program. The fragment shader mirrors the TS math above.
152
+ * Registered into the default ShaderLibrary by `registerAnimationToonShader`.
153
+ * ------------------------------------------------------------------------- */
154
+
155
+ export const ANIMATION_TOON_VERTEX_SOURCE = `#version 300 es
156
+ // ${ANIMATION_TOON_SHADER_MARKER}
157
+ precision highp float;
158
+ layout(location = 0) in vec3 a_position;
159
+ layout(location = 1) in vec3 a_normal;
160
+ layout(location = 4) in vec4 a_color;
161
+ uniform mat4 u_modelViewProjection;
162
+ uniform mat4 u_modelMatrix;
163
+ uniform mat4 u_normalMatrix;
164
+ out vec3 v_normal;
165
+ out vec3 v_worldPosition;
166
+ out vec4 v_vertexColor;
167
+ void main() {
168
+ v_normal = mat3(u_normalMatrix) * a_normal;
169
+ v_worldPosition = (u_modelMatrix * vec4(a_position, 1.0)).xyz;
170
+ v_vertexColor = a_color;
171
+ gl_Position = u_modelViewProjection * vec4(a_position, 1.0);
172
+ }
173
+ `;
174
+
175
+ export const ANIMATION_TOON_FRAGMENT_SOURCE = `#version 300 es
176
+ // ${ANIMATION_TOON_SHADER_MARKER}
177
+ precision highp float;
178
+ uniform vec4 u_baseColor;
179
+ uniform float u_alphaCutoff;
180
+ uniform float u_toonBands;
181
+ uniform float u_toonShadowFloor;
182
+ uniform vec3 u_toonLightDirection;
183
+ uniform vec3 u_toonLightColor;
184
+ uniform vec3 u_toonRimColor;
185
+ uniform float u_toonRimPower;
186
+ uniform float u_toonRimIntensity;
187
+ uniform vec3 u_cameraPosition;
188
+ in vec3 v_normal;
189
+ in vec3 v_worldPosition;
190
+ in vec4 v_vertexColor;
191
+ out vec4 outColor;
192
+
193
+ // Quantize t in [0,1] into u_toonBands discrete levels — mirrors quantizeToonBand().
194
+ float a3dToonQuantize(float t, float bands) {
195
+ float steps = clamp(floor(bands + 0.5), 2.0, 16.0);
196
+ float clamped = clamp(t, 0.0, 1.0);
197
+ float level = min(steps - 1.0, floor(clamped * steps));
198
+ return level / (steps - 1.0);
199
+ }
200
+
201
+ void main() {
202
+ vec3 normal = normalize(v_normal);
203
+ if (!gl_FrontFacing) normal = -normal;
204
+ vec3 lightDir = normalize(u_toonLightDirection);
205
+ vec3 viewDir = normalize(u_cameraPosition - v_worldPosition);
206
+
207
+ float ndotl = max(dot(normal, lightDir), 0.0);
208
+ float ndotv = clamp(dot(normal, viewDir), 0.0, 1.0);
209
+
210
+ // Banded diffuse ramp lifted off zero by the shadow floor — mirrors toonDiffuseRamp().
211
+ float floorTerm = clamp(u_toonShadowFloor, 0.0, 1.0);
212
+ float banded = a3dToonQuantize(ndotl, u_toonBands);
213
+ float ramp = floorTerm + (1.0 - floorTerm) * banded;
214
+
215
+ // Fresnel rim — mirrors toonRimTerm().
216
+ float rim = clamp(pow(1.0 - ndotv, max(u_toonRimPower, 0.0)), 0.0, 1.0) * clamp(u_toonRimIntensity, 0.0, 1.0);
217
+
218
+ vec3 base = u_baseColor.rgb * v_vertexColor.rgb;
219
+ vec3 lit = base * ramp * u_toonLightColor;
220
+ vec3 color = clamp(lit + u_toonRimColor * rim, 0.0, 1.0);
221
+
222
+ float alpha = u_baseColor.a * v_vertexColor.a;
223
+ if (alpha < u_alphaCutoff) discard;
224
+ outColor = vec4(color, alpha);
225
+ }
226
+ `;
227
+
228
+ /**
229
+ * Register the animation toon shader program into a ShaderLibrary-like object.
230
+ * Typed structurally to avoid an import cycle with ShaderLibrary; the real
231
+ * `ShaderLibrary.register` matches this signature.
232
+ */
233
+ export interface AnimationToonShaderRegistrar {
234
+ register(shader: {
235
+ readonly name: string;
236
+ readonly marker: string;
237
+ readonly vertex: string;
238
+ readonly fragment: string;
239
+ }): void;
240
+ }
241
+
242
+ export function registerAnimationToonShader(library: AnimationToonShaderRegistrar): void {
243
+ library.register({
244
+ name: ANIMATION_TOON_SHADER_NAME,
245
+ marker: ANIMATION_TOON_SHADER_MARKER,
246
+ vertex: ANIMATION_TOON_VERTEX_SOURCE,
247
+ fragment: ANIMATION_TOON_FRAGMENT_SOURCE
248
+ });
249
+ }
250
+
251
+ function identityMatrix(): Float32Array {
252
+ return new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
253
+ }
254
+
255
+ function validateUnit(value: number, label: string): void {
256
+ if (!Number.isFinite(value) || value < 0 || value > 1) {
257
+ throw new RangeError(`AnimationToonMaterial ${label} must be finite and within [0, 1]`);
258
+ }
259
+ }
260
+
261
+ function validateColor4(value: readonly number[], label: string): void {
262
+ if (value.length !== 4 || value.some((c) => !Number.isFinite(c) || c < 0 || c > 1)) {
263
+ throw new RangeError(`AnimationToonMaterial ${label} must contain four finite values in [0, 1]`);
264
+ }
265
+ }
266
+
267
+ function validateColor3(value: readonly number[], label: string): void {
268
+ if (value.length !== 3 || value.some((c) => !Number.isFinite(c) || c < 0 || c > 1)) {
269
+ throw new RangeError(`AnimationToonMaterial ${label} must contain three finite values in [0, 1]`);
270
+ }
271
+ }
272
+
273
+ function validateFiniteVec3(value: readonly number[], label: string): void {
274
+ if (value.length !== 3 || value.some((c) => !Number.isFinite(c))) {
275
+ throw new RangeError(`AnimationToonMaterial ${label} must contain three finite values`);
276
+ }
277
+ }
278
+
279
+ export class AnimationToonMaterial extends Material {
280
+ constructor(options: AnimationToonMaterialOptions = {}) {
281
+ const baseColor = options.baseColor ?? [0.92, 0.58, 0.16, 1];
282
+ const lightDirection = options.lightDirection ?? [0.3, 0.8, 0.5];
283
+ const lightColor = options.lightColor ?? [1, 1, 1];
284
+ const rimColor = options.rimColor ?? [0.6, 0.7, 1];
285
+ const bands = options.bands ?? 4;
286
+ const shadowFloor = options.shadowFloor ?? 0.35;
287
+ const rimPower = options.rimPower ?? 3;
288
+ const rimIntensity = options.rimIntensity ?? 0.5;
289
+ const alphaCutoff = options.alphaCutoff ?? 0;
290
+
291
+ validateColor4(baseColor, "baseColor");
292
+ validateFiniteVec3(lightDirection, "lightDirection");
293
+ if (Math.hypot(...lightDirection) <= 0) {
294
+ throw new RangeError("AnimationToonMaterial lightDirection must be non-zero");
295
+ }
296
+ validateColor3(lightColor, "lightColor");
297
+ validateColor3(rimColor, "rimColor");
298
+ if (!Number.isInteger(bands) || bands < ANIMATION_TOON_MIN_BANDS || bands > ANIMATION_TOON_MAX_BANDS) {
299
+ throw new RangeError(
300
+ `AnimationToonMaterial bands must be an integer in [${ANIMATION_TOON_MIN_BANDS}, ${ANIMATION_TOON_MAX_BANDS}]`
301
+ );
302
+ }
303
+ validateUnit(shadowFloor, "shadowFloor");
304
+ if (!Number.isFinite(rimPower) || rimPower < 0) {
305
+ throw new RangeError("AnimationToonMaterial rimPower must be finite and non-negative");
306
+ }
307
+ validateUnit(rimIntensity, "rimIntensity");
308
+ validateUnit(alphaCutoff, "alphaCutoff");
309
+
310
+ super({
311
+ name: options.name ?? "animation-toon",
312
+ shaderKey: ANIMATION_TOON_SHADER_NAME,
313
+ renderState: options.renderState,
314
+ parameters: {
315
+ u_baseColor: baseColor,
316
+ u_alphaCutoff: alphaCutoff,
317
+ u_toonBands: bands,
318
+ u_toonShadowFloor: shadowFloor,
319
+ u_toonLightDirection: lightDirection,
320
+ u_toonLightColor: lightColor,
321
+ u_toonRimColor: rimColor,
322
+ u_toonRimPower: rimPower,
323
+ u_toonRimIntensity: rimIntensity,
324
+ u_cameraPosition: [0, 0, 0],
325
+ u_modelViewProjection: identityMatrix(),
326
+ u_modelMatrix: identityMatrix(),
327
+ u_normalMatrix: identityMatrix()
328
+ },
329
+ requiredAttributes: ["a_position", "a_normal"],
330
+ uniformSchema: [
331
+ { name: "u_baseColor", kind: "vec4" },
332
+ { name: "u_alphaCutoff", kind: "float" },
333
+ { name: "u_toonBands", kind: "float" },
334
+ { name: "u_toonShadowFloor", kind: "float" },
335
+ { name: "u_toonLightDirection", kind: "vec3" },
336
+ { name: "u_toonLightColor", kind: "vec3" },
337
+ { name: "u_toonRimColor", kind: "vec3" },
338
+ { name: "u_toonRimPower", kind: "float" },
339
+ { name: "u_toonRimIntensity", kind: "float" },
340
+ { name: "u_cameraPosition", kind: "vec3" },
341
+ { name: "u_modelViewProjection", kind: "mat4" },
342
+ { name: "u_modelMatrix", kind: "mat4" },
343
+ { name: "u_normalMatrix", kind: "mat4" }
344
+ ]
345
+ });
346
+ }
347
+
348
+ set baseColor(value: readonly [number, number, number, number]) {
349
+ validateColor4(value, "baseColor");
350
+ this.setParameter("u_baseColor", value);
351
+ }
352
+
353
+ get baseColor(): readonly [number, number, number, number] {
354
+ return this.getParameter("u_baseColor") as readonly [number, number, number, number];
355
+ }
356
+
357
+ set bands(value: number) {
358
+ if (!Number.isInteger(value) || value < ANIMATION_TOON_MIN_BANDS || value > ANIMATION_TOON_MAX_BANDS) {
359
+ throw new RangeError(
360
+ `AnimationToonMaterial bands must be an integer in [${ANIMATION_TOON_MIN_BANDS}, ${ANIMATION_TOON_MAX_BANDS}]`
361
+ );
362
+ }
363
+ this.setParameter("u_toonBands", value);
364
+ }
365
+
366
+ get bands(): number {
367
+ return this.getParameter("u_toonBands") as number;
368
+ }
369
+
370
+ set shadowFloor(value: number) {
371
+ validateUnit(value, "shadowFloor");
372
+ this.setParameter("u_toonShadowFloor", value);
373
+ }
374
+
375
+ get shadowFloor(): number {
376
+ return this.getParameter("u_toonShadowFloor") as number;
377
+ }
378
+
379
+ set rimIntensity(value: number) {
380
+ validateUnit(value, "rimIntensity");
381
+ this.setParameter("u_toonRimIntensity", value);
382
+ }
383
+
384
+ get rimIntensity(): number {
385
+ return this.getParameter("u_toonRimIntensity") as number;
386
+ }
387
+ }