Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(sdk): the Water, Terrain, Foliage, Hair, Skin and Eye shaders ship with their modules, and a material that names one brings its module into an export

yhhfe9fc5a0651a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ad80e7da2..710c738e6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,6 +35,8 @@ published separately; it ships inside the editor.
- **Contact shadows and screen-space bounce light.** Contact Shadows and Screen Space GI effects add small sharp shadows at contacts and one bounce of colour between surfaces, and the weather softens the sun's shadows. `contactShadows` marches a short ray toward the scene's sun in the depth buffer; `ssgi` gathers eight rays a pixel over the G-buffer's normals at half size into a float target and averages the neighbours on the same surface before laying the light, times the receiver's colour, over the picture. The Weather component raises the sun's `sourceAngle` and lowers its `shadowStrength` as the sky clouds over.
- **Terrain and Foliage.** A Terrain component makes ground from a heightmap or noise, paints it with grass, dirt, snow and rock layers and draws it in chunks that coarsen with distance; a Foliage component scatters grass or a mesh over it, swaying in the wind and thinning out with distance.
- **Separate material maps packed on import.** Importing an FBX with separate metalness, roughness and occlusion images now packs them into the one map the Model shader samples, instead of dropping them. Occlusion goes in red, roughness in green and metal in blue, each from its image's red channel (a glossiness map is inverted); a channel with no image stays at full so the material's own value speaks. Only PNG and DDS can be read.
+- **Built-in shaders ship with their modules.** The Water, Terrain, Foliage, Hair, Skin and Eye built-in shaders now ship with their modules instead of the core: a package that installs none of them no longer carries about 80 KB of shader source (a blank web package is 80 KB smaller, a blank WeChat bundle 7% smaller). A material that names one of them is what brings its module into an export, with no component in a scene needed; the three character shaders are a new `esengine/character-shaders` module.
+- **Opaque models no longer vanish beside glass.** Opaque draws are told from blended ones by the stage they are sorted in, not by their blend mode, when a frame has both a refracting surface and blended draws; before, a lit material whose blend mode was not None (the Model material) was treated as blended and could vanish from such a frame.
- **Water quality levels and a lite water.** Water spends what the quality level allows. Each level's profile has `waterWaves` (waves summed, 2 / 3 / 4), `waterRippleCells` (the ripple grid's side, 32 / 64 / 128), `waterDisplacement` (off is the lite water: flat geometry with the waves in the shading only), `waterMirror` and `waterCaustics`. What `Waters.sample` answers follows what is drawn, and a minigame with no quality policy gets the Low level's water.
- **Move gizmo plane handles closer in.** The three plane handles of the move gizmo sit closer to its centre, on the inside corner between each pair of axes, instead of out at a third of the way along them.
- **Water editing in the viewport.** Select a lake or a river and its points are handles in the viewport: drag a point to move it on the body's own plane, drag the circle on an edge to insert one, press and release a point to delete it (a lake keeps three, a river two), and drag the handle beside a river's point to set its width there. Each gesture is one undo step. Details shows a water body only the fields its form uses, with a Boundary row for a lake's outline and the point count and the pressed point's width under a Spline. Also: the move gizmo now travels with the entity while an axis is dragged, instead of staying where the grab began.
diff --git a/docs/WATER_DESIGN.md b/docs/WATER_DESIGN.md
index 1b49b92a5..7a2f4b1c2 100644
--- a/docs/WATER_DESIGN.md
+++ b/docs/WATER_DESIGN.md
@@ -191,7 +191,8 @@ WaterBody {
| web 的 sdk 目录 | 1,513,617 | 1,530,867 | +17,250 字节 |
| wechat 的 game-bundle.js | 1,095,783 | 1,118,163 | +22,380 字节(2.0%) |
-含水时才带 `esengine/water` 模块(导出里出现 `sdk/water`),不含水的包没有它。**发现:**`builtin:water` 的着色器源码(约 38 KB)和 Terrain、Foliage、Hair、Skin、Eye 的(各 6 到 10 KB)一样放在核心的 `builtinShaders.ts` 里,所以不含水的包也带着这 38 KB 字符串,压缩不动字符串。要让它们跟模块走,得改着色器模板表的注册方式(编辑器的着色器目录、管线的数据纹理审计、加载器都读这张表),是一件单独的事,这里没做。
+含水时才带 `esengine/water` 模块(导出里出现 `sdk/water`),不含水的包没有它。**发现及处理:**`builtin:water` 的着色器源码(约 38 KB)和 Terrain、Foliage、Hair、Skin、Eye 的(各 6 到 10 KB)原先都放在核心的 `builtinShaders.ts` 里,所以不含水的包也带着这些字符串,压缩不动字符串。已改成各模块安装时注册自己的模板(`registerBuiltinShaderTemplate`):水、地形、植被各随自己的模块,Hair、Skin、Eye 是新的 `esengine/character-shaders` 模块;导出时由材质的 `shader: 'builtin:…'` 决定带哪个模块。改后空白项目导出:web 的 sdk 目录 1,513,617 → 1,433,641 字节(−80 KB),WeChat 的 game-bundle.js 1,095,783 → 1,015,282 字节(−7.3%);含水的项目 wechat 1,118,163 → 1,078,127。完整入口(编辑器、node)仍然安装全部模块,所以它们的着色器目录不变。
+**仍留在核心的水:**`core-content` 重新导出了水的组件与插件代码(`WaterBody`、`WaterPlugin` 等,让脚本能从 `esengine` 取到它们),所以不含水的包还带着这部分 JS(量级几十 KB 以内,未单独量)。
另一条发现:`esengine/water` 和 `esengine/spline` 子路径在 W8 之前一直没有被构建进 `dist`(`package.json` 写了出口,但 rolldown 与 rollup 的入口里没有它们),W8 已补上。
## 10. 需要你确认的
diff --git a/pipeline/src/bundle/engineSubpaths.ts b/pipeline/src/bundle/engineSubpaths.ts
index 50a4688d5..9ae85b0c7 100644
--- a/pipeline/src/bundle/engineSubpaths.ts
+++ b/pipeline/src/bundle/engineSubpaths.ts
@@ -20,6 +20,7 @@ export const ESENGINE_SUBPATHS: Readonly<Record<string, string>> = {
'esengine/foliage': 'foliage/index.js',
'esengine/water': 'water/index.js',
'esengine/spline': 'spline/index.js',
+ 'esengine/character-shaders': 'character-shaders/index.js',
'esengine/logic': 'logic/index.js',
'esengine/debug-channel': 'debug-channel/index.js',
'esengine/ai': 'ai/index.js',
diff --git a/pipeline/src/export/contentSubsystems.ts b/pipeline/src/export/contentSubsystems.ts
index 00fabcc79..a9d3d006b 100644
--- a/pipeline/src/export/contentSubsystems.ts
+++ b/pipeline/src/export/contentSubsystems.ts
@@ -21,7 +21,8 @@ export async function contentSubsystems(
const usage = new Map<Subsystem, string[]>();
for (const rel of includedPaths) {
const ext = path.extname(rel).toLowerCase();
- if (ext !== '.esscene' && ext !== '.esprefab') continue;
+ // A material is read too: its shader may be one a module ships, with nothing in any scene to say so.
+ if (ext !== '.esscene' && ext !== '.esprefab' && ext !== '.esmaterial') continue;
let doc: unknown;
try {
doc = JSON.parse(await readFile(path.join(root, rel), 'utf8'));
diff --git a/pipeline/src/project/targetSupport.ts b/pipeline/src/project/targetSupport.ts
index 4a4a1338d..493017928 100644
--- a/pipeline/src/project/targetSupport.ts
+++ b/pipeline/src/project/targetSupport.ts
@@ -27,7 +27,7 @@ import { NATIVE_PLATFORMS, type ExportPlatform } from './platforms';
/** An engine subsystem a build target may lack. */
export type Subsystem =
- | 'text' | 'tilemap' | 'terrain' | 'foliage' | 'water' | 'spline' | 'timeline' | 'fxBasic' | 'fxAtmosphere' | 'fxLighting' | 'particles' | 'postprocess' | 'physics' | 'physics3d'
+ | 'text' | 'tilemap' | 'terrain' | 'foliage' | 'water' | 'spline' | 'characterShaders' | 'timeline' | 'fxBasic' | 'fxAtmosphere' | 'fxLighting' | 'particles' | 'postprocess' | 'physics' | 'physics3d'
| 'spine' | 'dragonbones' | 'video' | 'ai' | 'logic' | 'replication' | 'gameplay';
/**
@@ -48,6 +48,8 @@ export const SUBSYSTEM_COMPONENTS: Readonly<Record<Subsystem, readonly string[]>
foliage: ['Foliage'],
water: ['WaterBody', 'Floating'],
spline: ['Spline'],
+ // Hair, Skin and Eye are shaders, not components: a character's materials are what name them (see SHADER_SUBSYSTEM).
+ characterShaders: [],
particles: ['ParticleEmitter', 'ParticleForceField'],
postprocess: ['PostProcessVolume'],
physics: [
@@ -90,6 +92,15 @@ export const POST_PROCESS_EFFECT_GROUP: Readonly<Record<string, Subsystem>> = {
underwater: 'water',
};
+/**
+ * Which module carries each built-in shader that is not the core's. A material names its shader by `shader: 'builtin:<id>'`, so a project that authors one
+ * uses the module that ships it — with no component in a scene to say so (a Water material on a plain mesh, a character's Hair).
+ */
+export const SHADER_SUBSYSTEM: Readonly<Record<string, Subsystem>> = {
+ 'builtin:water': 'water', 'builtin:terrain': 'terrain', 'builtin:foliage': 'foliage',
+ 'builtin:hair': 'characterShaders', 'builtin:skin': 'characterShaders', 'builtin:eye': 'characterShaders',
+};
+
/** Human name, for a message someone packaging a game has to act on. */
export const SUBSYSTEM_LABEL: Readonly<Record<Subsystem, string>> = {
text: 'Text',
@@ -102,6 +113,7 @@ export const SUBSYSTEM_LABEL: Readonly<Record<Subsystem, string>> = {
foliage: 'Foliage',
water: 'Water',
spline: 'Splines',
+ characterShaders: 'Character shaders (hair, skin, eye)',
particles: 'Particles',
postprocess: 'Post-processing',
physics: 'Physics (Box2D)',
@@ -134,6 +146,7 @@ export const SUBSYSTEM_INSTALL: Readonly<Record<Subsystem, string>> = {
foliage: 'esengine/foliage',
water: 'esengine/water',
spline: 'esengine/spline',
+ characterShaders: 'esengine/character-shaders',
physics: 'esengine/physics',
physics3d: 'esengine/physics3d',
spine: 'esengine/spine',
@@ -215,6 +228,11 @@ export function collectSubsystems(doc: unknown, into: Set<Subsystem> = new Set()
if (typeof rec.type === 'string') {
const subsystem = COMPONENT_SUBSYSTEM.get(rec.type);
if (subsystem) into.add(subsystem);
+ // A material names the built-in shader it is made from.
+ if (rec.type === 'material' && typeof rec.shader === 'string') {
+ const shaderSubsystem = SHADER_SUBSYSTEM[rec.shader];
+ if (shaderSubsystem) into.add(shaderSubsystem);
+ }
// A volume's effects are named in its data, and each is carried by one group.
if (rec.type === 'PostProcessVolume') collectEffectGroups(rec.data, into);
}
diff --git a/pipeline/tests/engine-installs.test.ts b/pipeline/tests/engine-installs.test.ts
index 0c0b9b27c..a9839dfe8 100644
--- a/pipeline/tests/engine-installs.test.ts
+++ b/pipeline/tests/engine-installs.test.ts
@@ -11,7 +11,7 @@
import { describe, it, expect } from 'vitest';
import { engineInstalls, forcedSideModules, moduleChoices, moduleForAsset } from '../src/bundle/engineInstalls';
import { ENGINE_MODULES, ESENGINE_SUBPATHS, NOT_A_MODULE } from '../src/bundle/engineSubpaths';
-import { POST_PROCESS_EFFECT_GROUP, SUBSYSTEM_COMPONENTS, SUBSYSTEM_INSTALL, type Subsystem } from '../src/project/targetSupport';
+import { POST_PROCESS_EFFECT_GROUP, SHADER_SUBSYSTEM, SUBSYSTEM_COMPONENTS, SUBSYSTEM_INSTALL, type Subsystem } from '../src/project/targetSupport';
describe('the engine modules a package installs', () => {
it('takes a subsystem from the components its content authored', () => {
@@ -29,6 +29,19 @@ describe('the engine modules a package installs', () => {
expect(engineInstalls({ subsystems: ['water'] }).subpaths).toEqual(['esengine/water']);
});
+ it('takes the module of a built-in shader from the material that names it, with no component in a scene to say so', async () => {
+ const { collectSubsystems } = await import('../src/project/targetSupport');
+ const material = (shader: string) => ({ version: '1.0', type: 'material', shader, properties: {} });
+ expect([...collectSubsystems(material('builtin:water'))]).toEqual(['water']);
+ expect([...collectSubsystems(material('builtin:terrain'))]).toEqual(['terrain']);
+ expect([...collectSubsystems(material('builtin:foliage'))]).toEqual(['foliage']);
+ for (const id of ['hair', 'skin', 'eye']) expect([...collectSubsystems(material(`builtin:${id}`))]).toEqual(['characterShaders']);
+ // A core shader and a project's own shader carry nothing extra.
+ expect([...collectSubsystems(material('builtin:model'))]).toEqual([]);
+ expect([...collectSubsystems(material('assets/shaders/custom.esshader'))]).toEqual([]);
+ expect(engineInstalls({ subsystems: ['characterShaders'] }).subpaths).toEqual(['esengine/character-shaders']);
+ });
+
it('takes splines from a Spline, so a game without one carries none', () => {
expect(engineInstalls({ subsystems: ['spline'] }).subpaths).toEqual(['esengine/spline']);
});
@@ -232,6 +245,8 @@ describe('every module a project can refuse', () => {
}
// …and a post-process effect group is found by the effects a volume names.
for (const group of Object.values(POST_PROCESS_EFFECT_GROUP)) byComponent.add(SUBSYSTEM_INSTALL[group]);
+ // …and a module that ships built-in shaders is found by the materials that name them.
+ for (const shader of Object.values(SHADER_SUBSYSTEM)) byComponent.add(SUBSYSTEM_INSTALL[shader]);
expect(ENGINE_MODULES.filter((m) => !byComponent.has(m))).toEqual([]);
});
});
diff --git a/sdk/package.json b/sdk/package.json
index 44486d340..3d6ae7f81 100644
--- a/sdk/package.json
+++ b/sdk/package.json
@@ -25,6 +25,7 @@
"./dist/foliage/index.js",
"./dist/water/index.js",
"./dist/spline/index.js",
+ "./dist/character-shaders/index.js",
"./dist/logic/index.js",
"./dist/debug-channel/index.js",
"./dist/ai/index.js",
@@ -111,6 +112,10 @@
"import": "./dist/spline/index.js",
"types": "./dist/spline/index.d.ts"
},
+ "./character-shaders": {
+ "import": "./dist/character-shaders/index.js",
+ "types": "./dist/character-shaders/index.d.ts"
+ },
"./logic": {
"import": "./dist/logic/index.js",
"types": "./dist/logic/index.d.ts"
diff --git a/sdk/rolldown.config.js b/sdk/rolldown.config.js
index 1a3d13f05..073063a12 100644
--- a/sdk/rolldown.config.js
+++ b/sdk/rolldown.config.js
@@ -105,6 +105,7 @@ export default defineConfig([
'foliage/index': 'src/foliage/index.ts',
'water/index': 'src/water/index.ts',
'spline/index': 'src/spline/index.ts',
+ 'character-shaders/index': 'src/character/index.ts',
'logic/index': 'src/logic/index.ts',
'debug-channel/index': 'src/debug-channel/index.ts',
'ai/index': 'src/ai/index.ts',
diff --git a/sdk/rollup.config.js b/sdk/rollup.config.js
index d0c3094c6..56daa3385 100644
--- a/sdk/rollup.config.js
+++ b/sdk/rollup.config.js
@@ -27,6 +27,7 @@ export default [
'tilemap/index': 'src/tilemap/index.ts',
'water/index': 'src/water/index.ts',
'spline/index': 'src/spline/index.ts',
+ 'character-shaders/index': 'src/character/index.ts',
'logic/index': 'src/logic/index.ts',
'debug-channel/index': 'src/debug-channel/index.ts',
'ai/index': 'src/ai/index.ts',
diff --git a/sdk/src/asset/loaders/MaterialShaderAssetLoader.ts b/sdk/src/asset/loaders/MaterialShaderAssetLoader.ts
index 045b6ab12..f40d1bc17 100644
--- a/sdk/src/asset/loaders/MaterialShaderAssetLoader.ts
+++ b/sdk/src/asset/loaders/MaterialShaderAssetLoader.ts
@@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import type { AssetLoader, LoadContext } from '../AssetLoader';
import { BUILTIN_REF_PREFIX, isBuiltinAssetRef } from '../documentRef';
-import { builtinShaderTemplate } from '../../render/builtinShaders';
+import { builtinShaderTemplate, BUILTIN_SHADER_MODULE } from '../../render/builtinShaders';
import { Material, type ShaderHandle } from '../../render/material';
import { disposeAll } from '../../util/disposeAll';
@@ -36,7 +36,13 @@ export class MaterialShaderAssetLoader implements AssetLoader<MaterialShaderSour
let source: string;
if (isBuiltinAssetRef(path)) {
const template = builtinShaderTemplate(path.slice(BUILTIN_REF_PREFIX.length));
- if (!template) throw new Error(`Unknown built-in shader: ${path}`);
+ if (!template) {
+ const id = path.slice(BUILTIN_REF_PREFIX.length);
+ const module = BUILTIN_SHADER_MODULE[id];
+ throw new Error(module
+ ? `The built-in shader ${path} ships in ${module}, which this package did not install: import '${module}'.`
+ : `Unknown built-in shader: ${path}`);
+ }
source = template.source;
} else {
source = ctx.readSource ? await ctx.readSource(path) : await ctx.loadText(ctx.catalog.getBuildPath(path));
diff --git a/sdk/src/character/characterShaders.ts b/sdk/src/character/characterShaders.ts
new file mode 100644
index 000000000..f33d2d463
--- /dev/null
+++ b/sdk/src/character/characterShaders.ts
@@ -0,0 +1,632 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file characterShaders.ts
+ * @brief The Hair, Skin and Eye built-in shaders a character is shaded with.
+ *
+ * The shader source lives with the module that installs it, not in the core's list of built-in shaders: a package that does not install the module carries none of it.
+ */
+import { makeBuiltinShaderTemplate } from '../render/builtinShaders';
+
+
+// Hair: lit as strands. The flow map gives the strand direction; the lighting does Kajiya-Kay diffuse and two shifted highlights along it
+// (misc.y above zero is the switch). Two-sided and translucent; the base colour is the mesh's own texture, as in the Model shader.
+const HAIR = `#pragma shader "Hair"
+#pragma version 300 es
+#pragma domain Lit
+#pragma param u_tint color default(1,1,1,1)
+#pragma param u_normalMap texture default(flatnormal) texel(data)
+#pragma param u_ormMap texture default(white) texel(data)
+#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)
+// Which way each strand runs: red and green are a direction across the texture, the way an authored flow map writes it.
+#pragma param u_flowMap texture default(white) texel(data)
+// A texture that shifts the highlight along the strand from one place to the next, and how far, and how often it repeats.
+#pragma param u_shiftMap texture default(white) texel(data)
+#pragma param u_shiftAmount float default(0.35) range(0,1) ui(slider)
+#pragma param u_shiftTile float default(6) range(0.1,64) ui(slider)
+// How tightly each highlight is drawn (roughness: lower is a narrower line), how strong they are, and how far each rides off the strand's
+// centre line. The second is the coloured one, tinted by u_secondaryTint.
+#pragma param u_roughness float default(0.3) range(0.05,1) ui(slider)
+#pragma param u_specular float default(1) range(0,2) ui(slider)
+#pragma param u_shiftPrimary float default(-0.1) range(-1,1) ui(slider)
+#pragma param u_shiftSecondary float default(0.2) range(-1,1) ui(slider)
+#pragma param u_secondaryTint color default(0.75,0.45,0.25,1)
+// Light that comes through the strands from behind.
+#pragma param u_translucency float default(0.4) range(0,1) ui(slider)
+// A negative bias on the colour's mip level keeps the fine strands a hair card is covered in from washing out at a slant, which they do at once.
+#pragma param u_sharpen float default(-0.8) range(-3,1) ui(slider)
+// How much darker the roots are than the tips: the occlusion map's red, pushed.
+#pragma param u_rootDark float default(0.5) range(0,1) ui(slider)
+// A card's alpha cut at this level, and sharpened to about a texel so alpha-to-coverage feathers the edge of every strand instead of stair-stepping it; 0 keeps the card opaque.
+#pragma param u_alphaCutoff float default(0) range(0,1) ui(slider)
+#pragma coverage u_alphaCutoff
+// The soft outer band of a card: where its alpha falls from the cutoff down by this much, a second, blended pass draws the strand's tip fading out
+// (the opaque pass keeps what is above the cutoff, feathered by coverage). Sorted with the other blended draws; 0 leaves it out.
+#pragma param u_tipSoftness float default(0) range(0,0.5) ui(slider)
+#pragma tips u_tipSoftness
+
+#pragma fragment
+precision highp float;
+
+in vec4 v_color;
+in vec2 v_texCoord;
+in highp vec2 v_worldPos;
+#ifdef MESH_NORMALS
+in highp vec3 v_worldNormal;
+in highp vec3 v_worldXYZ;
+#endif
+uniform sampler2D u_textures[1];
+
+#ifdef ES_GBUFFER
+layout(location = 0) out vec4 fragColor;
+layout(location = 1) out vec4 fragSurface;
+
+vec2 packNormal(vec3 n) {
+ n /= (abs(n.x) + abs(n.y) + abs(n.z));
+ vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
+ vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
+ return e * 0.5 + 0.5;
+}
+#else
+out vec4 fragColor;
+#endif
+
+void main() {
+#ifdef MESH_NORMALS
+ highp vec3 Ng = normalize(v_worldNormal);
+ highp vec3 P = v_worldXYZ;
+ highp vec3 N = perturbNormal(Ng, P, v_texCoord, sampleNormal(u_normalMap, v_texCoord));
+ if (!gl_FrontFacing) { N = -N; Ng = -Ng; }
+ // The strand: the flow map's direction across the texture, put onto the surface and made to lie across the normal.
+ vec2 flow = texture(u_flowMap, v_texCoord).rg * 2.0 - 1.0;
+ highp vec3 T = perturbNormal(Ng, P, v_texCoord, vec3(flow, 0.0));
+ T -= N * dot(N, T);
+ if (dot(T, T) < 1e-6) T = cross(N, abs(N.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0));
+ T = normalize(T);
+#else
+ highp vec3 P = vec3(v_worldPos, 0.0);
+ highp vec3 N = vec3(0.0, 0.0, 1.0);
+ highp vec3 T = vec3(0.0, 1.0, 0.0);
+#endif
+ vec4 base = texture(u_textures[0], v_texCoord, u_sharpen) * v_color * u_tint;
+ // Before any discard: a derivative needs every fragment of its quad.
+ float alpha = 1.0;
+#ifdef ES_TIP_PASS
+ // The band below the cutoff, fading in toward it, and a texel past it so it meets the opaque card without a gap.
+ alpha = (base.a - (u_alphaCutoff - u_tipSoftness)) / max(u_tipSoftness, 1e-4);
+ if (base.a >= u_alphaCutoff + fwidth(base.a) || alpha <= 0.0) discard;
+ alpha = min(alpha, 1.0);
+#else
+ if (u_alphaCutoff > 0.0) {
+ alpha = (base.a - u_alphaCutoff) / max(fwidth(base.a), 1e-4) + 0.5;
+ if (alpha <= 0.0) discard;
+ alpha = min(alpha, 1.0);
+ }
+#endif
+ float root = texture(u_ormMap, v_texCoord).r;
+ float ao = mix(1.0, root, u_occlusionStrength);
+ base.rgb *= mix(1.0, 0.35 + 0.65 * root, u_rootDark);
+ float noise = (texture(u_shiftMap, v_texCoord * u_shiftTile).r - 0.5) * u_shiftAmount;
+ SurfaceExt ext = surfaceExtNone(N);
+ ext.aniso = vec4(T, 0.0);
+ ext.misc = vec4(0.046, 1.0, u_shiftPrimary + noise, u_shiftSecondary + noise);
+ ext.scatter = vec4(base.rgb * u_secondaryTint.rgb * 1.6, 0.35);
+ ext.trans = vec4(u_translucency, 0.25, 0.0, 0.0);
+ vec3 viewDir = viewDirection(P);
+ vec3 lit = applyLightingSurface(base.rgb, N, P, viewDir, 0.0, u_roughness, u_specular, ao, ext);
+ fragColor = vec4(lit, alpha);
+#ifdef ES_GBUFFER
+ fragSurface = vec4(packNormal(normalize(N)), u_roughness, 0.75);
+#endif
+}
+#pragma end
+
+#pragma fragment wgsl
+#ifdef ES_GBUFFER
+struct FragOut {
+ @location(0) color : vec4f,
+ @location(1) surface : vec4f,
+};
+
+fn packNormal(n : vec3f) -> vec2f {
+ let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
+ let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
+ var e = a.xy;
+ if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
+ return e * 0.5 + 0.5;
+}
+
+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
+#else
+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
+#endif
+ let duvx = dpdx(v.v_texCoord);
+ let duvy = dpdy(v.v_texCoord);
+ g_probeSlot = v.v_probeSlot;
+#ifdef MESH_LIGHTMAP
+ g_lightmap = v.v_lightmap;
+#endif
+#ifdef MESH_TANGENTS
+ g_worldTangent = v.v_worldTangent;
+#endif
+ var base = textureSampleBias(t0, s0, v.v_texCoord, mc.u_sharpen) * v.v_color * mc.u_tint;
+ let edge = max(abs(dpdx(base.a)) + abs(dpdy(base.a)), 1e-4);
+ let flowTex = textureSampleGrad(u_flowMap, u_flowMap_s, v.v_texCoord, duvx, duvy).rg * 2.0 - vec2f(1.0);
+ let orm = textureSampleGrad(u_ormMap, u_ormMap_s, v.v_texCoord, duvx, duvy);
+ let shiftTex = textureSampleGrad(u_shiftMap, u_shiftMap_s, v.v_texCoord * mc.u_shiftTile, duvx * mc.u_shiftTile, duvy * mc.u_shiftTile).r;
+#ifdef MESH_NORMALS
+ var Ng = normalize(v.v_worldNormal);
+ let P = v.v_worldXYZ;
+ var N = perturbNormal(Ng, P, v.v_texCoord, sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy));
+ if (!front) { N = -N; Ng = -Ng; }
+ var T = perturbNormal(Ng, P, v.v_texCoord, vec3f(flowTex, 0.0));
+ T = T - N * dot(N, T);
+ if (dot(T, T) < 1e-6) {
+ var seed = vec3f(1.0, 0.0, 0.0);
+ if (abs(N.y) < 0.99) { seed = vec3f(0.0, 1.0, 0.0); }
+ T = cross(N, seed);
+ }
+ T = normalize(T);
+#else
+ let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
+ let N = vec3f(0.0, 0.0, 1.0);
+ let T = vec3f(0.0, 1.0, 0.0);
+#endif
+ var alpha = 1.0;
+#ifdef ES_TIP_PASS
+ alpha = (base.a - (mc.u_alphaCutoff - mc.u_tipSoftness)) / max(mc.u_tipSoftness, 1e-4);
+ if (base.a >= mc.u_alphaCutoff + edge || alpha <= 0.0) { discard; }
+ alpha = min(alpha, 1.0);
+#else
+ if (mc.u_alphaCutoff > 0.0) {
+ alpha = (base.a - mc.u_alphaCutoff) / edge + 0.5;
+ if (alpha <= 0.0) { discard; }
+ alpha = min(alpha, 1.0);
+ }
+#endif
+ let ao = mix(1.0, orm.r, mc.u_occlusionStrength);
+ base = vec4f(base.rgb * mix(1.0, 0.35 + 0.65 * orm.r, mc.u_rootDark), base.a);
+ let noise = (shiftTex - 0.5) * mc.u_shiftAmount;
+ var ext = surfaceExtNone(N);
+ ext.aniso = vec4f(T, 0.0);
+ ext.misc = vec4f(0.046, 1.0, mc.u_shiftPrimary + noise, mc.u_shiftSecondary + noise);
+ ext.scatter = vec4f(base.rgb * mc.u_secondaryTint.rgb * 1.6, 0.35);
+ ext.trans = vec4f(mc.u_translucency, 0.25, 0.0, 0.0);
+ let viewDir = viewDirection(P);
+ let lit = applyLightingSurface(base.rgb, N, P, viewDir, 0.0, mc.u_roughness, mc.u_specular, ao, ext);
+#ifdef ES_GBUFFER
+ var frag : FragOut;
+ frag.color = vec4f(lit, alpha);
+ frag.surface = vec4f(packNormal(normalize(N)), mc.u_roughness, 0.75);
+ return frag;
+#else
+ return vec4f(lit, alpha);
+#endif
+}
+#pragma end
+`;
+
+// Skin: scattering without blurring the picture. Diffuse light comes from a softer normal than the surface's own (softest for red), the
+// terminator is warmed, thin curved places let light through, and the shine is two lobes and a fuzz. Base colour is the mesh's texture.
+const SKIN = `#pragma shader "Skin"
+#pragma version 300 es
+#pragma domain Lit
+#pragma param u_tint color default(1,1,1,1)
+#pragma param u_normalMap texture default(flatnormal) texel(data)
+#pragma param u_ormMap texture default(white) texel(data)
+#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)
+// A second, fine normal map tiled over the surface (pores, creases) laid on the sharp normal the shine reads: how strongly, and how many times it repeats.
+// The soft diffuse normals do not carry it, as they do not carry the baked pores either. Import it with sRGB off and wrap repeat; 0 leaves it out.
+#pragma param u_detailMap texture default(flatnormal) texel(data)
+#pragma param u_detailStrength float default(0) range(0,2) ui(slider)
+#pragma param u_detailTile float default(16) range(1,128) ui(slider)
+// How rough the surface is (a scale on the map's own roughness), how much it shines, and the second, tighter lobe.
+#pragma param u_roughness float default(1) range(0,2) ui(slider)
+#pragma param u_specular float default(1) range(0,2) ui(slider)
+#pragma param u_lobe2 float default(0.18) range(0,1) ui(slider)
+#pragma param u_lobe2Roughness float default(0.4) range(0.05,1) ui(slider)
+// How much softer than the surface's own normal the diffuse light's normal is: for red, then for green and blue (mip levels of the normal map).
+#pragma param u_blurRed float default(3.2) range(0,8) ui(slider)
+#pragma param u_blurGreen float default(1.6) range(0,8) ui(slider)
+// The colour the terminator takes and how far light wraps past it.
+#pragma param u_sssColor color default(0.95,0.4,0.3,1)
+#pragma param u_sssWrap float default(0.45) range(0,1) ui(slider)
+// Light through thin places: how much, and how much of a surface's curvature counts as thin.
+#pragma param u_translucency float default(0.5) range(0,1) ui(slider)
+#pragma param u_thinness float default(0.5) range(0,1) ui(slider)
+// Pre-integrated scattering: the wrap past the terminator is taken per colour (red furthest) and widens where the surface turns sharply, which
+// is what makes a nose or an ear glow at its edge while a flat cheek stays crisp. u_curvature is how fast a turn counts as sharp, per world unit.
+#pragma param u_preIntegrate float default(0) range(0,1) ui(slider)
+#pragma param u_curvature float default(8) range(0,64) ui(slider)
+// The fine fuzz that lights a rim.
+#pragma param u_fuzz float default(0.1) range(0,1) ui(slider)
+// Marks the surface for the Subsurface Scattering effect, which spreads what is left of the light on a larger scale. Off by default.
+#pragma param u_subsurface float default(0) range(0,1) ui(slider)
+
+#pragma fragment
+precision highp float;
+
+in vec4 v_color;
+in vec2 v_texCoord;
+in highp vec2 v_worldPos;
+#ifdef MESH_NORMALS
+in highp vec3 v_worldNormal;
+in highp vec3 v_worldXYZ;
+#endif
+uniform sampler2D u_textures[1];
+
+#ifdef ES_GBUFFER
+layout(location = 0) out vec4 fragColor;
+layout(location = 1) out vec4 fragSurface;
+
+vec2 packNormal(vec3 n) {
+ n /= (abs(n.x) + abs(n.y) + abs(n.z));
+ vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
+ vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
+ return e * 0.5 + 0.5;
+}
+#else
+out vec4 fragColor;
+#endif
+
+vec3 skinNormalAt(float bias) {
+ return normalize(texture(u_normalMap, v_texCoord, bias).xyz * 2.0 - 1.0);
+}
+
+// The baked normal with the detail's slope added to it, in the tangent frame both are read in.
+vec3 skinDetail(vec3 baked, vec2 detail) {
+ return normalize(vec3(baked.xy + detail * u_detailStrength, baked.z));
+}
+
+void main() {
+ // Taken before any branch: a derivative needs every fragment of its quad.
+ vec2 detail = texture(u_detailMap, v_texCoord * u_detailTile).xy * 2.0 - 1.0;
+#ifdef MESH_NORMALS
+ highp vec3 Ng = normalize(v_worldNormal);
+ highp vec3 P = v_worldXYZ;
+ if (!gl_FrontFacing) Ng = -Ng;
+ highp vec3 N = perturbNormal(Ng, P, v_texCoord, skinDetail(skinNormalAt(0.0), detail));
+ highp vec3 Nmid = perturbNormal(Ng, P, v_texCoord, skinNormalAt(u_blurGreen));
+ highp vec3 Nsoft = perturbNormal(Ng, P, v_texCoord, skinNormalAt(u_blurRed));
+ // How fast the surface turns across a pixel, against how big a pixel is: the thin, strongly curved places.
+ float turn = (length(dFdx(Ng)) + length(dFdy(Ng))) / max(length(dFdx(P)) + length(dFdy(P)), 1e-5);
+ float thick = 1.0 - clamp(turn * 12.0 * u_thinness, 0.0, 1.0);
+ float curv = clamp(turn * u_curvature, 0.0, 1.0);
+#else
+ highp vec3 P = vec3(v_worldPos, 0.0);
+ highp vec3 N = vec3(0.0, 0.0, 1.0);
+ highp vec3 Nmid = N;
+ highp vec3 Nsoft = N;
+ float thick = 1.0;
+ float curv = 0.0;
+#endif
+ vec4 base = texture(u_textures[0], v_texCoord) * v_color * u_tint;
+ vec4 orm = texture(u_ormMap, v_texCoord);
+ float ao = mix(1.0, orm.r, u_occlusionStrength);
+ float rough = clamp(orm.g * u_roughness, 0.05, 1.0);
+ vec3 viewDir = viewDirection(P);
+
+ // The diffuse light, from two softnesses of normal, with the terminator warmed and light let through a thin place.
+ SurfaceExt wrapped = surfaceExtNone(Nsoft);
+ wrapped.scatter = vec4(u_sssColor.rgb * base.rgb * 1.4, u_sssWrap);
+ wrapped.trans = vec4(u_translucency, thick, curv, u_preIntegrate);
+ vec3 soft = applyLightingSurface(base.rgb, Nsoft, P, viewDir, 0.0, 1.0, 0.0, ao, wrapped);
+ SurfaceExt wrappedMid = surfaceExtNone(Nmid);
+ wrappedMid.scatter = wrapped.scatter;
+ wrappedMid.trans = wrapped.trans;
+ vec3 mid = applyLightingSurface(base.rgb, Nmid, P, viewDir, 0.0, 1.0, 0.0, ao, wrappedMid);
+ vec3 diffuse = vec3(soft.r, mid.g, mid.b);
+
+ // The shine: two lobes and a fuzz, on the surface's own normal, with no diffuse under it.
+ SurfaceExt shine = surfaceExtNone(N);
+ shine.coat = vec4(u_lobe2, u_lobe2Roughness, 0.0, 0.0);
+ shine.coatN = vec4(N, 0.0);
+ shine.sheen = vec4(vec3(u_fuzz), 0.5);
+ vec3 spec = applyLightingSurface(vec3(0.0), N, P, viewDir, 0.0, rough, u_specular, ao, shine);
+
+ float alpha = u_subsurface > 0.0 ? 0.996 : 1.0;
+ fragColor = vec4(diffuse + spec, alpha);
+#ifdef ES_GBUFFER
+ fragSurface = vec4(packNormal(normalize(N)), rough, 0.75);
+#endif
+}
+#pragma end
+
+#pragma fragment wgsl
+#ifdef ES_GBUFFER
+struct FragOut {
+ @location(0) color : vec4f,
+ @location(1) surface : vec4f,
+};
+
+fn packNormal(n : vec3f) -> vec2f {
+ let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
+ let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
+ var e = a.xy;
+ if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
+ return e * 0.5 + 0.5;
+}
+
+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
+#else
+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
+#endif
+ // Every sample is taken here, before any branch.
+ g_probeSlot = v.v_probeSlot;
+#ifdef MESH_LIGHTMAP
+ g_lightmap = v.v_lightmap;
+#endif
+#ifdef MESH_TANGENTS
+ g_worldTangent = v.v_worldTangent;
+#endif
+ let base = textureSample(t0, s0, v.v_texCoord) * v.v_color * mc.u_tint;
+ let orm = textureSample(u_ormMap, u_ormMap_s, v.v_texCoord);
+ let detail = textureSample(u_detailMap, u_detailMap_s, v.v_texCoord * mc.u_detailTile).xy * 2.0 - vec2f(1.0);
+ let bakedSharp = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, 0.0).xyz * 2.0 - vec3f(1.0));
+ let tangentSharp = normalize(vec3f(bakedSharp.xy + detail * mc.u_detailStrength, bakedSharp.z));
+ let tangentMid = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurGreen).xyz * 2.0 - vec3f(1.0));
+ let tangentSoft = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurRed).xyz * 2.0 - vec3f(1.0));
+#ifdef MESH_NORMALS
+ var Ng = normalize(v.v_worldNormal);
+ let P = v.v_worldXYZ;
+ if (!front) { Ng = -Ng; }
+ let N = perturbNormal(Ng, P, v.v_texCoord, tangentSharp);
+ let Nmid = perturbNormal(Ng, P, v.v_texCoord, tangentMid);
+ let Nsoft = perturbNormal(Ng, P, v.v_texCoord, tangentSoft);
+ let turn = (length(dpdx(Ng)) + length(dpdy(Ng))) / max(length(dpdx(P)) + length(dpdy(P)), 1e-5);
+ let thick = 1.0 - clamp(turn * 12.0 * mc.u_thinness, 0.0, 1.0);
+ let curv = clamp(turn * mc.u_curvature, 0.0, 1.0);
+#else
+ let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
+ let N = vec3f(0.0, 0.0, 1.0);
+ let Nmid = N;
+ let Nsoft = N;
+ let thick = 1.0;
+ let curv = 0.0;
+#endif
+ let ao = mix(1.0, orm.r, mc.u_occlusionStrength);
+ let rough = clamp(orm.g * mc.u_roughness, 0.05, 1.0);
+ let viewDir = viewDirection(P);
+
+ var wrapped = surfaceExtNone(Nsoft);
+ wrapped.scatter = vec4f(mc.u_sssColor.rgb * base.rgb * 1.4, mc.u_sssWrap);
+ wrapped.trans = vec4f(mc.u_translucency, thick, curv, mc.u_preIntegrate);
+ let soft = applyLightingSurface(base.rgb, Nsoft, P, viewDir, 0.0, 1.0, 0.0, ao, wrapped);
+ var wrappedMid = surfaceExtNone(Nmid);
+ wrappedMid.scatter = wrapped.scatter;
+ wrappedMid.trans = wrapped.trans;
+ let mid = applyLightingSurface(base.rgb, Nmid, P, viewDir, 0.0, 1.0, 0.0, ao, wrappedMid);
+ let diffuse = vec3f(soft.r, mid.g, mid.b);
+
+ var shine = surfaceExtNone(N);
+ shine.coat = vec4f(mc.u_lobe2, mc.u_lobe2Roughness, 0.0, 0.0);
+ shine.coatN = vec4f(N, 0.0);
+ shine.sheen = vec4f(vec3f(mc.u_fuzz), 0.5);
+ let spec = applyLightingSurface(vec3f(0.0), N, P, viewDir, 0.0, rough, mc.u_specular, ao, shine);
+
+ var alpha = 1.0;
+ if (mc.u_subsurface > 0.0) { alpha = 0.996; }
+#ifdef ES_GBUFFER
+ var frag : FragOut;
+ frag.color = vec4f(diffuse + spec, alpha);
+ frag.surface = vec4f(packNormal(normalize(N)), rough, 0.75);
+ return frag;
+#else
+ return vec4f(diffuse + spec, alpha);
+#endif
+}
+#pragma end
+`;
+
+const EYE = `#pragma shader "Eye"
+#pragma version 300 es
+#pragma domain Lit
+#pragma param u_tint color default(1,1,1,1)
+// The iris is built from two maps and the numbers below: a mask that is white over the iris and the dome of the cornea
+// (u_depthMap), and that dome's normals (u_irisNormalMap). A painted eye texture on the mesh multiplies the result.
+#pragma param u_depthMap texture default(black) texel(data)
+#pragma param u_irisNormalMap texture default(flatnormal) texel(data)
+#pragma param u_scleraColor color default(0.92,0.9,0.87,1)
+// The iris from the pupil's edge out to the limbus, and how dark its rim is.
+#pragma param u_irisColor color default(0.22,0.4,0.52,1)
+#pragma param u_irisColor2 color default(0.08,0.17,0.22,1)
+#pragma param u_irisSize float default(0.42) range(0.1,0.6) ui(slider)
+#pragma param u_pupilSize float default(0.3) range(0,0.8) ui(slider)
+#pragma param u_limbal float default(0.7) range(0,1) ui(slider)
+// How far behind the cornea the iris lies (in uv), and the cornea's index of refraction, which bend what is seen of it.
+#pragma param u_irisDepth float default(0.06) range(0,0.2) ui(slider)
+#pragma param u_ior float default(1.336) range(1,1.6) ui(slider)
+// The wet cornea is nearly a mirror; the white of the eye is a duller sheen with light that wraps past the terminator.
+#pragma param u_corneaRoughness float default(0.04) range(0.01,0.3) ui(slider)
+#pragma param u_scleraRoughness float default(0.45) range(0.1,1) ui(slider)
+#pragma param u_specular float default(1) range(0,2) ui(slider)
+#pragma param u_scleraWrap float default(0.35) range(0,1) ui(slider)
+// The lids shade the eye they sit on. u_lidMap is an authored occlusion map (white where the eye is open); u_lidShadow also darkens, and fades the
+// cornea's reflections, where the surface faces up from u_lidStart (a dot with the world's up) to straight up: the upper lid, for an upright head.
+#pragma param u_lidMap texture default(white) texel(data)
+#pragma param u_lidShadow float default(0.5) range(0,1) ui(slider)
+#pragma param u_lidStart float default(0.2) range(-1,1) ui(slider)
+
+#pragma fragment
+precision highp float;
+
+in vec4 v_color;
+in vec2 v_texCoord;
+in highp vec2 v_worldPos;
+#ifdef MESH_NORMALS
+in highp vec3 v_worldNormal;
+in highp vec3 v_worldXYZ;
+#endif
+uniform sampler2D u_textures[1];
+
+#ifdef ES_GBUFFER
+layout(location = 0) out vec4 fragColor;
+layout(location = 1) out vec4 fragSurface;
+
+vec2 packNormal(vec3 n) {
+ n /= (abs(n.x) + abs(n.y) + abs(n.z));
+ vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
+ vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
+ return e * 0.5 + 0.5;
+}
+#else
+out vec4 fragColor;
+#endif
+
+void main() {
+ vec2 uv = v_texCoord;
+ // Every sample that takes part in a derivative is taken here, before any branch.
+ float h = texture(u_depthMap, uv).r;
+ float lid = texture(u_lidMap, uv).r;
+ vec3 dome = normalize(texture(u_irisNormalMap, uv).xyz * 2.0 - 1.0);
+#ifdef MESH_NORMALS
+ highp vec3 Ng = normalize(v_worldNormal);
+ highp vec3 P = v_worldXYZ;
+ if (!gl_FrontFacing) Ng = -Ng;
+ highp vec3 T = perturbNormal(Ng, P, uv, vec3(1.0, 0.0, 0.0));
+ highp vec3 B = perturbNormal(Ng, P, uv, vec3(0.0, 1.0, 0.0));
+ highp vec3 Ndome = perturbNormal(Ng, P, uv, dome);
+#else
+ highp vec3 P = vec3(v_worldPos, 0.0);
+ highp vec3 Ng = vec3(0.0, 0.0, 1.0);
+ highp vec3 T = vec3(1.0, 0.0, 0.0);
+ highp vec3 B = vec3(0.0, 1.0, 0.0);
+ highp vec3 Ndome = Ng;
+#endif
+ vec3 viewDir = viewDirection(P);
+ // The ray from the eye, in the surface's own frame, bent into the cornea and carried down to the iris: that is where on
+ // the iris this pixel looks, which moves as the eye turns.
+ vec3 vt = normalize(vec3(dot(viewDir, T), dot(viewDir, B), max(dot(viewDir, Ng), 0.05)));
+ vec3 R = refract(-vt, dome, 1.0 / u_ior);
+ vec2 uvI = uv + R.xy * (u_irisDepth * h / max(-R.z, 0.2));
+ float m = texture(u_depthMap, uvI).r;
+ float irisMask = smoothstep(0.15, 0.85, m);
+ float surfaceMask = smoothstep(0.15, 0.85, h);
+
+ vec2 c = uvI - vec2(0.5);
+ float rr = length(c) / max(u_irisSize, 1e-3);
+ float ang = atan(c.y, c.x);
+ float fibers = 0.82 + 0.18 * sin(ang * 70.0 + sin(ang * 13.0 + rr * 9.0) * 2.0);
+ vec3 iris = mix(u_irisColor.rgb, u_irisColor2.rgb, smoothstep(0.2, 0.95, rr)) * fibers;
+ iris *= 1.0 - u_limbal * smoothstep(0.78, 1.0, rr);
+ iris *= smoothstep(u_pupilSize - 0.05, u_pupilSize + 0.03, rr);
+ vec4 painted = texture(u_textures[0], uv) * v_color * u_tint;
+ vec3 albedo = mix(u_scleraColor.rgb, iris, irisMask) * painted.rgb;
+
+ float ao = 1.0;
+ float lidShade = lid * (1.0 - u_lidShadow * smoothstep(u_lidStart, 1.0, Ng.y));
+ vec3 Nd = normalize(mix(Ng, Ndome, 0.5 * surfaceMask));
+ SurfaceExt body = surfaceExtNone(Nd);
+ body.scatter = vec4(vec3(0.9, 0.5, 0.4) * albedo, u_scleraWrap * (1.0 - surfaceMask));
+ vec3 diffuse = applyLightingSurface(albedo, Nd, P, viewDir, 0.0, 1.0, 0.0, ao, body);
+
+ // The cornea: a clear dome, so its normal is the dome's, and it shows every light and the sky as a small bright shape.
+ vec3 Nc = normalize(mix(Ng, Ndome, surfaceMask));
+ SurfaceExt shine = surfaceExtNone(Nc);
+ shine.coat = vec4(0.25 * (1.0 - surfaceMask), u_scleraRoughness, 0.0, 0.0);
+ shine.coatN = vec4(Ng, 0.0);
+ vec3 spec = applyLightingSurface(vec3(0.0), Nc, P, viewDir, 0.0, u_corneaRoughness, u_specular, ao, shine);
+
+ fragColor = vec4((diffuse + spec) * lidShade, 1.0);
+#ifdef ES_GBUFFER
+ fragSurface = vec4(packNormal(normalize(Nc)), u_corneaRoughness, 0.75);
+#endif
+}
+#pragma end
+
+#pragma fragment wgsl
+#ifdef ES_GBUFFER
+struct FragOut {
+ @location(0) color : vec4f,
+ @location(1) surface : vec4f,
+};
+
+fn packNormal(n : vec3f) -> vec2f {
+ let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
+ let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
+ var e = a.xy;
+ if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
+ return e * 0.5 + 0.5;
+}
+
+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
+#else
+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
+#endif
+ g_probeSlot = v.v_probeSlot;
+#ifdef MESH_LIGHTMAP
+ g_lightmap = v.v_lightmap;
+#endif
+#ifdef MESH_TANGENTS
+ g_worldTangent = v.v_worldTangent;
+#endif
+ let uv = v.v_texCoord;
+ // Every sample is taken here, before any branch.
+ let h = textureSample(u_depthMap, u_depthMap_s, uv).r;
+ let lid = textureSample(u_lidMap, u_lidMap_s, uv).r;
+ let dome = normalize(textureSample(u_irisNormalMap, u_irisNormalMap_s, uv).xyz * 2.0 - vec3f(1.0));
+#ifdef MESH_NORMALS
+ var Ng = normalize(v.v_worldNormal);
+ let P = v.v_worldXYZ;
+ if (!front) { Ng = -Ng; }
+ let T = perturbNormal(Ng, P, uv, vec3f(1.0, 0.0, 0.0));
+ let B = perturbNormal(Ng, P, uv, vec3f(0.0, 1.0, 0.0));
+ let Ndome = perturbNormal(Ng, P, uv, dome);
+#else
+ let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
+ let Ng = vec3f(0.0, 0.0, 1.0);
+ let T = vec3f(1.0, 0.0, 0.0);
+ let B = vec3f(0.0, 1.0, 0.0);
+ let Ndome = Ng;
+#endif
+ let viewDir = viewDirection(P);
+ let vt = normalize(vec3f(dot(viewDir, T), dot(viewDir, B), max(dot(viewDir, Ng), 0.05)));
+ let R = refract(-vt, dome, 1.0 / mc.u_ior);
+ let uvI = uv + R.xy * (mc.u_irisDepth * h / max(-R.z, 0.2));
+ let m = textureSample(u_depthMap, u_depthMap_s, uvI).r;
+ let irisMask = smoothstep(0.15, 0.85, m);
+ let surfaceMask = smoothstep(0.15, 0.85, h);
+
+ let c = uvI - vec2f(0.5);
+ let rr = length(c) / max(mc.u_irisSize, 1e-3);
+ let ang = atan2(c.y, c.x);
+ let fibers = 0.82 + 0.18 * sin(ang * 70.0 + sin(ang * 13.0 + rr * 9.0) * 2.0);
+ var iris = mix(mc.u_irisColor.rgb, mc.u_irisColor2.rgb, smoothstep(0.2, 0.95, rr)) * fibers;
+ iris = iris * (1.0 - mc.u_limbal * smoothstep(0.78, 1.0, rr));
+ iris = iris * smoothstep(mc.u_pupilSize - 0.05, mc.u_pupilSize + 0.03, rr);
+ let painted = textureSample(t0, s0, uv) * v.v_color * mc.u_tint;
+ let albedo = mix(mc.u_scleraColor.rgb, iris, irisMask) * painted.rgb;
+
+ let ao = 1.0;
+ let lidShade = lid * (1.0 - mc.u_lidShadow * smoothstep(mc.u_lidStart, 1.0, Ng.y));
+ let Nd = normalize(mix(Ng, Ndome, 0.5 * surfaceMask));
+ var body = surfaceExtNone(Nd);
+ body.scatter = vec4f(vec3f(0.9, 0.5, 0.4) * albedo, mc.u_scleraWrap * (1.0 - surfaceMask));
+ let diffuse = applyLightingSurface(albedo, Nd, P, viewDir, 0.0, 1.0, 0.0, ao, body);
+
+ let Nc = normalize(mix(Ng, Ndome, surfaceMask));
+ var shine = surfaceExtNone(Nc);
+ shine.coat = vec4f(0.25 * (1.0 - surfaceMask), mc.u_scleraRoughness, 0.0, 0.0);
+ shine.coatN = vec4f(Ng, 0.0);
+ let spec = applyLightingSurface(vec3f(0.0), Nc, P, viewDir, 0.0, mc.u_corneaRoughness, mc.u_specular, ao, shine);
+
+#ifdef ES_GBUFFER
+ var frag : FragOut;
+ frag.color = vec4f((diffuse + spec) * lidShade, 1.0);
+ frag.surface = vec4f(packNormal(normalize(Nc)), mc.u_corneaRoughness, 0.75);
+ return frag;
+#else
+ return vec4f((diffuse + spec) * lidShade, 1.0);
+#endif
+}
+#pragma end
+`;
+
+export const CHARACTER_SHADER_TEMPLATES = [
+ makeBuiltinShaderTemplate('hair', 'Hair',
+ 'Strand lighting: Kajiya-Kay diffuse, two shifted highlights along a flow map, and light through the strands.', HAIR),
+ makeBuiltinShaderTemplate('skin', 'Skin',
+ 'Soft diffuse by colour, a warm terminator, light through thin places, two shine lobes and a fuzz — without blurring the picture.', SKIN),
+ makeBuiltinShaderTemplate('eye', 'Eye',
+ 'A wet cornea over an iris that moves as the eye turns: refracted depth, a pupil, a dark limbal ring, and catchlights of every light.', EYE),
+];
diff --git a/sdk/src/character/characterSupport.ts b/sdk/src/character/characterSupport.ts
new file mode 100644
index 000000000..546c569c8
--- /dev/null
+++ b/sdk/src/character/characterSupport.ts
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file characterSupport.ts — what a build that ships the character shaders tells the core.
+ *
+ * A call, not a file's side effect — see tilemap/tilemapSupport.ts.
+ */
+import { registerBuiltinShaderTemplate } from '../render/builtinShaders';
+import { CHARACTER_SHADER_TEMPLATES } from './characterShaders';
+
+export function registerCharacterShaders(): void {
+ for (const template of CHARACTER_SHADER_TEMPLATES) registerBuiltinShaderTemplate(template);
+}
diff --git a/sdk/src/character/index.ts b/sdk/src/character/index.ts
new file mode 100644
index 000000000..13705e93e
--- /dev/null
+++ b/sdk/src/character/index.ts
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * The `esengine/character-shaders` subpath. Importing it INSTALLS the Hair, Skin and Eye built-in shaders a character model's materials use. A package whose content
+ * uses none of them imports nothing and carries none.
+ */
+import { registerCharacterShaders } from './characterSupport';
+
+registerCharacterShaders();
+
+export { CHARACTER_SHADER_TEMPLATES } from './characterShaders';
diff --git a/sdk/src/foliage/foliageShader.ts b/sdk/src/foliage/foliageShader.ts
new file mode 100644
index 000000000..cd4504780
--- /dev/null
+++ b/sdk/src/foliage/foliageShader.ts
@@ -0,0 +1,199 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file foliageShader.ts
+ * @brief The Foliage built-in shader: plants that sway in the wind.
+ *
+ * The shader source lives with the module that installs it, not in the core's list of built-in shaders: a package that does not install the module carries none of it.
+ */
+import { makeBuiltinShaderTemplate } from '../render/builtinShaders';
+
+
+// Plants that sway: the vertex hook bends by the weights the Foliage component paints in the vertex colour (red the height up the plant,
+// green its own phase, blue how much light reaches it), with a travelling gust and a flutter. Two-sided and translucent.
+const FOLIAGE = `#pragma shader "Foliage"
+#pragma version 300 es
+#pragma domain Lit
+#pragma param u_baseColor color default(0.14,0.30,0.08,1)
+#pragma param u_tipColor color default(0.55,0.72,0.25,1)
+// A texture over the plant (leaves, bark), and whether it is used at all: grass is drawn from the two colours and wants none.
+#pragma param u_albedo texture default(white)
+#pragma param u_useTexture float default(0) range(0,1) ui(slider)
+#pragma param u_alphaCutoff float default(0.5) range(0,1) ui(slider)
+#pragma coverage u_alphaCutoff
+#pragma param u_roughness float default(0.8) range(0,1) ui(slider)
+// Light that comes through a leaf from behind, and how thick the leaf is.
+#pragma param u_translucency float default(0.5) range(0,1) ui(slider)
+#pragma param u_thickness float default(0.3) range(0,1) ui(slider)
+// The wind: how hard it blows, how fast it moves, where it comes from (degrees), how long a gust is (the lower, the longer), and how much flutter rides on it.
+#pragma param u_windStrength float default(2) range(0,20) ui(slider)
+#pragma param u_windSpeed float default(1.2) range(0,6) ui(slider)
+#pragma param u_windDirection float default(40) range(0,360) ui(slider)
+#pragma param u_gustScale float default(0.02) range(0.001,0.5) ui(slider)
+#pragma param u_flutter float default(0.4) range(0,4) ui(slider)
+#pragma vertexbounds 30
+#pragma vertexoffset
+void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
+ float weight = es_vertexColor.r;
+ float phase = es_vertexColor.g * 6.2831853;
+ vec3 anchor = (esInstanceModel() * vec4(local, 1.0)).xyz;
+ float angle = radians(u_windDirection);
+ vec2 dir = vec2(sin(angle), cos(angle));
+ float t = u_time.x * u_windSpeed;
+ float gust = sin(dot(anchor.xz, dir) * u_gustScale - t * 1.3) * 0.5 + 0.5;
+ float flutter = sin(t * 3.1 + phase + anchor.x * 0.7) * u_flutter;
+ float sway = (gust + 0.25 * sin(t + phase)) * u_windStrength + flutter * weight;
+ float bend = weight * weight * sway;
+ local.xz += dir * bend;
+ // The length is kept: what the sideways bend takes from the height it gives back by lowering the tip.
+ local.y -= abs(bend) * 0.35 * weight;
+}
+#pragma end
+
+#pragma vertexoffset wgsl
+fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, vec3f>, uv : vec2f) {
+ let weight = es_vertexColor.r;
+ let phase = es_vertexColor.g * 6.2831853;
+ let anchor = (esInstanceModel() * vec4f(*local, 1.0)).xyz;
+ let angle = radians(mc.u_windDirection);
+ let dir = vec2f(sin(angle), cos(angle));
+ let t = tc.u_time.x * mc.u_windSpeed;
+ let gust = sin(dot(anchor.xz, dir) * mc.u_gustScale - t * 1.3) * 0.5 + 0.5;
+ let flutter = sin(t * 3.1 + phase + anchor.x * 0.7) * mc.u_flutter;
+ let sway = (gust + 0.25 * sin(t + phase)) * mc.u_windStrength + flutter * weight;
+ let bend = weight * weight * sway;
+ (*local).x = (*local).x + dir.x * bend;
+ (*local).z = (*local).z + dir.y * bend;
+ (*local).y = (*local).y - abs(bend) * 0.35 * weight;
+}
+#pragma end
+
+#pragma fragment
+precision highp float;
+
+in vec4 v_color;
+in vec2 v_texCoord;
+in highp vec2 v_worldPos;
+#ifdef MESH_NORMALS
+in highp vec3 v_worldNormal;
+in highp vec3 v_worldXYZ;
+#endif
+
+#ifdef ES_GBUFFER
+layout(location = 0) out vec4 fragColor;
+layout(location = 1) out vec4 fragSurface;
+
+vec2 packNormal(vec3 n) {
+ n /= (abs(n.x) + abs(n.y) + abs(n.z));
+ vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
+ vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
+ return e * 0.5 + 0.5;
+}
+#else
+out vec4 fragColor;
+#endif
+
+void main() {
+#ifdef MESH_NORMALS
+ highp vec3 P = v_worldXYZ;
+ highp vec3 N = normalize(v_worldNormal);
+ if (!gl_FrontFacing) N = -N;
+#else
+ highp vec3 P = vec3(v_worldPos, 0.0);
+ highp vec3 N = vec3(0.0, 0.0, 1.0);
+#endif
+ float height = clamp(v_color.r, 0.0, 1.0);
+ float light = v_color.b;
+ vec4 tex = texture(u_albedo, v_texCoord);
+ vec3 albedo = mix(u_baseColor.rgb, u_tipColor.rgb, height);
+ float alpha = 1.0;
+ if (u_useTexture > 0.5) {
+ albedo *= tex.rgb;
+ alpha = tex.a;
+ alpha = (alpha - u_alphaCutoff) / max(fwidth(alpha), 1e-4) + 0.5;
+ if (alpha <= 0.0) discard;
+ alpha = min(alpha, 1.0);
+ }
+ albedo *= light;
+ SurfaceExt ext = surfaceExtNone(N);
+ if (u_translucency > 0.0) {
+ ext.scatter = vec4(albedo * 1.3, u_translucency * 0.5);
+ ext.trans = vec4(u_translucency, u_thickness, 0.0, 0.0);
+ }
+ vec3 viewDir = viewDirection(P);
+ vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, u_roughness, 0.5, 1.0, ext);
+ fragColor = vec4(lit, alpha);
+#ifdef ES_GBUFFER
+ fragSurface = vec4(packNormal(N), u_roughness, 0.75);
+#endif
+}
+#pragma end
+
+#pragma fragment wgsl
+#ifdef ES_GBUFFER
+struct FragOut {
+ @location(0) color : vec4f,
+ @location(1) surface : vec4f,
+};
+
+fn packNormal(n : vec3f) -> vec2f {
+ let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
+ let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
+ var e = a.xy;
+ if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
+ return e * 0.5 + 0.5;
+}
+
+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
+#else
+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
+#endif
+ // Taken before any branch: a derivative needs every fragment of its quad.
+ let duvx = dpdx(v.v_texCoord);
+ let duvy = dpdy(v.v_texCoord);
+ g_probeSlot = v.v_probeSlot;
+#ifdef MESH_LIGHTMAP
+ g_lightmap = v.v_lightmap;
+#endif
+ let tex = textureSampleGrad(u_albedo, u_albedo_s, v.v_texCoord, duvx, duvy);
+ let edge = max(fwidth(tex.a), 1e-4);
+#ifdef MESH_NORMALS
+ let P = v.v_worldXYZ;
+ var N = normalize(v.v_worldNormal);
+ if (!front) { N = -N; }
+#else
+ let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
+ let N = vec3f(0.0, 0.0, 1.0);
+#endif
+ let height = clamp(v.v_color.r, 0.0, 1.0);
+ let light = v.v_color.b;
+ var albedo = mix(mc.u_baseColor.rgb, mc.u_tipColor.rgb, vec3f(height));
+ var alpha = 1.0;
+ if (mc.u_useTexture > 0.5) {
+ albedo = albedo * tex.rgb;
+ alpha = (tex.a - mc.u_alphaCutoff) / edge + 0.5;
+ if (alpha <= 0.0) { discard; }
+ alpha = min(alpha, 1.0);
+ }
+ albedo = albedo * light;
+ var ext = surfaceExtNone(N);
+ if (mc.u_translucency > 0.0) {
+ ext.scatter = vec4f(albedo * 1.3, mc.u_translucency * 0.5);
+ ext.trans = vec4f(mc.u_translucency, mc.u_thickness, 0.0, 0.0);
+ }
+ let viewDir = viewDirection(P);
+ let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, mc.u_roughness, 0.5, 1.0, ext);
+#ifdef ES_GBUFFER
+ var frag : FragOut;
+ frag.color = vec4f(lit, alpha);
+ frag.surface = vec4f(packNormal(N), mc.u_roughness, 0.75);
+ return frag;
+#else
+ return vec4f(lit, alpha);
+#endif
+}
+#pragma end
+`;
+
+export const FOLIAGE_SHADER_TEMPLATE = makeBuiltinShaderTemplate('foliage', 'Foliage',
+ 'Plants that sway in the wind: a vertex bend by painted weights, two-sided and translucent, with an optional cutout texture.', FOLIAGE);
diff --git a/sdk/src/foliage/foliageSupport.ts b/sdk/src/foliage/foliageSupport.ts
index 77242223a..82542094c 100644
--- a/sdk/src/foliage/foliageSupport.ts
+++ b/sdk/src/foliage/foliageSupport.ts
@@ -6,8 +6,11 @@
* A call, not a file's side effect — see tilemap/tilemapSupport.ts.
*/
import { addEntryPlugin } from '../runtime/entryPlugins';
+import { registerBuiltinShaderTemplate } from '../render/builtinShaders';
+import { FOLIAGE_SHADER_TEMPLATE } from './foliageShader';
import { FoliagePlugin } from './foliagePlugin';
export function registerFoliageSupport(): void {
+ registerBuiltinShaderTemplate(FOLIAGE_SHADER_TEMPLATE);
addEntryPlugin('foliage', () => new FoliagePlugin());
}
diff --git a/sdk/src/platform/nativeModuleRegistry.ts b/sdk/src/platform/nativeModuleRegistry.ts
index cc0ff43f7..0794eaa97 100644
--- a/sdk/src/platform/nativeModuleRegistry.ts
+++ b/sdk/src/platform/nativeModuleRegistry.ts
@@ -32,6 +32,7 @@ import * as FxLightingModule from '../postprocess/fxLighting';
import * as FoliageModule from '../foliage';
import * as WaterModule from '../water';
import * as SplineModule from '../spline';
+import * as CharacterShadersModule from '../character';
import * as ScriptGraphModule from '../logic';
import * as AiModule from '../ai';
import * as ReplicationModule from '../net/replication';
@@ -59,6 +60,7 @@ export const NATIVE_MODULE_NAMESPACES: Readonly<Record<string, unknown>> = {
'esengine/foliage': FoliageModule,
'esengine/water': WaterModule,
'esengine/spline': SplineModule,
+ 'esengine/character-shaders': CharacterShadersModule,
'esengine/logic': ScriptGraphModule,
'esengine/ai': AiModule,
'esengine/replication': ReplicationModule,
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index db37b8a9f..0cab20af3 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -12,7 +12,6 @@
*/
import { paramDefaultValue, reflectEsshader } from './shaderReflect';
-import { CAUSTICS_GLSL, CAUSTICS_WGSL } from './waterCaustics';
export interface BuiltinShaderTemplate {
id: string;
@@ -46,8 +45,9 @@ function templateDefaults(source: string): Record<string, unknown> {
return out;
}
-const template = (id: string, label: string, description: string, source: string): BuiltinShaderTemplate =>
+export const makeBuiltinShaderTemplate = (id: string, label: string, description: string, source: string): BuiltinShaderTemplate =>
({ id, label, description, source, defaults: templateDefaults(source) });
+const template = makeBuiltinShaderTemplate;
const SPRITE_UNLIT = `#pragma shader "Sprite Unlit"
#pragma version 300 es
@@ -693,1676 +693,6 @@ fn packNormal(n : vec3f) -> vec2f {
#pragma end
`;
-// A horizontal body of water: waves that travel, the scene seen through it bent and tinted by how much water lies in front of it, the sky
-// and the lights reflected off it, and foam where it meets what it lies on and on the crests. It reads the picture of the opaque scene
-// behind it and how far each pixel of that is from the eye, which the frame takes before it draws a material that declares them, so
-// the depth of the water at a pixel is what is measured, not a number the author guessed. Where the device cannot hold the distances
-// (no float target) it falls back to `u_fallbackDepth` as the depth everywhere.
-const WATER = `#pragma shader "Water"
-#pragma version 300 es
-#pragma domain Lit
-// The colour the water scatters back: lighter where it is shallow, darker where it is deep.
-#pragma param u_shallowColor color default(0.10,0.42,0.46,1)
-#pragma param u_deepColor color default(0.01,0.08,0.15,1)
-// How fast each colour is absorbed along the way through the water (red goes first), and how thick the water is, per world unit.
-#pragma param u_absorption color default(0.45,0.09,0.05,1)
-#pragma param u_density float default(0.12) range(0,4) ui(slider)
-// The waves: height, length of the longest, how sharp their crests are drawn, how fast they travel and which way (degrees).
-#pragma param u_waveHeight float default(0.3) range(0,8) ui(slider)
-#pragma param u_waveLength float default(14) range(0.5,300) ui(slider)
-#pragma param u_waveSpeed float default(1) range(0,4) ui(slider)
-#pragma param u_waveDirection float default(30) range(0,360) ui(slider)
-// The small ripples the long waves carry: how steep and how long each is.
-#pragma param u_rippleStrength float default(0.3) range(0,1) ui(slider)
-#pragma param u_rippleLength float default(2.5) range(0.1,40) ui(slider)
-#pragma param u_roughness float default(0.04) range(0,1) ui(slider)
-// How far what is seen through the water is shifted by its waves, and the depth of water at which that stops growing.
-#pragma param u_refractionStrength float default(1) range(0,4) ui(slider)
-#pragma param u_distortionRange float default(12) range(0.1,200) ui(slider)
-// Foam: its colour, how deep the water is where it begins at the shore (world units), and how much of the wave crests carry it.
-#pragma param u_foamColor color default(0.95,0.97,1,1)
-#pragma param u_foamWidth float default(1.2) range(0,20) ui(slider)
-#pragma param u_foamCrest float default(0.5) range(0,1) ui(slider)
-// The depth taken where the device could not measure one.
-#pragma param u_fallbackDepth float default(4) range(0,200) ui(slider)
-// Where the waves are measured. 0 is the world's xz plane, which is what a flat plane of water wants. 1 is the body's own space: the mesh's uv is its
-// local x and z (plus u_origin, which a body that follows the camera moves so the waves stay put), and the waves then lift the geometry and follow the
-// body's tilt, which is what the WaterBody component builds.
-#pragma param u_waveSpace float default(0) range(0,1) ui(slider)
-#pragma param u_origin vec2 default(0,0)
-// The cell size of the mesh's finest ring, for a body the WaterBody component built: a vertex in a coarser ring (its colour's red says which) fades the
-// waves it cannot draw, those shorter than six of its cells. 0 draws every wave at every vertex.
-#pragma param u_cellSize float default(0) range(0,100) ui(slider)
-// A lake's shore, baked by the WaterBody component over its box: red the signed distance to its outline (0.5 at the edge), green how deep the water is,
-// blue the share of the waves kept, all over u_shoreRange. The pixels outside the outline are cut, the waves die toward the shore, and it foams there.
-#pragma param u_shoreOn float default(0) range(0,1) ui(slider)
-#pragma param u_shoreMap texture default(white) texel(data)
-#pragma param u_shoreOrigin vec2 default(0,0)
-#pragma param u_shoreSize vec2 default(1,1)
-#pragma param u_shoreRange float default(16) range(0.01,10000) ui(slider)
-// A river's current: how fast it runs (world units a second, 0 for still water) along the direction the mesh's vertex colour gives (red and green) at the speed
-// its blue says (half is the nominal), and how long, in seconds, each of the two copies of the surface the shader carries downstream lives before the other takes over.
-#pragma param u_flowSpeed float default(0) range(0,50) ui(slider)
-#pragma param u_flowCycle float default(2) range(0.2,20) ui(slider)
-// How much of the waves lifts the geometry: 1 for a lake or an ocean, 0 for a river, whose surface is drawn flat and moved only in the shading.
-#pragma param u_geoWaves float default(1) range(0,1) ui(slider)
-// The net of light the waves throw on what is under the water: how strong it is, how large one cell of it is (world units), and how fast it swims. The Underwater
-// effect draws the same one from below, with these numbers, so the two halves of the picture agree.
-#pragma param u_causticStrength float default(0.6) range(0,3) ui(slider)
-#pragma param u_causticScale float default(2) range(0.05,2000) ui(slider)
-#pragma param u_causticSpeed float default(0.35) range(0,4) ui(slider)
-// What the quality level allows (the WaterBody component writes these): how many of the four waves are summed, whether the surface seen from below mirrors the world under it,
-// and whether the sun's web of light is drawn on what lies under the water.
-#pragma param u_waveCount float default(4) range(1,4) ui(slider)
-#pragma param u_mirror float default(1) range(0,1) ui(slider)
-#pragma param u_causticOn float default(1) range(0,1) ui(slider)
-// The disturbance the water keeps (WaterBody ripples): a window of the body's xz plane, red the height of the ripples (0.5 at rest, u_rippleRange above or below at 1 and 0)
-// and green the foam they left, as the CPU stepped them. u_rippleSize is the window's side, u_rippleTexel one texel of it.
-#pragma param u_rippleOn float default(0) range(0,1) ui(slider)
-#pragma param u_rippleMap texture default(white) texel(data)
-#pragma param u_rippleOrigin vec2 default(0,0)
-#pragma param u_rippleSize float default(64) range(0.01,100000) ui(slider)
-#pragma param u_rippleTexel float default(0.0156) range(0,1) ui(slider)
-#pragma param u_rippleRange float default(1) range(0.0001,1000) ui(slider)
-#pragma param u_sceneColor texture default(white)
-#pragma param u_sceneDepthMap texture default(white) texel(data)
-
-#pragma vertexbounds 16
-#pragma vertexoffset
-// The height of the waves at p: the same four peaked sines the fragment takes its slope from, so the surface that is lit is the one that is drawn.
-float waterWaveHeight(vec2 p, float time, float cell) {
- const vec4 SET[4] = vec4[4](vec4(0.0, 1.0, 1.0, 1.0), vec4(28.0, 0.62, 0.55, 1.1), vec4(-37.0, 0.37, 0.3, 1.2), vec4(71.0, 0.21, 0.15, 1.35));
- float height = 0.0;
- float base = radians(u_waveDirection);
- for (int i = 0; i < 4; ++i) {
- if (float(i) >= u_waveCount) break;
- float length_ = u_waveLength * SET[i].y;
- float k = 6.2831853 / length_;
- float a = base + radians(SET[i].x);
- vec2 d = vec2(sin(a), cos(a));
- float omega = sqrt(9.8 * k) * u_waveSpeed * SET[i].w;
- float fade = cell > 0.0 ? clamp((length_ / cell - 3.0) / 3.0, 0.0, 1.0) : 1.0;
- height += u_waveHeight * SET[i].z * exp(sin(k * dot(d, p) - omega * time) - 1.0) * fade;
- }
- return height;
-}
-void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
- if (u_waveSpace < 0.5) return;
- float cell = u_cellSize > 0.0 ? u_cellSize * exp2(es_vertexColor.r * 7.0) : 0.0;
- float damp = u_shoreOn > 0.5 ? es_vertexColor.g : 1.0;
- local.y += waterWaveHeight(uv + u_origin, u_time.x, cell) * damp * u_geoWaves;
-}
-#pragma end
-
-#pragma vertexoffset wgsl
-fn waterWaveHeight(p : vec2f, time : f32, cell : f32) -> f32 {
- var wave = array<vec4f, 4>(vec4f(0.0, 1.0, 1.0, 1.0), vec4f(28.0, 0.62, 0.55, 1.1), vec4f(-37.0, 0.37, 0.3, 1.2), vec4f(71.0, 0.21, 0.15, 1.35));
- var height = 0.0;
- let base = radians(mc.u_waveDirection);
- for (var i : i32 = 0; i < 4; i = i + 1) {
- if (f32(i) >= mc.u_waveCount) { break; }
- let spec = wave[i];
- let span = mc.u_waveLength * spec.y;
- let k = 6.2831853 / span;
- let a = base + radians(spec.x);
- let d = vec2f(sin(a), cos(a));
- let omega = sqrt(9.8 * k) * mc.u_waveSpeed * spec.w;
- var fade = 1.0;
- if (cell > 0.0) { fade = clamp((span / cell - 3.0) / 3.0, 0.0, 1.0); }
- height = height + mc.u_waveHeight * spec.z * exp(sin(k * dot(d, p) - omega * time) - 1.0) * fade;
- }
- return height;
-}
-fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, vec3f>, uv : vec2f) {
- if (mc.u_waveSpace < 0.5) { return; }
- var cell = 0.0;
- if (mc.u_cellSize > 0.0) { cell = mc.u_cellSize * exp2(es_vertexColor.r * 7.0); }
- var damp = 1.0;
- if (mc.u_shoreOn > 0.5) { damp = es_vertexColor.g; }
- (*local).y = (*local).y + waterWaveHeight(uv + mc.u_origin, tc.u_time.x, cell) * damp * mc.u_geoWaves;
-}
-#pragma end
-
-#pragma fragment
-precision highp float;
-
-in vec4 v_color;
-in vec2 v_texCoord;
-in highp vec2 v_worldPos;
-#ifdef MESH_NORMALS
-in highp vec3 v_worldNormal;
-in highp vec3 v_worldXYZ;
-#endif
-
-#ifdef ES_GBUFFER
-layout(location = 0) out vec4 fragColor;
-layout(location = 1) out vec4 fragSurface;
-
-vec2 packNormal(vec3 n) {
- n /= (abs(n.x) + abs(n.y) + abs(n.z));
- vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
- vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
- return e * 0.5 + 0.5;
-}
-#else
-out vec4 fragColor;
-#endif
-
-float waterHash(vec2 p) {
- p = fract(p * vec2(0.1031, 0.1030));
- p += dot(p, p.yx + 33.33);
- return fract((p.x + p.y) * p.x);
-}
-float waterNoise(vec2 p) {
- vec2 i = floor(p);
- vec2 f = fract(p);
- f = f * f * (3.0 - 2.0 * f);
- return mix(mix(waterHash(i), waterHash(i + vec2(1.0, 0.0)), f.x),
- mix(waterHash(i + vec2(0.0, 1.0)), waterHash(i + vec2(1.0, 1.0)), f.x), f.y);
-}
-/// The ripples' height at p: two layers of noise drifting in different directions.
-float ripples(vec2 p, float time) {
- return waterNoise(p / u_rippleLength + vec2(0.31, 0.12) * time) * 0.6
- + waterNoise(p / (u_rippleLength * 0.43) - vec2(0.17, 0.4) * time) * 0.4;
-}
-
-/// The slope of the water's surface at p (d height / d x, d height / d z) and how near the waves are to a crest, 0 to 1.
-/// Four waves a few directions apart, each a peaked sine (exp(sin - 1)) whose crests are narrower than its troughs.
-vec2 waveSlope(vec2 p, float time, float dist, out float crest) {
- const vec4 SET[4] = vec4[4](vec4(0.0, 1.0, 1.0, 1.0), vec4(28.0, 0.62, 0.55, 1.1), vec4(-37.0, 0.37, 0.3, 1.2), vec4(71.0, 0.21, 0.15, 1.35));
- vec2 slope = vec2(0.0);
- float crestSum = 0.0;
- float weightSum = 0.0;
- float base = radians(u_waveDirection);
- for (int i = 0; i < 4; ++i) {
- if (float(i) >= u_waveCount) break;
- float length_ = u_waveLength * SET[i].y;
- float k = 6.2831853 / length_;
- float a = base + radians(SET[i].x);
- vec2 d = vec2(sin(a), cos(a));
- float omega = sqrt(9.8 * k) * u_waveSpeed * SET[i].w;
- float theta = k * dot(d, p) - omega * time;
- float e = exp(sin(theta) - 1.0);
- // A wave much shorter than a pixel is noise, not a wave: it fades out with distance.
- float seen = clamp(1.0 - dist / (length_ * 220.0), 0.0, 1.0);
- slope += d * (u_waveHeight * SET[i].z * e * cos(theta) * k * seen);
- crestSum += e * SET[i].z;
- weightSum += SET[i].z;
- }
- crest = crestSum / weightSum;
- return slope;
-}
-
-${CAUSTICS_GLSL}
-/// The first directional light that shines: xyz is where it stands in the sky, w 1 if there is one, color its colour and strength, tiles its place in the shadow atlas.
-void waterSun(out vec4 dir, out vec4 color, out ivec2 tiles) {
- dir = vec4(0.0);
- color = vec4(0.0);
- tiles = ivec2(0);
- for (int i = 0; i < 16; ++i) {
- Light l = u_lights[i];
- if (l.posDir.z > 0.5 && l.posDir.z < 1.5 && l.color.a > 0.0) {
- dir = vec4(-normalize(vec3(l.posDir.x, l.posDir.y, l.posDir.w)), 1.0);
- color = l.color;
- tiles = ivec2(int(l.shadowMap.x), int(l.shadowMap.y));
- return;
- }
- }
-}
-
-/// Whether the sun reaches a point: one tap of the shadow map, 1 where there is none.
-float waterSunVisibility(vec3 worldPos, int first, int count) {
-#ifdef ES_RECEIVE_SHADOW
- if (u_shadowParams.x < 0.5 || count <= 0) return 1.0;
- for (int k = 0; k < 16; ++k) {
- if (k >= count) break;
- int t = first + k;
- vec4 c = u_shadowMatrix[t] * vec4(worldPos, 1.0);
- if (c.w <= 0.0) continue;
- vec3 proj = c.xyz / c.w;
- vec2 uv = proj.xy * 0.5 + 0.5;
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z < 0.0 || proj.z > 1.0) continue;
- vec4 rect = u_shadowTile[t];
- vec2 at = rect.xy + uv * rect.z;
- vec2 lo = rect.xy + u_shadowParams.y * 0.5;
- vec2 hi = rect.xy + rect.z - u_shadowParams.y * 0.5;
- float d = unpackDepth(textureLod(u_shadowMap, clamp(at, lo, hi), 0.0).rgb);
- return d < clamp(proj.z * 0.5 + 0.5, 0.0, 1.0) - 0.0025 ? 0.0 : 1.0;
- }
-#endif
- return 1.0;
-}
-
-/// What a ray from p along r meets in the picture of the scene the frame copied, as a colour with alpha 1, or alpha 0 where it meets nothing on screen.
-vec4 waterReflect(vec3 p, vec3 r, float eyeDist) {
- float stride = max(0.5, eyeDist * 0.03);
- float travelled = stride;
- for (int i = 0; i < 20; ++i) {
- vec3 q = p + r * travelled;
- vec4 clip = u_projection * vec4(q, 1.0);
- if (clip.w <= 1e-4) break;
- vec2 uv = clip.xy / clip.w * 0.5 + 0.5;
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) break;
- float qDist = u_camera.w > 0.5 ? length(q - u_camera.xyz) : -dot(q, u_camera.xyz);
- float sceneDist = textureLod(u_sceneDepthMap, uv, 0.0).r;
- // The ray has gone behind something the picture holds: that is what it met, if it did not skip clean through a thin one.
- if (sceneDist < qDist && qDist - sceneDist < stride * 2.0 + 1.0) return vec4(textureLod(u_sceneColor, uv, 0.0).rgb, 1.0);
- stride *= 1.25;
- travelled += stride;
- }
- return vec4(0.0);
-}
-
-/// The slope of the surface at p and how near it is to a crest: the waves, and the ripples they carry, flattened with distance.
-vec2 waterSurface(vec2 p, float time, float dist, out float crest) {
- vec2 slope = waveSlope(p, time, dist, crest);
- float rippleFade = clamp(1.0 - dist / (u_rippleLength * 180.0), 0.0, 1.0);
- float e = u_rippleLength * 0.1;
- vec2 ripple = vec2(ripples(p + vec2(e, 0.0), time) - ripples(p - vec2(e, 0.0), time),
- ripples(p + vec2(0.0, e), time) - ripples(p - vec2(0.0, e), time)) / (2.0 * e);
- return slope + ripple * (u_rippleStrength * u_rippleLength * rippleFade);
-}
-
-void main() {
-#ifdef MESH_NORMALS
- highp vec3 P = v_worldXYZ;
-#else
- highp vec3 P = vec3(v_worldPos.x, 0.0, v_worldPos.y);
-#endif
- float time = u_time.x;
- vec3 viewDir = viewDirection(P);
- float waterDist = u_camera.w > 0.5 ? length(P - u_camera.xyz) : -dot(P, u_camera.xyz);
-
- // The surface: the long waves, then the ripples they carry, flattened with distance so the far water is calm rather than aliased.
- vec2 wp = u_waveSpace > 0.5 ? v_texCoord + u_origin : P.xz;
- // The lake's shore at this pixel (a texture read, taken before any branch).
- vec4 shoreTex = texture(u_shoreMap, clamp((v_texCoord - u_shoreOrigin) / max(u_shoreSize, vec2(1e-4)), vec2(0.0), vec2(1.0)));
- float shoreDist = (shoreTex.r - 0.5) * 2.0 * u_shoreRange;
- float shoreDepth = shoreTex.g * u_shoreRange;
- float damp = u_shoreOn > 0.5 ? shoreTex.b : 1.0;
- // The ripples at this pixel: their height on four sides gives the slope they add, and the foam is what they left (reads taken before any branch).
- vec2 ripUv = (wp - u_rippleOrigin) / max(u_rippleSize, 1e-4);
- vec4 ripC = texture(u_rippleMap, ripUv);
- vec2 ripStep = vec2(u_rippleTexel);
- float ripCell = max(u_rippleSize * u_rippleTexel, 1e-5);
- float ripL = texture(u_rippleMap, ripUv - vec2(ripStep.x, 0.0)).r, ripR = texture(u_rippleMap, ripUv + vec2(ripStep.x, 0.0)).r;
- float ripD = texture(u_rippleMap, ripUv - vec2(0.0, ripStep.y)).r, ripU = texture(u_rippleMap, ripUv + vec2(0.0, ripStep.y)).r;
- bool ripIn = u_rippleOn > 0.5 && u_waveSpace > 0.5 && all(greaterThanEqual(ripUv, vec2(0.0))) && all(lessThanEqual(ripUv, vec2(1.0)));
- vec2 ripSlope = ripIn ? vec2(ripR - ripL, ripU - ripD) * u_rippleRange / ripCell : vec2(0.0);
- float ripFoam = ripIn ? ripC.g : 0.0;
- float crest;
- vec2 slope;
- if (u_flowSpeed > 0.0) {
- // A river: the surface rides the current. Two copies of it are carried downstream a cycle at a time, each fading in as the other fades
- // out, so the moment one is put back at its start is hidden by the other.
- vec2 flow = (v_color.rg * 2.0 - 1.0) * (v_color.b * 2.0) * u_flowSpeed;
- float cycle = max(u_flowCycle, 0.1);
- float ph0 = fract(time / cycle);
- float ph1 = fract(time / cycle + 0.5);
- float w0 = 1.0 - abs(2.0 * ph0 - 1.0);
- float crest0, crest1;
- vec2 s0 = waterSurface(wp - flow * (ph0 * cycle), time, waterDist, crest0);
- vec2 s1 = waterSurface(wp - flow * (ph1 * cycle), time, waterDist, crest1);
- slope = mix(s1, s0, w0);
- crest = mix(crest1, crest0, w0);
- } else {
- slope = waterSurface(wp, time, waterDist, crest);
- }
- slope *= damp;
- crest *= damp;
- slope += ripSlope;
- vec3 N = normalize(vec3(-slope.x, 1.0, -slope.y));
- vec3 Ngeo = vec3(0.0, 1.0, 0.0);
-#ifdef MESH_NORMALS
- if (u_waveSpace > 0.5) {
- // The body's own frame: the mesh's normal is its up (the hook lifts the vertices and leaves it alone), and the slope leans along
- // its uv axes, which are its local x and z.
- Ngeo = normalize(v_worldNormal);
- N = perturbNormal(Ngeo, P, v_texCoord, normalize(vec3(-slope.x, -slope.y, 1.0)));
- }
-#endif
- // Seen from below, the surface turns over, and light goes the other way through it: out of the water, into the air.
- bool below = dot(viewDir, Ngeo) < 0.0;
- if (below) { N = -N; Ngeo = -Ngeo; }
-
- // What is behind the water: the picture and the distance, where the frame took them.
- bool haveColor = texture(u_sceneColor, vec2(0.5)).a < 0.75;
- bool haveDepth = haveColor && texture(u_sceneDepthMap, vec2(0.5)).a < 0.75;
- vec4 clip0 = u_projection * vec4(P, 1.0);
- vec2 uv0 = clip0.xy / max(clip0.w, 1e-4) * 0.5 + 0.5;
- float behindDist = haveDepth ? texture(u_sceneDepthMap, clamp(uv0, vec2(0.001), vec2(0.999))).r : waterDist + u_fallbackDepth;
- float column0 = max(behindDist - waterDist, 0.0);
-
- // Bent by the waves: the ray through the tilted surface against the ray through a flat one, over as much water as there is.
- float RATIO = below ? 1.333 : 1.0 / 1.333;
- vec3 bentWave = refract(-viewDir, N, RATIO);
- vec3 bentFlat = refract(-viewDir, Ngeo, RATIO);
- // Past the critical angle (about 49 degrees) nothing gets out of the water: refract gives nothing, and the surface is a mirror.
- if (below && (dot(bentWave, bentWave) < 0.5 || dot(bentFlat, bentFlat) < 0.5)) { bentWave = bentFlat = vec3(0.0); }
- float reach = min(column0, u_distortionRange) * u_refractionStrength;
- vec4 clip1 = u_projection * vec4(P + (bentWave - bentFlat) * reach, 1.0);
- vec2 uv1 = clip1.xy / max(clip1.w, 1e-4) * 0.5 + 0.5;
- vec3 behind = vec3(0.0);
- float column = column0;
- if (haveColor) {
- uv1 = clamp(uv1, vec2(0.001), vec2(0.999));
- float dist1 = haveDepth ? texture(u_sceneDepthMap, uv1).r : behindDist;
- // Something standing in front of the water cannot be what is seen through it: keep the straight-through lookup.
- if (dist1 < waterDist) { uv1 = clamp(uv0, vec2(0.001), vec2(0.999)); dist1 = behindDist; }
- behind = texture(u_sceneColor, uv1).rgb;
- column = max(dist1 - waterDist, 0.0);
- // The sun through the waves lights what is under them in a web of bright threads, where the sun reaches it and as much as the water between lets through.
- if (!below && haveDepth && u_causticStrength > 0.0 && u_causticOn > 0.5) {
- vec4 sunDir, sunColor;
- ivec2 sunTiles;
- waterSun(sunDir, sunColor, sunTiles);
- float cosUp = dot(sunDir.xyz, Ngeo);
- if (sunDir.w > 0.5 && cosUp > 0.05) {
- vec3 seabed = P + bentWave * column;
- float depthBelow = max(dot(P - seabed, Ngeo), 0.0);
- vec3 onSurface = seabed + sunDir.xyz * (depthBelow / max(cosUp, 0.15));
- float c = causticPattern(onSurface.xz / u_causticScale, time * u_causticSpeed);
- float reachSun = exp(-dot(u_absorption.rgb, vec3(1.0 / 3.0)) * u_density * depthBelow / max(cosUp, 0.15));
- behind *= 1.0 + u_causticStrength * c * cosUp * waterSunVisibility(seabed, sunTiles.x, sunTiles.y) * reachSun * min(sunColor.a, 2.0) * 0.5;
- }
- }
- }
-
- // The water in between takes the light out of it, red first, and gives back its own colour: more of it the deeper it goes.
- vec3 transmit = exp(-u_absorption.rgb * (u_density * column));
- // From below the water between the eye and the surface is the camera's own medium (the underwater effect fogs it), not a column behind the surface.
- if (below) transmit = vec3(1.0);
- float murk = 1.0 - dot(transmit, vec3(1.0 / 3.0));
- vec3 scatterColor = mix(u_shallowColor.rgb, u_deepColor.rgb, 1.0 - exp(-column * u_density * 0.5));
-
- // Foam: along the shore, where the water is shallow, broken by noise that moves; and on the crests of the waves.
- float vertical = column * max(abs(viewDir.y), 0.15);
- float foamScale = 1.0 / (u_rippleLength * 1.2);
- float breakup = waterNoise(wp * foamScale + vec2(time * 0.07, time * 0.05)) * 0.6 + waterNoise(wp * foamScale * 3.0 - vec2(time * 0.13, 0.0)) * 0.4;
- float pulse = 0.65 + 0.35 * sin(time * 0.9 + breakup * 6.0);
- float shore = haveDepth && u_foamWidth > 0.0 ? clamp((1.0 - vertical / (u_foamWidth * pulse)) * 1.5 - breakup * 0.8, 0.0, 1.0) : 0.0;
- // The baked shore foams too, where the depth buffer cannot say (no chain, no float targets) and along the lake's outline.
- if (u_shoreOn > 0.5 && u_foamWidth > 0.0) {
- shore = max(shore, clamp((1.0 - min(shoreDist, shoreDepth) / (u_foamWidth * pulse)) * 1.5 - breakup * 0.8, 0.0, 1.0));
- }
- float onCrest = u_foamCrest > 0.0 ? smoothstep(1.0 - 0.3 * u_foamCrest, 1.0, crest) * smoothstep(0.35, 0.75, breakup) : 0.0;
- float foam = clamp(max(max(shore, onCrest * u_foamCrest), ripFoam * (0.55 + 0.45 * breakup)), 0.0, 1.0);
-
- SurfaceExt ext = surfaceExtNone(N);
- ext.misc.x = 0.0204;
- vec3 albedo = mix(scatterColor * murk, u_foamColor.rgb, foam);
- float roughness = mix(u_roughness, 0.7, foam);
- vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 1.0, 1.0, ext);
- float cosV = clamp(dot(N, viewDir), 0.0, 1.0);
- float fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosV, 5.0);
- if (below) {
- // Out of the water the glancing rays are reflected whole: Snell's window, a cone of sky overhead in a mirror of the water below it.
- float sinT = 1.333 * sqrt(max(1.0 - cosV * cosV, 0.0));
- float cosT = sqrt(max(1.0 - sinT * sinT, 0.0));
- fresnel = sinT >= 1.0 ? 1.0 : 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosT, 5.0);
- }
- vec3 seen = behind * transmit * (1.0 - fresnel) * (1.0 - foam);
- // From below the water is a mirror past the critical angle, and what it mirrors is the world under it: the picture, followed along the reflected ray.
- vec3 mirror = vec3(0.0);
- if (below && fresnel > 0.02 && haveColor && haveDepth && u_mirror > 0.5) {
- vec4 met = waterReflect(P, reflect(-viewDir, N), waterDist);
- mirror = met.a > 0.5 ? met.rgb : mix(u_shallowColor.rgb, u_deepColor.rgb, 0.5) * 0.25;
- }
- seen += mirror * fresnel * (1.0 - foam);
- // Outside the lake's outline there is no water.
- if (u_shoreOn > 0.5 && shoreDist < 0.0) discard;
- fragColor = vec4(lit + seen, haveColor ? 1.0 : 0.85);
-#ifdef ES_GBUFFER
- fragSurface = vec4(packNormal(normalize(N)), roughness, 0.75);
-#endif
-}
-#pragma end
-
-#pragma fragment wgsl
-#ifdef ES_GBUFFER
-struct FragOut {
- @location(0) color : vec4f,
- @location(1) surface : vec4f,
-};
-
-fn packNormal(n : vec3f) -> vec2f {
- let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
- let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
- var e = a.xy;
- if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
- return e * 0.5 + 0.5;
-}
-#endif
-
-fn waterHash(p0 : vec2f) -> f32 {
- var p = fract(p0 * vec2f(0.1031, 0.1030));
- p = p + dot(p, p.yx + 33.33);
- return fract((p.x + p.y) * p.x);
-}
-fn waterNoise(p : vec2f) -> f32 {
- let i = floor(p);
- var f = fract(p);
- f = f * f * (3.0 - 2.0 * f);
- return mix(mix(waterHash(i), waterHash(i + vec2f(1.0, 0.0)), f.x),
- mix(waterHash(i + vec2f(0.0, 1.0)), waterHash(i + vec2f(1.0, 1.0)), f.x), f.y);
-}
-fn ripples(p : vec2f, time : f32) -> f32 {
- return waterNoise(p / mc.u_rippleLength + vec2f(0.31, 0.12) * time) * 0.6
- + waterNoise(p / (mc.u_rippleLength * 0.43) - vec2f(0.17, 0.4) * time) * 0.4;
-}
-
-struct Waves { slope : vec2f, crest : f32 };
-
-fn waveSlope(p : vec2f, time : f32, dist : f32) -> Waves {
- var wave = array<vec4f, 4>(vec4f(0.0, 1.0, 1.0, 1.0), vec4f(28.0, 0.62, 0.55, 1.1), vec4f(-37.0, 0.37, 0.3, 1.2), vec4f(71.0, 0.21, 0.15, 1.35));
- var slope = vec2f(0.0);
- var crestSum = 0.0;
- var weightSum = 0.0;
- let base = radians(mc.u_waveDirection);
- for (var i : i32 = 0; i < 4; i = i + 1) {
- if (f32(i) >= mc.u_waveCount) { break; }
- let spec = wave[i];
- let span = mc.u_waveLength * spec.y;
- let k = 6.2831853 / span;
- let a = base + radians(spec.x);
- let d = vec2f(sin(a), cos(a));
- let omega = sqrt(9.8 * k) * mc.u_waveSpeed * spec.w;
- let theta = k * dot(d, p) - omega * time;
- let e = exp(sin(theta) - 1.0);
- let seen = clamp(1.0 - dist / (span * 220.0), 0.0, 1.0);
- slope = slope + d * (mc.u_waveHeight * spec.z * e * cos(theta) * k * seen);
- crestSum = crestSum + e * spec.z;
- weightSum = weightSum + spec.z;
- }
- var out : Waves;
- out.slope = slope;
- out.crest = crestSum / weightSum;
- return out;
-}
-
-${CAUSTICS_WGSL}
-struct WaterSun { dir : vec4f, color : vec4f, tiles : vec2i };
-/// The first directional light that shines: dir.xyz is where it stands in the sky, dir.w 1 if there is one.
-fn waterSun() -> WaterSun {
- var s : WaterSun;
- s.dir = vec4f(0.0);
- s.color = vec4f(0.0);
- s.tiles = vec2i(0, 0);
- for (var i = 0; i < 16; i++) {
- let l = lc.u_lights[i];
- if (l.posDir.z > 0.5 && l.posDir.z < 1.5 && l.color.a > 0.0) {
- s.dir = vec4f(-normalize(vec3f(l.posDir.x, l.posDir.y, l.posDir.w)), 1.0);
- s.color = l.color;
- s.tiles = vec2i(i32(l.shadowMap.x), i32(l.shadowMap.y));
- return s;
- }
- }
- return s;
-}
-
-fn waterSunVisibility(worldPos : vec3f, first : i32, count : i32) -> f32 {
- if (lc.u_shadowParams.x < 0.5 || count <= 0) { return 1.0; }
- for (var k = 0; k < 16; k++) {
- if (k >= count) { break; }
- let t = first + k;
- let c = lc.u_shadowMatrix[t] * vec4f(worldPos, 1.0);
- if (c.w <= 0.0) { continue; }
- let proj = c.xyz / c.w;
- let uv = proj.xy * 0.5 + 0.5;
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z < 0.0 || proj.z > 1.0) { continue; }
- let rect = lc.u_shadowTile[t];
- let base = vec2f(rect.x, 1.0 - rect.y - rect.z);
- let at = base + vec2f(uv.x, 1.0 - uv.y) * rect.z;
- let lo = base + lc.u_shadowParams.y * 0.5;
- let hi = base + rect.z - lc.u_shadowParams.y * 0.5;
- let d = unpackDepth(textureSampleLevel(t2, s2, clamp(at, lo, hi), 0.0).rgb);
- return select(1.0, 0.0, d < clamp(proj.z * 0.5 + 0.5, 0.0, 1.0) - 0.0025);
- }
- return 1.0;
-}
-
-/// What a ray from p along r meets in the picture of the scene the frame copied: the colour (xyz) and 1 (w), or w 0 where it meets nothing on screen.
-fn waterReflect(p : vec3f, r : vec3f, eyeDist : f32) -> vec4f {
- var stride = max(0.5, eyeDist * 0.03);
- var travelled = stride;
- for (var i = 0; i < 20; i++) {
- let q = p + r * travelled;
- let clip = frame.projection * vec4f(q, 1.0);
- if (clip.w <= 1e-4) { break; }
- let uv = vec2f(clip.x / clip.w * 0.5 + 0.5, 0.5 - clip.y / clip.w * 0.5);
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { break; }
- var qDist = -dot(q, frame.camera.xyz);
- if (frame.camera.w > 0.5) { qDist = length(q - frame.camera.xyz); }
- let sceneDist = textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, uv, 0.0).r;
- if (sceneDist < qDist && qDist - sceneDist < stride * 2.0 + 1.0) { return vec4f(textureSampleLevel(u_sceneColor, u_sceneColor_s, uv, 0.0).rgb, 1.0); }
- stride = stride * 1.25;
- travelled = travelled + stride;
- }
- return vec4f(0.0);
-}
-
-/// The slope of the surface at p (x, y) and how near it is to a crest (z): the waves, and the ripples they carry, flattened with distance.
-fn waterSurface(p : vec2f, time : f32, dist : f32) -> vec3f {
- let waves = waveSlope(p, time, dist);
- let rippleFade = clamp(1.0 - dist / (mc.u_rippleLength * 180.0), 0.0, 1.0);
- let eps = mc.u_rippleLength * 0.1;
- let ripple = vec2f(ripples(p + vec2f(eps, 0.0), time) - ripples(p - vec2f(eps, 0.0), time),
- ripples(p + vec2f(0.0, eps), time) - ripples(p - vec2f(0.0, eps), time)) / (2.0 * eps);
- return vec3f(waves.slope + ripple * (mc.u_rippleStrength * mc.u_rippleLength * rippleFade), waves.crest);
-}
-
-#ifdef ES_GBUFFER
-@fragment fn fs_main(v : VSOut) -> FragOut {
-#else
-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
-#endif
- g_probeSlot = v.v_probeSlot;
-#ifdef MESH_LIGHTMAP
- g_lightmap = v.v_lightmap;
-#endif
-#ifdef MESH_NORMALS
- let P = v.v_worldXYZ;
-#else
- let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
-#endif
- let time = tc.u_time.x;
- let viewDir = viewDirection(P);
- var waterDist = -dot(P, frame.camera.xyz);
- if (frame.camera.w > 0.5) { waterDist = length(P - frame.camera.xyz); }
-
- var wp = P.xz;
- if (mc.u_waveSpace > 0.5) { wp = v.v_texCoord + mc.u_origin; }
- let shoreTex = textureSample(u_shoreMap, u_shoreMap_s, clamp((v.v_texCoord - mc.u_shoreOrigin) / max(mc.u_shoreSize, vec2f(1e-4)), vec2f(0.0), vec2f(1.0)));
- let shoreDist = (shoreTex.r - 0.5) * 2.0 * mc.u_shoreRange;
- let shoreDepth = shoreTex.g * mc.u_shoreRange;
- var damp = 1.0;
- if (mc.u_shoreOn > 0.5) { damp = shoreTex.b; }
- // The ripples at this pixel: their height on four sides gives the slope they add, and the foam is what they left (taken in uniform control flow).
- let ripUv = (wp - mc.u_rippleOrigin) / max(mc.u_rippleSize, 1e-4);
- let ripC = textureSample(u_rippleMap, u_rippleMap_s, ripUv);
- let ripCell = max(mc.u_rippleSize * mc.u_rippleTexel, 1e-5);
- let ripL = textureSample(u_rippleMap, u_rippleMap_s, ripUv - vec2f(mc.u_rippleTexel, 0.0)).r;
- let ripR = textureSample(u_rippleMap, u_rippleMap_s, ripUv + vec2f(mc.u_rippleTexel, 0.0)).r;
- let ripD = textureSample(u_rippleMap, u_rippleMap_s, ripUv - vec2f(0.0, mc.u_rippleTexel)).r;
- let ripU = textureSample(u_rippleMap, u_rippleMap_s, ripUv + vec2f(0.0, mc.u_rippleTexel)).r;
- let ripIn = mc.u_rippleOn > 0.5 && mc.u_waveSpace > 0.5 && all(ripUv >= vec2f(0.0)) && all(ripUv <= vec2f(1.0));
- var ripSlope = vec2f(0.0);
- var ripFoam = 0.0;
- if (ripIn) { ripSlope = vec2f(ripR - ripL, ripU - ripD) * mc.u_rippleRange / ripCell; ripFoam = ripC.g; }
- var surface = vec3f(0.0);
- if (mc.u_flowSpeed > 0.0) {
- // A river: two copies of the surface carried downstream a cycle at a time, each fading in as the other fades out.
- let flow = (v.v_color.rg * 2.0 - vec2f(1.0)) * (v.v_color.b * 2.0) * mc.u_flowSpeed;
- let cycle = max(mc.u_flowCycle, 0.1);
- let ph0 = fract(time / cycle);
- let ph1 = fract(time / cycle + 0.5);
- let w0 = 1.0 - abs(2.0 * ph0 - 1.0);
- let s0 = waterSurface(wp - flow * (ph0 * cycle), time, waterDist);
- let s1 = waterSurface(wp - flow * (ph1 * cycle), time, waterDist);
- surface = mix(s1, s0, w0);
- } else {
- surface = waterSurface(wp, time, waterDist);
- }
- var waves : Waves;
- waves.crest = surface.z * damp;
- var slope = surface.xy * damp + ripSlope;
- var N = normalize(vec3f(-slope.x, 1.0, -slope.y));
- var Ngeo = vec3f(0.0, 1.0, 0.0);
-#ifdef MESH_NORMALS
- if (mc.u_waveSpace > 0.5) {
- Ngeo = normalize(v.v_worldNormal);
- N = perturbNormal(Ngeo, P, v.v_texCoord, normalize(vec3f(-slope.x, -slope.y, 1.0)));
- }
-#endif
- let below = dot(viewDir, Ngeo) < 0.0;
- if (below) { N = -N; Ngeo = -Ngeo; }
-
- let haveColor = textureSampleLevel(u_sceneColor, u_sceneColor_s, vec2f(0.5), 0.0).a < 0.75;
- let haveDepth = haveColor && textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, vec2f(0.5), 0.0).a < 0.75;
- let clip0 = frame.projection * vec4f(P, 1.0);
- let w0 = max(clip0.w, 1e-4);
- // This device's rows run down the texture, so clip y is flipped on the way to a uv.
- let uv0 = vec2f(clip0.x / w0 * 0.5 + 0.5, 0.5 - clip0.y / w0 * 0.5);
- var behindDist = waterDist + mc.u_fallbackDepth;
- if (haveDepth) { behindDist = textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, clamp(uv0, vec2f(0.001), vec2f(0.999)), 0.0).r; }
- let column0 = max(behindDist - waterDist, 0.0);
-
- var ratio = 1.0 / 1.333;
- if (below) { ratio = 1.333; }
- var bentWave = refract(-viewDir, N, ratio);
- var bentFlat = refract(-viewDir, Ngeo, ratio);
- if (below && (dot(bentWave, bentWave) < 0.5 || dot(bentFlat, bentFlat) < 0.5)) { bentWave = vec3f(0.0); bentFlat = vec3f(0.0); }
- let reach = min(column0, mc.u_distortionRange) * mc.u_refractionStrength;
- let clip1 = frame.projection * vec4f(P + (bentWave - bentFlat) * reach, 1.0);
- let w1 = max(clip1.w, 1e-4);
- var uv1 = vec2f(clip1.x / w1 * 0.5 + 0.5, 0.5 - clip1.y / w1 * 0.5);
- var behind = vec3f(0.0);
- var column = column0;
- if (haveColor) {
- uv1 = clamp(uv1, vec2f(0.001), vec2f(0.999));
- var dist1 = behindDist;
- if (haveDepth) { dist1 = textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, uv1, 0.0).r; }
- if (dist1 < waterDist) { uv1 = clamp(uv0, vec2f(0.001), vec2f(0.999)); dist1 = behindDist; }
- behind = textureSampleLevel(u_sceneColor, u_sceneColor_s, uv1, 0.0).rgb;
- column = max(dist1 - waterDist, 0.0);
- if (!below && haveDepth && mc.u_causticStrength > 0.0 && mc.u_causticOn > 0.5) {
- let sun = waterSun();
- let cosUp = dot(sun.dir.xyz, Ngeo);
- if (sun.dir.w > 0.5 && cosUp > 0.05) {
- let seabed = P + bentWave * column;
- let depthBelow = max(dot(P - seabed, Ngeo), 0.0);
- let onSurface = seabed + sun.dir.xyz * (depthBelow / max(cosUp, 0.15));
- let c = causticPattern(onSurface.xz / mc.u_causticScale, time * mc.u_causticSpeed);
- let reachSun = exp(-dot(mc.u_absorption.rgb, vec3f(1.0 / 3.0)) * mc.u_density * depthBelow / max(cosUp, 0.15));
- behind = behind * (1.0 + mc.u_causticStrength * c * cosUp * waterSunVisibility(seabed, sun.tiles.x, sun.tiles.y) * reachSun * min(sun.color.a, 2.0) * 0.5);
- }
- }
- }
-
- var transmit = exp(-mc.u_absorption.rgb * (mc.u_density * column));
- if (below) { transmit = vec3f(1.0); }
- let murk = 1.0 - dot(transmit, vec3f(1.0 / 3.0));
- let scatterColor = mix(mc.u_shallowColor.rgb, mc.u_deepColor.rgb, vec3f(1.0 - exp(-column * mc.u_density * 0.5)));
-
- let vertical = column * max(abs(viewDir.y), 0.15);
- let foamScale = 1.0 / (mc.u_rippleLength * 1.2);
- let breakup = waterNoise(wp * foamScale + vec2f(time * 0.07, time * 0.05)) * 0.6 + waterNoise(wp * foamScale * 3.0 - vec2f(time * 0.13, 0.0)) * 0.4;
- let pulse = 0.65 + 0.35 * sin(time * 0.9 + breakup * 6.0);
- var shore = 0.0;
- if (haveDepth && mc.u_foamWidth > 0.0) { shore = clamp((1.0 - vertical / (mc.u_foamWidth * pulse)) * 1.5 - breakup * 0.8, 0.0, 1.0); }
- if (mc.u_shoreOn > 0.5 && mc.u_foamWidth > 0.0) {
- shore = max(shore, clamp((1.0 - min(shoreDist, shoreDepth) / (mc.u_foamWidth * pulse)) * 1.5 - breakup * 0.8, 0.0, 1.0));
- }
- var onCrest = 0.0;
- if (mc.u_foamCrest > 0.0) { onCrest = smoothstep(1.0 - 0.3 * mc.u_foamCrest, 1.0, waves.crest) * smoothstep(0.35, 0.75, breakup); }
- let foam = clamp(max(max(shore, onCrest * mc.u_foamCrest), ripFoam * (0.55 + 0.45 * breakup)), 0.0, 1.0);
-
- var ext = surfaceExtNone(N);
- ext.misc.x = 0.0204;
- let albedo = mix(scatterColor * murk, mc.u_foamColor.rgb, vec3f(foam));
- let roughness = mix(mc.u_roughness, 0.7, foam);
- let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 1.0, 1.0, ext);
- let cosV = clamp(dot(N, viewDir), 0.0, 1.0);
- var fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosV, 5.0);
- if (below) {
- let sinT = 1.333 * sqrt(max(1.0 - cosV * cosV, 0.0));
- let cosT = sqrt(max(1.0 - sinT * sinT, 0.0));
- if (sinT >= 1.0) { fresnel = 1.0; } else { fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosT, 5.0); }
- }
- var seen = behind * transmit * (1.0 - fresnel) * (1.0 - foam);
- var mirror = vec3f(0.0);
- if (below && fresnel > 0.02 && haveColor && haveDepth && mc.u_mirror > 0.5) {
- let met = waterReflect(P, reflect(-viewDir, N), waterDist);
- if (met.w > 0.5) { mirror = met.xyz; } else { mirror = mix(mc.u_shallowColor.rgb, mc.u_deepColor.rgb, vec3f(0.5)) * 0.25; }
- }
- seen = seen + mirror * fresnel * (1.0 - foam);
- var alpha = 0.85;
- if (haveColor) { alpha = 1.0; }
- if (mc.u_shoreOn > 0.5 && shoreDist < 0.0) { discard; }
-#ifdef ES_GBUFFER
- var frag : FragOut;
- frag.color = vec4f(lit + seen, alpha);
- frag.surface = vec4f(packNormal(normalize(N)), roughness, 0.75);
- return frag;
-#else
- return vec4f(lit + seen, alpha);
-#endif
-}
-#pragma end
-`;
-
-// Ground painted by four layers. The Terrain component writes how much of each layer a point of ground has into the vertex colour
-// (red, green, blue, alpha: grass, dirt, snow, rock), and this shader draws them: each layer is a texture tiled by world position so
-// it runs unbroken from one chunk to the next, and the rock is projected from three sides so a cliff is not a smear.
-const TERRAIN = `#pragma shader "Terrain"
-#pragma version 300 es
-#pragma domain Lit
-#pragma param u_layer0 texture default(white)
-#pragma param u_layer1 texture default(white)
-#pragma param u_layer2 texture default(white)
-#pragma param u_layer3 texture default(white)
-// Each layer's colour, which multiplies its texture, and how many world units one repeat of it covers.
-#pragma param u_tint0 color default(0.28,0.46,0.2,1)
-#pragma param u_tint1 color default(0.45,0.36,0.24,1)
-#pragma param u_tint2 color default(0.93,0.95,0.98,1)
-#pragma param u_tint3 color default(0.4,0.38,0.36,1)
-#pragma param u_tile0 float default(24) range(1,800) ui(slider)
-#pragma param u_tile1 float default(24) range(1,800) ui(slider)
-#pragma param u_tile2 float default(24) range(1,800) ui(slider)
-#pragma param u_tile3 float default(40) range(1,800) ui(slider)
-// How much a layer with more weight takes over from the others at the seam: 1 is a plain blend, higher a harder edge.
-#pragma param u_sharpness float default(1.6) range(1,8) ui(slider)
-// The wide, slow variation laid over the first layer so a big meadow does not read as one repeated tile.
-#pragma param u_macroVariation float default(0.35) range(0,1) ui(slider)
-#pragma param u_roughness float default(0.92) range(0,1) ui(slider)
-
-#pragma fragment
-precision highp float;
-
-in vec4 v_color;
-in vec2 v_texCoord;
-in highp vec2 v_worldPos;
-#ifdef MESH_NORMALS
-in highp vec3 v_worldNormal;
-in highp vec3 v_worldXYZ;
-#endif
-
-#ifdef ES_GBUFFER
-layout(location = 0) out vec4 fragColor;
-layout(location = 1) out vec4 fragSurface;
-
-vec2 packNormal(vec3 n) {
- n /= (abs(n.x) + abs(n.y) + abs(n.z));
- vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
- vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
- return e * 0.5 + 0.5;
-}
-#else
-out vec4 fragColor;
-#endif
-
-void main() {
-#ifdef MESH_NORMALS
- highp vec3 P = v_worldXYZ;
- highp vec3 N = normalize(v_worldNormal);
-#else
- highp vec3 P = vec3(v_worldPos.x, 0.0, v_worldPos.y);
- highp vec3 N = vec3(0.0, 1.0, 0.0);
-#endif
- vec4 w = max(v_color, vec4(0.0));
- w /= max(dot(w, vec4(1.0)), 1e-4);
- // The strongest layer wins its seam more cleanly the higher the sharpness.
- w = pow(w, vec4(u_sharpness));
- w /= max(dot(w, vec4(1.0)), 1e-4);
-
- vec3 c0 = texture(u_layer0, P.xz / u_tile0).rgb * u_tint0.rgb;
- c0 *= mix(1.0, texture(u_layer0, P.xz / (u_tile0 * 7.3)).r * 1.8, u_macroVariation);
- vec3 c1 = texture(u_layer1, P.xz / u_tile1).rgb * u_tint1.rgb;
- vec3 c2 = texture(u_layer2, P.xz / u_tile2).rgb * u_tint2.rgb;
- // Rock is seen from three sides, in proportion to how the ground faces them, so a sheer face is not stretched.
- vec3 axis = pow(abs(N), vec3(4.0));
- axis /= dot(axis, vec3(1.0));
- vec3 c3 = (texture(u_layer3, P.zy / u_tile3).rgb * axis.x
- + texture(u_layer3, P.xz / u_tile3).rgb * axis.y
- + texture(u_layer3, P.xy / u_tile3).rgb * axis.z) * u_tint3.rgb;
- vec3 albedo = c0 * w.x + c1 * w.y + c2 * w.z + c3 * w.w;
-
- SurfaceExt ext = surfaceExtNone(N);
- vec3 viewDir = viewDirection(P);
- vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, u_roughness, 0.5, 1.0, ext);
- fragColor = vec4(lit, 1.0);
-#ifdef ES_GBUFFER
- fragSurface = vec4(packNormal(N), u_roughness, 0.75);
-#endif
-}
-#pragma end
-
-#pragma fragment wgsl
-#ifdef ES_GBUFFER
-struct FragOut {
- @location(0) color : vec4f,
- @location(1) surface : vec4f,
-};
-
-fn packNormal(n : vec3f) -> vec2f {
- let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
- let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
- var e = a.xy;
- if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
- return e * 0.5 + 0.5;
-}
-
-@fragment fn fs_main(v : VSOut) -> FragOut {
-#else
-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
-#endif
- g_probeSlot = v.v_probeSlot;
-#ifdef MESH_LIGHTMAP
- g_lightmap = v.v_lightmap;
-#endif
-#ifdef MESH_NORMALS
- let P = v.v_worldXYZ;
- let N = normalize(v.v_worldNormal);
-#else
- let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
- let N = vec3f(0.0, 1.0, 0.0);
-#endif
- // Every sample is taken here, in uniform control flow, where a derivative is allowed.
- let layer0 = textureSample(u_layer0, u_layer0_s, P.xz / mc.u_tile0).rgb;
- let macro0 = textureSample(u_layer0, u_layer0_s, P.xz / (mc.u_tile0 * 7.3)).r;
- let layer1 = textureSample(u_layer1, u_layer1_s, P.xz / mc.u_tile1).rgb;
- let layer2 = textureSample(u_layer2, u_layer2_s, P.xz / mc.u_tile2).rgb;
- let rockX = textureSample(u_layer3, u_layer3_s, P.zy / mc.u_tile3).rgb;
- let rockY = textureSample(u_layer3, u_layer3_s, P.xz / mc.u_tile3).rgb;
- let rockZ = textureSample(u_layer3, u_layer3_s, P.xy / mc.u_tile3).rgb;
-
- var w = max(v.v_color, vec4f(0.0));
- w = w / max(dot(w, vec4f(1.0)), 1e-4);
- w = pow(w, vec4f(mc.u_sharpness));
- w = w / max(dot(w, vec4f(1.0)), 1e-4);
-
- let c0 = layer0 * mc.u_tint0.rgb * mix(1.0, macro0 * 1.8, mc.u_macroVariation);
- let c1 = layer1 * mc.u_tint1.rgb;
- let c2 = layer2 * mc.u_tint2.rgb;
- var axis = pow(abs(N), vec3f(4.0));
- axis = axis / dot(axis, vec3f(1.0));
- let c3 = (rockX * axis.x + rockY * axis.y + rockZ * axis.z) * mc.u_tint3.rgb;
- let albedo = c0 * w.x + c1 * w.y + c2 * w.z + c3 * w.w;
-
- let ext = surfaceExtNone(N);
- let viewDir = viewDirection(P);
- let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, mc.u_roughness, 0.5, 1.0, ext);
-#ifdef ES_GBUFFER
- var frag : FragOut;
- frag.color = vec4f(lit, 1.0);
- frag.surface = vec4f(packNormal(N), mc.u_roughness, 0.75);
- return frag;
-#else
- return vec4f(lit, 1.0);
-#endif
-}
-#pragma end
-`;
-
-// Plants that sway: the vertex hook bends by the weights the Foliage component paints in the vertex colour (red the height up the plant,
-// green its own phase, blue how much light reaches it), with a travelling gust and a flutter. Two-sided and translucent.
-const FOLIAGE = `#pragma shader "Foliage"
-#pragma version 300 es
-#pragma domain Lit
-#pragma param u_baseColor color default(0.14,0.30,0.08,1)
-#pragma param u_tipColor color default(0.55,0.72,0.25,1)
-// A texture over the plant (leaves, bark), and whether it is used at all: grass is drawn from the two colours and wants none.
-#pragma param u_albedo texture default(white)
-#pragma param u_useTexture float default(0) range(0,1) ui(slider)
-#pragma param u_alphaCutoff float default(0.5) range(0,1) ui(slider)
-#pragma coverage u_alphaCutoff
-#pragma param u_roughness float default(0.8) range(0,1) ui(slider)
-// Light that comes through a leaf from behind, and how thick the leaf is.
-#pragma param u_translucency float default(0.5) range(0,1) ui(slider)
-#pragma param u_thickness float default(0.3) range(0,1) ui(slider)
-// The wind: how hard it blows, how fast it moves, where it comes from (degrees), how long a gust is (the lower, the longer), and how much flutter rides on it.
-#pragma param u_windStrength float default(2) range(0,20) ui(slider)
-#pragma param u_windSpeed float default(1.2) range(0,6) ui(slider)
-#pragma param u_windDirection float default(40) range(0,360) ui(slider)
-#pragma param u_gustScale float default(0.02) range(0.001,0.5) ui(slider)
-#pragma param u_flutter float default(0.4) range(0,4) ui(slider)
-#pragma vertexbounds 30
-#pragma vertexoffset
-void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
- float weight = es_vertexColor.r;
- float phase = es_vertexColor.g * 6.2831853;
- vec3 anchor = (esInstanceModel() * vec4(local, 1.0)).xyz;
- float angle = radians(u_windDirection);
- vec2 dir = vec2(sin(angle), cos(angle));
- float t = u_time.x * u_windSpeed;
- float gust = sin(dot(anchor.xz, dir) * u_gustScale - t * 1.3) * 0.5 + 0.5;
- float flutter = sin(t * 3.1 + phase + anchor.x * 0.7) * u_flutter;
- float sway = (gust + 0.25 * sin(t + phase)) * u_windStrength + flutter * weight;
- float bend = weight * weight * sway;
- local.xz += dir * bend;
- // The length is kept: what the sideways bend takes from the height it gives back by lowering the tip.
- local.y -= abs(bend) * 0.35 * weight;
-}
-#pragma end
-
-#pragma vertexoffset wgsl
-fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, vec3f>, uv : vec2f) {
- let weight = es_vertexColor.r;
- let phase = es_vertexColor.g * 6.2831853;
- let anchor = (esInstanceModel() * vec4f(*local, 1.0)).xyz;
- let angle = radians(mc.u_windDirection);
- let dir = vec2f(sin(angle), cos(angle));
- let t = tc.u_time.x * mc.u_windSpeed;
- let gust = sin(dot(anchor.xz, dir) * mc.u_gustScale - t * 1.3) * 0.5 + 0.5;
- let flutter = sin(t * 3.1 + phase + anchor.x * 0.7) * mc.u_flutter;
- let sway = (gust + 0.25 * sin(t + phase)) * mc.u_windStrength + flutter * weight;
- let bend = weight * weight * sway;
- (*local).x = (*local).x + dir.x * bend;
- (*local).z = (*local).z + dir.y * bend;
- (*local).y = (*local).y - abs(bend) * 0.35 * weight;
-}
-#pragma end
-
-#pragma fragment
-precision highp float;
-
-in vec4 v_color;
-in vec2 v_texCoord;
-in highp vec2 v_worldPos;
-#ifdef MESH_NORMALS
-in highp vec3 v_worldNormal;
-in highp vec3 v_worldXYZ;
-#endif
-
-#ifdef ES_GBUFFER
-layout(location = 0) out vec4 fragColor;
-layout(location = 1) out vec4 fragSurface;
-
-vec2 packNormal(vec3 n) {
- n /= (abs(n.x) + abs(n.y) + abs(n.z));
- vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
- vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
- return e * 0.5 + 0.5;
-}
-#else
-out vec4 fragColor;
-#endif
-
-void main() {
-#ifdef MESH_NORMALS
- highp vec3 P = v_worldXYZ;
- highp vec3 N = normalize(v_worldNormal);
- if (!gl_FrontFacing) N = -N;
-#else
- highp vec3 P = vec3(v_worldPos, 0.0);
- highp vec3 N = vec3(0.0, 0.0, 1.0);
-#endif
- float height = clamp(v_color.r, 0.0, 1.0);
- float light = v_color.b;
- vec4 tex = texture(u_albedo, v_texCoord);
- vec3 albedo = mix(u_baseColor.rgb, u_tipColor.rgb, height);
- float alpha = 1.0;
- if (u_useTexture > 0.5) {
- albedo *= tex.rgb;
- alpha = tex.a;
- alpha = (alpha - u_alphaCutoff) / max(fwidth(alpha), 1e-4) + 0.5;
- if (alpha <= 0.0) discard;
- alpha = min(alpha, 1.0);
- }
- albedo *= light;
- SurfaceExt ext = surfaceExtNone(N);
- if (u_translucency > 0.0) {
- ext.scatter = vec4(albedo * 1.3, u_translucency * 0.5);
- ext.trans = vec4(u_translucency, u_thickness, 0.0, 0.0);
- }
- vec3 viewDir = viewDirection(P);
- vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, u_roughness, 0.5, 1.0, ext);
- fragColor = vec4(lit, alpha);
-#ifdef ES_GBUFFER
- fragSurface = vec4(packNormal(N), u_roughness, 0.75);
-#endif
-}
-#pragma end
-
-#pragma fragment wgsl
-#ifdef ES_GBUFFER
-struct FragOut {
- @location(0) color : vec4f,
- @location(1) surface : vec4f,
-};
-
-fn packNormal(n : vec3f) -> vec2f {
- let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
- let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
- var e = a.xy;
- if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
- return e * 0.5 + 0.5;
-}
-
-@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
-#else
-@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
-#endif
- // Taken before any branch: a derivative needs every fragment of its quad.
- let duvx = dpdx(v.v_texCoord);
- let duvy = dpdy(v.v_texCoord);
- g_probeSlot = v.v_probeSlot;
-#ifdef MESH_LIGHTMAP
- g_lightmap = v.v_lightmap;
-#endif
- let tex = textureSampleGrad(u_albedo, u_albedo_s, v.v_texCoord, duvx, duvy);
- let edge = max(fwidth(tex.a), 1e-4);
-#ifdef MESH_NORMALS
- let P = v.v_worldXYZ;
- var N = normalize(v.v_worldNormal);
- if (!front) { N = -N; }
-#else
- let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
- let N = vec3f(0.0, 0.0, 1.0);
-#endif
- let height = clamp(v.v_color.r, 0.0, 1.0);
- let light = v.v_color.b;
- var albedo = mix(mc.u_baseColor.rgb, mc.u_tipColor.rgb, vec3f(height));
- var alpha = 1.0;
- if (mc.u_useTexture > 0.5) {
- albedo = albedo * tex.rgb;
- alpha = (tex.a - mc.u_alphaCutoff) / edge + 0.5;
- if (alpha <= 0.0) { discard; }
- alpha = min(alpha, 1.0);
- }
- albedo = albedo * light;
- var ext = surfaceExtNone(N);
- if (mc.u_translucency > 0.0) {
- ext.scatter = vec4f(albedo * 1.3, mc.u_translucency * 0.5);
- ext.trans = vec4f(mc.u_translucency, mc.u_thickness, 0.0, 0.0);
- }
- let viewDir = viewDirection(P);
- let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, mc.u_roughness, 0.5, 1.0, ext);
-#ifdef ES_GBUFFER
- var frag : FragOut;
- frag.color = vec4f(lit, alpha);
- frag.surface = vec4f(packNormal(N), mc.u_roughness, 0.75);
- return frag;
-#else
- return vec4f(lit, alpha);
-#endif
-}
-#pragma end
-`;
-
-// Hair: lit as strands. The flow map gives the strand direction; the lighting does Kajiya-Kay diffuse and two shifted highlights along it
-// (misc.y above zero is the switch). Two-sided and translucent; the base colour is the mesh's own texture, as in the Model shader.
-const HAIR = `#pragma shader "Hair"
-#pragma version 300 es
-#pragma domain Lit
-#pragma param u_tint color default(1,1,1,1)
-#pragma param u_normalMap texture default(flatnormal) texel(data)
-#pragma param u_ormMap texture default(white) texel(data)
-#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)
-// Which way each strand runs: red and green are a direction across the texture, the way an authored flow map writes it.
-#pragma param u_flowMap texture default(white) texel(data)
-// A texture that shifts the highlight along the strand from one place to the next, and how far, and how often it repeats.
-#pragma param u_shiftMap texture default(white) texel(data)
-#pragma param u_shiftAmount float default(0.35) range(0,1) ui(slider)
-#pragma param u_shiftTile float default(6) range(0.1,64) ui(slider)
-// How tightly each highlight is drawn (roughness: lower is a narrower line), how strong they are, and how far each rides off the strand's
-// centre line. The second is the coloured one, tinted by u_secondaryTint.
-#pragma param u_roughness float default(0.3) range(0.05,1) ui(slider)
-#pragma param u_specular float default(1) range(0,2) ui(slider)
-#pragma param u_shiftPrimary float default(-0.1) range(-1,1) ui(slider)
-#pragma param u_shiftSecondary float default(0.2) range(-1,1) ui(slider)
-#pragma param u_secondaryTint color default(0.75,0.45,0.25,1)
-// Light that comes through the strands from behind.
-#pragma param u_translucency float default(0.4) range(0,1) ui(slider)
-// A negative bias on the colour's mip level keeps the fine strands a hair card is covered in from washing out at a slant, which they do at once.
-#pragma param u_sharpen float default(-0.8) range(-3,1) ui(slider)
-// How much darker the roots are than the tips: the occlusion map's red, pushed.
-#pragma param u_rootDark float default(0.5) range(0,1) ui(slider)
-// A card's alpha cut at this level, and sharpened to about a texel so alpha-to-coverage feathers the edge of every strand instead of stair-stepping it; 0 keeps the card opaque.
-#pragma param u_alphaCutoff float default(0) range(0,1) ui(slider)
-#pragma coverage u_alphaCutoff
-// The soft outer band of a card: where its alpha falls from the cutoff down by this much, a second, blended pass draws the strand's tip fading out
-// (the opaque pass keeps what is above the cutoff, feathered by coverage). Sorted with the other blended draws; 0 leaves it out.
-#pragma param u_tipSoftness float default(0) range(0,0.5) ui(slider)
-#pragma tips u_tipSoftness
-
-#pragma fragment
-precision highp float;
-
-in vec4 v_color;
-in vec2 v_texCoord;
-in highp vec2 v_worldPos;
-#ifdef MESH_NORMALS
-in highp vec3 v_worldNormal;
-in highp vec3 v_worldXYZ;
-#endif
-uniform sampler2D u_textures[1];
-
-#ifdef ES_GBUFFER
-layout(location = 0) out vec4 fragColor;
-layout(location = 1) out vec4 fragSurface;
-
-vec2 packNormal(vec3 n) {
- n /= (abs(n.x) + abs(n.y) + abs(n.z));
- vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
- vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
- return e * 0.5 + 0.5;
-}
-#else
-out vec4 fragColor;
-#endif
-
-void main() {
-#ifdef MESH_NORMALS
- highp vec3 Ng = normalize(v_worldNormal);
- highp vec3 P = v_worldXYZ;
- highp vec3 N = perturbNormal(Ng, P, v_texCoord, sampleNormal(u_normalMap, v_texCoord));
- if (!gl_FrontFacing) { N = -N; Ng = -Ng; }
- // The strand: the flow map's direction across the texture, put onto the surface and made to lie across the normal.
- vec2 flow = texture(u_flowMap, v_texCoord).rg * 2.0 - 1.0;
- highp vec3 T = perturbNormal(Ng, P, v_texCoord, vec3(flow, 0.0));
- T -= N * dot(N, T);
- if (dot(T, T) < 1e-6) T = cross(N, abs(N.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0));
- T = normalize(T);
-#else
- highp vec3 P = vec3(v_worldPos, 0.0);
- highp vec3 N = vec3(0.0, 0.0, 1.0);
- highp vec3 T = vec3(0.0, 1.0, 0.0);
-#endif
- vec4 base = texture(u_textures[0], v_texCoord, u_sharpen) * v_color * u_tint;
- // Before any discard: a derivative needs every fragment of its quad.
- float alpha = 1.0;
-#ifdef ES_TIP_PASS
- // The band below the cutoff, fading in toward it, and a texel past it so it meets the opaque card without a gap.
- alpha = (base.a - (u_alphaCutoff - u_tipSoftness)) / max(u_tipSoftness, 1e-4);
- if (base.a >= u_alphaCutoff + fwidth(base.a) || alpha <= 0.0) discard;
- alpha = min(alpha, 1.0);
-#else
- if (u_alphaCutoff > 0.0) {
- alpha = (base.a - u_alphaCutoff) / max(fwidth(base.a), 1e-4) + 0.5;
- if (alpha <= 0.0) discard;
- alpha = min(alpha, 1.0);
- }
-#endif
- float root = texture(u_ormMap, v_texCoord).r;
- float ao = mix(1.0, root, u_occlusionStrength);
- base.rgb *= mix(1.0, 0.35 + 0.65 * root, u_rootDark);
- float noise = (texture(u_shiftMap, v_texCoord * u_shiftTile).r - 0.5) * u_shiftAmount;
- SurfaceExt ext = surfaceExtNone(N);
- ext.aniso = vec4(T, 0.0);
- ext.misc = vec4(0.046, 1.0, u_shiftPrimary + noise, u_shiftSecondary + noise);
- ext.scatter = vec4(base.rgb * u_secondaryTint.rgb * 1.6, 0.35);
- ext.trans = vec4(u_translucency, 0.25, 0.0, 0.0);
- vec3 viewDir = viewDirection(P);
- vec3 lit = applyLightingSurface(base.rgb, N, P, viewDir, 0.0, u_roughness, u_specular, ao, ext);
- fragColor = vec4(lit, alpha);
-#ifdef ES_GBUFFER
- fragSurface = vec4(packNormal(normalize(N)), u_roughness, 0.75);
-#endif
-}
-#pragma end
-
-#pragma fragment wgsl
-#ifdef ES_GBUFFER
-struct FragOut {
- @location(0) color : vec4f,
- @location(1) surface : vec4f,
-};
-
-fn packNormal(n : vec3f) -> vec2f {
- let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
- let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
- var e = a.xy;
- if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
- return e * 0.5 + 0.5;
-}
-
-@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
-#else
-@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
-#endif
- let duvx = dpdx(v.v_texCoord);
- let duvy = dpdy(v.v_texCoord);
- g_probeSlot = v.v_probeSlot;
-#ifdef MESH_LIGHTMAP
- g_lightmap = v.v_lightmap;
-#endif
-#ifdef MESH_TANGENTS
- g_worldTangent = v.v_worldTangent;
-#endif
- var base = textureSampleBias(t0, s0, v.v_texCoord, mc.u_sharpen) * v.v_color * mc.u_tint;
- let edge = max(abs(dpdx(base.a)) + abs(dpdy(base.a)), 1e-4);
- let flowTex = textureSampleGrad(u_flowMap, u_flowMap_s, v.v_texCoord, duvx, duvy).rg * 2.0 - vec2f(1.0);
- let orm = textureSampleGrad(u_ormMap, u_ormMap_s, v.v_texCoord, duvx, duvy);
- let shiftTex = textureSampleGrad(u_shiftMap, u_shiftMap_s, v.v_texCoord * mc.u_shiftTile, duvx * mc.u_shiftTile, duvy * mc.u_shiftTile).r;
-#ifdef MESH_NORMALS
- var Ng = normalize(v.v_worldNormal);
- let P = v.v_worldXYZ;
- var N = perturbNormal(Ng, P, v.v_texCoord, sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy));
- if (!front) { N = -N; Ng = -Ng; }
- var T = perturbNormal(Ng, P, v.v_texCoord, vec3f(flowTex, 0.0));
- T = T - N * dot(N, T);
- if (dot(T, T) < 1e-6) {
- var seed = vec3f(1.0, 0.0, 0.0);
- if (abs(N.y) < 0.99) { seed = vec3f(0.0, 1.0, 0.0); }
- T = cross(N, seed);
- }
- T = normalize(T);
-#else
- let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
- let N = vec3f(0.0, 0.0, 1.0);
- let T = vec3f(0.0, 1.0, 0.0);
-#endif
- var alpha = 1.0;
-#ifdef ES_TIP_PASS
- alpha = (base.a - (mc.u_alphaCutoff - mc.u_tipSoftness)) / max(mc.u_tipSoftness, 1e-4);
- if (base.a >= mc.u_alphaCutoff + edge || alpha <= 0.0) { discard; }
- alpha = min(alpha, 1.0);
-#else
- if (mc.u_alphaCutoff > 0.0) {
- alpha = (base.a - mc.u_alphaCutoff) / edge + 0.5;
- if (alpha <= 0.0) { discard; }
- alpha = min(alpha, 1.0);
- }
-#endif
- let ao = mix(1.0, orm.r, mc.u_occlusionStrength);
- base = vec4f(base.rgb * mix(1.0, 0.35 + 0.65 * orm.r, mc.u_rootDark), base.a);
- let noise = (shiftTex - 0.5) * mc.u_shiftAmount;
- var ext = surfaceExtNone(N);
- ext.aniso = vec4f(T, 0.0);
- ext.misc = vec4f(0.046, 1.0, mc.u_shiftPrimary + noise, mc.u_shiftSecondary + noise);
- ext.scatter = vec4f(base.rgb * mc.u_secondaryTint.rgb * 1.6, 0.35);
- ext.trans = vec4f(mc.u_translucency, 0.25, 0.0, 0.0);
- let viewDir = viewDirection(P);
- let lit = applyLightingSurface(base.rgb, N, P, viewDir, 0.0, mc.u_roughness, mc.u_specular, ao, ext);
-#ifdef ES_GBUFFER
- var frag : FragOut;
- frag.color = vec4f(lit, alpha);
- frag.surface = vec4f(packNormal(normalize(N)), mc.u_roughness, 0.75);
- return frag;
-#else
- return vec4f(lit, alpha);
-#endif
-}
-#pragma end
-`;
-
-// Skin: scattering without blurring the picture. Diffuse light comes from a softer normal than the surface's own (softest for red), the
-// terminator is warmed, thin curved places let light through, and the shine is two lobes and a fuzz. Base colour is the mesh's texture.
-const SKIN = `#pragma shader "Skin"
-#pragma version 300 es
-#pragma domain Lit
-#pragma param u_tint color default(1,1,1,1)
-#pragma param u_normalMap texture default(flatnormal) texel(data)
-#pragma param u_ormMap texture default(white) texel(data)
-#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)
-// A second, fine normal map tiled over the surface (pores, creases) laid on the sharp normal the shine reads: how strongly, and how many times it repeats.
-// The soft diffuse normals do not carry it, as they do not carry the baked pores either. Import it with sRGB off and wrap repeat; 0 leaves it out.
-#pragma param u_detailMap texture default(flatnormal) texel(data)
-#pragma param u_detailStrength float default(0) range(0,2) ui(slider)
-#pragma param u_detailTile float default(16) range(1,128) ui(slider)
-// How rough the surface is (a scale on the map's own roughness), how much it shines, and the second, tighter lobe.
-#pragma param u_roughness float default(1) range(0,2) ui(slider)
-#pragma param u_specular float default(1) range(0,2) ui(slider)
-#pragma param u_lobe2 float default(0.18) range(0,1) ui(slider)
-#pragma param u_lobe2Roughness float default(0.4) range(0.05,1) ui(slider)
-// How much softer than the surface's own normal the diffuse light's normal is: for red, then for green and blue (mip levels of the normal map).
-#pragma param u_blurRed float default(3.2) range(0,8) ui(slider)
-#pragma param u_blurGreen float default(1.6) range(0,8) ui(slider)
-// The colour the terminator takes and how far light wraps past it.
-#pragma param u_sssColor color default(0.95,0.4,0.3,1)
-#pragma param u_sssWrap float default(0.45) range(0,1) ui(slider)
-// Light through thin places: how much, and how much of a surface's curvature counts as thin.
-#pragma param u_translucency float default(0.5) range(0,1) ui(slider)
-#pragma param u_thinness float default(0.5) range(0,1) ui(slider)
-// Pre-integrated scattering: the wrap past the terminator is taken per colour (red furthest) and widens where the surface turns sharply, which
-// is what makes a nose or an ear glow at its edge while a flat cheek stays crisp. u_curvature is how fast a turn counts as sharp, per world unit.
-#pragma param u_preIntegrate float default(0) range(0,1) ui(slider)
-#pragma param u_curvature float default(8) range(0,64) ui(slider)
-// The fine fuzz that lights a rim.
-#pragma param u_fuzz float default(0.1) range(0,1) ui(slider)
-// Marks the surface for the Subsurface Scattering effect, which spreads what is left of the light on a larger scale. Off by default.
-#pragma param u_subsurface float default(0) range(0,1) ui(slider)
-
-#pragma fragment
-precision highp float;
-
-in vec4 v_color;
-in vec2 v_texCoord;
-in highp vec2 v_worldPos;
-#ifdef MESH_NORMALS
-in highp vec3 v_worldNormal;
-in highp vec3 v_worldXYZ;
-#endif
-uniform sampler2D u_textures[1];
-
-#ifdef ES_GBUFFER
-layout(location = 0) out vec4 fragColor;
-layout(location = 1) out vec4 fragSurface;
-
-vec2 packNormal(vec3 n) {
- n /= (abs(n.x) + abs(n.y) + abs(n.z));
- vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
- vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
- return e * 0.5 + 0.5;
-}
-#else
-out vec4 fragColor;
-#endif
-
-vec3 skinNormalAt(float bias) {
- return normalize(texture(u_normalMap, v_texCoord, bias).xyz * 2.0 - 1.0);
-}
-
-// The baked normal with the detail's slope added to it, in the tangent frame both are read in.
-vec3 skinDetail(vec3 baked, vec2 detail) {
- return normalize(vec3(baked.xy + detail * u_detailStrength, baked.z));
-}
-
-void main() {
- // Taken before any branch: a derivative needs every fragment of its quad.
- vec2 detail = texture(u_detailMap, v_texCoord * u_detailTile).xy * 2.0 - 1.0;
-#ifdef MESH_NORMALS
- highp vec3 Ng = normalize(v_worldNormal);
- highp vec3 P = v_worldXYZ;
- if (!gl_FrontFacing) Ng = -Ng;
- highp vec3 N = perturbNormal(Ng, P, v_texCoord, skinDetail(skinNormalAt(0.0), detail));
- highp vec3 Nmid = perturbNormal(Ng, P, v_texCoord, skinNormalAt(u_blurGreen));
- highp vec3 Nsoft = perturbNormal(Ng, P, v_texCoord, skinNormalAt(u_blurRed));
- // How fast the surface turns across a pixel, against how big a pixel is: the thin, strongly curved places.
- float turn = (length(dFdx(Ng)) + length(dFdy(Ng))) / max(length(dFdx(P)) + length(dFdy(P)), 1e-5);
- float thick = 1.0 - clamp(turn * 12.0 * u_thinness, 0.0, 1.0);
- float curv = clamp(turn * u_curvature, 0.0, 1.0);
-#else
- highp vec3 P = vec3(v_worldPos, 0.0);
- highp vec3 N = vec3(0.0, 0.0, 1.0);
- highp vec3 Nmid = N;
- highp vec3 Nsoft = N;
- float thick = 1.0;
- float curv = 0.0;
-#endif
- vec4 base = texture(u_textures[0], v_texCoord) * v_color * u_tint;
- vec4 orm = texture(u_ormMap, v_texCoord);
- float ao = mix(1.0, orm.r, u_occlusionStrength);
- float rough = clamp(orm.g * u_roughness, 0.05, 1.0);
- vec3 viewDir = viewDirection(P);
-
- // The diffuse light, from two softnesses of normal, with the terminator warmed and light let through a thin place.
- SurfaceExt wrapped = surfaceExtNone(Nsoft);
- wrapped.scatter = vec4(u_sssColor.rgb * base.rgb * 1.4, u_sssWrap);
- wrapped.trans = vec4(u_translucency, thick, curv, u_preIntegrate);
- vec3 soft = applyLightingSurface(base.rgb, Nsoft, P, viewDir, 0.0, 1.0, 0.0, ao, wrapped);
- SurfaceExt wrappedMid = surfaceExtNone(Nmid);
- wrappedMid.scatter = wrapped.scatter;
- wrappedMid.trans = wrapped.trans;
- vec3 mid = applyLightingSurface(base.rgb, Nmid, P, viewDir, 0.0, 1.0, 0.0, ao, wrappedMid);
- vec3 diffuse = vec3(soft.r, mid.g, mid.b);
-
- // The shine: two lobes and a fuzz, on the surface's own normal, with no diffuse under it.
- SurfaceExt shine = surfaceExtNone(N);
- shine.coat = vec4(u_lobe2, u_lobe2Roughness, 0.0, 0.0);
- shine.coatN = vec4(N, 0.0);
- shine.sheen = vec4(vec3(u_fuzz), 0.5);
- vec3 spec = applyLightingSurface(vec3(0.0), N, P, viewDir, 0.0, rough, u_specular, ao, shine);
-
- float alpha = u_subsurface > 0.0 ? 0.996 : 1.0;
- fragColor = vec4(diffuse + spec, alpha);
-#ifdef ES_GBUFFER
- fragSurface = vec4(packNormal(normalize(N)), rough, 0.75);
-#endif
-}
-#pragma end
-
-#pragma fragment wgsl
-#ifdef ES_GBUFFER
-struct FragOut {
- @location(0) color : vec4f,
- @location(1) surface : vec4f,
-};
-
-fn packNormal(n : vec3f) -> vec2f {
- let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
- let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
- var e = a.xy;
- if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
- return e * 0.5 + 0.5;
-}
-
-@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
-#else
-@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
-#endif
- // Every sample is taken here, before any branch.
- g_probeSlot = v.v_probeSlot;
-#ifdef MESH_LIGHTMAP
- g_lightmap = v.v_lightmap;
-#endif
-#ifdef MESH_TANGENTS
- g_worldTangent = v.v_worldTangent;
-#endif
- let base = textureSample(t0, s0, v.v_texCoord) * v.v_color * mc.u_tint;
- let orm = textureSample(u_ormMap, u_ormMap_s, v.v_texCoord);
- let detail = textureSample(u_detailMap, u_detailMap_s, v.v_texCoord * mc.u_detailTile).xy * 2.0 - vec2f(1.0);
- let bakedSharp = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, 0.0).xyz * 2.0 - vec3f(1.0));
- let tangentSharp = normalize(vec3f(bakedSharp.xy + detail * mc.u_detailStrength, bakedSharp.z));
- let tangentMid = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurGreen).xyz * 2.0 - vec3f(1.0));
- let tangentSoft = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurRed).xyz * 2.0 - vec3f(1.0));
-#ifdef MESH_NORMALS
- var Ng = normalize(v.v_worldNormal);
- let P = v.v_worldXYZ;
- if (!front) { Ng = -Ng; }
- let N = perturbNormal(Ng, P, v.v_texCoord, tangentSharp);
- let Nmid = perturbNormal(Ng, P, v.v_texCoord, tangentMid);
- let Nsoft = perturbNormal(Ng, P, v.v_texCoord, tangentSoft);
- let turn = (length(dpdx(Ng)) + length(dpdy(Ng))) / max(length(dpdx(P)) + length(dpdy(P)), 1e-5);
- let thick = 1.0 - clamp(turn * 12.0 * mc.u_thinness, 0.0, 1.0);
- let curv = clamp(turn * mc.u_curvature, 0.0, 1.0);
-#else
- let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
- let N = vec3f(0.0, 0.0, 1.0);
- let Nmid = N;
- let Nsoft = N;
- let thick = 1.0;
- let curv = 0.0;
-#endif
- let ao = mix(1.0, orm.r, mc.u_occlusionStrength);
- let rough = clamp(orm.g * mc.u_roughness, 0.05, 1.0);
- let viewDir = viewDirection(P);
-
- var wrapped = surfaceExtNone(Nsoft);
- wrapped.scatter = vec4f(mc.u_sssColor.rgb * base.rgb * 1.4, mc.u_sssWrap);
- wrapped.trans = vec4f(mc.u_translucency, thick, curv, mc.u_preIntegrate);
- let soft = applyLightingSurface(base.rgb, Nsoft, P, viewDir, 0.0, 1.0, 0.0, ao, wrapped);
- var wrappedMid = surfaceExtNone(Nmid);
- wrappedMid.scatter = wrapped.scatter;
- wrappedMid.trans = wrapped.trans;
- let mid = applyLightingSurface(base.rgb, Nmid, P, viewDir, 0.0, 1.0, 0.0, ao, wrappedMid);
- let diffuse = vec3f(soft.r, mid.g, mid.b);
-
- var shine = surfaceExtNone(N);
- shine.coat = vec4f(mc.u_lobe2, mc.u_lobe2Roughness, 0.0, 0.0);
- shine.coatN = vec4f(N, 0.0);
- shine.sheen = vec4f(vec3f(mc.u_fuzz), 0.5);
- let spec = applyLightingSurface(vec3f(0.0), N, P, viewDir, 0.0, rough, mc.u_specular, ao, shine);
-
- var alpha = 1.0;
- if (mc.u_subsurface > 0.0) { alpha = 0.996; }
-#ifdef ES_GBUFFER
- var frag : FragOut;
- frag.color = vec4f(diffuse + spec, alpha);
- frag.surface = vec4f(packNormal(normalize(N)), rough, 0.75);
- return frag;
-#else
- return vec4f(diffuse + spec, alpha);
-#endif
-}
-#pragma end
-`;
-
-const EYE = `#pragma shader "Eye"
-#pragma version 300 es
-#pragma domain Lit
-#pragma param u_tint color default(1,1,1,1)
-// The iris is built from two maps and the numbers below: a mask that is white over the iris and the dome of the cornea
-// (u_depthMap), and that dome's normals (u_irisNormalMap). A painted eye texture on the mesh multiplies the result.
-#pragma param u_depthMap texture default(black) texel(data)
-#pragma param u_irisNormalMap texture default(flatnormal) texel(data)
-#pragma param u_scleraColor color default(0.92,0.9,0.87,1)
-// The iris from the pupil's edge out to the limbus, and how dark its rim is.
-#pragma param u_irisColor color default(0.22,0.4,0.52,1)
-#pragma param u_irisColor2 color default(0.08,0.17,0.22,1)
-#pragma param u_irisSize float default(0.42) range(0.1,0.6) ui(slider)
-#pragma param u_pupilSize float default(0.3) range(0,0.8) ui(slider)
-#pragma param u_limbal float default(0.7) range(0,1) ui(slider)
-// How far behind the cornea the iris lies (in uv), and the cornea's index of refraction, which bend what is seen of it.
-#pragma param u_irisDepth float default(0.06) range(0,0.2) ui(slider)
-#pragma param u_ior float default(1.336) range(1,1.6) ui(slider)
-// The wet cornea is nearly a mirror; the white of the eye is a duller sheen with light that wraps past the terminator.
-#pragma param u_corneaRoughness float default(0.04) range(0.01,0.3) ui(slider)
-#pragma param u_scleraRoughness float default(0.45) range(0.1,1) ui(slider)
-#pragma param u_specular float default(1) range(0,2) ui(slider)
-#pragma param u_scleraWrap float default(0.35) range(0,1) ui(slider)
-// The lids shade the eye they sit on. u_lidMap is an authored occlusion map (white where the eye is open); u_lidShadow also darkens, and fades the
-// cornea's reflections, where the surface faces up from u_lidStart (a dot with the world's up) to straight up: the upper lid, for an upright head.
-#pragma param u_lidMap texture default(white) texel(data)
-#pragma param u_lidShadow float default(0.5) range(0,1) ui(slider)
-#pragma param u_lidStart float default(0.2) range(-1,1) ui(slider)
-
-#pragma fragment
-precision highp float;
-
-in vec4 v_color;
-in vec2 v_texCoord;
-in highp vec2 v_worldPos;
-#ifdef MESH_NORMALS
-in highp vec3 v_worldNormal;
-in highp vec3 v_worldXYZ;
-#endif
-uniform sampler2D u_textures[1];
-
-#ifdef ES_GBUFFER
-layout(location = 0) out vec4 fragColor;
-layout(location = 1) out vec4 fragSurface;
-
-vec2 packNormal(vec3 n) {
- n /= (abs(n.x) + abs(n.y) + abs(n.z));
- vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
- vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
- return e * 0.5 + 0.5;
-}
-#else
-out vec4 fragColor;
-#endif
-
-void main() {
- vec2 uv = v_texCoord;
- // Every sample that takes part in a derivative is taken here, before any branch.
- float h = texture(u_depthMap, uv).r;
- float lid = texture(u_lidMap, uv).r;
- vec3 dome = normalize(texture(u_irisNormalMap, uv).xyz * 2.0 - 1.0);
-#ifdef MESH_NORMALS
- highp vec3 Ng = normalize(v_worldNormal);
- highp vec3 P = v_worldXYZ;
- if (!gl_FrontFacing) Ng = -Ng;
- highp vec3 T = perturbNormal(Ng, P, uv, vec3(1.0, 0.0, 0.0));
- highp vec3 B = perturbNormal(Ng, P, uv, vec3(0.0, 1.0, 0.0));
- highp vec3 Ndome = perturbNormal(Ng, P, uv, dome);
-#else
- highp vec3 P = vec3(v_worldPos, 0.0);
- highp vec3 Ng = vec3(0.0, 0.0, 1.0);
- highp vec3 T = vec3(1.0, 0.0, 0.0);
- highp vec3 B = vec3(0.0, 1.0, 0.0);
- highp vec3 Ndome = Ng;
-#endif
- vec3 viewDir = viewDirection(P);
- // The ray from the eye, in the surface's own frame, bent into the cornea and carried down to the iris: that is where on
- // the iris this pixel looks, which moves as the eye turns.
- vec3 vt = normalize(vec3(dot(viewDir, T), dot(viewDir, B), max(dot(viewDir, Ng), 0.05)));
- vec3 R = refract(-vt, dome, 1.0 / u_ior);
- vec2 uvI = uv + R.xy * (u_irisDepth * h / max(-R.z, 0.2));
- float m = texture(u_depthMap, uvI).r;
- float irisMask = smoothstep(0.15, 0.85, m);
- float surfaceMask = smoothstep(0.15, 0.85, h);
-
- vec2 c = uvI - vec2(0.5);
- float rr = length(c) / max(u_irisSize, 1e-3);
- float ang = atan(c.y, c.x);
- float fibers = 0.82 + 0.18 * sin(ang * 70.0 + sin(ang * 13.0 + rr * 9.0) * 2.0);
- vec3 iris = mix(u_irisColor.rgb, u_irisColor2.rgb, smoothstep(0.2, 0.95, rr)) * fibers;
- iris *= 1.0 - u_limbal * smoothstep(0.78, 1.0, rr);
- iris *= smoothstep(u_pupilSize - 0.05, u_pupilSize + 0.03, rr);
- vec4 painted = texture(u_textures[0], uv) * v_color * u_tint;
- vec3 albedo = mix(u_scleraColor.rgb, iris, irisMask) * painted.rgb;
-
- float ao = 1.0;
- float lidShade = lid * (1.0 - u_lidShadow * smoothstep(u_lidStart, 1.0, Ng.y));
- vec3 Nd = normalize(mix(Ng, Ndome, 0.5 * surfaceMask));
- SurfaceExt body = surfaceExtNone(Nd);
- body.scatter = vec4(vec3(0.9, 0.5, 0.4) * albedo, u_scleraWrap * (1.0 - surfaceMask));
- vec3 diffuse = applyLightingSurface(albedo, Nd, P, viewDir, 0.0, 1.0, 0.0, ao, body);
-
- // The cornea: a clear dome, so its normal is the dome's, and it shows every light and the sky as a small bright shape.
- vec3 Nc = normalize(mix(Ng, Ndome, surfaceMask));
- SurfaceExt shine = surfaceExtNone(Nc);
- shine.coat = vec4(0.25 * (1.0 - surfaceMask), u_scleraRoughness, 0.0, 0.0);
- shine.coatN = vec4(Ng, 0.0);
- vec3 spec = applyLightingSurface(vec3(0.0), Nc, P, viewDir, 0.0, u_corneaRoughness, u_specular, ao, shine);
-
- fragColor = vec4((diffuse + spec) * lidShade, 1.0);
-#ifdef ES_GBUFFER
- fragSurface = vec4(packNormal(normalize(Nc)), u_corneaRoughness, 0.75);
-#endif
-}
-#pragma end
-
-#pragma fragment wgsl
-#ifdef ES_GBUFFER
-struct FragOut {
- @location(0) color : vec4f,
- @location(1) surface : vec4f,
-};
-
-fn packNormal(n : vec3f) -> vec2f {
- let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
- let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
- var e = a.xy;
- if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
- return e * 0.5 + 0.5;
-}
-
-@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
-#else
-@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
-#endif
- g_probeSlot = v.v_probeSlot;
-#ifdef MESH_LIGHTMAP
- g_lightmap = v.v_lightmap;
-#endif
-#ifdef MESH_TANGENTS
- g_worldTangent = v.v_worldTangent;
-#endif
- let uv = v.v_texCoord;
- // Every sample is taken here, before any branch.
- let h = textureSample(u_depthMap, u_depthMap_s, uv).r;
- let lid = textureSample(u_lidMap, u_lidMap_s, uv).r;
- let dome = normalize(textureSample(u_irisNormalMap, u_irisNormalMap_s, uv).xyz * 2.0 - vec3f(1.0));
-#ifdef MESH_NORMALS
- var Ng = normalize(v.v_worldNormal);
- let P = v.v_worldXYZ;
- if (!front) { Ng = -Ng; }
- let T = perturbNormal(Ng, P, uv, vec3f(1.0, 0.0, 0.0));
- let B = perturbNormal(Ng, P, uv, vec3f(0.0, 1.0, 0.0));
- let Ndome = perturbNormal(Ng, P, uv, dome);
-#else
- let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
- let Ng = vec3f(0.0, 0.0, 1.0);
- let T = vec3f(1.0, 0.0, 0.0);
- let B = vec3f(0.0, 1.0, 0.0);
- let Ndome = Ng;
-#endif
- let viewDir = viewDirection(P);
- let vt = normalize(vec3f(dot(viewDir, T), dot(viewDir, B), max(dot(viewDir, Ng), 0.05)));
- let R = refract(-vt, dome, 1.0 / mc.u_ior);
- let uvI = uv + R.xy * (mc.u_irisDepth * h / max(-R.z, 0.2));
- let m = textureSample(u_depthMap, u_depthMap_s, uvI).r;
- let irisMask = smoothstep(0.15, 0.85, m);
- let surfaceMask = smoothstep(0.15, 0.85, h);
-
- let c = uvI - vec2f(0.5);
- let rr = length(c) / max(mc.u_irisSize, 1e-3);
- let ang = atan2(c.y, c.x);
- let fibers = 0.82 + 0.18 * sin(ang * 70.0 + sin(ang * 13.0 + rr * 9.0) * 2.0);
- var iris = mix(mc.u_irisColor.rgb, mc.u_irisColor2.rgb, smoothstep(0.2, 0.95, rr)) * fibers;
- iris = iris * (1.0 - mc.u_limbal * smoothstep(0.78, 1.0, rr));
- iris = iris * smoothstep(mc.u_pupilSize - 0.05, mc.u_pupilSize + 0.03, rr);
- let painted = textureSample(t0, s0, uv) * v.v_color * mc.u_tint;
- let albedo = mix(mc.u_scleraColor.rgb, iris, irisMask) * painted.rgb;
-
- let ao = 1.0;
- let lidShade = lid * (1.0 - mc.u_lidShadow * smoothstep(mc.u_lidStart, 1.0, Ng.y));
- let Nd = normalize(mix(Ng, Ndome, 0.5 * surfaceMask));
- var body = surfaceExtNone(Nd);
- body.scatter = vec4f(vec3f(0.9, 0.5, 0.4) * albedo, mc.u_scleraWrap * (1.0 - surfaceMask));
- let diffuse = applyLightingSurface(albedo, Nd, P, viewDir, 0.0, 1.0, 0.0, ao, body);
-
- let Nc = normalize(mix(Ng, Ndome, surfaceMask));
- var shine = surfaceExtNone(Nc);
- shine.coat = vec4f(0.25 * (1.0 - surfaceMask), mc.u_scleraRoughness, 0.0, 0.0);
- shine.coatN = vec4f(Ng, 0.0);
- let spec = applyLightingSurface(vec3f(0.0), Nc, P, viewDir, 0.0, mc.u_corneaRoughness, mc.u_specular, ao, shine);
-
-#ifdef ES_GBUFFER
- var frag : FragOut;
- frag.color = vec4f((diffuse + spec) * lidShade, 1.0);
- frag.surface = vec4f(packNormal(normalize(Nc)), mc.u_corneaRoughness, 0.75);
- return frag;
-#else
- return vec4f((diffuse + spec) * lidShade, 1.0);
-#endif
-}
-#pragma end
-`;
-
export const BUILTIN_SHADER_TEMPLATES: readonly BuiltinShaderTemplate[] = [
template('sprite-unlit', 'Unlit', 'Texture × vertex color × tint, no lighting.', SPRITE_UNLIT),
template('sprite-lit', 'Lit', 'Lit by the scene\'s 2D lights; optional normal map.', SPRITE_LIT),
@@ -2378,20 +708,32 @@ export const BUILTIN_SHADER_TEMPLATES: readonly BuiltinShaderTemplate[] = [
template('model', 'Model',
'What an imported model\'s material says: normal map, emission, occlusion, alpha cutoff.',
MODEL),
- template('water', 'Water',
- 'Waves, refraction, reflection, depth absorption and foam for a horizontal body of water.', WATER),
- template('terrain', 'Terrain',
- 'Four texture layers (grass, dirt, snow, rock) weighted by the Terrain component, tiled by world position.', TERRAIN),
- template('foliage', 'Foliage',
- 'Plants that sway in the wind: a vertex bend by painted weights, two-sided and translucent, with an optional cutout texture.', FOLIAGE),
- template('hair', 'Hair',
- 'Strand lighting: Kajiya-Kay diffuse, two shifted highlights along a flow map, and light through the strands.', HAIR),
- template('skin', 'Skin',
- 'Soft diffuse by colour, a warm terminator, light through thin places, two shine lobes and a fuzz — without blurring the picture.', SKIN),
- template('eye', 'Eye',
- 'A wet cornea over an iris that moves as the eye turns: refracted depth, a pupil, a dark limbal ring, and catchlights of every light.', EYE),
];
+/** Where the templates that are not the core's sit in a list, so a menu reads the same whichever order the modules were installed in. */
+const MODULE_TEMPLATE_ORDER = ['water', 'terrain', 'foliage', 'hair', 'skin', 'eye'];
+
+/** The module a built-in shader ships in, for the error a project gets when its material names a shader whose module it did not install. */
+export const BUILTIN_SHADER_MODULE: Readonly<Record<string, string>> = {
+ water: 'esengine/water', terrain: 'esengine/terrain', foliage: 'esengine/foliage',
+ hair: 'esengine/character-shaders', skin: 'esengine/character-shaders', eye: 'esengine/character-shaders',
+};
+
+/**
+ * Adds a module's shader template to the built-in list. A module calls it when it is installed, so a package that does not install water carries none of its
+ * 38 KB of shader source. Registering the same id again replaces it.
+ */
+export function registerBuiltinShaderTemplate(entry: BuiltinShaderTemplate): void {
+ const at = BUILTIN_SHADER_TEMPLATES.findIndex((t) => t.id === entry.id);
+ if (at >= 0) { (BUILTIN_SHADER_TEMPLATES as BuiltinShaderTemplate[])[at] = entry; return; }
+ const list = BUILTIN_SHADER_TEMPLATES as BuiltinShaderTemplate[];
+ list.push(entry);
+ const rank = (id: string): number => { const i = MODULE_TEMPLATE_ORDER.indexOf(id); return i < 0 ? -1 : i; };
+ // Core templates keep their places; the module ones follow them in a fixed order.
+ const core = list.filter((t) => rank(t.id) < 0), modules = list.filter((t) => rank(t.id) >= 0).sort((a, b) => rank(a.id) - rank(b.id));
+ list.splice(0, list.length, ...core, ...modules);
+}
+
export function builtinShaderTemplate(id: string): BuiltinShaderTemplate | undefined {
return BUILTIN_SHADER_TEMPLATES.find((t) => t.id === id);
}
diff --git a/sdk/src/runtime/optionalPlugins.ts b/sdk/src/runtime/optionalPlugins.ts
index dfd98fcae..2c49717e3 100644
--- a/sdk/src/runtime/optionalPlugins.ts
+++ b/sdk/src/runtime/optionalPlugins.ts
@@ -28,6 +28,7 @@ import { registerTerrainSupport } from '../terrain/terrainSupport';
import { registerFoliageSupport } from '../foliage/foliageSupport';
import { registerWaterSupport } from '../water/waterSupport';
import { registerSplineSupport } from '../spline/splineSupport';
+import { registerCharacterShaders } from '../character/characterSupport';
import { registerScriptGraphSupport } from '../logic/logicSupport';
import { registerAiSupport } from '../ai/aiSupport';
import { registerReplicationSupport } from '../net/replication/replicationSupport';
@@ -48,6 +49,7 @@ export function installOptionalPlugins(): void {
registerFoliageSupport();
registerWaterSupport();
registerSplineSupport();
+ registerCharacterShaders();
registerScriptGraphSupport();
registerAiSupport();
registerReplicationSupport();
diff --git a/sdk/src/terrain/terrainShader.ts b/sdk/src/terrain/terrainShader.ts
new file mode 100644
index 000000000..dcc2a4f34
--- /dev/null
+++ b/sdk/src/terrain/terrainShader.ts
@@ -0,0 +1,166 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file terrainShader.ts
+ * @brief The Terrain built-in shader: four texture layers weighted by the Terrain component.
+ *
+ * The shader source lives with the module that installs it, not in the core's list of built-in shaders: a package that does not install the module carries none of it.
+ */
+import { makeBuiltinShaderTemplate } from '../render/builtinShaders';
+
+
+// Ground painted by four layers. The Terrain component writes how much of each layer a point of ground has into the vertex colour
+// (red, green, blue, alpha: grass, dirt, snow, rock), and this shader draws them: each layer is a texture tiled by world position so
+// it runs unbroken from one chunk to the next, and the rock is projected from three sides so a cliff is not a smear.
+const TERRAIN = `#pragma shader "Terrain"
+#pragma version 300 es
+#pragma domain Lit
+#pragma param u_layer0 texture default(white)
+#pragma param u_layer1 texture default(white)
+#pragma param u_layer2 texture default(white)
+#pragma param u_layer3 texture default(white)
+// Each layer's colour, which multiplies its texture, and how many world units one repeat of it covers.
+#pragma param u_tint0 color default(0.28,0.46,0.2,1)
+#pragma param u_tint1 color default(0.45,0.36,0.24,1)
+#pragma param u_tint2 color default(0.93,0.95,0.98,1)
+#pragma param u_tint3 color default(0.4,0.38,0.36,1)
+#pragma param u_tile0 float default(24) range(1,800) ui(slider)
+#pragma param u_tile1 float default(24) range(1,800) ui(slider)
+#pragma param u_tile2 float default(24) range(1,800) ui(slider)
+#pragma param u_tile3 float default(40) range(1,800) ui(slider)
+// How much a layer with more weight takes over from the others at the seam: 1 is a plain blend, higher a harder edge.
+#pragma param u_sharpness float default(1.6) range(1,8) ui(slider)
+// The wide, slow variation laid over the first layer so a big meadow does not read as one repeated tile.
+#pragma param u_macroVariation float default(0.35) range(0,1) ui(slider)
+#pragma param u_roughness float default(0.92) range(0,1) ui(slider)
+
+#pragma fragment
+precision highp float;
+
+in vec4 v_color;
+in vec2 v_texCoord;
+in highp vec2 v_worldPos;
+#ifdef MESH_NORMALS
+in highp vec3 v_worldNormal;
+in highp vec3 v_worldXYZ;
+#endif
+
+#ifdef ES_GBUFFER
+layout(location = 0) out vec4 fragColor;
+layout(location = 1) out vec4 fragSurface;
+
+vec2 packNormal(vec3 n) {
+ n /= (abs(n.x) + abs(n.y) + abs(n.z));
+ vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
+ vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
+ return e * 0.5 + 0.5;
+}
+#else
+out vec4 fragColor;
+#endif
+
+void main() {
+#ifdef MESH_NORMALS
+ highp vec3 P = v_worldXYZ;
+ highp vec3 N = normalize(v_worldNormal);
+#else
+ highp vec3 P = vec3(v_worldPos.x, 0.0, v_worldPos.y);
+ highp vec3 N = vec3(0.0, 1.0, 0.0);
+#endif
+ vec4 w = max(v_color, vec4(0.0));
+ w /= max(dot(w, vec4(1.0)), 1e-4);
+ // The strongest layer wins its seam more cleanly the higher the sharpness.
+ w = pow(w, vec4(u_sharpness));
+ w /= max(dot(w, vec4(1.0)), 1e-4);
+
+ vec3 c0 = texture(u_layer0, P.xz / u_tile0).rgb * u_tint0.rgb;
+ c0 *= mix(1.0, texture(u_layer0, P.xz / (u_tile0 * 7.3)).r * 1.8, u_macroVariation);
+ vec3 c1 = texture(u_layer1, P.xz / u_tile1).rgb * u_tint1.rgb;
+ vec3 c2 = texture(u_layer2, P.xz / u_tile2).rgb * u_tint2.rgb;
+ // Rock is seen from three sides, in proportion to how the ground faces them, so a sheer face is not stretched.
+ vec3 axis = pow(abs(N), vec3(4.0));
+ axis /= dot(axis, vec3(1.0));
+ vec3 c3 = (texture(u_layer3, P.zy / u_tile3).rgb * axis.x
+ + texture(u_layer3, P.xz / u_tile3).rgb * axis.y
+ + texture(u_layer3, P.xy / u_tile3).rgb * axis.z) * u_tint3.rgb;
+ vec3 albedo = c0 * w.x + c1 * w.y + c2 * w.z + c3 * w.w;
+
+ SurfaceExt ext = surfaceExtNone(N);
+ vec3 viewDir = viewDirection(P);
+ vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, u_roughness, 0.5, 1.0, ext);
+ fragColor = vec4(lit, 1.0);
+#ifdef ES_GBUFFER
+ fragSurface = vec4(packNormal(N), u_roughness, 0.75);
+#endif
+}
+#pragma end
+
+#pragma fragment wgsl
+#ifdef ES_GBUFFER
+struct FragOut {
+ @location(0) color : vec4f,
+ @location(1) surface : vec4f,
+};
+
+fn packNormal(n : vec3f) -> vec2f {
+ let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
+ let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
+ var e = a.xy;
+ if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
+ return e * 0.5 + 0.5;
+}
+
+@fragment fn fs_main(v : VSOut) -> FragOut {
+#else
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+#endif
+ g_probeSlot = v.v_probeSlot;
+#ifdef MESH_LIGHTMAP
+ g_lightmap = v.v_lightmap;
+#endif
+#ifdef MESH_NORMALS
+ let P = v.v_worldXYZ;
+ let N = normalize(v.v_worldNormal);
+#else
+ let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
+ let N = vec3f(0.0, 1.0, 0.0);
+#endif
+ // Every sample is taken here, in uniform control flow, where a derivative is allowed.
+ let layer0 = textureSample(u_layer0, u_layer0_s, P.xz / mc.u_tile0).rgb;
+ let macro0 = textureSample(u_layer0, u_layer0_s, P.xz / (mc.u_tile0 * 7.3)).r;
+ let layer1 = textureSample(u_layer1, u_layer1_s, P.xz / mc.u_tile1).rgb;
+ let layer2 = textureSample(u_layer2, u_layer2_s, P.xz / mc.u_tile2).rgb;
+ let rockX = textureSample(u_layer3, u_layer3_s, P.zy / mc.u_tile3).rgb;
+ let rockY = textureSample(u_layer3, u_layer3_s, P.xz / mc.u_tile3).rgb;
+ let rockZ = textureSample(u_layer3, u_layer3_s, P.xy / mc.u_tile3).rgb;
+
+ var w = max(v.v_color, vec4f(0.0));
+ w = w / max(dot(w, vec4f(1.0)), 1e-4);
+ w = pow(w, vec4f(mc.u_sharpness));
+ w = w / max(dot(w, vec4f(1.0)), 1e-4);
+
+ let c0 = layer0 * mc.u_tint0.rgb * mix(1.0, macro0 * 1.8, mc.u_macroVariation);
+ let c1 = layer1 * mc.u_tint1.rgb;
+ let c2 = layer2 * mc.u_tint2.rgb;
+ var axis = pow(abs(N), vec3f(4.0));
+ axis = axis / dot(axis, vec3f(1.0));
+ let c3 = (rockX * axis.x + rockY * axis.y + rockZ * axis.z) * mc.u_tint3.rgb;
+ let albedo = c0 * w.x + c1 * w.y + c2 * w.z + c3 * w.w;
+
+ let ext = surfaceExtNone(N);
+ let viewDir = viewDirection(P);
+ let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, mc.u_roughness, 0.5, 1.0, ext);
+#ifdef ES_GBUFFER
+ var frag : FragOut;
+ frag.color = vec4f(lit, 1.0);
+ frag.surface = vec4f(packNormal(N), mc.u_roughness, 0.75);
+ return frag;
+#else
+ return vec4f(lit, 1.0);
+#endif
+}
+#pragma end
+`;
+
+export const TERRAIN_SHADER_TEMPLATE = makeBuiltinShaderTemplate('terrain', 'Terrain',
+ 'Four texture layers (grass, dirt, snow, rock) weighted by the Terrain component, tiled by world position.', TERRAIN);
diff --git a/sdk/src/terrain/terrainSupport.ts b/sdk/src/terrain/terrainSupport.ts
index 2a99b5cf7..2e6dadbf2 100644
--- a/sdk/src/terrain/terrainSupport.ts
+++ b/sdk/src/terrain/terrainSupport.ts
@@ -6,8 +6,11 @@
* A call, not a file's side effect — see tilemap/tilemapSupport.ts.
*/
import { addEntryPlugin } from '../runtime/entryPlugins';
+import { registerBuiltinShaderTemplate } from '../render/builtinShaders';
+import { TERRAIN_SHADER_TEMPLATE } from './terrainShader';
import { TerrainPlugin } from './terrainPlugin';
export function registerTerrainSupport(): void {
+ registerBuiltinShaderTemplate(TERRAIN_SHADER_TEMPLATE);
addEntryPlugin('terrain', () => new TerrainPlugin());
}
diff --git a/sdk/src/water/waterShader.ts b/sdk/src/water/waterShader.ts
new file mode 100644
index 000000000..5a37b4943
--- /dev/null
+++ b/sdk/src/water/waterShader.ts
@@ -0,0 +1,731 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file waterShader.ts
+ * @brief The Water built-in shader: waves, refraction, reflection, caustics, foam.
+ *
+ * The shader source lives with the module that installs it, not in the core's list of built-in shaders: a package that does not install the module carries none of it.
+ */
+import { makeBuiltinShaderTemplate } from '../render/builtinShaders';
+import { CAUSTICS_GLSL, CAUSTICS_WGSL } from '../render/waterCaustics';
+
+// A horizontal body of water: waves that travel, the scene seen through it bent and tinted by how much water lies in front of it, the sky
+// and the lights reflected off it, and foam where it meets what it lies on and on the crests. It reads the picture of the opaque scene
+// behind it and how far each pixel of that is from the eye, which the frame takes before it draws a material that declares them, so
+// the depth of the water at a pixel is what is measured, not a number the author guessed. Where the device cannot hold the distances
+// (no float target) it falls back to `u_fallbackDepth` as the depth everywhere.
+const WATER = `#pragma shader "Water"
+#pragma version 300 es
+#pragma domain Lit
+// The colour the water scatters back: lighter where it is shallow, darker where it is deep.
+#pragma param u_shallowColor color default(0.10,0.42,0.46,1)
+#pragma param u_deepColor color default(0.01,0.08,0.15,1)
+// How fast each colour is absorbed along the way through the water (red goes first), and how thick the water is, per world unit.
+#pragma param u_absorption color default(0.45,0.09,0.05,1)
+#pragma param u_density float default(0.12) range(0,4) ui(slider)
+// The waves: height, length of the longest, how sharp their crests are drawn, how fast they travel and which way (degrees).
+#pragma param u_waveHeight float default(0.3) range(0,8) ui(slider)
+#pragma param u_waveLength float default(14) range(0.5,300) ui(slider)
+#pragma param u_waveSpeed float default(1) range(0,4) ui(slider)
+#pragma param u_waveDirection float default(30) range(0,360) ui(slider)
+// The small ripples the long waves carry: how steep and how long each is.
+#pragma param u_rippleStrength float default(0.3) range(0,1) ui(slider)
+#pragma param u_rippleLength float default(2.5) range(0.1,40) ui(slider)
+#pragma param u_roughness float default(0.04) range(0,1) ui(slider)
+// How far what is seen through the water is shifted by its waves, and the depth of water at which that stops growing.
+#pragma param u_refractionStrength float default(1) range(0,4) ui(slider)
+#pragma param u_distortionRange float default(12) range(0.1,200) ui(slider)
+// Foam: its colour, how deep the water is where it begins at the shore (world units), and how much of the wave crests carry it.
+#pragma param u_foamColor color default(0.95,0.97,1,1)
+#pragma param u_foamWidth float default(1.2) range(0,20) ui(slider)
+#pragma param u_foamCrest float default(0.5) range(0,1) ui(slider)
+// The depth taken where the device could not measure one.
+#pragma param u_fallbackDepth float default(4) range(0,200) ui(slider)
+// Where the waves are measured. 0 is the world's xz plane, which is what a flat plane of water wants. 1 is the body's own space: the mesh's uv is its
+// local x and z (plus u_origin, which a body that follows the camera moves so the waves stay put), and the waves then lift the geometry and follow the
+// body's tilt, which is what the WaterBody component builds.
+#pragma param u_waveSpace float default(0) range(0,1) ui(slider)
+#pragma param u_origin vec2 default(0,0)
+// The cell size of the mesh's finest ring, for a body the WaterBody component built: a vertex in a coarser ring (its colour's red says which) fades the
+// waves it cannot draw, those shorter than six of its cells. 0 draws every wave at every vertex.
+#pragma param u_cellSize float default(0) range(0,100) ui(slider)
+// A lake's shore, baked by the WaterBody component over its box: red the signed distance to its outline (0.5 at the edge), green how deep the water is,
+// blue the share of the waves kept, all over u_shoreRange. The pixels outside the outline are cut, the waves die toward the shore, and it foams there.
+#pragma param u_shoreOn float default(0) range(0,1) ui(slider)
+#pragma param u_shoreMap texture default(white) texel(data)
+#pragma param u_shoreOrigin vec2 default(0,0)
+#pragma param u_shoreSize vec2 default(1,1)
+#pragma param u_shoreRange float default(16) range(0.01,10000) ui(slider)
+// A river's current: how fast it runs (world units a second, 0 for still water) along the direction the mesh's vertex colour gives (red and green) at the speed
+// its blue says (half is the nominal), and how long, in seconds, each of the two copies of the surface the shader carries downstream lives before the other takes over.
+#pragma param u_flowSpeed float default(0) range(0,50) ui(slider)
+#pragma param u_flowCycle float default(2) range(0.2,20) ui(slider)
+// How much of the waves lifts the geometry: 1 for a lake or an ocean, 0 for a river, whose surface is drawn flat and moved only in the shading.
+#pragma param u_geoWaves float default(1) range(0,1) ui(slider)
+// The net of light the waves throw on what is under the water: how strong it is, how large one cell of it is (world units), and how fast it swims. The Underwater
+// effect draws the same one from below, with these numbers, so the two halves of the picture agree.
+#pragma param u_causticStrength float default(0.6) range(0,3) ui(slider)
+#pragma param u_causticScale float default(2) range(0.05,2000) ui(slider)
+#pragma param u_causticSpeed float default(0.35) range(0,4) ui(slider)
+// What the quality level allows (the WaterBody component writes these): how many of the four waves are summed, whether the surface seen from below mirrors the world under it,
+// and whether the sun's web of light is drawn on what lies under the water.
+#pragma param u_waveCount float default(4) range(1,4) ui(slider)
+#pragma param u_mirror float default(1) range(0,1) ui(slider)
+#pragma param u_causticOn float default(1) range(0,1) ui(slider)
+// The disturbance the water keeps (WaterBody ripples): a window of the body's xz plane, red the height of the ripples (0.5 at rest, u_rippleRange above or below at 1 and 0)
+// and green the foam they left, as the CPU stepped them. u_rippleSize is the window's side, u_rippleTexel one texel of it.
+#pragma param u_rippleOn float default(0) range(0,1) ui(slider)
+#pragma param u_rippleMap texture default(white) texel(data)
+#pragma param u_rippleOrigin vec2 default(0,0)
+#pragma param u_rippleSize float default(64) range(0.01,100000) ui(slider)
+#pragma param u_rippleTexel float default(0.0156) range(0,1) ui(slider)
+#pragma param u_rippleRange float default(1) range(0.0001,1000) ui(slider)
+#pragma param u_sceneColor texture default(white)
+#pragma param u_sceneDepthMap texture default(white) texel(data)
+
+#pragma vertexbounds 16
+#pragma vertexoffset
+// The height of the waves at p: the same four peaked sines the fragment takes its slope from, so the surface that is lit is the one that is drawn.
+float waterWaveHeight(vec2 p, float time, float cell) {
+ const vec4 SET[4] = vec4[4](vec4(0.0, 1.0, 1.0, 1.0), vec4(28.0, 0.62, 0.55, 1.1), vec4(-37.0, 0.37, 0.3, 1.2), vec4(71.0, 0.21, 0.15, 1.35));
+ float height = 0.0;
+ float base = radians(u_waveDirection);
+ for (int i = 0; i < 4; ++i) {
+ if (float(i) >= u_waveCount) break;
+ float length_ = u_waveLength * SET[i].y;
+ float k = 6.2831853 / length_;
+ float a = base + radians(SET[i].x);
+ vec2 d = vec2(sin(a), cos(a));
+ float omega = sqrt(9.8 * k) * u_waveSpeed * SET[i].w;
+ float fade = cell > 0.0 ? clamp((length_ / cell - 3.0) / 3.0, 0.0, 1.0) : 1.0;
+ height += u_waveHeight * SET[i].z * exp(sin(k * dot(d, p) - omega * time) - 1.0) * fade;
+ }
+ return height;
+}
+void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
+ if (u_waveSpace < 0.5) return;
+ float cell = u_cellSize > 0.0 ? u_cellSize * exp2(es_vertexColor.r * 7.0) : 0.0;
+ float damp = u_shoreOn > 0.5 ? es_vertexColor.g : 1.0;
+ local.y += waterWaveHeight(uv + u_origin, u_time.x, cell) * damp * u_geoWaves;
+}
+#pragma end
+
+#pragma vertexoffset wgsl
+fn waterWaveHeight(p : vec2f, time : f32, cell : f32) -> f32 {
+ var wave = array<vec4f, 4>(vec4f(0.0, 1.0, 1.0, 1.0), vec4f(28.0, 0.62, 0.55, 1.1), vec4f(-37.0, 0.37, 0.3, 1.2), vec4f(71.0, 0.21, 0.15, 1.35));
+ var height = 0.0;
+ let base = radians(mc.u_waveDirection);
+ for (var i : i32 = 0; i < 4; i = i + 1) {
+ if (f32(i) >= mc.u_waveCount) { break; }
+ let spec = wave[i];
+ let span = mc.u_waveLength * spec.y;
+ let k = 6.2831853 / span;
+ let a = base + radians(spec.x);
+ let d = vec2f(sin(a), cos(a));
+ let omega = sqrt(9.8 * k) * mc.u_waveSpeed * spec.w;
+ var fade = 1.0;
+ if (cell > 0.0) { fade = clamp((span / cell - 3.0) / 3.0, 0.0, 1.0); }
+ height = height + mc.u_waveHeight * spec.z * exp(sin(k * dot(d, p) - omega * time) - 1.0) * fade;
+ }
+ return height;
+}
+fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, vec3f>, uv : vec2f) {
+ if (mc.u_waveSpace < 0.5) { return; }
+ var cell = 0.0;
+ if (mc.u_cellSize > 0.0) { cell = mc.u_cellSize * exp2(es_vertexColor.r * 7.0); }
+ var damp = 1.0;
+ if (mc.u_shoreOn > 0.5) { damp = es_vertexColor.g; }
+ (*local).y = (*local).y + waterWaveHeight(uv + mc.u_origin, tc.u_time.x, cell) * damp * mc.u_geoWaves;
+}
+#pragma end
+
+#pragma fragment
+precision highp float;
+
+in vec4 v_color;
+in vec2 v_texCoord;
+in highp vec2 v_worldPos;
+#ifdef MESH_NORMALS
+in highp vec3 v_worldNormal;
+in highp vec3 v_worldXYZ;
+#endif
+
+#ifdef ES_GBUFFER
+layout(location = 0) out vec4 fragColor;
+layout(location = 1) out vec4 fragSurface;
+
+vec2 packNormal(vec3 n) {
+ n /= (abs(n.x) + abs(n.y) + abs(n.z));
+ vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
+ vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
+ return e * 0.5 + 0.5;
+}
+#else
+out vec4 fragColor;
+#endif
+
+float waterHash(vec2 p) {
+ p = fract(p * vec2(0.1031, 0.1030));
+ p += dot(p, p.yx + 33.33);
+ return fract((p.x + p.y) * p.x);
+}
+float waterNoise(vec2 p) {
+ vec2 i = floor(p);
+ vec2 f = fract(p);
+ f = f * f * (3.0 - 2.0 * f);
+ return mix(mix(waterHash(i), waterHash(i + vec2(1.0, 0.0)), f.x),
+ mix(waterHash(i + vec2(0.0, 1.0)), waterHash(i + vec2(1.0, 1.0)), f.x), f.y);
+}
+/// The ripples' height at p: two layers of noise drifting in different directions.
+float ripples(vec2 p, float time) {
+ return waterNoise(p / u_rippleLength + vec2(0.31, 0.12) * time) * 0.6
+ + waterNoise(p / (u_rippleLength * 0.43) - vec2(0.17, 0.4) * time) * 0.4;
+}
+
+/// The slope of the water's surface at p (d height / d x, d height / d z) and how near the waves are to a crest, 0 to 1.
+/// Four waves a few directions apart, each a peaked sine (exp(sin - 1)) whose crests are narrower than its troughs.
+vec2 waveSlope(vec2 p, float time, float dist, out float crest) {
+ const vec4 SET[4] = vec4[4](vec4(0.0, 1.0, 1.0, 1.0), vec4(28.0, 0.62, 0.55, 1.1), vec4(-37.0, 0.37, 0.3, 1.2), vec4(71.0, 0.21, 0.15, 1.35));
+ vec2 slope = vec2(0.0);
+ float crestSum = 0.0;
+ float weightSum = 0.0;
+ float base = radians(u_waveDirection);
+ for (int i = 0; i < 4; ++i) {
+ if (float(i) >= u_waveCount) break;
+ float length_ = u_waveLength * SET[i].y;
+ float k = 6.2831853 / length_;
+ float a = base + radians(SET[i].x);
+ vec2 d = vec2(sin(a), cos(a));
+ float omega = sqrt(9.8 * k) * u_waveSpeed * SET[i].w;
+ float theta = k * dot(d, p) - omega * time;
+ float e = exp(sin(theta) - 1.0);
+ // A wave much shorter than a pixel is noise, not a wave: it fades out with distance.
+ float seen = clamp(1.0 - dist / (length_ * 220.0), 0.0, 1.0);
+ slope += d * (u_waveHeight * SET[i].z * e * cos(theta) * k * seen);
+ crestSum += e * SET[i].z;
+ weightSum += SET[i].z;
+ }
+ crest = crestSum / weightSum;
+ return slope;
+}
+
+${CAUSTICS_GLSL}
+/// The first directional light that shines: xyz is where it stands in the sky, w 1 if there is one, color its colour and strength, tiles its place in the shadow atlas.
+void waterSun(out vec4 dir, out vec4 color, out ivec2 tiles) {
+ dir = vec4(0.0);
+ color = vec4(0.0);
+ tiles = ivec2(0);
+ for (int i = 0; i < 16; ++i) {
+ Light l = u_lights[i];
+ if (l.posDir.z > 0.5 && l.posDir.z < 1.5 && l.color.a > 0.0) {
+ dir = vec4(-normalize(vec3(l.posDir.x, l.posDir.y, l.posDir.w)), 1.0);
+ color = l.color;
+ tiles = ivec2(int(l.shadowMap.x), int(l.shadowMap.y));
+ return;
+ }
+ }
+}
+
+/// Whether the sun reaches a point: one tap of the shadow map, 1 where there is none.
+float waterSunVisibility(vec3 worldPos, int first, int count) {
+#ifdef ES_RECEIVE_SHADOW
+ if (u_shadowParams.x < 0.5 || count <= 0) return 1.0;
+ for (int k = 0; k < 16; ++k) {
+ if (k >= count) break;
+ int t = first + k;
+ vec4 c = u_shadowMatrix[t] * vec4(worldPos, 1.0);
+ if (c.w <= 0.0) continue;
+ vec3 proj = c.xyz / c.w;
+ vec2 uv = proj.xy * 0.5 + 0.5;
+ if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z < 0.0 || proj.z > 1.0) continue;
+ vec4 rect = u_shadowTile[t];
+ vec2 at = rect.xy + uv * rect.z;
+ vec2 lo = rect.xy + u_shadowParams.y * 0.5;
+ vec2 hi = rect.xy + rect.z - u_shadowParams.y * 0.5;
+ float d = unpackDepth(textureLod(u_shadowMap, clamp(at, lo, hi), 0.0).rgb);
+ return d < clamp(proj.z * 0.5 + 0.5, 0.0, 1.0) - 0.0025 ? 0.0 : 1.0;
+ }
+#endif
+ return 1.0;
+}
+
+/// What a ray from p along r meets in the picture of the scene the frame copied, as a colour with alpha 1, or alpha 0 where it meets nothing on screen.
+vec4 waterReflect(vec3 p, vec3 r, float eyeDist) {
+ float stride = max(0.5, eyeDist * 0.03);
+ float travelled = stride;
+ for (int i = 0; i < 20; ++i) {
+ vec3 q = p + r * travelled;
+ vec4 clip = u_projection * vec4(q, 1.0);
+ if (clip.w <= 1e-4) break;
+ vec2 uv = clip.xy / clip.w * 0.5 + 0.5;
+ if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) break;
+ float qDist = u_camera.w > 0.5 ? length(q - u_camera.xyz) : -dot(q, u_camera.xyz);
+ float sceneDist = textureLod(u_sceneDepthMap, uv, 0.0).r;
+ // The ray has gone behind something the picture holds: that is what it met, if it did not skip clean through a thin one.
+ if (sceneDist < qDist && qDist - sceneDist < stride * 2.0 + 1.0) return vec4(textureLod(u_sceneColor, uv, 0.0).rgb, 1.0);
+ stride *= 1.25;
+ travelled += stride;
+ }
+ return vec4(0.0);
+}
+
+/// The slope of the surface at p and how near it is to a crest: the waves, and the ripples they carry, flattened with distance.
+vec2 waterSurface(vec2 p, float time, float dist, out float crest) {
+ vec2 slope = waveSlope(p, time, dist, crest);
+ float rippleFade = clamp(1.0 - dist / (u_rippleLength * 180.0), 0.0, 1.0);
+ float e = u_rippleLength * 0.1;
+ vec2 ripple = vec2(ripples(p + vec2(e, 0.0), time) - ripples(p - vec2(e, 0.0), time),
+ ripples(p + vec2(0.0, e), time) - ripples(p - vec2(0.0, e), time)) / (2.0 * e);
+ return slope + ripple * (u_rippleStrength * u_rippleLength * rippleFade);
+}
+
+void main() {
+#ifdef MESH_NORMALS
+ highp vec3 P = v_worldXYZ;
+#else
+ highp vec3 P = vec3(v_worldPos.x, 0.0, v_worldPos.y);
+#endif
+ float time = u_time.x;
+ vec3 viewDir = viewDirection(P);
+ float waterDist = u_camera.w > 0.5 ? length(P - u_camera.xyz) : -dot(P, u_camera.xyz);
+
+ // The surface: the long waves, then the ripples they carry, flattened with distance so the far water is calm rather than aliased.
+ vec2 wp = u_waveSpace > 0.5 ? v_texCoord + u_origin : P.xz;
+ // The lake's shore at this pixel (a texture read, taken before any branch).
+ vec4 shoreTex = texture(u_shoreMap, clamp((v_texCoord - u_shoreOrigin) / max(u_shoreSize, vec2(1e-4)), vec2(0.0), vec2(1.0)));
+ float shoreDist = (shoreTex.r - 0.5) * 2.0 * u_shoreRange;
+ float shoreDepth = shoreTex.g * u_shoreRange;
+ float damp = u_shoreOn > 0.5 ? shoreTex.b : 1.0;
+ // The ripples at this pixel: their height on four sides gives the slope they add, and the foam is what they left (reads taken before any branch).
+ vec2 ripUv = (wp - u_rippleOrigin) / max(u_rippleSize, 1e-4);
+ vec4 ripC = texture(u_rippleMap, ripUv);
+ vec2 ripStep = vec2(u_rippleTexel);
+ float ripCell = max(u_rippleSize * u_rippleTexel, 1e-5);
+ float ripL = texture(u_rippleMap, ripUv - vec2(ripStep.x, 0.0)).r, ripR = texture(u_rippleMap, ripUv + vec2(ripStep.x, 0.0)).r;
+ float ripD = texture(u_rippleMap, ripUv - vec2(0.0, ripStep.y)).r, ripU = texture(u_rippleMap, ripUv + vec2(0.0, ripStep.y)).r;
+ bool ripIn = u_rippleOn > 0.5 && u_waveSpace > 0.5 && all(greaterThanEqual(ripUv, vec2(0.0))) && all(lessThanEqual(ripUv, vec2(1.0)));
+ vec2 ripSlope = ripIn ? vec2(ripR - ripL, ripU - ripD) * u_rippleRange / ripCell : vec2(0.0);
+ float ripFoam = ripIn ? ripC.g : 0.0;
+ float crest;
+ vec2 slope;
+ if (u_flowSpeed > 0.0) {
+ // A river: the surface rides the current. Two copies of it are carried downstream a cycle at a time, each fading in as the other fades
+ // out, so the moment one is put back at its start is hidden by the other.
+ vec2 flow = (v_color.rg * 2.0 - 1.0) * (v_color.b * 2.0) * u_flowSpeed;
+ float cycle = max(u_flowCycle, 0.1);
+ float ph0 = fract(time / cycle);
+ float ph1 = fract(time / cycle + 0.5);
+ float w0 = 1.0 - abs(2.0 * ph0 - 1.0);
+ float crest0, crest1;
+ vec2 s0 = waterSurface(wp - flow * (ph0 * cycle), time, waterDist, crest0);
+ vec2 s1 = waterSurface(wp - flow * (ph1 * cycle), time, waterDist, crest1);
+ slope = mix(s1, s0, w0);
+ crest = mix(crest1, crest0, w0);
+ } else {
+ slope = waterSurface(wp, time, waterDist, crest);
+ }
+ slope *= damp;
+ crest *= damp;
+ slope += ripSlope;
+ vec3 N = normalize(vec3(-slope.x, 1.0, -slope.y));
+ vec3 Ngeo = vec3(0.0, 1.0, 0.0);
+#ifdef MESH_NORMALS
+ if (u_waveSpace > 0.5) {
+ // The body's own frame: the mesh's normal is its up (the hook lifts the vertices and leaves it alone), and the slope leans along
+ // its uv axes, which are its local x and z.
+ Ngeo = normalize(v_worldNormal);
+ N = perturbNormal(Ngeo, P, v_texCoord, normalize(vec3(-slope.x, -slope.y, 1.0)));
+ }
+#endif
+ // Seen from below, the surface turns over, and light goes the other way through it: out of the water, into the air.
+ bool below = dot(viewDir, Ngeo) < 0.0;
+ if (below) { N = -N; Ngeo = -Ngeo; }
+
+ // What is behind the water: the picture and the distance, where the frame took them.
+ bool haveColor = texture(u_sceneColor, vec2(0.5)).a < 0.75;
+ bool haveDepth = haveColor && texture(u_sceneDepthMap, vec2(0.5)).a < 0.75;
+ vec4 clip0 = u_projection * vec4(P, 1.0);
+ vec2 uv0 = clip0.xy / max(clip0.w, 1e-4) * 0.5 + 0.5;
+ float behindDist = haveDepth ? texture(u_sceneDepthMap, clamp(uv0, vec2(0.001), vec2(0.999))).r : waterDist + u_fallbackDepth;
+ float column0 = max(behindDist - waterDist, 0.0);
+
+ // Bent by the waves: the ray through the tilted surface against the ray through a flat one, over as much water as there is.
+ float RATIO = below ? 1.333 : 1.0 / 1.333;
+ vec3 bentWave = refract(-viewDir, N, RATIO);
+ vec3 bentFlat = refract(-viewDir, Ngeo, RATIO);
+ // Past the critical angle (about 49 degrees) nothing gets out of the water: refract gives nothing, and the surface is a mirror.
+ if (below && (dot(bentWave, bentWave) < 0.5 || dot(bentFlat, bentFlat) < 0.5)) { bentWave = bentFlat = vec3(0.0); }
+ float reach = min(column0, u_distortionRange) * u_refractionStrength;
+ vec4 clip1 = u_projection * vec4(P + (bentWave - bentFlat) * reach, 1.0);
+ vec2 uv1 = clip1.xy / max(clip1.w, 1e-4) * 0.5 + 0.5;
+ vec3 behind = vec3(0.0);
+ float column = column0;
+ if (haveColor) {
+ uv1 = clamp(uv1, vec2(0.001), vec2(0.999));
+ float dist1 = haveDepth ? texture(u_sceneDepthMap, uv1).r : behindDist;
+ // Something standing in front of the water cannot be what is seen through it: keep the straight-through lookup.
+ if (dist1 < waterDist) { uv1 = clamp(uv0, vec2(0.001), vec2(0.999)); dist1 = behindDist; }
+ behind = texture(u_sceneColor, uv1).rgb;
+ column = max(dist1 - waterDist, 0.0);
+ // The sun through the waves lights what is under them in a web of bright threads, where the sun reaches it and as much as the water between lets through.
+ if (!below && haveDepth && u_causticStrength > 0.0 && u_causticOn > 0.5) {
+ vec4 sunDir, sunColor;
+ ivec2 sunTiles;
+ waterSun(sunDir, sunColor, sunTiles);
+ float cosUp = dot(sunDir.xyz, Ngeo);
+ if (sunDir.w > 0.5 && cosUp > 0.05) {
+ vec3 seabed = P + bentWave * column;
+ float depthBelow = max(dot(P - seabed, Ngeo), 0.0);
+ vec3 onSurface = seabed + sunDir.xyz * (depthBelow / max(cosUp, 0.15));
+ float c = causticPattern(onSurface.xz / u_causticScale, time * u_causticSpeed);
+ float reachSun = exp(-dot(u_absorption.rgb, vec3(1.0 / 3.0)) * u_density * depthBelow / max(cosUp, 0.15));
+ behind *= 1.0 + u_causticStrength * c * cosUp * waterSunVisibility(seabed, sunTiles.x, sunTiles.y) * reachSun * min(sunColor.a, 2.0) * 0.5;
+ }
+ }
+ }
+
+ // The water in between takes the light out of it, red first, and gives back its own colour: more of it the deeper it goes.
+ vec3 transmit = exp(-u_absorption.rgb * (u_density * column));
+ // From below the water between the eye and the surface is the camera's own medium (the underwater effect fogs it), not a column behind the surface.
+ if (below) transmit = vec3(1.0);
+ float murk = 1.0 - dot(transmit, vec3(1.0 / 3.0));
+ vec3 scatterColor = mix(u_shallowColor.rgb, u_deepColor.rgb, 1.0 - exp(-column * u_density * 0.5));
+
+ // Foam: along the shore, where the water is shallow, broken by noise that moves; and on the crests of the waves.
+ float vertical = column * max(abs(viewDir.y), 0.15);
+ float foamScale = 1.0 / (u_rippleLength * 1.2);
+ float breakup = waterNoise(wp * foamScale + vec2(time * 0.07, time * 0.05)) * 0.6 + waterNoise(wp * foamScale * 3.0 - vec2(time * 0.13, 0.0)) * 0.4;
+ float pulse = 0.65 + 0.35 * sin(time * 0.9 + breakup * 6.0);
+ float shore = haveDepth && u_foamWidth > 0.0 ? clamp((1.0 - vertical / (u_foamWidth * pulse)) * 1.5 - breakup * 0.8, 0.0, 1.0) : 0.0;
+ // The baked shore foams too, where the depth buffer cannot say (no chain, no float targets) and along the lake's outline.
+ if (u_shoreOn > 0.5 && u_foamWidth > 0.0) {
+ shore = max(shore, clamp((1.0 - min(shoreDist, shoreDepth) / (u_foamWidth * pulse)) * 1.5 - breakup * 0.8, 0.0, 1.0));
+ }
+ float onCrest = u_foamCrest > 0.0 ? smoothstep(1.0 - 0.3 * u_foamCrest, 1.0, crest) * smoothstep(0.35, 0.75, breakup) : 0.0;
+ float foam = clamp(max(max(shore, onCrest * u_foamCrest), ripFoam * (0.55 + 0.45 * breakup)), 0.0, 1.0);
+
+ SurfaceExt ext = surfaceExtNone(N);
+ ext.misc.x = 0.0204;
+ vec3 albedo = mix(scatterColor * murk, u_foamColor.rgb, foam);
+ float roughness = mix(u_roughness, 0.7, foam);
+ vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 1.0, 1.0, ext);
+ float cosV = clamp(dot(N, viewDir), 0.0, 1.0);
+ float fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosV, 5.0);
+ if (below) {
+ // Out of the water the glancing rays are reflected whole: Snell's window, a cone of sky overhead in a mirror of the water below it.
+ float sinT = 1.333 * sqrt(max(1.0 - cosV * cosV, 0.0));
+ float cosT = sqrt(max(1.0 - sinT * sinT, 0.0));
+ fresnel = sinT >= 1.0 ? 1.0 : 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosT, 5.0);
+ }
+ vec3 seen = behind * transmit * (1.0 - fresnel) * (1.0 - foam);
+ // From below the water is a mirror past the critical angle, and what it mirrors is the world under it: the picture, followed along the reflected ray.
+ vec3 mirror = vec3(0.0);
+ if (below && fresnel > 0.02 && haveColor && haveDepth && u_mirror > 0.5) {
+ vec4 met = waterReflect(P, reflect(-viewDir, N), waterDist);
+ mirror = met.a > 0.5 ? met.rgb : mix(u_shallowColor.rgb, u_deepColor.rgb, 0.5) * 0.25;
+ }
+ seen += mirror * fresnel * (1.0 - foam);
+ // Outside the lake's outline there is no water.
+ if (u_shoreOn > 0.5 && shoreDist < 0.0) discard;
+ fragColor = vec4(lit + seen, haveColor ? 1.0 : 0.85);
+#ifdef ES_GBUFFER
+ fragSurface = vec4(packNormal(normalize(N)), roughness, 0.75);
+#endif
+}
+#pragma end
+
+#pragma fragment wgsl
+#ifdef ES_GBUFFER
+struct FragOut {
+ @location(0) color : vec4f,
+ @location(1) surface : vec4f,
+};
+
+fn packNormal(n : vec3f) -> vec2f {
+ let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
+ let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
+ var e = a.xy;
+ if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
+ return e * 0.5 + 0.5;
+}
+#endif
+
+fn waterHash(p0 : vec2f) -> f32 {
+ var p = fract(p0 * vec2f(0.1031, 0.1030));
+ p = p + dot(p, p.yx + 33.33);
+ return fract((p.x + p.y) * p.x);
+}
+fn waterNoise(p : vec2f) -> f32 {
+ let i = floor(p);
+ var f = fract(p);
+ f = f * f * (3.0 - 2.0 * f);
+ return mix(mix(waterHash(i), waterHash(i + vec2f(1.0, 0.0)), f.x),
+ mix(waterHash(i + vec2f(0.0, 1.0)), waterHash(i + vec2f(1.0, 1.0)), f.x), f.y);
+}
+fn ripples(p : vec2f, time : f32) -> f32 {
+ return waterNoise(p / mc.u_rippleLength + vec2f(0.31, 0.12) * time) * 0.6
+ + waterNoise(p / (mc.u_rippleLength * 0.43) - vec2f(0.17, 0.4) * time) * 0.4;
+}
+
+struct Waves { slope : vec2f, crest : f32 };
+
+fn waveSlope(p : vec2f, time : f32, dist : f32) -> Waves {
+ var wave = array<vec4f, 4>(vec4f(0.0, 1.0, 1.0, 1.0), vec4f(28.0, 0.62, 0.55, 1.1), vec4f(-37.0, 0.37, 0.3, 1.2), vec4f(71.0, 0.21, 0.15, 1.35));
+ var slope = vec2f(0.0);
+ var crestSum = 0.0;
+ var weightSum = 0.0;
+ let base = radians(mc.u_waveDirection);
+ for (var i : i32 = 0; i < 4; i = i + 1) {
+ if (f32(i) >= mc.u_waveCount) { break; }
+ let spec = wave[i];
+ let span = mc.u_waveLength * spec.y;
+ let k = 6.2831853 / span;
+ let a = base + radians(spec.x);
+ let d = vec2f(sin(a), cos(a));
+ let omega = sqrt(9.8 * k) * mc.u_waveSpeed * spec.w;
+ let theta = k * dot(d, p) - omega * time;
+ let e = exp(sin(theta) - 1.0);
+ let seen = clamp(1.0 - dist / (span * 220.0), 0.0, 1.0);
+ slope = slope + d * (mc.u_waveHeight * spec.z * e * cos(theta) * k * seen);
+ crestSum = crestSum + e * spec.z;
+ weightSum = weightSum + spec.z;
+ }
+ var out : Waves;
+ out.slope = slope;
+ out.crest = crestSum / weightSum;
+ return out;
+}
+
+${CAUSTICS_WGSL}
+struct WaterSun { dir : vec4f, color : vec4f, tiles : vec2i };
+/// The first directional light that shines: dir.xyz is where it stands in the sky, dir.w 1 if there is one.
+fn waterSun() -> WaterSun {
+ var s : WaterSun;
+ s.dir = vec4f(0.0);
+ s.color = vec4f(0.0);
+ s.tiles = vec2i(0, 0);
+ for (var i = 0; i < 16; i++) {
+ let l = lc.u_lights[i];
+ if (l.posDir.z > 0.5 && l.posDir.z < 1.5 && l.color.a > 0.0) {
+ s.dir = vec4f(-normalize(vec3f(l.posDir.x, l.posDir.y, l.posDir.w)), 1.0);
+ s.color = l.color;
+ s.tiles = vec2i(i32(l.shadowMap.x), i32(l.shadowMap.y));
+ return s;
+ }
+ }
+ return s;
+}
+
+fn waterSunVisibility(worldPos : vec3f, first : i32, count : i32) -> f32 {
+ if (lc.u_shadowParams.x < 0.5 || count <= 0) { return 1.0; }
+ for (var k = 0; k < 16; k++) {
+ if (k >= count) { break; }
+ let t = first + k;
+ let c = lc.u_shadowMatrix[t] * vec4f(worldPos, 1.0);
+ if (c.w <= 0.0) { continue; }
+ let proj = c.xyz / c.w;
+ let uv = proj.xy * 0.5 + 0.5;
+ if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z < 0.0 || proj.z > 1.0) { continue; }
+ let rect = lc.u_shadowTile[t];
+ let base = vec2f(rect.x, 1.0 - rect.y - rect.z);
+ let at = base + vec2f(uv.x, 1.0 - uv.y) * rect.z;
+ let lo = base + lc.u_shadowParams.y * 0.5;
+ let hi = base + rect.z - lc.u_shadowParams.y * 0.5;
+ let d = unpackDepth(textureSampleLevel(t2, s2, clamp(at, lo, hi), 0.0).rgb);
+ return select(1.0, 0.0, d < clamp(proj.z * 0.5 + 0.5, 0.0, 1.0) - 0.0025);
+ }
+ return 1.0;
+}
+
+/// What a ray from p along r meets in the picture of the scene the frame copied: the colour (xyz) and 1 (w), or w 0 where it meets nothing on screen.
+fn waterReflect(p : vec3f, r : vec3f, eyeDist : f32) -> vec4f {
+ var stride = max(0.5, eyeDist * 0.03);
+ var travelled = stride;
+ for (var i = 0; i < 20; i++) {
+ let q = p + r * travelled;
+ let clip = frame.projection * vec4f(q, 1.0);
+ if (clip.w <= 1e-4) { break; }
+ let uv = vec2f(clip.x / clip.w * 0.5 + 0.5, 0.5 - clip.y / clip.w * 0.5);
+ if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { break; }
+ var qDist = -dot(q, frame.camera.xyz);
+ if (frame.camera.w > 0.5) { qDist = length(q - frame.camera.xyz); }
+ let sceneDist = textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, uv, 0.0).r;
+ if (sceneDist < qDist && qDist - sceneDist < stride * 2.0 + 1.0) { return vec4f(textureSampleLevel(u_sceneColor, u_sceneColor_s, uv, 0.0).rgb, 1.0); }
+ stride = stride * 1.25;
+ travelled = travelled + stride;
+ }
+ return vec4f(0.0);
+}
+
+/// The slope of the surface at p (x, y) and how near it is to a crest (z): the waves, and the ripples they carry, flattened with distance.
+fn waterSurface(p : vec2f, time : f32, dist : f32) -> vec3f {
+ let waves = waveSlope(p, time, dist);
+ let rippleFade = clamp(1.0 - dist / (mc.u_rippleLength * 180.0), 0.0, 1.0);
+ let eps = mc.u_rippleLength * 0.1;
+ let ripple = vec2f(ripples(p + vec2f(eps, 0.0), time) - ripples(p - vec2f(eps, 0.0), time),
+ ripples(p + vec2f(0.0, eps), time) - ripples(p - vec2f(0.0, eps), time)) / (2.0 * eps);
+ return vec3f(waves.slope + ripple * (mc.u_rippleStrength * mc.u_rippleLength * rippleFade), waves.crest);
+}
+
+#ifdef ES_GBUFFER
+@fragment fn fs_main(v : VSOut) -> FragOut {
+#else
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+#endif
+ g_probeSlot = v.v_probeSlot;
+#ifdef MESH_LIGHTMAP
+ g_lightmap = v.v_lightmap;
+#endif
+#ifdef MESH_NORMALS
+ let P = v.v_worldXYZ;
+#else
+ let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
+#endif
+ let time = tc.u_time.x;
+ let viewDir = viewDirection(P);
+ var waterDist = -dot(P, frame.camera.xyz);
+ if (frame.camera.w > 0.5) { waterDist = length(P - frame.camera.xyz); }
+
+ var wp = P.xz;
+ if (mc.u_waveSpace > 0.5) { wp = v.v_texCoord + mc.u_origin; }
+ let shoreTex = textureSample(u_shoreMap, u_shoreMap_s, clamp((v.v_texCoord - mc.u_shoreOrigin) / max(mc.u_shoreSize, vec2f(1e-4)), vec2f(0.0), vec2f(1.0)));
+ let shoreDist = (shoreTex.r - 0.5) * 2.0 * mc.u_shoreRange;
+ let shoreDepth = shoreTex.g * mc.u_shoreRange;
+ var damp = 1.0;
+ if (mc.u_shoreOn > 0.5) { damp = shoreTex.b; }
+ // The ripples at this pixel: their height on four sides gives the slope they add, and the foam is what they left (taken in uniform control flow).
+ let ripUv = (wp - mc.u_rippleOrigin) / max(mc.u_rippleSize, 1e-4);
+ let ripC = textureSample(u_rippleMap, u_rippleMap_s, ripUv);
+ let ripCell = max(mc.u_rippleSize * mc.u_rippleTexel, 1e-5);
+ let ripL = textureSample(u_rippleMap, u_rippleMap_s, ripUv - vec2f(mc.u_rippleTexel, 0.0)).r;
+ let ripR = textureSample(u_rippleMap, u_rippleMap_s, ripUv + vec2f(mc.u_rippleTexel, 0.0)).r;
+ let ripD = textureSample(u_rippleMap, u_rippleMap_s, ripUv - vec2f(0.0, mc.u_rippleTexel)).r;
+ let ripU = textureSample(u_rippleMap, u_rippleMap_s, ripUv + vec2f(0.0, mc.u_rippleTexel)).r;
+ let ripIn = mc.u_rippleOn > 0.5 && mc.u_waveSpace > 0.5 && all(ripUv >= vec2f(0.0)) && all(ripUv <= vec2f(1.0));
+ var ripSlope = vec2f(0.0);
+ var ripFoam = 0.0;
+ if (ripIn) { ripSlope = vec2f(ripR - ripL, ripU - ripD) * mc.u_rippleRange / ripCell; ripFoam = ripC.g; }
+ var surface = vec3f(0.0);
+ if (mc.u_flowSpeed > 0.0) {
+ // A river: two copies of the surface carried downstream a cycle at a time, each fading in as the other fades out.
+ let flow = (v.v_color.rg * 2.0 - vec2f(1.0)) * (v.v_color.b * 2.0) * mc.u_flowSpeed;
+ let cycle = max(mc.u_flowCycle, 0.1);
+ let ph0 = fract(time / cycle);
+ let ph1 = fract(time / cycle + 0.5);
+ let w0 = 1.0 - abs(2.0 * ph0 - 1.0);
+ let s0 = waterSurface(wp - flow * (ph0 * cycle), time, waterDist);
+ let s1 = waterSurface(wp - flow * (ph1 * cycle), time, waterDist);
+ surface = mix(s1, s0, w0);
+ } else {
+ surface = waterSurface(wp, time, waterDist);
+ }
+ var waves : Waves;
+ waves.crest = surface.z * damp;
+ var slope = surface.xy * damp + ripSlope;
+ var N = normalize(vec3f(-slope.x, 1.0, -slope.y));
+ var Ngeo = vec3f(0.0, 1.0, 0.0);
+#ifdef MESH_NORMALS
+ if (mc.u_waveSpace > 0.5) {
+ Ngeo = normalize(v.v_worldNormal);
+ N = perturbNormal(Ngeo, P, v.v_texCoord, normalize(vec3f(-slope.x, -slope.y, 1.0)));
+ }
+#endif
+ let below = dot(viewDir, Ngeo) < 0.0;
+ if (below) { N = -N; Ngeo = -Ngeo; }
+
+ let haveColor = textureSampleLevel(u_sceneColor, u_sceneColor_s, vec2f(0.5), 0.0).a < 0.75;
+ let haveDepth = haveColor && textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, vec2f(0.5), 0.0).a < 0.75;
+ let clip0 = frame.projection * vec4f(P, 1.0);
+ let w0 = max(clip0.w, 1e-4);
+ // This device's rows run down the texture, so clip y is flipped on the way to a uv.
+ let uv0 = vec2f(clip0.x / w0 * 0.5 + 0.5, 0.5 - clip0.y / w0 * 0.5);
+ var behindDist = waterDist + mc.u_fallbackDepth;
+ if (haveDepth) { behindDist = textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, clamp(uv0, vec2f(0.001), vec2f(0.999)), 0.0).r; }
+ let column0 = max(behindDist - waterDist, 0.0);
+
+ var ratio = 1.0 / 1.333;
+ if (below) { ratio = 1.333; }
+ var bentWave = refract(-viewDir, N, ratio);
+ var bentFlat = refract(-viewDir, Ngeo, ratio);
+ if (below && (dot(bentWave, bentWave) < 0.5 || dot(bentFlat, bentFlat) < 0.5)) { bentWave = vec3f(0.0); bentFlat = vec3f(0.0); }
+ let reach = min(column0, mc.u_distortionRange) * mc.u_refractionStrength;
+ let clip1 = frame.projection * vec4f(P + (bentWave - bentFlat) * reach, 1.0);
+ let w1 = max(clip1.w, 1e-4);
+ var uv1 = vec2f(clip1.x / w1 * 0.5 + 0.5, 0.5 - clip1.y / w1 * 0.5);
+ var behind = vec3f(0.0);
+ var column = column0;
+ if (haveColor) {
+ uv1 = clamp(uv1, vec2f(0.001), vec2f(0.999));
+ var dist1 = behindDist;
+ if (haveDepth) { dist1 = textureSampleLevel(u_sceneDepthMap, u_sceneDepthMap_s, uv1, 0.0).r; }
+ if (dist1 < waterDist) { uv1 = clamp(uv0, vec2f(0.001), vec2f(0.999)); dist1 = behindDist; }
+ behind = textureSampleLevel(u_sceneColor, u_sceneColor_s, uv1, 0.0).rgb;
+ column = max(dist1 - waterDist, 0.0);
+ if (!below && haveDepth && mc.u_causticStrength > 0.0 && mc.u_causticOn > 0.5) {
+ let sun = waterSun();
+ let cosUp = dot(sun.dir.xyz, Ngeo);
+ if (sun.dir.w > 0.5 && cosUp > 0.05) {
+ let seabed = P + bentWave * column;
+ let depthBelow = max(dot(P - seabed, Ngeo), 0.0);
+ let onSurface = seabed + sun.dir.xyz * (depthBelow / max(cosUp, 0.15));
+ let c = causticPattern(onSurface.xz / mc.u_causticScale, time * mc.u_causticSpeed);
+ let reachSun = exp(-dot(mc.u_absorption.rgb, vec3f(1.0 / 3.0)) * mc.u_density * depthBelow / max(cosUp, 0.15));
+ behind = behind * (1.0 + mc.u_causticStrength * c * cosUp * waterSunVisibility(seabed, sun.tiles.x, sun.tiles.y) * reachSun * min(sun.color.a, 2.0) * 0.5);
+ }
+ }
+ }
+
+ var transmit = exp(-mc.u_absorption.rgb * (mc.u_density * column));
+ if (below) { transmit = vec3f(1.0); }
+ let murk = 1.0 - dot(transmit, vec3f(1.0 / 3.0));
+ let scatterColor = mix(mc.u_shallowColor.rgb, mc.u_deepColor.rgb, vec3f(1.0 - exp(-column * mc.u_density * 0.5)));
+
+ let vertical = column * max(abs(viewDir.y), 0.15);
+ let foamScale = 1.0 / (mc.u_rippleLength * 1.2);
+ let breakup = waterNoise(wp * foamScale + vec2f(time * 0.07, time * 0.05)) * 0.6 + waterNoise(wp * foamScale * 3.0 - vec2f(time * 0.13, 0.0)) * 0.4;
+ let pulse = 0.65 + 0.35 * sin(time * 0.9 + breakup * 6.0);
+ var shore = 0.0;
+ if (haveDepth && mc.u_foamWidth > 0.0) { shore = clamp((1.0 - vertical / (mc.u_foamWidth * pulse)) * 1.5 - breakup * 0.8, 0.0, 1.0); }
+ if (mc.u_shoreOn > 0.5 && mc.u_foamWidth > 0.0) {
+ shore = max(shore, clamp((1.0 - min(shoreDist, shoreDepth) / (mc.u_foamWidth * pulse)) * 1.5 - breakup * 0.8, 0.0, 1.0));
+ }
+ var onCrest = 0.0;
+ if (mc.u_foamCrest > 0.0) { onCrest = smoothstep(1.0 - 0.3 * mc.u_foamCrest, 1.0, waves.crest) * smoothstep(0.35, 0.75, breakup); }
+ let foam = clamp(max(max(shore, onCrest * mc.u_foamCrest), ripFoam * (0.55 + 0.45 * breakup)), 0.0, 1.0);
+
+ var ext = surfaceExtNone(N);
+ ext.misc.x = 0.0204;
+ let albedo = mix(scatterColor * murk, mc.u_foamColor.rgb, vec3f(foam));
+ let roughness = mix(mc.u_roughness, 0.7, foam);
+ let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 1.0, 1.0, ext);
+ let cosV = clamp(dot(N, viewDir), 0.0, 1.0);
+ var fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosV, 5.0);
+ if (below) {
+ let sinT = 1.333 * sqrt(max(1.0 - cosV * cosV, 0.0));
+ let cosT = sqrt(max(1.0 - sinT * sinT, 0.0));
+ if (sinT >= 1.0) { fresnel = 1.0; } else { fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosT, 5.0); }
+ }
+ var seen = behind * transmit * (1.0 - fresnel) * (1.0 - foam);
+ var mirror = vec3f(0.0);
+ if (below && fresnel > 0.02 && haveColor && haveDepth && mc.u_mirror > 0.5) {
+ let met = waterReflect(P, reflect(-viewDir, N), waterDist);
+ if (met.w > 0.5) { mirror = met.xyz; } else { mirror = mix(mc.u_shallowColor.rgb, mc.u_deepColor.rgb, vec3f(0.5)) * 0.25; }
+ }
+ seen = seen + mirror * fresnel * (1.0 - foam);
+ var alpha = 0.85;
+ if (haveColor) { alpha = 1.0; }
+ if (mc.u_shoreOn > 0.5 && shoreDist < 0.0) { discard; }
+#ifdef ES_GBUFFER
+ var frag : FragOut;
+ frag.color = vec4f(lit + seen, alpha);
+ frag.surface = vec4f(packNormal(normalize(N)), roughness, 0.75);
+ return frag;
+#else
+ return vec4f(lit + seen, alpha);
+#endif
+}
+#pragma end
+`;
+
+export const WATER_SHADER_TEMPLATE = makeBuiltinShaderTemplate('water', 'Water',
+ 'Waves, refraction, reflection, depth absorption and foam for a horizontal body of water.', WATER);
diff --git a/sdk/src/water/waterSupport.ts b/sdk/src/water/waterSupport.ts
index 825a90811..3110584cb 100644
--- a/sdk/src/water/waterSupport.ts
+++ b/sdk/src/water/waterSupport.ts
@@ -6,11 +6,14 @@
* A call, not a file's side effect — see tilemap/tilemapSupport.ts.
*/
import { addEntryPlugin } from '../runtime/entryPlugins';
+import { registerBuiltinShaderTemplate } from '../render/builtinShaders';
+import { WATER_SHADER_TEMPLATE } from './waterShader';
import { WaterPlugin } from './waterPlugin';
import { FloatingPlugin } from './floating';
import { UnderwaterPlugin, registerUnderwaterEffect } from './underwater';
export function registerWaterSupport(): void {
+ registerBuiltinShaderTemplate(WATER_SHADER_TEMPLATE);
addEntryPlugin('water', () => new WaterPlugin());
addEntryPlugin('floating', () => new FloatingPlugin());
addEntryPlugin('underwater', () => new UnderwaterPlugin());
diff --git a/sdk/tests/builtin-shader-registry.test.ts b/sdk/tests/builtin-shader-registry.test.ts
new file mode 100644
index 000000000..d8951c7ad
--- /dev/null
+++ b/sdk/tests/builtin-shader-registry.test.ts
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { describe, expect, it } from 'vitest';
+import {
+ BUILTIN_SHADER_TEMPLATES, BUILTIN_SHADER_MODULE, builtinShaderTemplate, registerBuiltinShaderTemplate, makeBuiltinShaderTemplate,
+} from '../src/render/builtinShaders';
+import { registerWaterSupport } from '../src/water/waterSupport';
+import { registerTerrainSupport } from '../src/terrain/terrainSupport';
+import { registerFoliageSupport } from '../src/foliage/foliageSupport';
+import { registerCharacterShaders } from '../src/character/characterSupport';
+
+const MODULE_IDS = ['water', 'terrain', 'foliage', 'hair', 'skin', 'eye'];
+
+describe('the built-in shaders that ship with modules', () => {
+ it('are the module\'s to install, not the core list\'s', () => {
+ // The core list carries the sprite and model shaders; water, terrain, foliage and the character shaders arrive when their module is installed.
+ const coreIds = BUILTIN_SHADER_TEMPLATES.map((t) => t.id).filter((id) => !MODULE_IDS.includes(id));
+ expect(coreIds).toEqual(expect.arrayContaining(['sprite-unlit', 'sprite-lit', 'model']));
+ expect(Object.keys(BUILTIN_SHADER_MODULE).sort()).toEqual([...MODULE_IDS].sort());
+ });
+
+ it('appear once each module is installed, in a fixed order, and installing twice changes nothing', () => {
+ registerWaterSupport(); registerTerrainSupport(); registerFoliageSupport(); registerCharacterShaders();
+ const once = BUILTIN_SHADER_TEMPLATES.map((t) => t.id);
+ registerCharacterShaders(); registerWaterSupport();
+ expect(BUILTIN_SHADER_TEMPLATES.map((t) => t.id)).toEqual(once);
+ expect(once.filter((id) => MODULE_IDS.includes(id))).toEqual(MODULE_IDS);
+ // The module ones follow the core's.
+ expect(once.indexOf('model')).toBeLessThan(once.indexOf('water'));
+ for (const id of MODULE_IDS) {
+ const t = builtinShaderTemplate(id)!;
+ expect(t.source.startsWith('#pragma shader')).toBe(true);
+ expect(Object.keys(t.defaults).length).toBeGreaterThan(0);
+ }
+ });
+
+ it('can be registered by anyone, and the same id replaces', () => {
+ const mine = makeBuiltinShaderTemplate('water', 'Mine', 'x', builtinShaderTemplate('sprite-unlit')!.source);
+ const before = builtinShaderTemplate('water');
+ registerBuiltinShaderTemplate(mine);
+ expect(builtinShaderTemplate('water')!.label).toBe('Mine');
+ registerBuiltinShaderTemplate(before!);
+ expect(builtinShaderTemplate('water')!.label).toBe('Water');
+ });
+});
diff --git a/tools/lib/sdkProgram.mjs b/tools/lib/sdkProgram.mjs
index 812694086..b7bff0081 100644
--- a/tools/lib/sdkProgram.mjs
+++ b/tools/lib/sdkProgram.mjs
@@ -67,6 +67,7 @@ export const ENTRIES = {
'foliage': 'src/foliage/index.ts',
'water': 'src/water/index.ts',
'spline': 'src/spline/index.ts',
+ 'character-shaders': 'src/character/index.ts',
'logic': 'src/logic/index.ts',
'debug-channel': 'src/debug-channel/index.ts',
'ai': 'src/ai/index.ts',
diff --git a/tools/nativeScriptModules.js b/tools/nativeScriptModules.js
index f35a7691b..ac2c6af12 100644
--- a/tools/nativeScriptModules.js
+++ b/tools/nativeScriptModules.js
@@ -73,6 +73,7 @@ export const MODULES = {
'esengine/foliage': { disposition: 'native-subpath' },
'esengine/water': { disposition: 'native-subpath' },
'esengine/spline': { disposition: 'native-subpath' },
+ 'esengine/character-shaders': { disposition: 'native-subpath' },
'esengine/logic': { disposition: 'native-subpath' },
'esengine/ai': { disposition: 'native-subpath' },
'esengine/replication': { disposition: 'native-subpath' },
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index cd1cc3723..d47d6970a 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2567,6 +2567,10 @@ export const NOTED = {
{"note":"Water holds things up. A Floating component on an entity with a RigidBody3D floats it: a buoyant force against gravity (`buoyancy` is the water's density against the body's, so 2 floats it half under) and a drag against its velocity relative to the water, which brings it to rest in a lake and carries it with a river's current, applied at a few points of the body so waves tip it. It works from the CPU's own water surface, so it needs no renderer, and it needs the 3D physics module, which gains force and mass queries."},
"feat(water): a camera under the surface sees through the water, fogged and tinted by what it has crossed":
{"note":"A camera under water sees through it. Whenever the active camera is below a water body's surface an Underwater effect is added to the post-process volumes: the light of each pixel is taken out colour by colour (red first) by the distance it crossed, with the same absorption and density the surface's material draws with, and the water gives back its own colour, dimmer the deeper the camera is. The surface seen from below turns over and, past the critical angle, reflects whole (Snell's window)."},
+ "feat(sdk): the Water, Terrain, Foliage, Hair, Skin and Eye shaders ship with their modules, and a material that names one brings its module into an export":
+ {"note":"The Water, Terrain, Foliage, Hair, Skin and Eye built-in shaders now ship with their modules instead of the core: a package that installs none of them no longer carries about 80 KB of shader source (a blank web package is 80 KB smaller, a blank WeChat bundle 7% smaller). A material that names one of them is what brings its module into an export, with no component in a scene needed; the three character shaders are a new `esengine/character-shaders` module."},
+ "fix(render): opaque draws are told from blended ones by their stage, so a lit model no longer vanishes in a frame with glass":
+ {"note":"Opaque draws are told from blended ones by the stage they are sorted in, not by their blend mode, when a frame has both a refracting surface and blended draws; before, a lit material whose blend mode was not None (the Model material) was treated as blended and could vanish from such a frame."},
"feat(water): quality levels set the waves, ripples, mirror and caustics water spends, and the low level is a lite water with flat geometry":
{"note":"Water spends what the quality level allows. Each level's profile has `waterWaves` (waves summed, 2 / 3 / 4), `waterRippleCells` (the ripple grid's side, 32 / 64 / 128), `waterDisplacement` (off is the lite water: flat geometry with the waves in the shading only), `waterMirror` and `waterCaustics`. What `Waters.sample` answers follows what is drawn, and a minigame with no quality policy gets the Low level's water."},
"fix(editor): the move gizmo's plane handles sit closer to its centre":