threedog 0.2.0 → 0.5.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 +290 -0
- package/README.md +9 -8
- package/core/Animator.js +15 -12
- package/core/Assets.js +83 -16
- package/core/Audio.js +45 -38
- package/core/Baked.js +32 -8
- package/core/Batching.js +102 -15
- package/core/Budget.js +39 -18
- package/core/Clock.js +40 -80
- package/core/DebugOverrides.js +45 -10
- package/core/Describe.js +104 -43
- package/core/Engine.js +190 -57
- package/core/Events.js +4 -1
- package/core/I18n.js +25 -13
- package/core/Input.js +168 -79
- package/core/Inspect.js +2 -2
- package/core/InspectMaterials.js +85 -0
- package/core/InspectPerf.js +61 -0
- package/core/InspectScene.js +13 -2
- package/core/Loading.js +2 -1
- package/core/Ops.js +50 -9
- package/core/Random.js +9 -2
- package/core/Rays.js +5 -5
- package/core/SaveStore.js +41 -9
- package/core/Saves.js +71 -15
- package/core/Stream.js +14 -10
- package/core/Threads.js +26 -7
- package/core/UrlState.js +25 -9
- package/core/Views.js +9 -6
- package/core/actor.js +1 -1
- package/core/adaptive.js +93 -0
- package/core/assetEntry.js +22 -5
- package/core/assetHot.js +176 -0
- package/core/assetLoad.js +38 -11
- package/core/assetManifest.js +48 -9
- package/core/assetReport.js +1 -1
- package/core/audioRules.js +13 -11
- package/core/audioVoice.js +69 -42
- package/core/canvasBox.js +1 -2
- package/core/catalog.js +5 -2
- 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 +82 -15
- package/core/describeRender.js +43 -17
- package/core/describeText.js +41 -21
- package/core/engineCamera.js +32 -0
- package/core/errorCodes.js +438 -19
- package/core/errorHints.js +37 -141
- package/core/errors.js +31 -8
- package/core/events.generated.js +12 -7
- package/core/gameLights.js +62 -0
- package/core/grayMap.js +20 -0
- package/core/grid.js +128 -0
- package/core/gridLoad.js +20 -0
- 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 +48 -12
- package/core/lightWatch.js +6 -4
- package/core/look/coreParts.js +137 -30
- package/core/look/part.js +175 -95
- package/core/look/points.js +255 -0
- package/core/look/report.js +34 -17
- package/core/look/shadowPart.js +63 -0
- package/core/look/state.js +45 -0
- package/core/look/write.js +12 -28
- package/core/loop.js +101 -37
- package/core/math.js +11 -3
- package/core/moduleSlots.js +75 -0
- package/core/moduleTypes.js +73 -0
- package/core/modules.js +145 -27
- package/core/movement.js +2 -2
- package/core/namedModel.js +70 -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 +168 -0
- package/core/page.js +2 -1
- package/core/params.js +30 -8
- package/core/plugins.js +277 -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 +203 -0
- package/core/sceneMethods.js +35 -30
- 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 +2 -1
- package/core/sharedPoint.js +317 -0
- package/core/skip.js +36 -28
- package/core/stackFrames.js +34 -0
- package/core/stacks.js +166 -0
- package/core/startup.js +153 -11
- package/core/streamSources.js +0 -11
- package/core/styleParts.js +65 -47
- package/core/targeting.js +1 -1
- package/core/textureBytes.js +20 -0
- package/core/thingExplain.js +2 -2
- package/core/thingOps.js +29 -6
- package/core/things.js +21 -11
- 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 +7 -1
- package/core/validate.js +65 -86
- package/core/wrap.js +106 -0
- package/docs/AI.md +89 -97
- package/docs/API.md +32 -28
- package/docs/CODEMAP.md +224 -149
- package/docs/COMMANDS.md +57 -34
- package/docs/CONVENTIONS.md +92 -30
- package/docs/MIGRATING-0.3.md +248 -0
- package/docs/MIGRATING-0.4.md +228 -0
- package/docs/MIGRATING-0.5.md +68 -0
- package/docs/PERF.md +35 -7
- package/docs/THREE.md +79 -27
- package/docs/api/ambience.md +0 -1
- package/docs/api/assets.md +12 -9
- package/docs/api/audio.md +10 -14
- package/docs/api/calendar.md +41 -0
- package/docs/api/cameraRig.md +16 -5
- package/docs/api/characters.md +41 -25
- package/docs/api/clock.md +6 -5
- package/docs/api/collision.md +9 -6
- package/docs/api/content.md +6 -4
- package/docs/api/dialogue.md +9 -9
- package/docs/api/engine.md +65 -62
- package/docs/api/events.md +19 -14
- package/docs/api/grids.md +3 -3
- package/docs/api/ground.md +1 -1
- package/docs/api/hud.md +2 -2
- package/docs/api/i18n.md +1 -1
- package/docs/api/input.md +11 -6
- package/docs/api/interactions.md +7 -5
- package/docs/api/inventory.md +17 -16
- package/docs/api/level.md +0 -1
- package/docs/api/lightPool.md +3 -1
- package/docs/api/music.md +1 -1
- package/docs/api/nav.md +3 -0
- package/docs/api/openWorld.md +2 -1
- package/docs/api/particles.md +2 -2
- package/docs/api/photo.md +0 -2
- package/docs/api/pickups.md +15 -0
- package/docs/api/post.md +5 -4
- 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 +11 -44
- package/docs/api/stream.md +1 -1
- package/docs/api/style.md +31 -29
- package/docs/api/terrain.md +9 -6
- package/docs/api/terrainLook.md +10 -0
- package/docs/api/timeline.md +3 -4
- package/docs/api/tools.md +30 -13
- package/docs/api/triggers.md +5 -10
- package/docs/api/vegetation.md +12 -8
- package/docs/api/vegetationLook.md +10 -0
- package/docs/api/water.md +4 -20
- package/docs/api/weather.md +5 -15
- package/docs/api/weatherLook.md +10 -0
- package/docs/api/world.md +2 -1
- package/docs/recipes/01-new-game.md +71 -59
- package/docs/recipes/02-npc.md +46 -29
- package/docs/recipes/03-items-pickups.md +38 -24
- package/docs/recipes/04-quests.md +34 -27
- package/docs/recipes/05-props-bake.md +154 -18
- package/docs/recipes/06-clock-events.md +100 -52
- package/docs/recipes/07-flow-scenes-pause.md +41 -10
- package/docs/recipes/08-weather-sky.md +146 -37
- package/docs/recipes/09-style.md +218 -0
- package/docs/recipes/10-blender-level.md +14 -2
- package/docs/recipes/11-vehicles.md +64 -7
- package/docs/recipes/13-animation.md +34 -10
- package/docs/recipes/14-custom-module.md +65 -34
- package/docs/recipes/15-perf.md +35 -6
- package/docs/recipes/16-dev-bridge.md +65 -41
- package/docs/recipes/18-world.md +21 -8
- package/docs/recipes/19-shooting-picking.md +12 -11
- package/docs/recipes/20-content-actions-save.md +63 -25
- package/docs/recipes/21-camera.md +49 -16
- package/docs/recipes/22-shop-gold.md +18 -18
- package/docs/recipes/23-ops.md +17 -11
- package/docs/recipes/26-ui-parts.md +61 -12
- package/docs/recipes/27-net.md +12 -6
- package/docs/recipes/28-terrain.md +35 -2
- package/docs/recipes/29-grid.md +71 -33
- package/docs/recipes/30-templates.md +64 -12
- package/docs/recipes/31-audio.md +20 -12
- package/docs/recipes/32-action-game.md +35 -2
- package/docs/recipes/34-environment-lighting.md +27 -4
- package/docs/recipes/35-style-from-reference.md +115 -51
- package/docs/recipes/36-sky-ground-plant-looks.md +37 -35
- package/docs/recipes/37-terrain-look-hooks.md +37 -15
- package/docs/recipes/38-environment-look-hooks.md +64 -48
- package/docs/recipes/40-scene.md +16 -13
- package/docs/recipes/41-thing-registry.md +11 -7
- package/docs/recipes/42-terrain-shape.md +3 -3
- package/docs/recipes/43-shader-patches-surface-looks.md +171 -74
- package/docs/recipes/44-water-weather-look-hooks.md +30 -21
- package/docs/recipes/45-cascaded-shadows.md +17 -8
- package/docs/recipes/46-blender-art-route.md +4 -4
- package/docs/recipes/47-island-snow-backdrop.md +11 -10
- package/docs/recipes/48-make-material.md +11 -6
- package/docs/recipes/49-game-module-contract.md +46 -33
- package/docs/recipes/50-own-renderer.md +2 -2
- package/docs/recipes/51-capability-catalog.md +4 -4
- package/docs/recipes/52-look.md +228 -54
- package/docs/recipes/53-ground-up-close.md +58 -0
- package/docs/recipes/54-three-idioms.md +88 -0
- package/docs/recipes/55-mods-time-wrap.md +112 -0
- package/docs/recipes/56-webgpu-tsl.md +260 -0
- package/docs/recipes/57-control.md +65 -0
- package/docs/recipes/58-input.md +57 -0
- package/docs/recipes/59-depth-post-effects.md +83 -0
- package/docs/recipes/60-judge-the-picture.md +55 -0
- package/examples/action/.threedog/content-index.json +1 -0
- package/examples/action/GAMEMAP.md +17 -13
- 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/look.js +1 -1
- package/examples/action/main.js +10 -13
- package/examples/action/models.js +24 -23
- package/examples/pullable.json +111 -0
- package/examples/rc2d/.threedog/content-index.json +1 -0
- package/examples/rc2d/GAMEMAP.md +83 -0
- package/examples/rc2d/README.md +52 -0
- package/examples/rc2d/main.js +63 -0
- package/examples/rc2d/rc2d.js +197 -0
- package/examples/rc2d/shaders.js +144 -0
- package/examples/sheepisland/.threedog/content-index.json +1 -0
- package/examples/sheepisland/GAMEMAP.md +13 -11
- package/examples/sheepisland/assets.js +5 -0
- package/examples/sheepisland/main.js +17 -12
- package/examples/sheepisland/style/index.js +6 -6
- package/examples/styletest/.threedog/content-index.json +1 -0
- package/examples/styletest/GAMEMAP.md +15 -16
- package/examples/styletest/assets.js +6 -0
- package/examples/styletest/content/npcs.js +1 -1
- package/examples/styletest/content/templates.js +1 -1
- package/examples/styletest/content/vegetation.js +1 -1
- package/examples/styletest/look.js +1 -1
- package/examples/styletest/main.js +18 -17
- package/examples/styletest/models.js +1 -1
- package/examples/styletest/params.js +3 -3
- package/examples/techniques/contact-shadows/README.md +92 -0
- package/examples/techniques/contact-shadows/contactShadows.js +51 -0
- package/examples/techniques/contact-shadows/main.js +53 -0
- package/examples/techniques/day-night-grade/README.md +86 -0
- package/examples/techniques/day-night-grade/dayNightGrade.js +76 -0
- package/examples/techniques/day-night-grade/main.js +51 -0
- package/examples/techniques/interior-light/README.md +103 -0
- package/examples/techniques/interior-light/interiorLight.js +45 -0
- package/examples/techniques/interior-light/main.js +124 -0
- package/examples/techniques/modular-building/README.md +125 -0
- package/examples/techniques/modular-building/kit.js +426 -0
- package/examples/techniques/modular-building/main.js +47 -0
- package/examples/techniques/night-lights/README.md +94 -0
- package/examples/techniques/night-lights/main.js +70 -0
- package/examples/techniques/night-lights/nightLights.js +91 -0
- package/examples/techniques/outlines-and-rim/README.md +59 -0
- package/examples/techniques/outlines-and-rim/main.js +84 -0
- package/examples/techniques/outlines-and-rim/outlines.js +64 -0
- package/examples/techniques/procedural-animation/README.md +77 -0
- package/examples/techniques/procedural-animation/creature.js +44 -0
- package/examples/techniques/procedural-animation/main.js +45 -0
- package/examples/techniques/procedural-animation/proceduralAnim.js +118 -0
- package/examples/techniques/procedural-surface/README.md +67 -0
- package/examples/techniques/procedural-surface/main.js +103 -0
- package/examples/techniques/procedural-surface/surface.js +120 -0
- package/examples/techniques/stylized-shading/README.md +75 -0
- package/examples/techniques/stylized-shading/main.js +93 -0
- package/examples/techniques/stylized-shading/shading.js +131 -0
- package/examples/techniques/sun-and-key-light/README.md +135 -0
- package/examples/techniques/sun-and-key-light/keyLight.js +53 -0
- package/examples/techniques/sun-and-key-light/main.js +95 -0
- package/examples/techniques/wet-road/README.md +99 -0
- package/examples/techniques/wet-road/main.js +68 -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 +84 -0
- package/examples/webgpu-tsl/README.md +42 -0
- package/examples/webgpu-tsl/main.js +21 -0
- package/examples/webgpu-tsl/tslScene.js +75 -0
- package/examples/world/.threedog/content-index.json +1 -0
- package/examples/world/GAMEMAP.md +22 -15
- package/examples/world/art/humanoid.js +42 -0
- package/examples/world/art/index.js +11 -0
- package/examples/world/art/models.js +58 -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/npcs.js +2 -2
- 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 +17 -13
- package/examples/world/style/index.js +61 -0
- package/examples/world/style/plugins/10-bloom.js +5 -0
- package/index.d.ts +50 -45
- package/index.js +27 -22
- package/llms.txt +20 -12
- 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 +81 -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 +70 -19
- package/modules/camera/contract.js +17 -0
- package/modules/camera/projection.js +11 -24
- package/modules/characters/Character.js +196 -59
- package/modules/characters/NPC.js +87 -36
- package/modules/characters/assetModel.js +27 -8
- 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 +87 -49
- package/modules/characters/moveHelpers.js +4 -5
- package/modules/characters/movers/common.js +97 -19
- package/modules/characters/movers/manual.js +20 -0
- package/modules/characters/movers/rapier.js +19 -16
- package/modules/characters/movers/simple.js +14 -11
- package/modules/characters/movers/support.js +172 -0
- package/modules/characters/npcAvoid.js +47 -42
- package/modules/characters/npcSim.js +12 -8
- 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 +46 -10
- package/modules/characters/useModel.js +1 -1
- package/modules/characters/vehicle.contract.js +21 -0
- package/modules/characters/walkTo.js +21 -7
- package/modules/collision/Collision.js +65 -25
- 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 +97 -28
- package/modules/content/loader.js +45 -33
- package/modules/content/models.js +11 -40
- package/modules/content/packs.js +6 -5
- package/modules/content/parts.js +41 -0
- package/modules/content/props.js +16 -9
- package/modules/content/rules.js +58 -0
- package/modules/content/stages.js +80 -0
- package/modules/content/templates.js +121 -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 +193 -67
- package/modules/gameplay/Inventory.js +69 -73
- 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 +18 -16
- package/modules/gameplay/pickups.js +268 -85
- package/modules/gameplay/questLog.js +4 -4
- package/modules/gameplay/quests.contract.js +17 -0
- package/modules/grid/Grid.js +92 -50
- package/modules/grid/Layer.js +14 -13
- package/modules/grid/index.js +60 -36
- package/modules/grid/ops.js +20 -18
- package/modules/grid/preview.js +15 -16
- package/modules/grid/strings.js +12 -0
- package/modules/grid/tiles.js +6 -5
- package/modules/hlod/index.js +51 -19
- package/modules/hud/index.js +35 -20
- package/modules/hud/parts.js +20 -7
- package/modules/level/heightfield.js +3 -1
- package/modules/level/index.js +45 -20
- package/modules/level/nodes.js +4 -3
- package/modules/lights/index.js +42 -18
- 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 +23 -14
- package/modules/particles/contract.js +17 -0
- package/modules/particles/effect.js +39 -11
- package/modules/photo/Photo.js +53 -36
- 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 +57 -52
- 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 +88 -176
- package/modules/sky/cascades.js +86 -25
- package/modules/sky/cascades.tsl.js +82 -0
- package/modules/sky/clouds.js +34 -19
- package/modules/sky/domeLayers.js +30 -15
- package/modules/sky/environment.js +83 -13
- package/modules/sky/fog.js +140 -95
- package/modules/sky/frame.js +112 -44
- package/modules/sky/look.js +22 -19
- package/modules/sky/lookFog.js +48 -52
- package/modules/sky/lookShadow.js +53 -41
- package/modules/sky/lookSky.js +82 -21
- package/modules/sky/season.js +21 -0
- package/modules/sky/shader.js +41 -19
- package/modules/sky/shadow.js +44 -20
- package/modules/sky/skyModule.js +45 -33
- package/modules/sky/tint.js +61 -0
- package/modules/terrain/ChunkManager.js +175 -108
- package/modules/terrain/chunkJob.js +1 -1
- package/modules/terrain/chunkQueries.js +6 -4
- package/modules/terrain/index.js +105 -50
- package/modules/terrain/look.js +83 -0
- package/modules/terrain/lookGround.js +21 -16
- package/modules/terrain/terrainDefaults.js +1 -1
- package/modules/terrain/terrainEdit.js +18 -20
- package/modules/terrain/terrainGen.js +5 -3
- 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 +107 -76
- package/modules/vegetation/Vegetation.js +190 -102
- package/modules/vegetation/densityMap.js +62 -0
- package/modules/vegetation/groundChunks.js +52 -58
- package/modules/vegetation/index.js +31 -10
- package/modules/vegetation/look.js +119 -0
- package/modules/vegetation/lookWind.js +71 -10
- package/modules/vegetation/modelGeometry.js +3 -2
- package/modules/vegetation/nearFade.js +45 -0
- package/modules/water/Water.js +65 -27
- package/modules/water/look.js +29 -18
- package/modules/water/surface.js +64 -25
- package/modules/water/surfaceDefaults.js +3 -0
- package/modules/weather/Weather.js +62 -104
- package/modules/weather/look.js +1 -2
- package/modules/weather/particles.js +24 -9
- package/modules/weather/strings.js +2 -2
- package/modules/weather/weatherLook.js +135 -0
- package/modules/world/World.js +24 -7
- 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 +7 -5
- package/package.json +8 -3
- package/presets.js +41 -27
- package/render/PostFX.js +145 -120
- package/render/backend.js +440 -0
- package/render/detail.js +150 -45
- package/render/effects.js +130 -38
- package/render/engineUniforms.js +10 -1
- package/render/fuse.js +5 -4
- package/render/fx/cloudShadow.tsl.js +16 -0
- package/render/fx/detail.tsl.js +66 -0
- package/render/fx/index.js +26 -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 +158 -0
- package/render/fx/season.tsl.js +13 -0
- package/render/fx/sky.tsl.js +260 -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/lighting.js +66 -14
- package/render/lights.js +223 -4
- package/render/lookGlow.js +139 -0
- package/render/lookSurface.js +99 -36
- package/render/materials.js +71 -52
- package/render/modeling.js +73 -0
- package/render/ownFrame.js +76 -0
- package/render/patches.js +311 -34
- package/render/postLook.js +124 -46
- package/render/postWebGL.js +197 -0
- package/render/probes.js +231 -0
- package/render/shaderErrors.js +154 -0
- package/render/slots.js +165 -0
- package/render/surface.js +53 -17
- package/render/terms.js +211 -0
- package/render/uniformDecl.js +3 -2
- package/types/common.d.ts +2 -2
- package/types/core.d.ts +620 -244
- package/types/gameplay.d.ts +494 -318
- 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 +44 -3
- package/types/generated/core/I18n.d.ts +2 -2
- package/types/generated/core/Input.d.ts +102 -31
- 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/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 +5 -5
- package/types/generated/modules/grid/index.d.ts +29 -17
- 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 +1 -1
- package/types/generated/modules/sky/Sky.d.ts +12 -104
- package/types/generated/modules/sky/skyModule.d.ts +1 -1
- package/types/generated/modules/timeline/Timeline.d.ts +8 -6
- package/types/generated/modules/weather/Weather.d.ts +17 -18
- package/types/generated/modules/weather/weatherLook.d.ts +12 -0
- package/types/generated/modules/world/World.d.ts +18 -3
- package/types/generated/render/PostFX.d.ts +67 -30
- package/types/generated/render/materials.d.ts +26 -20
- package/types/generated/render/modeling.d.ts +30 -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 +137 -44
- package/types/style.d.ts +125 -92
- package/types/tsl.d.ts +86 -0
- package/types/world.d.ts +164 -110
- package/ui/Focus.js +25 -14
- package/ui/LoadingScreen.js +1 -1
- package/ui/Parts.js +27 -11
- package/ui/UI.js +20 -10
- package/ui/cursor.js +115 -0
- package/ui/defaults.js +15 -11
- package/ui/theme.js +9 -6
- package/ui/touchPart.js +1 -1
- package/core/claims.js +0 -51
- package/core/look/layers.js +0 -136
- package/core/renamed.js +0 -21
- package/docs/api/entities.md +0 -61
- package/docs/recipes/09-style-pack.md +0 -234
- 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/layers.js +0 -69
- package/modules/terrain/chunkLevels.js +0 -40
- package/modules/terrain/mountTerrain.js +0 -32
- package/modules/world/cells.js +0 -42
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# The ground up close: options and diagnosis
|
|
2
|
+
|
|
3
|
+
Not a standard recipe. Several looks are right for different games, and none is the default. **Decide from the reference images first**: is the
|
|
4
|
+
ground flat color shapes, a painted surface, a photographic texture, or a few lit polygons? Then pick a row. If the ground already looks wrong,
|
|
5
|
+
jump to the symptoms.
|
|
6
|
+
|
|
7
|
+
## Approaches (pick by the look you want)
|
|
8
|
+
| Look | How | Typical values |
|
|
9
|
+
|---|---|---|
|
|
10
|
+
| Flat color blocks, no detail (minimal, flat illustration; on purpose) | `shading: 'flat'` or `'smooth'`, `colors` / `biomeColors`, no `detail`; a few `variation.patches` with `levels` | `resolution: 16`; `patches: { size: 90, levels: 3, strength: 0.15 }` |
|
|
11
|
+
| Color map + detail layer + near grass (soft variation, crisp borders) | `terrain({ shading: 'texture', colorResolution })`, `style.terrain.detail`, grass with `vegetation.define(name, { nearOnly: true })` | `colorResolution: 128`; `detail: { noise: 'dabs', scale: 0.5, strength: 0.25, normal: 0.03 }` |
|
|
12
|
+
| Tiling or triplanar textures (your own painted, photographed or generated images) | material spec `map` + `repeat`, or `detail: { texture: 'name' }` (`projection: 'triplanar'` on slopes and rocks) | `repeat: 8`; `detail: { texture, scale: 0.5, strength: 0.6 }` |
|
|
13
|
+
| Vertex color + toon lighting | vertex colors from `colors` and `variation.jitter`, `type: 'toon'` with a `ramp`; detail optional | `jitter: { size: 2, min: 0.9, range: 0.15 }`; `ramp: [0.4, 0.75, 1]` |
|
|
14
|
+
| Your own shader or plain three.js | `terrain({ material })`, or recipe 40 section 8 (what you give up) | any |
|
|
15
|
+
|
|
16
|
+
Detail noises (recipe 43): `grain` fine speckle, `paper` fibres, `stroke` directional streaks (stretched on purpose), `dabs` soft oval dabs with
|
|
17
|
+
no direction. `fade` (on by default) removes the detail where its pattern is smaller than a pixel. At run time: `look.ground.set`,
|
|
18
|
+
`look.surface.set({ detail })`, `look.surface.patch('detail', glsl)` (recipe 52).
|
|
19
|
+
|
|
20
|
+
Where each key is defined: `variation` (blend, jitter, patches) recipe 36; `shading`, `colorResolution`, `normals`, `normalFn`, heights from a
|
|
21
|
+
grid recipe 37; `detail` keys recipe 43; run-time changes `look.ground` recipe 52; terrain basics recipe 28.
|
|
22
|
+
|
|
23
|
+
## Symptom, causes, options
|
|
24
|
+
**Brushed metal at grazing angles.** Cause: `stroke` stretches one axis (`stretch` 6) and `world` projection looks straight down, so a flat
|
|
25
|
+
ground seen at a low angle shows the stretch as long streaks. Options: `noise: 'dabs'` or `'grain'` (no stretch); `stretch: 1`; a smaller
|
|
26
|
+
`strength`; `projection: 'triplanar'` only on slopes; no detail at all if the reference is flat.
|
|
27
|
+
|
|
28
|
+
**Slopes show terraces or creases.** Causes: heights from sparse data with linear (bilinear) interpolation show the data grid as kinks along
|
|
29
|
+
its cell lines (`terrain({ heightmap })` always reads its image bilinearly; `sampleGrid` defaults to `interp: 'linear'`); a coarse mesh
|
|
30
|
+
`resolution` (16 per 64 m chunk = one height every 4 m); `shading: 'flat'`, which lights each triangle on its own so every facet shows.
|
|
31
|
+
Options, any of them: more height samples (a larger image or table), or the data smoothed before the terrain reads it; `sampleGrid(...,
|
|
32
|
+
{ interp: 'cubic' })` for smooth heights everywhere outside the terrain module (placement, gameplay; recipe 37 section 7; a terrain `height`
|
|
33
|
+
function runs in a worker and cannot capture a sampler); a finer `resolution` (more triangles, collision and edit cells); `shading: 'smooth'`,
|
|
34
|
+
or `normals: 'smooth'` with `shading: 'texture'`; `diagonal: 'adaptive'` against zigzag ridges; or noise in the `height` function (`n.fbm`; ridges from
|
|
35
|
+
folding it) instead of a sparse table. Terraces can also be the look you want: `interp: 'nearest'`, or a stepped `height`.
|
|
36
|
+
|
|
37
|
+
**Blurry up close.** Cause: the color map is `colorResolution + 2` pixels per 64 m chunk side; 128 is 0.5 m per pixel, which is soft when the
|
|
38
|
+
camera is within a few meters. It does not depend on the mesh `resolution`. On a `low` tier the default is 64 (twice as coarse) unless you set
|
|
39
|
+
it. Options: `colorResolution: 256` (0.25 m; four times the image memory and worker time); a detail layer on top (it is computed per screen
|
|
40
|
+
pixel, so it stays sharp); near grass over the ground; or accept soft color and let a texture carry the fine part.
|
|
41
|
+
|
|
42
|
+
**One flat green.** Cause: not resolution. Color comes from `colors` / `biomeColors` per ground type (`groundType`), `variation.blend`
|
|
43
|
+
(wavelength and `strength` of the two-color noise) and `steep`. One color with no `biomeColors` and a flat blend is one green. Options: two
|
|
44
|
+
colors in `biomeColors` for the type; `blend.strength` 1.5; `variation.patches` with a `tint`; more ground types.
|
|
45
|
+
|
|
46
|
+
**Dark blotches.** Cause: usually cloud shadows (`style.clouds.shadow` above 0), not texture; they drift with `speed`, and bigger clouds mean
|
|
47
|
+
bigger blotches. Test by setting `clouds.shadow: 0` (live: `look.cloudShadow`); if they vanish, lower it or change `scale`. Blotches that
|
|
48
|
+
never move are `variation.patches` or a strong `detail`.
|
|
49
|
+
|
|
50
|
+
**Shimmer far away.** Cause: a pattern smaller than a pixel aliases. `detail.fade` (default) fades it out; `fade: false` keeps it. In your own
|
|
51
|
+
`glsl`, read `threedogDetailFootprint` (one pixel in pattern units; 1 = one cell per pixel) and fade yourself. A `variation.jitter.size` below a
|
|
52
|
+
pixel shimmers the same way; use a few meters, or `shading: 'texture'` (mipmapped).
|
|
53
|
+
|
|
54
|
+
**Near-only vegetation fades out by distance.** Any `nearOnly` species (grass, flowers, pebbles, twigs, snow tufts, debris ...) is placed per chunk ring
|
|
55
|
+
(`maxRing`), whose edge would be a visible line. By default instances shrink toward their base over a band that ends at `maxRing x chunk size` from the
|
|
56
|
+
player (start at 65% of it), so nothing pops per chunk. Tune with `fade: { start: 20, end: 45 }` (metres), or `fade: false` for a hard edge
|
|
57
|
+
(e.g. a species hidden in fog anyway). It is part of the species' own material (no extra draw call or program), keeps wind sway and shadows in step, and is skipped
|
|
58
|
+
when `effects: false`. A larger `maxRing`, fog, or a ground color near the species' base color still help for stylized looks with strong silhouettes.
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Plain three.js in this engine
|
|
2
|
+
|
|
3
|
+
```js
|
|
4
|
+
engine.scene.add(new THREE.PointLight(0xffaa55, 2, 8)); // anything the engine does not write: just do it
|
|
5
|
+
engine.lights.sun.intensity = 3; // an engine-driven value: this write takes it over (warns once)
|
|
6
|
+
engine.look.lights.manual('sun'); // the same takeover said on purpose (no warning); manual('sun', false) gives it back
|
|
7
|
+
engine.look.sky.replace(mySky); // the one three.js thing a part draws: your sky (or env map, material role, post chain)
|
|
8
|
+
engine.onRender(({ renderer, scene, camera }) => { /* render into your own target */ });
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The engine is three.js underneath: `engine.renderer`, `engine.scene` and `engine.camera` are the real objects, and plain three.js code works on them.
|
|
12
|
+
What you need to know is which values the engine **writes on its own** (every frame or on a timer) and how to take those over. A direct write to
|
|
13
|
+
one of them hands it to you (the engine stops writing it and warns once with the explicit call); the full list is recipe 52 "Who writes it".
|
|
14
|
+
Everything else: just do it.
|
|
15
|
+
|
|
16
|
+
Rule of thumb: write the three.js first; it works (auto-yield: the engine stops writing a value you wrote, warns once). `renderer.toneMapping =
|
|
17
|
+
THREE.ACESFilmicToneMapping` is a complete answer. The engine route is optional on top: `engine.look.<part>` (recipe 52) adds owners, layers and
|
|
18
|
+
keyframes. `set(...)` tunes and keeps the engine writing; `manual(key)` takes a value without the warning (`manual(key, false)` gives it back);
|
|
19
|
+
`replace(...)` hands a whole part your own object. `engine.describe().look` says, per value, who set it last; `describe().owned` who writes each
|
|
20
|
+
engine-driven value now.
|
|
21
|
+
|
|
22
|
+
| three.js idiom | Where it goes in the engine | Engine overwrites it? | How to take it over |
|
|
23
|
+
|---|---|---|---|
|
|
24
|
+
| `renderer.toneMapping` | Just set it: `engine.renderer.toneMapping = THREE.ACESFilmicToneMapping` (any three mapping). A curve of your own: a post stage in the `'tone'` slot, `engine.look.post.add({ name, slot: 'tone', create, node })`, which runs on both renderers (a `ShaderChunk` edit is page-wide and reaches only the default renderer) | Once, at `createEngine` (`style.toneMapping`, else AgX), never again | Nothing to take: your write stays; `describe().render.toneMapping.writer` says `game` |
|
|
25
|
+
| `renderer.toneMappingExposure` | Just set it, or `engine.look.tone.set({ exposure })` / `style.exposure` for owners, layers and day keyframes | Exposure keyframes (`style.exposure` as a list or function) are written every frame by the sky module; a number once | Your direct write takes it (auto-yield; describe tags it `three`). `look.tone.manual('exposure')` stops the keyframes without the warning. Own the whole renderer: `createEngine({ renderer })` (recipe 50) |
|
|
26
|
+
| `renderer.outputColorSpace` | Leave it at `SRGBColorSpace` (three's default) | No | Changing it makes every color wrong; `assets.report` flags it |
|
|
27
|
+
| `renderer.shadowMap` | `look.shadow.set({ enabled, type })` (a sky adds cascades, extent ...) | `enabled` / `type` on a change of look.shadow; the sky manages `needsUpdate` | Write it: the engine yields; or `createEngine({ renderer })` and run your own |
|
|
28
|
+
| `scene.environment` | A generated neutral room map under every style (`look.lights` `env.intensity` = its absolute intensity, 0 = none; under a sky `env.share` = its share of the ambient). With a sky: `look.env.replace('sky' \| assetName \| false)` | The baseline map is only written while `scene.environment` is empty or its own. A sky `'sky'` environment is rebuilt and reassigned | Assign your own `scene.environment`: with a sky the rebuilds stop (or say it first: `look.env.manual()`). Or hand it over: `look.env.replace(myTexture | myCubeTexture | myPmremTarget)` |
|
|
29
|
+
| `scene.background` | Style `background`, or `look.sky.replace(name \| Texture \| Object3D)` with a sky | The sky dome draws over it; a backdrop / skybox writes it every frame | Assign it: the dome hides and the sky stops writing it (`look.sky.manual('background')` says so first; `manual('background', false)` gives it back), or `look.sky.replace(yourTexture)` |
|
|
30
|
+
| `scene.fog` | `look.fog.set(...)` with a sky | The sky writes it every frame until you write it | Assign it: `scene.fog = new THREE.Fog(...)` (the engine yields; the one route, there is no `look.fog.replace`), or `look.fog.manual()` to keep the engine's fog and edit it yourself; `manual(false)` gives it back |
|
|
31
|
+
| Your own sky (three/addons `Sky`, a ShaderMaterial dome, a skybox) | `engine.look.sky.replace(mySky)`; feed it from `engine.uniforms` (`uSunDir`, `uDaylight`, `uTimeOfDay`, `uWind`, `uCloud`) | Never: the engine only hides its dome and adds `mySky` to the scene when it has no parent; time, lights, fog and weather keep running | `look.sky.replace(null)` gives the dome back. Per key instead of whole: `look.sky.set({ top: ({ state }) => ... })`, state from `engine.look.state({ ... })` |
|
|
32
|
+
| Lights (`DirectionalLight`, `PointLight`, ...) | Add them with `engine.scene.add(light)` before `engine.start()` (a light count change mid-play recompiles shaders; `lightPool()` for many) | Only `engine.lights.sun` / `.hemi` (driven by the sky when installed) | Write them directly (the sky stops writing what you wrote), or say so: `look.lights.manual('sun' \| 'sun.color' \| 'sun.direction' \| 'hemi')`; `look.lights.set(...)` keeps the day cycle; `sky({ sun: false })` or `createEngine({ lights: false })` and bring all your own |
|
|
33
|
+
| Shadow camera (`light.shadow.camera`, `mapSize`, `bias`) | Your own lights: just do it. Engine sun: `look.shadow.set(...)` (sky) | The engine sun's shadow box follows `engine.focus` every frame | `look.shadow.set({ follow: 'focus' \| 'camera' \| object3D \| fn \| false, extent })`, or place `sun.target` yourself (the box stops following). With `cascades > 1` built-in lit materials get the `csm` patch; a lit ShaderMaterial of yours joins with `look.shadow.include(mat)` |
|
|
34
|
+
| Materials (`new MeshStandardMaterial(...)`) | Just do it, or `makeMaterial('role', { color })` to get the style's material for a role | Your own materials: no. Role materials: `look.materials.set` live keys are written onto every material of the role | `look.materials.replace('role', material)` for a role. A key the `makeMaterial` call passed, or a value you set by hand, is never overwritten (recipe 52) |
|
|
35
|
+
| `onBeforeCompile`, `customProgramCacheKey` | Just assign them, on any material: `m.onBeforeCompile = s => { ... }`. A named, checked patch: `applyEffects(m, { patches: [...] })` | Nothing: on a material the engine patched (wind, clouds, season), your function runs first and the patches after it; your cache key is joined in front of theirs | - |
|
|
36
|
+
| Textures, `CanvasTexture`, `DataTexture` | Just do it; set `colorSpace = SRGBColorSpace` on color textures. File textures by name: `engine.assets.manifest({ textures })` then `assets.texture(name)` | No | A texture made in code gets a name too: `assets.manifest({ textures: { brick: () => makeBrick(), sign: mySignTexture } })`, then `map: 'brick'`; it keeps its own repeat / wrap / colorSpace. Baking: see recipe 05 |
|
|
37
|
+
| `EffectComposer` passes | `const h = engine.look.post.add(pass)` (`composer.addPass`; `{ order }` for `insertPass`; `h.remove()` for `removePass`): the pass joins the engine chain after the scene render, before tone mapping (FXAA / SMAA last; a pass that renders the scene replaces the scene render); a settings toggle: `add(pass, { name: 'glow' })`, then `look.post.set({ glow: false })` | The engine renders the main frame through its own composer; a composer of your own rendered to the canvas takes the frame over (the engine stops drawing, warns `[own-frame]`; delete the call to hand it back) | `look.post.replace([...effects])` for the whole chain. To read them: `engine.post.object()` (the EffectComposer, null until post first runs) and `engine.post.targets()` (`renderTarget1 / 2`, `readBuffer`, `writeBuffer`; read in `onRender(fn, { when: 'after' })`). Read-only: the engine rebuilds `passes`, resizes and disposes them |
|
|
38
|
+
| Render targets (mirror, minimap, portal, picking) | `engine.onRender(({ renderer, scene, camera, dt }) => { renderer.setRenderTarget(rt); renderer.render(scene, cam); })`; runs after the camera and batching, before the frame (`{ when: 'after' }` after it) | No; the render target is reset to the screen after each `before` callback | The main frame render itself stays the engine's |
|
|
39
|
+
| Loaders (`GLTFLoader`, `TextureLoader`, `EXRLoader`, `FBXLoader`, `FontLoader`, exporters) | glTF and png / jpg / ktx2 / hdr textures: `engine.assets.manifest(...)` gives them names, the loading screen and `describe` | No | A hand-made `GLTFLoader` / `TextureLoader` works (rule R21 warns: the asset gets no name). Formats the manifest cannot load (EXR, FBX, OBJ, typeface fonts) and exporters: just use the three.js addon |
|
|
40
|
+
| Camera (`fov`, `position`, `far`) | Camera modules (`followCamera()`, ...) or `engine.camera` with none installed | An installed camera module writes position / fov every frame; terrain view distance rewrites `camera.far` | `engine.takeCamera('me')` (gives it back on `remove()`), or just write `camera.position` (the rig stops; `look.view.manual('camera', false)` resumes). `camera.far` / `fov`: `look.view.manual('far' \| 'fov')`. Not yet: no slot to swap in a different camera object |
|
|
41
|
+
| Meshes (`scene.add(mesh)`) | `engine.add(mesh, { id })` (tracked, saveable) or plain `engine.scene.add(mesh)` | No: auto-batching only merges what the engine places (world records, stream builds, level files); your own meshes stay as you made them | Many static copies of your own mesh: `mesh.userData.batch = true` before adding (recipe 05, 2b) |
|
|
42
|
+
| `requestAnimationFrame` loop | `engine.onFrame(dt => ...)`, `engine.onFixed(dt => ...)` | The engine runs the only loop | Do not start a second loop |
|
|
43
|
+
|
|
44
|
+
Also not yet: contact shadows. WebGPU and TSL: recipe 56.
|
|
45
|
+
|
|
46
|
+
## Procedural modeling: what three.js already has
|
|
47
|
+
|
|
48
|
+
Models written in code (recipe 05, 2a) mostly need helpers three.js already ships: import them from `three` or `three/addons/...` inside the
|
|
49
|
+
model generator, so the result is cached and baked with the model. The engine adds only what three lacks (last row).
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import * as THREE from 'three';
|
|
53
|
+
import { mergeGeometries, mergeVertices } from 'three/addons/utils/BufferGeometryUtils.js';
|
|
54
|
+
import { MeshSurfaceSampler } from 'three/addons/math/MeshSurfaceSampler.js';
|
|
55
|
+
import { SimplexNoise } from 'three/addons/math/SimplexNoise.js';
|
|
56
|
+
import { bakeVertexAO } from 'threedog';
|
|
57
|
+
|
|
58
|
+
const rng = engine.random.seeded(7), simplex = new SimplexNoise({ random: () => rng.next() }); // seeded noise: the same shape every load
|
|
59
|
+
// weld first, or the noise opens cracks; mergeVertices compares every attribute, so drop normal and uv (their seams would stay unwelded)
|
|
60
|
+
const rock = mergeVertices(new THREE.IcosahedronGeometry(1, 3).deleteAttribute('normal').deleteAttribute('uv'));
|
|
61
|
+
const p = rock.attributes.position, v = new THREE.Vector3();
|
|
62
|
+
for (let i = 0; i < p.count; i++) { v.fromBufferAttribute(p, i); v.multiplyScalar(1 + 0.25 * simplex.noise3d(v.x, v.y, v.z)); p.setXYZ(i, v.x, v.y, v.z); }
|
|
63
|
+
rock.computeVertexNormals();
|
|
64
|
+
const slab = new THREE.CylinderGeometry(1.2, 1.4, 0.3).translate(0, -0.9, 0).deleteAttribute('uv');
|
|
65
|
+
const model = mergeGeometries([rock, slab]); // same attributes (position, normal), both indexed
|
|
66
|
+
bakeVertexAO(model, { samples: 32, strength: 0.7, occluders: [new THREE.PlaneGeometry(6, 6).rotateX(-Math.PI / 2).translate(0, -1.05, 0)] });
|
|
67
|
+
const sampler = new MeshSurfaceSampler(new THREE.Mesh(model)).setRandomGenerator(() => rng.next()).build();
|
|
68
|
+
const at = new THREE.Vector3(), up = new THREE.Vector3();
|
|
69
|
+
sampler.sample(at, up); // a point and its normal on the surface: where a tuft or stud goes
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
| Need | three.js answer | Engine |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| Parts into one geometry / one draw call | `BufferGeometryUtils.mergeGeometries(list, useGroups)` (same attributes on every part, all indexed or none); `BufferGeometryUtils.mergeVertices(g, tolerance)` welds; `BufferGeometryUtils.toCreasedNormals(g, angle)` keeps hard edges with smooth faces; `mergeGroups` | `mergeColored([[g, color], ...])` when each part is one flat color (vertex colors) |
|
|
75
|
+
| Revolve a profile (vase, bottle, shade) | `new LatheGeometry(points, segments)` | - |
|
|
76
|
+
| Sweep along a path (pipe, rope, rail, vine) | `new TubeGeometry(curve, ...)`; a `Shape` along a path: `new ExtrudeGeometry(shape, { extrudePath: curve, steps })`; sections that change: `new LoftGeometry(sections)` (three/addons geometries); bend a finished mesh along a curve: `Flow` (three/addons CurveModifier) | - |
|
|
77
|
+
| A 2D outline into a slab (sign, cutout, wall with holes) | `Shape` with `shape.holes`, `new ExtrudeGeometry(shape, { depth, bevelEnabled })`; flat: `ShapeGeometry` | - |
|
|
78
|
+
| Rounded box, parametric surface, 3D text | `RoundedBoxGeometry`, `ParametricGeometry`, `TextGeometry` with `FontLoader` (addons) | - |
|
|
79
|
+
| Points spread over a mesh (moss, studs, spikes, ornaments) | `new MeshSurfaceSampler(mesh).setWeightAttribute(name).setRandomGenerator(fn).build()`, then `.sample(position, normal, color, uv)` | Over the ground by height, slope and biome rules: `engine.vegetation.define(name, { rule })` (recipe 10) |
|
|
80
|
+
| A decal projected onto a mesh (stain, sticker, scorch) | `new DecalGeometry(mesh, position, orientation, size)`, material with `polygonOffset` | - |
|
|
81
|
+
| Convex hull of points (pebble, crystal, collision proxy) | `new ConvexGeometry(points)`; `ConvexHull` for the faces as data | - |
|
|
82
|
+
| Fewer triangles (a far copy of one model) | `await new SimplifyModifier().modify(geometry, count)` (async in this three version) | Far copies of whole areas: `hlod` (recipe 15) |
|
|
83
|
+
| More triangles (before noise or vertex AO) | `new TessellateModifier(maxEdge, iterations).modify(g)`, or more segments in the primitive | - |
|
|
84
|
+
| Hard edges on a smooth mesh | `new EdgeSplitModifier().modify(g, angle)` or `BufferGeometryUtils.toCreasedNormals` | - |
|
|
85
|
+
| Noise (lumpy rock, wobbly edge, color variation) | `new SimplexNoise({ random })` (2D, 3D, 4D; seeded as above); `ImprovedNoise` (3D Perlin, one fixed permutation) | Terrain heights: `@threedog/terrain-noise` (recipe 28) |
|
|
86
|
+
| A texture drawn in code (labels, stripes, gradients, pixel art) | `new CanvasTexture(canvas)` with `colorSpace = SRGBColorSpace`; `DataTexture` | Give it a name: `assets.manifest({ textures })` (table above) |
|
|
87
|
+
| Many copies | `new InstancedMesh(g, m, count)` with `setMatrixAt`; `BatchedMesh` for different geometries | Auto-batching for copies the engine places, `userData.batch` for yours (recipe 05, 2b) |
|
|
88
|
+
| Ambient occlusion in a code-built model | Not in three: `aoMap` needs a baked texture | `bakeVertexAO(geometry, { samples, radius, strength, occluders })`: hemisphere rays per vertex (three-mesh-bvh), multiplied into the vertex colors; detail is per vertex, so tessellate big faces first |
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Shared values, exclusive slots, time channels and wrapping module methods
|
|
2
|
+
|
|
3
|
+
A shared value is something several owners change: the game, a module, an effect. Name it, say who you are, say how you change it:
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
const slow = engine.mod('time.enemies', { owner: 'bullet-time', mul: 0.35, for: 2 }); // bullet time; slow.remove() ends it early
|
|
7
|
+
engine.mod('time.world', { owner: 'hitstop', set: 0, layer: 'system', for: 0.06 }); // decide on a higher layer: hides everything below
|
|
8
|
+
|
|
9
|
+
const storm = () => ({ name: 'storm', type: 'rules', install(e, ctx) {
|
|
10
|
+
ctx.mod('time.world', { mul: 0.8, layer: 'effect' }); // inside a module: owned by it, gone when the module goes
|
|
11
|
+
} });
|
|
12
|
+
|
|
13
|
+
const myBag = () => ({ name: 'myBag', type: 'rules', provides: ['inventory'], install(e) { /* ... */ } }); // only one may hold 'inventory'
|
|
14
|
+
engine.use(myBag(), { replaces: 'inventory' }); // swap the engine's part: the old one leaves with all its changes
|
|
15
|
+
const shop = () => ({ name: 'shop', type: 'rules', requires: ['inventory'] }); // depend on the slot, not on a module name
|
|
16
|
+
|
|
17
|
+
engine.describe().shared['time.world']; // { value: 0.8, writer: 'default', how: 'default 1 × storm 0.8', also: [{ owner: 'storm', ... }] }
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
What to remember:
|
|
21
|
+
- **Layers combine by themselves.** The game sets a base value, effects (weather, sunset, bullet time) multiply on top, the player's settings cap
|
|
22
|
+
it. You never pick a layer by hand unless you want to: game code and game modules write `'game'` (the default); effects pass `layer: 'effect'`.
|
|
23
|
+
- **`mul` / `add` to cooperate, `set` to decide.** Any number of owners can multiply and add. Two owners `set` the same value on the same layer is
|
|
24
|
+
an error in development that names both lines and gives three copyable fixes; copy one. Release builds let the later write win and warn once.
|
|
25
|
+
- **`replaces` to swap an engine part** that `provides` a slot; a second provider without it is the error `shared-taken`.
|
|
26
|
+
- Every handle ends with `h.remove()` (`engine.mod`, `ctx.mod`, `input.gate`, `wrap`, `freeze`, `takeCamera`); it drops only your own entry.
|
|
27
|
+
|
|
28
|
+
Only things several parties commonly write are shared values: time speeds and the calendar rate, pause / freeze / camera holders, move stats,
|
|
29
|
+
input gates; the look values (sun and ambient light, fog, exposure, shadow on / quality, post parameters) move onto them next. Your own objects,
|
|
30
|
+
materials and textures are plain three.js: never registered, never a conflict. Which tool to reach for: `../CONVENTIONS.md` "Customization ladder".
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
engine.mod('time.player', { owner: 'haste', mul: 1.2, for: 5 }); // haste on the player
|
|
34
|
+
const menu = engine.freeze('pause-menu'); /* ... */ menu.remove(); // pause menu: the picture, UI and ambient uTime (wind, water, clouds) keep running; { ambient: true } stops those too
|
|
35
|
+
engine.input.gate('jump', { owner: 'stun', for: 1.5 }); // no jumping while stunned
|
|
36
|
+
engine.wrap(engine.inventory, 'add', (next, id, n = 1, o) => // a weight rule on top of the engine's inventory
|
|
37
|
+
carried() + weightOf(id) * n > 30 ? false : next(id, n, o), { owner: 'weight' });
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## The rules in full: `engine.mod(point, { owner, set | add | mul | clamp, layer?, for?, id? })`
|
|
41
|
+
|
|
42
|
+
| Layer (low to high) | Who writes it |
|
|
43
|
+
|---|---|
|
|
44
|
+
| `engine` | the engine's own defaults and drivers |
|
|
45
|
+
| `style` | style packs |
|
|
46
|
+
| `game` (default) | game code, game modules |
|
|
47
|
+
| `effect` | temporary effects: weather, sunset fade, bullet time, haste |
|
|
48
|
+
| `settings` | the player's settings menu: only `clamp` / `mul` (a value point: `set: false`), so it can turn things down or off, never on |
|
|
49
|
+
| `system` | cutscenes, menus, photo tools (also "lock this number": a `set` here hides every layer below) |
|
|
50
|
+
| `debug` | debug overrides and panels |
|
|
51
|
+
|
|
52
|
+
- Inside a layer: `set`, then every `add`, then every `mul`, then every `clamp` (`{ min, max }`); the result is clamped to the point's own min / max.
|
|
53
|
+
- One entry per owner; the same owner writing again replaces its own entry (never a conflict). Only the owner ends it: `remove()`, or `for` seconds.
|
|
54
|
+
- Nothing remembers an old value: every read recomputes from the base and the entries still there, so effects end in any order without fighting.
|
|
55
|
+
- Development builds print one console line when a value the game `set` is shown as another number because effects changed it ("'game' set
|
|
56
|
+
sun 3, shown 1.8 = game 3 × weather 0.6 ... to lock the number: ..."). That is the rule working, not a bug.
|
|
57
|
+
- `describe().shared` has one row per point with entries: `value`, `writer` (whose value the rest acts on; `'default'` when nobody set it),
|
|
58
|
+
`layer`, `at` (the code line, development builds), `how` (`'cave 180 × storm 0.6'`), `also` (everyone else; `hidden: true` when a set above hides
|
|
59
|
+
theirs). `engine.stacks.of('storm')` lists everything one owner changes. Per-record points read `'move.speed[player]'`.
|
|
60
|
+
- A direct three.js write of a property a point drives is never an error: whoever applies the point calls `point.yieldToThree()` and stops; the row
|
|
61
|
+
says `writer: 'three (engine yielded)'`; `point.reclaim()` hands it back.
|
|
62
|
+
- Base values are yours: `player.speed = 6` is always fine; modifiers go through `engine.mod('move.speed', { owner, id: 'player', mul })`.
|
|
63
|
+
- Your own point: `engine.stacks.define('damageTaken', { per: true, min: 0 })`, then `engine.stacks.get('damageTaken').value(enemyId)`;
|
|
64
|
+
`kind: 'value'` holds a color, a string or a flag (set only), `kind: 'one'` is an exclusive slot (what `provides` makes).
|
|
65
|
+
- Saved: `engine` to `effect` entries. Not saved: `settings` (the settings module re-applies the player's choices), `system`, `debug`.
|
|
66
|
+
|
|
67
|
+
## Time channels
|
|
68
|
+
|
|
69
|
+
- `engine.onFixed(dt => moveEnemies(dt), { channel: 'enemies' })`: dt is that channel's scaled dt. Without `channel` dt is real (1/60): cooldowns
|
|
70
|
+
and UI timers are not slowed by bullet time by accident.
|
|
71
|
+
- `engine.characters.createPlayer` drives the player on channel `'player'`; `new Character(engine, { channel })` picks one for other characters.
|
|
72
|
+
- `engine.world.simulate('slime', { near, far, channel: 'enemies' })`: near and far dt follow the channel too (NPCs: their own channel).
|
|
73
|
+
- `engine.clock.rate` is the calendar speed (fast-forward a day), separate from the channels.
|
|
74
|
+
- One time source: `engine.time.tick` is the only counter; `engine.time.now('real' | 'ambient' | 'calendar' | channel)` are read-only views of it
|
|
75
|
+
(recipe 06 "The time table"). Passing time is `engine.skip({ seconds })`: every view moves as if stepped, timers fire in order, far NPCs and
|
|
76
|
+
the calendar catch up. Writing `engine.clock.now` throws (it is a view).
|
|
77
|
+
- Timers: `engine.clock.after(s, fnOrEvent, data, { channel })` / `clock.every(s, fn, { channel })` on any view (default `'calendar'`; `'real'`
|
|
78
|
+
ignores bullet time; `'enemies'` slows with it). One queue: `remove()` takes effect at once, even later in the same step.
|
|
79
|
+
- `character.velocity` / `character.vy` are not stacks: a write is an impulse the mover integrates from (no owner, nothing to hand back). Lasting
|
|
80
|
+
knockback is `character.push(vx, vz, seconds)`; full takeover is `character.mover = 'manual'`.
|
|
81
|
+
|
|
82
|
+
## Fixed-step order: `onFixed(fn, { phase })`
|
|
83
|
+
|
|
84
|
+
`'early'` (default) runs before every module (the player has not moved yet this step); `'late'` runs after all modules and before the clock (hit
|
|
85
|
+
checks that need this step's player position). The order never depends on which line registered first.
|
|
86
|
+
|
|
87
|
+
## Wrapping a module method: `engine.wrap(target, method, (next, ...args) => ...)`
|
|
88
|
+
|
|
89
|
+
- Before: do something, then `return next(...args)`. After: `const r = next(...args); ...; return r`. Around: change the args, or skip `next`.
|
|
90
|
+
- Several wrappers compose: the last one wrapped runs first. `handle.remove()` takes one out; the method is restored when the last goes.
|
|
91
|
+
- Every caller of that method goes through it (the game, the module's own ops, other modules). It wraps one method: a rule for every way items
|
|
92
|
+
enter the bag wraps `add` and `transfer`.
|
|
93
|
+
- Only public methods (the module's `contract.js` `api` list); `_private` names throw. `describe().wraps` lists `{ 'inventory.add': [owners] }`.
|
|
94
|
+
|
|
95
|
+
## Replacing a module
|
|
96
|
+
|
|
97
|
+
Two modules that each want to be "the" one (a sky, a tone-mapping stage, an inventory) declare an exclusive slot: `provides: ['inventory']`.
|
|
98
|
+
Other modules `requires: ['inventory']` and work with whichever holds it. `engine.use(myInventory(), { replaces: 'inventory' })` unloads the
|
|
99
|
+
holder (its tracked handles, its `ctx.mod` entries, its look parts, its `dispose`) before the new install, and puts it back if that fails.
|
|
100
|
+
|
|
101
|
+
Each replaceable module has a contract (`modules/gameplay/inventory.contract.js`, `interactions.contract.js`, `dialogue.contract.js`, `quests.contract.js`, `modules/flow/contract.js`,
|
|
102
|
+
`modules/characters/contract.js`, `modules/calendar/contract.js`, `modules/camera/contract.js`; object contract `modules/characters/vehicle.contract.js`): the api, fields, ops, events
|
|
103
|
+
and save keys other code relies on, and nothing more (the engine's callers use only that surface). `runContract(contract, () => myInventory(), { check })`
|
|
104
|
+
from `@threedog/tools/contract` checks a replacement (in a game installed from npm too); `tests/contract/fakeInventory.js` in the engine's
|
|
105
|
+
repository (not shipped) is a whole replacement in ~40 lines; `fakeDialogue.js`, `fakeQuests.js`, `fakeCamera.js` and `fakeVehicle.js`
|
|
106
|
+
are the same for their contracts, and the engine's NPCs, timeline, content loader, hud and characters run on them (test `tests/specs/ui-nav.mjs`).
|
|
107
|
+
A camera module's `prepare` hook: `runContract(contract, () => myCamera(), { check, prepare: e => { e.camera = ... } })` adds what it needs to the
|
|
108
|
+
small engine. A vehicle is checked as an object: `runContract(vehicleContract, () => myBoat, { check })`.
|
|
109
|
+
|
|
110
|
+
## Who holds what
|
|
111
|
+
|
|
112
|
+
`describe().holds`: pause / freeze / camera / debugView, `input` (pushed contexts with owners), `gates`. Every other shared value and slot: `describe().shared`.
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# WebGPU and your own TSL materials
|
|
2
|
+
|
|
3
|
+
**In this recipe**: WebGPU or WebGL · Turning it on, fallbacks · A minimal TSL material · The engine's values as nodes · The engine's shader
|
|
4
|
+
functions · Patches with a `tsl` field (lighting hooks) · Sky and fog hooks · Post effects as nodes · Mistakes, each with the fix · GLSL to TSL
|
|
5
|
+
|
|
6
|
+
On the WebGPU route a game draws with three.js node materials (`MeshStandardNodeMaterial` ...) and writes shaders in TSL, three's shading
|
|
7
|
+
language in JavaScript. Learn TSL from three itself: the [TSL guide](https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language) and
|
|
8
|
+
the [WebGPU examples](https://threejs.org/examples/?q=webgpu) are written for exactly this renderer. The engine adds nothing you must use:
|
|
9
|
+
its wind, cloud shadows, season tint and the rest are ordinary TSL functions in `threedog/tsl`, called where you want them. A whole small
|
|
10
|
+
scene written this way: `threedog examples pull webgpu-tsl`.
|
|
11
|
+
|
|
12
|
+
Rules (correctness, not taste): `import 'threedog/tsl'` comes first in `main.js`; GLSL does not run here (`ShaderMaterial`, `RawShaderMaterial`,
|
|
13
|
+
`onBeforeCompile`, three.js `Pass` objects, GLSL-only patches and hooks), and each one fails by name instead of drawing nothing.
|
|
14
|
+
|
|
15
|
+
## WebGPU or WebGL
|
|
16
|
+
WebGL stays the default and the faster choice on most machines today. Measured in 2026-11 on one scene (terrain, 3000 trees, shadows, bloom):
|
|
17
|
+
on an integrated GPU WebGL drew a frame fastest, WebGPU took about 1.5 times as long and WebGPURenderer's WebGL2 backend (what a browser
|
|
18
|
+
without WebGPU gets) about 1.7 times; on a discrete laptop GPU WebGL was still fastest. The first frame on WebGPU came 0.6 to 1.3 s later
|
|
19
|
+
(shader compilation). How to decide:
|
|
20
|
+
- Choose WebGPU when the game's look is built from TSL (you want to write shaders in JavaScript, compose them as functions, or start from a
|
|
21
|
+
three.js WebGPU example), or when you need something only node materials have.
|
|
22
|
+
- Stay on WebGL when the game must run well on modest hardware, when it already has GLSL (ShaderMaterials, `onBeforeCompile`, a three.js
|
|
23
|
+
`Pass`), or when start-up time matters more than the shading style. Recipe 43 is the GLSL route for the same tasks.
|
|
24
|
+
- Check on the hardware you care about: `threedog inspect perf` on both routes, same scene.
|
|
25
|
+
|
|
26
|
+
## Turning it on
|
|
27
|
+
```js
|
|
28
|
+
import 'threedog/tsl'; // first line: brings three/webgpu and the node versions of the engine's sky, post chain and patches
|
|
29
|
+
import { createEngine } from 'threedog';
|
|
30
|
+
|
|
31
|
+
const engine = createEngine({ canvas: document.getElementById('view'), renderer: 'webgpu' });
|
|
32
|
+
await engine.start(); // start() initializes the renderer, so createEngine stays synchronous
|
|
33
|
+
console.log(engine.describe().renderer); // { requested: 'webgpu', backend: 'webgpu' | 'webgl2' | 'webgl', reason }
|
|
34
|
+
```
|
|
35
|
+
Your own `new WebGPURenderer(...)` as `renderer` goes the same way (the engine configures only a renderer it built, recipe 50). When the
|
|
36
|
+
browser gives no WebGPU, `fallback` decides, and the console says once which one happened (`[threedog] WebGPU unavailable (...)`):
|
|
37
|
+
- `'webgl2'` (default): the same WebGPURenderer on its WebGL2 backend. Your TSL keeps working; frames are slower than plain WebGL.
|
|
38
|
+
- `'webgl'`: a `WebGLRenderer` instead. Faster, but node materials cannot draw there (`start()` fails when the scene has one). The engine's
|
|
39
|
+
own materials (terrain, vegetation, water, `makeMaterial`) are plain three.js materials with both a GLSL and a tsl form of each patch, so they
|
|
40
|
+
draw on either renderer with the same look. What engine modules built before `start()` is rebuilt for WebGLRenderer there: the post chain,
|
|
41
|
+
the sky dome (with the `look.sky.patch` hooks set so far), fog, cascaded shadows, particles, rain and snow. Your own objects are not:
|
|
42
|
+
build node materials after `start()` when `engine.describe().renderer.backend !== 'webgl'`.
|
|
43
|
+
- `'error'`: `start()` rejects; the game shows its own message.
|
|
44
|
+
|
|
45
|
+
## A minimal TSL material
|
|
46
|
+
```js
|
|
47
|
+
import * as THREE from 'three';
|
|
48
|
+
import { MeshStandardNodeMaterial } from 'three/webgpu';
|
|
49
|
+
import { color, positionWorld, sin } from 'three/tsl';
|
|
50
|
+
|
|
51
|
+
const mat = new MeshStandardNodeMaterial({ roughness: 0.5 });
|
|
52
|
+
mat.colorNode = color(0x4080ff).mul(sin(positionWorld.y.mul(4)).mul(0.25).add(0.75)); // bands by world height
|
|
53
|
+
engine.add(new THREE.Mesh(new THREE.TorusKnotGeometry(0.6, 0.2, 128, 16), mat));
|
|
54
|
+
```
|
|
55
|
+
A node material is lit, shadowed and fogged by three like its plain twin; you replace only the inputs you set (`colorNode`, `positionNode`,
|
|
56
|
+
`normalNode`, `emissiveNode`, `roughnessNode`, `opacityNode` ...).
|
|
57
|
+
|
|
58
|
+
## The engine's values as nodes: `uniforms(engine)`
|
|
59
|
+
`uniforms(engine)` returns `engine.uniforms` as TSL nodes with the same names: `uTime`, `uWind`, `uCloud`, `uSunDir`, `uDaylight`
|
|
60
|
+
(0 at night, 1 at noon), `uTimeOfDay`, `uFoliageTint`, `uGroundTint` ... (the list: `uniforms` in [the engine API](../api/engine.md)). The node reads the
|
|
61
|
+
engine's value each render, so weather, the calendar and the sky move your material with no per-frame code.
|
|
62
|
+
```js
|
|
63
|
+
import * as THREE from 'three';
|
|
64
|
+
import { MeshStandardNodeMaterial } from 'three/webgpu';
|
|
65
|
+
import { color, float, sin, smoothstep, uniform } from 'three/tsl';
|
|
66
|
+
import { uniforms } from 'threedog/tsl';
|
|
67
|
+
|
|
68
|
+
const u = uniforms(engine);
|
|
69
|
+
const glow = uniform(3); // a value of your own: change glow.value from game code (an op, a quest step)
|
|
70
|
+
const lantern = new MeshStandardNodeMaterial({ color: 0x3a2a18 });
|
|
71
|
+
const night = smoothstep(0.3, 0.9, float(1).sub(u.uDaylight));
|
|
72
|
+
lantern.emissiveNode = color(0xffa040).mul(night.mul(glow)).mul(sin(u.uTime.mul(7)).mul(0.08).add(0.92)); // lights up at dusk, flickers
|
|
73
|
+
engine.add(new THREE.Mesh(new THREE.OctahedronGeometry(0.3), lantern));
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## The engine's shader functions: `threedog/tsl`
|
|
77
|
+
Each is a TSL `Fn` with the same arguments and meaning as its GLSL twin in recipe 43, reading the same `engine.uniforms`; full signatures are
|
|
78
|
+
in the package's `types/tsl.d.ts`. Call them on nodes, put the result where it belongs:
|
|
79
|
+
|
|
80
|
+
| Function | Returns | Typical place |
|
|
81
|
+
|---|---|---|
|
|
82
|
+
| `wind(positionGeometry, objectOrigin(), amplitude)` | object-space offset (vec3) | `positionNode = positionLocal.add(...)`: grass, leaves, flags, hanging cloth |
|
|
83
|
+
| `objectOrigin()` | world origin of the object or instance (vec3) | a per-object phase or random seed |
|
|
84
|
+
| `cloudShadow(positionWorld)` | 0-1 light factor under drifting clouds | multiply into the color or the direct light |
|
|
85
|
+
| `season(color, 0 \| 1)` | the color moved by the calendar's tint (0 foliage, 1 ground) | `colorNode` |
|
|
86
|
+
| `detailNoise(p, kind, octaves)` / `detailBump(...)` | gray noise + height / a bumped normal | breaking up flat color; `normalNode` |
|
|
87
|
+
| `wrapDiffuse`, `shadowTint`, `rim` | light to add | inside a lighting hook (next section) |
|
|
88
|
+
| `waterWave`, `waterSurface(spec)`, `waterFresnel` | wave height / surface color / fresnel blend | a water material of your own |
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
import * as THREE from 'three';
|
|
92
|
+
import { MeshLambertNodeMaterial } from 'three/webgpu';
|
|
93
|
+
import { color, float, mix, positionGeometry, positionLocal, positionWorld, uv } from 'three/tsl';
|
|
94
|
+
import { wind, objectOrigin, cloudShadow, season } from 'threedog/tsl';
|
|
95
|
+
|
|
96
|
+
const flag = new MeshLambertNodeMaterial({ side: THREE.DoubleSide });
|
|
97
|
+
flag.colorNode = season(mix(color(0x803020), color(0xd06040), uv().x), 0).mul(cloudShadow(positionWorld));
|
|
98
|
+
flag.positionNode = positionLocal.add(wind(positionGeometry, objectOrigin(), float(0.4))); // its shadow sways too
|
|
99
|
+
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(1.2, 0.8, 12, 8).translate(0.6, 2.2, 0), flag);
|
|
100
|
+
mesh.castShadow = true;
|
|
101
|
+
engine.add(mesh);
|
|
102
|
+
```
|
|
103
|
+
three's shadow pass draws with the material's `positionNode`, so whatever moves vertices there moves their shadow: no depth material to keep
|
|
104
|
+
in step. With an `InstancedMesh` whose instances are rotated or scaled, the offset must turn with each instance; the patch form below does that.
|
|
105
|
+
|
|
106
|
+
## Patches with a `tsl` field (optional convenience)
|
|
107
|
+
`applyEffects(mat, { wind, season, clouds, patches })` (recipe 43) works on node materials: the engine's own patches carry a TSL version, and a
|
|
108
|
+
patch of yours gives `tsl: (ctx) => node` beside or instead of `glsl`. Same slots, same order and conflict rules; `ctx` holds the slot's value
|
|
109
|
+
so far (`ctx.color`, `ctx.position`, `ctx.normal` ...), `ctx.tsl` (three/tsl) and `ctx.fx` (the `threedog/tsl` functions). `ctx.position`
|
|
110
|
+
is object space before instancing, as in GLSL, so an instanced mesh's offset turns with each instance by itself. Return the new value. A patch that has only `glsl` stops with `patch 'x' has no tsl version`.
|
|
111
|
+
```js
|
|
112
|
+
import * as THREE from 'three';
|
|
113
|
+
import { MeshLambertNodeMaterial } from 'three/webgpu';
|
|
114
|
+
import { applyEffects } from 'threedog';
|
|
115
|
+
|
|
116
|
+
const leaves = new MeshLambertNodeMaterial({ color: 0x88aa55 });
|
|
117
|
+
applyEffects(leaves, { wind: 0.3, season: 'foliage', patches: [
|
|
118
|
+
// darker toward the ground: ctx.color is the vec4 surface color with the map and vertex colors applied
|
|
119
|
+
{ name: 'heightShade', slot: 'color', tsl: ({ color, tsl }) => tsl.vec4(color.rgb.mul(tsl.positionWorld.y.mul(0.25).add(0.5).clamp(0, 1)), color.a) },
|
|
120
|
+
] });
|
|
121
|
+
engine.add(new THREE.Mesh(new THREE.BoxGeometry(0.6, 3, 0.6, 1, 10, 1).translate(0, 1.5, 0), leaves));
|
|
122
|
+
```
|
|
123
|
+
The `lighting` slot edits light after three summed it: `tsl: (ctx) => ({ directDiffuse, directSpecular, indirectDiffuse, indirectSpecular })`
|
|
124
|
+
(any of the four, as nodes). For what GLSL reads in `lightsPars` (the sun's direction, its shadow alone), give hooks instead: `start(ctx)` runs
|
|
125
|
+
before the lights, `light(light, ctx)` may return a new color node for one light before three lights the surface with it (the
|
|
126
|
+
engine's cloud shadow dims the sun this way; return nothing to leave a light alone), `direct(light, ctx)` once per light (`light.direction`, `light.color` with the shadow in it, `light.shadow` 1 lit / 0 shadowed
|
|
127
|
+
or null, `light.directional`, `light.light`; what it adds to `ctx.directDiffuse` ... joins the summed light at the patch's place in the
|
|
128
|
+
list, as on WebGL), `after(ctx)` at the end; `ctx.state` is shared between them. The `if` in `direct` is
|
|
129
|
+
plain JavaScript run while the shader is built (which light this is), not a per-pixel branch.
|
|
130
|
+
```js
|
|
131
|
+
import * as THREE from 'three';
|
|
132
|
+
import { MeshStandardNodeMaterial } from 'three/webgpu';
|
|
133
|
+
import { applyEffects } from 'threedog';
|
|
134
|
+
|
|
135
|
+
const coolShade = { name: 'coolShade', slot: 'lighting', tsl: {
|
|
136
|
+
start: c => { c.state.lit = c.tsl.float(1).toVar('coolShadeLit'); },
|
|
137
|
+
direct: (light, c) => { if (light.directional && light.shadow) c.state.lit.assign(light.shadow); },
|
|
138
|
+
after: c => ({ indirectDiffuse: c.indirectDiffuse.mul(c.tsl.mix(c.tsl.vec3(0.8, 0.9, 1.25), c.tsl.vec3(1), c.state.lit)) }), // bluer fill in shadow
|
|
139
|
+
} };
|
|
140
|
+
const rock = applyEffects(new MeshStandardNodeMaterial({ color: 0x9a9080 }), { patches: [coolShade] });
|
|
141
|
+
engine.add(new THREE.Mesh(new THREE.DodecahedronGeometry(1), rock));
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Sky and fog hooks
|
|
145
|
+
The engine's sky dome and fog kinds have node versions; nothing changes in how you set them (recipe 08). The GLSL hooks (`look.sky.patch`
|
|
146
|
+
clouds and layers, `look.fog.patch('fog')`, style sky layers) take `{ glsl, tsl }` on this route: see 52's [tsl forms](52-look.md#tsl-forms).
|
|
147
|
+
A sky of your own: three's `SkyMesh` (`three/addons/objects/SkyMesh.js`) through `engine.look.sky.replace`.
|
|
148
|
+
|
|
149
|
+
## Post effects as nodes
|
|
150
|
+
On WebGPURenderer the post chain is one three.js `RenderPipeline` (`engine.post.object()` returns `{ pipeline, scenePass }`). The slots, order
|
|
151
|
+
and ownership rules of recipe 52 are unchanged; an effect gives `node: (input, ctx) => color` instead of `create`, and a fused snippet gives
|
|
152
|
+
`fuse.tsl: (color, uv, ctx) => color` beside its `fuse.glsl`. `ctx.p` holds a live uniform per number or color param (so `look.post.set` does
|
|
153
|
+
not rebuild), `ctx.tsl` is three/tsl, `ctx.scenePass` the scene pass (depth: `ctx.scenePass.getTextureNode('depth')`). The effects of
|
|
154
|
+
`@threedog/post-effects` run on both routes as they are. An effect that reads depth, with a GLSL pass and a TSL node in one object
|
|
155
|
+
(volumetric fog, god rays): [recipe 59](59-depth-post-effects.md).
|
|
156
|
+
```js
|
|
157
|
+
engine.look.post.add({ name: 'warmGrade', slot: 'ldr', params: { amount: 0.3 },
|
|
158
|
+
node: (input, { tsl, p }) => tsl.vec4(tsl.mix(input.rgb, input.rgb.mul(tsl.vec3(1.05, 0.95, 0.8)), p.amount), input.a) });
|
|
159
|
+
engine.look.post.set({ 'warmGrade.amount': 0.5 }); // a uniform write, no rebuild
|
|
160
|
+
```
|
|
161
|
+
`ldr` runs after tone mapping (display colors 0-1); something that should glow or bleed belongs in `hdr`. An effect with only `create`
|
|
162
|
+
(a three.js `Pass`) stops with its name.
|
|
163
|
+
|
|
164
|
+
## Mistakes, each with the fix
|
|
165
|
+
Each block shows the mistake as a comment and the working line under it.
|
|
166
|
+
```js
|
|
167
|
+
import * as THREE from 'three';
|
|
168
|
+
import { MeshBasicNodeMaterial } from 'three/webgpu';
|
|
169
|
+
import { color, mix, uv } from 'three/tsl';
|
|
170
|
+
|
|
171
|
+
// wrong: new THREE.ShaderMaterial({ fragmentShader }) or mat.onBeforeCompile = fn; WebGPURenderer never runs either (start() names the mesh)
|
|
172
|
+
const sign = new MeshBasicNodeMaterial();
|
|
173
|
+
sign.colorNode = mix(color(0x203060), color(0xf0c080), uv().y);
|
|
174
|
+
engine.add(new THREE.Mesh(new THREE.PlaneGeometry(2, 1), sign));
|
|
175
|
+
```
|
|
176
|
+
An sRGB picture as `scene.background` is tone-mapped on both routes and with or without post effects (the engine's frame matches
|
|
177
|
+
WebGPURenderer, which tone-maps the whole frame once). Plain three.js's WebGLRenderer leaves an sRGB background out of tone mapping, so a
|
|
178
|
+
`renderer.render(scene, camera)` of your own on WebGL shows it brighter than the engine's frame.
|
|
179
|
+
```js
|
|
180
|
+
import * as THREE from 'three';
|
|
181
|
+
import { MeshLambertNodeMaterial } from 'three/webgpu';
|
|
182
|
+
import { float, positionGeometry, positionLocal } from 'three/tsl';
|
|
183
|
+
import { wind, objectOrigin } from 'threedog/tsl';
|
|
184
|
+
|
|
185
|
+
const grass = new MeshLambertNodeMaterial({ color: 0x6a8a3a });
|
|
186
|
+
// wrong: wind(positionLocal, ...); positionLocal is after the instance transform, so every blade sways by its height in the field
|
|
187
|
+
grass.positionNode = positionLocal.add(wind(positionGeometry, objectOrigin(), float(0.3)));
|
|
188
|
+
engine.add(new THREE.InstancedMesh(new THREE.PlaneGeometry(0.05, 0.4, 1, 3).translate(0, 0.2, 0), grass, 10));
|
|
189
|
+
```
|
|
190
|
+
```js
|
|
191
|
+
import * as THREE from 'three';
|
|
192
|
+
import { MeshStandardNodeMaterial } from 'three/webgpu';
|
|
193
|
+
import { color, select, uv, vec3 } from 'three/tsl';
|
|
194
|
+
import { uniforms } from 'threedog/tsl';
|
|
195
|
+
|
|
196
|
+
const u = uniforms(engine), mat = new MeshStandardNodeMaterial();
|
|
197
|
+
// wrong: color(0xff0000).add(uv()); vec3 + vec2 gives no error, a channel is dropped or padded silently. Make the types match:
|
|
198
|
+
mat.colorNode = color(0xff0000).mul(0.5).add(vec3(uv(), 0).mul(0.5));
|
|
199
|
+
// wrong: u.uDaylight.value < 0.5 ? a : b, or u.uTime * 2; JavaScript runs once while the shader is built (and a node times a number is NaN)
|
|
200
|
+
mat.emissiveNode = select(u.uDaylight.lessThan(0.5), color(0xffaa44), color(0x000000)).mul(u.uTime.mul(2).sin().mul(0.5).add(0.5));
|
|
201
|
+
engine.add(new THREE.Mesh(new THREE.SphereGeometry(0.5), mat));
|
|
202
|
+
```
|
|
203
|
+
```js
|
|
204
|
+
import * as THREE from 'three';
|
|
205
|
+
import { MeshBasicNodeMaterial } from 'three/webgpu';
|
|
206
|
+
import { Fn, Loop, float, uniform, uv, vec3 } from 'three/tsl';
|
|
207
|
+
|
|
208
|
+
// wrong: uniform(new THREE.Vector3()).onRenderUpdate(({ value }) => value.copy(p)); the callback is handed the frame, not the value: it RETURNS the new value
|
|
209
|
+
const camHeight = uniform(0).onRenderUpdate(() => engine.camera.position.y);
|
|
210
|
+
const bands = new MeshBasicNodeMaterial();
|
|
211
|
+
// wrong: Loop(...) (or If, or x.addAssign) at the top level, outside an Fn: nothing holds the statement, three drops it with NO error and
|
|
212
|
+
// the sum stays 0. The engine warns in dev for a patch's or post effect's code; for your own material nothing tells you
|
|
213
|
+
bands.colorNode = Fn(() => {
|
|
214
|
+
const sum = float(0).toVar();
|
|
215
|
+
Loop(4, ({ i }) => { sum.addAssign(uv().x.mul(float(i).add(1)).fract().mul(0.25)); });
|
|
216
|
+
return vec3(sum).mul(camHeight.mul(0.1).clamp(0.5, 1));
|
|
217
|
+
})();
|
|
218
|
+
engine.add(new THREE.Mesh(new THREE.PlaneGeometry(2, 1), bands));
|
|
219
|
+
```
|
|
220
|
+
```js
|
|
221
|
+
import * as THREE from 'three';
|
|
222
|
+
import { RenderTarget } from 'three/webgpu';
|
|
223
|
+
|
|
224
|
+
// wrong: your own requestAnimationFrame loop calling engine.renderer.render(...); the engine runs the only loop and its post chain
|
|
225
|
+
const minimap = new RenderTarget(256, 256), top = new THREE.OrthographicCamera(-20, 20, 20, -20, 0.1, 100);
|
|
226
|
+
top.position.set(0, 50, 0); top.lookAt(0, 0, 0);
|
|
227
|
+
engine.onFrame(() => engine.renderOnce(minimap, top)); // one extra picture per frame; a fresh node frame each call
|
|
228
|
+
```
|
|
229
|
+
```js
|
|
230
|
+
import { RenderPipeline } from 'three/webgpu';
|
|
231
|
+
|
|
232
|
+
// wrong: new PostProcessing(renderer) from older three examples (three 0.187 names it RenderPipeline), and a second pipeline beside the engine's
|
|
233
|
+
console.log(engine.post.object()?.pipeline instanceof RenderPipeline); // the engine's own, once its chain is built; add effects with look.post.add
|
|
234
|
+
```
|
|
235
|
+
Also: three's `uniform` takes a value, not a `{ value }` object, so passing it `engine.uniforms.uWind` throws; use `uniforms(engine).uWind`.
|
|
236
|
+
A type mismatch never errors: look at the result (`engine.debug.view('lighting')`, `threedog shot`).
|
|
237
|
+
Many materials, many programs: WebGPURenderer builds one program per distinct set of patch uniform objects. Materials made from one spec
|
|
238
|
+
(`makeMaterial(role)`, the same `detail` / `lighting` options) share theirs, so a hundred stones build one; a patch you write once and pass
|
|
239
|
+
to every material shares too, a fresh patch object per material builds one program each.
|
|
240
|
+
|
|
241
|
+
## GLSL to TSL
|
|
242
|
+
| GLSL | TSL (`three/tsl`) |
|
|
243
|
+
|---|---|
|
|
244
|
+
| `float x = 1.0;` / `vec3(1.0, 0.5, 0.0)` | `float(1)` / `vec3(1, 0.5, 0)`, `color(0xff8000)` for a hex color |
|
|
245
|
+
| `a + b`, `a * b`, `-a`, `1.0 - a` | `a.add(b)`, `a.mul(b)`, `a.negate()`, `a.oneMinus()` |
|
|
246
|
+
| `x += y;` | `x.addAssign(y)` (x made with `.toVar()`) |
|
|
247
|
+
| `mix(a, b, t)`, `smoothstep(e0, e1, x)`, `clamp(x, 0.0, 1.0)` | `mix(a, b, t)`, `smoothstep(e0, e1, x)`, `x.clamp(0, 1)` |
|
|
248
|
+
| `uniform float uAmount;` | `const uAmount = uniform(0.5)`, changed by `uAmount.value = 1` |
|
|
249
|
+
| the engine's `uTime`, `uWind`, `uDaylight` | `uniforms(engine).uTime` ... (the clock every engine effect reads, not three's own `time`) |
|
|
250
|
+
| `varying vec3 vPos;` | built in: `positionWorld`, `normalView`...; your own: `node.toVarying()` |
|
|
251
|
+
| `vec4 c = texture2D(map, vUv);` | `const c = texture(map, uv())` |
|
|
252
|
+
| `gl_FragCoord.xy` | `screenCoordinate.xy` (pixels), `screenUV` (0-1) |
|
|
253
|
+
| `position`, `normal`, `uv` attributes | `positionGeometry`, `normalGeometry`, `const st = uv()` |
|
|
254
|
+
| moving vertices (`transformed`, `gl_Position`) | `material.positionNode` (object space), `vertexNode` (clip space) |
|
|
255
|
+
| `gl_FragColor = ...` | `colorNode` (lit surface color), `emissiveNode`, `outputNode` (final color) |
|
|
256
|
+
| `if (c) {...} else {...}` | `const v = select(c, a, b)`; inside `Fn`, `If` / `Else` blocks |
|
|
257
|
+
| `for (int i = 0; i < 4; i++)` | `Loop` with a count and a callback, inside `Fn` |
|
|
258
|
+
| `float f(vec3 p) {...}` | `const f = Fn(([p]) => ...)` |
|
|
259
|
+
| `discard;` | `Discard` inside `Fn`, or `alphaTest` with `opacityNode` |
|
|
260
|
+
| `#include <threedog_wind>`, `threedogWind(...)` | `import { wind } from 'threedog/tsl'`, `wind(...)` |
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Who steers a character: engine.control
|
|
2
|
+
|
|
3
|
+
Every character (the player, an NPC, a hand-built `Character`) has one control slot. One owner is in charge of it at a time, and only that
|
|
4
|
+
owner moves the character. Take it, steer, give it back:
|
|
5
|
+
|
|
6
|
+
```js
|
|
7
|
+
// a quest walks the guard to the gate; its schedule waits and carries on afterwards
|
|
8
|
+
const escort = engine.control.take(guard, { owner: 'escort', layer: 'script', step: (dt, ch) => ({ x: 0, z: 1 }) });
|
|
9
|
+
escort.remove();
|
|
10
|
+
|
|
11
|
+
// a roll: the keys pause for 0.4 s, then come back by themselves
|
|
12
|
+
const roll = engine.control.take(engine.player, { owner: 'roll', layer: 'script', step: () => ({ x: 0, z: 1, run: true }) });
|
|
13
|
+
engine.clock.after(0.4, () => roll.remove());
|
|
14
|
+
|
|
15
|
+
// no step: move it from your own code; move() does nothing while someone above holds it
|
|
16
|
+
const h = engine.control.take(drone, { owner: 'ai', layer: 'script' });
|
|
17
|
+
engine.onFixed(dt => h.move(dir.x, dir.z, false, dt));
|
|
18
|
+
|
|
19
|
+
engine.control.of(engine.player); // { owner: 'timeline', layer: 'cutscene', waiting: [{ owner: 'walkTo', layer: 'script' }, { owner: 'player-input', layer: 'base' }] }
|
|
20
|
+
engine.describe().control; // the same for every held character, by id
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
What to remember:
|
|
24
|
+
- **Layers, lowest first**: `base` (the character's own driver: the player's keys, an NPC's behaviour) < `script` (walkTo, quest scripts, AI
|
|
25
|
+
orders; the default) < `cutscene` (timeline) < `net` (another machine drives it). The highest holder is in charge.
|
|
26
|
+
- **A higher holder pauses the lower one, it does not cancel it.** When the cutscene ends, the walk it interrupted goes on; when the walk
|
|
27
|
+
arrives, the keys work again. Nobody has to remember what to restore.
|
|
28
|
+
- **One owner per layer.** Two different owners on the same layer is `[control-conflict]` in development, naming both: end the old one first
|
|
29
|
+
(`h.remove()`), or take another layer. The same owner taking again just replaces its own hold.
|
|
30
|
+
- **A held character moves only through its holder.** `character.move()` from anywhere else is `[control-bypass]` in development: the bug where
|
|
31
|
+
keys, a walk and a cutscene all pushed one body in the same step and the last one won.
|
|
32
|
+
- **`step(dt, ch)`** returns `{ x, z, run }` (world direction, length 0-1 scales the speed), `null` (stand still: gravity and the idle pose go on)
|
|
33
|
+
or `false` (do not move this step: you placed it yourself). `dt` is the character's time channel, so bullet time slows it too.
|
|
34
|
+
- **Inside a module** use `ctx.control.take(ch, { ... })`: the owner defaults to the module, and everything it holds is handed back when the
|
|
35
|
+
module goes (unloaded, rolled back, replaced).
|
|
36
|
+
|
|
37
|
+
## Who takes what
|
|
38
|
+
|
|
39
|
+
| Driver | Layer | Owner | Notes |
|
|
40
|
+
|---|---|---|---|
|
|
41
|
+
| `createPlayer({ control })` | base | `'player-input'` | `control` is `'camera'` or `(player, input, dt) => ({ x, z, run })`; jump and driving live here too |
|
|
42
|
+
| NPC behaviour (schedule, wander, your own) | base | `'npcs'` | paused near and far while anything above holds the NPC; kept by NPC id, so it survives the model being rebuilt |
|
|
43
|
+
| `character.walkTo(target)` | script | `'walkTo'` | paused under a cutscene (no progress, no stuck clock), resumes after |
|
|
44
|
+
| `timeline.play(steps, { actors })` | cutscene | `'timeline'` | `actors` defaults to `[engine.player]`; `[]` drives nobody; a `path` step adds its character. While the player is an actor, the `'cutscene'` input context is up |
|
|
45
|
+
| `net()` | net | `'net'` | a client holds every character the host drives; the host holds each peer's avatar. Taking one is `[control-net]`: change it with an op the host runs |
|
|
46
|
+
|
|
47
|
+
Two cutscenes running at once are one owner (`'timeline'`), counted per character, so they never conflict; the slot goes back when the last
|
|
48
|
+
one using that character ends.
|
|
49
|
+
|
|
50
|
+
## Control is not the body
|
|
51
|
+
|
|
52
|
+
**Control** decides where to go. **The mover** (`character.mover = 'simple' | 'rapier' | 'manual'`) decides how the body gets there: integration,
|
|
53
|
+
collision, gravity. They never mix: a cutscene can take a character whose mover is `'rapier'`, and a `'manual'` mover can be steered by the
|
|
54
|
+
player's input. `mover: 'manual'` is for a game that writes the position itself; it still needs someone in control to call `move()` each
|
|
55
|
+
step (a `control` function returning `null` does).
|
|
56
|
+
|
|
57
|
+
## Vehicles
|
|
58
|
+
|
|
59
|
+
Getting into a vehicle does not change who controls the character: the player's input (`'player-input'`) drives the car while seated. A
|
|
60
|
+
cutscene that takes the player while driving stops the steering; the rider stays on its seat.
|
|
61
|
+
|
|
62
|
+
## Saves
|
|
63
|
+
|
|
64
|
+
Holds are not saved: they are running code (a walk, a cutscene, a quest step), and that code takes control again after a load. A walk is
|
|
65
|
+
cancelled by a load (`walkTo` resolves `'cancelled'`).
|