threedog 0.0.0-stage → 0.2.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 +5 -0
- package/LICENSE +21 -0
- package/README.md +39 -2
- package/core/Animator.js +268 -0
- package/core/Assets.js +256 -0
- package/core/Audio.js +267 -0
- package/core/Baked.js +72 -0
- package/core/Batching.js +247 -0
- package/core/Budget.js +221 -0
- package/core/Clock.js +94 -0
- package/core/Commits.js +77 -0
- package/core/DebugOverrides.js +143 -0
- package/core/Describe.js +145 -0
- package/core/Engine.js +184 -0
- package/core/Events.js +153 -0
- package/core/I18n.js +94 -0
- package/core/Input.js +245 -0
- package/core/Inspect.js +77 -0
- package/core/InspectScene.js +191 -0
- package/core/Jobs.js +146 -0
- package/core/Loading.js +149 -0
- package/core/MaterialInfo.js +32 -0
- package/core/Ops.js +256 -0
- package/core/Random.js +52 -0
- package/core/Rays.js +160 -0
- package/core/SaveStore.js +85 -0
- package/core/Saves.js +254 -0
- package/core/Stream.js +279 -0
- package/core/Threads.js +158 -0
- package/core/UrlState.js +94 -0
- package/core/Vars.js +35 -0
- package/core/Views.js +49 -0
- package/core/actor.js +23 -0
- package/core/assetEntry.js +89 -0
- package/core/assetLoad.js +190 -0
- package/core/assetManifest.js +35 -0
- package/core/assetReport.js +83 -0
- package/core/audioRules.js +121 -0
- package/core/audioVoice.js +225 -0
- package/core/batchGroups.js +116 -0
- package/core/canvasBox.js +63 -0
- package/core/catalog.js +169 -0
- package/core/checks.js +67 -0
- package/core/claims.js +51 -0
- package/core/dayTime.js +15 -0
- package/core/debugView.js +76 -0
- package/core/describeRender.js +45 -0
- package/core/describeText.js +143 -0
- package/core/easing.js +34 -0
- package/core/errorCodes.js +205 -0
- package/core/errorHints.js +208 -0
- package/core/errors.js +40 -0
- package/core/events.generated.js +84 -0
- package/core/ground.js +102 -0
- package/core/holds.js +106 -0
- package/core/inputContext.js +69 -0
- package/core/inputDefaults.js +22 -0
- package/core/inputPad.js +35 -0
- package/core/inputPointer.js +48 -0
- package/core/inputTouch.js +48 -0
- package/core/lifecycle.js +69 -0
- package/core/lightWatch.js +56 -0
- package/core/look/coreParts.js +112 -0
- package/core/look/layers.js +136 -0
- package/core/look/part.js +173 -0
- package/core/look/report.js +79 -0
- package/core/look/write.js +86 -0
- package/core/loop.js +200 -0
- package/core/math.js +33 -0
- package/core/modules.js +196 -0
- package/core/movement.js +19 -0
- package/core/mulberry32.js +14 -0
- package/core/opsAtomic.js +31 -0
- package/core/opsCheck.js +97 -0
- package/core/opsFail.js +20 -0
- package/core/opsGuard.js +26 -0
- package/core/opsHistory.js +27 -0
- package/core/opsRun.js +77 -0
- package/core/page.js +78 -0
- package/core/params.js +132 -0
- package/core/pureWorker.js +5 -0
- package/core/renamed.js +21 -0
- package/core/saveApply.js +128 -0
- package/core/saveFiles.js +139 -0
- package/core/saveIds.js +53 -0
- package/core/saveVersions.js +97 -0
- package/core/scatter.js +92 -0
- package/core/sceneMethods.js +100 -0
- package/core/screenToGround.js +39 -0
- package/core/services.js +73 -0
- package/core/session.js +171 -0
- package/core/shadowTypes.js +15 -0
- package/core/skip.js +104 -0
- package/core/startup.js +135 -0
- package/core/streamCells.js +24 -0
- package/core/streamSources.js +73 -0
- package/core/styleParts.js +96 -0
- package/core/targeting.js +64 -0
- package/core/thingExplain.js +68 -0
- package/core/thingOps.js +168 -0
- package/core/things.js +212 -0
- package/core/threadPure.js +120 -0
- package/core/threadServe.js +25 -0
- package/core/tier.js +11 -0
- package/core/util.js +11 -0
- package/core/validate.js +204 -0
- package/docs/AI.md +123 -0
- package/docs/API.md +83 -0
- package/docs/CODEMAP.md +347 -0
- package/docs/COMMANDS.md +108 -0
- package/docs/CONVENTIONS.md +145 -0
- package/docs/PERF.md +217 -0
- package/docs/THREE.md +86 -0
- package/docs/api/ambience.md +15 -0
- package/docs/api/assets.md +62 -0
- package/docs/api/audio.md +53 -0
- package/docs/api/budget.md +40 -0
- package/docs/api/cameraRig.md +120 -0
- package/docs/api/characters.md +144 -0
- package/docs/api/clock.md +7 -0
- package/docs/api/collision.md +39 -0
- package/docs/api/content.md +64 -0
- package/docs/api/dialogue.md +39 -0
- package/docs/api/engine.md +199 -0
- package/docs/api/entities.md +61 -0
- package/docs/api/events.md +95 -0
- package/docs/api/flow.md +27 -0
- package/docs/api/grids.md +11 -0
- package/docs/api/ground.md +16 -0
- package/docs/api/hlod.md +34 -0
- package/docs/api/hud.md +31 -0
- package/docs/api/i18n.md +10 -0
- package/docs/api/input.md +24 -0
- package/docs/api/interactions.md +31 -0
- package/docs/api/inventory.md +66 -0
- package/docs/api/jobs.md +10 -0
- package/docs/api/level.md +41 -0
- package/docs/api/lightPool.md +56 -0
- package/docs/api/music.md +34 -0
- package/docs/api/nav.md +26 -0
- package/docs/api/net.md +14 -0
- package/docs/api/openWorld.md +24 -0
- package/docs/api/particles.md +41 -0
- package/docs/api/photo.md +15 -0
- package/docs/api/post.md +10 -0
- package/docs/api/quests.md +36 -0
- package/docs/api/rays.md +26 -0
- package/docs/api/rigid.md +84 -0
- package/docs/api/saves.md +32 -0
- package/docs/api/settings.md +36 -0
- package/docs/api/sky.md +58 -0
- package/docs/api/stream.md +31 -0
- package/docs/api/style.md +134 -0
- package/docs/api/terrain.md +34 -0
- package/docs/api/timeline.md +39 -0
- package/docs/api/tools.md +57 -0
- package/docs/api/triggers.md +56 -0
- package/docs/api/vars.md +9 -0
- package/docs/api/vegetation.md +52 -0
- package/docs/api/water.md +48 -0
- package/docs/api/weather.md +29 -0
- package/docs/api/world.md +30 -0
- package/docs/recipes/01-new-game.md +148 -0
- package/docs/recipes/02-npc.md +167 -0
- package/docs/recipes/03-items-pickups.md +158 -0
- package/docs/recipes/04-quests.md +83 -0
- package/docs/recipes/05-props-bake.md +159 -0
- package/docs/recipes/06-clock-events.md +117 -0
- package/docs/recipes/07-flow-scenes-pause.md +132 -0
- package/docs/recipes/08-weather-sky.md +74 -0
- package/docs/recipes/09-style-pack.md +234 -0
- package/docs/recipes/10-blender-level.md +67 -0
- package/docs/recipes/11-vehicles.md +55 -0
- package/docs/recipes/12-navmesh.md +79 -0
- package/docs/recipes/13-animation.md +149 -0
- package/docs/recipes/14-custom-module.md +91 -0
- package/docs/recipes/15-perf.md +78 -0
- package/docs/recipes/16-dev-bridge.md +180 -0
- package/docs/recipes/18-world.md +185 -0
- package/docs/recipes/19-shooting-picking.md +76 -0
- package/docs/recipes/20-content-actions-save.md +176 -0
- package/docs/recipes/21-camera.md +153 -0
- package/docs/recipes/22-shop-gold.md +107 -0
- package/docs/recipes/23-ops.md +175 -0
- package/docs/recipes/26-ui-parts.md +138 -0
- package/docs/recipes/27-net.md +96 -0
- package/docs/recipes/28-terrain.md +80 -0
- package/docs/recipes/29-grid.md +87 -0
- package/docs/recipes/30-templates.md +131 -0
- package/docs/recipes/31-audio.md +126 -0
- package/docs/recipes/32-action-game.md +33 -0
- package/docs/recipes/33-stream-sources.md +37 -0
- package/docs/recipes/34-environment-lighting.md +36 -0
- package/docs/recipes/35-style-from-reference.md +89 -0
- package/docs/recipes/36-sky-ground-plant-looks.md +116 -0
- package/docs/recipes/37-terrain-look-hooks.md +97 -0
- package/docs/recipes/38-environment-look-hooks.md +83 -0
- package/docs/recipes/40-scene.md +108 -0
- package/docs/recipes/41-thing-registry.md +93 -0
- package/docs/recipes/42-terrain-shape.md +64 -0
- package/docs/recipes/43-shader-patches-surface-looks.md +120 -0
- package/docs/recipes/44-water-weather-look-hooks.md +135 -0
- package/docs/recipes/45-cascaded-shadows.md +51 -0
- package/docs/recipes/46-blender-art-route.md +114 -0
- package/docs/recipes/47-island-snow-backdrop.md +44 -0
- package/docs/recipes/48-make-material.md +70 -0
- package/docs/recipes/49-game-module-contract.md +71 -0
- package/docs/recipes/50-own-renderer.md +12 -0
- package/docs/recipes/51-capability-catalog.md +63 -0
- package/docs/recipes/52-look.md +108 -0
- package/examples/action/DESIGN.md +3 -0
- package/examples/action/GAMEMAP.md +127 -0
- package/examples/action/content/actions.js +8 -0
- package/examples/action/content/audio.js +9 -0
- package/examples/action/content/index.js +10 -0
- package/examples/action/content/items.js +4 -0
- package/examples/action/content/places.js +4 -0
- package/examples/action/content/props.js +11 -0
- package/examples/action/content/templates.js +10 -0
- package/examples/action/content/views.js +6 -0
- package/examples/action/look.js +5 -0
- package/examples/action/main.js +137 -0
- package/examples/action/models.js +25 -0
- package/examples/sheepisland/GAMEMAP.md +107 -0
- package/examples/sheepisland/content/index.js +6 -0
- package/examples/sheepisland/content/props.js +19 -0
- package/examples/sheepisland/content/templates.js +5 -0
- package/examples/sheepisland/content/vegetation.js +8 -0
- package/examples/sheepisland/main.js +71 -0
- package/examples/sheepisland/params.js +25 -0
- package/examples/sheepisland/style/index.js +79 -0
- package/examples/sheepisland/style/models.js +59 -0
- package/examples/sheepisland/views.js +9 -0
- package/examples/styletest/GAMEMAP.md +135 -0
- package/examples/styletest/content/index.js +9 -0
- package/examples/styletest/content/items.js +8 -0
- package/examples/styletest/content/npcs.js +8 -0
- package/examples/styletest/content/places.js +5 -0
- package/examples/styletest/content/props.js +16 -0
- package/examples/styletest/content/templates.js +9 -0
- package/examples/styletest/content/vegetation.js +31 -0
- package/examples/styletest/look.js +11 -0
- package/examples/styletest/main.js +95 -0
- package/examples/styletest/models.js +87 -0
- package/examples/styletest/overrides.js +33 -0
- package/examples/styletest/panel.js +135 -0
- package/examples/styletest/params.js +32 -0
- package/examples/styletest/views.js +31 -0
- package/examples/world/DESIGN.md +5 -0
- package/examples/world/GAMEMAP.md +171 -0
- package/examples/world/content/actions.js +8 -0
- package/examples/world/content/audio.js +25 -0
- package/examples/world/content/bodies.js +12 -0
- package/examples/world/content/containers.js +4 -0
- package/examples/world/content/dialogues.js +33 -0
- package/examples/world/content/index.js +17 -0
- package/examples/world/content/items.js +10 -0
- package/examples/world/content/npcs.js +14 -0
- package/examples/world/content/pickups.js +11 -0
- package/examples/world/content/places.js +6 -0
- package/examples/world/content/props.js +12 -0
- package/examples/world/content/quests.js +15 -0
- package/examples/world/content/rules.js +23 -0
- package/examples/world/content/templates.js +5 -0
- package/examples/world/content/vegetation.js +32 -0
- package/examples/world/main.js +168 -0
- package/examples/world/models.js +28 -0
- package/index.d.ts +306 -0
- package/index.js +122 -0
- package/llms.txt +61 -0
- package/modules/ambience/Ambience.js +186 -0
- package/modules/camera/FirstPersonCamera.js +131 -0
- package/modules/camera/FixedCamera.js +162 -0
- package/modules/camera/FollowCamera.js +91 -0
- package/modules/camera/RtsCamera.js +132 -0
- package/modules/camera/common.js +182 -0
- package/modules/camera/index.js +11 -0
- package/modules/camera/projection.js +34 -0
- package/modules/characters/Character.js +235 -0
- package/modules/characters/NPC.js +242 -0
- package/modules/characters/assetModel.js +42 -0
- package/modules/characters/astar.js +66 -0
- package/modules/characters/behaviors.js +72 -0
- package/modules/characters/footsteps.js +13 -0
- package/modules/characters/index.js +215 -0
- package/modules/characters/moveHelpers.js +29 -0
- package/modules/characters/movers/common.js +62 -0
- package/modules/characters/movers/rapier.js +74 -0
- package/modules/characters/movers/simple.js +43 -0
- package/modules/characters/npcAvoid.js +129 -0
- package/modules/characters/npcSim.js +150 -0
- package/modules/characters/npcView.js +42 -0
- package/modules/characters/pathAsync.js +113 -0
- package/modules/characters/pathJob.js +62 -0
- package/modules/characters/pathWorker.js +7 -0
- package/modules/characters/playerStrings.js +12 -0
- package/modules/characters/proceduralRig.js +58 -0
- package/modules/characters/useModel.js +22 -0
- package/modules/characters/walkTo.js +58 -0
- package/modules/collision/Collision.js +274 -0
- package/modules/collision/grid.js +63 -0
- package/modules/content/explain.js +13 -0
- package/modules/content/ids.js +95 -0
- package/modules/content/index.js +154 -0
- package/modules/content/loader.js +190 -0
- package/modules/content/models.js +69 -0
- package/modules/content/packs.js +69 -0
- package/modules/content/props.js +89 -0
- package/modules/content/rules.js +134 -0
- package/modules/content/templates.js +189 -0
- package/modules/debug/DebugPanel.js +174 -0
- package/modules/entities/Entities.js +243 -0
- package/modules/entities/build.js +25 -0
- package/modules/entities/index.js +4 -0
- package/modules/flow/index.js +101 -0
- package/modules/gameplay/Container.js +227 -0
- package/modules/gameplay/Containers.js +89 -0
- package/modules/gameplay/Dialogue.js +219 -0
- package/modules/gameplay/Interactions.js +253 -0
- package/modules/gameplay/Inventory.js +224 -0
- package/modules/gameplay/Quests.js +205 -0
- package/modules/gameplay/boxOps.js +60 -0
- package/modules/gameplay/dialogueBox.js +48 -0
- package/modules/gameplay/interactionPrompt.js +28 -0
- package/modules/gameplay/inventoryOps.js +137 -0
- package/modules/gameplay/pickups.js +115 -0
- package/modules/gameplay/questLog.js +54 -0
- package/modules/gameplay/questSave.js +71 -0
- package/modules/grid/Grid.js +184 -0
- package/modules/grid/Layer.js +142 -0
- package/modules/grid/index.js +236 -0
- package/modules/grid/ops.js +73 -0
- package/modules/grid/preview.js +85 -0
- package/modules/grid/tiles.js +79 -0
- package/modules/hlod/index.js +167 -0
- package/modules/hud/index.js +167 -0
- package/modules/hud/parts.js +80 -0
- package/modules/level/geometry.js +46 -0
- package/modules/level/heightfield.js +160 -0
- package/modules/level/index.js +233 -0
- package/modules/level/nodes.js +84 -0
- package/modules/lights/index.js +227 -0
- package/modules/music/Music.js +294 -0
- package/modules/music/index.js +2 -0
- package/modules/music/rules.js +28 -0
- package/modules/nav/index.js +156 -0
- package/modules/nav/navPath.js +17 -0
- package/modules/net/Client.js +137 -0
- package/modules/net/Host.js +117 -0
- package/modules/net/Net.js +49 -0
- package/modules/net/index.js +77 -0
- package/modules/net/sync.js +114 -0
- package/modules/net/transports.js +118 -0
- package/modules/particles/Particles.js +179 -0
- package/modules/particles/effect.js +112 -0
- package/modules/particles/shaders.js +28 -0
- package/modules/photo/Photo.js +146 -0
- package/modules/rigid/RigidBodies.js +265 -0
- package/modules/rigid/Vehicles.js +97 -0
- package/modules/rigid/sync.js +158 -0
- package/modules/settings/Settings.js +297 -0
- package/modules/settings/apply.js +21 -0
- package/modules/settings/index.js +3 -0
- package/modules/settings/sections.js +156 -0
- package/modules/settings/strings.js +53 -0
- package/modules/settings/view.js +65 -0
- package/modules/sky/Sky.js +259 -0
- package/modules/sky/calendar.js +80 -0
- package/modules/sky/cascades.js +125 -0
- package/modules/sky/clouds.js +79 -0
- package/modules/sky/domeLayers.js +33 -0
- package/modules/sky/environment.js +141 -0
- package/modules/sky/fog.js +139 -0
- package/modules/sky/frame.js +77 -0
- package/modules/sky/layers.js +69 -0
- package/modules/sky/look.js +51 -0
- package/modules/sky/lookFog.js +80 -0
- package/modules/sky/lookShadow.js +50 -0
- package/modules/sky/lookSky.js +108 -0
- package/modules/sky/schedule.js +80 -0
- package/modules/sky/shader.js +96 -0
- package/modules/sky/shadow.js +74 -0
- package/modules/sky/skyModule.js +60 -0
- package/modules/terrain/ChunkManager.js +260 -0
- package/modules/terrain/chunkJob.js +19 -0
- package/modules/terrain/chunkLevels.js +40 -0
- package/modules/terrain/chunkQueries.js +42 -0
- package/modules/terrain/chunkWorker.js +5 -0
- package/modules/terrain/index.js +118 -0
- package/modules/terrain/lookGround.js +61 -0
- package/modules/terrain/mountTerrain.js +32 -0
- package/modules/terrain/terrainColorMap.js +41 -0
- package/modules/terrain/terrainDefaults.js +39 -0
- package/modules/terrain/terrainEdit.js +139 -0
- package/modules/terrain/terrainGen.js +155 -0
- package/modules/terrain/terrainIsland.js +131 -0
- package/modules/terrain/terrainMesh.js +92 -0
- package/modules/terrain/terrainShape.js +108 -0
- package/modules/terrain/terrainShapeMain.js +41 -0
- package/modules/terrain/terrainTexture.js +44 -0
- package/modules/timeline/Timeline.js +253 -0
- package/modules/timeline/Tween.js +114 -0
- package/modules/timeline/letterbox.js +24 -0
- package/modules/timeline/shots.js +90 -0
- package/modules/triggers/index.js +268 -0
- package/modules/vegetation/Vegetation.js +256 -0
- package/modules/vegetation/groundChunks.js +177 -0
- package/modules/vegetation/index.js +43 -0
- package/modules/vegetation/lookWind.js +37 -0
- package/modules/vegetation/modelGeometry.js +21 -0
- package/modules/water/Water.js +160 -0
- package/modules/water/look.js +80 -0
- package/modules/water/surface.js +88 -0
- package/modules/weather/Weather.js +242 -0
- package/modules/weather/look.js +75 -0
- package/modules/weather/particles.js +92 -0
- package/modules/weather/shaders.js +49 -0
- package/modules/weather/strings.js +15 -0
- package/modules/world/World.js +231 -0
- package/modules/world/cells.js +42 -0
- package/modules/world/explain.js +11 -0
- package/modules/world/index.js +71 -0
- package/modules/world/persist.js +107 -0
- package/modules/world/pos.js +20 -0
- package/modules/world/records.js +89 -0
- package/modules/world/sim.js +175 -0
- package/modules/world/simFar.js +82 -0
- package/modules/world/state.js +115 -0
- package/modules/world/thingSource.js +44 -0
- package/modules/world/views.js +182 -0
- package/package.json +66 -4
- package/presets.js +96 -0
- package/render/PostFX.js +240 -0
- package/render/detail.js +105 -0
- package/render/effects.js +144 -0
- package/render/engineUniforms.js +23 -0
- package/render/fuse.js +84 -0
- package/render/lighting.js +100 -0
- package/render/lights.js +18 -0
- package/render/lookSurface.js +161 -0
- package/render/materials.js +220 -0
- package/render/patches.js +252 -0
- package/render/postLook.js +86 -0
- package/render/surface.js +69 -0
- package/render/uniformDecl.js +29 -0
- package/types/common.d.ts +34 -0
- package/types/core.d.ts +1965 -0
- package/types/gameplay.d.ts +1822 -0
- package/types/generated/core/Audio.d.ts +200 -0
- package/types/generated/core/Batching.d.ts +47 -0
- package/types/generated/core/Budget.d.ts +90 -0
- package/types/generated/core/Commits.d.ts +31 -0
- package/types/generated/core/DebugOverrides.d.ts +52 -0
- package/types/generated/core/Events.d.ts +89 -0
- package/types/generated/core/I18n.d.ts +30 -0
- package/types/generated/core/Input.d.ts +118 -0
- package/types/generated/core/Jobs.d.ts +41 -0
- package/types/generated/core/Ops.d.ts +203 -0
- package/types/generated/core/Random.d.ts +49 -0
- package/types/generated/core/Rays.d.ts +49 -0
- package/types/generated/core/Saves.d.ts +162 -0
- package/types/generated/core/Stream.d.ts +93 -0
- package/types/generated/core/Threads.d.ts +82 -0
- package/types/generated/core/Views.d.ts +33 -0
- package/types/generated/core/audioVoice.d.ts +47 -0
- package/types/generated/core/page.d.ts +18 -0
- package/types/generated/core/session.d.ts +48 -0
- package/types/generated/modules/ambience/Ambience.d.ts +48 -0
- package/types/generated/modules/debug/DebugPanel.d.ts +24 -0
- package/types/generated/modules/flow/index.d.ts +10 -0
- package/types/generated/modules/grid/Grid.d.ts +151 -0
- package/types/generated/modules/grid/Layer.d.ts +65 -0
- package/types/generated/modules/grid/index.d.ts +86 -0
- package/types/generated/modules/hlod/index.d.ts +11 -0
- package/types/generated/modules/hud/index.d.ts +10 -0
- package/types/generated/modules/lights/index.d.ts +13 -0
- package/types/generated/modules/music/Music.d.ts +111 -0
- package/types/generated/modules/net/Net.d.ts +34 -0
- package/types/generated/modules/net/index.d.ts +53 -0
- package/types/generated/modules/net/transports.d.ts +58 -0
- package/types/generated/modules/particles/Particles.d.ts +69 -0
- package/types/generated/modules/sky/Sky.d.ts +145 -0
- package/types/generated/modules/sky/skyModule.d.ts +10 -0
- package/types/generated/modules/timeline/Timeline.d.ts +51 -0
- package/types/generated/modules/water/Water.d.ts +29 -0
- package/types/generated/modules/weather/Weather.d.ts +62 -0
- package/types/generated/modules/world/World.d.ts +242 -0
- package/types/generated/modules/world/index.d.ts +15 -0
- package/types/generated/render/PostFX.d.ts +125 -0
- package/types/generated/render/materials.d.ts +100 -0
- package/types/generated/render/patches.d.ts +41 -0
- package/types/generated/ui/Focus.d.ts +11 -0
- package/types/generated/ui/LoadingScreen.d.ts +2 -0
- package/types/generated/ui/UI.d.ts +57 -0
- package/types/nav.d.ts +52 -0
- package/types/physics.d.ts +592 -0
- package/types/style.d.ts +638 -0
- package/types/worker.d.ts +11 -0
- package/types/world.d.ts +772 -0
- package/ui/Focus.js +51 -0
- package/ui/LoadingScreen.js +42 -0
- package/ui/Parts.js +199 -0
- package/ui/UI.js +167 -0
- package/ui/defaults.js +85 -0
- package/ui/theme.js +124 -0
- package/ui/touchPart.js +49 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// Particles module: dust, falling leaves, splashes, chimney smoke, sparks... one effect = one THREE.Points (one draw call).
|
|
2
|
+
// Particles live in a ring buffer (the oldest are overwritten when full), updated on the CPU every frame with fixed-length arrays; no object per particle.
|
|
3
|
+
// Particles are affected by fog and do not write depth (they do not hide translucent objects behind); an effect with no live particles is not drawn at all.
|
|
4
|
+
//
|
|
5
|
+
// engine.use(particles());
|
|
6
|
+
// engine.particles.define('dust', { max: 300, lifetime: [0.6, 1.2], speed: [0.5, 1.5], direction: [0, 1, 0], spread: 1.2,
|
|
7
|
+
// gravity: -1, drag: 1.5, size: [0.3, 0.8], color: [0xbbbbbb, 0xdddddd], opacity: [0.6, 0] });
|
|
8
|
+
// engine.particles.emit('dust', { x, y, z }, { count: 20 }); // burst of 20 at once
|
|
9
|
+
// engine.particles.emit('dust', pos, { count: 10, overrides: { color: [0xff8800, 0x552200] } }); // temporary param overrides (this batch only)
|
|
10
|
+
// const h = engine.particles.attach('smoke', chimney, { rate: 8, offset: [0, 2, 0] }); // keep emitting while following an object
|
|
11
|
+
// h.remove(); h.rate = 3; h.active = false;
|
|
12
|
+
// engine.particles.count('dust') // live particle count; no name = all
|
|
13
|
+
//
|
|
14
|
+
// Params (all optional; below are the neutral fallback values):
|
|
15
|
+
// max 120 max particles alive at once (halved when engine.tier is 'low')
|
|
16
|
+
// lifetime [1,2] seconds each particle lives (random range)
|
|
17
|
+
// rate 10 particles per second by default for attach
|
|
18
|
+
// speed [0.5,1.5] initial speed (m/s); direction [0,1,0] emit direction; spread 0.5 max angle off the direction (radians, pi = all directions)
|
|
19
|
+
// area 0 radius the spawn point is randomly spread over (meters); a box [x,y,z] also works
|
|
20
|
+
// gravity -2 vertical acceleration (negative falls, positive rises, e.g. smoke)
|
|
21
|
+
// drag 0.5 air drag (fraction of speed lost per second)
|
|
22
|
+
// wind 0 how much wind pushes it (follows engine.uniforms.uWind strength, direction windDir); windDir [1,0,0.3]
|
|
23
|
+
// size [0.3,0.3] size (meters) at birth / at death
|
|
24
|
+
// color [0xcccccc,0xcccccc] color at birth / at death
|
|
25
|
+
// opacity [0.8,0] opacity at birth / at death
|
|
26
|
+
// texture asset name (a texture registered in engine.assets) or URL; default is a soft round dot
|
|
27
|
+
// blending 'normal' | 'additive' (use additive for sparks and magic motes)
|
|
28
|
+
// spin 0 texture rotation speed (radians/s, random sign) - for falling leaves
|
|
29
|
+
// worldSpace true with attach, particles stay in the world after birth (smoke trails behind); false = emit direction rotates with the object (e.g. exhaust, muzzle).
|
|
30
|
+
// after birth particles always move in world coordinates and are not dragged along by the object
|
|
31
|
+
// fog true affected by fog
|
|
32
|
+
// maxPixels 512 largest on-screen size of one particle (pixels), so smoke drifting past the camera does not fill the screen
|
|
33
|
+
//
|
|
34
|
+
// Style pack: style.particles[name] = {...same params...} sits under the define() params (what define sets wins).
|
|
35
|
+
// so a game can write only engine.particles.define('leaves', { max: 200 }) and leave color and texture to the style pack.
|
|
36
|
+
// style.particles.default = {...} is the fallback for all effects.
|
|
37
|
+
// The style pack can also define effects directly: a name in style.particles that the game never define()d is defined from the style pack on the first emit/attach.
|
|
38
|
+
import * as THREE from 'three';
|
|
39
|
+
import { vec3 } from '../../core/math.js';
|
|
40
|
+
import { Effect, NEUTRAL, KEYS } from './effect.js';
|
|
41
|
+
import { checkOptions, checkOptionsArg, warnMissing } from '../../core/validate.js';
|
|
42
|
+
const _vs = new THREE.Vector2(); // canvas CSS size (particle point scale follows the canvas height, not the window)
|
|
43
|
+
/** @import { ModuleDef } from '../../types/core' */
|
|
44
|
+
/** @import { Object3D, PositionLike } from '../../types/common' */
|
|
45
|
+
/** @import { ParticleEffect, ParticleEmitter, ParticleOptions } from '../../types/gameplay' */
|
|
46
|
+
|
|
47
|
+
/** engine.particles */
|
|
48
|
+
export class Particles {
|
|
49
|
+
/** @internal */
|
|
50
|
+
constructor(engine) {
|
|
51
|
+
/** @internal */ this.engine = engine;
|
|
52
|
+
/** Defined effects @type {Map<string, ParticleEffect>} */
|
|
53
|
+
this.effects = new Map();
|
|
54
|
+
/** @internal */ this.emitters = new Set();
|
|
55
|
+
/** @internal */ this._dir = new THREE.Vector3(); this._up = new THREE.Vector3(); this._t1 = new THREE.Vector3(); this._t2 = new THREE.Vector3();
|
|
56
|
+
/** @internal */ this._q = new THREE.Quaternion();
|
|
57
|
+
/** @internal */ this._o = new THREE.Vector3();
|
|
58
|
+
/** @internal */ this._e = new THREE.Vector3();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @internal */
|
|
62
|
+
_styleFor(name) { const s = this.engine.style.particles || {}; return { ...(s.default || {}), ...(s[name] || {}) }; }
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Define an effect (same name replaces it, and emitters attached to it move to the new definition); params sit on top of the style
|
|
66
|
+
* pack preset. Returns the effect object: .points is its THREE.Points, .material is the shader material
|
|
67
|
+
* @param {string} name
|
|
68
|
+
* @param {ParticleOptions} [opts]
|
|
69
|
+
* @returns {ParticleEffect}
|
|
70
|
+
*/
|
|
71
|
+
define(name, opts = {}) {
|
|
72
|
+
checkOptions(`particles.define('${name}')`, opts, KEYS);
|
|
73
|
+
const old = this.effects.get(name);
|
|
74
|
+
const o = { ...NEUTRAL, ...this._styleFor(name), ...opts };
|
|
75
|
+
const fx = new Effect(this, name, o);
|
|
76
|
+
this.effects.set(name, fx);
|
|
77
|
+
// emitters attached to the old effect keep running on the new one (redefining tunes an effect, it does not stop it)
|
|
78
|
+
if (old) { for (const h of this.emitters) if (h.fx === old) h.fx = fx; old.dispose(); }
|
|
79
|
+
return fx;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Whether this effect has been defined
|
|
83
|
+
* @param {string} name
|
|
84
|
+
* @returns {boolean}
|
|
85
|
+
*/
|
|
86
|
+
has(name) { return this.effects.has(name); }
|
|
87
|
+
/** @internal */
|
|
88
|
+
_get(name) {
|
|
89
|
+
let fx = this.effects.get(name);
|
|
90
|
+
if (!fx && this.engine.style.particles?.[name]) fx = this.define(name);
|
|
91
|
+
if (!fx) warnMissing('particle', name, [...this.effects.keys(), ...Object.keys(this.engine.style.particles || {}).filter(k => k !== 'default' && !this.effects.has(k))]);
|
|
92
|
+
return fx;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Burst: emit count particles at once (default 10) at position (Vector3 / Object3D / [x, y, z] / {x, y, z}). overrides affect this batch only (color, speed, direction...; max/texture/blending cannot be changed temporarily)
|
|
97
|
+
* @param {string} name
|
|
98
|
+
* @param {PositionLike} position
|
|
99
|
+
* @param {{ count?: number, overrides?: ParticleOptions | null }} [opts]
|
|
100
|
+
* @returns {void}
|
|
101
|
+
*/
|
|
102
|
+
emit(name, position, opts = {}) {
|
|
103
|
+
checkOptionsArg(`particles.emit('${name}')`, opts, ['count', 'overrides']);
|
|
104
|
+
const { count = 10, overrides = null } = typeof opts === 'object' && opts ? opts : {};
|
|
105
|
+
const fx = this._get(name); if (!fx) return;
|
|
106
|
+
if (overrides) checkOptions(`particles.emit('${name}') overrides`, overrides, KEYS);
|
|
107
|
+
fx.spawn(vec3(position, this._e), count, overrides ? { ...fx.o, ...overrides } : fx.o, null);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Keep emitting while following an object: rate per second, offset in the object's local coordinates [x, y, z] (meters). Returns a handle { remove(), rate, active } (active = false pauses)
|
|
112
|
+
* @param {string} name
|
|
113
|
+
* @param {Object3D} object3d
|
|
114
|
+
* @param {{ rate?: number, offset?: number[], overrides?: ParticleOptions | null }} [opts]
|
|
115
|
+
* @returns {ParticleEmitter}
|
|
116
|
+
*/
|
|
117
|
+
attach(name, object3d, opts = {}) {
|
|
118
|
+
checkOptions(`particles.attach('${name}')`, opts, ['rate', 'offset', 'overrides']);
|
|
119
|
+
const { rate, offset = [0, 0, 0], overrides = null } = opts;
|
|
120
|
+
if (overrides) checkOptions(`particles.attach('${name}') overrides`, overrides, KEYS);
|
|
121
|
+
const fx = this._get(name); if (!fx) return { remove() {}, rate: 0, active: false };
|
|
122
|
+
const h = { fx, object: object3d, rate: rate ?? fx.o.rate, offset, overrides, active: true, acc: 0, remove: () => { h.active = false; this.emitters.delete(h); } };
|
|
123
|
+
this.emitters.add(h);
|
|
124
|
+
return h;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Live particle count (no name = all)
|
|
129
|
+
* @param {string} [name]
|
|
130
|
+
* @returns {number}
|
|
131
|
+
*/
|
|
132
|
+
count(name) {
|
|
133
|
+
if (name) return this.effects.get(name)?.alive ?? 0;
|
|
134
|
+
let n = 0; for (const fx of this.effects.values()) n += fx.alive; return n;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Clear one effect's (or all) particles
|
|
139
|
+
* @param {string} [name]
|
|
140
|
+
* @returns {void}
|
|
141
|
+
*/
|
|
142
|
+
clear(name) { for (const fx of name ? [this.effects.get(name)].filter(Boolean) : this.effects.values()) { fx.life.fill(0); fx.aAlpha.array.fill(0); fx.alive = 0; fx.points.visible = false; } }
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Update all effects every frame (called by the module each frame)
|
|
146
|
+
* @param {number} dt
|
|
147
|
+
* @returns {void}
|
|
148
|
+
* @internal
|
|
149
|
+
*/
|
|
150
|
+
update(dt) {
|
|
151
|
+
const cam = this.engine.camera, pr = this.engine.renderer.getPixelRatio(), scale = this.engine.renderer.getSize(_vs).y * pr / (cam.orthoSize ? cam.orthoSize / cam.zoom : 2 * Math.tan(THREE.MathUtils.degToRad(cam.fov) / 2)); // orthographic: pixels per world unit
|
|
152
|
+
for (const h of this.emitters) {
|
|
153
|
+
if (!h.active) continue;
|
|
154
|
+
if (!h.object.parent) { h.remove(); continue; } // object removed from the scene: stop automatically
|
|
155
|
+
h.acc += dt * h.rate;
|
|
156
|
+
const n = Math.floor(h.acc);
|
|
157
|
+
if (n <= 0) continue;
|
|
158
|
+
h.acc -= n;
|
|
159
|
+
// merged params are cached on the handle (re-merged only when overrides is a new object), not spread every frame
|
|
160
|
+
if (h._ov !== h.overrides || h._base !== h.fx.o) { h._ov = h.overrides; h._base = h.fx.o; h._merged = h.overrides ? { ...h.fx.o, ...h.overrides } : h.fx.o; }
|
|
161
|
+
const o = h._merged;
|
|
162
|
+
h.object.updateWorldMatrix(true, false);
|
|
163
|
+
const p = this._o.set(h.offset[0], h.offset[1], h.offset[2]).applyMatrix4(h.object.matrixWorld);
|
|
164
|
+
h.object.getWorldQuaternion(this._q);
|
|
165
|
+
h.fx.spawn(p, n, o, o.worldSpace === false ? this._q : null);
|
|
166
|
+
}
|
|
167
|
+
for (const fx of this.effects.values()) { fx.material.uniforms.uScale.value = scale; fx.update(dt); }
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Particles -> engine.particles
|
|
173
|
+
* @returns {ModuleDef<Particles>}
|
|
174
|
+
*/
|
|
175
|
+
export function particles() {
|
|
176
|
+
let p = null;
|
|
177
|
+
return { name: 'particles', label: 'Particles', stage: 'presentation', presentation: true, install(e) { p = new Particles(e); return p; }, frame(dt) { p.update(dt); },
|
|
178
|
+
counts() { return { alive: p.count(), emitters: p.emitters.size }; } }; // engine.counts().particles
|
|
179
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// One particle effect (helper of Particles.js): a ring buffer of particles drawn as one THREE.Points, updated on the CPU every frame.
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
import { vert, frag } from './shaders.js';
|
|
4
|
+
|
|
5
|
+
export const NEUTRAL = {
|
|
6
|
+
max: 120, lifetime: [1, 2], rate: 10, speed: [0.5, 1.5], direction: [0, 1, 0], spread: 0.5, area: 0,
|
|
7
|
+
gravity: -2, drag: 0.5, wind: 0, windDir: [1, 0, 0.3], size: [0.3, 0.3], color: [0xcccccc, 0xcccccc], opacity: [0.8, 0],
|
|
8
|
+
texture: null, blending: 'normal', spin: 0, worldSpace: true, fog: true, maxPixels: 512,
|
|
9
|
+
};
|
|
10
|
+
export const KEYS = Object.keys(NEUTRAL);
|
|
11
|
+
const pair = v => Array.isArray(v) ? v : [v, v];
|
|
12
|
+
|
|
13
|
+
export class Effect {
|
|
14
|
+
constructor(sys, name, o) {
|
|
15
|
+
this.sys = sys; this.name = name; this.o = o;
|
|
16
|
+
const N = this.max = Math.max(1, Math.round(o.max * (sys.engine.tier === 'low' ? 0.5 : 1)));
|
|
17
|
+
this.pos = new Float32Array(N * 3); this.vel = new Float32Array(N * 3);
|
|
18
|
+
this.age = new Float32Array(N); this.life = new Float32Array(N).fill(0); // life 0 = empty slot
|
|
19
|
+
this.spinV = new Float32Array(N);
|
|
20
|
+
this.col0 = new Float32Array(N * 3); this.col1 = new Float32Array(N * 3); // start/end color of each particle (emit may override)
|
|
21
|
+
this.sz = new Float32Array(N * 2); this.op = new Float32Array(N * 2);
|
|
22
|
+
this.extra = new Float32Array(N * 3); // gravity, drag, wind (emit may override)
|
|
23
|
+
this.head = 0; this.alive = 0;
|
|
24
|
+
const g = this.geometry = new THREE.BufferGeometry();
|
|
25
|
+
const attr = (n, size) => { const a = new THREE.BufferAttribute(new Float32Array(N * size), size); a.setUsage(THREE.DynamicDrawUsage); g.setAttribute(n, a); return a; };
|
|
26
|
+
this.aPos = attr('position', 3); this.aSize = attr('aSize', 1); this.aAlpha = attr('aAlpha', 1); this.aAngle = attr('aAngle', 1); this.aColor = attr('aColor', 3);
|
|
27
|
+
this.material = new THREE.ShaderMaterial({
|
|
28
|
+
vertexShader: vert, fragmentShader: frag, transparent: true, depthWrite: false, fog: o.fog !== false,
|
|
29
|
+
blending: o.blending === 'additive' ? THREE.AdditiveBlending : THREE.NormalBlending,
|
|
30
|
+
uniforms: THREE.UniformsUtils.merge([THREE.UniformsLib.fog, { uMap: { value: null }, uHasMap: { value: 0 }, uScale: { value: 400 }, uMaxPx: { value: o.maxPixels } }]),
|
|
31
|
+
});
|
|
32
|
+
this.points = new THREE.Points(g, this.material);
|
|
33
|
+
this.points.frustumCulled = false; this.points.visible = false; this.points.renderOrder = 10;
|
|
34
|
+
this.points.matrixAutoUpdate = false;
|
|
35
|
+
sys.engine.add(this.points, { owner: 'engine:particles' });
|
|
36
|
+
this._c0 = new THREE.Color(); this._c1 = new THREE.Color();
|
|
37
|
+
if (o.texture) this._loadTexture(o.texture);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async _loadTexture(t) {
|
|
41
|
+
try {
|
|
42
|
+
const tex = t.isTexture ? t : await this.sys.engine.assets.texture(t);
|
|
43
|
+
this.material.uniforms.uMap.value = tex; this.material.uniforms.uHasMap.value = 1;
|
|
44
|
+
} catch (err) { console.warn(`[particles ${this.name}] texture failed to load; using a round dot instead`, err); }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Emit count particles. origin {x,y,z}; o is the merged params; frame optional: particle direction rotates with the object */
|
|
48
|
+
spawn(origin, count, o, quat) {
|
|
49
|
+
// presentation: particle scatter
|
|
50
|
+
const N = this.max, rnd = Math.random, life = pair(o.lifetime), spd = pair(o.speed), sz = pair(o.size), op = pair(o.opacity), col = pair(o.color);
|
|
51
|
+
this._c0.set(col[0]); this._c1.set(col[1]); // three converts to linear color space automatically
|
|
52
|
+
const dir = this.sys._dir.set(o.direction[0], o.direction[1], o.direction[2]).normalize();
|
|
53
|
+
if (quat) dir.applyQuaternion(quat);
|
|
54
|
+
// two perpendicular directions around axis dir, for random picks inside the cone
|
|
55
|
+
const up = Math.abs(dir.y) < 0.99 ? this.sys._up.set(0, 1, 0) : this.sys._up.set(1, 0, 0);
|
|
56
|
+
const t1 = this.sys._t1.crossVectors(dir, up).normalize(), t2 = this.sys._t2.crossVectors(dir, t1);
|
|
57
|
+
const area = o.area, ax = Array.isArray(area) ? area[0] : area, ay = Array.isArray(area) ? area[1] : area, az = Array.isArray(area) ? area[2] : area;
|
|
58
|
+
for (let n = 0; n < count; n++) {
|
|
59
|
+
const i = this.head; this.head = (this.head + 1) % N;
|
|
60
|
+
if (this.life[i] === 0) this.alive++;
|
|
61
|
+
const i3 = i * 3;
|
|
62
|
+
this.pos[i3] = origin.x + (rnd() * 2 - 1) * ax; this.pos[i3 + 1] = origin.y + (rnd() * 2 - 1) * ay; this.pos[i3 + 2] = origin.z + (rnd() * 2 - 1) * az;
|
|
63
|
+
// random direction in a cone: off-axis angle theta in [0, spread] (cos theta uniform = uniform on the sphere)
|
|
64
|
+
const cosT = 1 - rnd() * (1 - Math.cos(Math.min(Math.PI, o.spread))), sinT = Math.sqrt(1 - cosT * cosT), ph = rnd() * Math.PI * 2;
|
|
65
|
+
const a = Math.cos(ph) * sinT, b = Math.sin(ph) * sinT, v = spd[0] + rnd() * (spd[1] - spd[0]);
|
|
66
|
+
this.vel[i3] = (dir.x * cosT + t1.x * a + t2.x * b) * v;
|
|
67
|
+
this.vel[i3 + 1] = (dir.y * cosT + t1.y * a + t2.y * b) * v;
|
|
68
|
+
this.vel[i3 + 2] = (dir.z * cosT + t1.z * a + t2.z * b) * v;
|
|
69
|
+
this.age[i] = 0; this.life[i] = life[0] + rnd() * (life[1] - life[0]) || 0.01;
|
|
70
|
+
this.spinV[i] = o.spin ? (rnd() * 2 - 1) * o.spin : 0;
|
|
71
|
+
this.aAngle.array[i] = rnd() * Math.PI * 2;
|
|
72
|
+
this.col0[i3] = this._c0.r; this.col0[i3 + 1] = this._c0.g; this.col0[i3 + 2] = this._c0.b;
|
|
73
|
+
this.col1[i3] = this._c1.r; this.col1[i3 + 1] = this._c1.g; this.col1[i3 + 2] = this._c1.b;
|
|
74
|
+
this.sz[i * 2] = sz[0]; this.sz[i * 2 + 1] = sz[1]; this.op[i * 2] = op[0]; this.op[i * 2 + 1] = op[1];
|
|
75
|
+
this.extra[i3] = o.gravity; this.extra[i3 + 1] = o.drag; this.extra[i3 + 2] = o.wind;
|
|
76
|
+
}
|
|
77
|
+
this.points.visible = true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
update(dt) {
|
|
81
|
+
if (!this.alive) { this.points.visible = false; return; }
|
|
82
|
+
const N = this.max, P = this.pos, V = this.vel, ap = this.aPos.array, as = this.aSize.array, aa = this.aAlpha.array, ac = this.aColor.array, ag = this.aAngle.array;
|
|
83
|
+
const w = this.o.windDir, wl = Math.hypot(w[0], w[2]) || 1, wind = this.sys.engine.uniforms.uWind.value, wx = w[0] / wl * wind, wz = w[2] / wl * wind;
|
|
84
|
+
let alive = 0, hi = -1;
|
|
85
|
+
for (let i = 0; i < N; i++) {
|
|
86
|
+
const L = this.life[i];
|
|
87
|
+
if (L === 0) continue;
|
|
88
|
+
hi = i;
|
|
89
|
+
const age = this.age[i] += dt;
|
|
90
|
+
if (age >= L) { this.life[i] = 0; aa[i] = 0; continue; }
|
|
91
|
+
alive++;
|
|
92
|
+
const i3 = i * 3, k = age / L, g = this.extra[i3], dr = Math.max(0, 1 - this.extra[i3 + 1] * dt), wd = this.extra[i3 + 2];
|
|
93
|
+
V[i3] = V[i3] * dr + wx * wd * dt; V[i3 + 1] = V[i3 + 1] * dr + g * dt; V[i3 + 2] = V[i3 + 2] * dr + wz * wd * dt;
|
|
94
|
+
ap[i3] = P[i3] += V[i3] * dt; ap[i3 + 1] = P[i3 + 1] += V[i3 + 1] * dt; ap[i3 + 2] = P[i3 + 2] += V[i3 + 2] * dt;
|
|
95
|
+
as[i] = this.sz[i * 2] + (this.sz[i * 2 + 1] - this.sz[i * 2]) * k;
|
|
96
|
+
aa[i] = this.op[i * 2] + (this.op[i * 2 + 1] - this.op[i * 2]) * k;
|
|
97
|
+
ac[i3] = this.col0[i3] + (this.col1[i3] - this.col0[i3]) * k;
|
|
98
|
+
ac[i3 + 1] = this.col0[i3 + 1] + (this.col1[i3 + 1] - this.col0[i3 + 1]) * k;
|
|
99
|
+
ac[i3 + 2] = this.col0[i3 + 2] + (this.col1[i3 + 2] - this.col0[i3 + 2]) * k;
|
|
100
|
+
if (this.spinV[i]) ag[i] += this.spinV[i] * dt;
|
|
101
|
+
}
|
|
102
|
+
this.alive = alive;
|
|
103
|
+
this.points.visible = alive > 0;
|
|
104
|
+
// upload only the used range (0 to the last live particle), not the whole buffer
|
|
105
|
+
const n = hi + 1;
|
|
106
|
+
for (const a of this._attrs ||= [this.aPos, this.aSize, this.aAlpha, this.aColor, this.aAngle]) {
|
|
107
|
+
a.clearUpdateRanges(); a.addUpdateRange(0, n * a.itemSize); a.needsUpdate = true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
dispose() { this.sys.engine.remove(this.points); } // remove, then dispose geometry and material
|
|
112
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Particle point-sprite shaders: size by distance (capped at uMaxPx), soft round dot or a texture, rotation, fog, tone mapping and output color space.
|
|
2
|
+
export const vert = `
|
|
3
|
+
attribute float aSize; attribute float aAlpha; attribute float aAngle; attribute vec3 aColor;
|
|
4
|
+
varying float vAlpha; varying float vAngle; varying vec3 vColor;
|
|
5
|
+
uniform float uScale; uniform float uMaxPx;
|
|
6
|
+
#include <fog_pars_vertex>
|
|
7
|
+
void main() {
|
|
8
|
+
vAlpha = aAlpha; vAngle = aAngle; vColor = aColor;
|
|
9
|
+
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
10
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
11
|
+
gl_PointSize = aAlpha <= 0.0 ? 0.0 : min(aSize * uScale / max(0.1, -mvPosition.z), uMaxPx); // capped: no screen-filling sprites near the camera
|
|
12
|
+
#include <fog_vertex>
|
|
13
|
+
}`;
|
|
14
|
+
export const frag = `
|
|
15
|
+
uniform sampler2D uMap; uniform float uHasMap;
|
|
16
|
+
varying float vAlpha; varying float vAngle; varying vec3 vColor;
|
|
17
|
+
#include <fog_pars_fragment>
|
|
18
|
+
void main() {
|
|
19
|
+
vec2 p = gl_PointCoord - 0.5;
|
|
20
|
+
float c = cos(vAngle), s = sin(vAngle);
|
|
21
|
+
p = vec2(c * p.x - s * p.y, s * p.x + c * p.y);
|
|
22
|
+
vec4 tex = uHasMap > 0.5 ? texture2D(uMap, p + 0.5) : vec4(1.0, 1.0, 1.0, smoothstep(0.5, 0.2, length(p)));
|
|
23
|
+
gl_FragColor = vec4(vColor * tex.rgb, tex.a * vAlpha);
|
|
24
|
+
if (gl_FragColor.a < 0.01) discard;
|
|
25
|
+
#include <tonemapping_fragment>
|
|
26
|
+
#include <colorspace_fragment>
|
|
27
|
+
#include <fog_fragment>
|
|
28
|
+
}`;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// Photo mode: while taking the shot, temporarily raise resolution and shadow precision (with the sky module) and enable bloom, then restore at once, so normal frame rate is unaffected.
|
|
2
|
+
//
|
|
3
|
+
// Taking a photo is the op photo.take (recipes/23-ops.md): the photo key, a game's shutter button, the dev bridge and tests all run it, and
|
|
4
|
+
// photo.canTake (true or a reason) is its can. engine.photo.take() is the low-level capture the op calls (no checks).
|
|
5
|
+
// const r = engine.ops.run('photo.take'); // r.value = { url, time, position, subject } or r.code (e.g. 'noFilm')
|
|
6
|
+
// When the photo key's op is refused the module emits photo:denied { reason } (the game shows it; a button reads r.code itself).
|
|
7
|
+
// subject is what is in the middle of the frame { name, kind, distance }: NPC (kind 'npc'), entity ('entity'), named object ('object'),
|
|
8
|
+
// vegetation ('vegetation'; after hitting an instanced mesh, vegetation.instanceAt looks up the kind); null when it hits the sky or an unnamed ground.
|
|
9
|
+
// The photo key (action 'photo') is the game's: photo({ keys: ['KeyC'], pad: ['PadX'] }), photo({ ...presets.keys.photo }) (P / gamepad RB) or keys: [] for no key
|
|
10
|
+
// Shutter sound: the game's createEngine({ audio: { sounds: { shutter } } }) supplies a file, otherwise a synthesized fallback
|
|
11
|
+
import { checkOptions, moduleKeys } from '../../core/validate.js';
|
|
12
|
+
import { readParams, keysSchema } from '../../core/params.js';
|
|
13
|
+
import { toast } from '../../ui/UI.js';
|
|
14
|
+
const GROUP = 'photo'; // group name in the blobs store of engine.saves.store
|
|
15
|
+
const TAKE_KEYS = ['scale', 'shadowMapSize', 'bloom', 'quality', 'format'];
|
|
16
|
+
|
|
17
|
+
export class Photo {
|
|
18
|
+
constructor(engine, { scale = 1.6, shadowMapSize = 4096, bloom = true, quality = 0.92, format = 'image/jpeg', maxAlbum = 50 } = {}) {
|
|
19
|
+
Object.assign(this, { engine, scale, shadowMapSize, bloom, quality, format, maxAlbum });
|
|
20
|
+
this.album = [];
|
|
21
|
+
this._n = 0; // shots taken this run (photo id = shot time + sequence number)
|
|
22
|
+
this.canTake = () => true; // the op's can: a game returns false or a reason string (e.g. 'noFilm') to refuse
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Capture a photo now, without checks (the op photo.take checks canTake and calls this). Returns { url, time, position, subject } */
|
|
26
|
+
take(opts = {}) {
|
|
27
|
+
checkOptions('photo.take()', opts, TAKE_KEYS);
|
|
28
|
+
const o = { scale: this.scale, shadowMapSize: this.shadowMapSize, bloom: this.bloom, quality: this.quality, format: this.format, ...opts };
|
|
29
|
+
const e = this.engine, r = e.renderer, sky = e.sky;
|
|
30
|
+
const prevPR = r.getPixelRatio(), prevMap = sky?.shadowMapSize, prevBloom = e.post.get('bloom')?.enabled;
|
|
31
|
+
const pr = Math.min(3, prevPR * o.scale);
|
|
32
|
+
r.setPixelRatio(pr); e.post.setPixelRatio(pr);
|
|
33
|
+
if (e.shadows && sky) sky.setShadowMapSize(o.shadowMapSize);
|
|
34
|
+
e.look.post.set({ 'bloom.enabled': o.bloom });
|
|
35
|
+
r.shadowMap.needsUpdate = true; // shadows may update only every few frames (sky's shadow.updateEvery); force an update on the photo frame
|
|
36
|
+
e.post.render(0);
|
|
37
|
+
const url = r.domElement.toDataURL(o.format, o.quality);
|
|
38
|
+
r.setPixelRatio(prevPR); e.post.setPixelRatio(prevPR);
|
|
39
|
+
if (e.shadows && sky) sky.setShadowMapSize(prevMap);
|
|
40
|
+
if (prevBloom !== undefined) e.look.post.set({ 'bloom.enabled': prevBloom });
|
|
41
|
+
const p = e.actorPosition() ?? e.cameraRig?.target?.position; // where whoever the user controls stands (engine.actor)
|
|
42
|
+
const shot = { url, time: sky?.clock ?? '', position: p ? { x: Math.round(p.x), z: Math.round(p.z) } : null, subject: this.subject() };
|
|
43
|
+
this.album.unshift(shot); if (this.album.length > this.maxAlbum) this._drop(this.album.pop());
|
|
44
|
+
this._persist(shot);
|
|
45
|
+
e.events.emit('photo:taken', shot);
|
|
46
|
+
e.audio.sfx('shutter');
|
|
47
|
+
return shot;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ---------- photos are stored separately (Blob, not in the world save JSON) ----------
|
|
51
|
+
/** Store into IndexedDB: convert the data: URL to a Blob; failures must be reported, never dropped silently.
|
|
52
|
+
* Afterwards the album keeps a blob: URL instead of the data: string (a full-size JPEG string is megabytes of JS heap; 50 of them add up) */
|
|
53
|
+
async _persist(shot) {
|
|
54
|
+
const id = shot.id = `${Date.now().toString(36)}-${this._n++}`; // wall-clock: photo id
|
|
55
|
+
const meta = { time: shot.time, position: shot.position, subject: shot.subject };
|
|
56
|
+
const saves = this.engine.saves;
|
|
57
|
+
try {
|
|
58
|
+
await saves.queue(async () => { // queued at once, so saves.flush() waits for this photo
|
|
59
|
+
const blob = await (await fetch(shot.url)).blob();
|
|
60
|
+
if (this.album.includes(shot)) shot.url = URL.createObjectURL(blob); // still in the album: hold the bytes as a Blob from now on
|
|
61
|
+
await saves.store.putBlob(GROUP, id, blob, meta);
|
|
62
|
+
});
|
|
63
|
+
} catch (err) { this._fail(err); }
|
|
64
|
+
}
|
|
65
|
+
/** Photos pushed out of the album: delete from storage and release the blob: URLs created at load */
|
|
66
|
+
_drop(shot) {
|
|
67
|
+
if (shot.url.startsWith('blob:')) URL.revokeObjectURL(shot.url);
|
|
68
|
+
if (shot.id) this.engine.saves.queue(() => this.engine.saves.store.deleteBlob(GROUP, shot.id)).catch(err => this._fail(err));
|
|
69
|
+
}
|
|
70
|
+
/** At startup / after importing a save file, read the stored photos back into the album (newest first, merged with and de-duplicated against what is already there) */
|
|
71
|
+
async restore() {
|
|
72
|
+
const saves = this.engine.saves;
|
|
73
|
+
await saves.whenReady;
|
|
74
|
+
let rows; try { rows = await saves.store.blobs(GROUP); } catch (err) { this._fail(err); return; }
|
|
75
|
+
const have = new Set(this.album.map(s => s.id));
|
|
76
|
+
for (const r of rows) if (!have.has(r.id)) this.album.push({ id: r.id, url: URL.createObjectURL(r.blob), ...r.meta, at: r.at });
|
|
77
|
+
this.album.sort((a, b) => (b.at ?? Infinity) - (a.at ?? Infinity) || 0); // a just-taken shot has no `at`: goes first
|
|
78
|
+
while (this.album.length > this.maxAlbum) this._drop(this.album.pop());
|
|
79
|
+
}
|
|
80
|
+
_fail(err) {
|
|
81
|
+
console.error('[photo] failed to save photo', err);
|
|
82
|
+
toast(this.engine, this.engine.t('saves.failed', { why: err?.message || String(err) }), { kind: 'error', ms: 5000 });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** What is in the middle of the frame: { name, kind, distance } or null (usable without taking a photo) */
|
|
86
|
+
subject() {
|
|
87
|
+
const e = this.engine, skip = new Set([e.sky?.dome, e.player?.root].filter(Boolean));
|
|
88
|
+
const hit = e.pick(0, 0, { targets: [e.scene], filter: h => !h.object.isPoints && !h.object.isSprite && !under(h.object, skip) });
|
|
89
|
+
if (!hit) return null;
|
|
90
|
+
const distance = Math.round(hit.distance * 10) / 10;
|
|
91
|
+
if (hit.object.isInstancedMesh) { // instanced things (vegetation): ask vegetation which kind it is
|
|
92
|
+
const v = e.vegetation?.instanceAt(hit.point, { radius: 3 });
|
|
93
|
+
return v ? { name: v.species, kind: 'vegetation', distance } : null;
|
|
94
|
+
}
|
|
95
|
+
for (let o = hit.object; o; o = o.parent) {
|
|
96
|
+
const npc = e.npcs?.list().find(n => n.root === o);
|
|
97
|
+
if (npc) return { name: npc.name, kind: 'npc', distance };
|
|
98
|
+
if (o.userData.entity != null) return { name: e.entities?.get(o.userData.entity)?.type ?? String(o.userData.entity), kind: 'entity', distance };
|
|
99
|
+
if (o.userData.name || o.name) return { name: o.userData.name ?? o.name, kind: 'object', distance };
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function under(o, set) { for (let p = o; p; p = p.parent) if (set.has(p)) return true; return false; }
|
|
106
|
+
|
|
107
|
+
const STRINGS = { 'zh-CN': { 'photo.action': '拍照' }, en: { 'photo.action': 'Take photo' } };
|
|
108
|
+
|
|
109
|
+
const PHOTO_PARAMS = keysSchema(['keys', 'pad', 'maxAlbum', ...TAKE_KEYS]);
|
|
110
|
+
|
|
111
|
+
/** Photo module. Once installed: engine.ops.run('photo.take') takes a photo through photo.canTake; engine.photo.take() captures without checks */
|
|
112
|
+
export function photo(all = {}) {
|
|
113
|
+
readParams('photo', PHOTO_PARAMS, all);
|
|
114
|
+
const { keys: keys0, pad, ...opts } = all;
|
|
115
|
+
let bound = false; // the photo action has a key or pad button (set at install)
|
|
116
|
+
let ph = null;
|
|
117
|
+
return {
|
|
118
|
+
name: 'photo', params: PHOTO_PARAMS, options: all, label: 'Photo mode', optional: ['cameraRig', 'sky', 'vegetation', 'entities'],
|
|
119
|
+
install(e) {
|
|
120
|
+
e.i18n.add(STRINGS);
|
|
121
|
+
const keys = moduleKeys('photo()', 'photo', keys0, 'photo', e.input); // the game's key or a presets.keys bundle, never a built-in one
|
|
122
|
+
bound = keys.length > 0 || !!pad;
|
|
123
|
+
if (bound) { e.input.define('photo', keys, 'photo.action', { pad }); e.input.touchButton('photo', 'photo.action'); }
|
|
124
|
+
e.audio.register('shutter', (audio, p, out) => {
|
|
125
|
+
audio.noiseBurst({ dur: 0.03, type: 'highpass', freq: p.freq ?? 3000, vol: p.volume ?? 0.4, out });
|
|
126
|
+
audio.noiseBurst({ dur: 0.05, type: 'bandpass', freq: 1800, vol: (p.volume ?? 0.4) * 0.75, delay: 0.07, out }); return 0.15;
|
|
127
|
+
}, { builtIn: true });
|
|
128
|
+
ph = new Photo(e, opts);
|
|
129
|
+
ph.restore(); // read photos taken last time back from storage
|
|
130
|
+
e.ops.define('photo.take', {
|
|
131
|
+
desc: 'Take a photo (refused when photo.canTake() returns false or a reason)',
|
|
132
|
+
can: () => { const r = ph.canTake(); return r === true ? true : typeof r === 'string' && r ? r : 'denied'; },
|
|
133
|
+
run: () => ph.take(),
|
|
134
|
+
offers: () => [{}],
|
|
135
|
+
reasons: { denied: 'photo.canTake() returned false' },
|
|
136
|
+
});
|
|
137
|
+
e.events.on('save:imported', () => ph.restore()); // after restoring from a save file, the attachments in the file (photos) also go into the album
|
|
138
|
+
return ph;
|
|
139
|
+
},
|
|
140
|
+
frame(dt, e) {
|
|
141
|
+
if (!bound || !e.input.pressed('photo')) return;
|
|
142
|
+
const r = e.ops.run('photo.take', {}, { source: 'player' });
|
|
143
|
+
if (!r.ok && r.code !== 'halted') e.events.emit('photo:denied', { reason: r.code });
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|