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,42 @@
|
|
|
1
|
+
// TSL objectOrigin(): object / instance world origin.
|
|
2
|
+
// TSL objectOrigin(): world origin of the drawn object / instance (the `ip` of GLSL threedog_wind_vertex).
|
|
3
|
+
// GLSL version: modelMatrix * instanceMatrix * vec4(0,0,0,1). three has no public instance-matrix node; the first experiment wrapped mesh.instanceMatrix
|
|
4
|
+
// in a second instanced attribute, which tied the material to one mesh. This uses referenceAttribute('instanceMatrix'), as three's own instancing does: at build time it
|
|
5
|
+
// reads the attribute of the object being drawn, each object binding its own, so several InstancedMeshes can share one material.
|
|
6
|
+
// three takes this path only while vertex buffers suffice (Instance.js canReferenceInstanceData, 8-buffer limit); otherwise it compiles per mesh,
|
|
7
|
+
// which would mean copying three's private logic, so that case throws by name. BatchedMesh's batch matrices are also private to three: same error.
|
|
8
|
+
import { Fn, vec4, mat3, modelWorldMatrix, referenceAttribute } from 'three/tsl';
|
|
9
|
+
import { codedError } from '../../core/errorHints.js';
|
|
10
|
+
|
|
11
|
+
const MAX_VERTEX_BUFFERS = 8; // three/src/nodes/accessors/Instance.js (same constant, same test)
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The instance matrix node of the object being built, or null when it is not instanced (throws for what three binds privately)
|
|
15
|
+
* @param {any} builder @returns {import('three/webgpu').Node<'mat4'> | null}
|
|
16
|
+
*/
|
|
17
|
+
function instanceMatrixOf(builder) {
|
|
18
|
+
const o = builder.object;
|
|
19
|
+
if (o?.isBatchedMesh) throw codedError('tsl-misuse', 'threedog/tsl objectOrigin(): BatchedMesh is not supported in TSL yet');
|
|
20
|
+
if (!o?.isInstancedMesh) return null;
|
|
21
|
+
const buffers = 1 + (o.instanceColor !== null ? 1 : 0) + (builder.needsPreviousData() ? 1 : 0);
|
|
22
|
+
if (builder.renderer?.isRenderer !== true || Object.keys(o.geometry.attributes).length + buffers > MAX_VERTEX_BUFFERS)
|
|
23
|
+
throw codedError('tsl-misuse', `threedog/tsl objectOrigin(): InstancedMesh '${o.name || o.uuid}' has too many vertex attributes for a shared instance matrix`);
|
|
24
|
+
// @types/three types referenceAttribute() as a bare ReferenceAttributeNode (no node type, no operators); with 'mat4' it is a mat4 node
|
|
25
|
+
return /** @type {any} */ (referenceAttribute('instanceMatrix', 'mat4'));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** World position of the object's (or instance's) origin, vec3 */
|
|
29
|
+
export const objectOrigin = Fn((_, builder) => {
|
|
30
|
+
const m = instanceMatrixOf(builder), o = vec4(0, 0, 0, 1);
|
|
31
|
+
return modelWorldMatrix.mul(m ? m.mul(o) : o).xyz;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* An object-space offset carried through the instance's rotation / scale: GLSL patches add offsets before instanceMatrix,
|
|
36
|
+
* a TSL positionNode runs after it, so wind()'s offset goes through this to land the same
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export const instanceOffset = Fn(/** @param {[import('three/webgpu').Node<'vec3'>]} a @param {any} builder */ ([v], builder) => {
|
|
40
|
+
const m = instanceMatrixOf(builder);
|
|
41
|
+
return m ? mat3(m).mul(v) : v;
|
|
42
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Node-material versions of the particle and weather shaders (modules/particles/shaders.js, modules/weather/shaders.js) for WebGPURenderer.
|
|
2
|
+
// Why a registry instead of an import: modules/particles and modules/weather are in every WebGL game's bundle and must not pull in three/tsl, so this
|
|
3
|
+
// file (loaded with threedog/tsl, i.e. only on the WebGPU route) registers itself (render/backend.js registerWebGPUPart) and those modules look it up when the
|
|
4
|
+
// renderer is a WebGPURenderer (same pattern as patchNodes.tsl.js).
|
|
5
|
+
// Why sprites, not Points: WebGPU draws point primitives 1 pixel wide whatever the size, so each particle is an instance of a quad
|
|
6
|
+
// (three's Sprite + PointsNodeMaterial, see PointsNodeMaterial.js header); sizeNode then sets its on-screen size.
|
|
7
|
+
// Same numbers as GLSL: the modules keep their { value } uniform objects (game and engine code writes .value on either renderer) and the nodes read them
|
|
8
|
+
// back each render; sizes are device pixels like gl_PointSize (sizeNode is in CSS pixels, so it is divided by the pixel ratio), sprite uv is
|
|
9
|
+
// flipped to gl_PointCoord's y-down so textures and rotation land the same way.
|
|
10
|
+
// Fog: the node material's own fog (mixed before the output transform). The GLSL mixes fog after its tone-mapping / color-space chunks, which
|
|
11
|
+
// only matters straight on the canvas: in a linear target (every frame with post on) those chunks do nothing and both match (fog parity in
|
|
12
|
+
// tests/specs/fx-particles.mjs, mean within 0.01%). On the canvas WebGPU differs anyway for every material: it blends and fogs in its linear
|
|
13
|
+
// frame buffer, WebGL on the encoded canvas (9-15% on the test puff, as much without fog), and three's own WebGL materials fog in that same
|
|
14
|
+
// late order, so reordering here would only make particles disagree with the fogged meshes around them on WebGPU.
|
|
15
|
+
import * as TSL from 'three/tsl';
|
|
16
|
+
import { Sprite, PointsNodeMaterial, LineBasicNodeMaterial, InstancedBufferAttribute, DataTexture, SRGBColorSpace,
|
|
17
|
+
NormalBlending, AdditiveBlending } from 'three/webgpu';
|
|
18
|
+
import { valueNode } from './uniforms.tsl.js';
|
|
19
|
+
import { registerWebGPUPart } from '../backend.js';
|
|
20
|
+
|
|
21
|
+
const { Fn, texture, instancedDynamicBufferAttribute, attribute, varying, positionGeometry, modelViewMatrix, screenDPR, uv,
|
|
22
|
+
float, vec2, vec3, vec4, select, min, max, cos, sin, mod, smoothstep, step, dot, length, If, Discard } = TSL;
|
|
23
|
+
|
|
24
|
+
/** @template {string} T @typedef {import('three/webgpu').Node<T>} N */
|
|
25
|
+
/** The particle uniforms as nodes @typedef {{ uScale: N<'float'>, uMaxPx: N<'float'>, uHasMap: N<'float'> }} ParticleNodes */
|
|
26
|
+
/**
|
|
27
|
+
* The weather uniforms as nodes (modules/weather/particles.js; streaks have uWindDir / uLen, sprites uPx / uMaxPx / uSway / uSwayAmp / uUseMap).
|
|
28
|
+
* Colors are typed vec3 (what they are in the shader): @types/three's 'color' node type lacks the vec3 operators.
|
|
29
|
+
* @typedef {{ uCam: N<'vec3'>, uSize: N<'float'>, uFallOff: N<'float'>, uOpacity: N<'float'>, uColor: N<'vec3'>, uWindDir?: N<'vec2'>,
|
|
30
|
+
* uLen?: N<'float'>, uPx?: N<'float'>, uMaxPx?: N<'float'>, uSway?: N<'float'>, uSwayAmp?: N<'float'>, uUseMap?: N<'float'> }} WeatherNodes
|
|
31
|
+
*/
|
|
32
|
+
/** A per-instance attribute node of the given node type @type {<T extends string>(a: any, type: T) => N<T>} */
|
|
33
|
+
const at = (a, type) => /** @type {any} */ (instancedDynamicBufferAttribute(a, type));
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* { value } objects -> uniform nodes reading them each render (uniforms.tsl.js valueNode, the engine's one read-back form)
|
|
37
|
+
* @param {Record<string, { value: any }>} u @returns {any} ParticleNodes or WeatherNodes: node types follow the values (valueNode)
|
|
38
|
+
*/
|
|
39
|
+
function nodesOf(u) {
|
|
40
|
+
const n = {};
|
|
41
|
+
for (const [k, o] of Object.entries(u)) {
|
|
42
|
+
const v = o.value;
|
|
43
|
+
if (typeof v !== 'number' && !v?.isVector2 && !v?.isVector3 && !v?.isColor) continue; // textures go through mapSlot; a game's other own uniforms only meant something to its GLSL
|
|
44
|
+
n[k] = valueNode(o);
|
|
45
|
+
}
|
|
46
|
+
return n;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// sRGB like the textures games load, so swapping in a real one does not change the colour conversion the node was built with
|
|
50
|
+
const white = () => { const t = new DataTexture(new Uint8Array([255, 255, 255, 255]), 1, 1); t.colorSpace = SRGBColorSpace; t.needsUpdate = true; return t; };
|
|
51
|
+
|
|
52
|
+
/** uniforms.uMap as a texture node: writing .value swaps the texture and rebuilds the material (the GLSL side just swaps the sampler) */
|
|
53
|
+
function mapSlot(u, mat) {
|
|
54
|
+
const node = texture(u.uMap?.value || white());
|
|
55
|
+
u.uMap = { get value() { return node.value; }, set value(t) { node.value = t || white(); mat.needsUpdate = true; } };
|
|
56
|
+
return node;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A sprite drawing `count` instances with its own copy of the quad (engine.remove disposes the geometry; Sprite's default one is shared) */
|
|
60
|
+
function sprite(mat, count, attrs) {
|
|
61
|
+
const s = new Sprite(mat);
|
|
62
|
+
s.geometry = s.geometry.clone();
|
|
63
|
+
for (const [name, a] of Object.entries(attrs)) s.geometry.setAttribute(name, a); // listed so dispose frees their GPU buffers too
|
|
64
|
+
s.count = count;
|
|
65
|
+
return s;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** gl_PointCoord (y down) from the sprite uv (y up) */
|
|
69
|
+
const pointCoord = () => vec2(uv().x, uv().y.oneMinus());
|
|
70
|
+
|
|
71
|
+
/** Same as modules/particles/shaders.js, per particle from instanced attributes. Returns { material, object }; material.uniforms holds the { value } objects. */
|
|
72
|
+
function particles({ attrs, count, fog, additive, maxPixels }) {
|
|
73
|
+
const u = { uScale: { value: 400 }, uMaxPx: { value: maxPixels }, uHasMap: { value: 0 }, uMap: { value: null } };
|
|
74
|
+
const m = new PointsNodeMaterial({ transparent: true, depthWrite: false, fog, sizeAttenuation: false, blending: additive ? AdditiveBlending : NormalBlending });
|
|
75
|
+
/** @type {ParticleNodes} */ const U = nodesOf(u);
|
|
76
|
+
const map = mapSlot(u, m);
|
|
77
|
+
const pos = at(attrs.aPos, 'vec3'), size = at(attrs.aSize, 'float'), alpha = at(attrs.aAlpha, 'float');
|
|
78
|
+
m.positionNode = pos;
|
|
79
|
+
m.sizeNode = Fn(() => {
|
|
80
|
+
const z = modelViewMatrix.mul(vec4(pos, 1)).z;
|
|
81
|
+
return select(alpha.lessThanEqual(0), float(0), min(size.mul(U.uScale).div(max(0.1, z.negate())), U.uMaxPx)).div(screenDPR);
|
|
82
|
+
})();
|
|
83
|
+
const vAlpha = varying(alpha), vAngle = varying(at(attrs.aAngle, 'float')), vColor = varying(at(attrs.aColor, 'vec3'));
|
|
84
|
+
m.colorNode = Fn(() => {
|
|
85
|
+
const p0 = pointCoord().sub(0.5), c = cos(vAngle), s = sin(vAngle);
|
|
86
|
+
const p = vec2(c.mul(p0.x).sub(s.mul(p0.y)), s.mul(p0.x).add(c.mul(p0.y)));
|
|
87
|
+
const tex = select(U.uHasMap.greaterThan(0.5), map.sample(p.add(0.5)), vec4(1, 1, 1, smoothstep(0.2, 0.5, length(p)).oneMinus())); // = GLSL smoothstep(0.5, 0.2, x): reversed edges are undefined in WGSL
|
|
88
|
+
const a = tex.a.mul(vAlpha);
|
|
89
|
+
If(a.lessThan(0.01), () => { Discard(); });
|
|
90
|
+
return vec4(vColor.mul(tex.rgb), a);
|
|
91
|
+
})();
|
|
92
|
+
/** @type {any} */ (m).uniforms = u; // not a NodeMaterial field: where Particles.update / the texture loader write, as on the ShaderMaterial
|
|
93
|
+
return { material: m, object: sprite(m, count, attrs) };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const SHAPE = { round: d => step(dot(d, d), 0.25), soft: d => smoothstep(0, 0.25, dot(d, d)).oneMinus(), square: () => float(1) };
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Fall + wrap around the camera, as in weather/shaders.js (uFallOff and uSway arrive wrapped from the CPU)
|
|
100
|
+
* @param {WeatherNodes} U @param {N<'vec3'>} p @param {(fall: N<'float'>) => N<'float'>} dx @param {(fall: N<'float'>) => N<'float'>} dz
|
|
101
|
+
*/
|
|
102
|
+
const wrap = (U, p, dx, dz) => {
|
|
103
|
+
const fall = mod(p.y.sub(U.uFallOff), U.uSize), half = U.uSize.mul(0.5);
|
|
104
|
+
return vec3(mod(p.x.sub(U.uCam.x).add(dx(fall)), U.uSize).sub(half), fall.sub(U.uSize.mul(0.35)), mod(p.z.sub(U.uCam.z).add(dz(fall)), U.uSize).sub(half)).add(U.uCam);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** Weather streaks (LineSegments, 1px like WebGL lines): same attributes and uniforms as lineVert / lineFrag */
|
|
108
|
+
function weatherLines(u) {
|
|
109
|
+
/** @type {WeatherNodes} */ const U = nodesOf(u);
|
|
110
|
+
const m = new LineBasicNodeMaterial({ transparent: true, depthWrite: false });
|
|
111
|
+
const end = attribute('aEnd', 'float');
|
|
112
|
+
m.positionNode = Fn(() => {
|
|
113
|
+
const w = wrap(U, positionGeometry, f => U.uWindDir.x.mul(f).mul(0.3), f => U.uWindDir.y.mul(f).mul(0.3));
|
|
114
|
+
return w.sub(vec3(end.mul(U.uWindDir.x).mul(0.2), end.mul(U.uLen), end.mul(U.uWindDir.y).mul(0.2)));
|
|
115
|
+
})();
|
|
116
|
+
const vA = varying(end.mul(0.8).oneMinus());
|
|
117
|
+
m.colorNode = vec4(U.uColor, U.uOpacity.mul(vA));
|
|
118
|
+
return m;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Weather sprites (snow, drops): `geo`'s positions become the instances (geo itself is not drawn); same uniforms as pointVert / pointFrag(shape) */
|
|
122
|
+
function weatherPoints(u, geo, shape) {
|
|
123
|
+
const m = new PointsNodeMaterial({ transparent: true, depthWrite: false, sizeAttenuation: false });
|
|
124
|
+
/** @type {WeatherNodes} */ const U = nodesOf(u);
|
|
125
|
+
const map = mapSlot(u, m), src = geo.getAttribute('position');
|
|
126
|
+
const a = new InstancedBufferAttribute(src.array, 3), p = at(a, 'vec3');
|
|
127
|
+
const w = wrap(U, p, () => sin(U.uSway.mul(0.7).add(p.y)).mul(U.uSwayAmp), () => cos(U.uSway.mul(0.5).add(p.x)).mul(U.uSwayAmp));
|
|
128
|
+
m.positionNode = w;
|
|
129
|
+
m.sizeNode = min(U.uPx.mul(3).div(modelViewMatrix.mul(vec4(w, 1)).z.negate()), U.uMaxPx).div(screenDPR);
|
|
130
|
+
m.colorNode = Fn(() => {
|
|
131
|
+
const pc = pointCoord(), d = pc.sub(0.5);
|
|
132
|
+
const c = select(U.uUseMap.greaterThan(0.5), map.sample(pc), vec4(1, 1, 1, SHAPE[shape](d)));
|
|
133
|
+
If(c.a.lessThan(0.05), () => { Discard(); });
|
|
134
|
+
return vec4(U.uColor.mul(c.rgb), U.uOpacity.mul(c.a));
|
|
135
|
+
})();
|
|
136
|
+
return sprite(m, src.count, { aPos: a });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
registerWebGPUPart('particles', Object.freeze({ particles, weatherLines, weatherPoints }));
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// TSL side of shader patches: what a patch's `tsl` field does on a NodeMaterial (WebGPURenderer), slot by slot (render/slots.js).
|
|
2
|
+
// Why a registry instead of an import: render/patches.js is in every WebGL game's bundle and must not pull in three/tsl, so this module
|
|
3
|
+
// (loaded with threedog/tsl, i.e. only on the WebGPU route) registers itself (render/backend.js registerWebGPUPart) and patches.js looks it up when a
|
|
4
|
+
// NodeMaterial gets patches. One list per material (patches.js keeps it, with the same conflict / needs / replace-by-name rules as GLSL);
|
|
5
|
+
// this file only turns that list into nodes.
|
|
6
|
+
//
|
|
7
|
+
// Where each slot lands (and why it is not always the NodeMaterial property of the same name):
|
|
8
|
+
// position / shadowPosition / clipPosition / map / roughness / emissive: the properties positionNode / castShadowPositionNode / vertexNode /
|
|
9
|
+
// colorNode / roughnessNode / emissiveNode. position must be the property: three's shadow pass reads material.positionNode
|
|
10
|
+
// (Renderer._getShadowNodes), which is how a swaying tree's shadow sways with no depth material. These become instance accessors:
|
|
11
|
+
// the getter returns the chain, a game's own assignment (before or after patching) becomes the chain's starting value.
|
|
12
|
+
// position / shadowPosition run where GLSL runs them: on the object-space vertex before morphs, skinning, batching and instancing
|
|
13
|
+
// (three applies positionNode after all of those). The property holds a stand-in node; NodeMaterial.setupPosition (wrapped once below,
|
|
14
|
+
// for every NodeMaterial including three's shadow-pass material) runs the patch chain first, then three's own steps. A game's own
|
|
15
|
+
// positionNode keeps three's meaning (applied after instancing).
|
|
16
|
+
// color: after setupDiffuseColor, so ctx.color already has the map and vertex / instance colors (colorNode is applied before them).
|
|
17
|
+
// normal: around setupNormal (ctx.normal = what three would use: normalNode or the normal map, view space).
|
|
18
|
+
// output: around setupOutput, before fog (outputNode would come after fog, unlike the GLSL slot).
|
|
19
|
+
// lighting: the material's own LightingModel wrapped (setupLightingModel); code runs at the end of start(), after every light and the
|
|
20
|
+
// ambient are summed into reflectedLight but before LightsNode adds them up (measured in experiment E2: finish() is too late).
|
|
21
|
+
// Chaining: patches on one slot run in the order added, each ctx carrying the value so far (same rule as GLSL).
|
|
22
|
+
import * as TSL from 'three/tsl';
|
|
23
|
+
import { NodeMaterial, LightingNode } from 'three/webgpu';
|
|
24
|
+
import { codedError } from '../../core/errorHints.js';
|
|
25
|
+
import { wind } from './wind.tsl.js';
|
|
26
|
+
import { cloudShadow } from './cloudShadow.tsl.js';
|
|
27
|
+
import { season } from './season.tsl.js';
|
|
28
|
+
import { objectOrigin } from './objectOrigin.tsl.js';
|
|
29
|
+
import { detailNoise, detailBump } from './detail.tsl.js';
|
|
30
|
+
import { wrapDiffuse, shadowTint, rim } from './lighting.tsl.js';
|
|
31
|
+
import { waterWave, waterSurface, waterFresnel } from './water.tsl.js';
|
|
32
|
+
import { engineNodes, valueNode } from './uniforms.tsl.js';
|
|
33
|
+
import { registerWebGPUPart } from '../backend.js';
|
|
34
|
+
import { engineUniformName } from '../engineUniforms.js';
|
|
35
|
+
import { SLOTS, LIGHT_HOOKS as HOOKS } from '../slots.js';
|
|
36
|
+
import { notePatch } from '../../core/threePatches.js';
|
|
37
|
+
import { nameNodeBuild, lostStatements } from './nodeErrors.tsl.js';
|
|
38
|
+
|
|
39
|
+
const { Fn, positionLocal, modelViewProjection, materialColor, materialRoughness, materialEmissive, materialNormal, diffuseColor, vec3 } = TSL;
|
|
40
|
+
|
|
41
|
+
/** One engine uniform as a node, looked up from the renderer compiling it (same as the Fn library) */
|
|
42
|
+
const engineUniform = name => Fn((_, builder) => {
|
|
43
|
+
const n = engineNodes(builder, 'patch')[name];
|
|
44
|
+
if (!n) throw codedError('tsl-misuse', `threedog/tsl: engine.uniforms has no '${name}'`);
|
|
45
|
+
return n;
|
|
46
|
+
})();
|
|
47
|
+
// A patch's own uniforms ({ value } objects, shared with its GLSL side) as nodes: one node per object, reading the object every render, so
|
|
48
|
+
// detail.strength or the water's fresnel set live move both renderers. An engine uniform (engine.uniforms.uTime in the water patches) goes
|
|
49
|
+
// through engineUniform instead: resolved per compiling renderer, like resolveUniform on the GLSL side.
|
|
50
|
+
const OWN = new WeakMap();
|
|
51
|
+
/** @param {{ value: any }} o */
|
|
52
|
+
const patchUniform = o => {
|
|
53
|
+
const eng = engineUniformName(o);
|
|
54
|
+
if (eng) return engineUniform(eng);
|
|
55
|
+
let n = OWN.get(o);
|
|
56
|
+
if (!n) OWN.set(o, n = valueNode(o)); // render group: a 'color' / 'output' / 'lighting' patch leaves the material without node properties
|
|
57
|
+
return n;
|
|
58
|
+
};
|
|
59
|
+
/** Sample the texture held by a { value } object at uv; the node reads o.value, so a texture that finishes loading later (value swapped) is used */
|
|
60
|
+
const patchTexture = (o, uv) => {
|
|
61
|
+
const n = TSL.texture(o.value, uv);
|
|
62
|
+
Object.defineProperty(n, 'value', { configurable: true, get: () => o.value, set(v) { o.value = v; } });
|
|
63
|
+
return n;
|
|
64
|
+
};
|
|
65
|
+
/** What a patch's tsl code gets besides the slot value: the threedog/tsl functions and three/tsl itself (so engine files need no import) */
|
|
66
|
+
const FX = Object.freeze({ wind, cloudShadow, season, objectOrigin, detailNoise, detailBump, wrapDiffuse, shadowTint, rim,
|
|
67
|
+
waterWave, waterSurface, waterFresnel, uniform: engineUniform, patchUniform, patchTexture });
|
|
68
|
+
|
|
69
|
+
// slot -> [property, default value when the material has none]; built from the slot table's node column (render/slots.js), so the table cannot
|
|
70
|
+
// list a mapping this file lacks. The default is what three uses without the property.
|
|
71
|
+
const DEFAULTS = { positionNode: () => positionLocal, castShadowPositionNode: () => positionLocal, vertexNode: () => modelViewProjection,
|
|
72
|
+
colorNode: () => materialColor, roughnessNode: () => materialRoughness, emissiveNode: () => materialEmissive };
|
|
73
|
+
const PROPS = Object.fromEntries(Object.entries(SLOTS).filter(([, s]) => s.node?.prop).map(([n, s]) => {
|
|
74
|
+
if (!DEFAULTS[s.node.prop]) throw codedError('shader-patch', `[threedog] render/slots.js names node property '${s.node.prop}' for slot '${n}', which patchNodes.tsl.js has no default for`);
|
|
75
|
+
return [n, [s.node.prop, DEFAULTS[s.node.prop]]];
|
|
76
|
+
}));
|
|
77
|
+
// position chains, by their stand-in node: what setupPosition runs before three's morph / skinning / instancing steps
|
|
78
|
+
const PRE = new WeakMap();
|
|
79
|
+
/**
|
|
80
|
+
* The stand-in for a position chain: evaluates to the game's own value (or positionLocal, i.e. leaves three's result as it is) where three
|
|
81
|
+
* applies positionNode, while the chain itself runs at the start of setupPosition. Its cache key is the chain's, so a different chain is a
|
|
82
|
+
* different program. @param {any} chain @param {any} base
|
|
83
|
+
*/
|
|
84
|
+
function preMarker(chain, base) {
|
|
85
|
+
const m = Fn(() => base ?? positionLocal)();
|
|
86
|
+
m.customCacheKey = () => chain.getCacheKey();
|
|
87
|
+
PRE.set(m, chain);
|
|
88
|
+
return m;
|
|
89
|
+
}
|
|
90
|
+
// Why the prototype and not each material: three's shadow pass draws with its own NodeMaterial, handed our positionNode / castShadowPositionNode
|
|
91
|
+
// (Renderer._getShadowNodes), and the shadow must sway exactly like the surface. Only materials carrying one of our stand-ins are affected.
|
|
92
|
+
const setupPosition = NodeMaterial.prototype.setupPosition;
|
|
93
|
+
if (!(/** @type {{ _threedogPre?: boolean }} */ (/** @type {unknown} */ (setupPosition)))._threedogPre) { // already wrapped (module loaded twice)
|
|
94
|
+
NodeMaterial.prototype.setupPosition = Object.assign(function (builder) {
|
|
95
|
+
const chain = PRE.get(this.positionNode);
|
|
96
|
+
if (chain) positionLocal.assign(TSL.subBuild(chain, 'POSITION', 'vec3'));
|
|
97
|
+
return setupPosition.call(this, builder);
|
|
98
|
+
}, { _threedogPre: true });
|
|
99
|
+
notePatch('node-setup-position', 'NodeMaterial.prototype.setupPosition: runs the vertex-moving patch chain before three builds the position (WebGPU wind and its shadow)',
|
|
100
|
+
() => { NodeMaterial.prototype.setupPosition = setupPosition; }, { lost: 'on WebGPURenderer, wind and other vertex-moving patches no longer move vertices or their shadow' });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const BUILT = Object.keys(SLOTS).filter(n => SLOTS[n].node?.setup); // slots handled inside three's setup methods
|
|
104
|
+
const LIGHT_KEYS = ['directDiffuse', 'directSpecular', 'indirectDiffuse', 'indirectSpecular'];
|
|
105
|
+
|
|
106
|
+
/** The patch's code for a slot as { start?, light?, direct?, after? } (lighting) or a function, or undefined */
|
|
107
|
+
const codeOf = (p, slot) => p.tsl?.[slot];
|
|
108
|
+
const prefix = (p, slot) => `[threedog] patch '${p.name}' (slot ${slot})`;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Run one patch's code with the try the error prefix needs; `want` checks the result. The nodes it returns carry the prefix into three's
|
|
112
|
+
* later build (nameNodeBuild), and statements it issues outside three's build are named in dev (lostStatements). probe: false inside the
|
|
113
|
+
* build (ctx.builder: a stack is there, so a Loop is fine) and for compute()'s stand-in runs of the build-time slots, whose real run is there.
|
|
114
|
+
*/
|
|
115
|
+
function run(p, slot, fn, ctx, want = 'node', probe = !ctx.builder) {
|
|
116
|
+
let v;
|
|
117
|
+
try { v = probe ? lostStatements(prefix(p, slot), () => fn(ctx)) : fn(ctx); }
|
|
118
|
+
catch (e) {
|
|
119
|
+
const msg = String(e?.message ?? e);
|
|
120
|
+
if (msg.startsWith(prefix(p, slot))) throw e;
|
|
121
|
+
const err = Object.assign(codedError('shader-patch', `${prefix(p, slot)}: ${msg}`), { cause: e });
|
|
122
|
+
// the stack of the error that happened (the game's patch line first), not of this wrapper: devtools and three's 'TSL:' build-error log
|
|
123
|
+
// (StackTrace, first non-three frame) would otherwise point at this file. Chrome frames are ' at ...', Firefox / Safari 'fn@file:line:col'
|
|
124
|
+
const frames = String(e?.stack ?? '').split('\n').filter(l => /^\s+at |@.+:\d+:\d+$/.test(l));
|
|
125
|
+
if (frames.length) err.stack = `${err.name}: ${err.message}\n${frames.join('\n')}`;
|
|
126
|
+
throw err;
|
|
127
|
+
}
|
|
128
|
+
if (want === 'node' && !v?.isNode) throw codedError('shader-patch', `${prefix(p, slot)}: tsl must return a node (the new ${slot} value), got ${v === null ? 'null' : typeof v}`);
|
|
129
|
+
if (want === 'light' && v != null) {
|
|
130
|
+
const bad = typeof v !== 'object' ? [String(v)] : Object.keys(v).filter(k => !LIGHT_KEYS.includes(k) || !v[k]?.isNode);
|
|
131
|
+
if (bad.length) throw codedError('shader-patch', `${prefix(p, slot)}: tsl returns nothing or { ${LIGHT_KEYS.join(', ')} } as nodes; got ${bad.join(', ')}`);
|
|
132
|
+
for (const k in v || {}) nameNodeBuild(v[k], prefix(p, slot), ctx[k]);
|
|
133
|
+
}
|
|
134
|
+
return want === 'node' ? nameNodeBuild(v, prefix(p, slot), ctx[slot]) : v;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Fold every patch's code for one value slot over `value` (the value so far), in list order */
|
|
138
|
+
const chain = (list, slot, value, extra) => list.reduce((v, p) => { const fn = codeOf(p, slot);
|
|
139
|
+
return typeof fn === 'function' ? run(p, slot, fn, { ...extra, [slot]: v }) : v; }, value);
|
|
140
|
+
|
|
141
|
+
/** lighting hooks of a patch, normalised: a function is the end-of-lighting code */
|
|
142
|
+
const hooksOf = h => typeof h === 'function' ? { after: h } : h;
|
|
143
|
+
|
|
144
|
+
// a classic material has no `lights` flag (that is a ShaderMaterial / NodeMaterial field): the lit classic types are the ones three's node
|
|
145
|
+
// twins light
|
|
146
|
+
const lit = m => m.isNodeMaterial ? !!m.lights && !m.isMeshBasicNodeMaterial
|
|
147
|
+
: !!(m.isMeshLambertMaterial || m.isMeshPhongMaterial || m.isMeshStandardMaterial || m.isMeshToonMaterial);
|
|
148
|
+
/**
|
|
149
|
+
* Why this material cannot take the patch on the TSL side ('' = fine). patches.js throws it (strict) or skips with a warning, like a missing GLSL anchor.
|
|
150
|
+
* m is a NodeMaterial, or a classic material whose node twin three builds (same lit / roughness facts).
|
|
151
|
+
* @param {any} m @param {any} p
|
|
152
|
+
*/
|
|
153
|
+
function gaps(m, p) {
|
|
154
|
+
const out = [];
|
|
155
|
+
for (const slot of Object.keys(p.tsl || {})) {
|
|
156
|
+
if (!(slot in PROPS) && !BUILT.includes(slot)) out.push(`slot '${slot}' is WebGL only (render/slots.js has no NodeMaterial place for it)`);
|
|
157
|
+
// MeshBasicNodeMaterial has lights = true (for light maps) but no direct lighting, as MeshBasicMaterial has no lights chunk in GLSL
|
|
158
|
+
else if (slot === 'lighting' && !lit(m)) out.push(`slot 'lighting' needs a lit material (${m.type} has no lights)`);
|
|
159
|
+
else if (slot === 'roughness' && m.roughness === undefined) out.push(`slot 'roughness' needs a standard / physical material`);
|
|
160
|
+
const h = p.tsl[slot];
|
|
161
|
+
if (slot === 'lighting' ? !(typeof h === 'function' || (h && typeof h === 'object' && HOOKS.some(k => typeof h[k] === 'function')))
|
|
162
|
+
: typeof h !== 'function') out.push(`slot '${slot}' code must be (ctx) => node` + (slot === 'lighting' ? ' or { start?, light?, direct?, after? }' : ''));
|
|
163
|
+
}
|
|
164
|
+
return out.length ? `patch '${p.name}': ${out.join('; ')}` : '';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// The nodes a patch list turns into, kept per SOURCE material: a NodeMaterial patched directly, or a classic material (MeshLambertMaterial ...)
|
|
168
|
+
// patched for both renderers. Why classic materials too: the engine's own materials (terrain, vegetation, water) stay classic so
|
|
169
|
+
// one material object draws on WebGLRenderer (GLSL side) and on WebGPURenderer, including fallback: 'webgl', which is decided only in start().
|
|
170
|
+
// three's WebGPURenderer turns a classic material into a fresh node twin per build (NodeLibrary.fromMaterial); render/patches.js wraps that
|
|
171
|
+
// one call so the twin gets these nodes (dress). The source keeps them too, as non-enumerable accessors: three's shadow pass reads
|
|
172
|
+
// positionNode / castShadowPositionNode off the drawn (classic) material, which is how a swaying tree's shadow sways.
|
|
173
|
+
const STATE = new WeakMap(); // source -> { list, own (game values of PROPS), out (chained PROPS), color, key, fresh, error }
|
|
174
|
+
// chained node -> the game value it was built on. Why: three's NodeMaterial.copy reads source.positionNode through the accessor, i.e. the
|
|
175
|
+
// chain, and stores it as the copy's own value; copyPatches would then chain the same patches on top again (the effect applied twice)
|
|
176
|
+
const BASE = new WeakMap();
|
|
177
|
+
const baseOf = v => (v && BASE.has(v) ? BASE.get(v) : v ?? null);
|
|
178
|
+
// listOf / src: set for a classic source only (its list lives in patches.js), so a stale state rebuilds itself when read
|
|
179
|
+
const newState = () => ({ list: [], own: {}, out: {}, color: null, key: '', fresh: false, error: null, listOf: null, src: null });
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Accessors for every PROPS property on target, reading st (the chain, else the game's own value). enumerable: a NodeMaterial's own
|
|
183
|
+
* properties (three keys its program by them); a classic material or a node twin gets hidden ones (three's program key for those is the
|
|
184
|
+
* classic material's customProgramCacheKey, render/patches.js).
|
|
185
|
+
*/
|
|
186
|
+
function accessors(target, st, enumerable) {
|
|
187
|
+
for (const [prop] of Object.values(PROPS)) {
|
|
188
|
+
if (enumerable) st.own[prop] = baseOf(target[prop]);
|
|
189
|
+
Object.defineProperty(target, prop, { configurable: true, enumerable, get: () => { if (!st.fresh && st.listOf) classic(st.src, st.listOf()); return st.out[prop] ?? st.own[prop] ?? null; },
|
|
190
|
+
set(v) { st.own[prop] = v ?? null; st.fresh = false; if (enumerable) compute(target, st); target.needsUpdate = true; } });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Wrap three's setup methods of the NodeMaterial m (a patched one, or a classic material's node twin) to run st's color / normal / output / lighting code */
|
|
195
|
+
function wrapSetup(m, st, src) {
|
|
196
|
+
const ctx = extra => ({ fx: FX, tsl: TSL, material: src, ...extra });
|
|
197
|
+
const diffuse = m.setupDiffuseColor, normal = m.setupNormal, output = m.setupOutput, model = m.setupLightingModel;
|
|
198
|
+
m.setupDiffuseColor = function (builder) { const r = diffuse.call(this, builder); if (st.color) diffuseColor.assign(st.color); return r; };
|
|
199
|
+
m.setupNormal = function (builder) { return chain(st.list, 'normal', normal.call(this, builder), ctx({ builder })); };
|
|
200
|
+
m.setupOutput = function (builder, node) { return output.call(this, builder, chain(st.list, 'output', node, ctx({ builder }))); };
|
|
201
|
+
m.setupLightingModel = function (builder) {
|
|
202
|
+
const lm = model.call(this, builder);
|
|
203
|
+
const hooks = st.list.filter(p => codeOf(p, 'lighting')).map(p => [p, hooksOf(codeOf(p, 'lighting'))]);
|
|
204
|
+
if (!lm || !hooks.length) return lm;
|
|
205
|
+
// Why each direct hook adds into its own light sums: on WebGL every lighting patch runs after three's light loop, in list order, so
|
|
206
|
+
// light a patch adds is scaled only by the patches after it. Here direct() runs inside the loop; added straight into reflectedLight it
|
|
207
|
+
// would be scaled by every after() (the clouds patch darkened the lighting patch's wrap light on WebGPU only). Each patch's sums join
|
|
208
|
+
// reflectedLight at its own place in the list, right before its after().
|
|
209
|
+
const { start, direct } = lm, states = hooks.map(() => ({}));
|
|
210
|
+
let sums = [];
|
|
211
|
+
const light = (rl, i, b) => ctx({ builder: b, state: states[i], reflectedLight: rl, ...Object.fromEntries(LIGHT_KEYS.map(k => [k, rl[k]])) });
|
|
212
|
+
lm.start = function (b) {
|
|
213
|
+
const rl = b.context.reflectedLight;
|
|
214
|
+
sums = hooks.map(([, h], i) => h.direct ? Object.fromEntries(LIGHT_KEYS.map(k => [k, vec3(0).toVar(`tdLight${i}_${k}`)])) : null);
|
|
215
|
+
hooks.forEach(([p, h], i) => h.start && run(p, 'lighting', h.start, light(rl, i, b), 'any'));
|
|
216
|
+
start.call(this, b);
|
|
217
|
+
hooks.forEach(([p, h], i) => {
|
|
218
|
+
if (sums[i]) for (const k of LIGHT_KEYS) rl[k].addAssign(sums[i][k]);
|
|
219
|
+
if (!h.after) return;
|
|
220
|
+
const v = run(p, 'lighting', h.after, light(rl, i, b), 'light');
|
|
221
|
+
for (const k in v || {}) rl[k].assign(v[k]);
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
lm.direct = function (data, b) {
|
|
225
|
+
const ln = data.lightNode;
|
|
226
|
+
// the light as a patch needs it: direction and color (shadow already multiplied in), its shadow factor alone, whether it is the sun
|
|
227
|
+
const L = { direction: data.lightDirection, color: data.lightColor, shadow: ln?.shadowNode ? ln.shadowNode.x : null,
|
|
228
|
+
directional: !!ln?.light?.isDirectionalLight, light: ln?.light ?? null };
|
|
229
|
+
// light(light, ctx) hooks first, in list order: a new color for this one light before three lights the surface with it (the cloud
|
|
230
|
+
// shadow dims the sun only; GLSL does the same by wrapping three's getDirectionalLightInfo in lightsPars). direct() sees the color as
|
|
231
|
+
// three gave it, as GLSL code reading directionalLights[i].color does
|
|
232
|
+
let color = data.lightColor;
|
|
233
|
+
hooks.forEach(([p, h], i) => {
|
|
234
|
+
if (!h.light) return;
|
|
235
|
+
const v = run(p, 'lighting', c => h.light({ ...L, color }, c), light(sums[i] ?? data.reflectedLight, i, b), 'any');
|
|
236
|
+
if (v == null) return;
|
|
237
|
+
if (!v.isNode) throw codedError('shader-patch', `${prefix(p, 'lighting')}: light() returns the light's new color as a node, or nothing; got ${typeof v}`);
|
|
238
|
+
color = v;
|
|
239
|
+
});
|
|
240
|
+
const r = direct.call(this, color === data.lightColor ? data : { ...data, lightColor: color }, b);
|
|
241
|
+
hooks.forEach(([p, h], i) => h.direct && run(p, 'lighting', c => h.direct(L, c), light(sums[i], i, b), 'any'));
|
|
242
|
+
return r;
|
|
243
|
+
};
|
|
244
|
+
return lm;
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Install the accessors and method wrappers once on a NodeMaterial patched directly */
|
|
249
|
+
function install(m) {
|
|
250
|
+
const st = newState();
|
|
251
|
+
STATE.set(m, st);
|
|
252
|
+
accessors(m, st, true);
|
|
253
|
+
wrapSetup(m, st, m);
|
|
254
|
+
// clone() carries no patches (as on the GLSL side, where Material.copy skips onBeforeCompile): the copy gets the game's own values, not the
|
|
255
|
+
// chains, so a clone without copyPatches is the plain material and one with copyPatches has each effect once
|
|
256
|
+
const clone = m.clone, key = m.customProgramCacheKey;
|
|
257
|
+
m.clone = function () {
|
|
258
|
+
const c = clone.call(this);
|
|
259
|
+
for (const [prop] of Object.values(PROPS)) if (c[prop] === st.out[prop]) c[prop] = st.own[prop];
|
|
260
|
+
return c;
|
|
261
|
+
};
|
|
262
|
+
// three keys NodeMaterial programs by their node properties only; code added inside the setup methods is not one, so two materials
|
|
263
|
+
// differing only in a color / normal / output / lighting patch would share a program. st.key is built from those patches' node graphs
|
|
264
|
+
m.customProgramCacheKey = function () { return key.call(this) + (st.key ? '|threedog:' + st.key : ''); };
|
|
265
|
+
return st;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Recompute everything the list decides (called on every change): chained properties, the color node, the cache key */
|
|
269
|
+
function compute(m, st) {
|
|
270
|
+
const extra = { fx: FX, tsl: TSL, material: m };
|
|
271
|
+
const out = {};
|
|
272
|
+
for (const [slot, [prop, dflt]] of Object.entries(PROPS)) {
|
|
273
|
+
if (slot === 'shadowPosition') {
|
|
274
|
+
// the shadow passes run the vertex code of shadow: true patches only, each one's shadowPosition code where it has one, else its
|
|
275
|
+
// position code (the GLSL rule: patches.js shadowMaterial). three's shadow pass reads castShadowPositionNode before positionNode, so it
|
|
276
|
+
// is set whenever the two differ: a shadow: false patch then moves the surface but not its shadow, on both renderers
|
|
277
|
+
if (!st.list.some(p => codeOf(p, 'shadowPosition') || (codeOf(p, 'position') && !p.shadow))) continue;
|
|
278
|
+
const c = st.list.reduce((v, p) => { const s = !p.shadow ? null : codeOf(p, 'shadowPosition') ? 'shadowPosition' : codeOf(p, 'position') ? 'position' : null;
|
|
279
|
+
return s ? run(p, s, codeOf(p, s), { ...extra, position: v }) : v; }, positionLocal);
|
|
280
|
+
out[prop] = preMarker(c, st.own[prop] ?? st.own.positionNode);
|
|
281
|
+
} else if (!st.list.some(p => typeof codeOf(p, slot) === 'function')) continue;
|
|
282
|
+
else if (slot === 'position') out[prop] = preMarker(chain(st.list, slot, positionLocal, extra), st.own[prop]);
|
|
283
|
+
else out[prop] = chain(st.list, slot, st.own[prop] ?? dflt(), extra);
|
|
284
|
+
}
|
|
285
|
+
const color = st.list.some(p => codeOf(p, 'color')) ? chain(st.list, 'color', diffuseColor, extra) : null;
|
|
286
|
+
// the build-time slots are also run once here on stand-in values: a typo in the code fails now, at the call that added the patch,
|
|
287
|
+
// and the node graphs it returns key the program (customProgramCacheKey above)
|
|
288
|
+
const keys = [];
|
|
289
|
+
for (const p of st.list) {
|
|
290
|
+
const parts = [p.name + '=' + (p.cacheKey ?? '')];
|
|
291
|
+
if (codeOf(p, 'normal')) parts.push(run(p, 'normal', codeOf(p, 'normal'), { ...extra, normal: materialNormal }, 'node', false).getCacheKey(true));
|
|
292
|
+
if (codeOf(p, 'output')) parts.push(run(p, 'output', codeOf(p, 'output'), { ...extra, output: TSL.vec4(0) }, 'node', false).getCacheKey(true));
|
|
293
|
+
const h = codeOf(p, 'lighting');
|
|
294
|
+
if (typeof h === 'function') {
|
|
295
|
+
const v = run(p, 'lighting', h, { ...extra, state: {}, ...Object.fromEntries(LIGHT_KEYS.map(k => [k, vec3(0)])) }, 'light', false);
|
|
296
|
+
for (const k in v || {}) parts.push(k + v[k].getCacheKey(true));
|
|
297
|
+
} else if (h) parts.push(Object.values(h).map(String).join()); // hook objects: their text (put values that change the code into cacheKey)
|
|
298
|
+
if (codeOf(p, 'color')) parts.push('c');
|
|
299
|
+
keys.push(parts.join(':'));
|
|
300
|
+
}
|
|
301
|
+
for (const prop in out) BASE.set(out[prop], st.own[prop]);
|
|
302
|
+
st.out = out; st.color = color; st.key = (color ? color.getCacheKey(true) + ',' : '') + keys.join(',');
|
|
303
|
+
st.fresh = true;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Make the material's nodes follow its patch list (patchMaterial / unpatchMaterial / copyPatches call it after every change on a NodeMaterial)
|
|
308
|
+
* @param {any} m @param {any[]} list
|
|
309
|
+
*/
|
|
310
|
+
function apply(m, list) {
|
|
311
|
+
const st = STATE.get(m) || install(m);
|
|
312
|
+
const prev = st.list;
|
|
313
|
+
st.list = list.slice();
|
|
314
|
+
try { compute(m, st); } catch (e) { st.list = prev; compute(m, st); throw e; }
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// ---------- classic materials (one material, both renderers) ----------
|
|
318
|
+
|
|
319
|
+
/** Why a patch on a classic material does nothing on WebGPURenderer, or '' (its GLSL is never compiled there) @param {any} m @param {any} p */
|
|
320
|
+
function noNodeForm(m, p) {
|
|
321
|
+
if (p.tsl == null && p.glsl != null) return `patch '${p.name}' on ${m.type}${m.name ? ` '${m.name}'` : ''} is GLSL only, which WebGPURenderer never compiles: `
|
|
322
|
+
+ 'give it a tsl version next to glsl (recipe 43), or stay on WebGL (no renderer option)';
|
|
323
|
+
return gaps(m, p);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** The state of a classic source material for its current list, computed when stale; a patch that cannot run is kept as st.error, not thrown */
|
|
327
|
+
function classic(src, list) {
|
|
328
|
+
const st = classicState(src);
|
|
329
|
+
if (st.fresh) return st;
|
|
330
|
+
st.list = list.slice();
|
|
331
|
+
st.error = null;
|
|
332
|
+
try {
|
|
333
|
+
const bad = list.map(p => noNodeForm(src, p)).find(Boolean);
|
|
334
|
+
if (bad) throw codedError('webgpu-needs-tsl', `[threedog] ${bad}`);
|
|
335
|
+
compute(src, st);
|
|
336
|
+
} catch (e) {
|
|
337
|
+
// drawn without its patches instead of a frame loop that throws: start() and every later add name it (render/backend.js webglOnlyOn)
|
|
338
|
+
st.error = e; st.list = []; st.out = {}; st.color = null; st.key = ''; st.fresh = true;
|
|
339
|
+
}
|
|
340
|
+
return st;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* A classic material's patch list changed (patches.js): its nodes are rebuilt on the next read. The hidden accessors are installed on the
|
|
345
|
+
* first change, so the shadow pass finds positionNode even before the color pass converted the material.
|
|
346
|
+
* @param {any} src @param {() => any[]} listOf the material's current list
|
|
347
|
+
*/
|
|
348
|
+
function invalidate(src, listOf) { const st = classicState(src); st.listOf = listOf; st.fresh = false; }
|
|
349
|
+
|
|
350
|
+
/** A classic source's state, made (with its hidden accessors) on first use @param {any} src */
|
|
351
|
+
function classicState(src) {
|
|
352
|
+
let st = STATE.get(src);
|
|
353
|
+
if (!st) { STATE.set(src, st = newState()); st.src = src; accessors(src, st, false); }
|
|
354
|
+
return st;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Give the node twin three just made of a classic material (NodeLibrary.fromMaterial) the source's patch nodes; returns the twin
|
|
359
|
+
* @param {any} twin @param {any} src @param {any[]} list
|
|
360
|
+
*/
|
|
361
|
+
function dress(twin, src, list) {
|
|
362
|
+
environmentDiffuse(twin);
|
|
363
|
+
if (!list.length) return twin;
|
|
364
|
+
const st = classic(src, list);
|
|
365
|
+
if (!st.list.length) return twin;
|
|
366
|
+
accessors(twin, st, false);
|
|
367
|
+
wrapSetup(twin, st, src);
|
|
368
|
+
return twin;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Lambert / Phong twins: diffuse light from scene.environment, as WebGLRenderer gives the classic material. Why: three 0.187's WebGLRenderer
|
|
372
|
+
// lights MeshLambertMaterial / MeshPhongMaterial without an envMap of their own with scene.environment's irradiance (WebGLPrograms: PMREM
|
|
373
|
+
// for Lambert / Phong, lights_fragment_maps getIBLIrradiance), but their node twins (BasicEnvironmentNode) read only material.envMap, so on
|
|
374
|
+
// WebGPU every engine Lambert / Phong surface lost the room map's (or sky map's) fill: terrain and trees about 4% darker on the whole picture,
|
|
375
|
+
// a plain gray plane 30% darker (tests/specs/engine-webgpu.mjs measures both routes). Same formula as GLSL getIBLIrradiance:
|
|
376
|
+
// PI * the roughest PMREM level along the world normal (env rotation applied) * scene.environmentIntensity, added to the indirect irradiance.
|
|
377
|
+
class EnvironmentDiffuse extends LightingNode {
|
|
378
|
+
/** @param {any} envNode builder.environmentNode (a texture node of scene.environment) */
|
|
379
|
+
constructor(envNode) { super(); this.envNode = envNode; this.pmrem = null; this.updateBeforeType = TSL.NodeUpdateType.RENDER; }
|
|
380
|
+
// setup and updateBefore return nothing, like three's own EnvironmentNode (@types/three declares a Node / boolean result): typed any for that
|
|
381
|
+
/** @param {any} builder @returns {any} */
|
|
382
|
+
setup(builder) {
|
|
383
|
+
this.pmrem = TSL.pmremTexture(this.envNode.value, TSL.materialEnvRotation.mul(TSL.normalWorld), TSL.float(1));
|
|
384
|
+
builder.context.irradiance.addAssign(this.pmrem.mul(Math.PI).mul(TSL.materialEnvIntensity).rgb);
|
|
385
|
+
}
|
|
386
|
+
// the scene's environment can change without a rebuild (as three's EnvironmentNode allows)
|
|
387
|
+
/** @param {any} frame @returns {any} */
|
|
388
|
+
updateBefore(frame) { const t = this.envNode.value; if (this.pmrem && t && this.pmrem.value !== t) { this.pmrem.value = t; this.pmrem.updateBefore(frame); } }
|
|
389
|
+
}
|
|
390
|
+
/** @param {any} twin a node twin three just made of a classic material */
|
|
391
|
+
function environmentDiffuse(twin) {
|
|
392
|
+
if (!(twin.isMeshLambertNodeMaterial || twin.isMeshPhongNodeMaterial)) return;
|
|
393
|
+
const own = twin.setupEnvironment;
|
|
394
|
+
twin.setupEnvironment = function (builder) {
|
|
395
|
+
const env = own.call(this, builder);
|
|
396
|
+
return env || this.envMap || !builder.environmentNode ? env : new EnvironmentDiffuse(builder.environmentNode);
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Why the classic material's patches do not all draw on WebGPURenderer (the message), or null @param {any} src @param {any[]} list */
|
|
401
|
+
const problem = (src, list) => classic(src, list).error?.message ?? null;
|
|
402
|
+
|
|
403
|
+
registerWebGPUPart('nodePatches', Object.freeze({ gaps, apply, invalidate, dress, problem }));
|