threedog 0.2.1 → 0.7.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/CHANGELOG.md +371 -1
- package/README.md +21 -12
- package/core/Animator.js +15 -12
- package/core/Assets.js +92 -26
- package/core/Audio.js +45 -38
- package/core/Baked.js +30 -15
- package/core/Batching.js +102 -15
- package/core/Budget.js +40 -19
- package/core/Clock.js +40 -80
- package/core/DebugOverrides.js +75 -25
- package/core/Describe.js +105 -45
- package/core/Engine.js +203 -75
- package/core/Events.js +4 -1
- package/core/I18n.js +25 -13
- package/core/Input.js +175 -80
- package/core/Inspect.js +8 -5
- package/core/InspectMaterials.js +84 -0
- package/core/InspectPerf.js +68 -0
- package/core/InspectScene.js +13 -2
- package/core/Loading.js +2 -1
- package/core/MaterialInfo.js +7 -5
- package/core/Ops.js +50 -9
- package/core/Random.js +9 -2
- package/core/Rays.js +12 -7
- package/core/SaveStore.js +41 -9
- package/core/Saves.js +71 -15
- package/core/Stream.js +13 -10
- package/core/Threads.js +26 -7
- package/core/UrlState.js +69 -13
- package/core/Views.js +10 -6
- package/core/actor.js +1 -1
- package/core/adaptive.js +93 -0
- package/core/assetEntry.js +27 -7
- package/core/assetHot.js +176 -0
- package/core/assetLoad.js +44 -19
- package/core/assetManifest.js +53 -11
- package/core/assetReport.js +5 -5
- package/core/audioRules.js +13 -11
- package/core/audioVoice.js +69 -42
- package/core/batchGroups.js +9 -1
- package/core/canvasBox.js +1 -2
- package/core/catalog.js +21 -5
- package/core/cells.js +62 -0
- package/core/checks.js +10 -8
- package/core/chunkStream.js +87 -0
- package/core/control.js +149 -0
- package/core/copies.js +32 -0
- package/core/dayTime.js +12 -6
- package/core/debugView.js +85 -18
- package/core/describeRender.js +117 -21
- package/core/describeText.js +41 -21
- package/core/engineCamera.js +32 -0
- package/core/errorCodes.js +478 -32
- package/core/errorHints.js +41 -142
- package/core/errors.js +31 -8
- package/core/events.generated.js +13 -7
- package/core/gameLights.js +69 -0
- package/core/grayMap.js +20 -0
- package/core/grid.js +14 -13
- package/core/gridLoad.js +2 -1
- package/core/ground.js +6 -4
- package/core/headlessLoad.js +55 -0
- package/core/holds.js +78 -50
- package/core/inputContext.js +11 -7
- package/core/inputDefaults.js +24 -21
- package/core/inputPad.js +1 -1
- package/core/inputPointer.js +1 -1
- package/core/inputTouch.js +2 -2
- package/core/lifecycle.js +46 -12
- package/core/lightWatch.js +9 -5
- package/core/loop.js +109 -43
- package/core/math.js +18 -3
- package/core/moduleSlots.js +100 -0
- package/core/moduleTypes.js +86 -0
- package/core/modules.js +147 -29
- package/core/movement.js +2 -2
- package/core/namedModel.js +74 -0
- package/core/opsCheck.js +3 -3
- package/core/opsFail.js +15 -6
- package/core/opsGuard.js +4 -4
- package/core/opsRun.js +4 -3
- package/core/owned.js +171 -0
- package/core/page.js +44 -10
- package/core/params.js +30 -8
- package/core/pictureSlots.js +33 -0
- package/core/pictureWriters.js +343 -0
- package/core/plugins.js +245 -0
- package/core/saveApply.js +99 -17
- package/core/saveFiles.js +8 -6
- package/core/saveValues.js +46 -0
- package/core/saveVersions.js +20 -3
- package/core/scatter.js +48 -19
- package/core/sceneDelta.js +211 -0
- package/core/sceneMethods.js +31 -33
- package/core/screenToGround.js +1 -1
- package/core/services.js +27 -11
- package/core/session.js +28 -25
- package/core/shaderCost.js +78 -0
- package/core/shadowTypes.js +3 -2
- package/core/shadows.js +405 -0
- package/core/sharedPoint.js +303 -0
- package/core/skip.js +36 -28
- package/core/stackFrames.js +45 -0
- package/core/stacks.js +166 -0
- package/core/startup.js +153 -13
- package/core/streamSources.js +0 -11
- package/core/targeting.js +1 -1
- package/core/textureBytes.js +20 -0
- package/core/thingExplain.js +3 -3
- package/core/thingOps.js +29 -6
- package/core/things.js +22 -12
- package/core/threadPure.js +7 -3
- package/core/threePatches.js +47 -0
- package/core/tier.js +21 -10
- package/core/time.js +236 -0
- package/core/uiClock.js +25 -0
- package/core/util.js +8 -2
- package/core/validate.js +66 -87
- package/core/view.js +43 -0
- package/core/wrap.js +106 -0
- package/docs/AI.md +95 -104
- package/docs/API.md +35 -31
- package/docs/CODEMAP.md +231 -176
- package/docs/COMMANDS.md +63 -39
- package/docs/CONVENTIONS.md +99 -31
- package/docs/MIGRATING-0.3.md +248 -0
- package/docs/MIGRATING-0.4.md +320 -0
- package/docs/MIGRATING-0.5.md +68 -0
- package/docs/MIGRATING-0.6.md +126 -0
- package/docs/MIGRATING-0.7.md +39 -0
- package/docs/PERF.md +35 -7
- package/docs/THREE.md +82 -28
- package/docs/api/ambience.md +0 -1
- package/docs/api/assets.md +15 -11
- package/docs/api/audio.md +10 -14
- package/docs/api/budget.md +4 -0
- package/docs/api/calendar.md +41 -0
- package/docs/api/cameraRig.md +16 -5
- package/docs/api/characters.md +41 -24
- package/docs/api/clock.md +6 -5
- package/docs/api/collision.md +10 -7
- package/docs/api/content.md +6 -4
- package/docs/api/dialogue.md +9 -9
- package/docs/api/engine.md +95 -65
- package/docs/api/events.md +20 -14
- package/docs/api/grids.md +10 -3
- package/docs/api/ground.md +1 -1
- package/docs/api/hud.md +5 -2
- package/docs/api/i18n.md +1 -1
- package/docs/api/input.md +13 -7
- package/docs/api/interactions.md +10 -6
- package/docs/api/inventory.md +17 -17
- package/docs/api/level.md +0 -2
- package/docs/api/lightPool.md +3 -1
- package/docs/api/music.md +1 -1
- package/docs/api/nav.md +3 -0
- package/docs/api/occlusion.md +5 -0
- package/docs/api/openWorld.md +2 -1
- package/docs/api/particles.md +6 -3
- package/docs/api/photo.md +0 -2
- package/docs/api/pickups.md +21 -0
- package/docs/api/post.md +8 -5
- package/docs/api/quests.md +2 -3
- package/docs/api/rigid.md +16 -14
- package/docs/api/saves.md +6 -4
- package/docs/api/settings.md +2 -3
- package/docs/api/sky.md +18 -49
- package/docs/api/stream.md +1 -1
- package/docs/api/terrain.md +16 -7
- package/docs/api/terrainDraw.md +12 -0
- package/docs/api/timeline.md +3 -4
- package/docs/api/tools.md +32 -15
- package/docs/api/triggers.md +7 -10
- package/docs/api/vegetation.md +17 -9
- package/docs/api/vegetationDraw.md +10 -0
- package/docs/api/water.md +9 -21
- package/docs/api/weather.md +10 -15
- package/docs/api/weatherDraw.md +10 -0
- package/docs/api/world.md +3 -2
- package/docs/recipes/01-new-game.md +52 -42
- package/docs/recipes/02-npc.md +24 -7
- package/docs/recipes/03-items-pickups.md +57 -22
- package/docs/recipes/04-quests.md +12 -5
- package/docs/recipes/05-props-bake.md +174 -22
- package/docs/recipes/06-clock-events.md +100 -52
- package/docs/recipes/07-flow-scenes-pause.md +50 -10
- package/docs/recipes/08-weather-sky.md +134 -37
- package/docs/recipes/09-materials-lights-models.md +185 -0
- package/docs/recipes/10-blender-level.md +16 -4
- package/docs/recipes/11-vehicles.md +67 -10
- package/docs/recipes/13-animation.md +23 -9
- package/docs/recipes/14-custom-module.md +63 -29
- package/docs/recipes/15-perf.md +31 -10
- package/docs/recipes/16-dev-bridge.md +74 -40
- package/docs/recipes/18-world.md +26 -14
- package/docs/recipes/19-shooting-picking.md +12 -10
- package/docs/recipes/20-content-actions-save.md +60 -18
- package/docs/recipes/21-camera.md +55 -17
- package/docs/recipes/22-shop-gold.md +3 -3
- package/docs/recipes/23-ops.md +17 -11
- package/docs/recipes/26-ui-parts.md +86 -13
- package/docs/recipes/27-net.md +11 -5
- package/docs/recipes/28-terrain.md +36 -3
- package/docs/recipes/29-grid.md +46 -19
- package/docs/recipes/30-templates.md +72 -15
- package/docs/recipes/31-audio.md +20 -12
- package/docs/recipes/32-action-game.md +51 -4
- package/docs/recipes/34-environment-lighting.md +108 -21
- package/docs/recipes/35-picture-from-reference.md +147 -0
- package/docs/recipes/36-sky-ground-plants.md +158 -0
- package/docs/recipes/{37-terrain-look-hooks.md → 37-terrain-hooks.md} +9 -12
- package/docs/recipes/38-environment-hooks.md +67 -0
- package/docs/recipes/40-scene.md +114 -32
- package/docs/recipes/42-terrain-shape.md +2 -2
- package/docs/recipes/43-shader-patches-surfaces.md +206 -0
- package/docs/recipes/44-water-weather-hooks.md +191 -0
- package/docs/recipes/45-cascaded-shadows.md +130 -23
- package/docs/recipes/46-blender-art-route.md +17 -20
- package/docs/recipes/47-island-snow-backdrop.md +13 -15
- package/docs/recipes/49-game-module-contract.md +30 -17
- package/docs/recipes/50-own-renderer.md +4 -4
- package/docs/recipes/51-capability-catalog.md +3 -3
- package/docs/recipes/52-around-the-picture.md +181 -0
- package/docs/recipes/53-ground-up-close.md +12 -12
- package/docs/recipes/54-three-idioms.md +88 -0
- package/docs/recipes/55-mods-time-wrap.md +124 -0
- package/docs/recipes/56-webgpu-tsl.md +303 -0
- package/docs/recipes/57-control.md +104 -0
- package/docs/recipes/58-input.md +60 -0
- package/docs/recipes/59-depth-post-effects.md +83 -0
- package/docs/recipes/60-judge-the-picture.md +70 -0
- package/docs/recipes/61-deploy.md +27 -0
- package/docs/recipes/62-picture-code.md +244 -0
- package/docs/recipes/63-third-person-start.md +77 -0
- package/docs/recipes/64-particles.md +35 -0
- package/examples/action/.threedog/content-index.json +1 -0
- package/examples/action/GAMEMAP.md +20 -14
- package/examples/action/assets.js +8 -0
- package/examples/action/content/actions.js +1 -1
- package/examples/action/content/audio.js +5 -5
- package/examples/action/main.js +12 -15
- package/examples/action/models.js +23 -24
- package/examples/action/picture/index.js +13 -0
- package/examples/action/picture/lights.js +35 -0
- package/examples/action/picture/materials.js +8 -0
- package/examples/action/picture/palette.js +33 -0
- package/examples/plain-picture/.threedog/content-index.json +1 -0
- package/examples/plain-picture/FINDINGS.md +53 -0
- package/examples/plain-picture/GAMEMAP.md +90 -0
- package/examples/plain-picture/main.js +48 -0
- package/examples/plain-picture/picture/index.js +21 -0
- package/examples/plain-picture/picture/lights.js +54 -0
- package/examples/plain-picture/picture/materials.js +24 -0
- package/examples/plain-picture/picture/palette.js +16 -0
- package/examples/plain-picture/picture/sky.js +37 -0
- package/examples/pullable.json +133 -0
- package/examples/rc2d/.threedog/content-index.json +1 -0
- package/examples/rc2d/GAMEMAP.md +88 -0
- package/examples/rc2d/README.md +57 -0
- package/examples/rc2d/main.js +55 -0
- package/examples/rc2d/picture/index.js +18 -0
- package/examples/rc2d/picture/lights.js +11 -0
- package/examples/rc2d/picture/materials.js +18 -0
- package/examples/rc2d/picture/palette.js +15 -0
- package/examples/rc2d/picture/post.js +11 -0
- package/examples/rc2d/rc2d.js +187 -0
- package/examples/rc2d/shaders.js +144 -0
- package/examples/sheepisland/.threedog/content-index.json +1 -0
- package/examples/sheepisland/GAMEMAP.md +22 -17
- package/examples/sheepisland/assets.js +5 -0
- package/examples/sheepisland/content/templates.js +1 -1
- package/examples/sheepisland/content/vegetation.js +3 -1
- package/examples/sheepisland/main.js +29 -18
- package/examples/sheepisland/{style/models.js → models.js} +13 -24
- package/examples/sheepisland/picture/index.js +20 -0
- package/examples/sheepisland/picture/lights.js +34 -0
- package/examples/sheepisland/picture/materials.js +61 -0
- package/examples/sheepisland/picture/palette.js +60 -0
- package/examples/sheepisland/picture/sky.js +20 -0
- package/examples/sheepisland/views.js +1 -1
- package/examples/styletest/.threedog/content-index.json +1 -0
- package/examples/styletest/GAMEMAP.md +25 -22
- package/examples/styletest/assets.js +6 -0
- package/examples/styletest/content/templates.js +1 -1
- package/examples/styletest/content/vegetation.js +1 -1
- package/examples/styletest/main.js +25 -34
- package/examples/styletest/models.js +30 -30
- package/examples/styletest/params.js +2 -5
- package/examples/styletest/picture/env.js +38 -0
- package/examples/styletest/picture/index.js +16 -0
- package/examples/styletest/picture/lights.js +9 -0
- package/examples/styletest/picture/materials.js +28 -0
- package/examples/styletest/picture/palette.js +22 -0
- package/examples/techniques/contact-shadows/README.md +91 -0
- package/examples/techniques/contact-shadows/contactShadows.js +67 -0
- package/examples/techniques/contact-shadows/main.js +55 -0
- package/examples/techniques/contact-shadows/picture/index.js +14 -0
- package/examples/techniques/contact-shadows/picture/lights.js +34 -0
- package/examples/techniques/contact-shadows/picture/materials.js +12 -0
- package/examples/techniques/contact-shadows/picture/palette.js +17 -0
- package/examples/techniques/day-night-grade/README.md +83 -0
- package/examples/techniques/day-night-grade/main.js +41 -0
- package/examples/techniques/day-night-grade/picture/index.js +20 -0
- package/examples/techniques/day-night-grade/picture/lights.js +45 -0
- package/examples/techniques/day-night-grade/picture/materials.js +15 -0
- package/examples/techniques/day-night-grade/picture/palette.js +59 -0
- package/examples/techniques/day-night-grade/picture/post.js +25 -0
- package/examples/techniques/day-night-grade/picture/sky.js +33 -0
- package/examples/techniques/interior-light/README.md +106 -0
- package/examples/techniques/interior-light/interiorLight.js +47 -0
- package/examples/techniques/interior-light/main.js +87 -0
- package/examples/techniques/interior-light/picture/index.js +24 -0
- package/examples/techniques/interior-light/picture/lights.js +52 -0
- package/examples/techniques/interior-light/picture/materials.js +13 -0
- package/examples/techniques/interior-light/picture/palette.js +32 -0
- package/examples/techniques/interior-light/picture/sky.js +40 -0
- package/examples/techniques/modular-building/README.md +126 -0
- package/examples/techniques/modular-building/kit.js +426 -0
- package/examples/techniques/modular-building/main.js +39 -0
- package/examples/techniques/modular-building/picture/index.js +17 -0
- package/examples/techniques/modular-building/picture/lights.js +26 -0
- package/examples/techniques/modular-building/picture/materials.js +13 -0
- package/examples/techniques/modular-building/picture/palette.js +23 -0
- package/examples/techniques/night-lights/README.md +95 -0
- package/examples/techniques/night-lights/main.js +47 -0
- package/examples/techniques/night-lights/nightLights.js +95 -0
- package/examples/techniques/night-lights/picture/index.js +18 -0
- package/examples/techniques/night-lights/picture/lights.js +44 -0
- package/examples/techniques/night-lights/picture/materials.js +15 -0
- package/examples/techniques/night-lights/picture/palette.js +34 -0
- package/examples/techniques/night-lights/picture/post.js +9 -0
- package/examples/techniques/night-lights/picture/sky.js +36 -0
- package/examples/techniques/outlines-and-rim/README.md +59 -0
- package/examples/techniques/outlines-and-rim/main.js +61 -0
- package/examples/techniques/outlines-and-rim/outlines.js +64 -0
- package/examples/techniques/outlines-and-rim/picture/index.js +16 -0
- package/examples/techniques/outlines-and-rim/picture/lights.js +18 -0
- package/examples/techniques/outlines-and-rim/picture/materials.js +11 -0
- package/examples/techniques/outlines-and-rim/picture/palette.js +16 -0
- package/examples/techniques/outlines-and-rim/picture/post.js +10 -0
- package/examples/techniques/procedural-animation/README.md +79 -0
- package/examples/techniques/procedural-animation/creature.js +44 -0
- package/examples/techniques/procedural-animation/main.js +47 -0
- package/examples/techniques/procedural-animation/picture/index.js +15 -0
- package/examples/techniques/procedural-animation/picture/lights.js +35 -0
- package/examples/techniques/procedural-animation/picture/materials.js +10 -0
- package/examples/techniques/procedural-animation/picture/palette.js +16 -0
- package/examples/techniques/procedural-animation/picture/sky.js +31 -0
- package/examples/techniques/procedural-animation/proceduralAnim.js +118 -0
- package/examples/techniques/procedural-surface/README.md +69 -0
- package/examples/techniques/procedural-surface/main.js +65 -0
- package/examples/techniques/procedural-surface/picture/index.js +14 -0
- package/examples/techniques/procedural-surface/picture/lights.js +18 -0
- package/examples/techniques/procedural-surface/picture/materials.js +34 -0
- package/examples/techniques/procedural-surface/picture/palette.js +15 -0
- package/examples/techniques/procedural-surface/surface.js +120 -0
- package/examples/techniques/stylized-shading/README.md +74 -0
- package/examples/techniques/stylized-shading/main.js +62 -0
- package/examples/techniques/stylized-shading/picture/index.js +14 -0
- package/examples/techniques/stylized-shading/picture/lights.js +23 -0
- package/examples/techniques/stylized-shading/picture/materials.js +18 -0
- package/examples/techniques/stylized-shading/picture/palette.js +19 -0
- package/examples/techniques/stylized-shading/shading.js +131 -0
- package/examples/techniques/sun-and-key-light/README.md +133 -0
- package/examples/techniques/sun-and-key-light/keyLight.js +71 -0
- package/examples/techniques/sun-and-key-light/main.js +51 -0
- package/examples/techniques/sun-and-key-light/picture/index.js +13 -0
- package/examples/techniques/sun-and-key-light/picture/lights.js +33 -0
- package/examples/techniques/sun-and-key-light/picture/materials.js +9 -0
- package/examples/techniques/sun-and-key-light/picture/palette.js +29 -0
- package/examples/techniques/sun-and-key-light/picture/sky.js +16 -0
- package/examples/techniques/three-looks/README.md +21 -0
- package/examples/techniques/three-looks/main.js +58 -0
- package/examples/techniques/three-looks/picture/cartoon.js +23 -0
- package/examples/techniques/three-looks/picture/cinematic.js +27 -0
- package/examples/techniques/three-looks/picture/comic.js +27 -0
- package/examples/techniques/three-looks/picture/index.js +14 -0
- package/examples/techniques/three-looks/picture/sun.js +23 -0
- package/examples/techniques/wet-road/README.md +102 -0
- package/examples/techniques/wet-road/main.js +48 -0
- package/examples/techniques/wet-road/picture/index.js +17 -0
- package/examples/techniques/wet-road/picture/lights.js +57 -0
- package/examples/techniques/wet-road/picture/materials.js +16 -0
- package/examples/techniques/wet-road/picture/palette.js +24 -0
- package/examples/techniques/wet-road/picture/sky.js +46 -0
- package/examples/techniques/wet-road/wetRoad.js +209 -0
- package/examples/webgpu-tsl/.threedog/content-index.json +1 -0
- package/examples/webgpu-tsl/GAMEMAP.md +89 -0
- package/examples/webgpu-tsl/README.md +45 -0
- package/examples/webgpu-tsl/main.js +44 -0
- package/examples/webgpu-tsl/picture/index.js +18 -0
- package/examples/webgpu-tsl/picture/lights.js +25 -0
- package/examples/webgpu-tsl/picture/materials.js +36 -0
- package/examples/webgpu-tsl/picture/palette.js +23 -0
- package/examples/webgpu-tsl/picture/post.js +16 -0
- package/examples/webgpu-tsl/picture/sky.js +30 -0
- package/examples/world/.threedog/content-index.json +1 -0
- package/examples/world/GAMEMAP.md +31 -21
- package/examples/world/art/humanoid.js +38 -0
- package/examples/world/art/index.js +11 -0
- package/examples/world/art/models.js +57 -0
- package/examples/world/art/shapes.js +9 -0
- package/examples/world/assets.js +6 -0
- package/examples/world/content/audio.js +5 -5
- package/examples/world/content/items.js +1 -1
- package/examples/world/content/npcs.js +1 -1
- package/examples/world/content/props.js +1 -1
- package/examples/world/content/rules.js +1 -1
- package/examples/world/content/templates.js +1 -1
- package/examples/world/content/vegetation.js +1 -1
- package/examples/world/main.js +22 -18
- package/examples/world/models.js +17 -7
- package/examples/world/picture/index.js +20 -0
- package/examples/world/picture/lights.js +41 -0
- package/examples/world/picture/materials.js +34 -0
- package/examples/world/picture/palette.js +74 -0
- package/examples/world/picture/post.js +9 -0
- package/examples/world/picture/sky.js +39 -0
- package/index.d.ts +44 -54
- package/index.js +29 -25
- package/llms.txt +24 -15
- package/modules/ambience/Ambience.js +34 -23
- package/modules/calendar/Calendar.js +163 -0
- package/modules/calendar/contract.js +21 -0
- package/modules/calendar/index.js +82 -0
- package/modules/{sky → calendar}/schedule.js +17 -16
- package/modules/calendar/seasons.js +55 -0
- package/modules/camera/FirstPersonCamera.js +6 -15
- package/modules/camera/FixedCamera.js +33 -15
- package/modules/camera/FollowCamera.js +90 -16
- package/modules/camera/RtsCamera.js +18 -17
- package/modules/camera/common.js +73 -20
- package/modules/camera/contract.js +17 -0
- package/modules/camera/projection.js +11 -24
- package/modules/characters/Character.js +188 -48
- package/modules/characters/NPC.js +92 -41
- package/modules/characters/assetModel.js +8 -16
- package/modules/characters/behaviors.js +2 -1
- package/modules/characters/contract.js +13 -0
- package/modules/characters/footsteps.js +1 -1
- package/modules/characters/index.js +112 -49
- package/modules/characters/moveHelpers.js +4 -5
- package/modules/characters/movers/common.js +102 -19
- package/modules/characters/movers/manual.js +23 -0
- package/modules/characters/movers/rapier.js +19 -16
- package/modules/characters/movers/simple.js +14 -11
- package/modules/characters/movers/support.js +186 -0
- package/modules/characters/npcAvoid.js +47 -42
- package/modules/characters/npcSim.js +17 -9
- package/modules/characters/npcView.js +44 -16
- package/modules/characters/pathAsync.js +7 -3
- package/modules/characters/playerStrings.js +4 -3
- package/modules/characters/proceduralRig.js +19 -4
- package/modules/characters/useModel.js +2 -2
- package/modules/characters/vehicle.contract.js +21 -0
- package/modules/characters/walkTo.js +21 -7
- package/modules/collision/Collision.js +69 -27
- package/modules/collision/grid.js +8 -6
- package/modules/content/explain.js +2 -2
- package/modules/content/fields.js +102 -0
- package/modules/content/ids.js +8 -6
- package/modules/content/index.js +104 -27
- package/modules/content/loader.js +45 -33
- package/modules/content/models.js +58 -42
- package/modules/content/packs.js +6 -5
- package/modules/content/parts.js +41 -0
- package/modules/content/props.js +20 -11
- package/modules/content/rules.js +66 -0
- package/modules/content/stages.js +80 -0
- package/modules/content/templates.js +123 -30
- package/modules/debug/DebugPanel.js +24 -17
- package/modules/flow/contract.js +16 -0
- package/modules/flow/index.js +21 -15
- package/modules/gameplay/Container.js +41 -32
- package/modules/gameplay/Containers.js +30 -15
- package/modules/gameplay/Dialogue.js +113 -55
- package/modules/gameplay/Interactions.js +273 -72
- package/modules/gameplay/Inventory.js +72 -76
- package/modules/gameplay/Quests.js +75 -39
- package/modules/gameplay/boxOps.js +3 -3
- package/modules/gameplay/dialogue.contract.js +21 -0
- package/modules/gameplay/dialogueBox.js +4 -2
- package/modules/gameplay/dialogueBubbles.js +116 -0
- package/modules/gameplay/interactionPrompt.js +4 -1
- package/modules/gameplay/interactions.contract.js +16 -0
- package/modules/gameplay/inventory.contract.js +23 -0
- package/modules/gameplay/inventoryOps.js +19 -17
- package/modules/gameplay/pickups.js +306 -85
- package/modules/gameplay/questLog.js +6 -6
- package/modules/gameplay/quests.contract.js +17 -0
- package/modules/grid/Grid.js +92 -50
- package/modules/grid/Layer.js +27 -13
- package/modules/grid/index.js +96 -43
- package/modules/grid/ops.js +20 -18
- package/modules/grid/preview.js +37 -29
- package/modules/grid/strings.js +12 -0
- package/modules/grid/tiles.js +19 -22
- package/modules/hlod/index.js +52 -19
- package/modules/hud/index.js +48 -20
- package/modules/hud/parts.js +28 -13
- package/modules/level/heightfield.js +3 -1
- package/modules/level/index.js +49 -23
- package/modules/level/nodes.js +4 -3
- package/modules/lights/index.js +47 -19
- package/modules/music/Music.js +38 -21
- package/modules/music/rules.js +1 -1
- package/modules/nav/index.js +48 -42
- package/modules/net/Client.js +13 -6
- package/modules/net/Host.js +19 -5
- package/modules/net/Net.js +2 -1
- package/modules/net/index.js +13 -8
- package/modules/net/sync.js +9 -8
- package/modules/net/transports.js +77 -27
- package/modules/particles/Particles.js +58 -28
- package/modules/particles/contract.js +17 -0
- package/modules/particles/effect.js +54 -11
- package/modules/photo/Photo.js +56 -38
- package/modules/rigid/RigidBodies.js +194 -32
- package/modules/rigid/Vehicles.js +18 -15
- package/modules/rigid/sync.js +62 -17
- package/modules/settings/Settings.js +60 -55
- package/modules/settings/apply.js +38 -3
- package/modules/settings/sections.js +5 -3
- package/modules/settings/strings.js +3 -2
- package/modules/settings/view.js +5 -3
- package/modules/sky/Sky.js +132 -205
- package/modules/sky/clouds.js +37 -23
- package/modules/sky/dome.js +100 -0
- package/modules/sky/domeLayers.js +30 -15
- package/modules/sky/environment.js +45 -99
- package/modules/sky/fog.js +9 -139
- package/modules/sky/frame.js +60 -56
- package/modules/sky/season.js +21 -0
- package/modules/sky/shader.js +63 -35
- package/modules/sky/skyModule.js +52 -37
- package/modules/sky/tint.js +59 -0
- package/modules/terrain/ChunkManager.js +194 -110
- package/modules/terrain/chunkJob.js +1 -1
- package/modules/terrain/chunkQueries.js +6 -4
- package/modules/terrain/draw.js +154 -0
- package/modules/terrain/index.js +87 -54
- package/modules/terrain/terrainDefaults.js +2 -2
- package/modules/terrain/terrainEdit.js +18 -20
- package/modules/terrain/terrainGen.js +6 -4
- package/modules/terrain/terrainIsland.js +5 -4
- package/modules/terrain/terrainShape.js +12 -12
- package/modules/terrain/terrainShapeMain.js +8 -7
- package/modules/terrain/terrainTexture.js +21 -19
- package/modules/timeline/Timeline.js +95 -43
- package/modules/timeline/Tween.js +18 -10
- package/modules/timeline/letterbox.js +2 -2
- package/modules/timeline/shots.js +5 -1
- package/modules/triggers/index.js +111 -76
- package/modules/vegetation/Vegetation.js +229 -112
- package/modules/vegetation/densityMap.js +62 -0
- package/modules/vegetation/draw.js +191 -0
- package/modules/vegetation/groundChunks.js +52 -58
- package/modules/vegetation/index.js +32 -11
- package/modules/vegetation/modelGeometry.js +3 -2
- package/modules/vegetation/nearFade.js +8 -2
- package/modules/water/Water.js +157 -72
- package/modules/water/surface.js +86 -47
- package/modules/weather/Weather.js +96 -103
- package/modules/weather/particles.js +37 -14
- package/modules/weather/shaders.js +1 -1
- package/modules/weather/strings.js +2 -2
- package/modules/weather/weatherDraw.js +141 -0
- package/modules/world/World.js +26 -9
- package/modules/world/explain.js +1 -1
- package/modules/world/index.js +18 -4
- package/modules/world/persist.js +62 -2
- package/modules/world/pos.js +3 -2
- package/modules/world/records.js +8 -6
- package/modules/world/sim.js +60 -20
- package/modules/world/simFar.js +4 -3
- package/modules/world/state.js +3 -2
- package/modules/world/thingSource.js +1 -1
- package/modules/world/views.js +8 -6
- package/package.json +8 -3
- package/presets.js +43 -29
- package/render/PostFX.js +318 -135
- package/render/backend.js +486 -0
- package/render/cascades.js +195 -0
- package/render/cascades.tsl.js +96 -0
- package/render/daylight.js +39 -0
- package/render/detail.js +122 -41
- package/render/effects.js +155 -48
- package/render/engineUniforms.js +10 -1
- package/render/fuse.js +5 -4
- package/render/fx/cel.tsl.js +42 -0
- package/render/fx/cloudShadow.tsl.js +16 -0
- package/render/fx/detail.tsl.js +66 -0
- package/render/fx/index.js +28 -0
- package/render/fx/lighting.tsl.js +28 -0
- package/render/fx/nodeErrors.tsl.js +68 -0
- package/render/fx/noise.tsl.js +12 -0
- package/render/fx/objectOrigin.tsl.js +42 -0
- package/render/fx/particles.tsl.js +139 -0
- package/render/fx/patchNodes.tsl.js +403 -0
- package/render/fx/postWebGPU.tsl.js +181 -0
- package/render/fx/season.tsl.js +13 -0
- package/render/fx/sky.tsl.js +187 -0
- package/render/fx/uniforms.tsl.js +67 -0
- package/render/fx/water.tsl.js +53 -0
- package/render/fx/wind.tsl.js +22 -0
- package/render/fxNumbers.js +36 -0
- package/render/hotGlsl.js +24 -0
- package/render/layers.js +14 -0
- package/render/lighting.js +71 -18
- package/render/lightmap.js +142 -0
- package/render/lights.js +47 -6
- package/render/materials.js +55 -152
- package/render/modeling.js +73 -0
- package/render/neutral.js +23 -0
- package/render/occlusion.js +184 -0
- package/render/ownFrame.js +76 -0
- package/render/patches.js +317 -35
- package/render/postWebGL.js +205 -0
- package/render/probes.js +288 -0
- package/render/shaderErrors.js +154 -0
- package/render/slots.js +165 -0
- package/render/terms.js +163 -0
- package/render/uniformDecl.js +4 -3
- package/types/common.d.ts +2 -2
- package/types/core.d.ts +757 -346
- package/types/gameplay.d.ts +539 -330
- package/types/generated/core/Audio.d.ts +26 -42
- package/types/generated/core/Batching.d.ts +6 -2
- package/types/generated/core/Budget.d.ts +2 -8
- package/types/generated/core/DebugOverrides.d.ts +58 -9
- package/types/generated/core/I18n.d.ts +2 -2
- package/types/generated/core/Input.d.ts +110 -32
- package/types/generated/core/Ops.d.ts +31 -0
- package/types/generated/core/Random.d.ts +2 -2
- package/types/generated/core/Saves.d.ts +10 -2
- package/types/generated/core/Stream.d.ts +1 -1
- package/types/generated/core/Threads.d.ts +4 -1
- package/types/generated/core/Views.d.ts +0 -1
- package/types/generated/core/audioVoice.d.ts +17 -9
- package/types/generated/core/control.d.ts +55 -0
- package/types/generated/core/page.d.ts +9 -2
- package/types/generated/core/session.d.ts +2 -1
- package/types/generated/modules/ambience/Ambience.d.ts +0 -9
- package/types/generated/modules/calendar/Calendar.d.ts +84 -0
- package/types/generated/modules/calendar/index.d.ts +10 -0
- package/types/generated/modules/debug/DebugPanel.d.ts +2 -2
- package/types/generated/modules/grid/Grid.d.ts +61 -25
- package/types/generated/modules/grid/Layer.d.ts +13 -5
- package/types/generated/modules/grid/index.d.ts +56 -20
- package/types/generated/modules/music/Music.d.ts +5 -3
- package/types/generated/modules/net/index.d.ts +3 -3
- package/types/generated/modules/net/transports.d.ts +12 -3
- package/types/generated/modules/particles/Particles.d.ts +13 -6
- package/types/generated/modules/sky/Sky.d.ts +85 -110
- package/types/generated/modules/sky/skyModule.d.ts +1 -1
- package/types/generated/modules/timeline/Timeline.d.ts +8 -6
- package/types/generated/modules/water/Water.d.ts +50 -3
- package/types/generated/modules/weather/Weather.d.ts +44 -15
- package/types/generated/modules/weather/weatherDraw.d.ts +11 -0
- package/types/generated/modules/world/World.d.ts +20 -5
- package/types/generated/render/PostFX.d.ts +109 -29
- package/types/generated/render/lighting.d.ts +29 -0
- package/types/generated/render/materials.d.ts +31 -50
- package/types/generated/render/modeling.d.ts +30 -0
- package/types/generated/render/occlusion.d.ts +13 -0
- package/types/generated/render/patches.d.ts +16 -2
- package/types/generated/ui/UI.d.ts +6 -3
- package/types/nav.d.ts +6 -0
- package/types/physics.d.ts +140 -45
- package/types/tsl.d.ts +94 -0
- package/types/world.d.ts +489 -122
- package/ui/Focus.js +35 -14
- package/ui/LoadingScreen.js +3 -3
- package/ui/Parts.js +35 -15
- package/ui/UI.js +27 -17
- package/ui/cursor.js +115 -0
- package/ui/defaults.js +15 -11
- package/ui/theme.js +36 -27
- package/ui/touchPart.js +2 -2
- package/core/claims.js +0 -51
- package/core/look/coreParts.js +0 -112
- package/core/look/layers.js +0 -136
- package/core/look/part.js +0 -173
- package/core/look/report.js +0 -79
- package/core/look/write.js +0 -86
- package/core/renamed.js +0 -22
- package/core/styleParts.js +0 -98
- package/docs/api/entities.md +0 -61
- package/docs/api/style.md +0 -134
- package/docs/recipes/09-style-pack.md +0 -169
- package/docs/recipes/35-style-from-reference.md +0 -84
- package/docs/recipes/36-sky-ground-plant-looks.md +0 -118
- package/docs/recipes/38-environment-look-hooks.md +0 -88
- package/docs/recipes/41-thing-registry.md +0 -93
- package/docs/recipes/43-shader-patches-surface-looks.md +0 -135
- package/docs/recipes/44-water-weather-look-hooks.md +0 -135
- package/docs/recipes/48-make-material.md +0 -70
- package/docs/recipes/52-look.md +0 -146
- package/examples/action/look.js +0 -5
- package/examples/sheepisland/style/index.js +0 -79
- package/examples/styletest/look.js +0 -11
- package/examples/styletest/overrides.js +0 -33
- package/examples/styletest/panel.js +0 -135
- package/modules/entities/Entities.js +0 -243
- package/modules/entities/build.js +0 -25
- package/modules/entities/index.js +0 -4
- package/modules/sky/calendar.js +0 -80
- package/modules/sky/cascades.js +0 -125
- package/modules/sky/layers.js +0 -69
- package/modules/sky/look.js +0 -51
- package/modules/sky/lookFog.js +0 -80
- package/modules/sky/lookShadow.js +0 -50
- package/modules/sky/lookSky.js +0 -108
- package/modules/sky/shadow.js +0 -74
- package/modules/terrain/chunkLevels.js +0 -40
- package/modules/terrain/lookGround.js +0 -61
- package/modules/terrain/mountTerrain.js +0 -32
- package/modules/vegetation/lookWind.js +0 -37
- package/modules/water/look.js +0 -80
- package/modules/weather/look.js +0 -75
- package/modules/world/cells.js +0 -42
- package/render/lookSurface.js +0 -161
- package/render/postLook.js +0 -86
- package/render/surface.js +0 -69
- package/types/style.d.ts +0 -643
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// Post chain on WebGLRenderer (EffectComposer). PostFX.js holds the effect list and calls these with itself as `post`.
|
|
2
|
+
//
|
|
3
|
+
// Why EffectComposer and not three 0.187's renderer.setEffects(): setEffects fixes MSAA when the renderer is created (no
|
|
4
|
+
// runtime switch for the settings menu) and always tone-maps last, so there is no place for sRGB-space effects (LUT, palette
|
|
5
|
+
// quantising, paper grain). The engine owns the composer; post.object() / targets() hand it out for reading.
|
|
6
|
+
//
|
|
7
|
+
// MSAA: with no effect on, the canvas's own (the engine makes the context with the default antialias), and the scene renders straight to the
|
|
8
|
+
// canvas with nothing allocated (direct: Color, clear-color and sRGB texture backgrounds are drawn tone-mapped there too, as the composer does).
|
|
9
|
+
// With an effect on, or antialias switched away from the canvas's at runtime (a context's MSAA is fixed when it is made), it lives in the
|
|
10
|
+
// composer's render targets, so it survives any effect and can be switched at runtime.
|
|
11
|
+
// Why the state stays on the PostFX object (post.composer, post._fused ...): tests and post.object() read it there; splitting the file changed no behavior.
|
|
12
|
+
import * as THREE from 'three';
|
|
13
|
+
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
|
|
14
|
+
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
|
|
15
|
+
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
|
|
16
|
+
import { FusedPass, groupFuse, checkDepthOrder } from './fuse.js';
|
|
17
|
+
import { codedError } from '../core/errorHints.js';
|
|
18
|
+
import { holdShadowRedraw } from './backend.js';
|
|
19
|
+
import { notePatch, patchOn } from '../core/threePatches.js';
|
|
20
|
+
|
|
21
|
+
// One FusedPass per group of neighboring fuse effects, kept while the same effects stay grouped (a changed group recompiles once)
|
|
22
|
+
function fuseEntry(post, group, used) {
|
|
23
|
+
const key = group.map(e => e.name).join('+'), old = post._fused.get(key);
|
|
24
|
+
let pass = old && old.items.every((e, i) => e === group[i]) ? old : null;
|
|
25
|
+
if (!pass) { old?.dispose(); pass = new FusedPass(group); post._fused.set(key, pass); }
|
|
26
|
+
used.add(key);
|
|
27
|
+
return { name: `fused(${key})`, _impl: pass };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Shadow maps once a frame. three redraws every shadow map in each renderer.render() while shadowMap.autoUpdate is on, and effects that render the
|
|
31
|
+
// scene again for their own buffers (ao's normals, outline's depth) did so a second time: measured 2 redraws a frame with ao on, 5-20 % frame
|
|
32
|
+
// time (tests/specs/shadow-passes.mjs). So the pass that draws the scene (index 0 of the chain) turns redraws off once it has drawn, and
|
|
33
|
+
// render() puts the switch back after the whole chain. Why after the scene pass and not before the chain: that pass is the one whose
|
|
34
|
+
// shadows the picture shows, and it also uses up a requested redraw (needsUpdate, sky's shadow.updateEvery), so "every N frames" still works.
|
|
35
|
+
// Why wrapping the pass's render and not an extra marker pass: the chain's pass list is what tests and post.object() show.
|
|
36
|
+
const HOLDS = new WeakSet();
|
|
37
|
+
notePatch('post-shadow-hold', "render of the first pass of the WebGL post chain, which may be the game's own Pass: wrapped so shadow maps redraw once a frame (render/postWebGL.js)", () => {},
|
|
38
|
+
{ scope: 'post chain', lost: 'effects that draw the scene again (ao, outline) redraw every shadow map a second time each frame' });
|
|
39
|
+
function holdAfterScene(post, pass) {
|
|
40
|
+
if (!pass || HOLDS.has(pass)) return;
|
|
41
|
+
HOLDS.add(pass);
|
|
42
|
+
const render = pass.render;
|
|
43
|
+
pass.render = function (...a) {
|
|
44
|
+
const out = render.apply(this, a);
|
|
45
|
+
if (post._shadowRelease === null && patchOn('post-shadow-hold')) post._shadowRelease = holdShadowRedraw(post.engine.renderer, post.engine.scene);
|
|
46
|
+
return out;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
// The composer follows the renderer's size and pixel ratio, read every frame (four number compares). Why here and not only in the engine's
|
|
50
|
+
// resize: EffectComposer does not follow the renderer by itself, and a game calling renderer.setSize / setPixelRatio itself (three's usual
|
|
51
|
+
// window.onresize handler, or a sharper picture) left the chain drawing at the old size, stretched or blurred, on WebGL only (WebGPU's
|
|
52
|
+
// pass() nodes follow the drawing buffer on their own). The renderer is the one truth; the engine's resize and photo mode write only it.
|
|
53
|
+
const _css = new THREE.Vector2();
|
|
54
|
+
function follow(post) {
|
|
55
|
+
const c = post.composer, r = post.engine.renderer, s = r.getSize(_css), pr = r.getPixelRatio();
|
|
56
|
+
if (c._pixelRatio !== pr) c.setPixelRatio(pr);
|
|
57
|
+
if (c._width !== s.x || c._height !== s.y) c.setSize(s.x, s.y);
|
|
58
|
+
}
|
|
59
|
+
/** composer.render with the shadow switch put back afterwards, even when a pass throws */
|
|
60
|
+
function composerRender(post, dt) {
|
|
61
|
+
post._shadowRelease = null;
|
|
62
|
+
try { post.composer.render(dt); } finally { post._shadowRelease?.(); post._shadowRelease = undefined; }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The chain into a render target. The composer's last pass draws to the canvas whenever renderToScreen is on (three's passes call
|
|
67
|
+
* setRenderTarget(null) themselves), so the chain runs off-screen and its result, left in readBuffer (a swapping pass swapped it there, a
|
|
68
|
+
* non-swapping one wrote it there), is copied into target with the composer's own copy pass: the same values the canvas would get.
|
|
69
|
+
*/
|
|
70
|
+
function toTarget(post, dt, target) {
|
|
71
|
+
const c = post.composer, r = post.engine.renderer;
|
|
72
|
+
c.renderToScreen = false;
|
|
73
|
+
try { webglPost.render(post, dt); } finally { c.renderToScreen = true; }
|
|
74
|
+
const prev = r.getRenderTarget();
|
|
75
|
+
c.copyPass.renderToScreen = false;
|
|
76
|
+
try { c.copyPass.render(r, target, c.readBuffer); } finally { r.setRenderTarget(prev); }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// The meshes three draws a texture background with (WebGLBackground's plane and box), by their material names
|
|
80
|
+
const BG_MATERIALS = ['BackgroundMaterial', 'BackgroundCubeMaterial'];
|
|
81
|
+
notePatch('post-background-tone', "onBeforeRender of three's own background mesh (the one WebGLBackground keeps): tone mapping on while the engine draws straight to the canvas (render/postWebGL.js)", () => {},
|
|
82
|
+
{ scope: 'engine renderer', lost: 'with no effect on, a texture background is not tone-mapped, so it changes when the first effect turns on' });
|
|
83
|
+
/**
|
|
84
|
+
* draw() with three's background mesh tone-mapped. three resets material.toneMapped of that mesh in every render (before drawing it) and keeps
|
|
85
|
+
* the mesh to itself, so it is found once in the render list (the background is unshifted to opaque[0]) and given an onBeforeRender that turns
|
|
86
|
+
* tone mapping on while post._toneBg is set: three picks the program after onBeforeRender, so the switch costs one program, not one a frame.
|
|
87
|
+
* The first draw that finds the mesh is drawn again so even the first frame matches. Only here: the composer renders into a target (never
|
|
88
|
+
* tone-mapped there) and a game's own renderer.render keeps three's rule.
|
|
89
|
+
*/
|
|
90
|
+
function toneBackground(post, draw) {
|
|
91
|
+
const r = post.engine.renderer;
|
|
92
|
+
post._toneBg = true;
|
|
93
|
+
try {
|
|
94
|
+
draw();
|
|
95
|
+
const m = r.renderLists.get(post.engine.scene, 0).opaque[0]?.object;
|
|
96
|
+
if (!m || !BG_MATERIALS.includes(m.material?.name) || post._bgMeshes.has(m)) return;
|
|
97
|
+
post._bgMeshes.add(m);
|
|
98
|
+
const before = m.onBeforeRender;
|
|
99
|
+
m.onBeforeRender = function (...a) { before.apply(this, a); if (post._toneBg && patchOn('post-background-tone')) this.material.toneMapped = true; };
|
|
100
|
+
draw();
|
|
101
|
+
} finally { post._toneBg = false; }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const webglPost = {
|
|
105
|
+
/** @param {any} post */
|
|
106
|
+
init(post) {
|
|
107
|
+
/** @internal */ post._fused = /** @type {Map<string, FusedPass>} */ (new Map()); post._scenePass = null; post._output = null;
|
|
108
|
+
/** @internal three's background meshes already hooked (toneBackground) */ post._bgMeshes = new WeakSet(); post._toneBg = false;
|
|
109
|
+
},
|
|
110
|
+
object: post => post.composer,
|
|
111
|
+
targets(post) {
|
|
112
|
+
const c = post.composer;
|
|
113
|
+
// a fresh frozen object each call: reassigning a field here cannot swap the composer's own buffers
|
|
114
|
+
return c ? Object.freeze({ renderTarget1: c.renderTarget1, renderTarget2: c.renderTarget2, readBuffer: c.readBuffer, writeBuffer: c.writeBuffer }) : null;
|
|
115
|
+
},
|
|
116
|
+
/** a fuse effect's params changed: its fused pass re-reads the values (uniforms, no recompile) */
|
|
117
|
+
refresh(post) { for (const p of post._fused.values()) p.refresh(); },
|
|
118
|
+
samples: post => (post.active ? post.composer?.renderTarget1.samples ?? (post._aa ? 4 : 0) : post._ownAA ? 4 : 0),
|
|
119
|
+
setAntialias(post, on) {
|
|
120
|
+
if (post.composer) for (const rt of [post.composer.renderTarget1, post.composer.renderTarget2]) { rt.samples = on ? 4 : 0; rt.dispose(); }
|
|
121
|
+
},
|
|
122
|
+
// Rebuild the pass chain from the list: scene (or the default RenderPass) -> hdr -> tone (or the default OutputPass) -> ldr -> final.
|
|
123
|
+
// Only enabled effects are in the chain; an implementation is built the first time its effect is on and kept after.
|
|
124
|
+
sync(post) {
|
|
125
|
+
const { renderer, scene, camera } = post.engine, size = post._size();
|
|
126
|
+
if (!post.composer) {
|
|
127
|
+
const rt = new THREE.WebGLRenderTarget(size.x, size.y, { type: THREE.HalfFloatType, samples: post._aa ? 4 : 0 });
|
|
128
|
+
post.composer = new EffectComposer(renderer, rt);
|
|
129
|
+
// EffectComposer takes a given target's size (device pixels) as its CSS size; reset it so setPixelRatio does not scale twice
|
|
130
|
+
const css = renderer.getSize(new THREE.Vector2());
|
|
131
|
+
post.composer.setSize(css.x, css.y);
|
|
132
|
+
post._scenePass = new RenderPass(scene, camera);
|
|
133
|
+
const drawScene = post._scenePass.render.bind(post._scenePass);
|
|
134
|
+
post._scenePass.render = (...a) => { drawScene(...a); post.engine.occlusion?._sceneDrawn(); };
|
|
135
|
+
post._output = new OutputPass();
|
|
136
|
+
}
|
|
137
|
+
const c = post.composer, on = post._sorted().filter(e => e.enabled);
|
|
138
|
+
for (const e of on) if (!e._impl && !e._def.fuse) {
|
|
139
|
+
if (typeof e._def.create !== 'function') throw codedError('post-effect', `post effect '${e.name}' has only node() (a TSL node, WebGPURenderer only) and this game chose WebGLRenderer: use createEngine({ renderer: 'webgpu' }), or give it create: (ctx) => pass (a three.js postprocessing Pass)`);
|
|
140
|
+
e._impl = e._def.create({ engine: post.engine, scene, camera, width: size.x, height: size.y, params: e.params });
|
|
141
|
+
if (!e._impl || typeof e._impl.render !== 'function') { e._impl = null; throw codedError('post-effect', `post effect '${e.name}': create() must return a pass with render() (a three.js postprocessing Pass)`); }
|
|
142
|
+
}
|
|
143
|
+
const used = new Set();
|
|
144
|
+
const slot = s => groupFuse(on.filter(e => e.slot === s)).map(g => Array.isArray(g) ? fuseEntry(post, g, used) : g);
|
|
145
|
+
const named = [slot('scene')[0] ?? { name: 'scene render', _impl: post._scenePass }, ...slot('hdr'),
|
|
146
|
+
slot('tone')[0] ?? { name: 'output (tone mapping)', _impl: post._output }, ...slot('ldr'), ...slot('final')];
|
|
147
|
+
for (const [k, p] of post._fused) if (!used.has(k)) { p.dispose(); post._fused.delete(k); }
|
|
148
|
+
checkDepthOrder(named);
|
|
149
|
+
// Effects reading depth (outline) need a depth texture on both ping-pong targets; added once, kept
|
|
150
|
+
if (on.some(e => e._def.needsDepth)) for (const rt of [c.renderTarget1, c.renderTarget2]) if (!rt.depthTexture) { rt.depthTexture = new THREE.DepthTexture(size.x, size.y); rt.dispose(); }
|
|
151
|
+
c.passes = named.map(e => e._impl);
|
|
152
|
+
holdAfterScene(post, c.passes[0]);
|
|
153
|
+
for (const p of c.passes) p.setSize(size.x, size.y);
|
|
154
|
+
},
|
|
155
|
+
dispose(post) {
|
|
156
|
+
for (const p of post._fused.values()) p.dispose();
|
|
157
|
+
post._fused.clear();
|
|
158
|
+
post._bgTex?.dispose(); post._bgTex = null;
|
|
159
|
+
if (!post.composer) return;
|
|
160
|
+
post._scenePass.dispose(); post._output.dispose();
|
|
161
|
+
post.composer.dispose(); post.composer = null;
|
|
162
|
+
},
|
|
163
|
+
sceneTarget(post) {
|
|
164
|
+
if (!post.composer) post._sync();
|
|
165
|
+
return post.composer?.renderTarget1 ?? null; // RenderPass draws into readBuffer, which is renderTarget1 or its twin (same format: same program key)
|
|
166
|
+
},
|
|
167
|
+
render(post, dt, target = null) {
|
|
168
|
+
if (!post.composer) post._sync();
|
|
169
|
+
follow(post);
|
|
170
|
+
if (target) { toTarget(post, dt, target); return; }
|
|
171
|
+
// no background: RenderPass clears the half-float target with whatever clear value three last set, which after the previous frame's
|
|
172
|
+
// OutputPass is the clear color converted for the canvas (sRGB), not linear, so OutputPass tone-maps the wrong value. The clear color as a
|
|
173
|
+
// Color background for this render makes three set the linear value and clear with it (the same pixel a Color background gives)
|
|
174
|
+
const s = post.engine.scene, c = s.background == null && post._clearColor();
|
|
175
|
+
if (!c) { composerRender(post, dt); return; }
|
|
176
|
+
s.background = c;
|
|
177
|
+
try { composerRender(post, dt); } finally { s.background = null; }
|
|
178
|
+
},
|
|
179
|
+
/**
|
|
180
|
+
* The straight-to-canvas render. A THREE.Color scene.background, and with no background the renderer's clear color, is a clear,
|
|
181
|
+
* and three never tone-maps a clear, while the composer path tone-maps everything in its OutputPass: the sky color shifted whenever an effect
|
|
182
|
+
* or MSAA switched on. Here such a background is drawn for this one render as a 1x1 linear texture, which three draws as a tone-mapped quad,
|
|
183
|
+
* so both paths show the same pixel (render() fixes the composer side of a missing background). Not for a see-through clear (clear alpha < 1: the quad would make the canvas opaque) or when the game
|
|
184
|
+
* clears itself (autoClear off). A texture background: an sRGB one is drawn untone-mapped on the canvas by three's own rule (WebGLBackground
|
|
185
|
+
* sets toneMapped from the texture's color space every frame) and tone-mapped by OutputPass and by WebGPURenderer, so a skybox photo changed
|
|
186
|
+
* brightness when MSAA or an effect switched; toneBackground turns tone mapping on for it here. A linear (HDR) one agrees as it is.
|
|
187
|
+
* Why not always render through the composer: that costs a half-float target and a fullscreen pass on every page that has no effects.
|
|
188
|
+
*/
|
|
189
|
+
direct(post, target = null) {
|
|
190
|
+
const { renderer: r, scene: s, camera } = post.engine, bg = s.background;
|
|
191
|
+
// a render target gets the scene as three draws into one (linear, no tone mapping); post.active is what gives the canvas's pixels
|
|
192
|
+
if (target) { const prev = r.getRenderTarget(); r.setRenderTarget(target); try { r.render(s, camera); } finally { r.setRenderTarget(prev); } return; }
|
|
193
|
+
if (r.toneMapping !== THREE.NoToneMapping && bg?.isTexture && THREE.ColorManagement.getTransfer(bg.colorSpace) === THREE.SRGBTransfer) {
|
|
194
|
+
toneBackground(post, () => r.render(s, camera)); return;
|
|
195
|
+
}
|
|
196
|
+
const col = bg?.isColor ? bg : bg == null ? post._clearColor() : null;
|
|
197
|
+
if (!col || r.toneMapping === THREE.NoToneMapping) { r.render(s, camera); return; }
|
|
198
|
+
const t = post._bgTex ??= Object.assign(new THREE.DataTexture(new Float32Array(4), 1, 1, THREE.RGBAFormat, THREE.FloatType), { colorSpace: THREE.LinearSRGBColorSpace });
|
|
199
|
+
const d = t.image.data;
|
|
200
|
+
if (d[0] !== col.r || d[1] !== col.g || d[2] !== col.b || d[3] !== 1) { d.set([col.r, col.g, col.b, 1]); t.needsUpdate = true; }
|
|
201
|
+
const bi = s.backgroundIntensity; // a clear ignores backgroundIntensity; the texture would not
|
|
202
|
+
s.background = t; s.backgroundIntensity = 1;
|
|
203
|
+
try { r.render(s, camera); } finally { s.background = bg; s.backgroundIntensity = bi; }
|
|
204
|
+
},
|
|
205
|
+
};
|
package/render/probes.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
// Baked light probes. A grid of SH probes from the scene (bake / load / JSON) that every lit pixel samples for itself.
|
|
2
|
+
// How the light reaches materials: three's probe grid (LightProbeGridWebGL on WebGLRenderer, LightProbeGrid on WebGPURenderer) is a light in the
|
|
3
|
+
// scene whose 3D texture both renderers' lighting already reads, so nothing is patched and nothing runs per frame. handle.grid is that plain object.
|
|
4
|
+
// Why the engine keeps its own copy of the grid (handle.sh, half precision): three bakes the WebGL grid into a 32-bit float target. Measured on a
|
|
5
|
+
// fast GPU that costs +0.6 ms a frame against +0.03 ms in half precision, and float linear filtering is missing on many phones (the light goes
|
|
6
|
+
// black there). So the bake target is read back and thrown away, and what draws is a half-float Data3DTexture built from handle.sh, for a bake and a
|
|
7
|
+
// file alike. One path also means a shipped file looks like the bake it came from, on either renderer. It is three's atlas layout rebuilt by hand:
|
|
8
|
+
// tests/specs/probes.mjs compares the two pictures, so a three.js update that changes the layout fails there.
|
|
9
|
+
// Probes sit ON the bounds and the grid's box reaches half a step past them: three puts probes at cell centers, and it tests a pixel half a cell along
|
|
10
|
+
// its normal, so a roof exactly at bounds.max would fall outside a box that stops there (the black band on the roofs in the trial).
|
|
11
|
+
import { engineOwned } from '../core/things.js';
|
|
12
|
+
import * as THREE from 'three';
|
|
13
|
+
import { LightProbeGridWebGL } from 'three/addons/lighting/LightProbeGridWebGL.js';
|
|
14
|
+
import { readParams } from '../core/params.js';
|
|
15
|
+
import { codedError, coded } from '../core/errorHints.js';
|
|
16
|
+
import { warnOnce } from '../core/validate.js';
|
|
17
|
+
import { vec3 } from '../core/math.js';
|
|
18
|
+
import { isWebGPU, webgpuPart, freshFrame, readLayer, onRendererSwap } from './backend.js';
|
|
19
|
+
|
|
20
|
+
const FORMAT = 'threedog-probes', VERSION = 2, MAX_PROBES = 4096, SH = 27;
|
|
21
|
+
const CHUNK = 8; // probes baked per turn of the event loop, so the page keeps drawing during a long bake
|
|
22
|
+
const ATLAS = 7; // three's atlas: 7 RGBA sub-volumes along z, each with one padding slice at both ends
|
|
23
|
+
const BAKE = {
|
|
24
|
+
bounds: { type: 'object', desc: 'THREE.Box3 or { min, max } (positions) the grid spans; light reaches half a step past it, outside there is none' },
|
|
25
|
+
spacing: { type: 'number', min: 0.01, desc: 'largest distance between neighbour probes, meters' },
|
|
26
|
+
resolution: { type: 'int', default: 8, min: 4, max: 32, desc: "cube face size in px per probe (SH keeps only low frequencies; three's projection shader unrolls over every pixel, so big faces compile slowly)" },
|
|
27
|
+
include: { type: 'function', optional: true, desc: '(object) => bool: meshes that return false are hidden while baking (the player, moving props)' },
|
|
28
|
+
owner: { type: 'string', default: 'game', desc: 'who made the set (describe().render.probes)' },
|
|
29
|
+
};
|
|
30
|
+
const LOAD = { owner: BAKE.owner };
|
|
31
|
+
const STRINGS = {
|
|
32
|
+
bounds: 'engine.bakeProbes({ bounds }): bounds must be a THREE.Box3 or { min: [x, y, z], max: [x, y, z] } with max >= min on every axis; got {got}',
|
|
33
|
+
tooMany: 'engine.bakeProbes(): {n} probes ({dims}) is more than {max}; raise spacing (each probe costs 6 renders)',
|
|
34
|
+
noFloat: 'engine.bakeProbes(): this device cannot render to float textures (EXT_color_buffer_float), which three\'s WebGL bake needs. Bake on a desktop and ship the file: engine.loadProbes(json) works here',
|
|
35
|
+
data: 'engine.loadProbes(json): {why}. Pass what handle.toJSON() returned (format \'' + FORMAT + '\', version ' + VERSION + '), or bake again',
|
|
36
|
+
outside: '{n} lit object(s) reach outside the probe grid ({names}): the part outside gets no probe light and can go black. Widen bounds, or add light there. describe().render.probes lists them',
|
|
37
|
+
doubled: 'probe light is added on top of {what}, which the bake already saw, so that light counts twice. Lower it where the probes take over, or bake without it',
|
|
38
|
+
};
|
|
39
|
+
const sets = e => (e._probeSets ??= new Set());
|
|
40
|
+
|
|
41
|
+
/** Grid layout from bounds + spacing: probes on the bounds, at least 2 per axis. Why 2: three tests a pixel half a cell along its normal, so with one
|
|
42
|
+
* cell on an axis every upward surface above the middle of the box falls outside it. A flat axis gets its 2 layers a spacing apart around the bounds. */
|
|
43
|
+
function layout(bounds, spacing) {
|
|
44
|
+
const min = vec3(bounds.min), max = vec3(bounds.max);
|
|
45
|
+
if (![min.x, min.y, min.z, max.x, max.y, max.z].every(Number.isFinite) || max.x < min.x || max.y < min.y || max.z < min.z) {
|
|
46
|
+
throw codedError('probes-option', STRINGS.bounds.replace('{got}', JSON.stringify({ min, max })));
|
|
47
|
+
}
|
|
48
|
+
const origin = [], step = [], counts = [];
|
|
49
|
+
for (const k of ['x', 'y', 'z']) {
|
|
50
|
+
const size = max[k] - min[k], n = size < 1e-6 ? 2 : Math.ceil(size / spacing - 1e-6) + 1;
|
|
51
|
+
counts.push(n); step.push(size < 1e-6 ? spacing : size / (n - 1)); origin.push(size < 1e-6 ? min[k] - spacing / 2 : min[k]);
|
|
52
|
+
}
|
|
53
|
+
return { origin, step, counts };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The world box of a grid: the probes plus half a step on every side (what three calls the volume) */
|
|
57
|
+
function boxOf({ origin, step, counts }) {
|
|
58
|
+
const lo = i => origin[i] - step[i] / 2, hi = i => origin[i] + (counts[i] - 0.5) * step[i];
|
|
59
|
+
return new THREE.Box3(new THREE.Vector3(lo(0), lo(1), lo(2)), new THREE.Vector3(hi(0), hi(1), hi(2)));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** three's grid for this renderer (it has one class per renderer), placed on the data; not in the scene yet */
|
|
63
|
+
function makeGrid(r, d) {
|
|
64
|
+
const Grid = isWebGPU(r) ? webgpuPart('classes', 'a light probe grid').LightProbeGrid : LightProbeGridWebGL;
|
|
65
|
+
const box = boxOf(d), size = box.getSize(new THREE.Vector3());
|
|
66
|
+
const grid = new Grid(size.x, size.y, size.z, ...d.counts);
|
|
67
|
+
box.getCenter(grid.position); grid.updateBoundingBox();
|
|
68
|
+
return grid;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The half-float atlas three's lighting reads, from sh (probe at (z * ny + y) * nx + x, 27 numbers each, in three's packed order) */
|
|
72
|
+
function atlasTexture(sh, [nx, ny, nz]) {
|
|
73
|
+
const padded = nz + 2, row = nx * ny, data = new Uint16Array(row * ATLAS * padded * 4);
|
|
74
|
+
for (let t = 0; t < ATLAS; t++) for (let slice = 0; slice < padded; slice++) {
|
|
75
|
+
const iz = Math.min(nz - 1, Math.max(0, slice - 1)); // the padding slices copy their neighbour, so filtering never reads the next sub-volume
|
|
76
|
+
for (let p = 0; p < row; p++) for (let c = 0; c < 4; c++) {
|
|
77
|
+
const k = t * 4 + c;
|
|
78
|
+
if (k < SH) data[((t * padded + slice) * row + p) * 4 + c] = THREE.DataUtils.toHalfFloat(sh[(iz * row + p) * SH + k]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const tex = new THREE.Data3DTexture(data, nx, ny, ATLAS * padded);
|
|
82
|
+
tex.format = THREE.RGBAFormat; tex.type = THREE.HalfFloatType;
|
|
83
|
+
tex.minFilter = tex.magFilter = THREE.LinearFilter; tex.needsUpdate = true;
|
|
84
|
+
return tex;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The bake target back as sh numbers, rounded to half so the bake and its file are the same light.
|
|
88
|
+
* shortcut: reads grid._renderTarget, three gives no public way to the target; a three.js update that renames it fails tests/specs/probes.mjs */
|
|
89
|
+
async function readGrid(r, grid, [nx, ny, nz]) {
|
|
90
|
+
const sh = new Float32Array(nx * ny * nz * SH), row = nx * ny, { fromHalfFloat, toHalfFloat } = THREE.DataUtils;
|
|
91
|
+
for (let t = 0; t < ATLAS; t++) for (let iz = 0; iz < nz; iz++) {
|
|
92
|
+
const px = await readLayer(r, grid._renderTarget, nx, ny, t * (nz + 2) + 1 + iz), stride = px.length / ny; // WebGPU pads its rows
|
|
93
|
+
const half = px instanceof Uint16Array;
|
|
94
|
+
for (let iy = 0; iy < ny; iy++) for (let ix = 0; ix < nx; ix++) for (let c = 0; c < 4; c++) {
|
|
95
|
+
const k = t * 4 + c, v = px[iy * stride + ix * 4 + c];
|
|
96
|
+
if (k < SH) sh[(iz * row + iy * nx + ix) * SH + k] = fromHalfFloat(half ? v : toHalfFloat(v));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return sh;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Bytes as base64: btoa takes a string, and one spread call over a big array overflows the stack */
|
|
103
|
+
function toBase64(bytes) {
|
|
104
|
+
let s = '';
|
|
105
|
+
for (let i = 0; i < bytes.length; i += 8192) s += String.fromCharCode(...bytes.subarray(i, i + 8192));
|
|
106
|
+
return btoa(s);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const LIT = m => !!(m?.isMeshStandardMaterial || m?.isMeshLambertMaterial || m?.isMeshPhongMaterial || m?.isMeshToonMaterial || (m?.isNodeMaterial && m.lights));
|
|
110
|
+
const mats = o => Array.isArray(o.material) ? o.material : [o.material];
|
|
111
|
+
|
|
112
|
+
/** Visible lit meshes no set fully holds, and everything else in the scene that adds light the bake already saw. Runs on describe() and when a set is added, never per frame */
|
|
113
|
+
function probeChecks(e) {
|
|
114
|
+
const boxes = [...sets(e)].map(s => s.box), b = new THREE.Box3(), outside = [], doubled = [];
|
|
115
|
+
let mapped = 0;
|
|
116
|
+
e.scene.traverseVisible(o => {
|
|
117
|
+
if (o.isAmbientLight || o.isHemisphereLight || o.isLightProbe) { if (o.intensity > 0) doubled.push(`${o.type} ${o.name || o.id}`); return; }
|
|
118
|
+
if (!o.isMesh || !mats(o).some(LIT)) return;
|
|
119
|
+
if (mats(o).some(m => m.lightMap)) mapped++;
|
|
120
|
+
const src = o.isInstancedMesh ? o : o.geometry;
|
|
121
|
+
if (!src.boundingBox) src.computeBoundingBox();
|
|
122
|
+
b.copy(src.boundingBox).applyMatrix4(o.matrixWorld);
|
|
123
|
+
if (!boxes.some(x => x.containsBox(b))) outside.push(o.name || `${o.type} ${o.id}`);
|
|
124
|
+
});
|
|
125
|
+
if (e.scene.environment && e.scene.environmentIntensity > 0) doubled.push('scene.environment');
|
|
126
|
+
if (mapped) doubled.push(`material.lightMap on ${mapped} mesh(es)`);
|
|
127
|
+
return { outside, doubled };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** A probe set: grid data + the three.js grid in the scene that draws it. Made by bakeProbes / loadProbes only */
|
|
131
|
+
class ProbeSet {
|
|
132
|
+
constructor(e, data, { owner, source }) {
|
|
133
|
+
Object.assign(this, { _e: e, origin: data.origin, step: data.step, counts: data.counts, sh: data.sh, owner, source });
|
|
134
|
+
this.count = this.counts[0] * this.counts[1] * this.counts[2];
|
|
135
|
+
/** bake time in ms (null for a loaded set) */ this.bakeMs = data.bakeMs ?? null;
|
|
136
|
+
/** the world box the grid lights */ this.box = boxOf(this);
|
|
137
|
+
this._tex = atlasTexture(this.sh, this.counts);
|
|
138
|
+
/** three's probe grid, a plain light in the scene: priority, visible and (on WebGPU) intensity are the game's; three's WebGL shader ignores intensity */
|
|
139
|
+
this.grid = null;
|
|
140
|
+
this._build();
|
|
141
|
+
// createEngine({ renderer: 'webgpu', fallback: 'webgl' }) changes renderer inside start(): each renderer needs its own grid class
|
|
142
|
+
this._swap = onRendererSwap(e, () => { this.grid.removeFromParent(); this._build(); });
|
|
143
|
+
sets(e).add(this);
|
|
144
|
+
}
|
|
145
|
+
_build() {
|
|
146
|
+
const grid = makeGrid(this._e.renderer, this);
|
|
147
|
+
grid.name = 'probes:' + this.owner; grid.texture = this._tex;
|
|
148
|
+
this._e.scene.add(engineOwned('probes', grid));
|
|
149
|
+
this.grid = grid;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The interpolated SH at a position (trilinear between the 8 grid probes around it; outside the grid the nearest edge). For what the grid does not
|
|
153
|
+
* light by itself: a gameplay "how dark is it here", or a LightProbe of your own.
|
|
154
|
+
* @param {any} pos any position form (Vector3, Object3D, [x, y, z], { x, y, z })
|
|
155
|
+
* @param {THREE.SphericalHarmonics3 | THREE.LightProbe} [target] written in place (a LightProbe's sh); a new SphericalHarmonics3 when omitted
|
|
156
|
+
* @returns {THREE.SphericalHarmonics3}
|
|
157
|
+
*/
|
|
158
|
+
sample(pos, target) {
|
|
159
|
+
const sh = target?.isLightProbe ? target.sh : target ?? new THREE.SphericalHarmonics3();
|
|
160
|
+
const v = vec3(pos, this._s ??= new THREE.Vector3()), [nx, ny] = this.counts, c = sh.coefficients;
|
|
161
|
+
const axis = (x, i) => { const n = this.counts[i], t = Math.min(n - 1, Math.max(0, (x - this.origin[i]) / this.step[i])), a = Math.min(n - 2, Math.floor(t)); return [a, a + 1, t - a]; };
|
|
162
|
+
const [x0, x1, fx] = axis(v.x, 0), [y0, y1, fy] = axis(v.y, 1), [z0, z1, fz] = axis(v.z, 2);
|
|
163
|
+
for (const k of c) k.set(0, 0, 0);
|
|
164
|
+
const add = (ix, iy, iz, w) => {
|
|
165
|
+
if (w === 0) return;
|
|
166
|
+
const b = ((iz * ny + iy) * nx + ix) * SH;
|
|
167
|
+
for (let j = 0; j < 9; j++) c[j].x += this.sh[b + j * 3] * w, c[j].y += this.sh[b + j * 3 + 1] * w, c[j].z += this.sh[b + j * 3 + 2] * w;
|
|
168
|
+
};
|
|
169
|
+
for (const [iz, wz] of [[z0, 1 - fz], [z1, fz]]) for (const [iy, wy] of [[y0, 1 - fy], [y1, fy]]) for (const [ix, wx] of [[x0, 1 - fx], [x1, fx]]) add(ix, iy, iz, wx * wy * wz);
|
|
170
|
+
return sh;
|
|
171
|
+
}
|
|
172
|
+
/** World position of probe i @param {number} i @param {THREE.Vector3} [out] */
|
|
173
|
+
point(i, out = new THREE.Vector3()) {
|
|
174
|
+
const [nx, ny] = this.counts, ix = i % nx, iy = Math.floor(i / nx) % ny, iz = Math.floor(i / (nx * ny));
|
|
175
|
+
return out.set(this.origin[0] + ix * this.step[0], this.origin[1] + iy * this.step[1], this.origin[2] + iz * this.step[2]);
|
|
176
|
+
}
|
|
177
|
+
/** Plain data to ship as a file and give to engine.loadProbes (half precision, base64) */
|
|
178
|
+
toJSON() {
|
|
179
|
+
const half = Uint16Array.from(this.sh, v => THREE.DataUtils.toHalfFloat(v));
|
|
180
|
+
return { format: FORMAT, version: VERSION, origin: this.origin, step: this.step, counts: this.counts, half: toBase64(new Uint8Array(half.buffer)) };
|
|
181
|
+
}
|
|
182
|
+
/** describe().render.probes row */
|
|
183
|
+
info() {
|
|
184
|
+
const { outside, doubled } = probeChecks(this._e);
|
|
185
|
+
return { count: this.count, counts: this.counts, step: this.step.map(s => Math.round(s * 100) / 100), source: this.source, owner: this.owner,
|
|
186
|
+
outside: { count: outside.length, names: outside.slice(0, 3) }, doubled, ...(this.bakeMs != null ? { bakeMs: Math.round(this.bakeMs) } : {}) };
|
|
187
|
+
}
|
|
188
|
+
/** Take the grid out of the scene and free its texture; the data stays readable (sample, toJSON). Safe to call twice */
|
|
189
|
+
remove() {
|
|
190
|
+
if (!sets(this._e).delete(this)) return;
|
|
191
|
+
this.grid.removeFromParent(); this._tex.dispose(); this._swap.remove();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The debug view: one dot per probe, colored by its average (DC) light, over the real picture */
|
|
196
|
+
function drawProbes(e, scene) {
|
|
197
|
+
const list = [...sets(e)], n = list.reduce((s, x) => s + x.count, 0);
|
|
198
|
+
if (!n) return () => {};
|
|
199
|
+
const geo = new THREE.SphereGeometry(0.15, 8, 6), mat = new THREE.MeshBasicMaterial({ toneMapped: false }); // debug overlay only
|
|
200
|
+
const dots = new THREE.InstancedMesh(geo, mat, n), m = new THREE.Matrix4(), p = new THREE.Vector3(), c = new THREE.Color();
|
|
201
|
+
let k = 0;
|
|
202
|
+
for (const s of list) for (let i = 0; i < s.count; i++, k++) {
|
|
203
|
+
dots.setMatrixAt(k, m.makeTranslation(s.point(i, p)));
|
|
204
|
+
const b = i * SH, Y00 = 0.282095; // the SH constant term times Y00 is the probe's average radiance
|
|
205
|
+
dots.setColorAt(k, c.setRGB(s.sh[b] * Y00, s.sh[b + 1] * Y00, s.sh[b + 2] * Y00));
|
|
206
|
+
}
|
|
207
|
+
dots.name = 'debug:probes'; scene.add(engineOwned('debug', dots));
|
|
208
|
+
return () => { dots.removeFromParent(); geo.dispose(); mat.dispose(); dots.dispose(); };
|
|
209
|
+
}
|
|
210
|
+
function addSet(e, data, o) {
|
|
211
|
+
e._probeView ??= e.debug?.defineView('probes', { what: 'each light probe as a dot colored by its average light', apply: scene => drawProbes(e, scene) });
|
|
212
|
+
const set = new ProbeSet(e, data, o), { outside, doubled } = probeChecks(e);
|
|
213
|
+
if (outside.length) warnOnce(coded('probes-check', STRINGS.outside.replace('{n}', String(outside.length)).replace('{names}', outside.slice(0, 3).join(', '))));
|
|
214
|
+
if (doubled.length) warnOnce(coded('probes-check', STRINGS.doubled.replace('{what}', doubled.join(', '))));
|
|
215
|
+
return set;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Check a loaded file's shape; every problem is a coded refusal, never a half-applied grid */
|
|
219
|
+
function readData(json) {
|
|
220
|
+
const bad = why => codedError('probes-data', STRINGS.data.replace('{why}', why));
|
|
221
|
+
if (!json || typeof json !== 'object') throw bad(`got ${typeof json}`);
|
|
222
|
+
if (json.format !== FORMAT) throw bad(`format is ${JSON.stringify(json.format)}`);
|
|
223
|
+
if (json.version !== VERSION) throw bad(`version ${JSON.stringify(json.version)} is not supported`);
|
|
224
|
+
const tri = (k, ok) => { const v = json[k]; if (!Array.isArray(v) || v.length !== 3 || !v.every(ok)) throw bad(`${k} must be 3 numbers, got ${JSON.stringify(v)}`); return v; };
|
|
225
|
+
// 2 per axis and a step above 0: sample() divides by the step, a 0 gave NaN light (a black picture, no error)
|
|
226
|
+
const counts = tri('counts', x => Number.isInteger(x) && x >= 2), origin = tri('origin', Number.isFinite), step = tri('step', x => Number.isFinite(x) && x > 0);
|
|
227
|
+
const n = counts[0] * counts[1] * counts[2];
|
|
228
|
+
if (n > MAX_PROBES) throw bad(`${n} probes is more than ${MAX_PROBES}`);
|
|
229
|
+
if (typeof json.half !== 'string') throw bad(`half must be a base64 string, got ${typeof json.half}`);
|
|
230
|
+
let text;
|
|
231
|
+
try { text = atob(json.half); } catch { throw bad('half is not base64'); }
|
|
232
|
+
if (text.length !== n * SH * 2) throw bad(`half must hold ${n} x ${SH} half floats, got ${text.length} bytes`);
|
|
233
|
+
const bytes = Uint8Array.from(text, c => c.charCodeAt(0));
|
|
234
|
+
const sh = Float32Array.from(new Uint16Array(bytes.buffer), THREE.DataUtils.fromHalfFloat);
|
|
235
|
+
if (!sh.every(Number.isFinite)) throw bad('half holds a value that is not a finite number');
|
|
236
|
+
return { origin, step, counts, sh };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Mixed into Engine.prototype (core/Engine.js) */
|
|
240
|
+
export const probeMethods = {
|
|
241
|
+
/**
|
|
242
|
+
* Bake a grid of light probes from the scene as it is now: three's probe grid renders a cube at every grid point and keeps spherical harmonics, and
|
|
243
|
+
* every lit pixel inside the grid then takes its own interpolated light, on WebGL and WebGPU alike. The grid adds to whatever ambient light the
|
|
244
|
+
* scene has (hemi, environment), which the bake also saw: lower those where the probes take over (a warning and describe().render.probes say so).
|
|
245
|
+
* Outside the grid there is no probe light. A load-time cost, not per frame: 6 renders per probe, a few probes per task so the page keeps drawing;
|
|
246
|
+
* ship the result with `toJSON()` + `engine.loadProbes(json)` instead of baking at every start. Adding a grid recompiles the lit materials once:
|
|
247
|
+
* bake at start-up, not mid-play.
|
|
248
|
+
* @example const probes = await engine.bakeProbes({ bounds: { min: [-20, 0, -20], max: [20, 6, 20] }, spacing: 4, include: o => !o.userData.moving });
|
|
249
|
+
* saveFile('probes.json', JSON.stringify(probes.toJSON()));
|
|
250
|
+
* @param {{ bounds: THREE.Box3 | { min: any, max: any }, spacing: number, resolution?: number, include?: (o: THREE.Object3D) => boolean, owner?: string }} opts
|
|
251
|
+
* @returns {Promise<import('../types/core').ProbeSet>}
|
|
252
|
+
*/
|
|
253
|
+
async bakeProbes(opts) {
|
|
254
|
+
const o = readParams('engine.bakeProbes()', BAKE, opts), r = this.renderer;
|
|
255
|
+
const d = layout(o.bounds, o.spacing), n = d.counts[0] * d.counts[1] * d.counts[2];
|
|
256
|
+
if (n > MAX_PROBES) throw codedError('probes-option', STRINGS.tooMany.replace('{n}', n).replace('{dims}', d.counts.join(' x ')).replace('{max}', MAX_PROBES));
|
|
257
|
+
if (!isWebGPU(r) && !r.extensions.has('EXT_color_buffer_float')) throw codedError('probes-option', STRINGS.noFloat);
|
|
258
|
+
const t0 = performance.now(); // measure
|
|
259
|
+
const grid = makeGrid(r, d);
|
|
260
|
+
// hidden while the cubes render: meshes the filter rejects, and every other probe light (an earlier set lighting the capture would count twice)
|
|
261
|
+
const hide = [];
|
|
262
|
+
this.scene.traverse(x => { if (x.visible && (x.isLightProbe || x.isLightProbeGrid || (o.include && (x.isMesh || x.isPoints || x.isLine || x.isSprite) && !o.include(x)))) hide.push(x); });
|
|
263
|
+
try {
|
|
264
|
+
for (let start = 0; start < n; start += CHUNK) {
|
|
265
|
+
for (const x of hide) x.visible = false;
|
|
266
|
+
try {
|
|
267
|
+
freshFrame(r);
|
|
268
|
+
grid.bake(r, this.scene, { cubemapSize: o.resolution, near: 0.05, far: this.camera.far, start, count: Math.min(CHUNK, n - start) });
|
|
269
|
+
} finally { for (const x of hide) x.visible = true; } // restored before the await, so normal frames never see them hidden
|
|
270
|
+
await new Promise(go => setTimeout(go));
|
|
271
|
+
}
|
|
272
|
+
d.sh = await readGrid(r, grid, d.counts);
|
|
273
|
+
} finally { grid.dispose(); }
|
|
274
|
+
d.bakeMs = performance.now() - t0; // measure
|
|
275
|
+
return addSet(this, d, { owner: o.owner, source: 'bake' });
|
|
276
|
+
},
|
|
277
|
+
/**
|
|
278
|
+
* A probe set from a file made by `handle.toJSON()` (no baking), on either renderer whichever one baked it. A wrong shape is refused with code `probes-data` and nothing is added.
|
|
279
|
+
* @example const probes = engine.loadProbes(await (await fetch('probes.json')).json());
|
|
280
|
+
* @param {object} json
|
|
281
|
+
* @param {{ owner?: string }} [opts]
|
|
282
|
+
* @returns {import('../types/core').ProbeSet}
|
|
283
|
+
*/
|
|
284
|
+
loadProbes(json, opts) {
|
|
285
|
+
const o = readParams('engine.loadProbes()', LOAD, opts);
|
|
286
|
+
return addSet(this, readData(json), { owner: o.owner, source: 'file' });
|
|
287
|
+
},
|
|
288
|
+
};
|