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,86 @@
|
|
|
1
|
+
Day-night grade: light and color grade across the day, with a dark but readable night.
|
|
2
|
+
|
|
3
|
+
# Day-night grade (technique example)
|
|
4
|
+
|
|
5
|
+
A neutral gray scene under one clock: sky, sun / moon, ambient, exposure, a color grade and lamps all keyed by the time of day. Demo:
|
|
6
|
+
`npm run dev` in the engine package, open `/day-night-grade.html` (the day runs in 4 minutes; `?time=17:00` starts at a time). Shots:
|
|
7
|
+
`threedog shot /day-night-grade.html --time 12:00` (and `17:00`, `22:00`). The engine never imports this folder.
|
|
8
|
+
|
|
9
|
+
## The principle
|
|
10
|
+
A time of day is read from three things at once: the light's color and direction, how dark the shadows are, and how much color is left in
|
|
11
|
+
the picture. Changing only brightness gives an underexposed photo, not evening or night. Every number below is this example's current value
|
|
12
|
+
on a gray scene, given to explain an effect.
|
|
13
|
+
|
|
14
|
+
**Noon is the reference.** At noon `sun 3 x sin(elevation) + ambient 1.1` is close to pi, the level where mid-tone albedos show at their own
|
|
15
|
+
value on three's AgX curve (recipe 52). Grade neutral, exposure 1. Tune the palette here first; every other time is a deviation from it.
|
|
16
|
+
|
|
17
|
+
**Dusk: warm without oversaturation.**
|
|
18
|
+
- Put the warmth in the **light** (sun color `0xffa264`, horizon `0xdc9c6c`), not only in the grade: the lit sides turn warm and the shadow
|
|
19
|
+
sides stay cool, and that contrast is what reads as evening (warm key / cool fill).
|
|
20
|
+
- Drop the ambient (0.7) as the sun drops, so shadows deepen and take the sky's blue.
|
|
21
|
+
- In the grade, raise temperature and **lower saturation** (0.9): warm light on warm-ish albedos already adds chroma; a saturation boost on
|
|
22
|
+
top clips oranges into neon. The demo's 17:00 shot measures saturation 0.14 on a gray scene.
|
|
23
|
+
- Lamps fade in before dark (about 16:40 here), the way people switch them on.
|
|
24
|
+
|
|
25
|
+
**Night: dark but readable.**
|
|
26
|
+
- **Moonlight** is a weak, blue-white key (`0x8fa4d4`, 0.35). It still casts a direction, so forms read.
|
|
27
|
+
- **Ambient floor** (the example uses 0.26): keep some. With none, faces away from the moon are pure black and the player sees nothing.
|
|
28
|
+
- **Exposure** lifts a little (1.15) but nowhere near day: the demo's night mean luminance is about half of noon (0.35 vs 0.69).
|
|
29
|
+
- **The grade cools and desaturates** (temperature -0.45, saturation 0.72): eyes lose color in the dark (the Purkinje shift), so less color
|
|
30
|
+
reads as night even at a readable brightness. A touch of contrast (1.06) keeps silhouettes.
|
|
31
|
+
- **Lamps carry the scene**: warm pools of light where the player goes, emissive windows and lamp heads that read as light sources. Warm
|
|
32
|
+
lamps against a cool night is the strongest night cue.
|
|
33
|
+
|
|
34
|
+
How to judge it: shoot each time of day at the game's real camera (`threedog shot --time`) and read luminance, saturation, the "dark" area
|
|
35
|
+
and your own eyes. Common failures: night = noon with lower exposure (daytime colors, an underexposed photo); night with no ambient (crushed
|
|
36
|
+
black; the shot reports the "dark" area); dusk = saturation up (neon oranges).
|
|
37
|
+
|
|
38
|
+
## Plain three.js
|
|
39
|
+
- Tone and exposure: `renderer.toneMapping = THREE.AgXToneMapping` and `renderer.toneMappingExposure`
|
|
40
|
+
([WebGLRenderer](https://threejs.org/docs/#api/en/renderers/WebGLRenderer), [webgl_tonemapping](https://threejs.org/examples/#webgl_tonemapping)).
|
|
41
|
+
- Light over time: interpolate a few keyframes per frame and write `DirectionalLight.color` / `.intensity`, `HemisphereLight` colors and
|
|
42
|
+
intensity, `scene.fog.color` and `scene.background` ([Color.lerpColors](https://threejs.org/docs/#api/en/math/Color)).
|
|
43
|
+
- Grade: a final pass in `EffectComposer` — a `ShaderPass` that shifts temperature / saturation / contrast, or `LUTPass` with a 3D LUT
|
|
44
|
+
([webgl_postprocessing_3dlut](https://threejs.org/examples/#webgl_postprocessing_3dlut)).
|
|
45
|
+
- Lamps: `PointLight` / `SpotLight` intensity and `material.emissiveIntensity`, faded by a daylight value, never toggled.
|
|
46
|
+
|
|
47
|
+
## Style routes
|
|
48
|
+
- **Naturalistic**: warm sun at dusk, a blue moon, a cooled and desaturated night grade, lamps as the night's light. One full-screen grade pass.
|
|
49
|
+
- **Pastel / cozy**: night kept lighter (more ambient, more exposure) and dusk pinker. Same cost.
|
|
50
|
+
- **Horror**: a lower ambient floor, lamps carry everything; darkness is part of play. Same cost, often more lamps.
|
|
51
|
+
- **Stylized flat**: skip the grade and key only the sky and lamps; the palette swap does the work. No post pass.
|
|
52
|
+
- **Pixel / palette-limited**: swap whole palettes or a small set of LUTs at fixed times instead of continuous curves. One lookup.
|
|
53
|
+
|
|
54
|
+
## The engine helper (optional convenience)
|
|
55
|
+
`dayNightGrade.js` holds one clock and four tables, all keyed by `engine.calendar.timeOfDay` (0 midnight, 0.5 noon). `threedog examples pull
|
|
56
|
+
day-night-grade` copies it; replace its colors with the ones your DESIGN.md look names.
|
|
57
|
+
|
|
58
|
+
| Table | Read by | What it sets |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| `SKY` = `style.sky.keyframes` | the sky module | dome zenith / horizon, fog (= horizon), sun color + intensity (the moon at night), ambient (hemisphere and environment map) |
|
|
61
|
+
| `EXPOSURE` = `style.exposure` | the sky module | `renderer.toneMappingExposure` |
|
|
62
|
+
| `GRADE` | `followDayNight` each frame | the `lut()` post effect: temperature, saturation, contrast (`look.post`, owner `day-night`) |
|
|
63
|
+
| `LAMPS` | `followDayNight` each frame | the game's lamps: point-light intensity and emissive strength, by `calendar.daylight` (`LAMPS.offAbove: 0.35`) |
|
|
64
|
+
|
|
65
|
+
`dayNightStyle()` returns the first two plus the `lut` effect as a style fragment: `createEngine({ style: [yourStyle, dayNightStyle()] })`.
|
|
66
|
+
`followDayNight(engine, { lamps })` writes the grade and lamps; `sample(rows, t)` is the interpolation. Keyframes interpolate linearly and
|
|
67
|
+
wrap at midnight, so the first and last rows are the same night. The demo spec checks that ground under a lamp is over 1.3x open ground at 22:00.
|
|
68
|
+
|
|
69
|
+
Cost (1280x720, laptop GPU, this demo; median of 120 frames):
|
|
70
|
+
- Keyframes, exposure, lamp writes: no measurable cost (uniform writes).
|
|
71
|
+
- The `lut` grade pass: about **+1.5 ms** a frame (22:00: 5.8 ms with it, 4.3 ms without), mostly the post chain's full-screen pass; other
|
|
72
|
+
`ldr` effects (vignette, grain) fuse into the same pass for free.
|
|
73
|
+
- Point lights: each one adds to every lit material's shader. Keep `distance` short; for many lamps use `engine.lightPool` (recipe 09).
|
|
74
|
+
- `followDayNight` writes the grade only when it moved (sum of changes > 0.004), so the fused shader is not refreshed every frame.
|
|
75
|
+
|
|
76
|
+
Rules (correctness):
|
|
77
|
+
- Never toggle `light.visible` (or add / remove lights) as the day turns: a light-count change recompiles every lit material. Write intensity.
|
|
78
|
+
- Under a cycle, adjust brightness through the keyframes or `engine.mod('look.lights.hemi.intensity' | 'look.lights.sun.intensity', { mul })`; `look.lights.set` replaces
|
|
79
|
+
the keyframe value. Fixing night brightness with `look.lights.set({ hemi })` freezes the cycle; use the keyframe ambient, or
|
|
80
|
+
`engine.mod('look.lights.hemi.intensity', { mul })` for a place (a cave).
|
|
81
|
+
- Lamps and gameplay read `calendar.daylight` / `isDay` / `between`, never the clock text (`if (clock === '18:00')` breaks other day
|
|
82
|
+
lengths; recipe 08).
|
|
83
|
+
- `lut()` comes from `@threedog/post-effects`; install it in the game.
|
|
84
|
+
|
|
85
|
+
Sources: written for this engine from its sky contract (recipe 08), the three.js `toneMappingExposure` / AgX docs and common film-grading
|
|
86
|
+
practice (warm key / cool fill, Purkinje shift at night); no code copied.
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Day-night lighting and color grade: one set of keyframes over the day for the sky (sun / moon, hemisphere, fog), exposure, a post color grade,
|
|
3
|
+
// and the game's own lamps. Technique example (README.md next to this file); copy and adapt: the numbers are a neutral starting point, the
|
|
4
|
+
// colors of your game come from its DESIGN.md look, not from here.
|
|
5
|
+
import { lut } from '@threedog/post-effects';
|
|
6
|
+
|
|
7
|
+
/** Sky keyframes: [time 0-1, zenith, horizon (= fog color), sun color (the moon at night), sun intensity, ambient intensity] */
|
|
8
|
+
export const SKY = [
|
|
9
|
+
[0.00, 0x0d1626, 0x1f2c44, 0x8fa4d4, 0.35, 0.26], // night: the "sun" row is moonlight, blue and weak; ambient keeps a floor
|
|
10
|
+
[0.21, 0x172036, 0x3a4058, 0x9aa8d4, 0.35, 0.26],
|
|
11
|
+
[0.26, 0x55688c, 0xd09c7c, 0xffb27c, 1.2, 0.6], // sunrise: warm sun, horizon warm, zenith still cool (the contrast IS the look)
|
|
12
|
+
[0.33, 0x6d92c2, 0xc6d2dc, 0xfff0dc, 2.4, 0.9],
|
|
13
|
+
[0.50, 0x6e98ce, 0xcfdae4, 0xfffaf2, 3.0, 1.1], // noon: sun 3 + hemi 1.1 ~ pi: mid-tone albedos show at their own value
|
|
14
|
+
[0.68, 0x6a8ebe, 0xd4d0c6, 0xfff0d8, 2.6, 1.0],
|
|
15
|
+
[0.73, 0x4c5c86, 0xdc9c6c, 0xffa264, 1.6, 0.7], // dusk: warmth in the LIGHT color, ambient drops so shadows turn cool by contrast
|
|
16
|
+
[0.78, 0x222e4c, 0x5c5068, 0x9aa6d4, 0.35, 0.32], // blue hour: the moon row again, a little more ambient than deep night
|
|
17
|
+
[1.00, 0x0d1626, 0x1f2c44, 0x8fa4d4, 0.35, 0.26],
|
|
18
|
+
];
|
|
19
|
+
/** Exposure over the day [time, exposure]: lifted at night so the moonlit scene reads, but never back to day brightness */
|
|
20
|
+
export const EXPOSURE = [[0, 1.15], [0.22, 1.15], [0.3, 1.0], [0.7, 1.0], [0.77, 1.1], [1, 1.15]];
|
|
21
|
+
/** Color grade over the day [time, temperature (-1 cool..1 warm), saturation, contrast] (the lut() post effect's params) */
|
|
22
|
+
export const GRADE = [
|
|
23
|
+
[0.00, -0.45, 0.72, 1.06], // night: cooler and less saturated (eyes lose color in the dark), a touch of contrast keeps edges
|
|
24
|
+
[0.22, -0.3, 0.8, 1.04],
|
|
25
|
+
[0.28, 0.35, 0.95, 1.0],
|
|
26
|
+
[0.40, 0, 1, 1],
|
|
27
|
+
[0.65, 0, 1, 1],
|
|
28
|
+
[0.73, 0.5, 0.9, 1.04], // dusk: warm, and saturation DOWN as warmth goes up, or oranges clip into neon
|
|
29
|
+
[0.79, -0.25, 0.78, 1.05],
|
|
30
|
+
[1.00, -0.45, 0.72, 1.06],
|
|
31
|
+
];
|
|
32
|
+
/** Lamps: fully on below this daylight (0 night .. 1 noon), fading in above it */
|
|
33
|
+
export const LAMPS = { onBelow: 0.02, offAbove: 0.35 }; // daylight 0.35 is about 16:40: windows and lamps come on before dark, as people switch them on
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The style fragment that carries the keyframes: createEngine({ style: [yourStyle, dayNightStyle()] }) or merge the keys into your own.
|
|
37
|
+
* The grade is a look plugin of this fragment (the shape a plugins/ file exports), so it reaches the chain through look.post.add like any effect
|
|
38
|
+
*/
|
|
39
|
+
export function dayNightStyle() {
|
|
40
|
+
return { name: 'dayNight', sky: { keyframes: SKY, moonColor: 0xc8d4f0 }, exposure: EXPOSURE, plugins: { 'lut.js': { default: lut({ enabled: true }) } } };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Linear interpolation over [[t, ...values]] rows sorted by t, wrapping at 1 */
|
|
44
|
+
export function sample(rows, t) {
|
|
45
|
+
let i = rows.findIndex(r => r[0] > t);
|
|
46
|
+
if (i <= 0) i = i === 0 ? 0 : rows.length - 1;
|
|
47
|
+
const a = rows[Math.max(0, i - 1)], b = rows[i], k = b[0] === a[0] ? 0 : (t - a[0]) / (b[0] - a[0]);
|
|
48
|
+
return a.slice(1).map((v, j) => v + (b[j + 1] - v) * Math.min(1, Math.max(0, k)));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Drive the grade and the game's lamps from engine.calendar every frame. Lamps: [{ light?: THREE.Light, material?: { emissiveIntensity },
|
|
53
|
+
* intensity, emissive }]: full values at night. The grade is written through look.post (owner 'day-night'), only when it moved, so
|
|
54
|
+
* describe() shows who set it and the fused post shader is not refreshed every frame (the same owner overwrites its keys). Returns { remove(), level() }.
|
|
55
|
+
* @param {import('threedog').Engine} engine
|
|
56
|
+
* @param {{ lamps?: { light?: any, material?: any, intensity?: number, emissive?: number }[] }} [o]
|
|
57
|
+
*/
|
|
58
|
+
export function followDayNight(engine, { lamps = [] } = {}) {
|
|
59
|
+
let last = null, level = 0, post = null;
|
|
60
|
+
const smooth = (/** @type {number} */ x) => x * x * (3 - 2 * x);
|
|
61
|
+
const frame = engine.onFrame(() => {
|
|
62
|
+
const cal = /** @type {any} */ (engine).calendar, t = cal.timeOfDay;
|
|
63
|
+
const [temperature, saturation, contrast] = sample(GRADE, t);
|
|
64
|
+
if (!last || Math.abs(last[0] - temperature) + Math.abs(last[1] - saturation) + Math.abs(last[2] - contrast) > 0.004) {
|
|
65
|
+
last = [temperature, saturation, contrast];
|
|
66
|
+
post = engine.look.post.set({ 'lut.temperature': temperature, 'lut.saturation': saturation, 'lut.contrast': contrast }, { owner: 'day-night' });
|
|
67
|
+
}
|
|
68
|
+
level = 1 - smooth(Math.min(1, Math.max(0, (cal.daylight - LAMPS.onBelow) / (LAMPS.offAbove - LAMPS.onBelow))));
|
|
69
|
+
for (const l of lamps) {
|
|
70
|
+
// intensity only, never visible = false: a light count change recompiles every lit material (a stall at dusk)
|
|
71
|
+
if (l.light) l.light.intensity = (l.intensity ?? 1) * level;
|
|
72
|
+
if (l.material) l.material.emissiveIntensity = (l.emissive ?? 1) * level;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return { remove() { frame.remove(); post?.remove(); }, level: () => level };
|
|
76
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Demo page for the day-night-grade technique (page /day-night-grade.html). A neutral gray street corner: three houses with a lit window each,
|
|
3
|
+
// two lamp posts with a point light, a figure-sized capsule. The day runs in 4 minutes; ?time=17:30 (the engine reads it) starts at a time.
|
|
4
|
+
// Test page for tests/specs/day-night-grade.mjs: window.dayNightTest = { THREE, follow, lamps }.
|
|
5
|
+
import { createEngine, calendar, sky, fixedCamera, makeMaterial, THREE } from 'threedog';
|
|
6
|
+
import { dayNightStyle, followDayNight } from './dayNightGrade.js';
|
|
7
|
+
|
|
8
|
+
const engine = createEngine({ canvas: /** @type {HTMLCanvasElement} */ (document.getElementById('view')), style: dayNightStyle(),
|
|
9
|
+
ui: { loading: false }, saveKey: 'threedog-day-night' });
|
|
10
|
+
engine.use(calendar({ time: '12:00', dayLength: 240 }));
|
|
11
|
+
engine.use(sky());
|
|
12
|
+
engine.use(fixedCamera({ position: [9, 4.2, 11], lookAt: [0, 1.2, 0] }));
|
|
13
|
+
|
|
14
|
+
const ground = new THREE.Mesh(new THREE.PlaneGeometry(80, 80), makeMaterial('terrain', { color: 0x8e8e8e }));
|
|
15
|
+
ground.rotation.x = -Math.PI / 2;
|
|
16
|
+
ground.receiveShadow = true;
|
|
17
|
+
engine.scene.add(ground);
|
|
18
|
+
const add = (/** @type {THREE.BufferGeometry} */ g, /** @type {THREE.Material} */ m, x = 0, y = 0, z = 0) => {
|
|
19
|
+
const mesh = new THREE.Mesh(g, m);
|
|
20
|
+
mesh.position.set(x, y, z);
|
|
21
|
+
mesh.castShadow = mesh.receiveShadow = true;
|
|
22
|
+
engine.scene.add(mesh);
|
|
23
|
+
return mesh;
|
|
24
|
+
};
|
|
25
|
+
const wall = makeMaterial('prop', { color: 0xb2b2b2 }), roof = makeMaterial('prop', { color: 0x7a7a7a });
|
|
26
|
+
// warm window glass: the color is the lamp's, its strength follows the night (followDayNight writes emissiveIntensity)
|
|
27
|
+
const glass = makeMaterial('prop', { color: 0x202020, emissive: 0xffc27a, emissiveIntensity: 0 });
|
|
28
|
+
const lamps = [];
|
|
29
|
+
for (const [x, z, w] of [[-5, -4, 4], [1, -6, 5], [6, -2, 3.5]]) {
|
|
30
|
+
add(new THREE.BoxGeometry(w, 3, 4), wall, x, 1.5, z);
|
|
31
|
+
add(new THREE.ConeGeometry(w * 0.8, 1.4, 4).rotateY(Math.PI / 4), roof, x, 3.7, z);
|
|
32
|
+
add(new THREE.BoxGeometry(0.9, 0.8, 0.05), glass, x, 1.6, z + 2.01);
|
|
33
|
+
}
|
|
34
|
+
lamps.push({ material: glass, emissive: 2.2 });
|
|
35
|
+
// lamp posts: an emissive head (what you see) plus a point light (what it lights); both follow the night together
|
|
36
|
+
const head = makeMaterial('prop', { color: 0x202020, emissive: 0xffd29a, emissiveIntensity: 0 });
|
|
37
|
+
for (const [x, z] of [[-1.5, 1.5], [4.5, 2.5]]) {
|
|
38
|
+
add(new THREE.CylinderGeometry(0.06, 0.08, 3.2, 10), roof, x, 1.6, z);
|
|
39
|
+
add(new THREE.SphereGeometry(0.18, 16, 12), head, x, 3.3, z).castShadow = false;
|
|
40
|
+
const light = new THREE.PointLight(0xffc88a, 0, 9, 1.6); // intensity written by followDayNight; distance limits its cost to the street
|
|
41
|
+
light.position.set(x, 3.1, z);
|
|
42
|
+
engine.scene.add(light);
|
|
43
|
+
lamps.push({ light, intensity: 14 });
|
|
44
|
+
}
|
|
45
|
+
lamps.push({ material: head, emissive: 3 });
|
|
46
|
+
add(new THREE.CapsuleGeometry(0.28, 1.1, 8, 16), makeMaterial('prop', { color: 0xa0a0a0 }), 1.5, 0.83, 2.2);
|
|
47
|
+
add(new THREE.BoxGeometry(0.8, 0.8, 0.8), makeMaterial('prop', { color: 0x9c9c9c }), -3, 0.4, 2.5);
|
|
48
|
+
|
|
49
|
+
const follow = followDayNight(engine, { lamps });
|
|
50
|
+
Object.assign(window, { engine, dayNightTest: { THREE, follow, lamps } });
|
|
51
|
+
engine.start();
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Interior light: sun through a window with shadows, the window as an area light, faked bounce with light probes or a fill light, exposure from outside to inside, and lamps at night without glare.
|
|
2
|
+
|
|
3
|
+
# Interior light (technique example)
|
|
4
|
+
|
|
5
|
+
A neutral gray room (8 x 6 m, 3 m high, a window in the west wall, a door in the south wall), lit from outside by day and by lamps at night.
|
|
6
|
+
Run `npm run dev` and open `/interior-light.html`. The engine never imports this folder.
|
|
7
|
+
|
|
8
|
+
| URL | What changes |
|
|
9
|
+
|---|---|
|
|
10
|
+
| `?cam=inside \| doorway \| outside` | the camera view (the zone, and so exposure and ambient, follows the camera) |
|
|
11
|
+
| `&fill=probes \| light \| none` | bounce baked into light probes (default), one cheap point light, or nothing |
|
|
12
|
+
| `&night=1` | night: lamps instead of the sun |
|
|
13
|
+
| `&area=0` | no window area light |
|
|
14
|
+
| `&leak=1` | keep the outdoor ambient indoors (shows the problem the zones solve) |
|
|
15
|
+
|
|
16
|
+
All values sit in the parameter block at the top of `main.js` (`ROOM`, `WINDOW`, `SUN`, `AMBIENT`, `WINDOW_LIGHT`, `BOUNCE_LIGHT`, `EXPOSURE`,
|
|
17
|
+
`LAMPS`, `VIEWS`). Related: `sun-and-key-light` (the sun itself), `contact-shadows`, `night-lights` (lights in an outdoor night).
|
|
18
|
+
|
|
19
|
+
## The principle
|
|
20
|
+
**A room is lit from its openings.** Daylight enters in two parts: the sun (a hard patch on the floor, the brightest thing in the room) and
|
|
21
|
+
the sky seen through the window (a soft, cool light that falls off with distance from the window). Everything else is bounce: the sun patch
|
|
22
|
+
lights the ceiling and the walls around it. A room without bounce looks like a cave with one spotlight; a room where the bounce is as bright
|
|
23
|
+
as the patch looks flat.
|
|
24
|
+
|
|
25
|
+
**Outdoor ambient leaks.** Hemisphere and environment light reach every surface regardless of walls, so indoors they light the room as if
|
|
26
|
+
it had no roof. Turn them down inside and let the bounce do the fill. `?leak=1` shows the room with the outdoor values: evenly gray, the sun
|
|
27
|
+
patch barely stands out.
|
|
28
|
+
|
|
29
|
+
**Exposure is the eye.** Real interiors are many times darker than the street; the eye adapts, a camera changes exposure. Games compress the
|
|
30
|
+
range: inside gets a higher exposure, eased over about a second when the player crosses the threshold. Looking in from outside, the room usually
|
|
31
|
+
reads darker than the street (and from inside the window brighter than the room), which gives the space its depth; if your game wants a flat,
|
|
32
|
+
evenly lit look (a diorama, a cutaway), keep one exposure on purpose.
|
|
33
|
+
|
|
34
|
+
**Night: light the surfaces, not the bulb.** A lamp is a pool of light on a table, a wall, a floor. Hide the bulb in a shade, keep the visible
|
|
35
|
+
emitter dim, and let the light do the work; several small warm lights read cozier than one bright one. Glare and clipped white come from
|
|
36
|
+
bright visible emitters plus bloom; without bloom they just look like flat white shapes.
|
|
37
|
+
|
|
38
|
+
How to judge it in a shot: the sun patch is the brightest area, the wall opposite the window clearly dimmer, and the corner furthest from the
|
|
39
|
+
window dark but still readable if the game needs it readable. Check from the cameras the game really uses, including the doorway.
|
|
40
|
+
|
|
41
|
+
## Plain three.js
|
|
42
|
+
- Sun patch: a `DirectionalLight` with `castShadow`, every wall / ceiling / frame `castShadow`, and `shadow.camera` fitted to the room
|
|
43
|
+
([DirectionalLightShadow](https://threejs.org/docs/#api/en/lights/shadows/DirectionalLightShadow)).
|
|
44
|
+
- Window sky light: [RectAreaLight](https://threejs.org/docs/#api/en/lights/RectAreaLight) with `RectAreaLightUniformsLib.init()`
|
|
45
|
+
([webgl_lights_rectarealight](https://threejs.org/examples/#webgl_lights_rectarealight)).
|
|
46
|
+
- Bounce: a [LightProbe](https://threejs.org/docs/#api/en/lights/LightProbe) filled by `LightProbeGenerator.fromCubeRenderTarget` after a
|
|
47
|
+
`CubeCamera` render inside the room; or a weak unshadowed `PointLight` above the patch; or a lightmap (`material.lightMap`, needs a second UV set).
|
|
48
|
+
- Exposure: ease `renderer.toneMappingExposure` and the `HemisphereLight` intensity together when the camera enters a box.
|
|
49
|
+
- Lamps: `PointLight` with `decay: 2`, intensity in candela ([PointLight](https://threejs.org/docs/#api/en/lights/PointLight)), a low
|
|
50
|
+
`emissiveIntensity` on the shade.
|
|
51
|
+
|
|
52
|
+
## Style routes
|
|
53
|
+
- **Realistic**: probes or lightmaps for the bounce, a strong sun patch, exposure zones with a noticeable change. Costs a bake.
|
|
54
|
+
- **Stylized / cozy**: warmer bounce (`BOUNCE_LIGHT.color`), softer contrast between patch and room, more lamps at night, gentle exposure
|
|
55
|
+
change. Costs a few more lights.
|
|
56
|
+
- **Horror / stealth**: little or no fill, a hard window patch, slow exposure adaptation so the player is blind for a moment at the door. Cheapest.
|
|
57
|
+
- **Top-down or isometric**: the ceiling is usually hidden; the window patch and lamp pools carry the room. For 2D-like spreading light that
|
|
58
|
+
stops at walls see `examples/rc2d`.
|
|
59
|
+
- **Baked (static rooms)**: lightmaps from Blender give per-surface bounce at no run-time cost when the room does not change (recipe 46).
|
|
60
|
+
|
|
61
|
+
## The engine helper (optional convenience)
|
|
62
|
+
`threedog examples pull interior-light` copies `interiorLight.js` and this README into the game; the probes are `engine.bakeProbes` itself.
|
|
63
|
+
- `exposureZones(engine, { isInside, exposure, ambient, ease })`: changes exposure and the leaking outdoor ambient together, eased, and only
|
|
64
|
+
when the zone changes, with `engine.look.tone.set({ exposure }, { owner: 'zone' })` in `onFrame` (presentation). The page uses a box around
|
|
65
|
+
the room and `EXPOSURE.day.outside` / `.inside`, eased over `EXPOSURE.ease` seconds.
|
|
66
|
+
- `windowLight(engine, { position, lookAt, width, height })`: a `RectAreaLight` with its one-time uniforms init done.
|
|
67
|
+
|
|
68
|
+
| fill | what it gives | cost |
|
|
69
|
+
|---|---|---|
|
|
70
|
+
| `probes` | bounce from the actual sun patch and walls, colored by them, direction-dependent | a bake at load (6 cube renders per probe; `probes.bakeMs` says how long); at run time one LightProbe, about free |
|
|
71
|
+
| `light` | one unshadowed PointLight above the sun patch, standing in for its bounce | one more light in every lit shader; no bake. Simple and easy to tune, but it is a point, so it shows as a hot spot if too strong |
|
|
72
|
+
| `none` | the room as the direct lights leave it | nothing |
|
|
73
|
+
|
|
74
|
+
Measured in the test (linear light, the floor along the north wall and the wall's lower part, out of the sun patch): probes make them clearly
|
|
75
|
+
brighter than no fill (the test requires more than 1.15x).
|
|
76
|
+
|
|
77
|
+
Zones vs measured exposure: a zone is cheap and predictable, and the designer decides the values. Measured auto exposure (average the frame's
|
|
78
|
+
luminance, ease toward a target) adapts to anything but needs a downsample and readback every few frames, can pump when bright objects enter
|
|
79
|
+
the view, and still needs limits. A zone also carries the ambient change, which measured exposure cannot. Limit of zones: they follow the
|
|
80
|
+
camera, so looking into the room from outside it still gets outdoor ambient (`?cam=doorway`). When that view matters, give indoor surfaces
|
|
81
|
+
their own fill (probes sampled per object with `probes.sample(pos)`, or lightmaps).
|
|
82
|
+
|
|
83
|
+
Night (`?night=1`): the sky at night (its moonlight stays very low), the indoor zone's exposure at 1, two warm PointLights (physical falloff,
|
|
84
|
+
decay 2, intensity in candela) inside shades with an emissive of 0.35; one casts a 512 px shadow (a point light shadow is six renders: give it
|
|
85
|
+
only to the light that needs it). With bloom (`@threedog/post-effects`), keep the emitter's `emissiveIntensity` low or it blooms across the room.
|
|
86
|
+
|
|
87
|
+
Rules (correctness):
|
|
88
|
+
- `RectAreaLight` needs `RectAreaLightUniformsLib.init()` once, lights only `MeshStandardMaterial` / `MeshPhysicalMaterial`, and **casts no
|
|
89
|
+
shadow**: it lights through furniture and walls on its front side. Use it for the window's soft sky light or a screen, and keep the
|
|
90
|
+
shadowed part to the sun (a DirectionalLight) or a SpotLight.
|
|
91
|
+
- The sun through a window needs every wall, the ceiling and the window frame to `castShadow`, and a shadow box that covers the room. With
|
|
92
|
+
the sky module: `look.lights.manual('sun.direction')` to aim it, `look.shadow.set({ follow: false, extent })` to fit the box. Thin walls
|
|
93
|
+
want a small `normalBias` (here 1 cm) or light leaks along their inner edge.
|
|
94
|
+
- `engine.bakeProbes({ bounds, spacing, follow })` bakes the scene as it is at that moment: set the indoor ambient and the lights first, and
|
|
95
|
+
bake again (or ship one bake per state) if the lighting changes a lot (day / night). Probes add to the hemisphere and environment, which
|
|
96
|
+
the bake also saw (recipe 34). It is one spherical harmonic for the whole scene at a time, interpolated at the `follow` point: right for
|
|
97
|
+
the room you are in, wrong for a building seen from far away.
|
|
98
|
+
- The bake renders after start; this page tracks it in `engine.loading` so screenshots and tests wait for it.
|
|
99
|
+
- Do not name page URL parameters `time` or `view` (or the other engine scenario keys: `weather`, `pos`, `season`, `state`, `save`, `wait`,
|
|
100
|
+
`cutscene`): `?time=night` reaches the sky as a clock time, turns into NaN and blacks out the frame.
|
|
101
|
+
|
|
102
|
+
Sources: written for this repository, no code copied. Background: three.js `RectAreaLight` and `LightProbe` docs and examples (0.187), general
|
|
103
|
+
architectural-visualization and game-lighting writing on window light, bounce and eye adaptation.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Interior light helpers (technique example, README.md next to this file). Imports only three and its RectAreaLight addon.
|
|
3
|
+
// Why helpers: the zone switch has to change exposure and the leaking outdoor ambient together, eased, and only when the zone changes;
|
|
4
|
+
// the window light needs a one-time uniforms init. Both are easy to get half right by hand.
|
|
5
|
+
import * as THREE from 'three';
|
|
6
|
+
import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Exposure and ambient by zone, following the camera. `isInside(cameraPosition)` decides the zone; on a change the hemisphere / environment
|
|
10
|
+
* intensities switch at once (they light through walls) and the exposure eases over `ease` seconds (an eye adapting). Presentation only: it
|
|
11
|
+
* runs in onFrame and writes owner layers of look.lights / look.tone (remove() on the handle gives the style values back).
|
|
12
|
+
* @param {any} engine
|
|
13
|
+
* @param {{ isInside: (p: THREE.Vector3) => boolean, exposure: { inside: number, outside: number }, ambient: { inside: { hemi: number, env: number }, outside: { hemi: number, env: number } }, ease?: number, owner?: string }} o
|
|
14
|
+
*/
|
|
15
|
+
export function exposureZones(engine, { isInside, exposure, ambient, ease = 1.2, owner = 'zone' }) {
|
|
16
|
+
const zone = { name: '', exposure: 0, target: 0 };
|
|
17
|
+
/** @param {boolean} immediate @param {number} dt */
|
|
18
|
+
const apply = (immediate, dt = 0) => {
|
|
19
|
+
const name = isInside(engine.camera.position) ? 'inside' : 'outside';
|
|
20
|
+
if (name !== zone.name) {
|
|
21
|
+
zone.name = name;
|
|
22
|
+
zone.target = exposure[name];
|
|
23
|
+
engine.look.lights.set({ hemi: { intensity: ambient[name].hemi }, env: { intensity: ambient[name].env } }, { owner });
|
|
24
|
+
}
|
|
25
|
+
const next = immediate || !zone.exposure ? zone.target : zone.exposure + (zone.target - zone.exposure) * Math.min(1, dt / (ease / 3));
|
|
26
|
+
if (Math.abs(next - zone.exposure) > 1e-4) { zone.exposure = next; engine.look.tone.set({ exposure: next }, { owner }); }
|
|
27
|
+
};
|
|
28
|
+
const h = engine.onFrame((/** @type {number} */ dt) => apply(false, dt));
|
|
29
|
+
return { zone, apply, remove() { h?.remove?.(); engine.look.lights.set({ hemi: null, env: null }, { owner }); engine.look.tone.set({ exposure: null }, { owner }); } };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The sky seen through a window as a RectAreaLight in the opening, facing `lookAt`. Soft and window-shaped, but it casts no shadow and lights
|
|
34
|
+
* only MeshStandardMaterial / MeshPhysicalMaterial.
|
|
35
|
+
* @param {any} engine
|
|
36
|
+
* @param {{ position: number[], lookAt: number[], width: number, height: number, color?: THREE.ColorRepresentation, intensity?: number }} o
|
|
37
|
+
*/
|
|
38
|
+
export function windowLight(engine, { position, lookAt, width, height, color = '#cfe0ff', intensity = 2 }) {
|
|
39
|
+
RectAreaLightUniformsLib.init(); // once per page is enough; calling it again is harmless
|
|
40
|
+
const area = new THREE.RectAreaLight(color, intensity, width, height);
|
|
41
|
+
area.position.set(position[0], position[1], position[2]);
|
|
42
|
+
area.lookAt(lookAt[0], lookAt[1], lookAt[2]);
|
|
43
|
+
engine.scene.add(area);
|
|
44
|
+
return area;
|
|
45
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Technique example: interior light (page /interior-light.html, README.md next to this file). A neutral gray room (8 x 6 m, 3 m high) with a
|
|
3
|
+
// window in the west wall and a door in the south wall. One skill: lighting a room from outside (sun through the opening, sky light through the
|
|
4
|
+
// window), filling the shadowed side with bounce, the exposure change between outside and inside, and lamps at night.
|
|
5
|
+
// (time and view are the engine's own URL scenario keys, see core/UrlState.js: page params use other names)
|
|
6
|
+
// URL: ?night=1 ?cam=inside|doorway|outside ?fill=probes|light|none ?area=0 (no window area light) ?leak=1 (keep outdoor ambient indoors)
|
|
7
|
+
// Test page for tests/specs/techniques-light.mjs: window.interiorTest = { THREE, ready, points, zone }.
|
|
8
|
+
import { createEngine, sky, fixedCamera, THREE } from 'threedog';
|
|
9
|
+
import { exposureZones, windowLight } from './interiorLight.js';
|
|
10
|
+
|
|
11
|
+
// ---- parameters: change these ----------------------------------------------------------------------------------------------------------
|
|
12
|
+
const ROOM = { min: [-4, 0, -3], max: [4, 3, 3], wall: 0.2 };
|
|
13
|
+
const WINDOW = { z: [-1.1, 1.1], y: [0.8, 2.4] }; // opening in the west wall (x = -4)
|
|
14
|
+
const DOOR = { x: [1.4, 2.5], y: [0, 2.2] }; // opening in the south wall (z = +3)
|
|
15
|
+
const SUN = { elevation: 32, azimuth: -90 + 12, intensity: 3.2, color: '#fff0dc' }; // azimuth: compass degrees from north (-Z), -90 = from the west
|
|
16
|
+
// outdoor ambient (hemisphere + room environment) is not blocked by walls: indoors it is turned down and the fill comes from bounce instead
|
|
17
|
+
const AMBIENT = { outside: { hemi: 1.0, env: 0.3 }, inside: { hemi: 0.06, env: 0.04 } };
|
|
18
|
+
const WINDOW_LIGHT = { color: '#cfe0ff', intensity: 2.2 }; // RectAreaLight: the sky seen through the window (nits-like units, Standard / Physical only)
|
|
19
|
+
const BOUNCE_LIGHT = { color: '#ffe2c0', intensity: 1.2, distance: 7 }; // ?fill=light: one unshadowed point light standing in for the sun patch's bounce
|
|
20
|
+
// exposure per zone, eased over EASE seconds when the camera crosses the doorway (an eye adapting)
|
|
21
|
+
const EXPOSURE = { day: { outside: 1, inside: 1.9 }, night: { outside: 1.6, inside: 1 }, ease: 1.2 };
|
|
22
|
+
const LAMPS = [{ at: [2.6, 1.55, -2.2], intensity: 5, color: '#ffc58a', shadow: true }, { at: [-2.8, 1.0, 2.2], intensity: 2.5, color: '#ffb874', shadow: false }];
|
|
23
|
+
const VIEWS = {
|
|
24
|
+
inside: { position: [3.4, 1.65, 2.4], lookAt: [-3, 0.8, -0.6] },
|
|
25
|
+
doorway: { position: [1.95, 1.65, 5.2], lookAt: [0.6, 1.1, -3] },
|
|
26
|
+
outside: { position: [6, 1.7, 12], lookAt: [0, 1.2, 0] },
|
|
27
|
+
};
|
|
28
|
+
// ------------------------------------------------------------------------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
const url = new URLSearchParams(location.search);
|
|
31
|
+
const night = url.get('night') === '1', fill = url.get('fill') ?? 'probes', view = /** @type {keyof typeof VIEWS} */ (url.get('cam') ?? 'inside');
|
|
32
|
+
if (!VIEWS[view]) throw new Error(`?cam=${view}: use one of ${Object.keys(VIEWS).join(', ')}`);
|
|
33
|
+
|
|
34
|
+
const engine = createEngine({ canvas: /** @type {HTMLCanvasElement} */ (document.getElementById('view')), ui: { loading: false }, saveKey: 'threedog-interior' });
|
|
35
|
+
engine.use(sky({ time: night ? '22:30' : '15:00' }));
|
|
36
|
+
engine.use(fixedCamera({ shots: VIEWS, start: view }));
|
|
37
|
+
|
|
38
|
+
// ---- the sun through the window: aimed by hand, shadow box fitted to the room (8 m wide: 0.6 cm texels at 2048) ----
|
|
39
|
+
const sun = engine.lights.sun;
|
|
40
|
+
if (!night) {
|
|
41
|
+
engine.look.lights.manual('sun.direction');
|
|
42
|
+
const az = THREE.MathUtils.degToRad(SUN.azimuth), el = THREE.MathUtils.degToRad(SUN.elevation);
|
|
43
|
+
const dir = new THREE.Vector3(Math.sin(az) * Math.cos(el), Math.sin(el), -Math.cos(az) * Math.cos(el));
|
|
44
|
+
sun.target.position.set(0, 0, 0);
|
|
45
|
+
sun.position.copy(dir).multiplyScalar(160);
|
|
46
|
+
engine.look.lights.set({ sun: { intensity: SUN.intensity, color: SUN.color } }, { owner: 'technique' });
|
|
47
|
+
}
|
|
48
|
+
// thin walls need a small normal bias, or light leaks along the wall's inner edge; the bias is ~1 cm (shadow depth 0-1 x the camera's depth range)
|
|
49
|
+
engine.look.shadow.set({ follow: false, extent: 9, normalBias: 0.01, bias: -0.01 / (sun.shadow.camera.far - sun.shadow.camera.near), type: 'pcf', radius: 2 }, { owner: 'technique' });
|
|
50
|
+
|
|
51
|
+
// ---- the neutral gray room (plain three.js) ----
|
|
52
|
+
const mat = (/** @type {number} */ c, roughness = 0.9) => new THREE.MeshStandardMaterial({ color: c, roughness });
|
|
53
|
+
const wallMat = mat(0xb5b5b5), floorMat = mat(0x8f8f8f);
|
|
54
|
+
const box = (/** @type {number[]} */ min, /** @type {number[]} */ max, m = wallMat) => {
|
|
55
|
+
const mesh = new THREE.Mesh(new THREE.BoxGeometry(max[0] - min[0], max[1] - min[1], max[2] - min[2]), m);
|
|
56
|
+
mesh.position.set((min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2);
|
|
57
|
+
mesh.castShadow = mesh.receiveShadow = true;
|
|
58
|
+
engine.scene.add(mesh);
|
|
59
|
+
return mesh;
|
|
60
|
+
};
|
|
61
|
+
const [x0, , z0] = ROOM.min, [x1, h, z1] = ROOM.max, t = ROOM.wall;
|
|
62
|
+
const ground = new THREE.Mesh(new THREE.PlaneGeometry(120, 120), mat(0x8a8a8a, 0.95));
|
|
63
|
+
ground.rotation.x = -Math.PI / 2;
|
|
64
|
+
ground.position.y = -0.01;
|
|
65
|
+
ground.receiveShadow = true;
|
|
66
|
+
engine.scene.add(ground);
|
|
67
|
+
box([x0, -0.01, z0], [x1, 0.02, z1], floorMat); // the floor
|
|
68
|
+
box([x0 - t, h, z0 - t], [x1 + t, h + t, z1 + t]); // the ceiling
|
|
69
|
+
box([x0 - t, 0, z0 - t], [x1 + t, h, z0]); // north wall
|
|
70
|
+
box([x1, 0, z0], [x1 + t, h, z1]); // east wall
|
|
71
|
+
// west wall with the window: four pieces around the opening
|
|
72
|
+
box([x0 - t, 0, z0], [x0, h, WINDOW.z[0]]); box([x0 - t, 0, WINDOW.z[1]], [x0, h, z1]);
|
|
73
|
+
box([x0 - t, 0, WINDOW.z[0]], [x0, WINDOW.y[0], WINDOW.z[1]]); box([x0 - t, WINDOW.y[1], WINDOW.z[0]], [x0, h, WINDOW.z[1]]);
|
|
74
|
+
// south wall with the door
|
|
75
|
+
box([x0 - t, 0, z1], [DOOR.x[0], h, z1 + t]); box([DOOR.x[1], 0, z1], [x1 + t, h, z1 + t]); box([DOOR.x[0], DOOR.y[1], z1], [DOOR.x[1], h, z1 + t]);
|
|
76
|
+
// furniture-sized blocks: a table, a cabinet against the east wall, a sofa-sized block in the corner
|
|
77
|
+
box([-1.2, 0, -0.5], [0.4, 0.75, 0.5], mat(0xa0a0a0)); box([3.2, 0, -1.4], [3.95, 1.9, 0.6], mat(0x9c9c9c)); box([-3.9, 0, 1.4], [-2.2, 0.8, 2.9], mat(0xa6a6a6));
|
|
78
|
+
|
|
79
|
+
// ---- the window as a light: a RectAreaLight in the opening, facing in. Soft and shaped like the window, but it casts no shadow ----
|
|
80
|
+
if (url.get('area') !== '0' && !night) {
|
|
81
|
+
windowLight(engine, { position: [x0 - 0.02, (WINDOW.y[0] + WINDOW.y[1]) / 2, 0], lookAt: [0, (WINDOW.y[0] + WINDOW.y[1]) / 2, 0],
|
|
82
|
+
width: WINDOW.z[1] - WINDOW.z[0], height: WINDOW.y[1] - WINDOW.y[0], ...WINDOW_LIGHT });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ---- night: lamps. The bulb hides inside a shade; the light lands on surfaces, the visible emitter stays dim (no glare without bloom) ----
|
|
86
|
+
if (night) {
|
|
87
|
+
for (const l of LAMPS) {
|
|
88
|
+
const p = new THREE.PointLight(l.color, l.intensity, 0, 2); // physical falloff (decay 2); intensity in candela
|
|
89
|
+
p.position.set(l.at[0], l.at[1], l.at[2]);
|
|
90
|
+
p.castShadow = l.shadow;
|
|
91
|
+
if (l.shadow) { p.shadow.mapSize.set(512, 512); p.shadow.bias = -0.002; p.shadow.radius = 4; }
|
|
92
|
+
const shade = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.26, 0.28, 24, 1, true),
|
|
93
|
+
new THREE.MeshStandardMaterial({ color: 0xe8dccb, emissive: l.color, emissiveIntensity: 0.35, side: THREE.DoubleSide, roughness: 1 }));
|
|
94
|
+
shade.position.copy(p.position);
|
|
95
|
+
const stand = new THREE.Mesh(new THREE.CylinderGeometry(0.02, 0.02, l.at[1] - 0.14, 8), mat(0x777777));
|
|
96
|
+
stand.position.set(l.at[0], (l.at[1] - 0.14) / 2, l.at[2]);
|
|
97
|
+
engine.scene.add(p, shade, stand);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ---- zones: the camera inside the room gets indoor ambient and indoor exposure, eased. Presentation, so it runs per frame ----
|
|
102
|
+
const inside = (/** @type {THREE.Vector3} */ p) => p.x > x0 && p.x < x1 && p.z > z0 && p.z < z1 + 0.5 && p.y < h;
|
|
103
|
+
const leak = url.get('leak') === '1';
|
|
104
|
+
const { zone, apply: applyZone } = exposureZones(engine, { isInside: inside, exposure: EXPOSURE[night ? 'night' : 'day'], ease: EXPOSURE.ease,
|
|
105
|
+
ambient: leak ? { inside: AMBIENT.outside, outside: AMBIENT.outside } : AMBIENT });
|
|
106
|
+
|
|
107
|
+
// ---- fill: bounce baked into light probes, one cheap point light, or nothing ----
|
|
108
|
+
async function setupFill() {
|
|
109
|
+
applyZone(true); // bake with indoor ambient, as the room is meant to be seen
|
|
110
|
+
if (fill === 'probes') {
|
|
111
|
+
// bounds = the room's air; the probe light follows a fixed point in the room (one SH for the whole scene at a time)
|
|
112
|
+
const center = new THREE.Vector3(0, 1.2, 0);
|
|
113
|
+
await engine.bakeProbes({ bounds: { min: [x0 + 0.5, 0.4, z0 + 0.5], max: [x1 - 0.5, h - 0.4, z1 - 0.5] }, spacing: 2.5, follow: () => center });
|
|
114
|
+
} else if (fill === 'light' && !night) {
|
|
115
|
+
const bounce = new THREE.PointLight(BOUNCE_LIGHT.color, BOUNCE_LIGHT.intensity, BOUNCE_LIGHT.distance, 1);
|
|
116
|
+
bounce.position.set(-1.6, 0.4, 0.2); // just above where the sun patch lands
|
|
117
|
+
engine.scene.add(bounce);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// spots the spec reads: the floor along the north wall and the north wall's lower part, both out of the sun patch
|
|
121
|
+
const points = { corner: [-0.5, 0.03, -2.5], northWall: [1.5, 0.6, -2.98] };
|
|
122
|
+
// the bake renders cubes between frames, so it runs after start; tracked in engine.loading so screenshots and tests wait for it
|
|
123
|
+
const ready = engine.start().then(() => engine.loading.track('fill', setupFill));
|
|
124
|
+
Object.assign(window, { engine, interiorTest: { THREE, ready, points, zone, fill, night } });
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
Modular buildings on a grid: parameterized wall / window / door / roof / trim modules, an assembler for buildings and seeded streets, any style.
|
|
2
|
+
|
|
3
|
+
# Modular buildings (technique example)
|
|
4
|
+
|
|
5
|
+
A kit of building pieces made in code, snapped to a grid and assembled into buildings and whole streets, with the colours and materials left to
|
|
6
|
+
the game. The pieces are plain on purpose: the same kit draws a cozy town, a gray block-out or a cold sci-fi row by swapping one set of
|
|
7
|
+
materials and one set of trim profiles. Run it with `npm run dev` and open `/modular-building.html` (`?set=gray|warm|cool`, `seed=1`,
|
|
8
|
+
`count=10`, `view=street|close`, `batch=0|merge`). The engine never imports this folder; copy it and change anything.
|
|
9
|
+
|
|
10
|
+
## The principle
|
|
11
|
+
A town reads from far to near: skyline first, then the rhythm of openings, then trim, then small detail. A grid makes every piece fit every
|
|
12
|
+
slot, and a few repeated pieces are both cheap to draw and what makes a street look built rather than noisy. These are ways to judge your own
|
|
13
|
+
pieces, not fixed recipes; the numbers in `kit.js` are this example's current params, picked for a street seen from a few metres to a few dozen.
|
|
14
|
+
|
|
15
|
+
1. **Block out first: grid and silhouettes.** Decide the cell (one wall module wide) and the storey height from the player's size: block out
|
|
16
|
+
with the player model (or a capsule of its size) in view, or a door that looks fine alone is too short next to the player. Then place
|
|
17
|
+
plain boxes for whole buildings and look at the street from the game's camera. Height, width and roof shape are what read from far away;
|
|
18
|
+
if the skyline is dull, no window detail will save it.
|
|
19
|
+
2. **Then modules.** Replace blank cells with windows and doors. Every module of one kind shares its outline with its neighbours (same cell
|
|
20
|
+
width, same storey height, outer face on the same plane), so any module fits any slot.
|
|
21
|
+
3. **Then trim and detail, big to small.** Big: roof shape, floor count, overhangs. Medium: cornices and floor bands, pilasters on corners,
|
|
22
|
+
awnings, signs. Small: sills, mullions, planters. Each level should still read at the distance where the player sees it; detail smaller
|
|
23
|
+
than a few pixels on screen costs triangles and turns into noise or shimmer.
|
|
24
|
+
4. **One texture language.** Pick one way surfaces get their pattern (flat colour, vertex AO, one kind of canvas mask) and use it everywhere.
|
|
25
|
+
5. **Change style by swapping, not rewriting.** A style is a set of materials plus trim cross-sections plus window variants.
|
|
26
|
+
6. **Variation from a few choices, not continuous noise.** Pick each trait from a short list (floors, width, roof, window variant, trim
|
|
27
|
+
profile, door position, shop front) with a seeded random, and mirror upper floors around the middle so facades look designed. A few
|
|
28
|
+
variants keep geometry shared while no two neighbours repeat; a random size per window makes every window unique (noise, and nothing
|
|
29
|
+
batches). A street of one window reads as copy-paste.
|
|
30
|
+
7. **Facades follow the inside.** A door on a side wall facing another building, or windows where an interior wall would be: if the game
|
|
31
|
+
has interiors, derive facades from the floor plan.
|
|
32
|
+
|
|
33
|
+
How to judge it: a shot at the game's real camera, not a close-up. Does the skyline vary? Do neighbours differ? Does any detail shimmer or
|
|
34
|
+
vanish at that distance? Does anything flicker at seams when the camera moves?
|
|
35
|
+
|
|
36
|
+
## Plain three.js
|
|
37
|
+
- Pieces: `BoxGeometry`, `RoundedBoxGeometry` (addon), and trim strips as an `ExtrudeGeometry` of a `Shape` cross-section
|
|
38
|
+
([ExtrudeGeometry](https://threejs.org/docs/#api/en/geometries/ExtrudeGeometry)). Each module a `Group` placed at integer cell offsets.
|
|
39
|
+
- Many copies: [InstancedMesh](https://threejs.org/docs/#api/en/objects/InstancedMesh) per repeated piece
|
|
40
|
+
([webgl_instancing_performance](https://threejs.org/examples/#webgl_instancing_performance)), or
|
|
41
|
+
[BatchedMesh](https://threejs.org/docs/#api/en/objects/BatchedMesh) for mixed geometries sharing a material
|
|
42
|
+
([webgl_mesh_batch](https://threejs.org/examples/#webgl_mesh_batch)).
|
|
43
|
+
- Merging: `mergeGeometries` from [BufferGeometryUtils](https://threejs.org/docs/#examples/en/utils/BufferGeometryUtils) gives one draw per
|
|
44
|
+
material, at the cost of culling and per-piece edits.
|
|
45
|
+
- Seams: avoid coplanar faces; if one is unavoidable, `material.polygonOffset` is the last resort, not the plan.
|
|
46
|
+
|
|
47
|
+
## Style routes
|
|
48
|
+
- **Gray block-out**: boxes and `.` cells only; judges silhouette and scale before any art. Cheapest.
|
|
49
|
+
- **Cozy / storybook**: soft cove trims, rounded edges, warm slot materials, awnings and planters. More trim strips (one-off geometry).
|
|
50
|
+
- **Cold sci-fi / brutalist**: hard stepped trims, flat roofs, faintly emissive frames. Fewer pieces, more repetition.
|
|
51
|
+
- **Low-poly flat**: flat colour per slot, vertex AO for depth, no textures. Very cheap.
|
|
52
|
+
- **Hand-painted / textured**: one texture set per slot from a DCC tool, the kit only for layout. Texture memory instead of geometry.
|
|
53
|
+
- **Merged hero block**: a fixed set piece merged once for the fewest draws, when it never changes.
|
|
54
|
+
|
|
55
|
+
## The engine helper (optional convenience)
|
|
56
|
+
`kit.js` is the kit: modules, assembler, manifest entries; it imports only `three` and `three/addons`. `main.js` is the demo page; it builds the
|
|
57
|
+
same seed three times (gray block-out, warm with soft cove trims, cool with hard stepped trims and faintly emissive frames; the two coloured
|
|
58
|
+
sets are demo values, not advice). In the kit, the block-out stage is `facade` strings of `.` (blank walls) and `roof`. A style is the slot
|
|
59
|
+
materials (`wall`, `trim`, `roof`, `frame`, `glass`, `accent`, `awning`, `sign`) plus `profiles` plus `windows`. The only textures are gray
|
|
60
|
+
masks (awning stripes, sign text) tinted by the slot material's colour, so a material swap recolours them too. `street` mirrors upper floors
|
|
61
|
+
and picks traits with a seeded `rng`.
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { createModularKit, PROFILES, WINDOWS } from './kit.js';
|
|
65
|
+
const kit = createModularKit({
|
|
66
|
+
slots: { wall, trim, roof, frame, glass, accent, awning, sign }, // the game's materials; only wall is required, the rest fall back
|
|
67
|
+
cell: 2, storey: 3, wall: 0.2, // grid cell, floor height, wall thickness (metres)
|
|
68
|
+
profiles: PROFILES, windows: WINDOWS, // trim cross-sections and window variants: swap them per style
|
|
69
|
+
ao: { bake: bakeVertexAO, samples: 24 }, // optional: threedog's bakeVertexAO per module (slots then need vertexColors)
|
|
70
|
+
});
|
|
71
|
+
engine.assets.manifest(kit.pack); // mb-wall, mb-window, mb-door, mb-corner, mb-trim, mb-gable, mb-flatRoof, mb-awning,
|
|
72
|
+
// mb-sign, mb-chimney, mb-building, mb-street; all { build, params }
|
|
73
|
+
// content/props.js: { model: ['mb-street', { seed: 7, count: 12 }], x: 0, z: 0 } or { model: ['mb-building', { floors: 3 }], ... }
|
|
74
|
+
const b = kit.building({ width: 3, depth: 2, floors: 2, front: 'SDS/WWW', back: 'W.W', roof: 'gable', trim: 'cove', sign: 'BAKERY' });
|
|
75
|
+
const s = kit.street({ seed: 7, count: 10, gap: 1 });
|
|
76
|
+
s.userData.batch = true; engine.scene.add(s); // opt in to engine.batching (recipe 05, 2b)
|
|
77
|
+
engine.scene.add(kit.merge(s)); // or: one mesh per material (see costs)
|
|
78
|
+
kit.modules.window({ width: 1.1, height: 1.5, sill: 0.8, mullions: 2, planter: true }); // any single module as a Group
|
|
79
|
+
```
|
|
80
|
+
- Each module is a `Group` with one mesh per slot (pieces of the same slot are merged), identity transforms, geometry shared by every copy
|
|
81
|
+
with the same params. `userData.module = { type, side, floor, i }` names each placed module; the wall slab mesh is named `wall`.
|
|
82
|
+
- Because every piece is a registered `{ build, params }` entry, `threedog gallery` shows each module on a turntable, saving `kit.js` swaps
|
|
83
|
+
the pieces in a running game, and `?panel=1` drags the params (`threedog tune write` writes them back).
|
|
84
|
+
- `kit.dispose()` frees the geometries, textures and textured materials it made; the slot materials are the game's.
|
|
85
|
+
|
|
86
|
+
Costs (seed 1, 10 buildings, 960x540, `npm run shot`, 2026-11-02). Draw calls as `describe().perf` counts them (the shadow pass included),
|
|
87
|
+
about 74k triangles in every case:
|
|
88
|
+
|
|
89
|
+
| | draw calls |
|
|
90
|
+
|---|---|
|
|
91
|
+
| plain meshes (`?batch=0`) | 1377 |
|
|
92
|
+
| `engine.batching` (`userData.batch = true`, the default page) | 308 |
|
|
93
|
+
| `kit.merge` (`?batch=merge`) | 24 |
|
|
94
|
+
|
|
95
|
+
- `engine.batching` instances meshes that repeat at least 8 times in a 64 m cell: windows, walls, corners. What stays single is one-off
|
|
96
|
+
geometry: each building's roof and trim strips (their length is the building's), signs. Fewer distinct sizes = fewer draws.
|
|
97
|
+
- `kit.merge` gives one draw per material, but the whole merged street is drawn whenever any of it is on screen, and single pieces can no
|
|
98
|
+
longer be moved or swapped. Merge per block (a few buildings) in a large town so culling still works.
|
|
99
|
+
- Rounded edges use `RoundedBoxGeometry` with one segment (108 triangles per piece instead of 300 with two): a chamfer with smooth normals
|
|
100
|
+
catches the same highlight, and sills / pilasters / copings repeat hundreds of times.
|
|
101
|
+
- This kit's 4 cm mullions are fine up close and vanish or shimmer a few dozen metres away: size each level of detail for your camera.
|
|
102
|
+
|
|
103
|
+
Rules (correctness):
|
|
104
|
+
- **Every facade module spans exactly one cell and one storey, with its outer face at module z = 0**, details in front (+z), the slab behind.
|
|
105
|
+
A module that breaks this leaves gaps or overlaps that the assembler cannot fix.
|
|
106
|
+
- **Building origins land on whole cells** (`street` places each building at an integer cell offset). Off-grid origins break the snapping
|
|
107
|
+
the spec checks.
|
|
108
|
+
- **A facade row has one letter per footprint cell** (`W` window, `S` shop window with awning, `D` door, `.` blank); rows are separated by `/`,
|
|
109
|
+
ground floor first, and the last row repeats upward. A wrong length or a door above the ground floor throws, naming the side.
|
|
110
|
+
- **No two visible faces share a plane.** Two faces in one plane flicker when the camera moves; never rely on "same colour, nobody will
|
|
111
|
+
notice". Where pieces meet, either one covers the seam (corner pilasters cover the wall ends; front / back trims run past the corners,
|
|
112
|
+
side trims stop at the walls) or they are offset by a few millimetres (the sill top sits above the reveal floor).
|
|
113
|
+
- **Generators get every input from `params`.** The slot materials are live objects, so the kit's manifest entries are `cache: false`
|
|
114
|
+
(the dev cache would otherwise keep a model built with old materials). To bake a street, make the slots `makeMaterial` roles inside the
|
|
115
|
+
generator instead (recipe 05, section 2a) and drop `cache: false`.
|
|
116
|
+
|
|
117
|
+
Limits:
|
|
118
|
+
- Footprints are rectangles of cells (`width` x `depth`); an L-shaped building needs `building` extended to take a cell list (two buildings
|
|
119
|
+
with `gap: 0` meet wall to wall, and their corner pilasters then overlap).
|
|
120
|
+
- Gable ridges run along the building's width; hip roofs, dormers and balconies are not in the kit (each is one more module).
|
|
121
|
+
- The test is `npm test -- "modular building"` (tests/specs/modular-building.mjs): determinism per seed, grid snapping without gaps,
|
|
122
|
+
shared geometry, batching under 400 draw calls, params changing shape, the optional AO pass.
|
|
123
|
+
|
|
124
|
+
Sources: written for this example; no code copied. Background: the grid-snapped kit of reusable pieces is common modular level design practice
|
|
125
|
+
(Joel Burgess, "Skyrim's Modular Approach to Level Design", GDC 2013); `RoundedBoxGeometry` and `mergeGeometries` are three.js addons.
|