phoria2 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -9
- package/dist/bloom.js +4 -4
- package/dist/bloom.js.map +1 -1
- package/dist/charts.js +527 -510
- package/dist/charts.js.map +1 -1
- package/dist/core.js +1760 -1170
- package/dist/core.js.map +1 -1
- package/dist/debug.js +297 -0
- package/dist/debug.js.map +1 -0
- package/dist/edit.js +60 -60
- package/dist/edit.js.map +1 -1
- package/dist/explain.js +32 -13
- package/dist/explain.js.map +1 -1
- package/dist/gltf.js +155 -155
- package/dist/gltf.js.map +1 -1
- package/dist/keyboard.js +188 -0
- package/dist/keyboard.js.map +1 -0
- package/dist/motion.js +35 -35
- package/dist/motion.js.map +1 -1
- package/dist/particles.js +187 -114
- package/dist/particles.js.map +1 -1
- package/dist/passes.js +116 -104
- package/dist/passes.js.map +1 -1
- package/dist/phoria2.js +1 -1
- package/dist/picking.js +95 -62
- package/dist/picking.js.map +1 -1
- package/dist/snapshot.js +1 -1
- package/dist/tooltip.js +1 -1
- package/dist/types/View.d.ts +27 -0
- package/dist/types/area.d.ts +18 -0
- package/dist/types/debug.d.ts +74 -0
- package/dist/types/explain.d.ts +4 -0
- package/dist/types/frame.d.ts +5 -0
- package/dist/types/geometry/Geometry.d.ts +16 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/material/Material.d.ts +67 -0
- package/dist/types/material/Texture.d.ts +18 -2
- package/dist/types/particles/Emitter.d.ts +39 -2
- package/dist/types/scene/drawables.d.ts +23 -2
- package/dist/types/watch.d.ts +8 -1
- package/dist/types/webgl/hooks.d.ts +48 -0
- package/dist/types/webgl/passes.d.ts +4 -0
- package/dist/types/webgl/programs.d.ts +15 -5
- package/dist/types/webgl/resources.d.ts +12 -2
- package/dist/types/webgl/shaders.d.ts +41 -5
- package/llms-full.txt +275 -1
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/schema/scene.schema.json +75 -0
- package/src/View.js +121 -7
- package/src/area.js +131 -0
- package/src/charts/marks.js +29 -2
- package/src/debug.js +283 -0
- package/src/explain.js +19 -0
- package/src/frame.js +7 -2
- package/src/geometry/Geometry.js +16 -1
- package/src/index.js +1 -0
- package/src/material/Material.js +130 -4
- package/src/material/Texture.js +55 -2
- package/src/particles/Emitter.js +121 -10
- package/src/scene/drawables.js +89 -8
- package/src/watch.js +26 -3
- package/src/webgl/WebGLRenderer.js +55 -7
- package/src/webgl/hooks.js +121 -0
- package/src/webgl/passes.js +17 -2
- package/src/webgl/picking.js +45 -3
- package/src/webgl/programs.js +20 -9
- package/src/webgl/resources.js +169 -15
- package/src/webgl/shaders.js +545 -38
package/dist/particles.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"particles.js","names":["OPTIONS","HINTS","mat4.invert","fail","Vec3","checkOptions","invert","describe","checkNumber","Color","Material","Geometry","Node","Points","SPAWN_OPTIONS","OPTIONS$1","HINTS$1","DEFAULT_MATERIAL","tmpMat","Emitter","options","max","given","material","geometry","#max","#velocities","#ages","#lifetimes","#palette","#setBehaviour","#setSpawn","dt","known","key","#alive","#palettes","colorStops","v","s","spawn","saved","#saveSpawn","color","#spawnPalette","#paletteFor","origin","i","count","#spawn","#upload","#due","p","ages","lives","g","keep","hook","particle","#particle","age","#remove","i3","k","#paint","#followedOrigin","from","#lastOrigin","born","n","rest","t","o","randomInBall","dv","a","b","head","min","last","c","stops","x","f","world","owner","lifetimeRange","checkFollow","id","#paletteIds","range","list","r","y","z","OPTIONS","HINTS","Body"],"sources":["../src/particles/Emitter.js","../src/particles/Body.js"],"sourcesContent":["// A particle system as one Points node: particles are born at the emitter's origin, move under\n// velocity, gravity and drag, fade through a list of colours and die. The simulation runs in the\n// emitter's own update handler, so it follows the scene clock (frame-rate independent) and its\n// arrays go to the GPU as one buffer refill per frame.\nimport * as mat4 from '../math/mat4.js'\nimport { Geometry } from '../geometry/Geometry.js'\nimport { Material } from '../material/Material.js'\nimport { Points } from '../scene/drawables.js'\nimport { Node } from '../scene/Node.js'\nimport { Color } from '../value/Color.js'\nimport { Vec3 } from '../value/Vec3.js'\nimport { checkNumber, describe, fail } from '../validate.js'\n\n/** @import { MaterialOptions } from '../material/Material.js' */\n/** @import { NodeOptions } from '../scene/Node.js' */\n/** @import { ColorInput, Vec3Input } from '../types.js' */\n\n/**\n * A live particle, as passed to `onParticle`. Change `position` or `velocity` and the changes stick.\n * @typedef {object} Particle\n * @property {Vec3} position in the emitter's space\n * @property {Vec3} velocity units per second\n * @property {number} age seconds since birth\n * @property {number} lifetime seconds it lives for\n */\n\n/**\n * How particles are born. Each can also be passed to burst() for that burst alone.\n * @typedef {object} SpawnOptions\n * @property {Vec3Input} [origin] where particles start, in the emitter's space (default [0, 0, 0])\n * @property {number} [originSpread] start anywhere within this radius of the origin (default 0)\n * @property {Vec3Input} [velocity] starting velocity, units per second (default [0, 1, 0])\n * @property {number | Vec3Input} [velocitySpread] random extra velocity: a number spreads evenly in every\n * direction (a sphere - explosions), [x, y, z] spreads up to ± that much per axis (default 0)\n * @property {number | [number, number]} [lifetime] seconds, or a [min, max] range (default 1)\n * @property {ColorInput | ColorInput[]} [color] a colour, or colours each particle fades through over its\n * life - end on black to fade out with additive blending (default white)\n */\n\n/**\n * @typedef {NodeOptions & SpawnOptions & {\n * max?: number,\n * rate?: number,\n * gravity?: Vec3Input,\n * drag?: number,\n * follow?: Node | null,\n * material?: Material | MaterialOptions,\n * frustumCulled?: boolean,\n * renderOrder?: number,\n * onParticle?: ((p: Particle) => boolean | void) | null,\n * }} EmitterOptions\n * max: most live particles at once (default 1000). rate: particles born per second (default 100).\n * gravity: acceleration, e.g. [0, -9.8, 0] (default none). drag: share of velocity lost per second\n * (default 0). follow: a node whose position becomes the origin each frame - a trail that stays\n * behind it. onParticle: called for every live particle each frame after it moves; return false to\n * remove it (a particle hitting something).\n */\n\nconst SPAWN_OPTIONS = ['origin', 'originSpread', 'velocity', 'velocitySpread', 'lifetime', 'color']\nconst OPTIONS = [...Points.OPTIONS, ...SPAWN_OPTIONS,\n 'max', 'rate', 'gravity', 'drag', 'follow', 'onParticle']\nconst HINTS = {\n count: \"use 'max' for the most particles alive at once, 'rate' for how many are born per second\",\n lifetimeRnd: \"give 'lifetime' a [min, max] range\",\n velocityRnd: \"use 'velocitySpread'\",\n positionRnd: \"use 'originSpread'\",\n}\n// the look most particle effects want: glowing dots that don't hide each other\n/** @type {MaterialOptions} */\nconst DEFAULT_MATERIAL = { size: 0.1, vertexColors: true, blending: 'additive', depthWrite: false }\n\nconst tmpMat = new Float32Array(16)\n\n/**\n * Particles drawn as points. Add it to a scene and it runs itself.\n *\n * const sparks = scene.add(new Emitter({ rate: 200, velocitySpread: 2, gravity: [0, -9.8, 0], color: ['#ffd27f', '#000'] }))\n * sparks.burst(500, { velocitySpread: 8 })\n */\nexport class Emitter extends Points {\n static type = 'emitter'\n static get OPTIONS() { return OPTIONS }\n static get HINTS() { return { ...Points.HINTS, ...HINTS } }\n\n /** Particles born per second; 0 stops new ones (existing particles live out their lives). */\n rate = 100\n /** Acceleration on every particle, units per second². */\n gravity = new Vec3()\n /** Share of velocity lost per second. */\n drag = 0\n /** Where particles start, in the emitter's space. */\n origin = new Vec3()\n originSpread = 0\n velocity = new Vec3(0, 1, 0)\n /** @type {number | Vec3} */ velocitySpread = 0\n /** @type {[number, number]} */ lifetime = [1, 1]\n /** A node whose position becomes the origin each frame. @type {Node | null} */\n follow = null\n /** @type {((p: Particle) => boolean | void) | null} */\n onParticle = null\n\n #max\n #alive = 0\n #due = 0\n /** @type {Float32Array} */ #velocities\n /** @type {Float32Array} */ #ages\n /** @type {Float32Array} */ #lifetimes\n // Colour stop lists, sRGB like geometry colours. Palette 0 is the emitter's own colour; bursts with\n // their own colours add more, and each particle remembers which one it fades through.\n /** @type {number[][][]} */ #palettes = [[[1, 1, 1]]]\n /** @type {Map<string, number>} */ #paletteIds = new Map()\n /** @type {Uint8Array} */ #palette\n #spawnPalette = 0\n /** Last frame's origin, so a moving source leaves an even trail rather than clumps. */\n #lastOrigin = /** @type {Vec3 | null} */ (null)\n #particle = /** @type {Particle} */ ({ position: new Vec3(), velocity: new Vec3(), age: 0, lifetime: 0 })\n\n /** @param {EmitterOptions} [options] */\n constructor(options = {}) {\n if (/** @type {any} */ (options)?.geometry !== undefined) fail('Emitter', \"an Emitter makes its own geometry - size it with 'max'\")\n const max = options?.max ?? 1000\n if (!Number.isInteger(max) || max < 1) fail('Emitter', `'max' must be a whole number of 1 or more, got ${describe(max)}`)\n const given = options?.material\n if (given !== undefined && !(given instanceof Material) && (!given || typeof given !== 'object' || Array.isArray(given))) {\n fail('Emitter', `'material' is a Material or material options such as { size: 0.2 }, got ${describe(given)}`)\n }\n const material = given instanceof Material ? given : { ...DEFAULT_MATERIAL, ...given }\n const geometry = new Geometry({ positions: new Float32Array(max * 3), colors: new Float32Array(max * 3) })\n geometry.drawCount = 0\n super({ ...options, geometry, material })\n this.#max = max\n this.#velocities = new Float32Array(max * 3)\n this.#ages = new Float32Array(max)\n this.#lifetimes = new Float32Array(max)\n this.#palette = new Uint8Array(max)\n // particles spread anywhere, and stale slots would make bounds meaningless\n if (options.frustumCulled === undefined) this.frustumCulled = false\n\n this.#setBehaviour(options)\n this.#setSpawn(options)\n this.onUpdate((_, dt) => this.step(dt))\n }\n\n /**\n * Change several settings at once - a preset, say. Vectors are copied into the emitter's own,\n * so anything bound to `emitter.gravity` and friends stays bound. Live particles carry on.\n * @param {SpawnOptions & { rate?: number, gravity?: Vec3Input, drag?: number, follow?: Node | null,\n * onParticle?: ((p: Particle) => boolean | void) | null }} options\n * @returns {this}\n */\n set(options) {\n const known = [...SPAWN_OPTIONS, 'rate', 'gravity', 'drag', 'follow', 'onParticle']\n for (const key of Object.keys(options ?? {})) {\n if (!known.includes(key)) fail('Emitter', `set() doesn't take '${key}' - it takes ${known.join(', ')}`)\n }\n this.#setBehaviour(options)\n this.#setSpawn(options)\n return this\n }\n\n /** Most live particles at once. */\n get max() { return this.#max }\n\n /** Particles alive now. */\n get count() { return this.#alive }\n\n /** The colours particles fade through - one or several. @param {ColorInput | ColorInput[]} v */\n set color(v) { this.#palettes[0] = colorStops(v) }\n /** @returns {[number, number, number][]} */\n get color() { return /** @type {[number, number, number][]} */ (this.#palettes[0].map((s) => [...s])) }\n\n /**\n * Birth `count` particles at once - explosions, splashes. Options override the emitter's own\n * spawn settings for this burst only.\n * @param {number} count\n * @param {SpawnOptions} [options]\n * @returns {this}\n */\n burst(count, options = {}) {\n for (const key of Object.keys(options)) {\n if (!SPAWN_OPTIONS.includes(key)) fail('Emitter', `burst() doesn't take '${key}' - it takes ${SPAWN_OPTIONS.join(', ')}`)\n }\n const { color, ...spawn } = options\n const saved = this.#saveSpawn()\n this.#setSpawn(spawn)\n if (color !== undefined) this.#spawnPalette = this.#paletteFor(colorStops(color))\n const origin = this.origin.clone()\n for (let i = 0; i < count; i++) this.#spawn(origin, origin, 0)\n this.#setSpawn(saved)\n this.#spawnPalette = 0\n this.#upload()\n return this\n }\n\n /** Remove every particle. @returns {this} */\n clear() {\n this.#alive = 0\n this.#due = 0\n this.#upload()\n return this\n }\n\n /**\n * Advance the simulation by dt seconds. The emitter's update handler calls this each frame; call\n * it yourself only for an emitter outside a running scene.\n * @param {number} dt\n */\n step(dt) {\n if (dt <= 0) return\n const p = this.geometry.positions\n const v = this.#velocities, ages = this.#ages, lives = this.#lifetimes\n const g = this.gravity, keep = Math.exp(-this.drag * dt)\n const hook = this.onParticle, particle = this.#particle\n\n for (let i = 0; i < this.#alive;) {\n const age = ages[i] += dt\n if (age >= lives[i]) {\n this.#remove(i)\n continue\n }\n const i3 = i * 3\n for (let k = 0; k < 3; k++) {\n v[i3 + k] = (v[i3 + k] + g[k] * dt) * keep\n p[i3 + k] += v[i3 + k] * dt\n }\n this.#paint(i, age / lives[i])\n if (hook) {\n particle.position.set(p[i3], p[i3 + 1], p[i3 + 2])\n particle.velocity.set(v[i3], v[i3 + 1], v[i3 + 2])\n particle.age = age\n particle.lifetime = lives[i]\n if (hook(particle) === false) {\n this.#remove(i)\n continue\n }\n p.set(particle.position, i3)\n v.set(particle.velocity, i3)\n }\n i++\n }\n\n // new particles, spread along the path the origin took this frame\n const origin = this.#followedOrigin()\n const from = this.#lastOrigin ?? origin\n this.#due += this.rate * dt\n const born = Math.floor(this.#due)\n this.#due -= born\n for (let n = 0; n < born; n++) this.#spawn(from, origin, (n + Math.random()) / born * dt)\n this.#lastOrigin = origin.clone()\n this.#upload()\n }\n\n /** @internal @returns {Record<string, unknown>} */\n cloneOptions() {\n // an emitter makes its own geometry, so neither form carries it\n const rest = super.cloneOptions()\n delete rest.geometry\n return { ...rest, ...this.#saveSpawn(), max: this.#max, rate: this.rate, gravity: this.gravity.clone(),\n drag: this.drag, follow: this.follow, onParticle: this.onParticle, color: this.color }\n }\n\n /** @internal @returns {Record<string, unknown>} */\n jsonOptions() {\n const rest = super.jsonOptions()\n delete rest.geometry\n return { ...rest, max: this.#max, rate: this.rate, gravity: this.gravity.toArray(), drag: this.drag,\n origin: this.origin.toArray(), originSpread: this.originSpread, velocity: this.velocity.toArray(),\n velocitySpread: typeof this.velocitySpread === 'number' ? this.velocitySpread : this.velocitySpread.toArray(),\n lifetime: [...this.lifetime], color: this.#palettes[0].map((s) => new Color().setSRGB(s[0], s[1], s[2]).toJSON()) }\n }\n\n // birth one particle somewhere between a and b, already `head` seconds into its life\n /** @param {Vec3} a @param {Vec3} b @param {number} head */\n #spawn(a, b, head) {\n if (this.#alive >= this.#max) return\n const i = this.#alive++, i3 = i * 3\n const p = this.geometry.positions, v = this.#velocities\n const t = Math.random()\n const o = randomInBall(this.originSpread)\n const s = this.velocitySpread\n const dv = typeof s === 'number' ? randomInBall(s) : [(Math.random() * 2 - 1) * s[0], (Math.random() * 2 - 1) * s[1], (Math.random() * 2 - 1) * s[2]]\n for (let k = 0; k < 3; k++) {\n v[i3 + k] = this.velocity[k] + dv[k]\n p[i3 + k] = a[k] + (b[k] - a[k]) * t + o[k] + v[i3 + k] * head\n }\n const [min, max] = this.lifetime\n this.#lifetimes[i] = min + Math.random() * (max - min)\n this.#ages[i] = head\n this.#palette[i] = this.#spawnPalette\n this.#paint(i, head / this.#lifetimes[i])\n }\n\n // move the last particle into slot i\n /** @param {number} i */\n #remove(i) {\n const last = --this.#alive\n if (i === last) return\n const p = this.geometry.positions, c = /** @type {Float32Array} */ (this.geometry.colors), v = this.#velocities\n p.copyWithin(i * 3, last * 3, last * 3 + 3)\n c.copyWithin(i * 3, last * 3, last * 3 + 3)\n v.copyWithin(i * 3, last * 3, last * 3 + 3)\n this.#ages[i] = this.#ages[last]\n this.#lifetimes[i] = this.#lifetimes[last]\n this.#palette[i] = this.#palette[last]\n }\n\n /** @param {number} i @param {number} t 0-1 through its life */\n #paint(i, t) {\n const c = /** @type {Float32Array} */ (this.geometry.colors), stops = this.#palettes[this.#palette[i]]\n if (stops.length === 1) {\n c.set(stops[0], i * 3)\n return\n }\n const x = Math.min(Math.max(t, 0), 1) * (stops.length - 1)\n const s = Math.min(Math.floor(x), stops.length - 2), f = x - s\n const a = stops[s], b = stops[s + 1]\n c[i * 3] = a[0] + (b[0] - a[0]) * f\n c[i * 3 + 1] = a[1] + (b[1] - a[1]) * f\n c[i * 3 + 2] = a[2] + (b[2] - a[2]) * f\n }\n\n #upload() {\n this.geometry.drawCount = this.#alive\n this.geometry.needsUpdate()\n }\n\n /** The followed node's position in the emitter's space, or the origin. @returns {Vec3} */\n #followedOrigin() {\n if (!this.follow) return this.origin\n const world = this.follow.worldPosition\n if (!mat4.invert(tmpMat, this.worldMatrix)) return this.origin\n return this.origin.copy(world).applyMatrix4(tmpMat)\n }\n\n /** @param {SpawnOptions} o */\n #setSpawn(o) {\n const owner = 'Emitter'\n // vectors are copied in place, so references to them stay live\n if (o.origin !== undefined) this.origin.copy(o.origin, owner)\n if (o.originSpread !== undefined) this.originSpread = Math.max(0, checkNumber(owner, 'originSpread', o.originSpread))\n if (o.velocity !== undefined) this.velocity.copy(o.velocity, owner)\n if (o.velocitySpread !== undefined) {\n const s = o.velocitySpread\n if (typeof s === 'number') this.velocitySpread = checkNumber(owner, 'velocitySpread', s)\n else if (this.velocitySpread instanceof Vec3) this.velocitySpread.copy(s, owner)\n else this.velocitySpread = Vec3.fromInput(s, owner)\n }\n if (o.lifetime !== undefined) this.lifetime = lifetimeRange(o.lifetime)\n if (o.color !== undefined) this.#palettes[0] = colorStops(o.color)\n }\n\n /** @param {{ rate?: number, gravity?: Vec3Input, drag?: number, follow?: Node | null, onParticle?: ((p: Particle) => boolean | void) | null }} o */\n #setBehaviour(o) {\n const owner = 'Emitter'\n if (o.rate !== undefined) this.rate = Math.max(0, checkNumber(owner, 'rate', o.rate))\n if (o.gravity !== undefined) this.gravity.copy(o.gravity, owner)\n if (o.drag !== undefined) this.drag = Math.max(0, checkNumber(owner, 'drag', o.drag))\n if (o.follow !== undefined) this.follow = checkFollow(o.follow)\n if (o.onParticle !== undefined) this.onParticle = o.onParticle\n }\n\n /** The spawn settings other than colour, as options. @returns {SpawnOptions} */\n #saveSpawn() {\n return { origin: this.origin.clone(), originSpread: this.originSpread, velocity: this.velocity.clone(),\n velocitySpread: typeof this.velocitySpread === 'number' ? this.velocitySpread : this.velocitySpread.clone(),\n lifetime: /** @type {[number, number]} */ ([...this.lifetime]) }\n }\n\n // The palette id for a burst's colours, reused when the same colours come again. Ids are a byte\n // per particle, so past 255 distinct burst palettes the oldest is overwritten.\n /** @param {number[][]} stops @returns {number} */\n #paletteFor(stops) {\n const key = stops.flat().join()\n let id = this.#paletteIds.get(key)\n if (id === undefined) {\n id = this.#palettes.length < 256 ? this.#palettes.length : 1 + (this.#paletteIds.size % 255)\n for (const [k, v] of this.#paletteIds) if (v === id) this.#paletteIds.delete(k)\n this.#paletteIds.set(key, id)\n }\n this.#palettes[id] = stops\n return id\n }\n}\n\n/** @param {unknown} v @returns {Node | null} */\nfunction checkFollow(v) {\n if (v === null || v instanceof Node) return /** @type {Node | null} */ (v)\n return fail('Emitter', `'follow' must be a node, got ${describe(v)}`)\n}\n\n/** @param {number | [number, number]} v @returns {[number, number]} */\nfunction lifetimeRange(v) {\n const range = typeof v === 'number' ? [v, v] : v\n if (!Array.isArray(range) || range.length !== 2 || !range.every((n) => typeof n === 'number' && n > 0)) {\n fail('Emitter', `'lifetime' must be seconds above 0, or a [min, max] range, got ${describe(v)}`)\n }\n return [Math.min(range[0], range[1]), Math.max(range[0], range[1])]\n}\n\n/** @param {ColorInput | ColorInput[]} v @returns {number[][]} */\nfunction colorStops(v) {\n // a single [r, g, b] array is one colour, not three\n const list = Array.isArray(v) && !(v.length === 3 && v.every((n) => typeof n === 'number')) ? v : [v]\n if (!list.length) fail('Emitter', \"'color' needs at least one colour\")\n return list.map((c) => Color.fromInput(/** @type {ColorInput} */ (c), 'Emitter').toSRGB())\n}\n\n/** A random point within a ball of radius r, evenly spread. @param {number} r @returns {number[]} */\nfunction randomInBall(r) {\n if (!r) return [0, 0, 0]\n let x, y, z\n do {\n x = Math.random() * 2 - 1\n y = Math.random() * 2 - 1\n z = Math.random() * 2 - 1\n } while (x * x + y * y + z * z > 1)\n return [x * r, y * r, z * r]\n}\n","// Simple physics as a behaviour rather than a base class: any node moves with velocity, gravity\n// and drag once its body's update is attached - node.onUpdate(body.update).\nimport { Vec3 } from '../value/Vec3.js'\nimport { checkNumber, checkOptions } from '../validate.js'\n\n/** @import { Node } from '../scene/Node.js' */\n/** @import { Vec3Input } from '../types.js' */\n\n/**\n * @typedef {object} BodyOptions\n * @property {Vec3Input} [velocity] units per second (default [0, 0, 0])\n * @property {Vec3Input} [gravity] acceleration, e.g. [0, -9.8, 0] (default none)\n * @property {number} [drag] share of velocity lost per second (default 0)\n */\n\n/**\n * Velocity, gravity and drag for a node.\n *\n * const body = new Body({ velocity: [0, 20, 0], gravity: [0, -9.8, 0] })\n * rocket.onUpdate(body.update)\n */\nconst OPTIONS = ['velocity', 'gravity', 'drag']\nconst HINTS = {\n impulse: 'call body.impulse([x, y, z]) after creating it',\n mass: 'a Body has no mass - impulse() changes velocity directly',\n}\n\nexport class Body {\n /** The options it takes, and hints for names people reach for instead. */\n static OPTIONS = OPTIONS\n static HINTS = HINTS\n velocity = new Vec3()\n gravity = new Vec3()\n drag = 0\n\n /** @param {BodyOptions} [options] */\n constructor(options = {}) {\n checkOptions('Body', options, OPTIONS, HINTS)\n if (options.velocity !== undefined) this.velocity.copy(options.velocity, 'Body')\n if (options.gravity !== undefined) this.gravity.copy(options.gravity, 'Body')\n if (options.drag !== undefined) this.drag = Math.max(0, checkNumber('Body', 'drag', options.drag))\n }\n\n /** Add to the velocity at once - a kick. @param {Vec3Input} v @returns {this} */\n impulse(v) {\n this.velocity.add(Vec3.fromInput(v, 'Body'))\n return this\n }\n\n /**\n * Move a node one step. Bound, so it can be handed straight to node.onUpdate().\n * @param {Node} node\n * @param {number} dt seconds\n */\n update = (node, dt) => {\n const v = this.velocity, keep = Math.exp(-this.drag * dt)\n for (let k = 0; k < 3; k++) {\n v[k] = (v[k] + this.gravity[k] * dt) * keep\n node.position[k] += v[k] * dt\n }\n }\n}\n"],"mappings":";;AA0DA,IAAMc,IAAgB;CAAC;CAAU;CAAgB;CAAY;CAAkB;CAAY;AAAO,GAC5Fd,IAAU;CAAC,GAAGa,EAAO;CAAS,GAAGC;CACrC;CAAO;CAAQ;CAAW;CAAQ;CAAU;AAAY,GACpDb,IAAQ;CACZ,OAAO;CACP,aAAa;CACb,aAAa;CACb,aAAa;AACf,GAGMgB,IAAmB;CAAE,MAAM;CAAK,cAAc;CAAM,UAAU;CAAY,YAAY;AAAM,GAE5FC,oBAAS,IAAI,aAAa,EAAE,GAQrBC,IAAb,cAA6BN,EAAO;CAClC,OAAO,OAAO;CACd,WAAW,UAAU;EAAE,OAAOb;CAAQ;CACtC,WAAW,QAAQ;EAAE,OAAO;GAAE,GAAGa,EAAO;GAAO,GAAGZ;EAAM;CAAE;CAG1D,OAAO;CAEP,UAAU,IAAIG,EAAK;CAEnB,OAAO;CA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SAASO,MAAMzB,EAAEyB,KAAK/B;CAE/B;AACF"}
|
|
1
|
+
{"version":3,"file":"particles.js","names":["OPTIONS","HINTS","mat4.invert","checkNumber","invert","fail","checkOptions","describe","Vec3","Color","Points","Node","Geometry","Material","SPAWN_OPTIONS","BEHAVIOUR_OPTIONS","OPTIONS$1","HINTS$1","DEFAULT_MATERIAL","tmpMat","Emitter","options","max","given","material","geometry","#max","#velocities","#ages","#lifetimes","#palette","#setBehaviour","#setSpawn","dt","known","key","#alive","#palettes","colorStops","v","s","#setLooks","#size","#sizeSpread","#opacity","#spin","spawn","saved","#saveSpawn","color","#spawnPalette","#paletteFor","origin","i","count","#spawn","#upload","#due","p","ages","lives","g","keep","hook","particle","#particle","angles","spins","#spins","age","#remove","i3","k","#paint","#followedOrigin","from","#lastOrigin","born","n","rest","#looks","t","o","randomInBall","dv","a","b","head","min","#sizeFactors","last","c","sizes","along","opacities","stops","x","f","world","owner","lifetimeRange","checkFollow","lookStops","spinRange","plain","unstop","id","#paletteIds","range","list","name","most","r","y","z","OPTIONS","HINTS","Body"],"sources":["../src/particles/Emitter.js","../src/particles/Body.js"],"sourcesContent":["// A particle system as one Points node: particles are born at the emitter's origin, move under\n// velocity, gravity and drag, fade through a list of colours and die. The simulation runs in the\n// emitter's own update handler, so it follows the scene clock (frame-rate independent) and its\n// arrays go to the GPU as one buffer refill per frame. Particles that grow, fade or turn give the\n// geometry per-point sizes, opacities or angles, and are drawn as squares.\nimport * as mat4 from '../math/mat4.js'\nimport { Geometry } from '../geometry/Geometry.js'\nimport { Material } from '../material/Material.js'\nimport { Points } from '../scene/drawables.js'\nimport { Node } from '../scene/Node.js'\nimport { Color } from '../value/Color.js'\nimport { Vec3 } from '../value/Vec3.js'\nimport { checkNumber, describe, fail } from '../validate.js'\n\n/** @import { MaterialOptions } from '../material/Material.js' */\n/** @import { NodeOptions } from '../scene/Node.js' */\n/** @import { ColorInput, Vec3Input } from '../types.js' */\n\n/**\n * A live particle, as passed to `onParticle`. Change `position` or `velocity` and the changes stick.\n * @typedef {object} Particle\n * @property {Vec3} position in the emitter's space\n * @property {Vec3} velocity units per second\n * @property {number} age seconds since birth\n * @property {number} lifetime seconds it lives for\n */\n\n/**\n * How particles are born. Each can also be passed to burst() for that burst alone.\n * @typedef {object} SpawnOptions\n * @property {Vec3Input} [origin] where particles start, in the emitter's space (default [0, 0, 0])\n * @property {number} [originSpread] start anywhere within this radius of the origin (default 0)\n * @property {Vec3Input} [velocity] starting velocity, units per second (default [0, 1, 0])\n * @property {number | Vec3Input} [velocitySpread] random extra velocity: a number spreads evenly in every\n * direction (a sphere - explosions), [x, y, z] spreads up to ± that much per axis (default 0)\n * @property {number | [number, number]} [lifetime] seconds, or a [min, max] range (default 1)\n * @property {ColorInput | ColorInput[]} [color] a colour, or colours each particle fades through over its\n * life - end on black to fade out with additive blending (default white)\n */\n\n/**\n * @typedef {NodeOptions & SpawnOptions & {\n * max?: number,\n * rate?: number,\n * gravity?: Vec3Input,\n * drag?: number,\n * follow?: Node | null,\n * material?: Material | MaterialOptions,\n * frustumCulled?: boolean,\n * renderOrder?: number,\n * onParticle?: ((p: Particle) => boolean | void) | null,\n * sort?: boolean,\n * } & LookOptions} EmitterOptions\n * max: most live particles at once (default 1000). rate: particles born per second (default 100).\n * gravity: acceleration, e.g. [0, -9.8, 0] (default none). drag: share of velocity lost per second\n * (default 0). follow: a node whose position becomes the origin each frame - a trail that stays\n * behind it. onParticle: called for every live particle each frame after it moves; return false to\n * remove it (a particle hitting something). sort: draw furthest first, for soft particles with\n * blending 'normal' (smoke) that would otherwise layer wrongly.\n */\n\n/**\n * How particles look over their lives, beyond colour.\n * @typedef {object} LookOptions\n * @property {number | number[]} [size] a multiple of the material's size, or several each particle goes\n * through over its life - [0.5, 3] for smoke that billows out (default 1)\n * @property {number} [sizeSpread] each particle's size varies by up to this share either way, 0-1 (default 0)\n * @property {number | number[]} [opacity] 0-1, or several each particle goes through - [0, 1, 0] fades in\n * and out (default 1)\n * @property {number | [number, number]} [spin] radians per second: a number turns each particle up to that\n * fast either way, [min, max] a range. Spinning particles start at a random angle (default 0)\n */\n\nconst SPAWN_OPTIONS = ['origin', 'originSpread', 'velocity', 'velocitySpread', 'lifetime', 'color']\nconst LOOK_OPTIONS = ['size', 'sizeSpread', 'opacity', 'spin']\nconst BEHAVIOUR_OPTIONS = ['rate', 'gravity', 'drag', 'follow', 'onParticle', ...LOOK_OPTIONS]\nconst OPTIONS = [...Points.OPTIONS, ...SPAWN_OPTIONS, 'max', ...BEHAVIOUR_OPTIONS]\nconst HINTS = {\n count: \"use 'max' for the most particles alive at once, 'rate' for how many are born per second\",\n lifetimeRnd: \"give 'lifetime' a [min, max] range\",\n velocityRnd: \"use 'velocitySpread'\",\n positionRnd: \"use 'originSpread'\",\n}\n// the look most particle effects want: glowing dots that don't hide each other\n/** @type {MaterialOptions} */\nconst DEFAULT_MATERIAL = { size: 0.1, vertexColors: true, blending: 'additive', depthWrite: false }\n\nconst tmpMat = new Float32Array(16)\n\n/**\n * Particles drawn as points. Add it to a scene and it runs itself.\n *\n * const sparks = scene.add(new Emitter({ rate: 200, velocitySpread: 2, gravity: [0, -9.8, 0], color: ['#ffd27f', '#000'] }))\n * sparks.burst(500, { velocitySpread: 8 })\n */\nexport class Emitter extends Points {\n static type = 'emitter'\n static get OPTIONS() { return OPTIONS }\n static get HINTS() { return { ...Points.HINTS, ...HINTS } }\n\n /** Particles born per second; 0 stops new ones (existing particles live out their lives). */\n rate = 100\n /** Acceleration on every particle, units per second². */\n gravity = new Vec3()\n /** Share of velocity lost per second. */\n drag = 0\n /** Where particles start, in the emitter's space. */\n origin = new Vec3()\n originSpread = 0\n velocity = new Vec3(0, 1, 0)\n /** @type {number | Vec3} */ velocitySpread = 0\n /** @type {[number, number]} */ lifetime = [1, 1]\n /** A node whose position becomes the origin each frame. @type {Node | null} */\n follow = null\n /** @type {((p: Particle) => boolean | void) | null} */\n onParticle = null\n\n #max\n #alive = 0\n #due = 0\n /** @type {Float32Array} */ #velocities\n /** @type {Float32Array} */ #ages\n /** @type {Float32Array} */ #lifetimes\n // Colour stop lists, sRGB like geometry colours. Palette 0 is the emitter's own colour; bursts with\n // their own colours add more, and each particle remembers which one it fades through.\n /** @type {number[][][]} */ #palettes = [[[1, 1, 1]]]\n /** @type {Map<string, number>} */ #paletteIds = new Map()\n /** @type {Uint8Array} */ #palette\n #spawnPalette = 0\n // the looks: stops over each particle's life, and each one's own size factor and turn rate\n /** @type {number[]} */ #size = [1]\n #sizeSpread = 0\n /** @type {number[]} */ #opacity = [1]\n /** @type {[number, number]} */ #spin = [0, 0]\n /** @type {Float32Array | null} */ #sizeFactors = null\n /** @type {Float32Array | null} */ #spins = null\n /** Last frame's origin, so a moving source leaves an even trail rather than clumps. */\n #lastOrigin = /** @type {Vec3 | null} */ (null)\n #particle = /** @type {Particle} */ ({ position: new Vec3(), velocity: new Vec3(), age: 0, lifetime: 0 })\n\n /** @param {EmitterOptions} [options] */\n constructor(options = {}) {\n if (/** @type {any} */ (options)?.geometry !== undefined) fail('Emitter', \"an Emitter makes its own geometry - size it with 'max'\")\n const max = options?.max ?? 1000\n if (!Number.isInteger(max) || max < 1) fail('Emitter', `'max' must be a whole number of 1 or more, got ${describe(max)}`)\n const given = options?.material\n if (given !== undefined && !(given instanceof Material) && (!given || typeof given !== 'object' || Array.isArray(given))) {\n fail('Emitter', `'material' is a Material or material options such as { size: 0.2 }, got ${describe(given)}`)\n }\n const material = given instanceof Material ? given : { ...DEFAULT_MATERIAL, ...given }\n const geometry = new Geometry({ positions: new Float32Array(max * 3), colors: new Float32Array(max * 3) })\n geometry.drawCount = 0\n super({ ...options, geometry, material })\n this.#max = max\n this.#velocities = new Float32Array(max * 3)\n this.#ages = new Float32Array(max)\n this.#lifetimes = new Float32Array(max)\n this.#palette = new Uint8Array(max)\n // particles spread anywhere, and stale slots would make bounds meaningless\n if (options.frustumCulled === undefined) this.frustumCulled = false\n\n this.#setBehaviour(options)\n this.#setSpawn(options)\n this.onUpdate((_, dt) => this.step(dt))\n }\n\n /**\n * Change several settings at once - a preset, say. Vectors are copied into the emitter's own,\n * so anything bound to `emitter.gravity` and friends stays bound. Live particles carry on.\n * @param {SpawnOptions & LookOptions & { rate?: number, gravity?: Vec3Input, drag?: number, follow?: Node | null,\n * onParticle?: ((p: Particle) => boolean | void) | null }} options\n * @returns {this}\n */\n set(options) {\n const known = [...SPAWN_OPTIONS, ...BEHAVIOUR_OPTIONS]\n for (const key of Object.keys(options ?? {})) {\n if (!known.includes(key)) fail('Emitter', `set() doesn't take '${key}' - it takes ${known.join(', ')}`)\n }\n this.#setBehaviour(options)\n this.#setSpawn(options)\n return this\n }\n\n /** Most live particles at once. */\n get max() { return this.#max }\n\n /** Particles alive now. */\n get count() { return this.#alive }\n\n /** The colours particles fade through - one or several. @param {ColorInput | ColorInput[]} v */\n set color(v) { this.#palettes[0] = colorStops(v) }\n /** @returns {[number, number, number][]} */\n get color() { return /** @type {[number, number, number][]} */ (this.#palettes[0].map((s) => [...s])) }\n\n /** Each particle's size over its life, as multiples of the material's. @param {number | number[]} v */\n set size(v) { this.#setLooks({ size: v }) }\n /** @returns {number[]} */\n get size() { return [...this.#size] }\n /** How much each particle's size varies, 0-1 either way. @param {number} v */\n set sizeSpread(v) { this.#setLooks({ sizeSpread: v }) }\n get sizeSpread() { return this.#sizeSpread }\n /** Each particle's opacity over its life. @param {number | number[]} v */\n set opacity(v) { this.#setLooks({ opacity: v }) }\n /** @returns {number[]} */\n get opacity() { return [...this.#opacity] }\n /** How fast particles turn, radians per second, as a [min, max] range. @param {number | [number, number]} v */\n set spin(v) { this.#setLooks({ spin: v }) }\n /** @returns {[number, number]} */\n get spin() { return [...this.#spin] }\n\n /**\n * Birth `count` particles at once - explosions, splashes. Options override the emitter's own\n * spawn settings for this burst only.\n * @param {number} count\n * @param {SpawnOptions} [options]\n * @returns {this}\n */\n burst(count, options = {}) {\n for (const key of Object.keys(options)) {\n if (!SPAWN_OPTIONS.includes(key)) fail('Emitter', `burst() doesn't take '${key}' - it takes ${SPAWN_OPTIONS.join(', ')}`)\n }\n const { color, ...spawn } = options\n const saved = this.#saveSpawn()\n this.#setSpawn(spawn)\n if (color !== undefined) this.#spawnPalette = this.#paletteFor(colorStops(color))\n const origin = this.origin.clone()\n for (let i = 0; i < count; i++) this.#spawn(origin, origin, 0)\n this.#setSpawn(saved)\n this.#spawnPalette = 0\n this.#upload()\n return this\n }\n\n /** Remove every particle. @returns {this} */\n clear() {\n this.#alive = 0\n this.#due = 0\n this.#upload()\n return this\n }\n\n /**\n * Advance the simulation by dt seconds. The emitter's update handler calls this each frame; call\n * it yourself only for an emitter outside a running scene.\n * @param {number} dt\n */\n step(dt) {\n if (dt <= 0) return\n const p = this.geometry.positions\n const v = this.#velocities, ages = this.#ages, lives = this.#lifetimes\n const g = this.gravity, keep = Math.exp(-this.drag * dt)\n const hook = this.onParticle, particle = this.#particle\n const angles = this.geometry.angles, spins = this.#spins\n\n for (let i = 0; i < this.#alive;) {\n const age = ages[i] += dt\n if (age >= lives[i]) {\n this.#remove(i)\n continue\n }\n const i3 = i * 3\n for (let k = 0; k < 3; k++) {\n v[i3 + k] = (v[i3 + k] + g[k] * dt) * keep\n p[i3 + k] += v[i3 + k] * dt\n }\n if (angles && spins) angles[i] += spins[i] * dt\n this.#paint(i, age / lives[i])\n if (hook) {\n particle.position.set(p[i3], p[i3 + 1], p[i3 + 2])\n particle.velocity.set(v[i3], v[i3 + 1], v[i3 + 2])\n particle.age = age\n particle.lifetime = lives[i]\n if (hook(particle) === false) {\n this.#remove(i)\n continue\n }\n p.set(particle.position, i3)\n v.set(particle.velocity, i3)\n }\n i++\n }\n\n // new particles, spread along the path the origin took this frame\n const origin = this.#followedOrigin()\n const from = this.#lastOrigin ?? origin\n this.#due += this.rate * dt\n const born = Math.floor(this.#due)\n this.#due -= born\n for (let n = 0; n < born; n++) this.#spawn(from, origin, (n + Math.random()) / born * dt)\n this.#lastOrigin = origin.clone()\n this.#upload()\n }\n\n /** @internal @returns {Record<string, unknown>} */\n cloneOptions() {\n // an emitter makes its own geometry, so neither form carries it\n const rest = super.cloneOptions()\n delete rest.geometry\n return { ...rest, ...this.#saveSpawn(), max: this.#max, rate: this.rate, gravity: this.gravity.clone(),\n drag: this.drag, follow: this.follow, onParticle: this.onParticle, color: this.color, ...this.#looks() }\n }\n\n /** @internal @returns {Record<string, unknown>} */\n jsonOptions() {\n const rest = super.jsonOptions()\n delete rest.geometry\n return { ...rest, max: this.#max, rate: this.rate, gravity: this.gravity.toArray(), drag: this.drag,\n origin: this.origin.toArray(), originSpread: this.originSpread, velocity: this.velocity.toArray(),\n velocitySpread: typeof this.velocitySpread === 'number' ? this.velocitySpread : this.velocitySpread.toArray(),\n lifetime: [...this.lifetime], color: this.#palettes[0].map((s) => new Color().setSRGB(s[0], s[1], s[2]).toJSON()),\n ...this.#looks() }\n }\n\n // birth one particle somewhere between a and b, already `head` seconds into its life\n /** @param {Vec3} a @param {Vec3} b @param {number} head */\n #spawn(a, b, head) {\n if (this.#alive >= this.#max) return\n const i = this.#alive++, i3 = i * 3\n const p = this.geometry.positions, v = this.#velocities\n const t = Math.random()\n const o = randomInBall(this.originSpread)\n const s = this.velocitySpread\n const dv = typeof s === 'number' ? randomInBall(s) : [(Math.random() * 2 - 1) * s[0], (Math.random() * 2 - 1) * s[1], (Math.random() * 2 - 1) * s[2]]\n for (let k = 0; k < 3; k++) {\n v[i3 + k] = this.velocity[k] + dv[k]\n p[i3 + k] = a[k] + (b[k] - a[k]) * t + o[k] + v[i3 + k] * head\n }\n const [min, max] = this.lifetime\n this.#lifetimes[i] = min + Math.random() * (max - min)\n this.#ages[i] = head\n this.#palette[i] = this.#spawnPalette\n if (this.#sizeFactors) this.#sizeFactors[i] = 1 + (Math.random() * 2 - 1) * this.#sizeSpread\n const angles = this.geometry.angles, spins = this.#spins\n if (angles && spins) {\n spins[i] = this.#spin[0] + Math.random() * (this.#spin[1] - this.#spin[0])\n angles[i] = Math.random() * Math.PI * 2 + spins[i] * head\n }\n this.#paint(i, head / this.#lifetimes[i])\n }\n\n // move the last particle into slot i\n /** @param {number} i */\n #remove(i) {\n const last = --this.#alive\n if (i === last) return\n const p = this.geometry.positions, c = /** @type {Float32Array} */ (this.geometry.colors), v = this.#velocities\n p.copyWithin(i * 3, last * 3, last * 3 + 3)\n c.copyWithin(i * 3, last * 3, last * 3 + 3)\n v.copyWithin(i * 3, last * 3, last * 3 + 3)\n this.#ages[i] = this.#ages[last]\n this.#lifetimes[i] = this.#lifetimes[last]\n this.#palette[i] = this.#palette[last]\n for (const a of [this.geometry.sizes, this.geometry.opacities, this.geometry.angles, this.#sizeFactors, this.#spins]) {\n if (a) a[i] = a[last]\n }\n }\n\n /** @param {number} i @param {number} t 0-1 through its life */\n #paint(i, t) {\n const { sizes, opacities } = this.geometry\n if (sizes) sizes[i] = along(this.#size, t) * (this.#sizeFactors ? this.#sizeFactors[i] : 1)\n if (opacities) opacities[i] = along(this.#opacity, t)\n const c = /** @type {Float32Array} */ (this.geometry.colors), stops = this.#palettes[this.#palette[i]]\n if (stops.length === 1) {\n c.set(stops[0], i * 3)\n return\n }\n const x = Math.min(Math.max(t, 0), 1) * (stops.length - 1)\n const s = Math.min(Math.floor(x), stops.length - 2), f = x - s\n const a = stops[s], b = stops[s + 1]\n c[i * 3] = a[0] + (b[0] - a[0]) * f\n c[i * 3 + 1] = a[1] + (b[1] - a[1]) * f\n c[i * 3 + 2] = a[2] + (b[2] - a[2]) * f\n }\n\n #upload() {\n this.geometry.drawCount = this.#alive\n this.geometry.needsUpdate()\n }\n\n /** The followed node's position in the emitter's space, or the origin. @returns {Vec3} */\n #followedOrigin() {\n if (!this.follow) return this.origin\n const world = this.follow.worldPosition\n if (!mat4.invert(tmpMat, this.worldMatrix)) return this.origin\n return this.origin.copy(world).applyMatrix4(tmpMat)\n }\n\n /** @param {SpawnOptions} o */\n #setSpawn(o) {\n const owner = 'Emitter'\n // vectors are copied in place, so references to them stay live\n if (o.origin !== undefined) this.origin.copy(o.origin, owner)\n if (o.originSpread !== undefined) this.originSpread = Math.max(0, checkNumber(owner, 'originSpread', o.originSpread))\n if (o.velocity !== undefined) this.velocity.copy(o.velocity, owner)\n if (o.velocitySpread !== undefined) {\n const s = o.velocitySpread\n if (typeof s === 'number') this.velocitySpread = checkNumber(owner, 'velocitySpread', s)\n else if (this.velocitySpread instanceof Vec3) this.velocitySpread.copy(s, owner)\n else this.velocitySpread = Vec3.fromInput(s, owner)\n }\n if (o.lifetime !== undefined) this.lifetime = lifetimeRange(o.lifetime)\n if (o.color !== undefined) this.#palettes[0] = colorStops(o.color)\n }\n\n /** @param {LookOptions & { rate?: number, gravity?: Vec3Input, drag?: number, follow?: Node | null, onParticle?: ((p: Particle) => boolean | void) | null }} o */\n #setBehaviour(o) {\n this.#setLooks(o)\n const owner = 'Emitter'\n if (o.rate !== undefined) this.rate = Math.max(0, checkNumber(owner, 'rate', o.rate))\n if (o.gravity !== undefined) this.gravity.copy(o.gravity, owner)\n if (o.drag !== undefined) this.drag = Math.max(0, checkNumber(owner, 'drag', o.drag))\n if (o.follow !== undefined) this.follow = checkFollow(o.follow)\n if (o.onParticle !== undefined) this.onParticle = o.onParticle\n }\n\n // Sizes, opacities and angles go on the geometry when first wanted (the points are then drawn as\n // squares) and stay; live particles get the plain values until they are next painted.\n /** @param {LookOptions} o */\n #setLooks(o) {\n const owner = 'Emitter', g = this.geometry, max = this.#max\n if (o.size !== undefined) this.#size = lookStops('size', o.size, Infinity)\n if (o.sizeSpread !== undefined) this.#sizeSpread = Math.min(Math.max(checkNumber(owner, 'sizeSpread', o.sizeSpread), 0), 1)\n if (o.opacity !== undefined) this.#opacity = lookStops('opacity', o.opacity, 1)\n if (o.spin !== undefined) this.#spin = spinRange(o.spin)\n const plain = (/** @type {number[]} */ stops) => stops.length === 1 && stops[0] === 1\n if (!g.sizes && (!plain(this.#size) || this.#sizeSpread)) g.sizes = new Float32Array(max).fill(1)\n if (!this.#sizeFactors && this.#sizeSpread) this.#sizeFactors = new Float32Array(max).fill(1)\n if (!g.opacities && !plain(this.#opacity)) g.opacities = new Float32Array(max).fill(1)\n if (!g.angles && (this.#spin[0] || this.#spin[1])) {\n g.angles = new Float32Array(max)\n this.#spins = new Float32Array(max)\n }\n }\n\n /** The looks, as options. @returns {LookOptions} */\n #looks() {\n return { size: unstop(this.#size), sizeSpread: this.#sizeSpread, opacity: unstop(this.#opacity),\n spin: /** @type {[number, number]} */ ([...this.#spin]) }\n }\n\n /** The spawn settings other than colour, as options. @returns {SpawnOptions} */\n #saveSpawn() {\n return { origin: this.origin.clone(), originSpread: this.originSpread, velocity: this.velocity.clone(),\n velocitySpread: typeof this.velocitySpread === 'number' ? this.velocitySpread : this.velocitySpread.clone(),\n lifetime: /** @type {[number, number]} */ ([...this.lifetime]) }\n }\n\n // The palette id for a burst's colours, reused when the same colours come again. Ids are a byte\n // per particle, so past 255 distinct burst palettes the oldest is overwritten.\n /** @param {number[][]} stops @returns {number} */\n #paletteFor(stops) {\n const key = stops.flat().join()\n let id = this.#paletteIds.get(key)\n if (id === undefined) {\n id = this.#palettes.length < 256 ? this.#palettes.length : 1 + (this.#paletteIds.size % 255)\n for (const [k, v] of this.#paletteIds) if (v === id) this.#paletteIds.delete(k)\n this.#paletteIds.set(key, id)\n }\n this.#palettes[id] = stops\n return id\n }\n}\n\n/** @param {unknown} v @returns {Node | null} */\nfunction checkFollow(v) {\n if (v === null || v instanceof Node) return /** @type {Node | null} */ (v)\n return fail('Emitter', `'follow' must be a node, got ${describe(v)}`)\n}\n\n/** @param {number | [number, number]} v @returns {[number, number]} */\nfunction lifetimeRange(v) {\n const range = typeof v === 'number' ? [v, v] : v\n if (!Array.isArray(range) || range.length !== 2 || !range.every((n) => typeof n === 'number' && n > 0)) {\n fail('Emitter', `'lifetime' must be seconds above 0, or a [min, max] range, got ${describe(v)}`)\n }\n return [Math.min(range[0], range[1]), Math.max(range[0], range[1])]\n}\n\n/** @param {string} name @param {number | number[]} v @param {number} most @returns {number[]} */\nfunction lookStops(name, v, most) {\n const list = Array.isArray(v) ? v : [v]\n if (!list.length || !list.every((n) => typeof n === 'number' && Number.isFinite(n) && n >= 0)) {\n fail('Emitter', `'${name}' must be a number of 0 or more, or a list of them a particle goes through, got ${describe(v)}`)\n }\n return list.map((n) => Math.min(n, most))\n}\n\n/** One stop as a number, several as a list. @param {number[]} stops @returns {number | number[]} */\nfunction unstop(stops) {\n return stops.length === 1 ? stops[0] : [...stops]\n}\n\n/** The value t (0-1) of the way through evenly spaced stops. @param {number[]} stops @param {number} t */\nfunction along(stops, t) {\n if (stops.length === 1) return stops[0]\n const x = Math.min(Math.max(t, 0), 1) * (stops.length - 1)\n const s = Math.min(Math.floor(x), stops.length - 2)\n return stops[s] + (stops[s + 1] - stops[s]) * (x - s)\n}\n\n/** @param {number | [number, number]} v @returns {[number, number]} */\nfunction spinRange(v) {\n const range = typeof v === 'number' ? [-Math.abs(v), Math.abs(v)] : v\n if (!Array.isArray(range) || range.length !== 2 || !range.every((n) => typeof n === 'number' && Number.isFinite(n))) {\n fail('Emitter', `'spin' must be radians per second, or a [min, max] range, got ${describe(v)}`)\n }\n return [Math.min(range[0], range[1]), Math.max(range[0], range[1])]\n}\n\n/** @param {ColorInput | ColorInput[]} v @returns {number[][]} */\nfunction colorStops(v) {\n // a single [r, g, b] array is one colour, not three\n const list = Array.isArray(v) && !(v.length === 3 && v.every((n) => typeof n === 'number')) ? v : [v]\n if (!list.length) fail('Emitter', \"'color' needs at least one colour\")\n return list.map((c) => Color.fromInput(/** @type {ColorInput} */ (c), 'Emitter').toSRGB())\n}\n\n/** A random point within a ball of radius r, evenly spread. @param {number} r @returns {number[]} */\nfunction randomInBall(r) {\n if (!r) return [0, 0, 0]\n let x, y, z\n do {\n x = Math.random() * 2 - 1\n y = Math.random() * 2 - 1\n z = Math.random() * 2 - 1\n } while (x * x + y * y + z * z > 1)\n return [x * r, y * r, z * r]\n}\n","// Simple physics as a behaviour rather than a base class: any node moves with velocity, gravity\n// and drag once its body's update is attached - node.onUpdate(body.update).\nimport { Vec3 } from '../value/Vec3.js'\nimport { checkNumber, checkOptions } from '../validate.js'\n\n/** @import { Node } from '../scene/Node.js' */\n/** @import { Vec3Input } from '../types.js' */\n\n/**\n * @typedef {object} BodyOptions\n * @property {Vec3Input} [velocity] units per second (default [0, 0, 0])\n * @property {Vec3Input} [gravity] acceleration, e.g. [0, -9.8, 0] (default none)\n * @property {number} [drag] share of velocity lost per second (default 0)\n */\n\n/**\n * Velocity, gravity and drag for a node.\n *\n * const body = new Body({ velocity: [0, 20, 0], gravity: [0, -9.8, 0] })\n * rocket.onUpdate(body.update)\n */\nconst OPTIONS = ['velocity', 'gravity', 'drag']\nconst HINTS = {\n impulse: 'call body.impulse([x, y, z]) after creating it',\n mass: 'a Body has no mass - impulse() changes velocity directly',\n}\n\nexport class Body {\n /** The options it takes, and hints for names people reach for instead. */\n static OPTIONS = OPTIONS\n static HINTS = HINTS\n velocity = new Vec3()\n gravity = new Vec3()\n drag = 0\n\n /** @param {BodyOptions} [options] */\n constructor(options = {}) {\n checkOptions('Body', options, OPTIONS, HINTS)\n if (options.velocity !== undefined) this.velocity.copy(options.velocity, 'Body')\n if (options.gravity !== undefined) this.gravity.copy(options.gravity, 'Body')\n if (options.drag !== undefined) this.drag = Math.max(0, checkNumber('Body', 'drag', options.drag))\n }\n\n /** Add to the velocity at once - a kick. @param {Vec3Input} v @returns {this} */\n impulse(v) {\n this.velocity.add(Vec3.fromInput(v, 'Body'))\n return this\n }\n\n /**\n * Move a node one step. Bound, so it can be handed straight to node.onUpdate().\n * @param {Node} node\n * @param {number} dt seconds\n */\n update = (node, dt) => {\n const v = this.velocity, keep = Math.exp(-this.drag * dt)\n for (let k = 0; k < 3; k++) {\n v[k] = (v[k] + this.gravity[k] * dt) * keep\n node.position[k] += v[k] * dt\n }\n }\n}\n"],"mappings":";;AAyEA,IAAMc,IAAgB;CAAC;CAAU;CAAgB;CAAY;CAAkB;CAAY;AAAO,GAE5FC,IAAoB;CAA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CACnB,OAAO;CAGP,YAAY,IAAU,CAAC,GAAG;EAIxB,AAHAF,EAAa,QAAQe,GAAS8F,GAASC,CAAK,GACxC/F,EAAQ,aAAa,KAAA,KAAW,KAAK,SAAS,KAAKA,EAAQ,UAAU,MAAM,GAC3EA,EAAQ,YAAY,KAAA,KAAW,KAAK,QAAQ,KAAKA,EAAQ,SAAS,MAAM,GACxEA,EAAQ,SAAS,KAAA,MAAW,KAAK,OAAO,KAAK,IAAI,GAAGlB,EAAY,QAAQ,QAAQkB,EAAQ,IAAI,CAAC;CACnG;CAGA,QAAQ,GAAG;EAET,OADA,KAAK,SAAS,IAAIb,EAAK,UAAU+B,GAAG,MAAM,CAAC,GACpC;CACT;CAOA,UAAU,GAAM,MAAO;EACrB,IAAMA,IAAI,KAAK,UAAUuB,IAAO,KAAK,IAAI,CAAC,KAAK,OAAO7B,CAAE;EACxD,KAAK,IAAIuC,IAAI,GAAGA,IAAI,GAAG,KAErB,AADA,EAAEA,MAAMjC,EAAEiC,KAAK,KAAK,QAAQA,KAAKvC,KAAM6B,GACvC,EAAK,SAASU,MAAMjC,EAAEiC,KAAKvC;CAE/B;AACF"}
|
package/dist/passes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Bt as e, C as t, Ct as n, D as r, E as i, N as a, O as o, Rt as s, S as c, T as l, Tt as u, Vt as d, j as f, k as p, w as m, wt as h, x as g } from "./core.js";
|
|
2
2
|
// src/irradiance.js
|
|
3
|
-
function
|
|
3
|
+
function _({ data: e, width: t, height: n }) {
|
|
4
4
|
let r = /* @__PURE__ */ new Float64Array(27), i = Math.max(1, Math.floor(t / 128)), a = Math.max(1, Math.floor(n / 64)), o = 2 * Math.PI * i / t, s = Math.PI * a / n;
|
|
5
5
|
for (let c = 0; c < n; c += a) {
|
|
6
6
|
let l = (c + .5 * a) / n * Math.PI, u = Math.sin(l), d = Math.cos(l), f = o * s * u;
|
|
@@ -47,7 +47,7 @@ function m({ data: e, width: t, height: n }) {
|
|
|
47
47
|
return u;
|
|
48
48
|
}
|
|
49
49
|
// src/webgl/passes.js
|
|
50
|
-
var
|
|
50
|
+
var v = `precision highp float;
|
|
51
51
|
in vec2 vUv;
|
|
52
52
|
uniform mat4 uInverseViewProjection;
|
|
53
53
|
uniform vec3 uCameraPosition;
|
|
@@ -56,9 +56,9 @@ uniform float uLod;
|
|
|
56
56
|
uniform float uIntensity;
|
|
57
57
|
uniform int uHdr;
|
|
58
58
|
out vec4 fragColor;
|
|
59
|
-
${
|
|
60
|
-
${
|
|
61
|
-
${
|
|
59
|
+
${r}
|
|
60
|
+
${o}
|
|
61
|
+
${a}
|
|
62
62
|
void main() {
|
|
63
63
|
vec4 far = uInverseViewProjection * vec4(vUv * 2.0 - 1.0, 1.0, 1.0);
|
|
64
64
|
vec3 d = normalize(far.xyz / far.w - uCameraPosition);
|
|
@@ -66,14 +66,14 @@ vec3 c = textureLod(uBackground, equirectUv(d), uLod).rgb * uIntensity;
|
|
|
66
66
|
if (uHdr == 1) c = toneMap(c);
|
|
67
67
|
fragColor = vec4(linearToSrgb(c), 1.0);
|
|
68
68
|
}
|
|
69
|
-
`,
|
|
69
|
+
`, y = `precision highp float;
|
|
70
70
|
in vec2 vUv;
|
|
71
71
|
uniform sampler2D uSource;
|
|
72
72
|
uniform float uRoughness;
|
|
73
73
|
uniform float uSourceTexels;
|
|
74
74
|
uniform float uCopyLod;
|
|
75
75
|
out vec4 fragColor;
|
|
76
|
-
${
|
|
76
|
+
${o}
|
|
77
77
|
const float PI = 3.14159265359;
|
|
78
78
|
const uint SAMPLES = 192u;
|
|
79
79
|
vec2 hammersley(uint i) {
|
|
@@ -114,74 +114,74 @@ weight += NoL;
|
|
|
114
114
|
}
|
|
115
115
|
fragColor = vec4(sum / max(weight, 1e-4), 1.0);
|
|
116
116
|
}
|
|
117
|
-
`,
|
|
117
|
+
`, b = `precision highp float;
|
|
118
118
|
in vec3 vWorldPosition;
|
|
119
119
|
uniform vec4 uMask;
|
|
120
120
|
out vec4 fragColor;
|
|
121
|
-
${
|
|
121
|
+
${l}
|
|
122
122
|
void main() {
|
|
123
123
|
clipAway(vWorldPosition);
|
|
124
124
|
fragColor = uMask;
|
|
125
125
|
}
|
|
126
|
-
`,
|
|
126
|
+
`, x = 16, S = `uniform sampler2D uSource;
|
|
127
127
|
uniform vec2 uStep;
|
|
128
128
|
uniform int uRadius;
|
|
129
129
|
vec4 widened(vec2 uv) {
|
|
130
130
|
vec4 best = vec4(0.0);
|
|
131
131
|
float nearest = 1e9;
|
|
132
|
-
for (int i = -${
|
|
132
|
+
for (int i = -${x}; i <= ${x}; i++) {
|
|
133
133
|
float d = abs(float(i));
|
|
134
134
|
if (d > float(uRadius)) continue;
|
|
135
135
|
vec4 s = texture(uSource, uv + uStep * float(i));
|
|
136
|
-
if (s.a > 0.0 && d <= s.a * ${
|
|
136
|
+
if (s.a > 0.0 && d <= s.a * ${x}.0 && d < nearest) {
|
|
137
137
|
best = s;
|
|
138
138
|
nearest = d;
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
141
|
return best;
|
|
142
142
|
}
|
|
143
|
-
`,
|
|
143
|
+
`, C = `precision highp float;
|
|
144
144
|
in vec2 vUv;
|
|
145
145
|
out vec4 fragColor;
|
|
146
|
-
${
|
|
146
|
+
${S}
|
|
147
147
|
void main() {
|
|
148
148
|
fragColor = widened(vUv);
|
|
149
149
|
}
|
|
150
|
-
`,
|
|
150
|
+
`, w = `precision highp float;
|
|
151
151
|
in vec2 vUv;
|
|
152
152
|
uniform sampler2D uMask;
|
|
153
153
|
out vec4 fragColor;
|
|
154
|
-
${
|
|
155
|
-
${
|
|
154
|
+
${r}
|
|
155
|
+
${S}
|
|
156
156
|
void main() {
|
|
157
157
|
if (texture(uMask, vUv).a > 0.0) discard;
|
|
158
158
|
vec4 ring = widened(vUv);
|
|
159
159
|
if (ring.a <= 0.0) discard;
|
|
160
160
|
fragColor = vec4(linearToSrgb(ring.rgb), 1.0);
|
|
161
161
|
}
|
|
162
|
-
`,
|
|
162
|
+
`, T = `precision highp float;
|
|
163
163
|
in vec3 vWorldPosition;
|
|
164
164
|
uniform vec4 uMask;
|
|
165
165
|
layout(location = 0) out vec4 fragColor;
|
|
166
166
|
layout(location = 1) out vec4 fragCoverage;
|
|
167
|
-
${
|
|
167
|
+
${l}
|
|
168
168
|
void main() {
|
|
169
169
|
clipAway(vWorldPosition);
|
|
170
170
|
fragColor = uMask;
|
|
171
171
|
fragCoverage = vec4(1.0);
|
|
172
172
|
}
|
|
173
|
-
`,
|
|
173
|
+
`, E = `uniform sampler2D uMask;
|
|
174
174
|
uniform sampler2D uCoverage;
|
|
175
175
|
uniform vec2 uTexel;
|
|
176
176
|
uniform int uRadius;
|
|
177
177
|
vec4 masked(vec2 uv, float coverage) {
|
|
178
178
|
vec4 m = texture(uMask, uv) / coverage;
|
|
179
|
-
return vec4(m.rgb, m.a * ${
|
|
179
|
+
return vec4(m.rgb, m.a * ${x}.0);
|
|
180
180
|
}
|
|
181
|
-
`,
|
|
181
|
+
`, D = `precision highp float;
|
|
182
182
|
in vec2 vUv;
|
|
183
183
|
out vec4 fragColor;
|
|
184
|
-
${
|
|
184
|
+
${E}
|
|
185
185
|
void main() {
|
|
186
186
|
float nearest = 1e9;
|
|
187
187
|
int at = 0;
|
|
@@ -200,12 +200,12 @@ found = true;
|
|
|
200
200
|
}
|
|
201
201
|
fragColor = found ? vec4(float(at + 32) / 255.0, 0.0, 0.0, 1.0) : vec4(0.0);
|
|
202
202
|
}
|
|
203
|
-
`,
|
|
203
|
+
`, O = `precision highp float;
|
|
204
204
|
in vec2 vUv;
|
|
205
205
|
uniform sampler2D uNearest;
|
|
206
206
|
out vec4 fragColor;
|
|
207
|
-
${
|
|
208
|
-
${
|
|
207
|
+
${r}
|
|
208
|
+
${E}
|
|
209
209
|
void main() {
|
|
210
210
|
float here = texture(uCoverage, vUv).r;
|
|
211
211
|
if (here >= 1.0) discard;
|
|
@@ -229,7 +229,13 @@ float alpha = clamp(ring.a - nearest + 0.5, 0.0, 1.0) * (1.0 - here);
|
|
|
229
229
|
if (alpha <= 0.0) discard;
|
|
230
230
|
fragColor = vec4(linearToSrgb(ring.rgb) * alpha, alpha);
|
|
231
231
|
}
|
|
232
|
-
`,
|
|
232
|
+
`, k = [f, b], A = [p, C], j = [p, w], M = [f, T], N = [p, D], P = [p, O], F = [f, i], I = [p, v], L = [p, y], R = 512, z = 6, B = class {
|
|
233
|
+
time = 0;
|
|
234
|
+
cameraPosition = [
|
|
235
|
+
0,
|
|
236
|
+
0,
|
|
237
|
+
0
|
|
238
|
+
];
|
|
233
239
|
#e = /* @__PURE__ */ new WeakMap();
|
|
234
240
|
#t = null;
|
|
235
241
|
#n = null;
|
|
@@ -253,33 +259,33 @@ fragColor = vec4(linearToSrgb(ring.rgb) * alpha, alpha);
|
|
|
253
259
|
});
|
|
254
260
|
let r = e.image, i = this.#d(r.width, r.height, Math.floor(Math.log2(Math.max(r.width, r.height))) + 1);
|
|
255
261
|
n.texSubImage2D(n.TEXTURE_2D, 0, 0, 0, r.width, r.height, n.RGBA, n.FLOAT, r.data), n.generateMipmap(n.TEXTURE_2D);
|
|
256
|
-
let a = this.#d(
|
|
262
|
+
let a = this.#d(R, R / 2, z), o = this.programs.ready("prefilter", [], L);
|
|
257
263
|
n.useProgram(o.program), o.set("uSource", i), o.set("uSourceTexels", r.width * r.height);
|
|
258
264
|
let s = n.createFramebuffer();
|
|
259
265
|
n.bindFramebuffer(n.FRAMEBUFFER, s), n.disable(n.DEPTH_TEST), n.disable(n.BLEND), n.disable(n.CULL_FACE);
|
|
260
|
-
for (let e = 0; e <
|
|
261
|
-
let t =
|
|
266
|
+
for (let e = 0; e < z; e++) {
|
|
267
|
+
let t = R >> e;
|
|
262
268
|
n.framebufferTexture2D(n.FRAMEBUFFER, n.COLOR_ATTACHMENT0, n.TEXTURE_2D, a, e), n.viewport(0, 0, t, t / 2), o.set("uRoughness", e / 5), o.set("uCopyLod", Math.max(0, Math.log2(r.width / t))), this.programs.drawFullscreen();
|
|
263
269
|
}
|
|
264
270
|
return n.bindFramebuffer(n.FRAMEBUFFER, null), n.deleteFramebuffer(s), n.enable(n.DEPTH_TEST), t = {
|
|
265
271
|
source: i,
|
|
266
272
|
prefiltered: a,
|
|
267
273
|
maxLod: 5,
|
|
268
|
-
sh:
|
|
274
|
+
sh: _(r),
|
|
269
275
|
version: e.version
|
|
270
276
|
}, this.#e.set(e, t), t;
|
|
271
277
|
}
|
|
272
|
-
background(
|
|
273
|
-
let i = this.gl,
|
|
274
|
-
if (
|
|
275
|
-
let c = this.programs.ready("background", [],
|
|
278
|
+
background(t, n, r) {
|
|
279
|
+
let i = this.gl, a = n.isHDR, o = a ? this.environment(n) : null;
|
|
280
|
+
if (a && !o) return;
|
|
281
|
+
let c = this.programs.ready("background", [], I);
|
|
276
282
|
i.useProgram(c.program);
|
|
277
|
-
let l = Math.min(Math.max(
|
|
278
|
-
|
|
283
|
+
let l = Math.min(Math.max(t.backgroundBlur, 0), 1);
|
|
284
|
+
o ? (c.set("uBackground", l > 0 ? o.prefiltered : o.source), c.set("uLod", l * o.maxLod), c.set("uIntensity", t.environment === n ? t.environmentIntensity : 1)) : (c.set("uBackground", this.resources.texture(n)), c.set("uLod", l * 6), c.set("uIntensity", 1)), c.set("uHdr", +!!a), c.set("uExposure", r.exposure), c.set("uToneMapping", r.toneMapping), c.set("uInverseViewProjection", e(s(), t.camera.viewProjection)), c.set("uCameraPosition", t.camera.position), i.disable(i.DEPTH_TEST), i.disable(i.BLEND), i.disable(i.CULL_FACE), i.depthMask(!1), this.programs.drawFullscreen(), i.depthMask(!0), i.enable(i.DEPTH_TEST);
|
|
279
285
|
}
|
|
280
286
|
shadowMap(e) {
|
|
281
287
|
let t = this.gl;
|
|
282
|
-
this.#t ??= this.#l(
|
|
288
|
+
this.#t ??= this.#l(h), t.bindFramebuffer(t.FRAMEBUFFER, this.#t.framebuffer), t.viewport(0, 0, h, h), this.#a(), this.#s(e.casters, e.viewProjection), this.#o();
|
|
283
289
|
let n = new Float32Array([
|
|
284
290
|
.5,
|
|
285
291
|
0,
|
|
@@ -303,20 +309,20 @@ fragColor = vec4(linearToSrgb(ring.rgb) * alpha, alpha);
|
|
|
303
309
|
sampler: "uShadowMap",
|
|
304
310
|
texture: this.#t.texture,
|
|
305
311
|
uniforms: {
|
|
306
|
-
uShadowMatrix:
|
|
307
|
-
uShadowTexel: 1 /
|
|
312
|
+
uShadowMatrix: d(/* @__PURE__ */ new Float32Array(16), n, e.viewProjection),
|
|
313
|
+
uShadowTexel: 1 / h,
|
|
308
314
|
uShadowBias: e.texelSize * 1.5
|
|
309
315
|
}
|
|
310
316
|
};
|
|
311
317
|
}
|
|
312
318
|
spotShadowMaps(e) {
|
|
313
319
|
let t = this.gl;
|
|
314
|
-
this.#n ??= this.#l(
|
|
315
|
-
let n = /* @__PURE__ */ new Float32Array(64), r = /* @__PURE__ */ new Float32Array(16),
|
|
316
|
-
for (let
|
|
317
|
-
let e =
|
|
318
|
-
t.viewport(e *
|
|
319
|
-
let
|
|
320
|
+
this.#n ??= this.#l(u), t.bindFramebuffer(t.FRAMEBUFFER, this.#n.framebuffer), t.viewport(0, 0, u, u), this.#a();
|
|
321
|
+
let n = /* @__PURE__ */ new Float32Array(64), r = /* @__PURE__ */ new Float32Array(16), i = /* @__PURE__ */ new Float32Array(4), a = u / 2;
|
|
322
|
+
for (let o of e) {
|
|
323
|
+
let e = o.tile % 2, s = Math.floor(o.tile / 2);
|
|
324
|
+
t.viewport(e * a, s * a, a, a), this.#s(o.casters, o.viewProjection);
|
|
325
|
+
let c = new Float32Array([
|
|
320
326
|
.25,
|
|
321
327
|
0,
|
|
322
328
|
0,
|
|
@@ -330,16 +336,16 @@ fragColor = vec4(linearToSrgb(ring.rgb) * alpha, alpha);
|
|
|
330
336
|
.5,
|
|
331
337
|
0,
|
|
332
338
|
e * .5 + .25,
|
|
333
|
-
|
|
339
|
+
s * .5 + .25,
|
|
334
340
|
.5,
|
|
335
341
|
1
|
|
336
342
|
]);
|
|
337
|
-
n.set(
|
|
343
|
+
n.set(d(/* @__PURE__ */ new Float32Array(16), c, o.viewProjection), o.tile * 16), r.set([
|
|
338
344
|
e * .5,
|
|
339
|
-
|
|
345
|
+
s * .5,
|
|
340
346
|
e * .5 + .5,
|
|
341
|
-
|
|
342
|
-
],
|
|
347
|
+
s * .5 + .5
|
|
348
|
+
], o.tile * 4), i[o.tile] = o.texelAngle * 1.5;
|
|
343
349
|
}
|
|
344
350
|
return this.#o(), {
|
|
345
351
|
define: "SPOT_SHADOWS",
|
|
@@ -348,15 +354,15 @@ fragColor = vec4(linearToSrgb(ring.rgb) * alpha, alpha);
|
|
|
348
354
|
uniforms: {
|
|
349
355
|
uSpotShadowMatrix: n,
|
|
350
356
|
uSpotShadowTile: r,
|
|
351
|
-
uSpotShadowBias:
|
|
352
|
-
uSpotShadowTexel: 1 /
|
|
357
|
+
uSpotShadowBias: i,
|
|
358
|
+
uSpotShadowTexel: 1 / u
|
|
353
359
|
}
|
|
354
360
|
};
|
|
355
361
|
}
|
|
356
362
|
pointShadowMap(e) {
|
|
357
363
|
let t = this.gl;
|
|
358
|
-
return this.#r ??= this.#u(
|
|
359
|
-
t.bindFramebuffer(t.FRAMEBUFFER, this.#r.framebuffers[
|
|
364
|
+
return this.#r ??= this.#u(n), e.faces.forEach((e, r) => {
|
|
365
|
+
t.bindFramebuffer(t.FRAMEBUFFER, this.#r.framebuffers[r]), t.viewport(0, 0, n, n), this.#a(), this.#s(e.casters, e.viewProjection);
|
|
360
366
|
}), this.#o(), {
|
|
361
367
|
define: "POINT_SHADOW",
|
|
362
368
|
sampler: "uPointShadowMap",
|
|
@@ -376,68 +382,74 @@ fragColor = vec4(linearToSrgb(ring.rgb) * alpha, alpha);
|
|
|
376
382
|
let e = this.gl;
|
|
377
383
|
e.disable(e.POLYGON_OFFSET_FILL), e.bindFramebuffer(e.FRAMEBUFFER, null);
|
|
378
384
|
}
|
|
379
|
-
#s(e,
|
|
380
|
-
let
|
|
381
|
-
for (let
|
|
382
|
-
let e =
|
|
383
|
-
|
|
384
|
-
let
|
|
385
|
-
|
|
386
|
-
let
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
385
|
+
#s(e, n) {
|
|
386
|
+
let r = this.gl;
|
|
387
|
+
for (let i of e) {
|
|
388
|
+
let e = i.instances, a = e ? ["INSTANCED"] : [];
|
|
389
|
+
i.clip && a.push("CLIP");
|
|
390
|
+
let o = i.material, s = o.alphaTest > 0, l = s && !!o.map && o.triplanar > 0;
|
|
391
|
+
s && a.push("ALPHA_TEST", ...o.map ? ["MAP"] : [], ...l ? ["TRIPLANAR"] : []), s && o.map?.uvSet === 1 && i.geometry.uvs2 && a.push("UV2", "uMapSet vUv2");
|
|
392
|
+
let u = c(o);
|
|
393
|
+
u && a.push(...t(u, !0));
|
|
394
|
+
let d = this.programs.ready("depth", a, F, u?.source);
|
|
395
|
+
if (!d.failed) {
|
|
396
|
+
if (r.useProgram(d.program), u && m(d, o, this.time, this.cameraPosition, (e) => this.resources.texture(e)), d.set("uViewProjection", n), d.set("uModel", i.worldMatrix), i.clip && d.set("uClipPlane", i.clip.plane), s) {
|
|
397
|
+
d.set("uOpacity", o.opacity), d.set("uAlphaTest", o.alphaTest);
|
|
398
|
+
let e = o.map;
|
|
399
|
+
e && (d.set("uMapUv", g(e)), d.set("uMap", this.resources.texture(e))), l && d.set("uTriplanar", o.triplanar);
|
|
400
|
+
}
|
|
401
|
+
this.resources.drawTriangles(i.geometry, e);
|
|
391
402
|
}
|
|
392
|
-
this.resources.drawTriangles(r.geometry, e);
|
|
393
403
|
}
|
|
394
404
|
}
|
|
395
|
-
outlineMask(e,
|
|
396
|
-
let
|
|
397
|
-
|
|
398
|
-
let
|
|
399
|
-
for (let
|
|
400
|
-
let e = Math.min(
|
|
401
|
-
|
|
402
|
-
for (let
|
|
403
|
-
let
|
|
404
|
-
|
|
405
|
-
let
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
405
|
+
outlineMask(e, n, r, i, a, o) {
|
|
406
|
+
let s = this.gl, l = this.#c(r, i, o);
|
|
407
|
+
s.bindFramebuffer(s.FRAMEBUFFER, l.msaa ? l.msaa.framebuffer : l.mask.framebuffer), l.msaa && s.drawBuffers([s.COLOR_ATTACHMENT0, s.COLOR_ATTACHMENT1]), s.viewport(0, 0, r, i), s.clearColor(0, 0, 0, 0), s.clear(s.COLOR_BUFFER_BIT), s.disable(s.DEPTH_TEST), s.disable(s.BLEND), s.disable(s.CULL_FACE), s.depthMask(!1);
|
|
408
|
+
let u = 1;
|
|
409
|
+
for (let r of e) {
|
|
410
|
+
let e = Math.min(x, Math.max(1, Math.round(r.width * a)));
|
|
411
|
+
u = Math.max(u, e);
|
|
412
|
+
for (let i of r.items) {
|
|
413
|
+
let a = i.instances ? ["INSTANCED"] : [];
|
|
414
|
+
i.clip && a.push("CLIP");
|
|
415
|
+
let l = c(i.material);
|
|
416
|
+
l && a.push(...t(l, !1));
|
|
417
|
+
let u = o ? this.programs.ready("outline-cover", a, M, l?.source) : this.programs.ready("outline-mask", a, k, l?.source);
|
|
418
|
+
u.failed || (s.useProgram(u.program), l && m(u, i.material, n.time ?? 0, n.camera.position, (e) => this.resources.texture(e)), u.set("uViewProjection", n.camera.viewProjection), u.set("uModel", i.worldMatrix), u.set("uMask", [
|
|
419
|
+
r.color[0],
|
|
420
|
+
r.color[1],
|
|
421
|
+
r.color[2],
|
|
422
|
+
e / x
|
|
423
|
+
]), i.clip && u.set("uClipPlane", i.clip.plane), this.resources.drawTriangles(i.geometry, i.instances));
|
|
412
424
|
}
|
|
413
425
|
}
|
|
414
|
-
if (
|
|
415
|
-
|
|
416
|
-
for (let [e, t] of [[
|
|
426
|
+
if (l.msaa && l.coverage) {
|
|
427
|
+
s.bindFramebuffer(s.READ_FRAMEBUFFER, l.msaa.framebuffer);
|
|
428
|
+
for (let [e, t] of [[s.COLOR_ATTACHMENT0, l.mask], [s.COLOR_ATTACHMENT1, l.coverage]]) s.readBuffer(e), s.bindFramebuffer(s.DRAW_FRAMEBUFFER, t.framebuffer), s.blitFramebuffer(0, 0, r, i, 0, 0, r, i, s.COLOR_BUFFER_BIT, s.NEAREST);
|
|
417
429
|
}
|
|
418
|
-
if (
|
|
419
|
-
let e = this.programs.ready("outline-smooth-widen", [],
|
|
420
|
-
|
|
430
|
+
if (s.bindFramebuffer(s.FRAMEBUFFER, l.wide.framebuffer), s.clear(s.COLOR_BUFFER_BIT), l.coverage) {
|
|
431
|
+
let e = this.programs.ready("outline-smooth-widen", [], N);
|
|
432
|
+
s.useProgram(e.program), e.set("uMask", l.mask.texture), e.set("uCoverage", l.coverage.texture), e.set("uTexel", [1 / r, 1 / i]), e.set("uRadius", u);
|
|
421
433
|
} else {
|
|
422
|
-
let e = this.programs.ready("outline-widen", [],
|
|
423
|
-
|
|
434
|
+
let e = this.programs.ready("outline-widen", [], A);
|
|
435
|
+
s.useProgram(e.program), e.set("uSource", l.mask.texture), e.set("uStep", [1 / r, 0]), e.set("uRadius", u);
|
|
424
436
|
}
|
|
425
|
-
return this.programs.drawFullscreen(),
|
|
426
|
-
mask:
|
|
427
|
-
wide:
|
|
428
|
-
coverage:
|
|
429
|
-
radius:
|
|
430
|
-
width:
|
|
431
|
-
height:
|
|
437
|
+
return this.programs.drawFullscreen(), s.depthMask(!0), s.enable(s.DEPTH_TEST), s.bindFramebuffer(s.FRAMEBUFFER, null), {
|
|
438
|
+
mask: l.mask.texture,
|
|
439
|
+
wide: l.wide.texture,
|
|
440
|
+
coverage: l.coverage?.texture ?? null,
|
|
441
|
+
radius: u,
|
|
442
|
+
width: r,
|
|
443
|
+
height: i
|
|
432
444
|
};
|
|
433
445
|
}
|
|
434
446
|
outlineComposite(e) {
|
|
435
447
|
let t = this.gl;
|
|
436
448
|
if (e.coverage) {
|
|
437
|
-
let n = this.programs.ready("outline-smooth-composite", [],
|
|
449
|
+
let n = this.programs.ready("outline-smooth-composite", [], P);
|
|
438
450
|
t.useProgram(n.program), n.set("uNearest", e.wide), n.set("uMask", e.mask), n.set("uCoverage", e.coverage), n.set("uTexel", [1 / e.width, 1 / e.height]), n.set("uRadius", e.radius);
|
|
439
451
|
} else {
|
|
440
|
-
let n = this.programs.ready("outline-composite", [],
|
|
452
|
+
let n = this.programs.ready("outline-composite", [], j);
|
|
441
453
|
t.useProgram(n.program), n.set("uSource", e.wide), n.set("uMask", e.mask), n.set("uStep", [0, 1 / e.height]), n.set("uRadius", e.radius);
|
|
442
454
|
}
|
|
443
455
|
t.disable(t.DEPTH_TEST), t.disable(t.CULL_FACE), t.depthMask(!1), t.enable(t.BLEND), t.blendFunc(t.ONE, t.ONE_MINUS_SRC_ALPHA), this.programs.drawFullscreen(), t.depthMask(!0), t.enable(t.DEPTH_TEST);
|
|
@@ -519,6 +531,6 @@ fragColor = vec4(linearToSrgb(ring.rgb) * alpha, alpha);
|
|
|
519
531
|
return r.activeTexture(r.TEXTURE15), r.bindTexture(r.TEXTURE_2D, i), r.texStorage2D(r.TEXTURE_2D, n, r.RGBA16F, e, t), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_WRAP_S, r.REPEAT), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_WRAP_T, r.CLAMP_TO_EDGE), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_MIN_FILTER, r.LINEAR_MIPMAP_LINEAR), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_MAG_FILTER, r.LINEAR), r.pixelStorei(r.UNPACK_FLIP_Y_WEBGL, !1), i;
|
|
520
532
|
}
|
|
521
533
|
};
|
|
522
|
-
export {
|
|
534
|
+
export { B as Passes };
|
|
523
535
|
|
|
524
536
|
//# sourceMappingURL=passes.js.map
|