castle-web-cli 0.4.231 → 0.4.233
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/dist/agent-failures.d.ts +8 -0
- package/dist/agent-failures.js +19 -2
- package/dist/agent-prompts.d.ts +1 -1
- package/dist/agent-prompts.js +10 -3
- package/dist/agent.d.ts +64 -3
- package/dist/agent.js +170 -81
- package/dist/castle-host/host.d.ts +1 -0
- package/dist/castle-host/host.js +51 -3
- package/dist/castleJson.d.ts +4 -2
- package/dist/castleJson.js +2 -0
- package/dist/check.d.ts +3 -1
- package/dist/check.js +11 -1
- package/dist/devSessionStorage.js +47 -1
- package/dist/importPinMismatch.d.ts +22 -0
- package/dist/importPinMismatch.js +77 -0
- package/dist/importRegression.js +3 -15
- package/dist/imports.d.ts +10 -3
- package/dist/imports.js +88 -20
- package/dist/index.js +5 -4
- package/dist/init.js +1 -1
- package/dist/platformDoc.js +22 -0
- package/dist/routerFences.d.ts +5 -0
- package/dist/routerFences.js +12 -2
- package/dist/save-deck.d.ts +5 -0
- package/dist/save-deck.js +37 -4
- package/dist/serve.js +3 -0
- package/dist/serverBundle.js +5 -3
- package/dist/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
- package/dist/shell/assets/index-MVLfNYd9.css +1 -0
- package/dist/shell/index.html +2 -2
- package/dist/taskSteer.d.ts +13 -0
- package/dist/taskSteer.js +22 -2
- package/kits/base/castle.json +1 -1
- package/kits/base/package-lock.json +1 -1
- package/kits/base/sdk/README.md +79 -2
- package/kits/base/sdk/avatar.d.ts +6 -2
- package/kits/base/sdk/avatar.js +12 -4
- package/kits/base/sdk/castle.d.ts +1 -1
- package/kits/base/sdk/commands.d.ts +14 -0
- package/kits/base/sdk/hub.d.ts +6 -2
- package/kits/base/sdk/hub.js +13 -0
- package/kits/base/sdk/multiplayer.js +17 -1
- package/kits/base/sdk/save.js +2 -1
- package/kits/base/sdk/server/hub.d.ts +87 -0
- package/kits/base/sdk/server/hub.js +70 -0
- package/kits/base/sdk/server/platformHandle.d.ts +31 -1
- package/kits/base/sdk/server/storage.d.ts +13 -0
- package/kits/base/sdk/server/storage.js +46 -0
- package/kits/base/sdk/server/wrapper.d.ts +36 -5
- package/kits/base/sdk/server/wrapper.js +83 -8
- package/kits/hub-game/CLAUDE.md +352 -12
- package/kits/hub-game/blueprints/avatar-10.scene +28 -0
- package/kits/hub-game/blueprints/avatar-11.scene +28 -0
- package/kits/hub-game/blueprints/avatar-12.scene +28 -0
- package/kits/hub-game/blueprints/avatar-13.scene +28 -0
- package/kits/hub-game/blueprints/avatar-14.scene +28 -0
- package/kits/hub-game/blueprints/avatar-15.scene +28 -0
- package/kits/hub-game/blueprints/avatar-4.scene +28 -0
- package/kits/hub-game/blueprints/avatar-5.scene +28 -0
- package/kits/hub-game/blueprints/avatar-6.scene +28 -0
- package/kits/hub-game/blueprints/avatar-7.scene +28 -0
- package/kits/hub-game/blueprints/avatar-8.scene +28 -0
- package/kits/hub-game/blueprints/avatar-9.scene +28 -0
- package/kits/hub-game/code/client/actorBudget.js +47 -0
- package/kits/hub-game/code/client/attention.js +11 -2
- package/kits/hub-game/code/client/avatar.js +345 -32
- package/kits/hub-game/code/client/avatarCap.js +228 -0
- package/kits/hub-game/code/client/avatarColors.js +70 -0
- package/kits/hub-game/code/client/avatarFaces.js +21 -0
- package/kits/hub-game/code/client/avatarShare.js +251 -0
- package/kits/hub-game/code/client/ballot.js +467 -0
- package/kits/hub-game/code/client/build.js +22 -0
- package/kits/hub-game/code/client/chat.js +12 -0
- package/kits/hub-game/code/client/chrome.js +1334 -50
- package/kits/hub-game/code/client/companion.js +716 -0
- package/kits/hub-game/code/client/companionDress.js +99 -0
- package/kits/hub-game/code/client/decor.js +509 -0
- package/kits/hub-game/code/client/earned.js +131 -0
- package/kits/hub-game/code/client/emoteBubble.js +75 -0
- package/kits/hub-game/code/client/grab.js +363 -20
- package/kits/hub-game/code/client/hats.js +26 -0
- package/kits/hub-game/code/client/hookLayer.js +328 -15
- package/kits/hub-game/code/client/hudClear.js +126 -0
- package/kits/hub-game/code/client/idle.js +163 -22
- package/kits/hub-game/code/client/interestCap.js +81 -0
- package/kits/hub-game/code/client/keys.js +36 -0
- package/kits/hub-game/code/client/labelsNear.js +205 -0
- package/kits/hub-game/code/client/menuIcons.js +34 -0
- package/kits/hub-game/code/client/pack.js +6 -2
- package/kits/hub-game/code/client/packFormat.js +3 -3
- package/kits/hub-game/code/client/rehello.js +167 -0
- package/kits/hub-game/code/client/shot.js +92 -0
- package/kits/hub-game/code/client/sound.js +63 -8
- package/kits/hub-game/code/client/soundscape.js +1151 -0
- package/kits/hub-game/code/client/soundscapeActions.js +99 -0
- package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
- package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
- package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
- package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
- package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
- package/kits/hub-game/code/client/soundscapeCues.js +362 -0
- package/kits/hub-game/code/client/soundscapeMap.js +181 -0
- package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
- package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
- package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
- package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
- package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
- package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
- package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
- package/kits/hub-game/code/client/soundscapeUi.js +211 -0
- package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
- package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
- package/kits/hub-game/code/client/spin.js +101 -0
- package/kits/hub-game/code/client/state.js +131 -1
- package/kits/hub-game/code/client/stuck.js +259 -0
- package/kits/hub-game/code/client/talk.js +538 -47
- package/kits/hub-game/code/client/tiers.js +541 -27
- package/kits/hub-game/code/client/toastBand.js +37 -0
- package/kits/hub-game/code/client/together.js +98 -1
- package/kits/hub-game/code/client/visibility.js +43 -0
- package/kits/hub-game/code/client/wear.js +470 -97
- package/kits/hub-game/code/client/yours.js +199 -0
- package/kits/hub-game/code/companionSpec.js +45 -0
- package/kits/hub-game/code/privateText.js +7 -0
- package/kits/hub-game/code/server/actions.js +133 -0
- package/kits/hub-game/code/server/actors.js +14 -1
- package/kits/hub-game/code/server/ask.js +155 -0
- package/kits/hub-game/code/server/ballot.js +539 -0
- package/kits/hub-game/code/server/cells.js +100 -0
- package/kits/hub-game/code/server/companion.js +82 -0
- package/kits/hub-game/code/server/cues.js +70 -0
- package/kits/hub-game/code/server/decor.js +269 -0
- package/kits/hub-game/code/server/earned.js +461 -0
- package/kits/hub-game/code/server/friendBests.js +166 -0
- package/kits/hub-game/code/server/friendsAmong.js +232 -0
- package/kits/hub-game/code/server/grab.js +296 -11
- package/kits/hub-game/code/server/history.js +5 -0
- package/kits/hub-game/code/server/hub.js +1317 -52
- package/kits/hub-game/code/server/idle.js +30 -11
- package/kits/hub-game/code/server/index.js +2 -2
- package/kits/hub-game/code/server/labels.js +28 -2
- package/kits/hub-game/code/server/manifest.js +424 -8
- package/kits/hub-game/code/server/menu.js +133 -0
- package/kits/hub-game/code/server/npcs.js +830 -80
- package/kits/hub-game/code/server/party.js +13 -2
- package/kits/hub-game/code/server/players.js +22 -0
- package/kits/hub-game/code/server/say.js +8 -2
- package/kits/hub-game/code/server/shot.js +110 -0
- package/kits/hub-game/code/server/standing.js +124 -0
- package/kits/hub-game/code/server/vis.js +113 -0
- package/kits/hub-game/code/server/wear.js +128 -11
- package/kits/hub-game/code/server/yours.js +59 -0
- package/kits/hub-game/code/shared/cues.js +9 -0
- package/kits/hub-game/code/shared/decor.js +41 -0
- package/kits/hub-game/code/shared/shot.js +19 -0
- package/kits/hub-game/code/shared/spin.js +57 -0
- package/kits/hub-game/code/systems/hub.js +1083 -85
- package/kits/hub-game/docs/content-pack.md +1 -1
- package/kits/hub-game/docs/contract.md +2440 -61
- package/kits/hub-game/docs/home-pieces.md +64 -0
- package/kits/hub-game/models/avatar-10.pxmodel +28 -0
- package/kits/hub-game/models/avatar-11.pxmodel +28 -0
- package/kits/hub-game/models/avatar-12.pxmodel +27 -0
- package/kits/hub-game/models/avatar-13.pxmodel +24 -0
- package/kits/hub-game/models/avatar-14.pxmodel +29 -0
- package/kits/hub-game/models/avatar-15.pxmodel +24 -0
- package/kits/hub-game/models/avatar-4.pxmodel +29 -0
- package/kits/hub-game/models/avatar-5.pxmodel +27 -0
- package/kits/hub-game/models/avatar-6.pxmodel +26 -0
- package/kits/hub-game/models/avatar-7.pxmodel +25 -0
- package/kits/hub-game/models/avatar-8.pxmodel +24 -0
- package/kits/hub-game/models/avatar-9.pxmodel +30 -0
- package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
- package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
- package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
- package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
- package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
- package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
- package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
- package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
- package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
- package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
- package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
- package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
- package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
- package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
- package/kits/hub-game/models/hats/cap.pxmodel +162 -0
- package/kits/hub-game/models/hats/captain.pxmodel +178 -0
- package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
- package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
- package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
- package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
- package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
- package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
- package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
- package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
- package/kits/hub-game/models/hats/straw.pxmodel +139 -0
- package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
- package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
- package/kits/hub-game/models/hats/witch.pxmodel +147 -0
- package/kits/hub-game/package-lock.json +2 -0
- package/kits/hub-game/package.json +2 -1
- package/kits/hub-game/scripts/check.mjs +78 -11
- package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
- package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
- package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
- package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
- package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
- package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
- package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
- package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
- package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
- package/kits/hub-game/scripts/gen-earned.mjs +285 -0
- package/kits/hub-game/scripts/gen-hats.mjs +827 -0
- package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
- package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
- package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
- package/kits/hub-game/scripts/test-actions.mjs +298 -0
- package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
- package/kits/hub-game/scripts/test-actors.mjs +19 -17
- package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
- package/kits/hub-game/scripts/test-ask.mjs +269 -0
- package/kits/hub-game/scripts/test-attention.mjs +10 -1
- package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
- package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
- package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
- package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
- package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
- package/kits/hub-game/scripts/test-avatar.mjs +540 -7
- package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
- package/kits/hub-game/scripts/test-build.mjs +31 -0
- package/kits/hub-game/scripts/test-cells.mjs +71 -0
- package/kits/hub-game/scripts/test-chat.mjs +694 -0
- package/kits/hub-game/scripts/test-classic.mjs +2 -0
- package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
- package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
- package/kits/hub-game/scripts/test-companion.mjs +259 -0
- package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
- package/kits/hub-game/scripts/test-decor.mjs +457 -0
- package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
- package/kits/hub-game/scripts/test-earned.mjs +1194 -0
- package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
- package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
- package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
- package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
- package/kits/hub-game/scripts/test-grab.mjs +864 -3
- package/kits/hub-game/scripts/test-hats.mjs +483 -0
- package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
- package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
- package/kits/hub-game/scripts/test-hub-trip.mjs +1228 -1
- package/kits/hub-game/scripts/test-hud-clear.mjs +1000 -0
- package/kits/hub-game/scripts/test-idle.mjs +361 -1
- package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
- package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
- package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
- package/kits/hub-game/scripts/test-keys.mjs +46 -1
- package/kits/hub-game/scripts/test-labels.mjs +596 -0
- package/kits/hub-game/scripts/test-manifest.mjs +282 -0
- package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
- package/kits/hub-game/scripts/test-menu.mjs +296 -0
- package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
- package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
- package/kits/hub-game/scripts/test-pack.mjs +7 -0
- package/kits/hub-game/scripts/test-party.mjs +22 -1
- package/kits/hub-game/scripts/test-picker.mjs +314 -0
- package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
- package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
- package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
- package/kits/hub-game/scripts/test-say.mjs +59 -2
- package/kits/hub-game/scripts/test-shot.mjs +756 -0
- package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
- package/kits/hub-game/scripts/test-spin.mjs +314 -0
- package/kits/hub-game/scripts/test-standing.mjs +416 -0
- package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
- package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
- package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
- package/kits/hub-game/scripts/test-tiers.mjs +666 -3
- package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
- package/kits/hub-game/scripts/test-try-on.mjs +940 -0
- package/kits/hub-game/scripts/test-vis.mjs +343 -0
- package/kits/hub-game/scripts/test-visibility.mjs +134 -0
- package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
- package/kits/hub-game/scripts/test-yours.mjs +372 -0
- package/kits/multiplayer-3d/CLAUDE.md +50 -0
- package/kits/multiplayer-3d/castle.json +5 -5
- package/kits/multiplayer-3d/code/client/avatars.js +7 -0
- package/kits/multiplayer-3d/code/client/gait.js +65 -0
- package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
- package/kits/multiplayer-3d/code/options.js +5 -0
- package/kits/multiplayer-3d/code/server/ownership.js +10 -2
- package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
- package/kits/multiplayer-3d/code/server/world.js +7 -0
- package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
- package/kits/multiplayer-3d/package.json +2 -1
- package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
- package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
- package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
- package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
- package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
- package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
- package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
- package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
- package/kits/physics-2d/CLAUDE.md +9 -0
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/scene.js +288 -1
- package/kits/physics-2d/engine/skyKey.js +129 -0
- package/kits/physics-2d/engine/stillClock.js +106 -0
- package/kits/physics-2d/engine/updateBudget.js +21 -0
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +370 -1
- package/kits/physics-3d/CLAUDE.md +1391 -0
- package/kits/physics-3d/behaviors/Player.jsx +921 -3
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/docs/pxmodel-format.md +12 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +98 -10
- package/kits/physics-3d/engine3d/actornode.js +86 -0
- package/kits/physics-3d/engine3d/animationBudget.js +23 -0
- package/kits/physics-3d/engine3d/editorWorld.js +6 -0
- package/kits/physics-3d/engine3d/emptynodes.js +232 -0
- package/kits/physics-3d/engine3d/framecap.js +96 -0
- package/kits/physics-3d/engine3d/gait.js +118 -0
- package/kits/physics-3d/engine3d/kithooks.js +47 -0
- package/kits/physics-3d/engine3d/look.js +507 -48
- package/kits/physics-3d/engine3d/lookbake.js +208 -0
- package/kits/physics-3d/engine3d/materialpresets.js +2 -0
- package/kits/physics-3d/engine3d/motion.js +39 -10
- package/kits/physics-3d/engine3d/nearshadows.js +37 -19
- package/kits/physics-3d/engine3d/optimize.js +58 -5
- package/kits/physics-3d/engine3d/orbitinput.js +755 -0
- package/kits/physics-3d/engine3d/orbitlens.js +48 -0
- package/kits/physics-3d/engine3d/ownmodel.js +213 -0
- package/kits/physics-3d/engine3d/pxmodel.js +42 -11
- package/kits/physics-3d/engine3d/shadowfade.js +882 -0
- package/kits/physics-3d/engine3d/sky.js +547 -0
- package/kits/physics-3d/engine3d/skylife.js +306 -0
- package/kits/physics-3d/engine3d/unchanged.js +11 -0
- package/kits/physics-3d/engine3d/viewrange.js +52 -0
- package/kits/physics-3d/engine3d/world3d.js +870 -87
- package/kits/physics-3d/main.jsx +5 -0
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
- package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
- package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
- package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
- package/kits/physics-3d/scripts/test-camera-orbit.mjs +4588 -0
- package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
- package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
- package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
- package/kits/physics-3d/scripts/test-gait.mjs +387 -0
- package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
- package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
- package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
- package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
- package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
- package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
- package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
- package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
- package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +13 -1
- package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
- package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
- package/kits/physics-3d/scripts/test-steady-hover.mjs +635 -0
- package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
- package/kits/physics-3d/systems/physics3d.js +474 -12
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/server/gameHooks.js +7 -0
- package/kits/real-time/code/server/session.js +97 -19
- package/package.json +2 -2
- package/dist/shell/assets/index-Cf61cNfT.css +0 -1
|
@@ -0,0 +1,1920 @@
|
|
|
1
|
+
// The near shadows' far edge under the orbit camera key
|
|
2
|
+
// (engine3d/shadowfade.js): a scene with both `"camera": { "follow": "rigid",
|
|
3
|
+
// "control": "orbit" }` and `"shadows": { "distance": n }` draws the frames
|
|
4
|
+
// the near-shadow mode draws with the sun's shadow fading out over the last
|
|
5
|
+
// quarter of the mode's reach, through world-local variants of the lit
|
|
6
|
+
// materials, put on the meshes for that one render call only.
|
|
7
|
+
//
|
|
8
|
+
// Everything here runs three.js's own WebGLRenderer (render lists, program
|
|
9
|
+
// cache, light setup, shadow pass, loop unrolling) on a WebGL2 context that
|
|
10
|
+
// draws nothing (`fakeGL`), so program cache keys, the renderer's program
|
|
11
|
+
// list, the shader source handed to the context and every draw call are
|
|
12
|
+
// three's. What is checked:
|
|
13
|
+
// - under both keys: the receiving lit meshes are drawn with variants whose
|
|
14
|
+
// fade is whole to 0.75 of the reach and gone at it, and the compiled
|
|
15
|
+
// shader fades the first directional shadow only;
|
|
16
|
+
// - without both keys (no key, near shadows only, orbit only, an editor's
|
|
17
|
+
// frame): no variant, no change to any material (own properties, hooks,
|
|
18
|
+
// key, version, listeners, JSON), and the program list, the shader
|
|
19
|
+
// sources and the draws are those of a world that never had the code;
|
|
20
|
+
// - a keyed and a keyless world sharing materials: the keyless one is as if
|
|
21
|
+
// the keyed one were keyless too;
|
|
22
|
+
// - a render nested in the frame from an object's `onBeforeRender` draws
|
|
23
|
+
// the originals; a nested `renderWorld` drops the fade, after the frame;
|
|
24
|
+
// - a scene load, an editor frame, a keyless frame and dispose leave no
|
|
25
|
+
// variant, no program of one, nothing on any mesh;
|
|
26
|
+
// - variants follow their originals: added meshes, changed arrays,
|
|
27
|
+
// `needsUpdate`, a new hook, colour; one not drawn for a while is
|
|
28
|
+
// disposed.
|
|
29
|
+
//
|
|
30
|
+
// test-static-actors.mjs runs it (`npm run check`).
|
|
31
|
+
// node scripts/test-orbit-shadow-fade.mjs
|
|
32
|
+
import assert from 'node:assert/strict';
|
|
33
|
+
import { readFileSync } from 'node:fs';
|
|
34
|
+
import { runInNewContext } from 'node:vm';
|
|
35
|
+
import * as THREE from 'three';
|
|
36
|
+
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
|
|
37
|
+
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
|
|
38
|
+
import { OutputPass } from 'three/examples/jsm/postprocessing/OutputPass.js';
|
|
39
|
+
import './test-static-physics.mjs';
|
|
40
|
+
|
|
41
|
+
const { makeWorld3D, renderWorld, disposeWorld } = await import('../engine3d/world3d.js');
|
|
42
|
+
const { Physics3DSystem } = await import('../systems/physics3d.js');
|
|
43
|
+
const { FADE_FROM, PROGRAM_KEY_SUFFIX, shadowFadeOriginal } = await import('../engine3d/shadowfade.js');
|
|
44
|
+
const { knownCompileHook, knownProgramKey, markKitHooks } = await import('../engine3d/kithooks.js');
|
|
45
|
+
|
|
46
|
+
// A WebGL2 context that draws nothing, checks program lifetimes and models
|
|
47
|
+
// Chromium's deferred native link readback. `sources` is every shader
|
|
48
|
+
// source handed to it.
|
|
49
|
+
function fakeGL(parallelShaderCompile = false) {
|
|
50
|
+
const names = new Map();
|
|
51
|
+
let next = 0xa000;
|
|
52
|
+
names.set('COMPLETION_STATUS_KHR', 0x91b1);
|
|
53
|
+
const constant = (name) => {
|
|
54
|
+
if (!names.has(name)) names.set(name, next++);
|
|
55
|
+
return names.get(name);
|
|
56
|
+
};
|
|
57
|
+
const nameOf = (value) => [...names].find(([, v]) => v === value)?.[0];
|
|
58
|
+
const sources = [];
|
|
59
|
+
const programs = new Map();
|
|
60
|
+
const programCalls = [];
|
|
61
|
+
const pendingPrograms = new Set();
|
|
62
|
+
function checkProgram(method, program) {
|
|
63
|
+
const state = programs.get(program);
|
|
64
|
+
assert.ok(state && !state.deleted, `${method}: ${state ? `deleted program ${state.id}, deleted at ${state.deleted}` : 'unknown program'}`);
|
|
65
|
+
programCalls.push({ method, program });
|
|
66
|
+
}
|
|
67
|
+
// Chromium's passthrough decoder queues a native completion query at
|
|
68
|
+
// link time, even without the WebGL KHR extension. ProcessQueries checks
|
|
69
|
+
// LINK_STATUS on its saved service id, then reads program metadata. If
|
|
70
|
+
// deletion removed the id mapping meanwhile, those reads use program 0:
|
|
71
|
+
// twelve glGetProgramiv errors from a single deleted probe. Model the
|
|
72
|
+
// deferred readback here; JavaScript queries alone cannot catch it.
|
|
73
|
+
// gpu/command_buffer/service/gles2_cmd_decoder_passthrough{,_doers}.cc
|
|
74
|
+
function finishProgramQueries() {
|
|
75
|
+
const errors = [];
|
|
76
|
+
for (const program of pendingPrograms) {
|
|
77
|
+
pendingPrograms.delete(program);
|
|
78
|
+
const metadataProgram = programs.get(program).deleted ? null : program;
|
|
79
|
+
const queries = [
|
|
80
|
+
['LINK_STATUS', program],
|
|
81
|
+
...[
|
|
82
|
+
'ACTIVE_ATTRIBUTES', 'ACTIVE_UNIFORMS', 'LINK_STATUS',
|
|
83
|
+
'ACTIVE_ATTRIBUTE_MAX_LENGTH', 'ACTIVE_UNIFORM_MAX_LENGTH',
|
|
84
|
+
'ACTIVE_UNIFORM_BLOCKS', 'ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH',
|
|
85
|
+
'TRANSFORM_FEEDBACK_BUFFER_MODE', 'TRANSFORM_FEEDBACK_VARYINGS',
|
|
86
|
+
'TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH', 'ACTIVE_UNIFORMS',
|
|
87
|
+
].map((p) => [p, metadataProgram]),
|
|
88
|
+
];
|
|
89
|
+
for (const [p, queried] of queries) {
|
|
90
|
+
try {
|
|
91
|
+
checkProgram(`Chromium deferred getProgramParameter(${p})`, queried);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
errors.push(error);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (errors.length) throw new AggregateError(errors, `${errors.length} invalid deferred program queries: ${errors[0].message}`);
|
|
98
|
+
}
|
|
99
|
+
const params = {
|
|
100
|
+
VERSION: 'WebGL 2.0',
|
|
101
|
+
SHADING_LANGUAGE_VERSION: 'WebGL GLSL ES 3.00',
|
|
102
|
+
VIEWPORT: new Int32Array([0, 0, 412, 870]),
|
|
103
|
+
SCISSOR_BOX: new Int32Array([0, 0, 412, 870]),
|
|
104
|
+
};
|
|
105
|
+
const methods = {
|
|
106
|
+
getParameter(p) {
|
|
107
|
+
const name = nameOf(p);
|
|
108
|
+
if (name in params) return params[name];
|
|
109
|
+
if (name?.startsWith('MAX_')) return name.includes('TEXTURE_SIZE') ? 4096 : 16;
|
|
110
|
+
return 0;
|
|
111
|
+
},
|
|
112
|
+
getExtension: (name) => parallelShaderCompile && name === 'KHR_parallel_shader_compile' ? { COMPLETION_STATUS_KHR: constant('COMPLETION_STATUS_KHR') } : null,
|
|
113
|
+
getSupportedExtensions: () => parallelShaderCompile ? ['KHR_parallel_shader_compile'] : [],
|
|
114
|
+
getContextAttributes: () => ({ alpha: true, antialias: false, depth: true, stencil: false, premultipliedAlpha: true }),
|
|
115
|
+
getShaderPrecisionFormat: () => ({ precision: 23, rangeMin: 127, rangeMax: 127 }),
|
|
116
|
+
createProgram() {
|
|
117
|
+
const program = {};
|
|
118
|
+
programs.set(program, { id: programs.size + 1, deleted: null });
|
|
119
|
+
return program;
|
|
120
|
+
},
|
|
121
|
+
deleteProgram(program) {
|
|
122
|
+
const state = programs.get(program);
|
|
123
|
+
assert.ok(state, 'deleteProgram: unknown program');
|
|
124
|
+
state.deleted = new Error().stack;
|
|
125
|
+
},
|
|
126
|
+
linkProgram(program) {
|
|
127
|
+
checkProgram('linkProgram', program);
|
|
128
|
+
pendingPrograms.add(program);
|
|
129
|
+
},
|
|
130
|
+
getProgramParameter(program, p) {
|
|
131
|
+
// Blocking metadata reads finish the queued native link readback.
|
|
132
|
+
if (p !== constant('COMPLETION_STATUS_KHR')) finishProgramQueries();
|
|
133
|
+
checkProgram(`getProgramParameter(${nameOf(p)})`, program);
|
|
134
|
+
if (p === constant('ACTIVE_UNIFORMS') || p === constant('ACTIVE_ATTRIBUTES')) return 1;
|
|
135
|
+
return p === constant('LINK_STATUS') || p === constant('COMPLETION_STATUS_KHR');
|
|
136
|
+
},
|
|
137
|
+
getShaderParameter: () => true,
|
|
138
|
+
getProgramInfoLog(program) {
|
|
139
|
+
finishProgramQueries();
|
|
140
|
+
checkProgram('getProgramInfoLog', program);
|
|
141
|
+
return '';
|
|
142
|
+
},
|
|
143
|
+
useProgram(program) {
|
|
144
|
+
if (program !== null) checkProgram('useProgram', program);
|
|
145
|
+
},
|
|
146
|
+
getUniformLocation(program) {
|
|
147
|
+
checkProgram('getUniformLocation', program);
|
|
148
|
+
return {};
|
|
149
|
+
},
|
|
150
|
+
getActiveUniform(program) {
|
|
151
|
+
checkProgram('getActiveUniform', program);
|
|
152
|
+
return { name: 'unused', size: 1, type: constant('FLOAT') };
|
|
153
|
+
},
|
|
154
|
+
getShaderInfoLog: () => '',
|
|
155
|
+
isContextLost: () => false,
|
|
156
|
+
checkFramebufferStatus: () => constant('FRAMEBUFFER_COMPLETE'),
|
|
157
|
+
getAttribLocation(program) {
|
|
158
|
+
checkProgram('getAttribLocation', program);
|
|
159
|
+
return 0;
|
|
160
|
+
},
|
|
161
|
+
getActiveAttrib(program) {
|
|
162
|
+
checkProgram('getActiveAttrib', program);
|
|
163
|
+
return { name: 'position', size: 1, type: constant('FLOAT_VEC3') };
|
|
164
|
+
},
|
|
165
|
+
shaderSource: (shader, source) => sources.push(source),
|
|
166
|
+
};
|
|
167
|
+
const canvas = { width: 412, height: 870, style: {}, addEventListener() {}, removeEventListener() {} };
|
|
168
|
+
const target = { canvas, drawingBufferWidth: 412, drawingBufferHeight: 870, drawingBufferColorSpace: 'srgb', sources, programs, programCalls, finishProgramQueries };
|
|
169
|
+
return new Proxy(target, {
|
|
170
|
+
get(t, prop) {
|
|
171
|
+
if (prop in t) return t[prop];
|
|
172
|
+
if (typeof prop !== 'string') return undefined;
|
|
173
|
+
if (prop in methods) return methods[prop];
|
|
174
|
+
if (/^[A-Z0-9_]+$/.test(prop)) return constant(prop);
|
|
175
|
+
return () => ({});
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function makeSun() {
|
|
181
|
+
const sun = new THREE.DirectionalLight(0xffffff, 1.15);
|
|
182
|
+
sun.position.set(6, 18, -6);
|
|
183
|
+
sun.castShadow = true;
|
|
184
|
+
sun.shadow.mapSize.set(2048, 2048);
|
|
185
|
+
Object.assign(sun.shadow.camera, { near: 0.5, far: 50, top: 85, bottom: -35, left: -75, right: 75 });
|
|
186
|
+
sun.shadow.camera.updateProjectionMatrix();
|
|
187
|
+
return sun;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Every draw a world's renderer makes: the object, the material, how deep in
|
|
191
|
+
// `renderer.render` calls, whether it is a shadow pass draw, and for a
|
|
192
|
+
// variant the fade it was drawn with.
|
|
193
|
+
const worlds = [];
|
|
194
|
+
let parallelShaderCompile = false;
|
|
195
|
+
|
|
196
|
+
function makeWorld(stuff) {
|
|
197
|
+
const gl = fakeGL(parallelShaderCompile);
|
|
198
|
+
// The animation loop's frame source, for `dispose`.
|
|
199
|
+
globalThis.self = { requestAnimationFrame: () => 0, cancelAnimationFrame() {} };
|
|
200
|
+
const renderer = new THREE.WebGLRenderer({ canvas: gl.canvas, context: gl });
|
|
201
|
+
delete globalThis.self;
|
|
202
|
+
renderer.shadowMap.enabled = true;
|
|
203
|
+
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
|
|
204
|
+
const scene = new THREE.Scene();
|
|
205
|
+
const sun = makeSun();
|
|
206
|
+
scene.add(sun);
|
|
207
|
+
const camera = new THREE.PerspectiveCamera(70, 412 / 870, 0.1, 1000);
|
|
208
|
+
// The orbit camera near the horizontal.
|
|
209
|
+
camera.position.set(0, 1.6, 10);
|
|
210
|
+
camera.lookAt(0, 1.2, 0);
|
|
211
|
+
const world = {
|
|
212
|
+
scene,
|
|
213
|
+
camera,
|
|
214
|
+
renderer,
|
|
215
|
+
sun: null,
|
|
216
|
+
composer: { render: () => renderer.render(scene, camera) },
|
|
217
|
+
nodes: new Map(),
|
|
218
|
+
parked: new Map(),
|
|
219
|
+
models: new Map(),
|
|
220
|
+
skipping: { on: false },
|
|
221
|
+
modelSigs: new Map(),
|
|
222
|
+
sigEpoch: 0,
|
|
223
|
+
syncCount: 0,
|
|
224
|
+
syncDepth: 0,
|
|
225
|
+
generation: 0,
|
|
226
|
+
snapshots: new WeakMap(),
|
|
227
|
+
nearShadows: null,
|
|
228
|
+
orbitLens: null,
|
|
229
|
+
shadowFade: null,
|
|
230
|
+
lookBake: null,
|
|
231
|
+
instances: new Map(),
|
|
232
|
+
gl,
|
|
233
|
+
draws: [],
|
|
234
|
+
depth: 0,
|
|
235
|
+
};
|
|
236
|
+
const render = renderer.render;
|
|
237
|
+
renderer.render = function (...args) {
|
|
238
|
+
world.depth += 1;
|
|
239
|
+
try {
|
|
240
|
+
return render.apply(this, args);
|
|
241
|
+
} finally {
|
|
242
|
+
world.depth -= 1;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
const direct = renderer.renderBufferDirect;
|
|
246
|
+
renderer.renderBufferDirect = function (cam, sc, geometry, material, object, group) {
|
|
247
|
+
const shadow = material.isMeshDepthMaterial === true || material.isMeshDistanceMaterial === true;
|
|
248
|
+
const draw = { name: object.name, material, depth: world.depth, shadow, fade: undefined };
|
|
249
|
+
world.draws.push(draw);
|
|
250
|
+
// The value bound for this draw (the program is made inside the call).
|
|
251
|
+
direct.call(this, cam, sc, geometry, material, object, group);
|
|
252
|
+
if (!shadow) draw.fade = renderer.properties.get(material).uniforms?.nearShadowFade?.value.toArray();
|
|
253
|
+
};
|
|
254
|
+
furnish(world, stuff);
|
|
255
|
+
worlds.push(world);
|
|
256
|
+
return world;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// The materials a scene's meshes draw with: one of each lit kind, one
|
|
260
|
+
// look.js-style patched, a transparent double-sided one (three flips its
|
|
261
|
+
// side for each draw), an array of a lit and an unlit one, an unlit one and
|
|
262
|
+
// a lit one on a mesh that receives no shadow, and one with a deck's own
|
|
263
|
+
// shader hook.
|
|
264
|
+
function makeStuff() {
|
|
265
|
+
const patched = new THREE.MeshStandardMaterial({ name: 'patched' });
|
|
266
|
+
lookPatch(patched);
|
|
267
|
+
const lit = {
|
|
268
|
+
ground: new THREE.MeshStandardMaterial({ name: 'ground' }),
|
|
269
|
+
standard: new THREE.MeshStandardMaterial({ name: 'standard' }),
|
|
270
|
+
physical: new THREE.MeshPhysicalMaterial({ name: 'physical' }),
|
|
271
|
+
lambert: new THREE.MeshLambertMaterial({ name: 'lambert' }),
|
|
272
|
+
phong: new THREE.MeshPhongMaterial({ name: 'phong' }),
|
|
273
|
+
toon: new THREE.MeshToonMaterial({ name: 'toon' }),
|
|
274
|
+
patched,
|
|
275
|
+
glass: new THREE.MeshStandardMaterial({ name: 'glass', transparent: true, opacity: 0.5, side: THREE.DoubleSide }),
|
|
276
|
+
};
|
|
277
|
+
const pair = [new THREE.MeshStandardMaterial({ name: 'pair-lit' }), new THREE.MeshBasicMaterial({ name: 'pair-unlit' })];
|
|
278
|
+
const basic = new THREE.MeshBasicMaterial({ name: 'basic' });
|
|
279
|
+
const shy = new THREE.MeshStandardMaterial({ name: 'shy' });
|
|
280
|
+
const foreign = new THREE.MeshStandardMaterial({ name: 'foreign' });
|
|
281
|
+
deckHook(foreign);
|
|
282
|
+
const geometry = new THREE.BoxGeometry(1, 1, 1);
|
|
283
|
+
return { lit, pair, basic, shy, foreign, geometry, all: [...Object.values(lit), ...pair, basic, shy, foreign] };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function furnish(world, stuff) {
|
|
287
|
+
const add = (name, material, x, z, receive = true) => {
|
|
288
|
+
const mesh = new THREE.Mesh(name === 'ground' ? new THREE.PlaneGeometry(200, 200) : stuff.geometry, material);
|
|
289
|
+
mesh.name = name;
|
|
290
|
+
mesh.receiveShadow = receive;
|
|
291
|
+
mesh.castShadow = name !== 'ground';
|
|
292
|
+
if (name === 'ground') mesh.rotation.x = -Math.PI / 2;
|
|
293
|
+
else mesh.position.set(x, 0.5, z);
|
|
294
|
+
world.scene.add(mesh);
|
|
295
|
+
return mesh;
|
|
296
|
+
};
|
|
297
|
+
// All in the view.
|
|
298
|
+
let x = -2.8;
|
|
299
|
+
for (const [name, material] of Object.entries(stuff.lit)) add(name, material, (x += 0.8), 0);
|
|
300
|
+
add('pair', stuff.pair, -1.5, -3);
|
|
301
|
+
add('basic', stuff.basic, 0, -3);
|
|
302
|
+
add('shy', stuff.shy, 1.5, -3, false);
|
|
303
|
+
add('foreign', stuff.foreign, -3, -3);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// A look.js-style patch, marked as the kit's (kithooks.js): wraps what the
|
|
307
|
+
// material had.
|
|
308
|
+
function lookPatch(material) {
|
|
309
|
+
const previous = material.onBeforeCompile;
|
|
310
|
+
const previousKey = material.customProgramCacheKey;
|
|
311
|
+
material.onBeforeCompile = (shader, renderer) => {
|
|
312
|
+
previous.call(material, shader, renderer);
|
|
313
|
+
shader.fragmentShader = '// look\n' + shader.fragmentShader;
|
|
314
|
+
};
|
|
315
|
+
material.customProgramCacheKey = () => `look|${previousKey.call(material)}`;
|
|
316
|
+
markKitHooks(material, previous, previousKey);
|
|
317
|
+
material.needsUpdate = true;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// A deck's own hook: never run for a variant, so its material is drawn as
|
|
321
|
+
// it is.
|
|
322
|
+
function deckHook(material) {
|
|
323
|
+
material.onBeforeCompile = (shader) => {
|
|
324
|
+
shader.fragmentShader = '// deck\n' + shader.fragmentShader;
|
|
325
|
+
};
|
|
326
|
+
material.needsUpdate = true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// A runtime as physics-2d's SceneRuntime has it after a load.
|
|
330
|
+
function runtime(world, { orbit = false, distance = 0 } = {}) {
|
|
331
|
+
return {
|
|
332
|
+
three: world,
|
|
333
|
+
cameraControl: orbit ? 'orbit' : null,
|
|
334
|
+
cameraFollowsRigidly: true,
|
|
335
|
+
nearShadowDistance: distance,
|
|
336
|
+
data: { engine: { camera: { follow: 'rigid', ...(orbit ? { control: 'orbit' } : {}) }, ...(distance ? { shadows: { distance } } : {}) } },
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
const KEYED = { orbit: true, distance: 40 };
|
|
340
|
+
|
|
341
|
+
const physics = new Physics3DSystem();
|
|
342
|
+
|
|
343
|
+
function frames(world, run, count) {
|
|
344
|
+
for (let i = 0; i < count; i++) {
|
|
345
|
+
world.draws = [];
|
|
346
|
+
renderWorld(world, run);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Everything of a material's that a world could change.
|
|
351
|
+
function materialState(material) {
|
|
352
|
+
const listeners = material._listeners ?? {};
|
|
353
|
+
return {
|
|
354
|
+
own: Object.getOwnPropertyNames(material).sort(),
|
|
355
|
+
hooks: [material.onBeforeCompile, material.customProgramCacheKey, material.onBeforeRender],
|
|
356
|
+
key: material.customProgramCacheKey(),
|
|
357
|
+
version: material.version,
|
|
358
|
+
side: material.side,
|
|
359
|
+
listeners: Object.keys(listeners).map((type) => [type, listeners[type].length]),
|
|
360
|
+
json: JSON.stringify(material.toJSON()),
|
|
361
|
+
prototype: Object.getPrototypeOf(material),
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// What a renderer made and drew, by names (uuids differ from world to world).
|
|
366
|
+
function rendered(world) {
|
|
367
|
+
return {
|
|
368
|
+
programs: world.renderer.info.programs.map((program) => [program.cacheKey.replace(/[0-9a-f-]{36}/g, 'uuid'), program.usedTimes]),
|
|
369
|
+
sources: world.gl.sources.slice(),
|
|
370
|
+
draws: world.draws.map(({ name, material, depth, shadow }) => [name, material.name, material.type, depth, shadow]),
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const smooth = (a, b, x) => {
|
|
375
|
+
const t = Math.min(1, Math.max(0, (x - a) / (b - a)));
|
|
376
|
+
return t * t * (3 - 2 * t);
|
|
377
|
+
};
|
|
378
|
+
// nearShadowFadeAt, as the shader computes it.
|
|
379
|
+
const fadeAt = ([from, to], depth) => 1 - smooth(from, to, depth);
|
|
380
|
+
|
|
381
|
+
// A variant's own data properties: the rest are accessors to its original.
|
|
382
|
+
const ownValues = (variant) =>
|
|
383
|
+
Reflect.ownKeys(variant)
|
|
384
|
+
.filter((key) => typeof key === 'string' && 'value' in Reflect.getOwnPropertyDescriptor(variant, key))
|
|
385
|
+
.sort();
|
|
386
|
+
|
|
387
|
+
const isVariantOf = (material, original) => material !== original && shadowFadeOriginal(material) === original;
|
|
388
|
+
|
|
389
|
+
// Review 14: an LOD holds the material three listed by locking it or
|
|
390
|
+
// redefining it as an accessor without a setter during that render.
|
|
391
|
+
function heldMaterialWorld(keyed, accessor) {
|
|
392
|
+
const stuff = makeStuff();
|
|
393
|
+
const world = makeWorld(stuff);
|
|
394
|
+
const mesh = world.scene.getObjectByName('standard');
|
|
395
|
+
const original = stuff.lit.standard;
|
|
396
|
+
const lod = new THREE.LOD();
|
|
397
|
+
world.scene.remove(mesh);
|
|
398
|
+
lod.addLevel(mesh, 0);
|
|
399
|
+
world.scene.add(lod);
|
|
400
|
+
const run = runtime(world, keyed ? KEYED : { distance: 40 });
|
|
401
|
+
frames(world, run, 2);
|
|
402
|
+
let once = true;
|
|
403
|
+
lod.update = function (camera) {
|
|
404
|
+
THREE.LOD.prototype.update.call(this, camera);
|
|
405
|
+
if (!once) return;
|
|
406
|
+
once = false;
|
|
407
|
+
const held = mesh.material;
|
|
408
|
+
Object.defineProperty(mesh, 'material', accessor
|
|
409
|
+
? { get: () => held, configurable: false }
|
|
410
|
+
: { value: held, writable: false, configurable: false });
|
|
411
|
+
};
|
|
412
|
+
frames(world, run, 3);
|
|
413
|
+
if (keyed) assert.ok(isVariantOf(mesh.material, original), 'the mesh holds the variant');
|
|
414
|
+
return { world, mesh, original, lod, run };
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function heldMaterialDraws(world) {
|
|
418
|
+
// A held variant has its own sorting id and renderer entry; the original
|
|
419
|
+
// may also retain its warmup program. Compare draw contents and programs,
|
|
420
|
+
// independently of their order and the number of material references.
|
|
421
|
+
return {
|
|
422
|
+
draws: world.draws.map(({ name, material, shadow, fade }) => [name, material.name, shadow, fade]).sort(),
|
|
423
|
+
programs: world.renderer.info.programs.map((program) => program.cacheKey).sort(),
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const scenarios = {
|
|
428
|
+
'importing the fade does not build the faded lights string'() {
|
|
429
|
+
const source = readFileSync(new URL('../engine3d/shadowfade.js', import.meta.url), 'utf8')
|
|
430
|
+
.replace(/^import .*;$/gm, '').replace(/export /g, '');
|
|
431
|
+
let shaderReads = 0;
|
|
432
|
+
const three = new Proxy(THREE, {
|
|
433
|
+
get(target, key) {
|
|
434
|
+
if (key === 'ShaderChunk') {
|
|
435
|
+
shaderReads += 1;
|
|
436
|
+
throw new Error('import must not read the shader chunk');
|
|
437
|
+
}
|
|
438
|
+
return target[key];
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
runInNewContext(source, { THREE: three });
|
|
442
|
+
assert.equal(shaderReads, 0);
|
|
443
|
+
},
|
|
444
|
+
'keyless hook installers mark functions without a bookkeeping object'() {
|
|
445
|
+
for (const [file, name] of [['look', 'patchMaterial'], ['optimize', 'finishFromVertices'], ['materialpresets', 'glowByColor']]) {
|
|
446
|
+
const source = readFileSync(new URL(`../engine3d/${file}.js`, import.meta.url), 'utf8');
|
|
447
|
+
const start = source.indexOf(`function ${name}(`);
|
|
448
|
+
const end = source.indexOf('\n}', start) + 2;
|
|
449
|
+
let marked = 0;
|
|
450
|
+
const install = runInNewContext(`(${source.slice(start, end)})`, {
|
|
451
|
+
markKitHooks(...args) {
|
|
452
|
+
marked += 1;
|
|
453
|
+
assert.ok(args.slice(1).every((arg) => typeof arg === 'function'), `${file}: plain hook arguments only`);
|
|
454
|
+
assert.equal(args.length, file === 'materialpresets' ? 1 : 3);
|
|
455
|
+
markKitHooks(...args);
|
|
456
|
+
},
|
|
457
|
+
});
|
|
458
|
+
const material = new THREE.MeshStandardMaterial();
|
|
459
|
+
material.onBeforeCompile = material.onBeforeCompile;
|
|
460
|
+
material.customProgramCacheKey = material.customProgramCacheKey;
|
|
461
|
+
const before = Reflect.ownKeys(material);
|
|
462
|
+
install(material, { rim: 0.2, ao: 0 }, { getAttribute: () => null });
|
|
463
|
+
assert.equal(marked, 1);
|
|
464
|
+
assert.ok(knownCompileHook(material.onBeforeCompile));
|
|
465
|
+
assert.ok(knownProgramKey(material.customProgramCacheKey));
|
|
466
|
+
assert.deepEqual(Reflect.ownKeys(material), before, `${file}: marking adds no material bookkeeping`);
|
|
467
|
+
material.dispose();
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
'a fresh keyless world allocates no own-model registration set and disposes'() {
|
|
471
|
+
const gl = fakeGL();
|
|
472
|
+
gl.canvas.getContext = () => gl;
|
|
473
|
+
gl.canvas.clientWidth = 412;
|
|
474
|
+
gl.canvas.clientHeight = 870;
|
|
475
|
+
const previousWindow = globalThis.window;
|
|
476
|
+
const previousSelf = globalThis.self;
|
|
477
|
+
globalThis.window = { devicePixelRatio: 1 };
|
|
478
|
+
globalThis.self = { requestAnimationFrame: () => 0, cancelAnimationFrame() {} };
|
|
479
|
+
try {
|
|
480
|
+
const world = makeWorld3D(gl.canvas);
|
|
481
|
+
assert.equal(world.ownModelWith, undefined);
|
|
482
|
+
assert.equal(Object.hasOwn(world, 'ownModelWith'), false);
|
|
483
|
+
disposeWorld(world);
|
|
484
|
+
} finally {
|
|
485
|
+
if (previousWindow === undefined) delete globalThis.window;
|
|
486
|
+
else globalThis.window = previousWindow;
|
|
487
|
+
if (previousSelf === undefined) delete globalThis.self;
|
|
488
|
+
else globalThis.self = previousSelf;
|
|
489
|
+
}
|
|
490
|
+
},
|
|
491
|
+
|
|
492
|
+
'the fake rejects deleted and unknown programs, including deferred Chromium queries'() {
|
|
493
|
+
const gl = fakeGL(parallelShaderCompile);
|
|
494
|
+
const program = gl.createProgram();
|
|
495
|
+
gl.linkProgram(program);
|
|
496
|
+
gl.deleteProgram(program);
|
|
497
|
+
assert.throws(() => gl.finishProgramQueries(), (error) => error instanceof AggregateError && error.errors.length === 12);
|
|
498
|
+
const queries = [
|
|
499
|
+
['getProgramParameter', gl.LINK_STATUS], ['getProgramInfoLog'], ['useProgram'],
|
|
500
|
+
['getUniformLocation', 'unused'], ['getActiveUniform', 0], ['getAttribLocation', 'position'],
|
|
501
|
+
];
|
|
502
|
+
if (parallelShaderCompile) queries.push(['getProgramParameter', gl.getExtension('KHR_parallel_shader_compile').COMPLETION_STATUS_KHR]);
|
|
503
|
+
for (const [method, ...args] of queries) {
|
|
504
|
+
assert.throws(() => gl[method](program, ...args), /deleted program/, method);
|
|
505
|
+
assert.throws(() => gl[method]({}, ...args), /unknown program/, method);
|
|
506
|
+
}
|
|
507
|
+
gl.useProgram(null); // Unbinding is valid.
|
|
508
|
+
},
|
|
509
|
+
|
|
510
|
+
'the listener probe finishes first-use initialization before disposal, with or without shader diagnostics'() {
|
|
511
|
+
for (const diagnostics of [true, false]) {
|
|
512
|
+
const world = makeWorld(makeStuff());
|
|
513
|
+
world.renderer.debug.checkShaderErrors = diagnostics;
|
|
514
|
+
const compile = world.renderer.compile;
|
|
515
|
+
let probe = null;
|
|
516
|
+
let program = null;
|
|
517
|
+
let calls = 0;
|
|
518
|
+
world.renderer.compile = function (...args) {
|
|
519
|
+
const materials = compile.apply(this, args);
|
|
520
|
+
assert.equal(materials.size, 1, 'the private probe is compiled alone');
|
|
521
|
+
probe = [...materials][0];
|
|
522
|
+
program = this.properties.get(probe).currentProgram.program;
|
|
523
|
+
calls++;
|
|
524
|
+
return materials;
|
|
525
|
+
};
|
|
526
|
+
frames(world, runtime(world, { orbit: true, distance: 12 }), 6);
|
|
527
|
+
assert.equal(calls, 1, 'the listener is learned once per renderer');
|
|
528
|
+
assert.ok(world.gl.programs.get(program).deleted, 'the private program was released');
|
|
529
|
+
assert.equal(world.renderer.properties.has(probe), false, 'its material entry was released');
|
|
530
|
+
const queries = world.gl.programCalls.filter((call) => call.program === program).map((call) => call.method);
|
|
531
|
+
assert.ok(queries.includes('getProgramParameter(ACTIVE_UNIFORMS)'), 'first-use uniform discovery ran while live');
|
|
532
|
+
assert.ok(queries.includes('getUniformLocation'), 'uniform locations were initialized');
|
|
533
|
+
assert.ok(!queries.includes('useProgram'), 'the probe was never bound for a draw');
|
|
534
|
+
assert.equal(queries.includes('getProgramInfoLog'), diagnostics, 'shader diagnostics retain their setting');
|
|
535
|
+
world.gl.finishProgramQueries();
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
|
|
539
|
+
'page load with orbit control and shadow distance 12 keeps programs live through compilation and disposal': async function () {
|
|
540
|
+
const world = makeWorld(makeStuff());
|
|
541
|
+
const composer = new EffectComposer(world.renderer);
|
|
542
|
+
const output = new OutputPass();
|
|
543
|
+
composer.addPass(new RenderPass(world.scene, world.camera));
|
|
544
|
+
composer.addPass(output);
|
|
545
|
+
world.composer = composer;
|
|
546
|
+
const run = runtime(world, { orbit: true, distance: 12 });
|
|
547
|
+
assert.equal(world.renderer.debug.checkShaderErrors, true, "three's default shader diagnostics");
|
|
548
|
+
frames(world, run, 1);
|
|
549
|
+
frames(world, run, 1);
|
|
550
|
+
assert.ok(world.draws.some((draw) => draw.fade), 'the first near-shadow frame fades');
|
|
551
|
+
await world.renderer.compileAsync(world.scene, world.camera);
|
|
552
|
+
frames(world, run, 4);
|
|
553
|
+
assert.ok(world.gl.programCalls.some(({ method }) => method === 'getProgramParameter(LINK_STATUS)'), 'first-use link checks ran');
|
|
554
|
+
if (parallelShaderCompile) assert.ok(world.gl.programCalls.some(({ method }) => method === 'getProgramParameter(COMPLETION_STATUS_KHR)'), 'parallel completion checks ran');
|
|
555
|
+
frames(world, runtime(world, { distance: 12 }), 1);
|
|
556
|
+
await world.renderer.compileAsync(world.scene, world.camera);
|
|
557
|
+
output.dispose();
|
|
558
|
+
disposeWorld(world);
|
|
559
|
+
world.gl.finishProgramQueries();
|
|
560
|
+
},
|
|
561
|
+
|
|
562
|
+
'a swept held variant is inert and gives its program back when the key goes'() {
|
|
563
|
+
for (const accessor of [false, true]) {
|
|
564
|
+
const play = (keyed) => {
|
|
565
|
+
const { world, mesh, original, lod, run } = heldMaterialWorld(keyed, accessor);
|
|
566
|
+
lod.visible = false;
|
|
567
|
+
frames(world, run, 121);
|
|
568
|
+
if (keyed) assert.equal(world.shadowFade.variants.has(original), false, 'the held variant was swept');
|
|
569
|
+
lod.visible = true;
|
|
570
|
+
frames(world, run, 1);
|
|
571
|
+
assert.equal(world.draws.find((draw) => draw.name === 'standard' && !draw.shadow).fade, undefined, 'a swept variant recompiles unfaded');
|
|
572
|
+
const heldProgram = world.renderer.properties.get(mesh.material);
|
|
573
|
+
frames(world, runtime(world, { distance: 40 }), 3);
|
|
574
|
+
if (keyed) assert.notEqual(world.renderer.properties.get(mesh.material), heldProgram, 'dropping releases even a swept held variant');
|
|
575
|
+
assert.equal(world.renderer.info.programs.filter((program) => program.cacheKey.includes(PROGRAM_KEY_SUFFIX)).length, 0);
|
|
576
|
+
return heldMaterialDraws(world);
|
|
577
|
+
};
|
|
578
|
+
assert.deepEqual(play(true), play(false), `accessor ${accessor}: keyless programs and draws as without the key`);
|
|
579
|
+
}
|
|
580
|
+
},
|
|
581
|
+
|
|
582
|
+
'an inert held variant follows the original current compile hook and program key'() {
|
|
583
|
+
for (const accessor of [false, true]) {
|
|
584
|
+
for (const customKey of [false, true]) {
|
|
585
|
+
const play = (keyed) => {
|
|
586
|
+
const { world, mesh, original } = heldMaterialWorld(keyed, accessor);
|
|
587
|
+
const keyless = runtime(world, { distance: 40 });
|
|
588
|
+
frames(world, keyless, 1);
|
|
589
|
+
let calls = 0;
|
|
590
|
+
original.onBeforeCompile = function (shader, renderer) {
|
|
591
|
+
assert.equal(this, original, 'the compile hook receives the original');
|
|
592
|
+
assert.equal(renderer, world.renderer);
|
|
593
|
+
calls++;
|
|
594
|
+
shader.fragmentShader = '// review-14-keyless-hook\n' + shader.fragmentShader;
|
|
595
|
+
};
|
|
596
|
+
if (customKey) original.customProgramCacheKey = function () {
|
|
597
|
+
assert.equal(this, original, 'the key function receives the original');
|
|
598
|
+
return 'review-14-keyless-key';
|
|
599
|
+
};
|
|
600
|
+
original.needsUpdate = true;
|
|
601
|
+
const start = world.gl.sources.length;
|
|
602
|
+
frames(world, keyless, 1);
|
|
603
|
+
assert.equal(calls, 1, 'the current hook compiles once');
|
|
604
|
+
const sources = world.gl.sources.slice(start);
|
|
605
|
+
assert.ok(sources.some((source) => source.includes('review-14-keyless-hook')));
|
|
606
|
+
assert.equal(mesh.material.customProgramCacheKey(), original.customProgramCacheKey());
|
|
607
|
+
return { ...heldMaterialDraws(world), sources, calls, key: mesh.material.customProgramCacheKey() };
|
|
608
|
+
};
|
|
609
|
+
assert.deepEqual(play(true), play(false), `accessor ${accessor}, custom key ${customKey}: current hooks, sources, programs and draws as without the key`);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
},
|
|
613
|
+
|
|
614
|
+
'under both keys the receiving lit meshes fade out over the last quarter of the reach'() {
|
|
615
|
+
const stuff = makeStuff();
|
|
616
|
+
const world = makeWorld(stuff);
|
|
617
|
+
world.draws = [];
|
|
618
|
+
renderWorld(world, runtime(world, KEYED));
|
|
619
|
+
// The first frame is drawn plain (nearshadows.js term 3), with the originals.
|
|
620
|
+
assert.ok(world.draws.every((draw) => draw.shadow || stuff.all.includes(draw.material)), 'first frame: originals');
|
|
621
|
+
// The materials as three's own drawing of them left them.
|
|
622
|
+
const before = new Map(stuff.all.map((material) => [material, materialState(material)]));
|
|
623
|
+
for (let frame = 0; frame < 4; frame++) {
|
|
624
|
+
world.draws = [];
|
|
625
|
+
renderWorld(world, runtime(world, KEYED));
|
|
626
|
+
const reach = world.nearShadows.shadow.reach;
|
|
627
|
+
assert.ok(reach > 0 && reach <= 40, `the mode drew it (reach ${reach})`);
|
|
628
|
+
const main = world.draws.filter((draw) => !draw.shadow);
|
|
629
|
+
assert.deepEqual(
|
|
630
|
+
[...new Set(main.map((draw) => draw.name))].sort(),
|
|
631
|
+
[...Object.keys(stuff.lit), 'pair', 'basic', 'shy', 'foreign'].sort(),
|
|
632
|
+
'every mesh is drawn'
|
|
633
|
+
);
|
|
634
|
+
for (const draw of main) {
|
|
635
|
+
const original = draw.name === 'pair' ? stuff.pair.find((m) => m.name === draw.material.name) : stuff.all.find((m) => m.name === draw.material.name);
|
|
636
|
+
// (A mesh with an array of materials is drawn as it is.)
|
|
637
|
+
const faded = draw.name in stuff.lit;
|
|
638
|
+
if (faded) {
|
|
639
|
+
assert.ok(isVariantOf(draw.material, original), `${draw.name} is drawn with its variant`);
|
|
640
|
+
assert.deepEqual(draw.fade, [FADE_FROM * reach, reach], `${draw.name}: whole to 0.75 of the reach, gone at it`);
|
|
641
|
+
} else {
|
|
642
|
+
assert.equal(draw.material, original, `${draw.name}/${draw.material.name} is drawn with its own material`);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
// The shadow pass draws the casters with three's depth materials made
|
|
646
|
+
// from the originals.
|
|
647
|
+
assert.ok(world.draws.filter((draw) => draw.shadow).length > 0);
|
|
648
|
+
// After the frame every mesh has its own material.
|
|
649
|
+
world.scene.traverse((object) => {
|
|
650
|
+
if (!object.isMesh) return;
|
|
651
|
+
const want = object.name === 'pair' ? stuff.pair : object.name in stuff.lit ? stuff.lit[object.name] : stuff[object.name];
|
|
652
|
+
assert.equal(object.material, want, `${object.name} has its own material back`);
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
// The fade: 0 at the far edge and past it, 1 well inside.
|
|
656
|
+
const reach = world.nearShadows.shadow.reach;
|
|
657
|
+
const uniform = [FADE_FROM * reach, reach];
|
|
658
|
+
assert.equal(fadeAt(uniform, reach), 0, 'gone at the far edge');
|
|
659
|
+
assert.equal(fadeAt(uniform, reach * 2), 0, 'and past it');
|
|
660
|
+
assert.equal(fadeAt(uniform, reach * 0.5), 1, 'whole half way');
|
|
661
|
+
assert.equal(fadeAt(uniform, FADE_FROM * reach), 1, 'whole to three quarters');
|
|
662
|
+
assert.ok(Math.abs(fadeAt(uniform, 0.875 * reach) - 0.5) < 1e-12, 'half between');
|
|
663
|
+
// The shader three compiled: the first directional shadow faded, the
|
|
664
|
+
// look patch kept, once.
|
|
665
|
+
const faded = world.gl.sources.filter((source) => source.includes('nearShadowFadeAt('));
|
|
666
|
+
assert.ok(faded.length >= 5, `variant fragment shaders compiled, one per program kind (${faded.length})`);
|
|
667
|
+
for (const source of faded) {
|
|
668
|
+
assert.ok(source.includes('directionalLightShadow.shadowIntensity * ( 0 == 0 ? nearShadowFadeAt( vViewPosition.z ) : 1.0 )'), 'unrolled for the sun');
|
|
669
|
+
assert.ok(source.includes('return 1.0 - smoothstep( nearShadowFade.x, nearShadowFade.y, depth );'));
|
|
670
|
+
assert.equal(source.split('uniform vec2 nearShadowFade;').length, 2, 'declared once');
|
|
671
|
+
}
|
|
672
|
+
assert.ok(faded.some((source) => source.includes('// look')), "look.js's patch stands in its variant");
|
|
673
|
+
// The program keys: each variant's is its original's plus the suffix.
|
|
674
|
+
const keys = world.renderer.info.programs.map((program) => program.cacheKey);
|
|
675
|
+
for (const material of Object.values(stuff.lit)) {
|
|
676
|
+
assert.ok(keys.some((key) => key.endsWith(`,${material.customProgramCacheKey()}${PROGRAM_KEY_SUFFIX}`)), `${material.name}: a program of its own`);
|
|
677
|
+
}
|
|
678
|
+
// And nothing of the originals changed by the faded frames but three's
|
|
679
|
+
// own version bumps (a double-sided transparent draw flips the side),
|
|
680
|
+
// which are the original's as they are without the key: the same as a
|
|
681
|
+
// keyless twin given the same frames.
|
|
682
|
+
const twinStuff = makeStuff();
|
|
683
|
+
const twin = makeWorld(twinStuff);
|
|
684
|
+
frames(twin, runtime(twin, { distance: 40 }), 5);
|
|
685
|
+
for (const [i, material] of stuff.all.entries()) {
|
|
686
|
+
const { version, ...now } = materialState(material);
|
|
687
|
+
const { version: _was, ...then } = before.get(material);
|
|
688
|
+
assert.deepEqual(now, then, `${material.name}: no hook, key, property or listener added`);
|
|
689
|
+
assert.equal(version, twinStuff.all[i].version, `${material.name}: the version a keyless world leaves`);
|
|
690
|
+
}
|
|
691
|
+
},
|
|
692
|
+
|
|
693
|
+
'without both keys nothing differs from a world that never had the code'() {
|
|
694
|
+
for (const options of [{}, { distance: 40 }, { orbit: true }, null]) {
|
|
695
|
+
// `null`: an editor's frame (no runtime).
|
|
696
|
+
const stuff = makeStuff();
|
|
697
|
+
const plain = makeStuff();
|
|
698
|
+
const world = makeWorld(stuff);
|
|
699
|
+
const baseline = makeWorld(plain);
|
|
700
|
+
const before = stuff.all.map(materialState);
|
|
701
|
+
frames(world, options ? runtime(world, options) : undefined, 5);
|
|
702
|
+
// The baseline: the engine's keyless draw is the bias fit and one
|
|
703
|
+
// render; here the render alone, by three, with no engine at all for
|
|
704
|
+
// `{}` and the editor frame. Near shadows alone (`distance`) and the
|
|
705
|
+
// orbit key alone are compared to the same scene under main below.
|
|
706
|
+
for (let i = 0; i < 5; i++) {
|
|
707
|
+
baseline.draws = [];
|
|
708
|
+
if (options?.distance || options?.orbit) renderWorld(baseline, runtime(baseline, options));
|
|
709
|
+
else renderWorld(baseline, options ? runtime(baseline) : undefined);
|
|
710
|
+
}
|
|
711
|
+
assert.equal(world.shadowFade, null, `${JSON.stringify(options)}: no fade kept`);
|
|
712
|
+
assert.deepEqual(rendered(world), rendered(baseline), `${JSON.stringify(options)}: the same programs, sources and draws`);
|
|
713
|
+
for (const source of world.gl.sources) assert.ok(!source.includes('nearShadowFade'));
|
|
714
|
+
for (const key of world.renderer.info.programs.map((program) => program.cacheKey)) assert.ok(!key.includes(PROGRAM_KEY_SUFFIX));
|
|
715
|
+
const after = stuff.all.map(materialState);
|
|
716
|
+
const reference = plain.all.map(materialState);
|
|
717
|
+
for (let i = 0; i < after.length; i++) {
|
|
718
|
+
assert.deepEqual(after[i].own, reference[i].own, `${stuff.all[i].name}: the same own properties as the untouched twin`);
|
|
719
|
+
assert.deepEqual(after[i].hooks, before[i].hooks);
|
|
720
|
+
assert.equal(after[i].key, before[i].key);
|
|
721
|
+
assert.equal(after[i].version, reference[i].version);
|
|
722
|
+
assert.deepEqual(after[i].listeners, reference[i].listeners);
|
|
723
|
+
assert.equal(after[i].json.replace(/[0-9a-f-]{36}/g, 'uuid'), reference[i].json.replace(/[0-9a-f-]{36}/g, 'uuid'));
|
|
724
|
+
}
|
|
725
|
+
world.scene.traverse((object) => {
|
|
726
|
+
if (object.isMesh) assert.ok(stuff.all.includes(object.material) || object.material === stuff.pair);
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
},
|
|
730
|
+
|
|
731
|
+
'a keyed and a keyless world sharing materials: the keyless one is as if both were keyless'() {
|
|
732
|
+
const play = (keyed) => {
|
|
733
|
+
const stuff = makeStuff();
|
|
734
|
+
const a = makeWorld(stuff);
|
|
735
|
+
const b = makeWorld(stuff);
|
|
736
|
+
// One frame each (the keyed one's first is plain), then the state of
|
|
737
|
+
// the shared materials, then interleaved frames.
|
|
738
|
+
frames(a, runtime(a, keyed ? KEYED : { distance: 40 }), 1);
|
|
739
|
+
frames(b, runtime(b, { distance: 40 }), 1);
|
|
740
|
+
const seen = [];
|
|
741
|
+
for (let i = 0; i < 6; i++) {
|
|
742
|
+
const before = stuff.all.map(materialState);
|
|
743
|
+
frames(a, runtime(a, keyed ? KEYED : { distance: 40 }), 1);
|
|
744
|
+
if (keyed) {
|
|
745
|
+
assert.ok(a.draws.some((draw) => draw.fade), 'the keyed world fades');
|
|
746
|
+
// Only three's own version bumps, the original's as without the
|
|
747
|
+
// key (compared across the two runs below).
|
|
748
|
+
const strip = (states) => states.map(({ version: _version, ...rest }) => rest);
|
|
749
|
+
assert.deepEqual(strip(stuff.all.map(materialState)), strip(before), 'a keyed frame leaves the shared materials as they were');
|
|
750
|
+
}
|
|
751
|
+
frames(b, runtime(b, { distance: 40 }), 1);
|
|
752
|
+
seen.push(rendered(b).draws);
|
|
753
|
+
assert.ok(b.draws.every((draw) => draw.shadow || stuff.all.includes(draw.material)), 'the keyless world draws the shared materials');
|
|
754
|
+
}
|
|
755
|
+
assert.equal(b.shadowFade, null);
|
|
756
|
+
return { b: { ...rendered(b), draws: seen }, materials: stuff.all.map((material) => [materialState(material).own, material.version]) };
|
|
757
|
+
};
|
|
758
|
+
const shared = play(true);
|
|
759
|
+
const alone = play(false);
|
|
760
|
+
assert.deepEqual(shared, alone, 'the keyless world: programs, sources, draws');
|
|
761
|
+
},
|
|
762
|
+
|
|
763
|
+
'a render nested in the frame sees the originals; a nested renderWorld drops the fade after the frame'() {
|
|
764
|
+
const stuff = makeStuff();
|
|
765
|
+
const world = makeWorld(stuff);
|
|
766
|
+
const other = new THREE.PerspectiveCamera(50, 1, 0.1, 100);
|
|
767
|
+
other.position.set(5, 5, 5);
|
|
768
|
+
other.lookAt(0, 0, 0);
|
|
769
|
+
const probe = world.scene.getObjectByName('standard');
|
|
770
|
+
let nest = null;
|
|
771
|
+
const onMeshes = [];
|
|
772
|
+
probe.onBeforeRender = () => {
|
|
773
|
+
if (!nest || world.depth > 1) return;
|
|
774
|
+
const what = nest;
|
|
775
|
+
nest = null;
|
|
776
|
+
world.scene.traverse((object) => {
|
|
777
|
+
if (object.isMesh) onMeshes.push(object.material);
|
|
778
|
+
});
|
|
779
|
+
what();
|
|
780
|
+
};
|
|
781
|
+
frames(world, runtime(world, KEYED), 2);
|
|
782
|
+
// A plain renderer.render of the same scene from an object's callback.
|
|
783
|
+
nest = () => world.renderer.render(world.scene, other);
|
|
784
|
+
world.draws = [];
|
|
785
|
+
renderWorld(world, runtime(world, KEYED));
|
|
786
|
+
const nested = world.draws.filter((draw) => draw.depth === 2 && !draw.shadow);
|
|
787
|
+
assert.ok(nested.length >= 10, 'the nested render drew');
|
|
788
|
+
for (const draw of nested) assert.ok(stuff.all.includes(draw.material), `nested: ${draw.name} drew its own material`);
|
|
789
|
+
assert.ok(onMeshes.length > 0 && onMeshes.every((material) => stuff.all.includes(material) || material === stuff.pair), 'every mesh had its own material inside the frame');
|
|
790
|
+
const outer = world.draws.filter((draw) => draw.depth === 1 && !draw.shadow && draw.name === 'ground');
|
|
791
|
+
assert.ok(outer.length === 1 && isVariantOf(outer[0].material, stuff.lit.ground), 'the frame itself drew the variant');
|
|
792
|
+
// (The probe's own mesh, which has a callback of its own, is drawn
|
|
793
|
+
// unfaded.)
|
|
794
|
+
const after = world.draws.slice(world.draws.findLastIndex((draw) => draw.depth === 2) + 1).filter((draw) => !draw.shadow && draw.name in stuff.lit && draw.name !== 'standard');
|
|
795
|
+
assert.ok(after.length > 0 && after.every((draw) => draw.fade?.[1] > 0), 'and drew faded after the nested render');
|
|
796
|
+
// A nested renderWorld, keyless: the fade is dropped, its variants are
|
|
797
|
+
// disposed once the frame is over, and the mode is off (nearshadows.js
|
|
798
|
+
// term 2).
|
|
799
|
+
const variants = new Set(world.draws.filter((draw) => draw.fade).map((draw) => draw.material));
|
|
800
|
+
nest = () => renderWorld(world, runtime(world));
|
|
801
|
+
world.draws = [];
|
|
802
|
+
renderWorld(world, runtime(world, KEYED));
|
|
803
|
+
assert.equal(world.shadowFade, null);
|
|
804
|
+
for (const variant of variants) assert.ok(!world.renderer.properties.has(variant), 'variant freed');
|
|
805
|
+
assert.ok(!world.renderer.info.programs.some((program) => program.cacheKey.endsWith(PROGRAM_KEY_SUFFIX)), 'and its programs');
|
|
806
|
+
world.scene.traverse((object) => {
|
|
807
|
+
if (object.isMesh) assert.ok(stuff.all.includes(object.material) || object.material === stuff.pair);
|
|
808
|
+
});
|
|
809
|
+
},
|
|
810
|
+
|
|
811
|
+
'a scene load, an editor frame, a keyless frame, the orbit key alone and dispose leave nothing behind'() {
|
|
812
|
+
const ends = {
|
|
813
|
+
load: (world) => physics.reset({ three: world }),
|
|
814
|
+
editor: (world) => renderWorld(world, undefined),
|
|
815
|
+
keyless: (world) => renderWorld(world, runtime(world, { distance: 40 })),
|
|
816
|
+
orbitOnly: (world) => renderWorld(world, runtime(world, { orbit: true })),
|
|
817
|
+
dispose: (world) => disposeWorld(world),
|
|
818
|
+
};
|
|
819
|
+
for (const [name, end] of Object.entries(ends)) {
|
|
820
|
+
const stuff = makeStuff();
|
|
821
|
+
const world = makeWorld(stuff);
|
|
822
|
+
const twin = makeWorld(makeStuff());
|
|
823
|
+
frames(world, runtime(world, KEYED), 4);
|
|
824
|
+
frames(twin, runtime(twin, { distance: 40 }), 4);
|
|
825
|
+
const variants = [...world.shadowFade.variants.values()].map((entry) => entry.variant);
|
|
826
|
+
assert.equal(variants.length, 8, `${name}: one variant per receiving lit material alone on its mesh`);
|
|
827
|
+
end(world);
|
|
828
|
+
if (name !== 'load' && name !== 'dispose') end(twin);
|
|
829
|
+
assert.equal(world.shadowFade, null, `${name}: nothing kept`);
|
|
830
|
+
for (const variant of variants) assert.ok(!world.renderer.properties.has(variant), `${name}: ${variant.name} freed`);
|
|
831
|
+
assert.ok(!world.renderer.info.programs.some((program) => program.cacheKey.endsWith(PROGRAM_KEY_SUFFIX)), `${name}: no program of a variant`);
|
|
832
|
+
world.scene.traverse((object) => {
|
|
833
|
+
if (object.isMesh) assert.ok(stuff.all.includes(object.material) || object.material === stuff.pair, `${name}: ${object.name} has its own`);
|
|
834
|
+
});
|
|
835
|
+
// The originals as a keyless world's drawing leaves them.
|
|
836
|
+
const theirs = twin.scene.children.filter((object) => object.isMesh).flatMap((mesh) => [mesh.material].flat());
|
|
837
|
+
for (const material of stuff.all) {
|
|
838
|
+
const mine = materialState(material);
|
|
839
|
+
const twins = materialState(theirs.find((one) => one.name === material.name));
|
|
840
|
+
assert.deepEqual(mine.own, twins.own, `${name}: ${material.name} has the own properties a keyless world leaves`);
|
|
841
|
+
assert.deepEqual(mine.listeners, twins.listeners, `${name}: ${material.name}: and its listeners`);
|
|
842
|
+
assert.equal(mine.key, twins.key, `${name}: ${material.name}: and its key`);
|
|
843
|
+
}
|
|
844
|
+
// A keyed frame after the load starts afresh.
|
|
845
|
+
if (name !== 'dispose') {
|
|
846
|
+
frames(world, runtime(world, KEYED), 3);
|
|
847
|
+
assert.ok(world.draws.some((draw) => draw.fade), `${name}: the key again fades again`);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
},
|
|
851
|
+
|
|
852
|
+
'variants follow their originals'() {
|
|
853
|
+
const stuff = makeStuff();
|
|
854
|
+
const world = makeWorld(stuff);
|
|
855
|
+
frames(world, runtime(world, KEYED), 3);
|
|
856
|
+
const variantDrawn = (name) => world.draws.find((draw) => !draw.shadow && draw.name === name && draw.fade);
|
|
857
|
+
// A colour change: read through, uploaded.
|
|
858
|
+
stuff.lit.standard.color.set(0x123456);
|
|
859
|
+
frames(world, runtime(world, KEYED), 1);
|
|
860
|
+
const standard = variantDrawn('standard').material;
|
|
861
|
+
assert.equal(standard.color, stuff.lit.standard.color, 'the same colour object');
|
|
862
|
+
assert.deepEqual(world.renderer.properties.get(standard).uniforms.diffuse.value.toArray(), stuff.lit.standard.color.toArray());
|
|
863
|
+
// A new (kit) hook and needsUpdate on the original: the variant made
|
|
864
|
+
// with the old one is dropped and freed, and a new one is made with it.
|
|
865
|
+
const versionWas = standard.version;
|
|
866
|
+
lookPatch(stuff.lit.standard);
|
|
867
|
+
assert.equal(standard.version, versionWas + 1, "the original's version");
|
|
868
|
+
frames(world, runtime(world, KEYED), 1);
|
|
869
|
+
const renewed = variantDrawn('standard').material;
|
|
870
|
+
assert.notEqual(renewed, standard, 'a new variant');
|
|
871
|
+
assert.ok(!world.renderer.properties.has(standard), 'the old one freed');
|
|
872
|
+
assert.ok(
|
|
873
|
+
world.renderer.properties.get(renewed).currentProgram.cacheKey.endsWith(`,${stuff.lit.standard.customProgramCacheKey()}${PROGRAM_KEY_SUFFIX}`) &&
|
|
874
|
+
stuff.lit.standard.customProgramCacheKey().startsWith('look|'),
|
|
875
|
+
'its program has the hook'
|
|
876
|
+
);
|
|
877
|
+
// The transparent double-sided one: three flips the side for each draw;
|
|
878
|
+
// the variant's reads the original's after, and the original's version
|
|
879
|
+
// has three's bumps as it would without the key.
|
|
880
|
+
const glass = variantDrawn('glass').material;
|
|
881
|
+
assert.equal(glass.side, THREE.DoubleSide);
|
|
882
|
+
assert.ok(!Object.prototype.hasOwnProperty.call(stuff.lit.glass, 'side') || stuff.lit.glass.side === THREE.DoubleSide);
|
|
883
|
+
stuff.lit.glass.side = THREE.FrontSide;
|
|
884
|
+
assert.equal(glass.side, THREE.FrontSide);
|
|
885
|
+
// A mesh added mid-scene; an array changed in place (drawn as it is).
|
|
886
|
+
const late = new THREE.MeshLambertMaterial({ name: 'late' });
|
|
887
|
+
const mesh = new THREE.Mesh(stuff.geometry, late);
|
|
888
|
+
mesh.name = 'late';
|
|
889
|
+
mesh.receiveShadow = true;
|
|
890
|
+
world.scene.add(mesh);
|
|
891
|
+
stuff.pair[1] = new THREE.MeshPhongMaterial({ name: 'pair-now-lit' });
|
|
892
|
+
frames(world, runtime(world, KEYED), 1);
|
|
893
|
+
assert.ok(isVariantOf(variantDrawn('late').material, late), 'the new mesh is faded');
|
|
894
|
+
const pair = world.draws.filter((draw) => !draw.shadow && draw.name === 'pair');
|
|
895
|
+
assert.deepEqual(
|
|
896
|
+
pair.map((draw) => [draw.material.name, Boolean(draw.fade)]),
|
|
897
|
+
[
|
|
898
|
+
['pair-lit', false],
|
|
899
|
+
['pair-now-lit', false],
|
|
900
|
+
]
|
|
901
|
+
);
|
|
902
|
+
// Removed: the variant is disposed after it has not been drawn for a while.
|
|
903
|
+
world.scene.remove(mesh);
|
|
904
|
+
const lateVariant = world.shadowFade.variants.get(late).variant;
|
|
905
|
+
frames(world, runtime(world, KEYED), 130);
|
|
906
|
+
assert.ok(!world.shadowFade.variants.has(late), 'swept');
|
|
907
|
+
assert.ok(!world.renderer.properties.has(lateVariant), 'and freed');
|
|
908
|
+
assert.equal(world.shadowFade.count, 0, 'no mesh is noted outside a frame');
|
|
909
|
+
},
|
|
910
|
+
|
|
911
|
+
// Reviews 1 and 2: a deck's own hook that stores the shader on the
|
|
912
|
+
// material (through `this` or a closure), writes a define, or renders
|
|
913
|
+
// another world that shares the material, as three's examples do. Its
|
|
914
|
+
// material is never given a variant, so its hook only ever runs for the
|
|
915
|
+
// material itself, and nothing of it is written by the fade.
|
|
916
|
+
"a material with a deck's own compile hook is drawn as it is, and its hook never runs for a variant"() {
|
|
917
|
+
// The same steps with a keyed and with a keyless first world must end the
|
|
918
|
+
// same for the second, keyless, world (two renderers compiling one
|
|
919
|
+
// material is three's own business; the key must add nothing to it).
|
|
920
|
+
const play = (via, keyed) => {
|
|
921
|
+
const stuff = makeStuff();
|
|
922
|
+
const material = stuff.lit.standard;
|
|
923
|
+
let other = null;
|
|
924
|
+
let calls = 0;
|
|
925
|
+
material.onBeforeCompile = function (shader) {
|
|
926
|
+
calls += 1;
|
|
927
|
+
const self = via === 'this' ? this : material;
|
|
928
|
+
shader.uniforms.myOpacity = { value: 1 };
|
|
929
|
+
self.userData.shader = shader;
|
|
930
|
+
self.defines.MY_DEFINE = '';
|
|
931
|
+
shader.fragmentShader = 'uniform float myOpacity;\n' + shader.fragmentShader;
|
|
932
|
+
// A keyless world sharing the material, drawn from inside the
|
|
933
|
+
// compile (review 2).
|
|
934
|
+
if (via === 'reentrant' && other && !other.drawing) {
|
|
935
|
+
other.drawing = true;
|
|
936
|
+
frames(other, runtime(other), 1);
|
|
937
|
+
other.drawing = false;
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
material.onBeforeRender = function () {
|
|
941
|
+
if (this.userData.shader) this.userData.shader.uniforms.myOpacity.value = this.opacity;
|
|
942
|
+
};
|
|
943
|
+
const a = makeWorld(stuff);
|
|
944
|
+
const b = makeWorld(stuff);
|
|
945
|
+
other = via === 'reentrant' ? b : null;
|
|
946
|
+
const run = runtime(a, keyed ? KEYED : { distance: 40 });
|
|
947
|
+
frames(b, runtime(b), 1);
|
|
948
|
+
const uniform = b.renderer.properties.get(material).uniforms.myOpacity;
|
|
949
|
+
frames(a, run, 3);
|
|
950
|
+
if (keyed) {
|
|
951
|
+
assert.ok(a.draws.some((draw) => draw.fade), `${via}: the keyed world fades the rest`);
|
|
952
|
+
const standard = a.draws.filter((draw) => draw.name === 'standard' && !draw.shadow);
|
|
953
|
+
assert.ok(standard.length > 0 && standard.every((draw) => draw.material === material && !draw.fade), `${via}: drawn as it is, unfaded`);
|
|
954
|
+
assert.equal(a.shadowFade.variants.has(material), false, `${via}: no variant`);
|
|
955
|
+
}
|
|
956
|
+
material.opacity = 0.25;
|
|
957
|
+
frames(b, runtime(b), 1);
|
|
958
|
+
const { prototype, json, ...state } = materialState(material);
|
|
959
|
+
assert.ok(json);
|
|
960
|
+
return {
|
|
961
|
+
calls,
|
|
962
|
+
uniform: uniform.value,
|
|
963
|
+
stashed: material.userData.shader === b.renderer.properties.get(material).uniforms ? 'b' : 'other',
|
|
964
|
+
defines: { ...material.defines },
|
|
965
|
+
state: JSON.parse(JSON.stringify({ ...state, hooks: state.hooks.map(String) }).replace(/[0-9a-f-]{36}/g, 'uuid')),
|
|
966
|
+
proto: prototype === THREE.MeshStandardMaterial.prototype,
|
|
967
|
+
b: rendered(b).programs,
|
|
968
|
+
};
|
|
969
|
+
};
|
|
970
|
+
for (const via of ['this', 'closure', 'reentrant']) {
|
|
971
|
+
assert.deepEqual(play(via, true), play(via, false), `${via}: the key changes nothing for the keyless world or the material`);
|
|
972
|
+
}
|
|
973
|
+
},
|
|
974
|
+
|
|
975
|
+
// Review 1: the shadow pass skipped from the scene's own onBeforeRender.
|
|
976
|
+
'every render nested in a callback after the list of draws sees the originals, whatever the scene does to the shadow pass'() {
|
|
977
|
+
const settings = {
|
|
978
|
+
autoUpdate: (world, on) => (world.renderer.shadowMap.autoUpdate = on),
|
|
979
|
+
enabled: (world, on) => (world.renderer.shadowMap.enabled = on),
|
|
980
|
+
castShadow: (world, on) => (world.scene.children[0].castShadow = on),
|
|
981
|
+
none: () => {},
|
|
982
|
+
};
|
|
983
|
+
for (const [name, set] of Object.entries(settings)) {
|
|
984
|
+
for (const from of ['object', 'scene']) {
|
|
985
|
+
const stuff = makeStuff();
|
|
986
|
+
const world = makeWorld(stuff);
|
|
987
|
+
frames(world, runtime(world, KEYED), 2);
|
|
988
|
+
const pass = world.renderer.shadowMap.render;
|
|
989
|
+
let once = true;
|
|
990
|
+
const nest = () => {
|
|
991
|
+
if (!once) return;
|
|
992
|
+
once = false;
|
|
993
|
+
world.renderer.render(world.scene, world.camera);
|
|
994
|
+
};
|
|
995
|
+
const hook = () => {
|
|
996
|
+
set(world, false);
|
|
997
|
+
nest();
|
|
998
|
+
};
|
|
999
|
+
if (from === 'scene') world.scene.onBeforeRender = hook;
|
|
1000
|
+
else world.scene.getObjectByName('standard').onBeforeRender = hook;
|
|
1001
|
+
world.draws = [];
|
|
1002
|
+
renderWorld(world, runtime(world, KEYED));
|
|
1003
|
+
const nested = world.draws.filter((draw) => draw.depth === 2 && !draw.shadow);
|
|
1004
|
+
const outer = world.draws.filter((draw) => draw.depth === 1 && !draw.shadow);
|
|
1005
|
+
assert.ok(nested.length >= 10, `${name}/${from}: the nested render drew`);
|
|
1006
|
+
assert.ok(
|
|
1007
|
+
nested.every((draw) => stuff.all.includes(draw.material)),
|
|
1008
|
+
`${name}/${from}: nested draws are the originals (${nested.filter((draw) => draw.fade).length} faded)`
|
|
1009
|
+
);
|
|
1010
|
+
if (from === 'object') {
|
|
1011
|
+
assert.ok(outer.some((draw) => draw.fade), `${name}/${from}: the frame itself is faded`);
|
|
1012
|
+
} else {
|
|
1013
|
+
// A scene with an onBeforeRender of its own is not faded at all.
|
|
1014
|
+
assert.ok(outer.every((draw) => stuff.all.includes(draw.material)), `${name}/${from}: the frame is drawn with the originals`);
|
|
1015
|
+
}
|
|
1016
|
+
assert.equal(world.renderer.shadowMap.render, pass, `${name}/${from}: the shadow pass has its own function back`);
|
|
1017
|
+
if (from === 'scene') assert.equal(world.scene.onBeforeRender, hook, 'and the scene its own hook, never touched');
|
|
1018
|
+
world.scene.traverse((object) => {
|
|
1019
|
+
if (object.isMesh) assert.ok(stuff.all.includes(object.material) || object.material === stuff.pair);
|
|
1020
|
+
});
|
|
1021
|
+
set(world, true);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
const world = makeWorld(makeStuff());
|
|
1025
|
+
frames(world, runtime(world, KEYED), 3);
|
|
1026
|
+
assert.ok(!Object.prototype.hasOwnProperty.call(world.scene, 'onBeforeRender'), 'the scene is never given a hook');
|
|
1027
|
+
assert.ok(Object.prototype.hasOwnProperty.call(world.renderer.shadowMap, 'render'), "three's own pass function, as it was");
|
|
1028
|
+
},
|
|
1029
|
+
|
|
1030
|
+
// Review 2: a deck that replaces a hook mid-frame with one that calls the
|
|
1031
|
+
// one it found.
|
|
1032
|
+
'a hook a deck replaces mid-frame, chaining to the one it found, stays safe after the frame'() {
|
|
1033
|
+
for (const which of ['scene', 'pass']) {
|
|
1034
|
+
const stuff = makeStuff();
|
|
1035
|
+
const world = makeWorld(stuff);
|
|
1036
|
+
frames(world, runtime(world, KEYED), 2);
|
|
1037
|
+
const own = world.renderer.shadowMap.render;
|
|
1038
|
+
let replaced = null;
|
|
1039
|
+
let captured = null;
|
|
1040
|
+
world.scene.getObjectByName('standard').onBeforeRender = () => {
|
|
1041
|
+
if (replaced) return;
|
|
1042
|
+
const target = which === 'scene' ? world.scene : world.renderer.shadowMap;
|
|
1043
|
+
const key = which === 'scene' ? 'onBeforeRender' : 'render';
|
|
1044
|
+
captured = target[key];
|
|
1045
|
+
replaced = function (...args) {
|
|
1046
|
+
return captured.apply(this, args);
|
|
1047
|
+
};
|
|
1048
|
+
target[key] = replaced;
|
|
1049
|
+
};
|
|
1050
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1051
|
+
delete world.scene.getObjectByName('standard').onBeforeRender;
|
|
1052
|
+
if (which === 'pass') assert.notEqual(captured, own, 'the deck caught the fade\'s hook');
|
|
1053
|
+
// Keyless frames, then faded ones, then keyless again: no throw, no
|
|
1054
|
+
// recursion, the keyless frames drawn with the originals.
|
|
1055
|
+
for (const run of [runtime(world, { distance: 40 }), runtime(world, KEYED), runtime(world, KEYED), runtime(world, KEYED), runtime(world)]) {
|
|
1056
|
+
world.draws = [];
|
|
1057
|
+
renderWorld(world, run);
|
|
1058
|
+
if (run.cameraControl !== 'orbit') assert.ok(world.draws.every((draw) => !draw.fade), `${which}: a keyless frame is not faded`);
|
|
1059
|
+
world.scene.traverse((object) => {
|
|
1060
|
+
if (object.isMesh) assert.ok(stuff.all.includes(object.material) || object.material === stuff.pair, `${which}: ${object.name} has its own`);
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
frames(world, runtime(world, KEYED), 2);
|
|
1064
|
+
// A scene with a hook of its own is not faded; a replaced pass is.
|
|
1065
|
+
assert.equal(world.draws.some((draw) => draw.fade), which === 'pass', `${which}: fades only without a scene hook of the deck's`);
|
|
1066
|
+
if (which === 'pass') {
|
|
1067
|
+
// What the deck kept holds nothing of the fade now.
|
|
1068
|
+
frames(world, runtime(world), 1);
|
|
1069
|
+
assert.equal(world.shadowFade, null);
|
|
1070
|
+
world.draws = [];
|
|
1071
|
+
world.renderer.render(world.scene, world.camera);
|
|
1072
|
+
assert.ok(world.draws.length > 0 && world.draws.every((draw) => !draw.fade));
|
|
1073
|
+
}
|
|
1074
|
+
if (which === 'scene') assert.equal(world.scene.onBeforeRender, replaced);
|
|
1075
|
+
}
|
|
1076
|
+
},
|
|
1077
|
+
|
|
1078
|
+
// Reviews 1, 3 and 4: deck code that runs inside the frame and reaches a
|
|
1079
|
+
// variant (a callback installed mid-frame on a later mesh, handed the
|
|
1080
|
+
// material three listed; a deck LOD's `update`, run while the draws are
|
|
1081
|
+
// listed) writes through it. Every write lands on the original as it
|
|
1082
|
+
// would without the key: compared with the same steps in a keyless world.
|
|
1083
|
+
'writes through a variant land on the material it fades, as without the key'() {
|
|
1084
|
+
const play = (keyed, via) => {
|
|
1085
|
+
const stuff = makeStuff();
|
|
1086
|
+
const a = makeWorld(stuff);
|
|
1087
|
+
const b = makeWorld(stuff);
|
|
1088
|
+
const material = stuff.lit.standard;
|
|
1089
|
+
const run = runtime(a, keyed ? KEYED : { distance: 40 });
|
|
1090
|
+
let lod = null;
|
|
1091
|
+
if (via === 'lod') {
|
|
1092
|
+
const mesh = a.scene.getObjectByName('standard');
|
|
1093
|
+
a.scene.remove(mesh);
|
|
1094
|
+
lod = new THREE.LOD();
|
|
1095
|
+
lod.addLevel(mesh, 0);
|
|
1096
|
+
a.scene.add(lod);
|
|
1097
|
+
}
|
|
1098
|
+
frames(a, run, 2);
|
|
1099
|
+
const write = (target) => {
|
|
1100
|
+
target.roughness = 0.17;
|
|
1101
|
+
target.map = new THREE.Texture();
|
|
1102
|
+
Object.defineProperty(target, 'metalness', { value: 0.23, writable: true, configurable: true, enumerable: false });
|
|
1103
|
+
target.brandNew = 'kept';
|
|
1104
|
+
target.needsUpdate = true;
|
|
1105
|
+
};
|
|
1106
|
+
let once = true;
|
|
1107
|
+
if (via === 'lod') {
|
|
1108
|
+
lod.update = function (camera) {
|
|
1109
|
+
THREE.LOD.prototype.update.call(this, camera);
|
|
1110
|
+
if (!once) return;
|
|
1111
|
+
once = false;
|
|
1112
|
+
write(this.levels[0].object.material);
|
|
1113
|
+
};
|
|
1114
|
+
} else {
|
|
1115
|
+
a.scene.getObjectByName('basic').onBeforeRender = () => {
|
|
1116
|
+
if (!once) return;
|
|
1117
|
+
once = false;
|
|
1118
|
+
a.scene.getObjectByName('standard').onBeforeRender = (renderer, scene, camera, geometry, drawn) => {
|
|
1119
|
+
delete a.scene.getObjectByName('standard').onBeforeRender;
|
|
1120
|
+
write(drawn);
|
|
1121
|
+
};
|
|
1122
|
+
};
|
|
1123
|
+
}
|
|
1124
|
+
a.draws = [];
|
|
1125
|
+
renderWorld(a, run);
|
|
1126
|
+
const drawnWith = a.draws.find((draw) => draw.name === 'standard' && !draw.shadow)?.material;
|
|
1127
|
+
if (keyed) assert.ok(isVariantOf(drawnWith, material), `${via}: written through the variant`);
|
|
1128
|
+
frames(a, run, 2);
|
|
1129
|
+
frames(b, runtime(b), 1);
|
|
1130
|
+
const uniforms = b.renderer.properties.get(material).uniforms;
|
|
1131
|
+
return {
|
|
1132
|
+
roughness: material.roughness,
|
|
1133
|
+
metalness: Object.getOwnPropertyDescriptor(material, 'metalness'),
|
|
1134
|
+
map: Boolean(material.map),
|
|
1135
|
+
brandNew: material.brandNew,
|
|
1136
|
+
own: Object.keys(material).sort(),
|
|
1137
|
+
keyless: [uniforms.roughness.value, uniforms.metalness.value, Boolean(uniforms.map.value)],
|
|
1138
|
+
// The variant's own values (not accessors forwarding to the original).
|
|
1139
|
+
variantOwn: keyed && a.shadowFade.variants.get(material) ? ownValues(a.shadowFade.variants.get(material).variant) : null,
|
|
1140
|
+
};
|
|
1141
|
+
};
|
|
1142
|
+
for (const via of ['callback', 'lod']) {
|
|
1143
|
+
const keyed = play(true, via);
|
|
1144
|
+
const keyless = play(false, via);
|
|
1145
|
+
assert.deepEqual(keyed.variantOwn, ['id'], `${via}: nothing left on the variant`);
|
|
1146
|
+
delete keyed.variantOwn;
|
|
1147
|
+
delete keyless.variantOwn;
|
|
1148
|
+
assert.deepEqual(keyed, keyless, `${via}: the material and the keyless world as without the key`);
|
|
1149
|
+
assert.deepEqual(keyed.keyless, [0.17, 0.23, true]);
|
|
1150
|
+
}
|
|
1151
|
+
},
|
|
1152
|
+
|
|
1153
|
+
// Review 5: through a variant a deck LOD's `update` reaches, the events
|
|
1154
|
+
// and `dispose` of the original, a forwarder redefined, and properties the
|
|
1155
|
+
// original has that are not enumerable or are symbols. Each compared with
|
|
1156
|
+
// the same steps in a keyless world sharing the material.
|
|
1157
|
+
'through a variant, events, dispose and every own property are the original\'s'() {
|
|
1158
|
+
const HIDDEN = Symbol('hidden');
|
|
1159
|
+
const cases = {
|
|
1160
|
+
dispose: {
|
|
1161
|
+
prepare: (material, log) => material.addEventListener('dispose', () => {
|
|
1162
|
+
log.push('disposed');
|
|
1163
|
+
material.roughness = 0.17;
|
|
1164
|
+
}),
|
|
1165
|
+
write: (seen) => seen.dispose(),
|
|
1166
|
+
},
|
|
1167
|
+
listener: {
|
|
1168
|
+
prepare: () => {},
|
|
1169
|
+
write: (seen, log) => {
|
|
1170
|
+
const listener = () => log.push('custom');
|
|
1171
|
+
seen.addEventListener('custom', listener);
|
|
1172
|
+
log.push(seen.hasEventListener('custom', listener));
|
|
1173
|
+
seen.dispatchEvent({ type: 'custom' });
|
|
1174
|
+
},
|
|
1175
|
+
after: (material) => material.dispatchEvent({ type: 'custom' }),
|
|
1176
|
+
},
|
|
1177
|
+
redefined: {
|
|
1178
|
+
prepare: (material) => {
|
|
1179
|
+
material.extra = 1;
|
|
1180
|
+
},
|
|
1181
|
+
write: (seen) => {
|
|
1182
|
+
Object.defineProperty(seen, 'extra', { value: 0.23, writable: true, configurable: true, enumerable: true });
|
|
1183
|
+
seen.roughness = seen.extra;
|
|
1184
|
+
},
|
|
1185
|
+
twice: true,
|
|
1186
|
+
},
|
|
1187
|
+
hidden: {
|
|
1188
|
+
prepare: (material) => {
|
|
1189
|
+
Object.defineProperty(material, 'hiddenRoughness', { value: 0.3, writable: true, configurable: true, enumerable: false });
|
|
1190
|
+
material[HIDDEN] = 2;
|
|
1191
|
+
},
|
|
1192
|
+
write: (seen) => {
|
|
1193
|
+
seen.roughness = seen.hiddenRoughness;
|
|
1194
|
+
seen[HIDDEN] += 1;
|
|
1195
|
+
},
|
|
1196
|
+
},
|
|
1197
|
+
};
|
|
1198
|
+
const play = (keyed, { prepare, write, after, twice }) => {
|
|
1199
|
+
const stuff = makeStuff();
|
|
1200
|
+
const a = makeWorld(stuff);
|
|
1201
|
+
const b = makeWorld(stuff);
|
|
1202
|
+
const material = stuff.lit.standard;
|
|
1203
|
+
const log = [];
|
|
1204
|
+
prepare(material, log);
|
|
1205
|
+
const mesh = a.scene.getObjectByName('standard');
|
|
1206
|
+
a.scene.remove(mesh);
|
|
1207
|
+
const lod = new THREE.LOD();
|
|
1208
|
+
lod.addLevel(mesh, 0);
|
|
1209
|
+
a.scene.add(lod);
|
|
1210
|
+
const run = runtime(a, keyed ? KEYED : { distance: 40 });
|
|
1211
|
+
frames(a, run, 2);
|
|
1212
|
+
frames(b, runtime(b), 1);
|
|
1213
|
+
let writes = twice ? 2 : 1;
|
|
1214
|
+
let seen = null;
|
|
1215
|
+
lod.update = function (camera) {
|
|
1216
|
+
THREE.LOD.prototype.update.call(this, camera);
|
|
1217
|
+
if (writes === 0) return;
|
|
1218
|
+
writes -= 1;
|
|
1219
|
+
seen = this.levels[0].object.material;
|
|
1220
|
+
write(seen, log);
|
|
1221
|
+
};
|
|
1222
|
+
frames(a, run, 3);
|
|
1223
|
+
after?.(material, log);
|
|
1224
|
+
if (keyed) assert.ok(isVariantOf(seen, material), 'reached through the variant');
|
|
1225
|
+
frames(b, runtime(b), 1);
|
|
1226
|
+
const uniforms = b.renderer.properties.get(material).uniforms;
|
|
1227
|
+
return {
|
|
1228
|
+
log,
|
|
1229
|
+
roughness: material.roughness,
|
|
1230
|
+
extra: Object.getOwnPropertyDescriptor(material, 'extra'),
|
|
1231
|
+
hidden: [Object.getOwnPropertyDescriptor(material, 'hiddenRoughness'), material[HIDDEN]],
|
|
1232
|
+
listeners: Object.fromEntries(Object.entries(material._listeners ?? {}).map(([type, list]) => [type, list.length])),
|
|
1233
|
+
keyless: uniforms.roughness.value,
|
|
1234
|
+
variants: keyed ? [...a.shadowFade.variants.values()].every(({ variant }) => ownValues(variant).join() === 'id') : true,
|
|
1235
|
+
};
|
|
1236
|
+
};
|
|
1237
|
+
for (const [name, steps] of Object.entries(cases)) {
|
|
1238
|
+
const keyed = play(true, steps);
|
|
1239
|
+
assert.equal(keyed.variants, true, `${name}: nothing left on any variant`);
|
|
1240
|
+
assert.deepEqual(keyed, play(false, steps), `${name}: the material, its events and the keyless world as without the key`);
|
|
1241
|
+
}
|
|
1242
|
+
},
|
|
1243
|
+
|
|
1244
|
+
// Review 5: the variant's program goes when its original is disposed, by
|
|
1245
|
+
// the deck or through the variant, and the renderer's listener for it
|
|
1246
|
+
// never fires a listener of the original's twice or not at all.
|
|
1247
|
+
"disposing the original lets go of the variant's program"() {
|
|
1248
|
+
const stuff = makeStuff();
|
|
1249
|
+
const world = makeWorld(stuff);
|
|
1250
|
+
const material = stuff.lit.standard;
|
|
1251
|
+
let disposed = 0;
|
|
1252
|
+
material.addEventListener('dispose', () => (disposed += 1));
|
|
1253
|
+
frames(world, runtime(world, KEYED), 3);
|
|
1254
|
+
const variant = world.shadowFade.variants.get(material).variant;
|
|
1255
|
+
// (Its program is shared with the other variants of the same key, so
|
|
1256
|
+
// what goes is the renderer's hold on it for this variant.)
|
|
1257
|
+
const held = world.renderer.properties.get(variant);
|
|
1258
|
+
assert.ok(held.currentProgram, 'the variant has a program');
|
|
1259
|
+
const uses = held.currentProgram.usedTimes;
|
|
1260
|
+
material.dispose();
|
|
1261
|
+
assert.equal(disposed, 1);
|
|
1262
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1263
|
+
assert.notEqual(world.renderer.properties.get(variant), held, "the renderer let go of the variant's program and took it again");
|
|
1264
|
+
assert.equal(world.renderer.properties.get(variant).currentProgram.usedTimes, uses, 'held once, not twice');
|
|
1265
|
+
// Through the variant.
|
|
1266
|
+
variant.dispose();
|
|
1267
|
+
assert.equal(disposed, 2, "the original's listener, once");
|
|
1268
|
+
assert.equal(world.renderer.properties.has(variant), false, "the variant's program let go of at once");
|
|
1269
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1270
|
+
assert.equal(world.renderer.properties.get(variant).currentProgram.usedTimes, uses);
|
|
1271
|
+
assert.equal(disposed, 2, 'and not again');
|
|
1272
|
+
// The original's dispose listeners: the deck's and one per renderer that
|
|
1273
|
+
// drew it, as without the key.
|
|
1274
|
+
assert.equal(material._listeners.dispose.length, 2);
|
|
1275
|
+
},
|
|
1276
|
+
|
|
1277
|
+
// Review 6: the renderer's dispose listener is known by the renderer, so
|
|
1278
|
+
// a deck that asks a variant its program key and then adds a dispose
|
|
1279
|
+
// listener of its own, before the variant's first compile, is not taken
|
|
1280
|
+
// for the renderer.
|
|
1281
|
+
"a deck's dispose listener added through a variant is never taken for the renderer's"() {
|
|
1282
|
+
const play = (keyed) => {
|
|
1283
|
+
const stuff = makeStuff();
|
|
1284
|
+
const a = makeWorld(stuff);
|
|
1285
|
+
const b = makeWorld(stuff);
|
|
1286
|
+
const mesh = a.scene.getObjectByName('standard');
|
|
1287
|
+
const material = mesh.material;
|
|
1288
|
+
a.scene.remove(mesh);
|
|
1289
|
+
const lod = new THREE.LOD();
|
|
1290
|
+
lod.addLevel(mesh, 0);
|
|
1291
|
+
a.scene.add(lod);
|
|
1292
|
+
const run = runtime(a, keyed ? KEYED : { distance: 40 });
|
|
1293
|
+
frames(a, run, 1);
|
|
1294
|
+
let calls = 0;
|
|
1295
|
+
let once = true;
|
|
1296
|
+
lod.update = function (camera) {
|
|
1297
|
+
THREE.LOD.prototype.update.call(this, camera);
|
|
1298
|
+
if (!once) return;
|
|
1299
|
+
once = false;
|
|
1300
|
+
const seen = mesh.material;
|
|
1301
|
+
seen.customProgramCacheKey();
|
|
1302
|
+
seen.addEventListener('dispose', () => {
|
|
1303
|
+
calls += 1;
|
|
1304
|
+
material.roughness = 0.123;
|
|
1305
|
+
});
|
|
1306
|
+
};
|
|
1307
|
+
frames(a, run, 1);
|
|
1308
|
+
const variant = a.shadowFade?.variants.get(material)?.variant;
|
|
1309
|
+
if (keyed) assert.ok(variant && a.renderer.properties.has(variant), 'the variant was drawn');
|
|
1310
|
+
// The key goes: every variant let go of, nothing disposed.
|
|
1311
|
+
frames(a, runtime(a), 1);
|
|
1312
|
+
frames(b, runtime(b), 1);
|
|
1313
|
+
return {
|
|
1314
|
+
calls,
|
|
1315
|
+
keyless: b.renderer.properties.get(material).uniforms.roughness.value,
|
|
1316
|
+
retained: Boolean(variant) && a.renderer.properties.has(variant),
|
|
1317
|
+
faded: a.renderer.info.programs.filter((program) => program.cacheKey.includes(PROGRAM_KEY_SUFFIX)).length,
|
|
1318
|
+
listeners: material._listeners.dispose.length,
|
|
1319
|
+
};
|
|
1320
|
+
};
|
|
1321
|
+
assert.deepEqual(play(true), play(false));
|
|
1322
|
+
assert.deepEqual(play(true), { calls: 0, keyless: 1, retained: false, faded: 0, listeners: 3 });
|
|
1323
|
+
},
|
|
1324
|
+
|
|
1325
|
+
// Review 6: what is absent, and the class's own, read through a variant
|
|
1326
|
+
// or its shared prototype, as on the original.
|
|
1327
|
+
'reads of absent keys and of the class through a variant are the original\'s'() {
|
|
1328
|
+
const stuff = makeStuff();
|
|
1329
|
+
const world = makeWorld(stuff);
|
|
1330
|
+
const material = stuff.lit.physical;
|
|
1331
|
+
frames(world, runtime(world, KEYED), 3);
|
|
1332
|
+
const variant = world.shadowFade.variants.get(material).variant;
|
|
1333
|
+
const shared = Object.getPrototypeOf(variant);
|
|
1334
|
+
const symbol = Symbol('absent');
|
|
1335
|
+
for (const read of [(o) => o.constructor, (o) => o.missingProperty, (o) => o[symbol], (o) => o.isShaderMaterial, (o) => o.clearcoat, (o) => o.toJSON]) {
|
|
1336
|
+
assert.equal(read(variant), read(material), `through the variant: ${read}`);
|
|
1337
|
+
assert.doesNotThrow(() => read(shared), `the shared prototype: ${read}`);
|
|
1338
|
+
}
|
|
1339
|
+
assert.equal(shared.constructor, material.constructor);
|
|
1340
|
+
assert.equal(shared.missingProperty, undefined);
|
|
1341
|
+
assert.equal(Object.getPrototypeOf(material).constructor, material.constructor);
|
|
1342
|
+
// And again after the reads gave those keys forwarders.
|
|
1343
|
+
assert.equal(variant.constructor, material.constructor);
|
|
1344
|
+
assert.equal(variant.missingProperty, undefined);
|
|
1345
|
+
material.missingProperty = 'now';
|
|
1346
|
+
assert.equal(variant.missingProperty, 'now');
|
|
1347
|
+
assert.equal(shared.missingProperty, undefined, 'the shared prototype has none of an original');
|
|
1348
|
+
},
|
|
1349
|
+
|
|
1350
|
+
// Review 7: a keyed deck drawing another scene with the world's renderer,
|
|
1351
|
+
// which draws the original itself: a dispose that a redraw has answered
|
|
1352
|
+
// before the next faded frame still lets the variant's program go, so
|
|
1353
|
+
// dispose-and-change cycles do not pile programs up.
|
|
1354
|
+
"a dispose a redraw has already answered still lets the variant's program go"() {
|
|
1355
|
+
const stuff = makeStuff();
|
|
1356
|
+
const world = makeWorld(stuff);
|
|
1357
|
+
const material = stuff.lit.standard;
|
|
1358
|
+
const other = new THREE.Scene();
|
|
1359
|
+
other.add(new THREE.Mesh(stuff.geometry, material));
|
|
1360
|
+
const camera = new THREE.PerspectiveCamera();
|
|
1361
|
+
frames(world, runtime(world, KEYED), 3);
|
|
1362
|
+
world.renderer.render(other, camera);
|
|
1363
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1364
|
+
const faded = () => world.renderer.info.programs.filter((program) => program.cacheKey.includes(PROGRAM_KEY_SUFFIX)).length;
|
|
1365
|
+
const before = faded();
|
|
1366
|
+
const variant = world.shadowFade.variants.get(material).variant;
|
|
1367
|
+
for (let cycle = 0; cycle < 6; cycle++) {
|
|
1368
|
+
material.defines[`CYCLE_${cycle}`] = '';
|
|
1369
|
+
material.needsUpdate = true;
|
|
1370
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1371
|
+
material.dispose();
|
|
1372
|
+
// The redraw puts the renderer's listener back in the same array.
|
|
1373
|
+
world.renderer.render(other, camera);
|
|
1374
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1375
|
+
delete material.defines[`CYCLE_${cycle}`];
|
|
1376
|
+
}
|
|
1377
|
+
assert.equal(world.shadowFade.variants.get(material).variant, variant, 'the same variant');
|
|
1378
|
+
assert.ok(faded() <= before + 1, `faded programs stay bounded (${before} -> ${faded()})`);
|
|
1379
|
+
},
|
|
1380
|
+
|
|
1381
|
+
// Review 8: a mesh with an array of materials, and a material whose class
|
|
1382
|
+
// is not one of three's own, are drawn as they are; a write into the
|
|
1383
|
+
// mesh's array, and a subclass constructor, touch nothing the keyless
|
|
1384
|
+
// world sees.
|
|
1385
|
+
'only three\'s own lit materials, one to a mesh, are faded'() {
|
|
1386
|
+
// An LOD update writing into the mesh's own array.
|
|
1387
|
+
const arrays = (keyed) => {
|
|
1388
|
+
const stuff = makeStuff();
|
|
1389
|
+
const a = makeWorld(stuff);
|
|
1390
|
+
const b = makeWorld(stuff);
|
|
1391
|
+
const mesh = a.scene.getObjectByName('pair');
|
|
1392
|
+
a.scene.remove(mesh);
|
|
1393
|
+
const lod = new THREE.LOD();
|
|
1394
|
+
lod.addLevel(mesh, 0);
|
|
1395
|
+
a.scene.add(lod);
|
|
1396
|
+
const run = runtime(a, keyed ? KEYED : { distance: 40 });
|
|
1397
|
+
frames(a, run, 2);
|
|
1398
|
+
const replacement = new THREE.MeshStandardMaterial({ name: 'replacement' });
|
|
1399
|
+
let once = true;
|
|
1400
|
+
lod.update = function (camera) {
|
|
1401
|
+
THREE.LOD.prototype.update.call(this, camera);
|
|
1402
|
+
if (!once) return;
|
|
1403
|
+
once = false;
|
|
1404
|
+
this.levels[0].object.material[0] = replacement;
|
|
1405
|
+
};
|
|
1406
|
+
frames(a, run, 2);
|
|
1407
|
+
if (keyed) assert.ok(a.draws.filter((draw) => draw.name === 'pair' && !draw.shadow).every((draw) => !draw.fade), 'the array mesh is not faded');
|
|
1408
|
+
frames(b, runtime(b), 1);
|
|
1409
|
+
return [stuff.pair.map((material) => material.name), b.draws.filter((draw) => draw.name === 'pair' && !draw.shadow).map((draw) => draw.material.name).sort()];
|
|
1410
|
+
};
|
|
1411
|
+
assert.deepEqual(arrays(true), arrays(false));
|
|
1412
|
+
assert.deepEqual(arrays(true)[1], ['pair-unlit', 'replacement']);
|
|
1413
|
+
// A subclass whose constructor writes elsewhere (here: a shared
|
|
1414
|
+
// material's roughness) is never constructed again.
|
|
1415
|
+
const subclass = (keyed) => {
|
|
1416
|
+
const stuff = makeStuff();
|
|
1417
|
+
const shared = stuff.lit.lambert;
|
|
1418
|
+
class Deck extends THREE.MeshStandardMaterial {
|
|
1419
|
+
constructor(parameters) {
|
|
1420
|
+
super(parameters);
|
|
1421
|
+
shared.emissiveIntensity = (shared.emissiveIntensity ?? 1) + 1;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
const deck = new Deck({ name: 'deck' });
|
|
1425
|
+
const a = makeWorld(stuff);
|
|
1426
|
+
const b = makeWorld(stuff);
|
|
1427
|
+
const mesh = new THREE.Mesh(stuff.geometry, deck);
|
|
1428
|
+
mesh.name = 'deck';
|
|
1429
|
+
mesh.receiveShadow = true;
|
|
1430
|
+
a.scene.add(mesh);
|
|
1431
|
+
frames(a, runtime(a, keyed ? KEYED : { distance: 40 }), 4);
|
|
1432
|
+
if (keyed) {
|
|
1433
|
+
assert.ok(a.draws.some((draw) => draw.fade), 'the rest fade');
|
|
1434
|
+
assert.ok(a.draws.filter((draw) => draw.name === 'deck' && !draw.shadow).every((draw) => draw.material === deck && !draw.fade), 'drawn as it is');
|
|
1435
|
+
}
|
|
1436
|
+
frames(b, runtime(b), 1);
|
|
1437
|
+
return [shared.emissiveIntensity, b.renderer.properties.get(shared).uniforms.emissive ? shared.emissiveIntensity : null];
|
|
1438
|
+
};
|
|
1439
|
+
assert.deepEqual(subclass(true), subclass(false));
|
|
1440
|
+
},
|
|
1441
|
+
|
|
1442
|
+
// Review 9: a hook, a material's listeners, or a mesh's material behind a
|
|
1443
|
+
// deck's accessor: never faded, and the getter called exactly as often as
|
|
1444
|
+
// without the key, so whatever it does to shared state is the same.
|
|
1445
|
+
'a hook or a mesh property behind an accessor is never faded, nor read by the fade'() {
|
|
1446
|
+
const cases = {
|
|
1447
|
+
onBeforeCompile: (material) => material,
|
|
1448
|
+
customProgramCacheKey: (material) => material,
|
|
1449
|
+
onBeforeRender: (material) => material,
|
|
1450
|
+
_listeners: (material) => material,
|
|
1451
|
+
material: (material, mesh) => mesh,
|
|
1452
|
+
receiveShadow: (material, mesh) => mesh,
|
|
1453
|
+
};
|
|
1454
|
+
const play = (keyed, key, owner) => {
|
|
1455
|
+
const stuff = makeStuff();
|
|
1456
|
+
const material = stuff.lit.standard;
|
|
1457
|
+
const a = makeWorld(stuff);
|
|
1458
|
+
const b = makeWorld(stuff);
|
|
1459
|
+
const mesh = a.scene.getObjectByName('standard');
|
|
1460
|
+
const target = owner(material, mesh);
|
|
1461
|
+
let value = target[key] ?? (key === '_listeners' ? {} : undefined);
|
|
1462
|
+
let reads = 0;
|
|
1463
|
+
Object.defineProperty(target, key, {
|
|
1464
|
+
configurable: true,
|
|
1465
|
+
get() {
|
|
1466
|
+
reads += 1;
|
|
1467
|
+
material.roughness = Math.min(1, reads / 100);
|
|
1468
|
+
return value;
|
|
1469
|
+
},
|
|
1470
|
+
set(next) {
|
|
1471
|
+
value = next;
|
|
1472
|
+
},
|
|
1473
|
+
});
|
|
1474
|
+
for (let i = 0; i < 4; i++) frames(a, runtime(a, keyed ? KEYED : { distance: 40 }), 1);
|
|
1475
|
+
if (keyed) {
|
|
1476
|
+
assert.ok(a.draws.some((draw) => draw.fade), `${key}: the rest fade`);
|
|
1477
|
+
assert.ok(a.draws.filter((draw) => draw.name === 'standard' && !draw.shadow).every((draw) => !draw.fade), `${key}: drawn as it is`);
|
|
1478
|
+
}
|
|
1479
|
+
frames(b, runtime(b), 1);
|
|
1480
|
+
return { reads, roughness: material.roughness, keyless: b.renderer.properties.get(material).uniforms.roughness.value };
|
|
1481
|
+
};
|
|
1482
|
+
for (const [key, owner] of Object.entries(cases)) {
|
|
1483
|
+
assert.deepEqual(play(true, key, owner), play(false, key, owner), `${key}: the getter called as without the key`);
|
|
1484
|
+
}
|
|
1485
|
+
},
|
|
1486
|
+
|
|
1487
|
+
// Review 10: a mesh whose material cannot be written (read-only on the
|
|
1488
|
+
// mesh, or read-only on its prototype) is drawn as it is, as without the
|
|
1489
|
+
// key.
|
|
1490
|
+
'a mesh whose material cannot be written is drawn unfaded'() {
|
|
1491
|
+
for (const how of ['read-only', 'inherited']) {
|
|
1492
|
+
const play = (keyed) => {
|
|
1493
|
+
const stuff = makeStuff();
|
|
1494
|
+
const world = makeWorld(stuff);
|
|
1495
|
+
const mesh = world.scene.getObjectByName('standard');
|
|
1496
|
+
if (how === 'read-only') Object.defineProperty(mesh, 'material', { value: mesh.material, writable: false, configurable: true, enumerable: true });
|
|
1497
|
+
else {
|
|
1498
|
+
const proto = Object.create(Object.getPrototypeOf(mesh));
|
|
1499
|
+
Object.defineProperty(proto, 'material', { value: mesh.material, writable: false });
|
|
1500
|
+
delete mesh.material;
|
|
1501
|
+
Object.setPrototypeOf(mesh, proto);
|
|
1502
|
+
}
|
|
1503
|
+
assert.doesNotThrow(() => frames(world, runtime(world, keyed ? KEYED : { distance: 40 }), 4), `${how}: drawn`);
|
|
1504
|
+
if (keyed) assert.ok(world.draws.some((draw) => draw.fade), `${how}: the rest fade`);
|
|
1505
|
+
return world.draws.filter((draw) => draw.name === 'standard' && !draw.shadow).map((draw) => [draw.material === stuff.lit.standard, Boolean(draw.fade)]);
|
|
1506
|
+
};
|
|
1507
|
+
assert.deepEqual(play(true), play(false), `${how}: drawn with its own material, unfaded`);
|
|
1508
|
+
}
|
|
1509
|
+
},
|
|
1510
|
+
|
|
1511
|
+
// Review 11: a shadow pass a deck has made read-only draws the frame
|
|
1512
|
+
// unfaded, without a throw, and no variant is left on any mesh.
|
|
1513
|
+
'a shadow pass that cannot take the hook draws the frame unfaded'() {
|
|
1514
|
+
const play = (keyed) => {
|
|
1515
|
+
const stuff = makeStuff();
|
|
1516
|
+
const world = makeWorld(stuff);
|
|
1517
|
+
const run = runtime(world, keyed ? KEYED : { distance: 40 });
|
|
1518
|
+
frames(world, run, 2);
|
|
1519
|
+
const pass = world.renderer.shadowMap;
|
|
1520
|
+
const render = pass.render;
|
|
1521
|
+
Object.defineProperty(pass, 'render', { value: render, writable: false, configurable: true });
|
|
1522
|
+
assert.doesNotThrow(() => frames(world, run, 3));
|
|
1523
|
+
const drawn = world.draws.filter((draw) => !draw.shadow).map((draw) => [draw.name, draw.material.name, Boolean(draw.fade)]);
|
|
1524
|
+
world.scene.traverse((object) => {
|
|
1525
|
+
if (object.isMesh) assert.ok(stuff.all.includes(object.material) || object.material === stuff.pair, 'no variant left on a mesh');
|
|
1526
|
+
});
|
|
1527
|
+
assert.equal(pass.render, render);
|
|
1528
|
+
Object.defineProperty(pass, 'render', { value: render, writable: true, configurable: true });
|
|
1529
|
+
frames(world, run, 2);
|
|
1530
|
+
return { drawn, fadesAgain: world.draws.some((draw) => draw.fade) };
|
|
1531
|
+
};
|
|
1532
|
+
const keyed = play(true);
|
|
1533
|
+
const keyless = play(false);
|
|
1534
|
+
assert.deepEqual(keyed.drawn, keyless.drawn, 'unfaded while the pass is read-only');
|
|
1535
|
+
assert.equal(keyed.fadesAgain, true, 'and faded again once it is not');
|
|
1536
|
+
},
|
|
1537
|
+
|
|
1538
|
+
// A deck locks the shadow pass during a keyed frame, but leaves it
|
|
1539
|
+
// configurable: the frame's end restores its original function, and
|
|
1540
|
+
// removing the orbit key leaves frames as without the key.
|
|
1541
|
+
'a shadow pass made read-only mid-frame gets its original function back'() {
|
|
1542
|
+
const play = (keyed) => {
|
|
1543
|
+
const stuff = makeStuff();
|
|
1544
|
+
const world = makeWorld(stuff);
|
|
1545
|
+
const pass = world.renderer.shadowMap;
|
|
1546
|
+
const render = pass.render;
|
|
1547
|
+
const run = runtime(world, keyed ? KEYED : { distance: 40 });
|
|
1548
|
+
frames(world, run, 2);
|
|
1549
|
+
let once = true;
|
|
1550
|
+
world.scene.getObjectByName('basic').onBeforeRender = () => {
|
|
1551
|
+
if (!once) return;
|
|
1552
|
+
once = false;
|
|
1553
|
+
if (keyed) assert.notEqual(pass.render, render, 'the fade hook is installed during the frame');
|
|
1554
|
+
Object.defineProperty(pass, 'render', { writable: false, configurable: true });
|
|
1555
|
+
};
|
|
1556
|
+
assert.doesNotThrow(() => frames(world, run, 1));
|
|
1557
|
+
assert.equal(once, false, 'the deck locked the pass during the render');
|
|
1558
|
+
assert.equal(pass.render, render, 'the frame end restores exactly the original function');
|
|
1559
|
+
assert.equal(Object.getOwnPropertyDescriptor(pass, 'render').writable, false);
|
|
1560
|
+
assert.equal(Object.getOwnPropertyDescriptor(pass, 'render').configurable, true);
|
|
1561
|
+
if (keyed) assert.ok(world.draws.some((draw) => draw.fade), 'the keyed frame faded');
|
|
1562
|
+
frames(world, runtime(world, { distance: 40 }), 3);
|
|
1563
|
+
assert.equal(world.shadowFade ?? null, null, 'the orbit key is removed');
|
|
1564
|
+
assert.equal(pass.render, render, 'keyless frames retain the original function');
|
|
1565
|
+
assert.ok(world.draws.every((draw) => !draw.fade), 'keyless draws are unfaded');
|
|
1566
|
+
return heldMaterialDraws(world);
|
|
1567
|
+
};
|
|
1568
|
+
assert.deepEqual(play(true), play(false), 'keyless programs and draws as without the key');
|
|
1569
|
+
},
|
|
1570
|
+
|
|
1571
|
+
// Review 11: a deck LOD makes a faded mesh's material read-only in the
|
|
1572
|
+
// render, after the variants went on. Restoring is total: configurable,
|
|
1573
|
+
// the original goes back with the deck's attributes; not configurable, the
|
|
1574
|
+
// mesh is held and tried at every frame boundary, and nothing throws.
|
|
1575
|
+
'restoring a mesh whose material was locked mid-render'() {
|
|
1576
|
+
const play = (keyed, configurable) => {
|
|
1577
|
+
const stuff = makeStuff();
|
|
1578
|
+
const a = makeWorld(stuff);
|
|
1579
|
+
const mesh = a.scene.getObjectByName('standard');
|
|
1580
|
+
const material = stuff.lit.standard;
|
|
1581
|
+
a.scene.remove(mesh);
|
|
1582
|
+
const lod = new THREE.LOD();
|
|
1583
|
+
lod.addLevel(mesh, 0);
|
|
1584
|
+
a.scene.add(lod);
|
|
1585
|
+
const run = runtime(a, keyed ? KEYED : { distance: 40 });
|
|
1586
|
+
frames(a, run, 2);
|
|
1587
|
+
let once = true;
|
|
1588
|
+
lod.update = function (camera) {
|
|
1589
|
+
THREE.LOD.prototype.update.call(this, camera);
|
|
1590
|
+
if (!once) return;
|
|
1591
|
+
once = false;
|
|
1592
|
+
Object.defineProperty(mesh, 'material', { value: mesh.material, writable: false, configurable, enumerable: true });
|
|
1593
|
+
};
|
|
1594
|
+
assert.doesNotThrow(() => frames(a, run, 2), 'the keyed frames');
|
|
1595
|
+
// Every other mesh has its own material back.
|
|
1596
|
+
a.scene.traverse((object) => {
|
|
1597
|
+
if (object.isMesh && object !== mesh) assert.ok(stuff.all.includes(object.material) || object.material === stuff.pair);
|
|
1598
|
+
});
|
|
1599
|
+
assert.doesNotThrow(() => frames(a, runtime(a, { distance: 40 }), 2), 'a keyless frame after');
|
|
1600
|
+
const descriptor = Object.getOwnPropertyDescriptor(mesh, 'material');
|
|
1601
|
+
return {
|
|
1602
|
+
own: mesh.material === material,
|
|
1603
|
+
variantOf: shadowFadeOriginal(mesh.material) === material,
|
|
1604
|
+
writable: descriptor.writable,
|
|
1605
|
+
configurable: descriptor.configurable,
|
|
1606
|
+
drawn: a.draws.filter((draw) => !draw.shadow && draw.name !== 'standard').map((draw) => [draw.name, draw.material.name, Boolean(draw.fade)]),
|
|
1607
|
+
};
|
|
1608
|
+
};
|
|
1609
|
+
// Configurable: exactly as without the key.
|
|
1610
|
+
const keyed = play(true, true);
|
|
1611
|
+
assert.deepEqual(keyed, play(false, true));
|
|
1612
|
+
assert.deepEqual([keyed.own, keyed.writable, keyed.configurable], [true, false, true]);
|
|
1613
|
+
// Not configurable: the mesh keeps the variant (which reads and writes
|
|
1614
|
+
// the original), everything else is as without the key.
|
|
1615
|
+
const stuck = play(true, false);
|
|
1616
|
+
const plain = play(false, false);
|
|
1617
|
+
assert.equal(stuck.variantOf, true, 'held with its variant');
|
|
1618
|
+
assert.deepEqual(stuck.drawn, plain.drawn, 'the rest of the keyless frame as without the key');
|
|
1619
|
+
},
|
|
1620
|
+
|
|
1621
|
+
// Review 12: a mesh whose material is inherited (writable on its
|
|
1622
|
+
// prototype) is drawn unfaded, so no own property is left on it: a change
|
|
1623
|
+
// to the prototype's material shows in every later frame, as without the
|
|
1624
|
+
// key.
|
|
1625
|
+
'a mesh whose material is inherited is drawn unfaded and keeps inheriting'() {
|
|
1626
|
+
const play = (keyed) => {
|
|
1627
|
+
const stuff = makeStuff();
|
|
1628
|
+
const world = makeWorld(stuff);
|
|
1629
|
+
const mesh = world.scene.getObjectByName('standard');
|
|
1630
|
+
const proto = Object.create(Object.getPrototypeOf(mesh));
|
|
1631
|
+
proto.material = mesh.material;
|
|
1632
|
+
delete mesh.material;
|
|
1633
|
+
Object.setPrototypeOf(mesh, proto);
|
|
1634
|
+
frames(world, runtime(world, keyed ? KEYED : { distance: 40 }), 3);
|
|
1635
|
+
if (keyed) assert.ok(world.draws.some((draw) => draw.fade), 'the rest fade');
|
|
1636
|
+
const own = Object.prototype.hasOwnProperty.call(mesh, 'material');
|
|
1637
|
+
proto.material = stuff.lit.lambert;
|
|
1638
|
+
frames(world, runtime(world), 1);
|
|
1639
|
+
return { own, drawn: world.draws.filter((draw) => draw.name === 'standard' && !draw.shadow).map((draw) => draw.material.name) };
|
|
1640
|
+
};
|
|
1641
|
+
assert.deepEqual(play(true), play(false));
|
|
1642
|
+
assert.deepEqual(play(true), { own: false, drawn: ['lambert'] });
|
|
1643
|
+
},
|
|
1644
|
+
|
|
1645
|
+
// Review 13: a deck LOD redefines a faded mesh's material as an accessor
|
|
1646
|
+
// during the render. With a setter, the original is set through it once;
|
|
1647
|
+
// without one, the mesh is held, and once the fade is dropped the variant
|
|
1648
|
+
// it keeps is inert: keyless frames draw the original's program, with no
|
|
1649
|
+
// fade program made again.
|
|
1650
|
+
'a material redefined as an accessor mid-render is set back, and a variant left behind is inert'() {
|
|
1651
|
+
for (const withSetter of [true, false]) {
|
|
1652
|
+
const play = (keyed) => {
|
|
1653
|
+
const stuff = makeStuff();
|
|
1654
|
+
const a = makeWorld(stuff);
|
|
1655
|
+
const mesh = a.scene.getObjectByName('standard');
|
|
1656
|
+
const material = stuff.lit.standard;
|
|
1657
|
+
a.scene.remove(mesh);
|
|
1658
|
+
const lod = new THREE.LOD();
|
|
1659
|
+
lod.addLevel(mesh, 0);
|
|
1660
|
+
a.scene.add(lod);
|
|
1661
|
+
const run = runtime(a, keyed ? KEYED : { distance: 40 });
|
|
1662
|
+
frames(a, run, 2);
|
|
1663
|
+
let once = true;
|
|
1664
|
+
let store = null;
|
|
1665
|
+
lod.update = function (camera) {
|
|
1666
|
+
THREE.LOD.prototype.update.call(this, camera);
|
|
1667
|
+
if (!once) return;
|
|
1668
|
+
once = false;
|
|
1669
|
+
store = mesh.material;
|
|
1670
|
+
Object.defineProperty(mesh, 'material', {
|
|
1671
|
+
configurable: true,
|
|
1672
|
+
enumerable: true,
|
|
1673
|
+
get: () => store,
|
|
1674
|
+
...(withSetter ? { set: (value) => (store = value) } : {}),
|
|
1675
|
+
});
|
|
1676
|
+
};
|
|
1677
|
+
assert.doesNotThrow(() => frames(a, run, 2));
|
|
1678
|
+
// The key goes.
|
|
1679
|
+
frames(a, runtime(a, { distance: 40 }), 3);
|
|
1680
|
+
const drawn = a.draws.filter((draw) => draw.name === 'standard' && !draw.shadow);
|
|
1681
|
+
return {
|
|
1682
|
+
holdsOriginal: store === material,
|
|
1683
|
+
faded: drawn.some((draw) => draw.fade),
|
|
1684
|
+
key: drawn.map((draw) => a.renderer.properties.get(draw.material).currentProgram.cacheKey.includes(PROGRAM_KEY_SUFFIX)),
|
|
1685
|
+
programs: a.renderer.info.programs.filter((program) => program.cacheKey.includes(PROGRAM_KEY_SUFFIX)).length,
|
|
1686
|
+
shader: a.renderer.properties.get(drawn[0].material).currentProgram === a.renderer.properties.get(material).currentProgram || !a.renderer.properties.has(material),
|
|
1687
|
+
};
|
|
1688
|
+
};
|
|
1689
|
+
const keyed = play(true);
|
|
1690
|
+
assert.deepEqual({ ...keyed, holdsOriginal: withSetter ? keyed.holdsOriginal : true }, { ...play(false), holdsOriginal: true }, `setter ${withSetter}: keyless frames as without the key`);
|
|
1691
|
+
if (withSetter) assert.equal(keyed.holdsOriginal, true, 'the original set back through the setter');
|
|
1692
|
+
}
|
|
1693
|
+
},
|
|
1694
|
+
|
|
1695
|
+
// A variant follows its original: what is set on the original shows
|
|
1696
|
+
// through, and a property the variant cannot give up drops it.
|
|
1697
|
+
'a variant reads the original live, and is dropped when it cannot give a property up'() {
|
|
1698
|
+
const stuff = makeStuff();
|
|
1699
|
+
const world = makeWorld(stuff);
|
|
1700
|
+
const material = stuff.lit.standard;
|
|
1701
|
+
frames(world, runtime(world, KEYED), 3);
|
|
1702
|
+
const variant = world.shadowFade.variants.get(material).variant;
|
|
1703
|
+
material.roughness = 0.8;
|
|
1704
|
+
material.metalness = 0.81;
|
|
1705
|
+
material.custom = 'later';
|
|
1706
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1707
|
+
assert.equal(world.shadowFade.variants.get(material).variant, variant, 'the same variant');
|
|
1708
|
+
assert.equal(variant.roughness, 0.8);
|
|
1709
|
+
assert.equal(variant.custom, 'later', 'a property the original gained');
|
|
1710
|
+
variant.custom = 'through';
|
|
1711
|
+
assert.equal(material.custom, 'through', 'and written through');
|
|
1712
|
+
assert.equal(world.renderer.properties.get(variant).uniforms.roughness.value, 0.8, 'drawn with them');
|
|
1713
|
+
assert.equal(world.renderer.properties.get(variant).uniforms.metalness.value, 0.81);
|
|
1714
|
+
Object.defineProperty(variant, 'emissiveIntensity', { value: 3, writable: false, configurable: false });
|
|
1715
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1716
|
+
assert.equal(material.emissiveIntensity, 3, 'the define is the original\'s');
|
|
1717
|
+
const renewed = world.shadowFade.variants.get(material).variant;
|
|
1718
|
+
assert.notEqual(renewed, variant, 'a new variant');
|
|
1719
|
+
assert.ok(!world.renderer.properties.has(variant), 'the old one freed');
|
|
1720
|
+
assert.equal(renewed.emissiveIntensity, 3);
|
|
1721
|
+
},
|
|
1722
|
+
|
|
1723
|
+
// Review 3: a deck's own material draw callback, which three runs with the
|
|
1724
|
+
// material it draws as `this`.
|
|
1725
|
+
"a material with a deck's own draw callback is drawn as it is, and its writes land on it"() {
|
|
1726
|
+
const roughness = (keyed) => {
|
|
1727
|
+
const stuff = makeStuff();
|
|
1728
|
+
const a = makeWorld(stuff);
|
|
1729
|
+
const b = makeWorld(stuff);
|
|
1730
|
+
const material = stuff.lit.standard;
|
|
1731
|
+
frames(a, runtime(a, keyed ? KEYED : { distance: 40 }), 2);
|
|
1732
|
+
material.onBeforeRender = function (_, scene) {
|
|
1733
|
+
if (scene === a.scene) this.roughness = 0.17;
|
|
1734
|
+
};
|
|
1735
|
+
frames(a, runtime(a, keyed ? KEYED : { distance: 40 }), 2);
|
|
1736
|
+
if (keyed) {
|
|
1737
|
+
const standard = a.draws.filter((draw) => draw.name === 'standard' && !draw.shadow);
|
|
1738
|
+
assert.ok(standard.length > 0 && standard.every((draw) => draw.material === material && !draw.fade), 'drawn as it is, unfaded');
|
|
1739
|
+
assert.ok(a.draws.some((draw) => draw.fade), 'the rest fade');
|
|
1740
|
+
assert.equal(a.shadowFade.variants.has(material), false, 'its variant dropped');
|
|
1741
|
+
}
|
|
1742
|
+
frames(b, runtime(b), 1);
|
|
1743
|
+
return [material.roughness, b.renderer.properties.get(material).uniforms.roughness.value];
|
|
1744
|
+
};
|
|
1745
|
+
assert.deepEqual(roughness(true), roughness(false), 'the keyless world uploads what it would without the key');
|
|
1746
|
+
assert.deepEqual(roughness(true), [0.17, 0.17]);
|
|
1747
|
+
// A mesh with a callback of its own (handed the material it draws) is
|
|
1748
|
+
// drawn with its own material.
|
|
1749
|
+
const stuff = makeStuff();
|
|
1750
|
+
const world = makeWorld(stuff);
|
|
1751
|
+
world.scene.getObjectByName('physical').onAfterRender = () => {};
|
|
1752
|
+
frames(world, runtime(world, KEYED), 3);
|
|
1753
|
+
const physical = world.draws.filter((draw) => draw.name === 'physical' && !draw.shadow);
|
|
1754
|
+
assert.ok(physical.length > 0 && physical.every((draw) => draw.material === stuff.lit.physical));
|
|
1755
|
+
},
|
|
1756
|
+
|
|
1757
|
+
// Review 3: a hook put on the original during the frame (from another
|
|
1758
|
+
// mesh's callback, after the variants went on) never runs for the variant.
|
|
1759
|
+
'a hook the original is given mid-frame never runs on its variant'() {
|
|
1760
|
+
for (const which of ['onBeforeCompile', 'customProgramCacheKey']) {
|
|
1761
|
+
const stuff = makeStuff();
|
|
1762
|
+
const world = makeWorld(stuff);
|
|
1763
|
+
const material = stuff.lit.standard;
|
|
1764
|
+
frames(world, runtime(world, KEYED), 2);
|
|
1765
|
+
const variant = world.shadowFade.variants.get(material).variant;
|
|
1766
|
+
const receivers = [];
|
|
1767
|
+
let once = true;
|
|
1768
|
+
// An unlit mesh, drawn before the standard one (sorted by program).
|
|
1769
|
+
world.scene.getObjectByName('basic').onBeforeRender = () => {
|
|
1770
|
+
if (!once) return;
|
|
1771
|
+
once = false;
|
|
1772
|
+
if (which === 'onBeforeCompile') {
|
|
1773
|
+
material.onBeforeCompile = function (shader) {
|
|
1774
|
+
receivers.push(this);
|
|
1775
|
+
material.userData.shader = shader;
|
|
1776
|
+
};
|
|
1777
|
+
} else {
|
|
1778
|
+
material.customProgramCacheKey = function () {
|
|
1779
|
+
receivers.push(this);
|
|
1780
|
+
return 'deck-key';
|
|
1781
|
+
};
|
|
1782
|
+
}
|
|
1783
|
+
material.needsUpdate = true;
|
|
1784
|
+
};
|
|
1785
|
+
world.draws = [];
|
|
1786
|
+
renderWorld(world, runtime(world, KEYED));
|
|
1787
|
+
assert.ok(!receivers.includes(variant), `${which}: the deck's function never ran on the variant`);
|
|
1788
|
+
assert.ok(!material.userData.shader?.uniforms.nearShadowFade, `${which}: nothing of the fade stored on the original`);
|
|
1789
|
+
const key = world.renderer.properties.get(variant).currentProgram?.cacheKey ?? '';
|
|
1790
|
+
assert.ok(!key.includes('deck-key'), `${which}: the variant's program is not keyed by the deck's function`);
|
|
1791
|
+
// The next frame: drawn as it is, the variant freed.
|
|
1792
|
+
delete world.scene.getObjectByName('basic').onBeforeRender;
|
|
1793
|
+
frames(world, runtime(world, KEYED), 2);
|
|
1794
|
+
const standard = world.draws.filter((draw) => draw.name === 'standard' && !draw.shadow);
|
|
1795
|
+
assert.ok(standard.every((draw) => draw.material === material && !draw.fade), `${which}: then drawn as it is`);
|
|
1796
|
+
assert.ok(!world.renderer.properties.has(variant), `${which}: and its variant freed`);
|
|
1797
|
+
}
|
|
1798
|
+
},
|
|
1799
|
+
|
|
1800
|
+
// Review 2: a define added or deleted in place on the original.
|
|
1801
|
+
"a variant's defines are the original's, changed in place or not"() {
|
|
1802
|
+
const stuff = makeStuff();
|
|
1803
|
+
const world = makeWorld(stuff);
|
|
1804
|
+
const material = stuff.lit.standard;
|
|
1805
|
+
material.defines.EXTRA = '';
|
|
1806
|
+
material.needsUpdate = true;
|
|
1807
|
+
frames(world, runtime(world, KEYED), 3);
|
|
1808
|
+
const variant = world.shadowFade.variants.get(material).variant;
|
|
1809
|
+
const key = () => world.renderer.properties.get(variant).currentProgram.cacheKey;
|
|
1810
|
+
assert.ok(key().includes('EXTRA'), 'compiled with the define');
|
|
1811
|
+
delete material.defines.EXTRA;
|
|
1812
|
+
material.needsUpdate = true;
|
|
1813
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1814
|
+
assert.equal(variant.defines, material.defines, 'the same object');
|
|
1815
|
+
assert.ok(!key().includes('EXTRA'), 'and recompiled without it');
|
|
1816
|
+
material.defines.LATER = '';
|
|
1817
|
+
material.needsUpdate = true;
|
|
1818
|
+
frames(world, runtime(world, KEYED), 1);
|
|
1819
|
+
assert.ok(key().includes('LATER'));
|
|
1820
|
+
assert.deepEqual(Object.keys(variant), []);
|
|
1821
|
+
},
|
|
1822
|
+
|
|
1823
|
+
'a throw inside the render leaves every mesh its own material and both hooks off'() {
|
|
1824
|
+
const stuff = makeStuff();
|
|
1825
|
+
const world = makeWorld(stuff);
|
|
1826
|
+
const pass = world.renderer.shadowMap.render;
|
|
1827
|
+
frames(world, runtime(world, KEYED), 2);
|
|
1828
|
+
for (const where of ['scene', 'object']) {
|
|
1829
|
+
const boom = () => {
|
|
1830
|
+
throw new Error('deck');
|
|
1831
|
+
};
|
|
1832
|
+
if (where === 'scene') world.scene.onBeforeRender = boom;
|
|
1833
|
+
else world.scene.getObjectByName('standard').onBeforeRender = boom;
|
|
1834
|
+
assert.throws(() => renderWorld(world, runtime(world, KEYED)), /deck/);
|
|
1835
|
+
world.scene.traverse((object) => {
|
|
1836
|
+
if (object.isMesh) assert.ok(stuff.all.includes(object.material) || object.material === stuff.pair, `${where}: ${object.name}`);
|
|
1837
|
+
});
|
|
1838
|
+
assert.equal(world.renderer.shadowMap.render, pass);
|
|
1839
|
+
assert.equal(world.scene.onBeforeRender, where === 'scene' ? boom : THREE.Object3D.prototype.onBeforeRender);
|
|
1840
|
+
delete world.scene.onBeforeRender;
|
|
1841
|
+
delete world.scene.getObjectByName('standard').onBeforeRender;
|
|
1842
|
+
}
|
|
1843
|
+
frames(world, runtime(world, KEYED), 3);
|
|
1844
|
+
assert.ok(world.draws.some((draw) => draw.fade), 'and the next frames fade again');
|
|
1845
|
+
},
|
|
1846
|
+
|
|
1847
|
+
'a sun that is not the first casting directional light: no fade'() {
|
|
1848
|
+
const stuff = makeStuff();
|
|
1849
|
+
const world = makeWorld(stuff);
|
|
1850
|
+
const group = new THREE.Group();
|
|
1851
|
+
const first = new THREE.DirectionalLight();
|
|
1852
|
+
first.castShadow = true;
|
|
1853
|
+
group.add(first);
|
|
1854
|
+
world.scene.add(group);
|
|
1855
|
+
world.scene.children.unshift(world.scene.children.pop());
|
|
1856
|
+
frames(world, runtime(world, KEYED), 4);
|
|
1857
|
+
assert.ok(world.draws.every((draw) => !draw.fade), 'nothing faded');
|
|
1858
|
+
assert.ok(world.draws.every((draw) => draw.shadow || stuff.all.includes(draw.material)));
|
|
1859
|
+
},
|
|
1860
|
+
};
|
|
1861
|
+
|
|
1862
|
+
// The cost of a faded frame's own work: the walk, the swaps and the swaps
|
|
1863
|
+
// back, for a scene of `count` receiving meshes.
|
|
1864
|
+
export function measure(count = 500, frames = 2000) {
|
|
1865
|
+
const pass = { render() {} };
|
|
1866
|
+
// A renderer that compiles by adding its one dispose listener, as three's.
|
|
1867
|
+
const released = () => {};
|
|
1868
|
+
const compile = (scene) => scene.traverse((object) => object.material?.addEventListener('dispose', released));
|
|
1869
|
+
const world = { scene: new THREE.Scene(), camera: new THREE.PerspectiveCamera(), renderer: { shadowMap: pass, compile }, shadowFade: null };
|
|
1870
|
+
const sun = makeSun();
|
|
1871
|
+
world.scene.add(sun);
|
|
1872
|
+
const materials = Array.from({ length: 40 }, () => new THREE.MeshStandardMaterial());
|
|
1873
|
+
for (let i = 0; i < count; i++) {
|
|
1874
|
+
const mesh = new THREE.Mesh(undefined, materials[i % materials.length]);
|
|
1875
|
+
mesh.receiveShadow = true;
|
|
1876
|
+
world.scene.add(mesh);
|
|
1877
|
+
}
|
|
1878
|
+
return import('../engine3d/shadowfade.js').then(({ holdShadowFade }) => {
|
|
1879
|
+
const fade = holdShadowFade(world, { nearShadowDistance: 40 });
|
|
1880
|
+
// As three's render calls them: the scene's hook, the shadow pass's,
|
|
1881
|
+
// then the frame's end.
|
|
1882
|
+
const frame = () => {
|
|
1883
|
+
fade.begin(40, sun);
|
|
1884
|
+
world.scene.onBeforeRender(null, world.scene, world.camera, null);
|
|
1885
|
+
pass.render([], world.scene, world.camera);
|
|
1886
|
+
fade.end();
|
|
1887
|
+
};
|
|
1888
|
+
for (let i = 0; i < 200; i++) frame();
|
|
1889
|
+
assert.equal(fade.variants.size, 40);
|
|
1890
|
+
const start = process.hrtime.bigint();
|
|
1891
|
+
for (let i = 0; i < frames; i++) frame();
|
|
1892
|
+
return Number(process.hrtime.bigint() - start) / 1000 / frames;
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
export async function run() {
|
|
1897
|
+
let count = 0;
|
|
1898
|
+
for (const parallel of [false, true]) {
|
|
1899
|
+
parallelShaderCompile = parallel;
|
|
1900
|
+
for (const [name, scenario] of Object.entries(scenarios)) {
|
|
1901
|
+
if (process.env.ONLY && !name.includes(process.env.ONLY)) continue;
|
|
1902
|
+
try {
|
|
1903
|
+
worlds.length = 0;
|
|
1904
|
+
await scenario();
|
|
1905
|
+
for (const world of worlds) world.gl.finishProgramQueries();
|
|
1906
|
+
count += 1;
|
|
1907
|
+
} catch (err) {
|
|
1908
|
+
err.message = `${name} (KHR_parallel_shader_compile: ${parallel}): ${err.message}`;
|
|
1909
|
+
throw err;
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
parallelShaderCompile = false;
|
|
1914
|
+
return count;
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
if (process.argv[1] === new URL(import.meta.url).pathname) {
|
|
1918
|
+
console.log(`orbit shadow fade: ${await run()} scenarios`);
|
|
1919
|
+
console.log(`a faded frame's own work, 500 receiving meshes: ${(await measure()).toFixed(1)} us`);
|
|
1920
|
+
}
|