3D engine that AI agents drive
Move liquid, splat and environment map code out of the core bundle
Nguyen Xuan Ancc58b0ba18c0
diff --git a/bench/reference/harness.js b/bench/reference/harness.jsindex 9aa520bd..b5ae4144 100644--- a/bench/reference/harness.js+++ b/bench/reference/harness.js@@ -6,6 +6,8 @@ import { LOOK_OPTIONS } from './look-options.js';import { skinnedGeometry } from '/src/path-tracer-scene.js';import { boxProjectedGeometry } from '/src/export-box-uv.js';import '/src/material-layer-shaders.js';+import '/src/liquid-shader.js';+import '/src/splat-shader.js';import { skyOpenness } from '/src/closed-room.js';import { fogSkyVisibility } from '/src/fog-sky-visibility.js';import { createCascadedShadows } from '/src/cascaded-shadows.js';diff --git a/bench/size/README.md b/bench/size/README.mdindex fa5f8c3a..81609cc2 100644--- a/bench/size/README.md+++ b/bench/size/README.md@@ -10,17 +10,40 @@`manifold-3d` and `@dimforge/rapier3d-compat` are optional peers that load separately, so no bundle counts them.-## Baseline+## Where the goal scenes stand-Measured on 2026-10-31 with three.js 0.186.1. Sizes are bytes gzip.+Measured 2026-11-03 with three.js 0.186.1. Sizes are bytes gzip. `npm run size` prints this table's 67js column and its distance to the goal on every run.-| Scene | 67js | three.js | Goal |-| --- | --- | --- | --- |-| `lit-box` | 48,762 | 133,652 | 50,000 |-| `gltf-box` | 159,422 | 155,814 | 80,000 |-| `agent-scene` | 185,592 | no equivalent | 100,000 |+| Scene | Before (base at 2026-11-03) | Now | three.js | Goal |+| --- | --- | --- | --- | --- |+| `lit-box` | 66,497 | 56,343 | 133,652 | 50,000 (6,343 over) |+| `gltf-box` | 90,790 | 80,866 | 155,814 | 80,000 (866 over) |+| `agent-scene` | 110,277 | 103,794 | no equivalent | 100,000 (3,794 over) |-`bench/size/baseline.json` holds every size. The goals are proposals from issue 23.+The values on 2026-10-30 (issue 23) were 48,758, 159,422 and 185,422. `bench/size/baseline.json` holds every size. The goals are proposals from issue 23.++## What is in the lit box++A box drawn by `Renderer67` with no addon, minified, before gzip (module sizes from the esbuild metafile; the sum is 191 KB, 54.8 KB gzip). The numbers are what the module adds to the bundle, not what it gzips to alone.++| Module | Bytes minified | What it is |+| --- | --- | --- |+| `src/renderer.js` | about 77,000 | the renderer: batching, shadows, post, diagnostics, skin and morph upload, all draw passes |+| `src/shaders.js` | about 43,000 | vertex, depth, benchmark and main fragment GLSL (comments in GLSL ship), skin and morph GLSL |+| `gl-matrix` mat4 and mat3 | about 22,000 | matrix functions the renderer and camera use |+| `src/post.js` | 14,200 | post, sky and bloom fragment shaders |+| `src/environment.js` | not in a lit box | the environment map builder; `environment-shading.js` (about 4,000) keeps the lookup GLSL that main names |+| `src/lighting.js`, `morph.js`, `skinning.js`, `material-layers.js`, `effects.js`, `geometry.js`, `budget.js`, `ambient-occlusion.js`, others | 1,000 to 5,000 each | smaller parts |++Moved out of the core bundle in this work, each loaded only by a scene that uses it (the pattern of `material-layer-shaders.js`):++| Addon | Holds | Saved in a lit box |+| --- | --- | --- |+| `liquid-shader` | liquid and foam GLSL, program and draw pass | 4,900 together with the splat GLSL, measured in steps |+| `splat-shader` | splat ground GLSL, validation, texture arrays, binding | included above, plus 2,700 for its JavaScript |+| `environment.js` (loads with `createEnvironmentMap`) | environment map builder, prefilter, BRDF and background shaders | 2,600 |++Still in the lit box and worth moving next, with rough gzip sizes: skin and morph GLSL plus `skinning.js` and `morph.js` (about 3,500; the depth and benchmark vertex shaders call them unconditionally), the tag code in main for terrain, moss, foliage, grass, fire, smoke and water (about 2,000), the environment lookup and shading GLSL (about 1,800, needs stubs in main), comments inside GLSL (about 2,000, a build step would strip them), and the post pass volume, occlusion and explosion steps.## How the check decidesdiff --git a/bench/size/budgets.json b/bench/size/budgets.jsonindex de18c020..068e7e1b 100644--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@ -54,6 +54,8 @@"scene:agent-scene+lazy": 300582,"entry:coplanar-overlap": 2960,"entry:view-transform": 2120,- "entry:view-transform-shader": 21213+ "entry:view-transform-shader": 21213,+ "entry:liquid-shader": 16892,+ "entry:splat-shader": 20897}}diff --git a/changes/281.md b/changes/281.mdnew file mode 100644index 00000000..5897b3a3--- /dev/null+++ b/changes/281.md@@ -0,0 +1 @@+- Move the liquid and foam pass and the splat ground material out of the core renderer bundle (part of #281). Breaking: a renderer that draws a transmissive, `liquid` or `foam` part needs `import '@codepawlhq/67js/addons/liquid-shader'`, and one that draws `material.layers` needs `import '@codepawlhq/67js/addons/splat-shader'`; recipes and glTF files with transmission load the liquid code on their own, and the Spume player imports it. The environment map builder loads with `createEnvironmentMap`, so a renderer without an environment map no longer carries it. Measured by `npm run size`, gzip: `scene:lit-box` 66,497 to 56,343, `scene:gltf-box` 90,790 to 80,866, `scene:agent-scene` 110,277 to 103,794. The liquid program also compiles on the first liquid part instead of at start. `npm run size` now prints each goal scene against its goal.diff --git a/changes/281.size.md b/changes/281.size.mdnew file mode 100644index 00000000..f3a71f57--- /dev/null+++ b/changes/281.size.md@@ -0,0 +1,4 @@+- entry:spume: spume draws liquid and foam, so it now carries the liquid shader and pass that left the core bundle; every scene without liquid stops paying for them.+- entry:ground-bounce: the ground bounce registers the environment map builder, which moved out of the core bundle with its lookup GLSL.+- entry:hdri: the HDRI entry registers the environment map builder and shading that moved out of the core bundle.+- archive: the liquid and splat shaders ship as two addon files with their own entry chunks, so the package grows by about 2.8 KB while every scene that does not use them loads less.diff --git a/package.json b/package.jsonindex 76ac02c5..d1566963 100644--- a/package.json+++ b/package.json@@ -90,6 +90,8 @@"./addons/material-layer-shaders": "./build/material-layer-shaders.js","./addons/view-transform-shader": "./build/view-transform-shader.js","./addons/view-transform": "./build/view-transform.js",+ "./addons/liquid-shader": "./build/liquid-shader.js",+ "./addons/splat-shader": "./build/splat-shader.js","./addons/light-linking": "./build/light-linking.js","./addons/cascaded-shadows": "./build/cascaded-shadows.js","./addons/gaussian-splats": "./build/gaussian-splats.js",diff --git a/scripts/measure-size.mjs b/scripts/measure-size.mjsindex 9f637a49..aa2c31d8 100644--- a/scripts/measure-size.mjs+++ b/scripts/measure-size.mjs@@ -139,15 +139,21 @@ async function writeJson(path, value) {await writeFile(path, `${JSON.stringify(value, null, 1)}\n`);}-function printComparisonWithThree(measured, three, goals) {+function printComparisonWithThree(measured, three) {console.log('\nThe same scene in both engines, in bytes gzip:');for (const [name, threeSize] of Object.entries(three)) {- const engineSize = measured[`scene:${name}`].gzip;- const goal = goals[name] === undefined ? '' : `, goal ${goals[name]}`;- console.log(` ${name.padEnd(14)} 67js ${engineSize}, three.js ${threeSize.gzip}${goal}`);+ console.log(` ${name.padEnd(14)} 67js ${measured[`scene:${name}`].gzip}, three.js ${threeSize.gzip}`);}+}++// A positive distance is bytes over the goal. A budget raised to fit a change shows here as the distance growing.+function printGoals(measured, goals, budgets) {+ console.log('\nGoal scenes against their goals, in bytes gzip:');for (const [name, goal] of Object.entries(goals)) {- if (!three[name]) console.log(` ${name.padEnd(14)} 67js ${measured[`scene:${name}`].gzip}, no three.js equivalent, goal ${goal}`);+ const size = measured[`scene:${name}`].gzip;+ const distance = size - goal;+ const status = distance <= 0 ? 'met' : `${distance} over`;+ console.log(` ${name.padEnd(14)} ${String(size).padStart(7)} of goal ${goal}: ${status} (budget ${budgets[`scene:${name}`]}, ${budgets[`scene:${name}`] - goal} over the goal)`);}}@@ -206,7 +212,8 @@ if (checkGrowth) {// The absolute budgets are the release ceilings. They are reported on a pull request and enforced with --absolute.console.log('\nAgainst the release budgets:');console.log(formatRows(rows));-printComparisonWithThree(measured, three, budgetFile.goals);+printComparisonWithThree(measured, three);+printGoals(measured, budgetFile.goals, budgetFile.budgets);if (ceilingProblems.length) {if (flags.has('--absolute')) {problems.push(...ceilingProblems);diff --git a/src/agent-scene-modules.js b/src/agent-scene-modules.jsindex b2c4a89f..732c1260 100644--- a/src/agent-scene-modules.js+++ b/src/agent-scene-modules.js@@ -17,6 +17,7 @@ const MODULE_LOADERS = {relations: () => import('./agent-relations.js'),timeline: () => import('./film-timeline.js'),layerShaders: () => import('./material-layer-shaders.js'),+ liquidShader: () => import('./liquid-shader.js'),terrain: () => import('./agent-terrain.js'),navigation: () => import('./agent-navigation.js'),lightLinking: () => import('./light-linking.js'),@@ -47,6 +48,7 @@ const MODULE_MARKERS = {relations: ['"relations":'],timeline: ['"timeline":{'],layerShaders: ['"anisotropy"', '"iridescence"'],+ liquidShader: ['"transmission"', '"liquid"', '"foam"'],terrain: ['"type":"terrain"'],// No recipe text asks for it, only the path option of scene_inspect does, so a host loads it with loadAllAgentSceneModules.navigation: [],diff --git a/src/environment-shading.js b/src/environment-shading.jsnew file mode 100644index 00000000..910505be--- /dev/null+++ b/src/environment-shading.js@@ -0,0 +1,97 @@+// The GLSL that every lit program carries to read an environment map, kept apart from the code that builds one so a+// renderer without an environment map does not carry the builder. environment.js registers that code when it loads.++export const equirectangularGlsl = `+vec3 directionFromEquirectangular(vec2 coordinates) {+ float azimuth = (coordinates.x - 0.5) * 6.28318530718;+ float polar = coordinates.y * 3.14159265359;+ return vec3(sin(polar) * sin(azimuth), cos(polar), -sin(polar) * cos(azimuth));+}+vec2 equirectangularFromDirection(vec3 direction) {+ return vec2(atan(direction.x, -direction.z) * 0.15915494309 + 0.5, acos(clamp(direction.y, -1.0, 1.0)) * 0.31830988618);+}+`;++export const environmentLookupGlsl = `+uniform sampler2D environmentSpecular;+uniform vec2 environmentRotation;+uniform float environmentIntensity;+uniform float environmentLevelMax;+${equirectangularGlsl}+vec3 environmentDirection(vec3 direction) {+ return vec3(environmentRotation.x * direction.x - environmentRotation.y * direction.z, direction.y, environmentRotation.y * direction.x + environmentRotation.x * direction.z);+}+vec3 environmentRadiance(vec3 direction, float level) {+ return textureLod(environmentSpecular, equirectangularFromDirection(environmentDirection(direction)), level).rgb * environmentIntensity;+}+`;++// environmentLighting adds back the light a rough microfacet surface bounces a second time, as Cycles' multiscatter GGX does (Fdez-Aguera 2019).+export const environmentShadingGlsl = `+uniform sampler2D environmentBrdf;+uniform float environmentEnabled;+uniform vec3 environmentIrradiance[9];+vec3 environmentDiffuse(vec3 normal) {+ vec3 mapped = environmentDirection(normal);+ // The light shading preset keeps the first four terms (the average and the direction of the light), not the second order ones.+ if (LIGHT_SHADING) {+ vec3 firstOrder = environmentIrradiance[0] + environmentIrradiance[1] * mapped.y + environmentIrradiance[2] * mapped.z + environmentIrradiance[3] * mapped.x;+ return max(firstOrder, vec3(0.0)) * environmentIntensity;+ }+ vec3 result = environmentIrradiance[0]+ + environmentIrradiance[1] * mapped.y+ + environmentIrradiance[2] * mapped.z+ + environmentIrradiance[3] * mapped.x+ + environmentIrradiance[4] * (mapped.x * mapped.y)+ + environmentIrradiance[5] * (mapped.y * mapped.z)+ + environmentIrradiance[6] * (3.0 * mapped.z * mapped.z - 1.0)+ + environmentIrradiance[7] * (mapped.x * mapped.z)+ + environmentIrradiance[8] * (mapped.x * mapped.x - mapped.y * mapped.y);+ return max(result, vec3(0.0)) * environmentIntensity;+}+// Light a rough microfacet loses on its first bounce and gets back on a second one, as a factor on single-scatter specular.+vec3 multipleScatterGain(vec2 lookup, vec3 baseReflectance) {+ float missingEnergy = 1.0 - (lookup.x + lookup.y);+ vec3 averageFresnel = baseReflectance + (1.0 - baseReflectance) / 21.0;+ return 1.0 / (1.0 - missingEnergy * averageFresnel);+}+// specularWeight scales only the reflected sky; leaf cards use less, since their bent normals face the view at grazing angles.+// Analytic fit of the lookup table (Karis 2014), for the light shading preset.+vec2 environmentBrdfFit(float normalView, float roughness) {+ vec4 terms = roughness * vec4(-1.0, -0.0275, -0.572, 0.022) + vec4(1.0, 0.0425, 1.04, -0.04);+ float edge = min(terms.x * terms.x, exp2(-9.28 * normalView)) * terms.x + terms.y;+ return vec2(-1.04, 1.04) * edge + terms.zw;+}+vec3 environmentLighting(vec3 normal, vec3 specularNormal, vec3 baseReflectance, vec3 viewDirection, vec3 reflected, vec3 color, float metallic, float roughness, float occlusion, float ambient, float specularWeight) {+ if (LIGHT_SHADING) {+ // No second bounce, and the sky reflection is the blurred sky of the surface's own normal: one irradiance read serves both.+ vec2 fitted = environmentBrdfFit(clamp(dot(normal, viewDirection), 0.001, 1.0), roughness);+ vec3 fittedColor = (baseReflectance * fitted.x + fitted.y) * specularWeight;+ vec3 irradiance = environmentDiffuse(normal);+ return irradiance * (fittedColor + color * (1.0 - metallic) * (1.0 - fittedColor)) * occlusion * ambient;+ }+ float normalView = clamp(dot(normal, viewDirection), 0.001, 1.0);+ vec2 lookup = texture(environmentBrdf, vec2(normalView, roughness)).rg;+ vec3 singleScatter = baseReflectance * lookup.x + lookup.y;+ vec3 specularColor = singleScatter * multipleScatterGain(lookup, baseReflectance) * specularWeight;+ vec3 specular = environmentRadiance(reflected, roughness * environmentLevelMax) * specularColor;+ vec3 diffuse = environmentDiffuse(normal) * color * (1.0 - metallic) * (1.0 - specularColor);+ return (specular + diffuse) * occlusion * ambient;+}++`;++// environment.js sets the JavaScript half: assertMap, estimateBytes, createResources and the prefilter, BRDF and+// background fragment shaders. The renderer asks for it only when the scene has an environment map.+const environmentSupport = {};++export function registerEnvironmentSupport(functions) {+ Object.assign(environmentSupport, functions);+}++export function requireEnvironmentSupport() {+ if (!environmentSupport.createResources) {+ throw new Error("An environment map needs its code: add import '@codepawlhq/67js/addons/hdri' or import createEnvironmentMap from the package entry.");+ }+ return environmentSupport;+}diff --git a/src/environment.js b/src/environment.jsindex 2aacc015..bc067f37 100644--- a/src/environment.js+++ b/src/environment.js@@ -2,6 +2,8 @@// Convention: image row 0 is straight up, the image centre looks toward -Z, and// `rotation` turns the environment about +Y in radians.+import { equirectangularGlsl, environmentLookupGlsl, environmentShadingGlsl, registerEnvironmentSupport } from './environment-shading.js';+export const ENVIRONMENT_ROUGHNESS_LEVELS = 7;export const ENVIRONMENT_BRDF_SIZE = 128;export const MAXIMUM_ENVIRONMENT_SIZE = 16384;@@ -145,16 +147,6 @@ function paddedHalfFloatSource(map) {return padded;}-const equirectangularGlsl = `-vec3 directionFromEquirectangular(vec2 coordinates) {- float azimuth = (coordinates.x - 0.5) * 6.28318530718;- float polar = coordinates.y * 3.14159265359;- return vec3(sin(polar) * sin(azimuth), cos(polar), -sin(polar) * cos(azimuth));-}-vec2 equirectangularFromDirection(vec3 direction) {- return vec2(atan(direction.x, -direction.z) * 0.15915494309 + 0.5, acos(clamp(direction.y, -1.0, 1.0)) * 0.31830988618);-}-`;export const environmentPrefilterFragment = `#version 300 esprecision highp float;@@ -255,74 +247,7 @@ void main() {}`;// Shared lookup: rotation, intensity and the prefiltered chain, sampled by direction and roughness level.-export const environmentLookupGlsl = `-uniform sampler2D environmentSpecular;-uniform vec2 environmentRotation;-uniform float environmentIntensity;-uniform float environmentLevelMax;-${equirectangularGlsl}-vec3 environmentDirection(vec3 direction) {- return vec3(environmentRotation.x * direction.x - environmentRotation.y * direction.z, direction.y, environmentRotation.y * direction.x + environmentRotation.x * direction.z);-}-vec3 environmentRadiance(vec3 direction, float level) {- return textureLod(environmentSpecular, equirectangularFromDirection(environmentDirection(direction)), level).rgb * environmentIntensity;-}-`;-// environmentLighting adds back the light a rough microfacet surface bounces a second time, as Cycles' multiscatter GGX does (Fdez-Aguera 2019).-export const environmentShadingGlsl = `-uniform sampler2D environmentBrdf;-uniform float environmentEnabled;-uniform vec3 environmentIrradiance[9];-vec3 environmentDiffuse(vec3 normal) {- vec3 mapped = environmentDirection(normal);- // The light shading preset keeps the first four terms (the average and the direction of the light), not the second order ones.- if (LIGHT_SHADING) {- vec3 firstOrder = environmentIrradiance[0] + environmentIrradiance[1] * mapped.y + environmentIrradiance[2] * mapped.z + environmentIrradiance[3] * mapped.x;- return max(firstOrder, vec3(0.0)) * environmentIntensity;- }- vec3 result = environmentIrradiance[0]- + environmentIrradiance[1] * mapped.y- + environmentIrradiance[2] * mapped.z- + environmentIrradiance[3] * mapped.x- + environmentIrradiance[4] * (mapped.x * mapped.y)- + environmentIrradiance[5] * (mapped.y * mapped.z)- + environmentIrradiance[6] * (3.0 * mapped.z * mapped.z - 1.0)- + environmentIrradiance[7] * (mapped.x * mapped.z)- + environmentIrradiance[8] * (mapped.x * mapped.x - mapped.y * mapped.y);- return max(result, vec3(0.0)) * environmentIntensity;-}-// Light a rough microfacet loses on its first bounce and gets back on a second one, as a factor on single-scatter specular.-vec3 multipleScatterGain(vec2 lookup, vec3 baseReflectance) {- float missingEnergy = 1.0 - (lookup.x + lookup.y);- vec3 averageFresnel = baseReflectance + (1.0 - baseReflectance) / 21.0;- return 1.0 / (1.0 - missingEnergy * averageFresnel);-}-// specularWeight scales only the reflected sky; leaf cards use less, since their bent normals face the view at grazing angles.-// Analytic fit of the lookup table (Karis 2014), for the light shading preset.-vec2 environmentBrdfFit(float normalView, float roughness) {- vec4 terms = roughness * vec4(-1.0, -0.0275, -0.572, 0.022) + vec4(1.0, 0.0425, 1.04, -0.04);- float edge = min(terms.x * terms.x, exp2(-9.28 * normalView)) * terms.x + terms.y;- return vec2(-1.04, 1.04) * edge + terms.zw;-}-vec3 environmentLighting(vec3 normal, vec3 specularNormal, vec3 baseReflectance, vec3 viewDirection, vec3 reflected, vec3 color, float metallic, float roughness, float occlusion, float ambient, float specularWeight) {- if (LIGHT_SHADING) {- // No second bounce, and the sky reflection is the blurred sky of the surface's own normal: one irradiance read serves both.- vec2 fitted = environmentBrdfFit(clamp(dot(normal, viewDirection), 0.001, 1.0), roughness);- vec3 fittedColor = (baseReflectance * fitted.x + fitted.y) * specularWeight;- vec3 irradiance = environmentDiffuse(normal);- return irradiance * (fittedColor + color * (1.0 - metallic) * (1.0 - fittedColor)) * occlusion * ambient;- }- float normalView = clamp(dot(normal, viewDirection), 0.001, 1.0);- vec2 lookup = texture(environmentBrdf, vec2(normalView, roughness)).rg;- vec3 singleScatter = baseReflectance * lookup.x + lookup.y;- vec3 specularColor = singleScatter * multipleScatterGain(lookup, baseReflectance) * specularWeight;- vec3 specular = environmentRadiance(reflected, roughness * environmentLevelMax) * specularColor;- vec3 diffuse = environmentDiffuse(normal) * color * (1.0 - metallic) * (1.0 - specularColor);- return (specular + diffuse) * occlusion * ambient;-}--`;export const environmentBackgroundFragment = `#version 300 esprecision highp float;@@ -418,3 +343,14 @@ export function createEnvironmentResources(context, map, { compileProgram, fulls},};}++export { environmentLookupGlsl, environmentShadingGlsl };++registerEnvironmentSupport({+ assertMap: assertEnvironmentMap,+ estimateBytes: estimateEnvironmentBytes,+ createResources: createEnvironmentResources,+ prefilterFragment: environmentPrefilterFragment,+ brdfFragment: environmentBrdfFragment,+ backgroundFragment: environmentBackgroundFragment,+});diff --git a/src/gltf.js b/src/gltf.jsindex 971da74b..a077cb4c 100644--- a/src/gltf.js+++ b/src/gltf.js@@ -22,10 +22,12 @@ export const GLTF_CAPABILITIES = Object.freeze({const LAYER_SHADER_EXTENSIONS = Object.freeze(['KHR_materials_anisotropy', 'KHR_materials_iridescence']);-// The shader code for anisotropy and iridescence is not in the core bundle; a file that uses either brings it in.+// The shader code for anisotropy, iridescence and transmission is not in the core bundle; a file that uses one brings it in.async function loadLayerShadersFor(document) {const usesLayerShaders = (document.materials ?? []).some(material => LAYER_SHADER_EXTENSIONS.some(extension => material?.extensions?.[extension]) || layerTextureReferences(material).length > 0 || materialUvTransforms(material ?? {}).normalRelative);if (usesLayerShaders) await import('./material-layer-shaders.js');+ const usesLiquidShader = (document.materials ?? []).some(material => material?.extensions?.KHR_materials_transmission);+ if (usesLiquidShader) await import('./liquid-shader.js');}const DEFAULT_MAX_BYTES = 32 * 1024 * 1024;diff --git a/src/liquid-renderer.js b/src/liquid-renderer.jsnew file mode 100644index 00000000..f116e350--- /dev/null+++ b/src/liquid-renderer.js@@ -0,0 +1,89 @@+import { vertexShader, liquidFragmentSource } from './shaders.js';+import { startProgram, finishProgram } from './gl-program.js';++// The renderer's liquid and foam pass, kept apart from renderer.js so a scene without liquid or foam does not carry it.+// liquid-shader.js registers drawLiquidPass; the renderer calls it with the frame state it needs.++const LIQUID_UNIFORMS = [+ 'viewProjection', 'lightProjection', 'eye', 'sceneColor', 'resolution', 'exposure', 'hdrEnabled', 'worldMode', 'daylight', 'lightDirection',+ 'keyColor', 'ambientIntensity', 'fogColor', 'fogDensity', 'glassState', 'glassTint', 'liquidOptics', 'foamShape',+ 'environmentSpecular', 'environmentEnabled', 'environmentIntensity', 'environmentRotation', 'environmentLevelMax',+ 'time', 'wind', 'explosionProgress', 'explosionCenter', 'assemblyAmount',+ 'skinEnabled', 'skinInfluences', 'jointPalette',+ 'morphEnabled', 'morphVertexCount', 'morphTargetCount', 'morphWeights[0]', 'morphTargets',+];++// The program compiles when the first liquid or foam batch draws, so a renderer without them never builds it.+function ensureLiquidProgram(renderer, environmentSpecularUnit) {+ if (renderer.liquidProgram) return;+ const context = renderer.context;+ renderer.liquidProgram = finishProgram(context, startProgram(context, vertexShader, renderer.withViewTransform(liquidFragmentSource(), 'liquid')));+ renderer.liquidUniforms = Object.fromEntries(LIQUID_UNIFORMS.map(name => [name, context.getUniformLocation(renderer.liquidProgram, name)]));+ context.useProgram(renderer.liquidProgram);+ context.uniform1i(renderer.liquidUniforms.skinInfluences, 6);+ context.uniform1i(renderer.liquidUniforms.jointPalette, 7);+ context.uniform1i(renderer.liquidUniforms.morphTargets, 8);+ context.uniform1i(renderer.liquidUniforms.environmentSpecular, environmentSpecularUnit);+}++export function drawLiquidPass(renderer, { frame, environment, assemblyAmount, environmentMap, liquids, foams, optics, environmentSpecularUnit, maximumDistance }) {+ ensureLiquidProgram(renderer, environmentSpecularUnit);+ const context = renderer.context;+ const width = renderer.canvas.width;+ const height = renderer.canvas.height;+ // Liquids refract the opaque scene, so resolve it into frameTexture before drawing them on top.+ context.bindFramebuffer(context.READ_FRAMEBUFFER, renderer.multisampleFramebuffer);+ context.bindFramebuffer(context.DRAW_FRAMEBUFFER, renderer.resolveFramebuffer);+ context.blitFramebuffer(0, 0, width, height, 0, 0, width, height, context.COLOR_BUFFER_BIT, context.NEAREST);+ context.bindFramebuffer(context.FRAMEBUFFER, renderer.multisampleFramebuffer);+ context.viewport(0, 0, width, height);+ context.enable(context.DEPTH_TEST);+ context.enable(context.CULL_FACE);+ context.useProgram(renderer.liquidProgram);+ const uniforms = renderer.liquidUniforms;+ context.uniformMatrix4fv(uniforms.viewProjection, false, renderer.viewProjection);+ context.uniformMatrix4fv(uniforms.lightProjection, false, renderer.lightProjection);+ context.uniform3fv(uniforms.eye, frame.eye);+ context.uniform2f(uniforms.resolution, width, height);+ context.uniform1f(uniforms.exposure, frame.exposure ?? 1);+ context.uniform1f(uniforms.hdrEnabled, renderer.hdr ? 1 : 0);+ context.uniform1f(uniforms.worldMode, renderer.world ? 1 : 0);+ context.uniform1f(uniforms.daylight, environment.daylight);+ context.uniform3fv(uniforms.lightDirection, environment.lightDirection);+ context.uniform3fv(uniforms.keyColor, renderer.lighting.keyColor);+ context.uniform1f(uniforms.ambientIntensity, renderer.lighting.ambientIntensity);+ context.uniform3fv(uniforms.fogColor, environment.fogColor);+ context.uniform1f(uniforms.fogDensity, environment.fogDensity);+ context.uniform4f(uniforms.liquidOptics, optics.thickness, optics.ior, optics.refraction, optics.scattering);+ context.uniform2f(uniforms.foamShape, optics.foamOpacity, optics.foamSoftness);+ renderer.applyEnvironmentUniforms(environmentMap, uniforms);+ context.uniform1f(uniforms.time, frame.time ?? 0);+ context.uniform1f(uniforms.wind, frame.wind ?? 0.3);+ context.uniform1f(uniforms.explosionProgress, frame.explosionProgress ?? 1);+ context.uniform3fv(uniforms.explosionCenter, environment.explosionCenter);+ context.uniform1f(uniforms.assemblyAmount, assemblyAmount);+ if (uniforms.skinEnabled) context.uniform1f(uniforms.skinEnabled, 0);+ if (uniforms.morphEnabled) context.uniform1f(uniforms.morphEnabled, 0);+ context.activeTexture(context.TEXTURE0);+ context.bindTexture(context.TEXTURE_2D, renderer.frameTexture);+ context.uniform1i(uniforms.sceneColor, 0);+ for (const batch of liquids) {+ const layers = batch.layerState;+ context.uniform4f(uniforms.glassState, layers[9], layers[10], layers[11], Math.min(layers[12], maximumDistance));+ context.uniform3f(uniforms.glassTint, layers[13], layers[14], layers[15]);+ context.bindVertexArray(batch.vertexArray);+ context.drawElementsInstanced(context.TRIANGLES, batch.indexCount, context.UNSIGNED_INT, 0, batch.instanceCount);+ }+ if (foams.length > 0) {+ context.enable(context.BLEND);+ context.blendFuncSeparate(context.SRC_ALPHA, context.ONE_MINUS_SRC_ALPHA, context.ONE, context.ONE_MINUS_SRC_ALPHA);+ context.depthMask(false);+ for (const batch of foams) {+ context.bindVertexArray(batch.vertexArray);+ context.drawElementsInstanced(context.TRIANGLES, batch.indexCount, context.UNSIGNED_INT, 0, batch.instanceCount);+ }+ context.depthMask(true);+ context.disable(context.BLEND);+ }+ context.bindVertexArray(null);+}diff --git a/src/liquid-shader.js b/src/liquid-shader.jsnew file mode 100644index 00000000..1733a44a--- /dev/null+++ b/src/liquid-shader.js@@ -0,0 +1,136 @@+import { environmentLookupGlsl } from './environment-shading.js';+import { registerShaderFeatureCode } from './shaders.js';+import { drawLiquidPass } from './liquid-renderer.js';++// Importing this module registers the liquid and foam shader. A renderer with a transmissive, liquid or foam part+// needs it; any other renderer never carries it.+// Liquid and foam are drawn after the opaque scene, which is resolved into sceneColor first.+// Liquid: refraction through the scene, Beer-Lambert absorption over a convex-body thickness+// estimate, in-scattering, Fresnel environment reflection and a GGX key highlight.+// Foam: soft blended droplets whose alpha falls off toward their silhouettes.+export const liquidFragmentShader = `#version 300 es+precision highp float;+in vec3 worldPosition;+in vec3 worldNormal;+flat in vec3 baseColor;+flat in vec4 surface;+in vec4 shadowPosition;+in vec2 textureUV;+in vec3 vertexTint;+flat in vec3 emissionColor;+uniform vec3 eye;+uniform mat4 viewProjection;+uniform sampler2D sceneColor;+uniform vec2 resolution;+uniform float exposure;+uniform float hdrEnabled;+uniform float worldMode;+uniform float daylight;+uniform vec3 lightDirection;+uniform vec3 keyColor;+uniform float ambientIntensity;+uniform vec3 fogColor;+uniform float fogDensity;+// x: thickness in metres, y: index of refraction, z: refraction strength, w: scattering+uniform vec4 liquidOptics;+// A transmissive PBR material: x: transmission, y: index of refraction, z: thickness in metres, w: attenuation distance+uniform vec4 glassState;+uniform vec3 glassTint;+// x: foam opacity, y: foam edge softness+uniform vec2 foamShape;+out vec4 outputColor;+const float PI = 3.14159265359;++vec3 toneMap(vec3 color) {+ return clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), 0.0, 1.0);+}++${environmentLookupGlsl}+uniform float environmentEnabled;++vec3 environmentColor(vec3 direction, float roughness) {+ if (environmentEnabled > 0.5) return environmentRadiance(direction, roughness * environmentLevelMax);+ if (worldMode > 0.5) {+ vec3 zenith = vec3(0.20, 0.38, 0.52) * max(daylight, 0.08);+ return mix(fogColor * 0.75, zenith, smoothstep(-0.15, 0.7, direction.y));+ }+ vec3 color = mix(vec3(0.025, 0.035, 0.045), vec3(0.22, 0.25, 0.27), smoothstep(-0.3, 0.8, direction.y));+ float blur = roughness * 0.35;+ float key = smoothstep(0.80 - blur, 0.87, dot(direction, normalize(vec3(-3.0, 5.0, 4.0))));+ float rim = smoothstep(0.90 - blur, 0.97, dot(direction, normalize(vec3(4.0, 2.0, -3.0))));+ float strip = exp(-pow((direction.x + 0.6) / (0.035 + blur), 2.0)) * smoothstep(-0.1, 0.6, direction.y);+ return color + key * vec3(2.8, 2.2, 1.6) + rim * vec3(0.5, 1.6, 1.5) + strip * vec3(0.7);+}++float specularLobe(vec3 normal, vec3 viewDirection, vec3 light, float roughness) {+ float normalLight = max(dot(normal, light), 0.0);+ if (normalLight == 0.0) return 0.0;+ vec3 halfway = normalize(viewDirection + light);+ float normalView = max(dot(normal, viewDirection), 0.001);+ float normalHalf = max(dot(normal, halfway), 0.0);+ float alpha = roughness * roughness;+ float denominator = normalHalf * normalHalf * (alpha * alpha - 1.0) + 1.0;+ float distribution = alpha * alpha / max(PI * denominator * denominator, 0.00001);+ float masking = pow(roughness + 1.0, 2.0) / 8.0;+ float geometry = normalView / (normalView * (1.0 - masking) + masking) * normalLight / (normalLight * (1.0 - masking) + masking);+ return distribution * geometry / max(4.0 * normalView * normalLight, 0.001) * normalLight;+}++vec3 ambientLight(vec3 normal) {+ vec3 studioAmbient = vec3(0.16, 0.20, 0.22) * (0.45 + 0.55 * max(normal.y, 0.0));+ vec3 worldAmbient = vec3(0.19, 0.25, 0.30) * max(daylight, 0.08) * (0.45 + 0.55 * max(normal.y, 0.0));+ return mix(studioAmbient, worldAmbient, worldMode) * ambientIntensity;+}++vec3 displayed(vec3 radiance) {+ return hdrEnabled > 0.5 ? clamp(radiance * exposure, vec3(0), vec3(65504.0)) : pow(toneMap(radiance * exposure), vec3(1.0 / 2.2));+}++void main() {+ vec3 normal = normalize(worldNormal);+ if (!gl_FrontFacing) normal = -normal;+ vec3 viewDirection = normalize(eye - worldPosition);+ float facing = max(dot(normal, viewDirection), 0.0);+ vec3 light = normalize(lightDirection);+ vec3 color = baseColor * vertexTint;+ float fog = (1.0 - exp(-length(worldPosition - eye) * fogDensity)) * worldMode;++ if (surface.w == 12.0) {+ float alpha = foamShape.x * smoothstep(0.0, max(foamShape.y, 0.001), facing);+ if (alpha < 0.004) discard;+ float wrapped = max(dot(normal, light) * 0.5 + 0.5, 0.0);+ vec3 radiance = color * (ambientLight(normal) * 1.6 + keyColor * wrapped * 0.32);+ radiance = mix(radiance, fogColor, fog);+ outputColor = vec4(displayed(radiance), alpha);+ return;+ }++ float roughness = clamp(surface.y, 0.02, 1.0);+ vec2 screen = gl_FragCoord.xy / resolution;+ // A convex body is thickest where it faces the camera and thins to nothing at its silhouette.+ bool glass = glassState.x > 0.0;+ float indexOfRefraction = glass ? glassState.y : liquidOptics.y;+ float thickness = (glass ? glassState.z : liquidOptics.x) * mix(0.15, 1.0, facing);+ vec3 bent = refract(-viewDirection, normal, 1.0 / indexOfRefraction);+ vec4 projected = viewProjection * vec4(worldPosition + bent * thickness, 1.0);+ vec2 refracted = projected.w > 0.0 ? projected.xy / projected.w * 0.5 + 0.5 : screen;+ refracted = clamp(mix(screen, refracted, glass ? 1.0 : liquidOptics.z), vec2(0.001), vec2(0.999));+ vec3 behind = texture(sceneColor, refracted).rgb;+ // The material colour is what one metre of this liquid lets through.+ // For a PBR material the colour tints what passes through and the attenuation colour is what attenuationDistance metres let through.+ vec3 absorption = pow(clamp(glass ? glassTint : color, vec3(0.001), vec3(1.0)), vec3(glass ? thickness / glassState.w : thickness));+ vec3 transmittance = glass ? color * absorption : absorption;+ float surfaceReflectance = pow((indexOfRefraction - 1.0) / (indexOfRefraction + 1.0), 2.0);+ float reflectance = surfaceReflectance + (1.0 - surfaceReflectance) * pow(1.0 - facing, 5.0);+ vec3 inScatter = color * (ambientLight(normal) + keyColor * max(dot(normal, light), 0.0) * 0.2) * (glass ? 0.0 : liquidOptics.w) * (1.0 - transmittance);+ // The share of a PBR material that does not transmit is lit like a diffuse surface.+ vec3 diffuseShare = glass ? color * (1.0 - glassState.x) * (ambientLight(normal) + keyColor * max(dot(normal, light), 0.0) / PI) : vec3(0.0);+ vec3 reflection = environmentColor(reflect(-viewDirection, normal), roughness) * ambientIntensity;+ vec3 highlight = keyColor * specularLobe(normal, viewDirection, light, roughness);+ vec3 surfaceLight = (inScatter + diffuseShare) * (1.0 - reflectance) + reflection * reflectance + highlight;+ surfaceLight = mix(surfaceLight, fogColor, fog);+ float transmitted = glass ? glassState.x : 1.0;+ outputColor = vec4(behind * transmittance * transmitted * (1.0 - reflectance) * (1.0 - fog) + displayed(surfaceLight), 1.0);+}`;++registerShaderFeatureCode('liquid', { fragment: liquidFragmentShader, draw: drawLiquidPass });diff --git a/src/renderer.js b/src/renderer.jsindex 1eef0426..7a62f43c 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -1,5 +1,5 @@import { mat4, mat3 } from 'gl-matrix';-import { layerTextureSupport, viewTransformShader, LIGHT_SHADING_FEATURE, SHADING_MODES, LIGHT_SHADING_MAXIMUM_SAMPLES, vertexShader, createVertexShader, createFragmentShader, liquidFragmentShader, createDepthVertexShader, depthFragmentShader, benchmarkVertexShader, benchmarkFragmentShader } from './shaders.js';+import { SPLAT_UNITS, requireSplatSupport, layerTextureSupport, LIGHT_SHADING_FEATURE, SHADING_MODES, LIGHT_SHADING_MAXIMUM_SAMPLES, createVertexShader, createFragmentShader, liquidPassDrawer, depthFragmentShader, benchmarkVertexShader, benchmarkFragmentShader, viewTransformShader, createDepthVertexShader } from './shaders.js';import { postFragment, createPostFragment, POST_STEPS, skyFragment, bloomFragment } from './post.js';import { fullscreenVertex, startProgram, finishProgram, compileProgram } from './gl-program.js';import { ambientOcclusionFragment } from './ambient-occlusion.js';@@ -12,9 +12,8 @@ import { createLightingState, updateLightingState } from './lighting.js';import { RenderTargetLedger } from './render-target-ledger.js';import { SHADOW_ATLAS_WIDTH, SHADOW_TILE_SIZE, SHADOW_TILES_PER_ROW, tileViewport, writeShadowTile } from './local-shadows.js';import { resolveVolumeFog, volumeShadowAmbient } from './volumetric.js';-import { assertEnvironmentMap, createEnvironmentResources, environmentBackgroundFragment, environmentBrdfFragment, environmentPrefilterFragment, estimateEnvironmentBytes } from './environment.js';+import { requireEnvironmentSupport } from './environment-shading.js';import { validateSkinGeometry, validateSkinPose, skinTextureLayout, packSkinInfluences } from './skinning.js';-import { validateSplatMaterial, splatBatchState, splatTextureBytes, resolveSplatSize, packSplatParameters, buildSplatPixels } from './splat.js';import { materialLayerState, materialLayerValue } from './material-layers.js';import { MaterialSnapshot } from './material-snapshot.js';import { fittedFogDensity } from './fog-fit.js';@@ -127,10 +126,6 @@ const ENVIRONMENT_UNIFORMS = [...ENVIRONMENT_LOOKUP_UNIFORMS, 'environmentBrdf',const LIGHT_MAP_UNIT = 6;const ENVIRONMENT_SPECULAR_UNIT = 9;const ENVIRONMENT_BRDF_UNIT = 10;-const SPLAT_COLOR_UNIT = 11;-const SPLAT_NORMAL_UNIT = 12;-const SPLAT_PACKED_UNIT = 13;-const SPLAT_MASK_UNIT = 14;const LOCAL_SHADOW_UNIT = 15;const LIGHT_UNIFORMS = ['pointLightCount', 'pointLightPositions[0]', 'pointLightColors[0]', 'pointLightRadii[0]', 'pointLightShadowTiles[0]','spotLightCount', 'spotLightPositions[0]', 'spotLightColors[0]', 'spotLightDirections[0]', 'spotLightParameters[0]',@@ -175,9 +170,9 @@ function materialMapState(material) {validateMaterialMaps(material);const normalScale = material.normalScale ?? DEFAULT_NORMAL_SCALE;const occlusionStrength = material.occlusionStrength ?? 1;- validateSplatMaterial(material);+ if (material.layers || material.splat) requireSplatSupport().validate(material);if (material.layers && PROCEDURAL_MATERIAL_TAGS.has(material.tag)) throw new Error('Splat layers require a standard PBR material tag.');- return [...MATERIAL_MAPS.map(field => material[field] ?? null), normalScale[0], normalScale[1], occlusionStrength, ...mossState(material), material.boxMapScale ?? 0, material.boxMapAxis ?? 1, ...lightMapState(material), ...splatBatchState(material), material.layerMaps ?? null, material.environmentProbe ?? null, material.waterReflection ?? null];+ return [...MATERIAL_MAPS.map(field => material[field] ?? null), normalScale[0], normalScale[1], occlusionStrength, ...mossState(material), material.boxMapScale ?? 0, material.boxMapAxis ?? 1, ...lightMapState(material), material.layers ?? null, material.splat ?? null, material.layerMaps ?? null, material.environmentProbe ?? null, material.waterReflection ?? null];}// A baked light map reads the second UV set, so it is only drawn for parts that cannot be skinned or morphed.@@ -569,13 +564,13 @@ export class Renderer67 {const material = group[0].material;if (!material.layers || splatLayerSets.has(material.layers)) continue;splatLayerSets.add(material.layers);- this.splatBytes += splatTextureBytes(resolveSplatSize(material, textureLimit), material.layers.length) + (material.splat?.maskMap ? textureDescriptorInfo(material.splat.maskMap, textureLimit).bytes : 0);+ this.splatBytes += requireSplatSupport().textureBytes(requireSplatSupport().resolveSize(material, textureLimit), material.layers.length) + (material.splat?.maskMap ? textureDescriptorInfo(material.splat.maskMap, textureLimit).bytes : 0);}this.textureBytes += this.splatBytes;this.capabilities.maximumTextureSize = textureLimit;this.environmentResources = null;this.environmentBackgroundProgram = null;- this.environmentBytes = !benchmark && environment.map ? estimateEnvironmentBytes(environment.map, textureLimit) : 0;+ this.environmentBytes = !benchmark && environment.map ? requireEnvironmentSupport().estimateBytes(environment.map, textureLimit) : 0;this.textureBytes += this.environmentBytes;for (const group of groups) {const geometry = group[0].geometry;@@ -607,7 +602,6 @@ export class Renderer67 {pending.post = startProgram(this.context, fullscreenVertex, this.withViewTransform(postFragment, 'post'));pending.sky = startProgram(this.context, fullscreenVertex, skyFragment);pending.bloom = startProgram(this.context, fullscreenVertex, bloomFragment);- pending.liquid = startProgram(this.context, vertexShader, this.withViewTransform(liquidFragmentShader, 'liquid'));if (environment.map) this.startEnvironmentPrograms();}this.finishMainProgram(pending.main);@@ -622,8 +616,6 @@ export class Renderer67 {this.bloomProgram = finishProgram(this.context, pending.bloom);this.bloomUniforms = Object.fromEntries(['sourceTexture', 'texelDirection', 'threshold', 'extract'].map(name => [name, this.context.getUniformLocation(this.bloomProgram, name)]));this.skyUniforms = Object.fromEntries(['skyTop', 'skyBottom', 'sunDirection', 'inverseViewProjection', 'eye', 'time', 'daylight', 'hdrEnabled'].map((name) => [name, this.context.getUniformLocation(this.skyProgram, name)]));- this.liquidProgram = finishProgram(this.context, pending.liquid);- this.liquidUniforms = Object.fromEntries(['viewProjection', 'lightProjection', 'eye', 'sceneColor', 'resolution', 'exposure', 'hdrEnabled', 'worldMode', 'daylight', 'lightDirection', 'keyColor', 'ambientIntensity', 'fogColor', 'fogDensity', 'glassState', 'glassTint', 'liquidOptics', 'foamShape', ...ENVIRONMENT_LOOKUP_UNIFORMS, 'time', 'wind', 'explosionProgress', 'explosionCenter', 'assemblyAmount', ...SKIN_UNIFORMS, ...MORPH_UNIFORMS].map((name) => [name, this.context.getUniformLocation(this.liquidProgram, name)]));}this.depthAlphaUniforms = Object.fromEntries(DEPTH_ALPHA_UNIFORMS.map(name => [name, this.context.getUniformLocation(this.depthProgram, name)]));this.depthUniform = this.context.getUniformLocation(this.depthProgram, 'lightProjection');@@ -633,13 +625,12 @@ export class Renderer67 {this.triangles = parts.reduce((total, part) => total + part.geometry.indices.length / 3, 0);this.whiteTexture = this.createTexture(null);// Disabled skin samplers still need distinct units from the depth sampler.- const liquidPrograms = this.liquidProgram ? [[this.liquidProgram, this.liquidUniforms]] : [];- for (const [program, uniforms] of [[this.program, this.uniforms], [this.depthProgram, this.depthSkinUniforms], ...liquidPrograms]) {+ for (const [program, uniforms] of [[this.program, this.uniforms], [this.depthProgram, this.depthSkinUniforms]]) {this.context.useProgram(program);this.context.uniform1i(uniforms.skinInfluences, 6);this.context.uniform1i(uniforms.jointPalette, 7);}- for (const [program, uniforms] of [[this.program, this.uniforms], [this.depthProgram, this.depthMorphUniforms], ...liquidPrograms]) {+ for (const [program, uniforms] of [[this.program, this.uniforms], [this.depthProgram, this.depthMorphUniforms]]) {this.context.useProgram(program);this.context.uniform1i(uniforms.morphTargets, 8);}@@ -647,7 +638,7 @@ export class Renderer67 {this.context.activeTexture(this.context.TEXTURE0 + unit);this.context.bindTexture(this.context.TEXTURE_2D, this.whiteTexture);}- this.initializeSplatPlaceholders();+ if (this.shaderFeatures.has('splat')) requireSplatSupport().initializePlaceholders(this);this.initializeEnvironment(environment.map);groups.forEach((group) => this.batches.push(this.uploadBatch(group)));this.createShadowMap();@@ -678,14 +669,9 @@ export class Renderer67 {context.deleteProgram(this.postProgram);this.postProgram = finishProgram(context, startProgram(context, fullscreenVertex, this.withViewTransform(postFragment, 'post')));this.postUniforms = postUniformLocations(context, this.postProgram);- const liquidNames = Object.keys(this.liquidUniforms);- context.deleteProgram(this.liquidProgram);- this.liquidProgram = finishProgram(context, startProgram(context, vertexShader, this.withViewTransform(liquidFragmentShader, 'liquid')));- this.liquidUniforms = Object.fromEntries(liquidNames.map(name => [name, context.getUniformLocation(this.liquidProgram, name)]));- context.useProgram(this.liquidProgram);- context.uniform1i(this.liquidUniforms.skinInfluences, 6);- context.uniform1i(this.liquidUniforms.jointPalette, 7);- context.uniform1i(this.liquidUniforms.morphTargets, 8);+ // The liquid pass builds its program on its next draw, with the new curve (liquid-renderer.js).+ if (this.liquidProgram) context.deleteProgram(this.liquidProgram);+ this.liquidProgram = null;if (this.environmentBackgroundProgram) context.deleteProgram(this.environmentBackgroundProgram);this.environmentBackgroundProgram = null;}@@ -790,95 +776,6 @@ export class Renderer67 {return texture;}- initializeSplatPlaceholders() {- const context = this.context;- this.splatPlaceholder = context.createTexture();- context.bindTexture(context.TEXTURE_2D_ARRAY, this.splatPlaceholder);- context.texImage3D(context.TEXTURE_2D_ARRAY, 0, context.RGBA8, 1, 1, 1, 0, context.RGBA, context.UNSIGNED_BYTE, new Uint8Array([128, 128, 128, 255]));- context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MIN_FILTER, context.LINEAR);- context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MAG_FILTER, context.LINEAR);- context.useProgram(this.program);- const units = [['splatColor', SPLAT_COLOR_UNIT], ['splatNormal', SPLAT_NORMAL_UNIT], ['splatPacked', SPLAT_PACKED_UNIT]];- for (const [name, unit] of units) {- context.uniform1i(this.uniforms[name], unit);- context.activeTexture(context.TEXTURE0 + unit);- context.bindTexture(context.TEXTURE_2D_ARRAY, this.splatPlaceholder);- }- context.uniform1i(this.uniforms.splatMask, SPLAT_MASK_UNIT);- context.activeTexture(context.TEXTURE0 + SPLAT_MASK_UNIT);- context.bindTexture(context.TEXTURE_2D, this.whiteTexture);- context.uniform1f(this.uniforms.splatEnabled, 0);- }-- createTextureArray(internalFormat, size, layerCount, pixels) {- const context = this.context;- const texture = context.createTexture();- context.bindTexture(context.TEXTURE_2D_ARRAY, texture);- const levels = Math.floor(Math.log2(size)) + 1;- context.texStorage3D(context.TEXTURE_2D_ARRAY, levels, internalFormat, size, size, layerCount);- context.pixelStorei(context.UNPACK_FLIP_Y_WEBGL, false);- context.pixelStorei(context.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);- context.pixelStorei(context.UNPACK_COLORSPACE_CONVERSION_WEBGL, context.NONE);- context.texSubImage3D(context.TEXTURE_2D_ARRAY, 0, 0, 0, 0, size, size, layerCount, context.RGBA, context.UNSIGNED_BYTE, pixels);- context.generateMipmap(context.TEXTURE_2D_ARRAY);- context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_WRAP_S, context.REPEAT);- context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_WRAP_T, context.REPEAT);- context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MIN_FILTER, context.LINEAR_MIPMAP_LINEAR);- context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MAG_FILTER, context.LINEAR);- this.applyAnisotropy(context.TEXTURE_2D_ARRAY);- return texture;- }-- createSplatResources(material) {- if (this.splatResources.has(material.layers)) return this.splatResources.get(material.layers);- const context = this.context;- const size = resolveSplatSize(material, context.getParameter(context.MAX_TEXTURE_SIZE));- const layerCount = material.layers.length;- const pixels = buildSplatPixels(material.layers, size);- const resources = {- size,- layerCount,- color: this.createTextureArray(context.SRGB8_ALPHA8, size, layerCount, pixels.color),- normal: this.createTextureArray(context.RGBA8, size, layerCount, pixels.normal),- packed: this.createTextureArray(context.RGBA8, size, layerCount, pixels.packed),- mask: material.splat?.maskMap ? this.createTexture(material.splat.maskMap) : this.whiteTexture,- parameters: packSplatParameters(material),- };- this.splatResources.set(material.layers, resources);- return resources;- }-- bindSplat(splat) {- const context = this.context;- const uniforms = this.uniforms;- context.uniform1f(uniforms.splatEnabled, splat ? 1 : 0);- if (!splat) return;- const bindings = [[SPLAT_COLOR_UNIT, context.TEXTURE_2D_ARRAY, splat.color], [SPLAT_NORMAL_UNIT, context.TEXTURE_2D_ARRAY, splat.normal], [SPLAT_PACKED_UNIT, context.TEXTURE_2D_ARRAY, splat.packed], [SPLAT_MASK_UNIT, context.TEXTURE_2D, splat.mask]];- for (const [unit, target, texture] of bindings) {- context.activeTexture(context.TEXTURE0 + unit);- context.bindTexture(target, texture);- }- const parameters = splat.parameters;- context.uniform4fv(uniforms['splatLayer[0]'], parameters.layer);- context.uniform4fv(uniforms['splatTint[0]'], parameters.tint);- context.uniform4fv(uniforms['splatSlope[0]'], parameters.slope);- context.uniform4fv(uniforms['splatHeightRange[0]'], parameters.height);- context.uniform1fv(uniforms['splatMaskChannel[0]'], parameters.mask);- context.uniform1fv(uniforms['splatMaskInvert[0]'], parameters.maskInvert);- context.uniform4fv(uniforms.splatBlend, parameters.blend);- context.uniform4fv(uniforms.splatOptions, parameters.options);- context.uniform4fv(uniforms.splatMaskBounds, parameters.maskBounds);- context.uniform4fv(uniforms.splatVariation, parameters.variation);- }-- disposeSplatResources() {- for (const resources of this.splatResources?.values() ?? []) {- for (const texture of [resources.color, resources.normal, resources.packed]) this.context.deleteTexture(texture);- }- this.splatResources?.clear();- if (this.splatPlaceholder) this.context.deleteTexture(this.splatPlaceholder);- }-// Parts can arrive after construction (replaceParts) or change their tag (updateParts). When one needs a shader feature// that the program left out, the program is compiled again with that feature added.requireShaderFeatures(parts) {@@ -899,10 +796,11 @@ export class Renderer67 {this.buildMainProgram();context.deleteProgram(previousProgram);context.useProgram(this.program);- for (const [name, unit] of [['skinInfluences', 6], ['jointPalette', 7], ['morphTargets', 8], ['splatColor', SPLAT_COLOR_UNIT], ['splatNormal', SPLAT_NORMAL_UNIT], ['splatPacked', SPLAT_PACKED_UNIT], ['splatMask', SPLAT_MASK_UNIT], ['environmentSpecular', ENVIRONMENT_SPECULAR_UNIT], ['environmentBrdf', ENVIRONMENT_BRDF_UNIT]]) {+ for (const [name, unit] of [['skinInfluences', 6], ['jointPalette', 7], ['morphTargets', 8], ...Object.entries(SPLAT_UNITS), ['environmentSpecular', ENVIRONMENT_SPECULAR_UNIT], ['environmentBrdf', ENVIRONMENT_BRDF_UNIT]]) {context.uniform1i(this.uniforms[name], unit);}context.uniform1f(this.uniforms.splatEnabled, 0);+ if (this.shaderFeatures.has('splat') && !this.splatPlaceholder) requireSplatSupport().initializePlaceholders(this);}uploadBatch(parts) {@@ -1082,7 +980,7 @@ export class Renderer67 {const map = parts[0].material.map ?? null;const mapState = materialMapState(parts[0].material);const textures = mapState.slice(0, 5).map(descriptor => this.createTexture(descriptor));- const splat = parts[0].material.layers ? this.createSplatResources(parts[0].material) : null;+ const splat = parts[0].material.layers ? requireSplatSupport().createResources(this, parts[0].material, requireSplatSupport()) : null;const lightMapTexture = mapState[14] ? this.createTexture(mapState[14]) : null;if (lightMapTexture && (parts[0].skin || parts[0].morph || !geometry.lightUvs)) throw new Error('A light map needs lightUvs on rigid geometry, with no skin or morph.');const skin = parts[0].skin ?? null;@@ -1660,7 +1558,7 @@ export class Renderer67 {// The prefilter and BRDF programs build while the other programs do, instead of after them.startEnvironmentPrograms() {this.pendingEnvironmentPrograms = new Map();- for (const fragmentSource of [environmentPrefilterFragment, environmentBrdfFragment]) {+ for (const fragmentSource of [requireEnvironmentSupport().prefilterFragment, requireEnvironmentSupport().brdfFragment]) {this.pendingEnvironmentPrograms.set(fragmentSource, startProgram(this.context, fullscreenVertex, fragmentSource));}}@@ -1695,10 +1593,10 @@ export class Renderer67 {// Prefilters a map the first time it is seen; maps spread from one created map share its data and reuse the GPU textures (an `irradianceSource` map, which only the diffuse light reads, counts as part of the map). Returns true when new textures were built.prepareEnvironment(map) {- assertEnvironmentMap(map);+ requireEnvironmentSupport().assertMap(map);if (this.environmentResources?.data === map.data && this.environmentResources.irradianceSource === map.irradianceSource) return false;- estimateEnvironmentBytes(map, this.capabilities.maximumTextureSize);- const resources = createEnvironmentResources(this.context, map, { compileProgram: (context, vertexSource, fragmentSource) => this.compileEnvironmentProgram(context, vertexSource, fragmentSource), fullscreenVertex });+ requireEnvironmentSupport().estimateBytes(map, this.capabilities.maximumTextureSize);+ const resources = requireEnvironmentSupport().createResources(this.context, map, { compileProgram: (context, vertexSource, fragmentSource) => this.compileEnvironmentProgram(context, vertexSource, fragmentSource), fullscreenVertex });this.environmentResources?.dispose();this.environmentResources = resources;this.textureBytes += resources.bytes - this.environmentBytes;@@ -1732,7 +1630,7 @@ export class Renderer67 {if (!map?.showBackground || this.transparent) return false;const context = this.context;if (!this.environmentBackgroundProgram) {- this.environmentBackgroundProgram = compileProgram(context, fullscreenVertex, this.withViewTransform(environmentBackgroundFragment, 'background'));+ this.environmentBackgroundProgram = compileProgram(context, fullscreenVertex, this.withViewTransform(requireEnvironmentSupport().backgroundFragment, 'background'));const names = ['inverseViewProjection', 'eye', 'backgroundLevel', 'exposure', 'hdrEnabled', 'environmentSpecular', 'environmentRotation', 'environmentIntensity', 'environmentLevelMax'];this.environmentBackgroundUniforms = Object.fromEntries(names.map(name => [name, context.getUniformLocation(this.environmentBackgroundProgram, name)]));}@@ -1895,64 +1793,7 @@ export class Renderer67 {const foams = this.batches.filter(batch => batchPass(batch) === 'foam');if (liquids.length === 0 && foams.length === 0) return;const optics = resolveLiquidOptics({ ...environment.liquid, ...frame.liquid });- const context = this.context;- const width = this.canvas.width;- const height = this.canvas.height;- // Liquids refract the opaque scene, so resolve it into frameTexture before drawing them on top.- context.bindFramebuffer(context.READ_FRAMEBUFFER, this.multisampleFramebuffer);- context.bindFramebuffer(context.DRAW_FRAMEBUFFER, this.resolveFramebuffer);- context.blitFramebuffer(0, 0, width, height, 0, 0, width, height, context.COLOR_BUFFER_BIT, context.NEAREST);- context.bindFramebuffer(context.FRAMEBUFFER, this.multisampleFramebuffer);- context.viewport(0, 0, width, height);- context.enable(context.DEPTH_TEST);- context.enable(context.CULL_FACE);- context.useProgram(this.liquidProgram);- const uniforms = this.liquidUniforms;- context.uniformMatrix4fv(uniforms.viewProjection, false, this.viewProjection);- context.uniformMatrix4fv(uniforms.lightProjection, false, this.lightProjection);- context.uniform3fv(uniforms.eye, frame.eye);- context.uniform2f(uniforms.resolution, width, height);- context.uniform1f(uniforms.exposure, frame.exposure ?? 1);- context.uniform1f(uniforms.hdrEnabled, this.hdr ? 1 : 0);- context.uniform1f(uniforms.worldMode, this.world ? 1 : 0);- context.uniform1f(uniforms.daylight, environment.daylight);- context.uniform3fv(uniforms.lightDirection, environment.lightDirection);- context.uniform3fv(uniforms.keyColor, this.lighting.keyColor);- context.uniform1f(uniforms.ambientIntensity, this.lighting.ambientIntensity);- context.uniform3fv(uniforms.fogColor, environment.fogColor);- context.uniform1f(uniforms.fogDensity, environment.fogDensity);- context.uniform4f(uniforms.liquidOptics, optics.thickness, optics.ior, optics.refraction, optics.scattering);- context.uniform2f(uniforms.foamShape, optics.foamOpacity, optics.foamSoftness);- this.applyEnvironmentUniforms(environmentMap, uniforms);- context.uniform1f(uniforms.time, frame.time ?? 0);- context.uniform1f(uniforms.wind, frame.wind ?? 0.3);- context.uniform1f(uniforms.explosionProgress, frame.explosionProgress ?? 1);- context.uniform3fv(uniforms.explosionCenter, environment.explosionCenter);- context.uniform1f(uniforms.assemblyAmount, assemblyAmount);- if (uniforms.skinEnabled) context.uniform1f(uniforms.skinEnabled, 0);- if (uniforms.morphEnabled) context.uniform1f(uniforms.morphEnabled, 0);- context.activeTexture(context.TEXTURE0);- context.bindTexture(context.TEXTURE_2D, this.frameTexture);- context.uniform1i(uniforms.sceneColor, 0);- for (const batch of liquids) {- const layers = batch.layerState;- context.uniform4f(uniforms.glassState, layers[9], layers[10], layers[11], Math.min(layers[12], MAX_FINITE_DISTANCE));- context.uniform3f(uniforms.glassTint, layers[13], layers[14], layers[15]);- context.bindVertexArray(batch.vertexArray);- context.drawElementsInstanced(context.TRIANGLES, batch.indexCount, context.UNSIGNED_INT, 0, batch.instanceCount);- }- if (foams.length > 0) {- context.enable(context.BLEND);- context.blendFuncSeparate(context.SRC_ALPHA, context.ONE_MINUS_SRC_ALPHA, context.ONE, context.ONE_MINUS_SRC_ALPHA);- context.depthMask(false);- for (const batch of foams) {- context.bindVertexArray(batch.vertexArray);- context.drawElementsInstanced(context.TRIANGLES, batch.indexCount, context.UNSIGNED_INT, 0, batch.instanceCount);- }- context.depthMask(true);- context.disable(context.BLEND);- }- context.bindVertexArray(null);+ liquidPassDrawer()(this, { frame, environment, assemblyAmount, environmentMap, liquids, foams, optics, environmentSpecularUnit: ENVIRONMENT_SPECULAR_UNIT, maximumDistance: MAX_FINITE_DISTANCE });}// Allocated on the first frame that asks for bloom, so scenes without it pay nothing.@@ -2158,7 +1999,7 @@ export class Renderer67 {this.context.uniform1f(this.uniforms.materialToon, layers[22]);this.context.uniform1f(this.uniforms.boxMapScale, batch.mapState[12]);this.context.uniform1i(this.uniforms.boxMapAxis, batch.mapState[13]);- this.bindSplat(batch.splat);+ if (this.shaderFeatures?.has('splat')) requireSplatSupport().bind(this, batch.splat);layerTextureSupport.bind?.(this, batch.mapState);this.context.uniform1f(this.uniforms.hasLightMap, batch.lightMapTexture ? 1 : 0);if (batch.lightMapTexture) {@@ -2382,7 +2223,7 @@ export class Renderer67 {if (this.environmentBackgroundProgram) this.context.deleteProgram(this.environmentBackgroundProgram);this.environmentResources?.dispose();for (const texture of this.textures.values()) this.context.deleteTexture(texture);- this.disposeSplatResources();+ if (this.splatPlaceholder) requireSplatSupport().dispose(this);this.textures.clear();for (const cache of [this.skinTextures, this.influenceTextures, this.morphTextures]) {for (const record of cache?.values() ?? []) this.context.deleteTexture(record.texture);diff --git a/src/shaders.js b/src/shaders.jsindex de3d3c08..fa244cc8 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -1,7 +1,6 @@import { MAX_MORPH_TARGETS } from './morph.js';import { MAX_POINT_LIGHTS, MAX_SPOT_LIGHTS, MAX_AREA_LIGHTS } from './light-limits.js';-import { environmentLookupGlsl, environmentShadingGlsl } from './environment.js';-import { splatGlsl } from './splat-shader.js';+import { environmentLookupGlsl, environmentShadingGlsl } from './environment-shading.js';const morphingShader = `uniform float morphEnabled;@@ -244,135 +243,6 @@ out vec4 outputColor;void main() { outputColor = vec4(color, 1.0); }`;-// Liquid and foam are drawn after the opaque scene, which is resolved into sceneColor first.-// Liquid: refraction through the scene, Beer-Lambert absorption over a convex-body thickness-// estimate, in-scattering, Fresnel environment reflection and a GGX key highlight.-// Foam: soft blended droplets whose alpha falls off toward their silhouettes.-export const liquidFragmentShader = `#version 300 es-precision highp float;-in vec3 worldPosition;-in vec3 worldNormal;-flat in vec3 baseColor;-flat in vec4 surface;-in vec4 shadowPosition;-in vec2 textureUV;-in vec3 vertexTint;-flat in vec3 emissionColor;-uniform vec3 eye;-uniform mat4 viewProjection;-uniform sampler2D sceneColor;-uniform vec2 resolution;-uniform float exposure;-uniform float hdrEnabled;-uniform float worldMode;-uniform float daylight;-uniform vec3 lightDirection;-uniform vec3 keyColor;-uniform float ambientIntensity;-uniform vec3 fogColor;-uniform float fogDensity;-// x: thickness in metres, y: index of refraction, z: refraction strength, w: scattering-uniform vec4 liquidOptics;-// A transmissive PBR material: x: transmission, y: index of refraction, z: thickness in metres, w: attenuation distance-uniform vec4 glassState;-uniform vec3 glassTint;-// x: foam opacity, y: foam edge softness-uniform vec2 foamShape;-out vec4 outputColor;-const float PI = 3.14159265359;--vec3 toneMap(vec3 color) {- return clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), 0.0, 1.0);-}--${environmentLookupGlsl}-uniform float environmentEnabled;--vec3 environmentColor(vec3 direction, float roughness) {- if (environmentEnabled > 0.5) return environmentRadiance(direction, roughness * environmentLevelMax);- if (worldMode > 0.5) {- vec3 zenith = vec3(0.20, 0.38, 0.52) * max(daylight, 0.08);- return mix(fogColor * 0.75, zenith, smoothstep(-0.15, 0.7, direction.y));- }- vec3 color = mix(vec3(0.025, 0.035, 0.045), vec3(0.22, 0.25, 0.27), smoothstep(-0.3, 0.8, direction.y));- float blur = roughness * 0.35;- float key = smoothstep(0.80 - blur, 0.87, dot(direction, normalize(vec3(-3.0, 5.0, 4.0))));- float rim = smoothstep(0.90 - blur, 0.97, dot(direction, normalize(vec3(4.0, 2.0, -3.0))));- float strip = exp(-pow((direction.x + 0.6) / (0.035 + blur), 2.0)) * smoothstep(-0.1, 0.6, direction.y);- return color + key * vec3(2.8, 2.2, 1.6) + rim * vec3(0.5, 1.6, 1.5) + strip * vec3(0.7);-}--float specularLobe(vec3 normal, vec3 viewDirection, vec3 light, float roughness) {- float normalLight = max(dot(normal, light), 0.0);- if (normalLight == 0.0) return 0.0;- vec3 halfway = normalize(viewDirection + light);- float normalView = max(dot(normal, viewDirection), 0.001);- float normalHalf = max(dot(normal, halfway), 0.0);- float alpha = roughness * roughness;- float denominator = normalHalf * normalHalf * (alpha * alpha - 1.0) + 1.0;- float distribution = alpha * alpha / max(PI * denominator * denominator, 0.00001);- float masking = pow(roughness + 1.0, 2.0) / 8.0;- float geometry = normalView / (normalView * (1.0 - masking) + masking) * normalLight / (normalLight * (1.0 - masking) + masking);- return distribution * geometry / max(4.0 * normalView * normalLight, 0.001) * normalLight;-}--vec3 ambientLight(vec3 normal) {- vec3 studioAmbient = vec3(0.16, 0.20, 0.22) * (0.45 + 0.55 * max(normal.y, 0.0));- vec3 worldAmbient = vec3(0.19, 0.25, 0.30) * max(daylight, 0.08) * (0.45 + 0.55 * max(normal.y, 0.0));- return mix(studioAmbient, worldAmbient, worldMode) * ambientIntensity;-}--vec3 displayed(vec3 radiance) {- return hdrEnabled > 0.5 ? clamp(radiance * exposure, vec3(0), vec3(65504.0)) : pow(toneMap(radiance * exposure), vec3(1.0 / 2.2));-}--void main() {- vec3 normal = normalize(worldNormal);- if (!gl_FrontFacing) normal = -normal;- vec3 viewDirection = normalize(eye - worldPosition);- float facing = max(dot(normal, viewDirection), 0.0);- vec3 light = normalize(lightDirection);- vec3 color = baseColor * vertexTint;- float fog = (1.0 - exp(-length(worldPosition - eye) * fogDensity)) * worldMode;-- if (surface.w == 12.0) {- float alpha = foamShape.x * smoothstep(0.0, max(foamShape.y, 0.001), facing);- if (alpha < 0.004) discard;- float wrapped = max(dot(normal, light) * 0.5 + 0.5, 0.0);- vec3 radiance = color * (ambientLight(normal) * 1.6 + keyColor * wrapped * 0.32);- radiance = mix(radiance, fogColor, fog);- outputColor = vec4(displayed(radiance), alpha);- return;- }-- float roughness = clamp(surface.y, 0.02, 1.0);- vec2 screen = gl_FragCoord.xy / resolution;- // A convex body is thickest where it faces the camera and thins to nothing at its silhouette.- bool glass = glassState.x > 0.0;- float indexOfRefraction = glass ? glassState.y : liquidOptics.y;- float thickness = (glass ? glassState.z : liquidOptics.x) * mix(0.15, 1.0, facing);- vec3 bent = refract(-viewDirection, normal, 1.0 / indexOfRefraction);- vec4 projected = viewProjection * vec4(worldPosition + bent * thickness, 1.0);- vec2 refracted = projected.w > 0.0 ? projected.xy / projected.w * 0.5 + 0.5 : screen;- refracted = clamp(mix(screen, refracted, glass ? 1.0 : liquidOptics.z), vec2(0.001), vec2(0.999));- vec3 behind = texture(sceneColor, refracted).rgb;- // The material colour is what one metre of this liquid lets through.- // For a PBR material the colour tints what passes through and the attenuation colour is what attenuationDistance metres let through.- vec3 absorption = pow(clamp(glass ? glassTint : color, vec3(0.001), vec3(1.0)), vec3(glass ? thickness / glassState.w : thickness));- vec3 transmittance = glass ? color * absorption : absorption;- float surfaceReflectance = pow((indexOfRefraction - 1.0) / (indexOfRefraction + 1.0), 2.0);- float reflectance = surfaceReflectance + (1.0 - surfaceReflectance) * pow(1.0 - facing, 5.0);- vec3 inScatter = color * (ambientLight(normal) + keyColor * max(dot(normal, light), 0.0) * 0.2) * (glass ? 0.0 : liquidOptics.w) * (1.0 - transmittance);- // The share of a PBR material that does not transmit is lit like a diffuse surface.- vec3 diffuseShare = glass ? color * (1.0 - glassState.x) * (ambientLight(normal) + keyColor * max(dot(normal, light), 0.0) / PI) : vec3(0.0);- vec3 reflection = environmentColor(reflect(-viewDirection, normal), roughness) * ambientIntensity;- vec3 highlight = keyColor * specularLobe(normal, viewDirection, light, roughness);- vec3 surfaceLight = (inScatter + diffuseShare) * (1.0 - reflectance) + reflection * reflectance + highlight;- surfaceLight = mix(surfaceLight, fogColor, fog);- float transmitted = glass ? glassState.x : 1.0;- outputColor = vec4(behind * transmittance * transmitted * (1.0 - reflectance) * (1.0 - fog) + displayed(surfaceLight), 1.0);-}`;-function buildFragmentShader(featureDefines, featureCode) {return `#version 300 es${featureDefines}@@ -663,7 +533,7 @@ vec3 mappedNormal(vec3 normal) {return dot(mapped, mapped) > 1e-12 ? normalize(mapped) : normal;}-${splatGlsl}+${featureCode("libraries")}// Moss is its own material over the base, not a tint: fully rough, a soft sheen at grazing angles and light that wraps// through it. Two masks, as in offline moss shading: a sharp one decides where moss is shaded, and a blurred one lifts@@ -1040,26 +910,75 @@ export const layerTextureSupport = {};// view-transform-shader.js sets apply(source, kind, viewTransform, hasLook), bindLook(renderer, program) and dispose(renderer).export const viewTransformShader = {};+// splat-shader.js sets the JavaScript half of the splat ground material: validate, textureBytes, resolveSize,+// packParameters and buildPixels. The renderer calls them only for a material with layers.+const splatSupport = {};++/** The texture units of the splat samplers. */+export const SPLAT_UNITS = Object.freeze({ splatColor: 11, splatNormal: 12, splatPacked: 13, splatMask: 14 });++export function registerSplatSupport(functions) {+ Object.assign(splatSupport, functions);+}++export function requireSplatSupport() {+ if (!splatSupport.validate) {+ throw new Error("Splat layers need their code: add import '@codepawlhq/67js/addons/splat-shader'.");+ }+ return splatSupport;+}/** Register the GLSL of an optional feature. Called by the module that holds it, on import. */export function registerShaderFeatureCode(feature, slots) {registeredFeatureCode.set(feature, slots);}+// Features that main still names, because a macro guards the call, but whose GLSL is in an addon module. A renderer+// without the feature gets the stub, which keeps the program compiling; a renderer with it needs the module imported.+const MODULE_FEATURES = {+ splat: {+ stub: { libraries: 'void applySplat(inout vec3 color, inout vec3 normal, inout float roughness, inout float cavity) {}\n' },+ },+};++// The addon that registers each feature's code; a feature not listed here is registered by material-layer-shaders.+const FEATURE_ADDONS = { splat: 'splat-shader', liquid: 'liquid-shader' };+function requireFeatureCode(feature) {const slots = registeredFeatureCode.get(feature);if (!slots) {- throw new Error(`The ${feature} layer needs its shader code: add import '@codepawlhq/67js/addons/material-layer-shaders' (a recipe: await loadAgentSceneModules(recipe)).`);+ const addon = FEATURE_ADDONS[feature] ?? 'material-layer-shaders';+ throw new Error(`The ${feature} feature needs its shader code: add import '@codepawlhq/67js/addons/${addon}' (a recipe: await loadAgentSceneModules(recipe)).`);}return slots;}+/** The fragment shader of the liquid and foam pass. It throws until liquid-shader.js is imported. */+export function liquidFragmentSource() {+ return requireFeatureCode('liquid').fragment;+}++/** The function that draws the liquid and foam pass for a renderer. It throws until liquid-shader.js is imported. */+export function liquidPassDrawer() {+ return requireFeatureCode('liquid').draw;+}++function hasSeparateCode(feature) {+ if (feature in MODULE_FEATURES) return true;+ if (feature in SHADER_FEATURE_CONDITIONS || feature === LIGHT_SHADING_FEATURE) return false;+ return !ENVIRONMENT_FEATURES.includes(feature) && !VERTEX_ONLY_FEATURES.includes(feature);+}+function featureCodeSplicer(features) {- const codes = features.filter(feature => !(feature in SHADER_FEATURE_CONDITIONS) && !ENVIRONMENT_FEATURES.includes(feature) && !VERTEX_ONLY_FEATURES.includes(feature) && feature !== LIGHT_SHADING_FEATURE).map(requireFeatureCode);+ const enabled = new Set(features);+ const codes = features.filter(hasSeparateCode).map(requireFeatureCode);+ for (const [feature, { stub }] of Object.entries(MODULE_FEATURES)) {+ if (!enabled.has(feature)) codes.push(stub);+ }return slot => codes.map(code => code[slot] ?? '').join('');}-export const SHADER_FEATURES = Object.freeze([...Object.keys(SHADER_FEATURE_CONDITIONS), ...ENVIRONMENT_FEATURES, ...VERTEX_ONLY_FEATURES]);+export const SHADER_FEATURES = Object.freeze([...Object.keys(SHADER_FEATURE_CONDITIONS), ...ENVIRONMENT_FEATURES, ...VERTEX_ONLY_FEATURES].filter(feature => !(feature in MODULE_FEATURES)));// The vertex shader pays on every vertex for skinning, morphing, wind, waves and the explosion, and SwiftShader runs a// choice between them whether or not the uniform picks it, so a renderer without those parts compiles them out.diff --git a/src/splat-renderer.js b/src/splat-renderer.jsnew file mode 100644index 00000000..7c4f6770--- /dev/null+++ b/src/splat-renderer.js@@ -0,0 +1,97 @@+import { SPLAT_UNITS } from './shaders.js';++// The renderer's splat ground code, kept apart from renderer.js so a scene without splat layers does not carry it.+// splat-shader.js registers these functions; each takes the renderer as its first argument.++function createTextureArray(renderer, internalFormat, size, layerCount, pixels) {+ const context = renderer.context;+ const texture = context.createTexture();+ context.bindTexture(context.TEXTURE_2D_ARRAY, texture);+ const levels = Math.floor(Math.log2(size)) + 1;+ context.texStorage3D(context.TEXTURE_2D_ARRAY, levels, internalFormat, size, size, layerCount);+ context.pixelStorei(context.UNPACK_FLIP_Y_WEBGL, false);+ context.pixelStorei(context.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);+ context.pixelStorei(context.UNPACK_COLORSPACE_CONVERSION_WEBGL, context.NONE);+ context.texSubImage3D(context.TEXTURE_2D_ARRAY, 0, 0, 0, 0, size, size, layerCount, context.RGBA, context.UNSIGNED_BYTE, pixels);+ context.generateMipmap(context.TEXTURE_2D_ARRAY);+ context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_WRAP_S, context.REPEAT);+ context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_WRAP_T, context.REPEAT);+ context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MIN_FILTER, context.LINEAR_MIPMAP_LINEAR);+ context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MAG_FILTER, context.LINEAR);+ renderer.applyAnisotropy(context.TEXTURE_2D_ARRAY);+ return texture;+}++export function initializeSplatPlaceholders(renderer) {+ const context = renderer.context;+ renderer.splatPlaceholder = context.createTexture();+ context.bindTexture(context.TEXTURE_2D_ARRAY, renderer.splatPlaceholder);+ context.texImage3D(context.TEXTURE_2D_ARRAY, 0, context.RGBA8, 1, 1, 1, 0, context.RGBA, context.UNSIGNED_BYTE, new Uint8Array([128, 128, 128, 255]));+ context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MIN_FILTER, context.LINEAR);+ context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MAG_FILTER, context.LINEAR);+ context.useProgram(renderer.program);+ for (const name of ['splatColor', 'splatNormal', 'splatPacked']) {+ context.uniform1i(renderer.uniforms[name], SPLAT_UNITS[name]);+ context.activeTexture(context.TEXTURE0 + SPLAT_UNITS[name]);+ context.bindTexture(context.TEXTURE_2D_ARRAY, renderer.splatPlaceholder);+ }+ context.uniform1i(renderer.uniforms.splatMask, SPLAT_UNITS.splatMask);+ context.activeTexture(context.TEXTURE0 + SPLAT_UNITS.splatMask);+ context.bindTexture(context.TEXTURE_2D, renderer.whiteTexture);+ context.uniform1f(renderer.uniforms.splatEnabled, 0);+}++export function createSplatResources(renderer, material, support) {+ if (renderer.splatResources.has(material.layers)) return renderer.splatResources.get(material.layers);+ const context = renderer.context;+ const size = support.resolveSize(material, context.getParameter(context.MAX_TEXTURE_SIZE));+ const layerCount = material.layers.length;+ const pixels = support.buildPixels(material.layers, size);+ const resources = {+ size,+ layerCount,+ color: createTextureArray(renderer, context.SRGB8_ALPHA8, size, layerCount, pixels.color),+ normal: createTextureArray(renderer, context.RGBA8, size, layerCount, pixels.normal),+ packed: createTextureArray(renderer, context.RGBA8, size, layerCount, pixels.packed),+ mask: material.splat?.maskMap ? renderer.createTexture(material.splat.maskMap) : renderer.whiteTexture,+ parameters: support.packParameters(material),+ };+ renderer.splatResources.set(material.layers, resources);+ return resources;+}++export function bindSplat(renderer, splat) {+ const context = renderer.context;+ const uniforms = renderer.uniforms;+ context.uniform1f(uniforms.splatEnabled, splat ? 1 : 0);+ if (!splat) return;+ const bindings = [+ [SPLAT_UNITS.splatColor, context.TEXTURE_2D_ARRAY, splat.color],+ [SPLAT_UNITS.splatNormal, context.TEXTURE_2D_ARRAY, splat.normal],+ [SPLAT_UNITS.splatPacked, context.TEXTURE_2D_ARRAY, splat.packed],+ [SPLAT_UNITS.splatMask, context.TEXTURE_2D, splat.mask],+ ];+ for (const [unit, target, texture] of bindings) {+ context.activeTexture(context.TEXTURE0 + unit);+ context.bindTexture(target, texture);+ }+ const parameters = splat.parameters;+ context.uniform4fv(uniforms['splatLayer[0]'], parameters.layer);+ context.uniform4fv(uniforms['splatTint[0]'], parameters.tint);+ context.uniform4fv(uniforms['splatSlope[0]'], parameters.slope);+ context.uniform4fv(uniforms['splatHeightRange[0]'], parameters.height);+ context.uniform1fv(uniforms['splatMaskChannel[0]'], parameters.mask);+ context.uniform1fv(uniforms['splatMaskInvert[0]'], parameters.maskInvert);+ context.uniform4fv(uniforms.splatBlend, parameters.blend);+ context.uniform4fv(uniforms.splatOptions, parameters.options);+ context.uniform4fv(uniforms.splatMaskBounds, parameters.maskBounds);+ context.uniform4fv(uniforms.splatVariation, parameters.variation);+}++export function disposeSplatResources(renderer) {+ for (const resources of renderer.splatResources.values()) {+ for (const texture of [resources.color, resources.normal, resources.packed]) renderer.context.deleteTexture(texture);+ }+ renderer.splatResources.clear();+ if (renderer.splatPlaceholder) renderer.context.deleteTexture(renderer.splatPlaceholder);+}diff --git a/src/splat-shader.js b/src/splat-shader.jsindex 2dd52332..5e2980e4 100644--- a/src/splat-shader.js+++ b/src/splat-shader.js@@ -1,4 +1,9 @@+import { registerShaderFeatureCode, registerSplatSupport } from './shaders.js';+import { initializeSplatPlaceholders, createSplatResources, bindSplat, disposeSplatResources } from './splat-renderer.js';+import { validateSplatMaterial, splatTextureBytes, resolveSplatSize, packSplatParameters, buildSplatPixels } from './splat.js';+// Fragment shader chunk for the splat ground material. It is spliced into the main fragment shader after terrainNoise.+// Importing this module registers it: a renderer with splat layers needs it, and any other renderer does not carry it.// Texture arrays: unit 11 color (sRGB), unit 12 normal (linear), unit 13 roughness (red) and height (green), unit 14 weight mask.export const splatGlsl = `uniform float splatEnabled;@@ -264,3 +269,16 @@ void applySplat(inout vec3 color, inout vec3 normal, inout float roughness, inoucavity = mix(1.0, mix(0.55, 1.0, smoothstep(0.1, 0.85, blendedHeight)), splatOptions.w);}`;++registerShaderFeatureCode('splat', { libraries: splatGlsl });+registerSplatSupport({+ validate: validateSplatMaterial,+ textureBytes: splatTextureBytes,+ resolveSize: resolveSplatSize,+ packParameters: packSplatParameters,+ buildPixels: buildSplatPixels,+ initializePlaceholders: initializeSplatPlaceholders,+ createResources: createSplatResources,+ bind: bindSplat,+ dispose: disposeSplatResources,+});diff --git a/src/spume.js b/src/spume.jsindex a41269ff..fb23a23b 100644--- a/src/spume.js+++ b/src/spume.js@@ -1,4 +1,5 @@import { mat4 } from 'gl-matrix';+import './liquid-shader.js';// Plays back a liquid bake from Spume (https://github.com/codepawl/spume).// Spume bakes offline on an NVIDIA GPU; this addon only reads its cache folder:diff --git a/tests/material-layer-shaders.test.js b/tests/material-layer-shaders.test.jsindex 93ac51c0..e5fd84ec 100644--- a/tests/material-layer-shaders.test.js+++ b/tests/material-layer-shaders.test.js@@ -21,8 +21,8 @@ test('the static graph of the core shaders does not reach the layer shader modultest('a layer without registered code fails with the import to add', async () => {const { createFragmentShader, fragmentShader } = await import('../src/shaders.js?without-layer-code');assert.doesNotMatch(fragmentShader, /anisotropicSpecular|filmReflectance/);- assert.throws(() => createFragmentShader(['anisotropy']), /anisotropy layer needs its shader code.*addons\/material-layer-shaders/);- assert.throws(() => createFragmentShader(['iridescence']), /iridescence layer needs its shader code.*addons\/material-layer-shaders/);+ assert.throws(() => createFragmentShader(['anisotropy']), /anisotropy feature needs its shader code.*addons\/material-layer-shaders/);+ assert.throws(() => createFragmentShader(['iridescence']), /iridescence feature needs its shader code.*addons\/material-layer-shaders/);assert.doesNotThrow(() => createFragmentShader(['water']));});diff --git a/tests/view-transform.test.js b/tests/view-transform.test.jsindex f6d234ba..2ff9758e 100644--- a/tests/view-transform.test.js+++ b/tests/view-transform.test.js@@ -3,13 +3,17 @@ import { readFileSync } from 'node:fs';import test from 'node:test';import { toneMapToRgba8 } from '../src/path-tracer.js';import { normalizeAgentLook } from '../src/agent-look.js';-import { fragmentShader, liquidFragmentShader, viewTransformShader } from '../src/shaders.js';+import '../src/liquid-shader.js';+import { fragmentShader, liquidFragmentSource, viewTransformShader } from '../src/shaders.js';import { environmentBackgroundFragment } from '../src/environment.js';import { postFragment } from '../src/post.js';import {acesChannel, AGX_CONSTANTS, applyViewTransform, displayColor, namedLook, neutralColor, NEUTRAL_CONSTANTS, parseCube, sampleLook,} from '../src/view-transform.js';import { LOOK_NAMES } from '../src/view-transform-names.js';++// The liquid pass is an addon since #281; importing it above registers its shader.+const liquidFragmentShader = liquidFragmentSource();import '../src/view-transform-shader.js';const golden = JSON.parse(readFileSync(new URL('./fixtures/blender/view-transform-golden.json', import.meta.url), 'utf8'));diff --git a/vite.lib.config.js b/vite.lib.config.jsindex 83caf393..d79fe2cf 100644--- a/vite.lib.config.js+++ b/vite.lib.config.js@@ -56,6 +56,8 @@ export default defineConfig({'material-layer-shaders': 'src/material-layer-shaders.js','view-transform-shader': 'src/view-transform-shader.js','view-transform': 'src/view-transform.js',+ 'liquid-shader': 'src/liquid-shader.js',+ 'splat-shader': 'src/splat-shader.js','light-linking': 'src/light-linking.js','cascaded-shadows': 'src/cascaded-shadows.js','gaussian-splats': 'src/gaussian-splats.js',