@egret/engine 0.0.0-stage → 7.0.2
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/LICENSE +201 -0
- package/NOTICE +21 -0
- package/README.md +101 -2
- package/README.zh-CN.md +102 -0
- package/dist/Engine.d.ts +77 -0
- package/dist/Engine.js +469 -0
- package/dist/animation/index.d.ts +6 -0
- package/dist/animation/index.js +5 -0
- package/dist/animation/sequenceController.d.ts +22 -0
- package/dist/animation/sequenceController.js +618 -0
- package/dist/animation/sequenceCoordinator.d.ts +26 -0
- package/dist/animation/sequenceCoordinator.js +161 -0
- package/dist/animation/sequenceInput.d.ts +25 -0
- package/dist/animation/sequenceInput.js +108 -0
- package/dist/animation/sequenceMath.d.ts +35 -0
- package/dist/animation/sequenceMath.js +164 -0
- package/dist/animation/sequenceResetSlot.d.ts +7 -0
- package/dist/animation/sequenceResetSlot.js +367 -0
- package/dist/animation/sequenceResetTypes.d.ts +48 -0
- package/dist/animation/sequenceResetTypes.js +2 -0
- package/dist/animation/types.d.ts +75 -0
- package/dist/animation/types.js +2 -0
- package/dist/assetBundle.d.ts +54 -0
- package/dist/assetBundle.js +173 -0
- package/dist/assetGroup.d.ts +17 -0
- package/dist/assetGroup.js +332 -0
- package/dist/assetGroupTypes.d.ts +17 -0
- package/dist/assetGroupTypes.js +2 -0
- package/dist/audio.d.ts +4 -0
- package/dist/audio.js +4 -0
- package/dist/audioCues.d.ts +47 -0
- package/dist/audioCues.js +127 -0
- package/dist/audioMixer.d.ts +7 -0
- package/dist/audioMixer.js +785 -0
- package/dist/audioTypes.d.ts +246 -0
- package/dist/audioTypes.js +3 -0
- package/dist/bitmapBindingTicket.d.ts +24 -0
- package/dist/bitmapBindingTicket.js +215 -0
- package/dist/bitmapButton.d.ts +4 -0
- package/dist/bitmapButton.js +407 -0
- package/dist/bitmapButtonTypes.d.ts +31 -0
- package/dist/bitmapButtonTypes.js +2 -0
- package/dist/bitmapFontParse.d.ts +10 -0
- package/dist/bitmapFontParse.js +246 -0
- package/dist/bitmapFontTypes.d.ts +112 -0
- package/dist/bitmapFontTypes.js +2 -0
- package/dist/bitmapResourceSlot.d.ts +26 -0
- package/dist/bitmapResourceSlot.js +340 -0
- package/dist/bitmapText.d.ts +6 -0
- package/dist/bitmapText.js +320 -0
- package/dist/bitmapfont.d.ts +5 -0
- package/dist/bitmapfont.js +5 -0
- package/dist/camera.d.ts +4 -0
- package/dist/camera.js +4 -0
- package/dist/camera2d.d.ts +7 -0
- package/dist/camera2d.js +419 -0
- package/dist/cameraTypes.d.ts +126 -0
- package/dist/cameraTypes.js +2 -0
- package/dist/closeHost.d.ts +4 -0
- package/dist/closeHost.js +66 -0
- package/dist/contentArtifacts.d.ts +12 -0
- package/dist/contentArtifacts.js +153 -0
- package/dist/contentAtlasPages.d.ts +13 -0
- package/dist/contentAtlasPages.js +117 -0
- package/dist/contentLoader.d.ts +6 -0
- package/dist/contentLoader.js +327 -0
- package/dist/contentTypes.d.ts +35 -0
- package/dist/contentTypes.js +2 -0
- package/dist/createEngine.d.ts +7 -0
- package/dist/createEngine.js +25 -0
- package/dist/createEngineForProfile.d.ts +5 -0
- package/dist/createEngineForProfile.js +45 -0
- package/dist/displaySubtreeTicket.d.ts +25 -0
- package/dist/displaySubtreeTicket.js +76 -0
- package/dist/fontContentLoader.d.ts +6 -0
- package/dist/fontContentLoader.js +298 -0
- package/dist/fontContentTypes.d.ts +39 -0
- package/dist/fontContentTypes.js +2 -0
- package/dist/gameClock.d.ts +24 -0
- package/dist/gameClock.js +246 -0
- package/dist/gameClockTypes.d.ts +37 -0
- package/dist/gameClockTypes.js +2 -0
- package/dist/gameRun.d.ts +53 -0
- package/dist/gameRun.js +97 -0
- package/dist/gamepad.d.ts +5 -0
- package/dist/gamepad.js +5 -0
- package/dist/gamepadInput.d.ts +4 -0
- package/dist/gamepadInput.js +281 -0
- package/dist/gamepadKeys.d.ts +4 -0
- package/dist/gamepadKeys.js +205 -0
- package/dist/gamepadTypes.d.ts +142 -0
- package/dist/gamepadTypes.js +3 -0
- package/dist/hover.d.ts +4 -0
- package/dist/hover.js +4 -0
- package/dist/hoverInput.d.ts +8 -0
- package/dist/hoverInput.js +503 -0
- package/dist/hoverInputTypes.d.ts +129 -0
- package/dist/hoverInputTypes.js +2 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +21 -0
- package/dist/input.d.ts +6 -0
- package/dist/input.js +5 -0
- package/dist/inputActions.d.ts +86 -0
- package/dist/inputActions.js +330 -0
- package/dist/internal/contentFactoryState.d.ts +19 -0
- package/dist/internal/contentFactoryState.js +42 -0
- package/dist/internal/mouseInputFactoryState.d.ts +57 -0
- package/dist/internal/mouseInputFactoryState.js +125 -0
- package/dist/internal/sequenceControllerFactoryState.d.ts +42 -0
- package/dist/internal/sequenceControllerFactoryState.js +98 -0
- package/dist/internal/textFieldFactoryState.d.ts +11 -0
- package/dist/internal/textFieldFactoryState.js +32 -0
- package/dist/internal/texturedFrameState.d.ts +4 -0
- package/dist/internal/texturedFrameState.js +12 -0
- package/dist/internal/uiFactoryState.d.ts +68 -0
- package/dist/internal/uiFactoryState.js +173 -0
- package/dist/internal/uiRootState.d.ts +67 -0
- package/dist/internal/uiRootState.js +103 -0
- package/dist/keyboardInput.d.ts +13 -0
- package/dist/keyboardInput.js +579 -0
- package/dist/keyboardInputTypes.d.ts +93 -0
- package/dist/keyboardInputTypes.js +2 -0
- package/dist/kit.d.ts +16 -0
- package/dist/kit.js +10 -0
- package/dist/layout.d.ts +5 -0
- package/dist/layout.js +132 -0
- package/dist/layoutTypes.d.ts +28 -0
- package/dist/layoutTypes.js +2 -0
- package/dist/manifestAssetGroup.d.ts +10 -0
- package/dist/manifestAssetGroup.js +477 -0
- package/dist/manifestResourceState.d.ts +17 -0
- package/dist/manifestResourceState.js +58 -0
- package/dist/manifestResourceTypes.d.ts +26 -0
- package/dist/manifestResourceTypes.js +2 -0
- package/dist/motion.d.ts +35 -0
- package/dist/motion.js +206 -0
- package/dist/mouseInput.d.ts +102 -0
- package/dist/mouseInput.js +562 -0
- package/dist/mouseInputTypes.d.ts +41 -0
- package/dist/mouseInputTypes.js +2 -0
- package/dist/paragraph.d.ts +8 -0
- package/dist/paragraph.js +7 -0
- package/dist/particleEmitter.d.ts +4 -0
- package/dist/particleEmitter.js +300 -0
- package/dist/particleTypes.d.ts +115 -0
- package/dist/particleTypes.js +2 -0
- package/dist/particles.d.ts +4 -0
- package/dist/particles.js +4 -0
- package/dist/physics2d.d.ts +4 -0
- package/dist/physics2d.js +4 -0
- package/dist/physics2dGeometry.d.ts +39 -0
- package/dist/physics2dGeometry.js +230 -0
- package/dist/physics2dTypes.d.ts +221 -0
- package/dist/physics2dTypes.js +2 -0
- package/dist/physics2dWorld.d.ts +4 -0
- package/dist/physics2dWorld.js +823 -0
- package/dist/pointerInput.d.ts +12 -0
- package/dist/pointerInput.js +300 -0
- package/dist/pointerInputTypes.d.ts +41 -0
- package/dist/pointerInputTypes.js +2 -0
- package/dist/random.d.ts +30 -0
- package/dist/random.js +131 -0
- package/dist/rendering/ownedPreparationFailure2D.d.ts +5 -0
- package/dist/rendering/ownedPreparationFailure2D.js +33 -0
- package/dist/rendering/packSharedDraws2D.d.ts +50 -0
- package/dist/rendering/packSharedDraws2D.js +379 -0
- package/dist/rendering/packedOrientation.d.ts +4 -0
- package/dist/rendering/packedOrientation.js +7 -0
- package/dist/rendering/prepareRectangles2D.d.ts +92 -0
- package/dist/rendering/prepareRectangles2D.js +481 -0
- package/dist/rendering/prepareSharedDraws2D.d.ts +90 -0
- package/dist/rendering/prepareSharedDraws2D.js +663 -0
- package/dist/rendering/preparedBatches2D.d.ts +17 -0
- package/dist/rendering/preparedBatches2D.js +30 -0
- package/dist/rendering/preparedSharedBatches2D.d.ts +44 -0
- package/dist/rendering/preparedSharedBatches2D.js +247 -0
- package/dist/rendering/sharedColor2D.d.ts +52 -0
- package/dist/rendering/sharedColor2D.js +191 -0
- package/dist/rendering/sharedEffects2D.d.ts +23 -0
- package/dist/rendering/sharedEffects2D.js +126 -0
- package/dist/rendering/sharedPreparationRetention2D.d.ts +120 -0
- package/dist/rendering/sharedPreparationRetention2D.js +357 -0
- package/dist/rendering/srgbBytes.d.ts +3 -0
- package/dist/rendering/srgbBytes.js +64 -0
- package/dist/rendering/texturedPreparationBudget2D.d.ts +31 -0
- package/dist/rendering/texturedPreparationBudget2D.js +81 -0
- package/dist/rendering/world3d/Animation3D.d.ts +104 -0
- package/dist/rendering/world3d/Animation3D.js +384 -0
- package/dist/rendering/world3d/AnimationTexture3D.d.ts +163 -0
- package/dist/rendering/world3d/AnimationTexture3D.js +603 -0
- package/dist/rendering/world3d/Batch3D.d.ts +75 -0
- package/dist/rendering/world3d/Batch3D.js +332 -0
- package/dist/rendering/world3d/Camera3D.d.ts +62 -0
- package/dist/rendering/world3d/Camera3D.js +132 -0
- package/dist/rendering/world3d/Character3D.d.ts +127 -0
- package/dist/rendering/world3d/Character3D.js +305 -0
- package/dist/rendering/world3d/Cloth3D.d.ts +130 -0
- package/dist/rendering/world3d/Cloth3D.js +503 -0
- package/dist/rendering/world3d/Compression3D.d.ts +22 -0
- package/dist/rendering/world3d/Compression3D.js +703 -0
- package/dist/rendering/world3d/Curves3D.d.ts +119 -0
- package/dist/rendering/world3d/Curves3D.js +460 -0
- package/dist/rendering/world3d/Decal3D.d.ts +43 -0
- package/dist/rendering/world3d/Decal3D.js +242 -0
- package/dist/rendering/world3d/Export3D.d.ts +13 -0
- package/dist/rendering/world3d/Export3D.js +263 -0
- package/dist/rendering/world3d/Geometry3D.d.ts +95 -0
- package/dist/rendering/world3d/Geometry3D.js +335 -0
- package/dist/rendering/world3d/GeometryBuilder3D.d.ts +53 -0
- package/dist/rendering/world3d/GeometryBuilder3D.js +196 -0
- package/dist/rendering/world3d/Gizmo3D.d.ts +80 -0
- package/dist/rendering/world3d/Gizmo3D.js +514 -0
- package/dist/rendering/world3d/Gltf3D.d.ts +73 -0
- package/dist/rendering/world3d/Gltf3D.js +996 -0
- package/dist/rendering/world3d/GpuParticles3D.d.ts +109 -0
- package/dist/rendering/world3d/GpuParticles3D.js +401 -0
- package/dist/rendering/world3d/Hdr3D.d.ts +35 -0
- package/dist/rendering/world3d/Hdr3D.js +191 -0
- package/dist/rendering/world3d/Jelly3D.d.ts +77 -0
- package/dist/rendering/world3d/Jelly3D.js +267 -0
- package/dist/rendering/world3d/Ktx23D.d.ts +30 -0
- package/dist/rendering/world3d/Ktx23D.js +112 -0
- package/dist/rendering/world3d/Light3D.d.ts +84 -0
- package/dist/rendering/world3d/Light3D.js +97 -0
- package/dist/rendering/world3d/Material3D.d.ts +253 -0
- package/dist/rendering/world3d/Material3D.js +269 -0
- package/dist/rendering/world3d/Mesh3D.d.ts +112 -0
- package/dist/rendering/world3d/Mesh3D.js +337 -0
- package/dist/rendering/world3d/Meshopt3D.d.ts +15 -0
- package/dist/rendering/world3d/Meshopt3D.js +276 -0
- package/dist/rendering/world3d/Navigation3D.d.ts +213 -0
- package/dist/rendering/world3d/Navigation3D.js +1043 -0
- package/dist/rendering/world3d/Object3D.d.ts +118 -0
- package/dist/rendering/world3d/Object3D.js +315 -0
- package/dist/rendering/world3d/Particles3D.d.ts +90 -0
- package/dist/rendering/world3d/Particles3D.js +277 -0
- package/dist/rendering/world3d/Physics3D.d.ts +430 -0
- package/dist/rendering/world3d/Physics3D.js +2840 -0
- package/dist/rendering/world3d/PhysicsHull3D.d.ts +37 -0
- package/dist/rendering/world3d/PhysicsHull3D.js +229 -0
- package/dist/rendering/world3d/PhysicsMesh3D.d.ts +41 -0
- package/dist/rendering/world3d/PhysicsMesh3D.js +340 -0
- package/dist/rendering/world3d/PhysicsQueries3D.d.ts +24 -0
- package/dist/rendering/world3d/PhysicsQueries3D.js +323 -0
- package/dist/rendering/world3d/PhysicsRemote3D.d.ts +179 -0
- package/dist/rendering/world3d/PhysicsRemote3D.js +536 -0
- package/dist/rendering/world3d/Ragdoll3D.d.ts +74 -0
- package/dist/rendering/world3d/Ragdoll3D.js +202 -0
- package/dist/rendering/world3d/Raycast3D.d.ts +40 -0
- package/dist/rendering/world3d/Raycast3D.js +247 -0
- package/dist/rendering/world3d/RenderList3D.d.ts +91 -0
- package/dist/rendering/world3d/RenderList3D.js +502 -0
- package/dist/rendering/world3d/Retarget3D.d.ts +21 -0
- package/dist/rendering/world3d/Retarget3D.js +185 -0
- package/dist/rendering/world3d/Rig3D.d.ts +57 -0
- package/dist/rendering/world3d/Rig3D.js +219 -0
- package/dist/rendering/world3d/Sdf3D.d.ts +181 -0
- package/dist/rendering/world3d/Sdf3D.js +1136 -0
- package/dist/rendering/world3d/SdfLod3D.d.ts +17 -0
- package/dist/rendering/world3d/SdfLod3D.js +56 -0
- package/dist/rendering/world3d/Serialize3D.d.ts +66 -0
- package/dist/rendering/world3d/Serialize3D.js +629 -0
- package/dist/rendering/world3d/Simplify3D.d.ts +29 -0
- package/dist/rendering/world3d/Simplify3D.js +426 -0
- package/dist/rendering/world3d/StateMachine3D.d.ts +87 -0
- package/dist/rendering/world3d/StateMachine3D.js +280 -0
- package/dist/rendering/world3d/SurfaceMaterial3D.d.ts +26 -0
- package/dist/rendering/world3d/SurfaceMaterial3D.js +138 -0
- package/dist/rendering/world3d/Terrain3D.d.ts +248 -0
- package/dist/rendering/world3d/Terrain3D.js +827 -0
- package/dist/rendering/world3d/Text3D.d.ts +95 -0
- package/dist/rendering/world3d/Text3D.js +560 -0
- package/dist/rendering/world3d/Texture3D.d.ts +148 -0
- package/dist/rendering/world3d/Texture3D.js +270 -0
- package/dist/rendering/world3d/Trail3D.d.ts +62 -0
- package/dist/rendering/world3d/Trail3D.js +195 -0
- package/dist/rendering/world3d/Vehicle3D.d.ts +102 -0
- package/dist/rendering/world3d/Vehicle3D.js +228 -0
- package/dist/rendering/world3d/VolumetricFog3D.d.ts +57 -0
- package/dist/rendering/world3d/VolumetricFog3D.js +230 -0
- package/dist/rendering/world3d/Water3D.d.ts +165 -0
- package/dist/rendering/world3d/Water3D.js +607 -0
- package/dist/rendering/world3d/core3D.d.ts +36 -0
- package/dist/rendering/world3d/core3D.js +99 -0
- package/dist/rendering/world3d/debug3D.d.ts +69 -0
- package/dist/rendering/world3d/debug3D.js +262 -0
- package/dist/rendering/world3d/decoders3D.d.ts +42 -0
- package/dist/rendering/world3d/decoders3D.js +133 -0
- package/dist/rendering/world3d/geometries3D.d.ts +111 -0
- package/dist/rendering/world3d/geometries3D.js +610 -0
- package/dist/rendering/world3d/helpers3D.d.ts +67 -0
- package/dist/rendering/world3d/helpers3D.js +178 -0
- package/dist/rendering/world3d/index.d.ts +103 -0
- package/dist/rendering/world3d/index.js +62 -0
- package/dist/rendering/world3d/math3D.d.ts +114 -0
- package/dist/rendering/world3d/math3D.js +675 -0
- package/dist/rendering/world3d/primitives3D.d.ts +67 -0
- package/dist/rendering/world3d/primitives3D.js +197 -0
- package/dist/resources.d.ts +8 -0
- package/dist/resources.js +6 -0
- package/dist/saveStore.d.ts +71 -0
- package/dist/saveStore.js +182 -0
- package/dist/semanticList.d.ts +126 -0
- package/dist/semanticList.js +175 -0
- package/dist/semanticTree.d.ts +10 -0
- package/dist/semanticTree.js +539 -0
- package/dist/semanticTypes.d.ts +175 -0
- package/dist/semanticTypes.js +2 -0
- package/dist/semantics.d.ts +6 -0
- package/dist/semantics.js +5 -0
- package/dist/sequencePlayback.d.ts +19 -0
- package/dist/sequencePlayback.js +391 -0
- package/dist/sequencePlaybackTypes.d.ts +24 -0
- package/dist/sequencePlaybackTypes.js +2 -0
- package/dist/sequenceTargetClaims.d.ts +10 -0
- package/dist/sequenceTargetClaims.js +31 -0
- package/dist/srgbBytes.d.ts +3 -0
- package/dist/srgbBytes.js +64 -0
- package/dist/startEngine.d.ts +5 -0
- package/dist/startEngine.js +31 -0
- package/dist/surfaceOwnership.d.ts +3 -0
- package/dist/surfaceOwnership.js +15 -0
- package/dist/text.d.ts +24 -0
- package/dist/text.js +4 -0
- package/dist/textField.d.ts +6 -0
- package/dist/textField.js +6 -0
- package/dist/textTable.d.ts +41 -0
- package/dist/textTable.js +114 -0
- package/dist/tileMapDraw.d.ts +7 -0
- package/dist/tileMapDraw.js +310 -0
- package/dist/tiledImport.d.ts +9 -0
- package/dist/tiledImport.js +357 -0
- package/dist/tilemap.d.ts +5 -0
- package/dist/tilemap.js +5 -0
- package/dist/tilemapTypes.d.ts +182 -0
- package/dist/tilemapTypes.js +2 -0
- package/dist/timer.d.ts +34 -0
- package/dist/timer.js +119 -0
- package/dist/tween.d.ts +15 -0
- package/dist/tween.js +329 -0
- package/dist/tweenTypes.d.ts +31 -0
- package/dist/tweenTypes.js +2 -0
- package/dist/ui/controlKeys.d.ts +113 -0
- package/dist/ui/controlKeys.js +250 -0
- package/dist/ui/controls.d.ts +58 -0
- package/dist/ui/controls.js +290 -0
- package/dist/ui/editing.d.ts +3 -0
- package/dist/ui/editing.js +3 -0
- package/dist/ui/index.d.ts +19 -0
- package/dist/ui/index.js +11 -0
- package/dist/ui/keyboardFocus.d.ts +32 -0
- package/dist/ui/keyboardFocus.js +146 -0
- package/dist/ui/layerTypes.d.ts +98 -0
- package/dist/ui/layerTypes.js +2 -0
- package/dist/ui/layers.d.ts +4 -0
- package/dist/ui/layers.js +586 -0
- package/dist/ui/leafContent.d.ts +21 -0
- package/dist/ui/leafContent.js +136 -0
- package/dist/ui/rangeControl.d.ts +53 -0
- package/dist/ui/rangeControl.js +168 -0
- package/dist/ui/scrollControl.d.ts +84 -0
- package/dist/ui/scrollControl.js +222 -0
- package/dist/ui/stateControls.d.ts +90 -0
- package/dist/ui/stateControls.js +331 -0
- package/dist/ui/textEditor.d.ts +54 -0
- package/dist/ui/textEditor.js +371 -0
- package/dist/ui/textEditorTypes.d.ts +85 -0
- package/dist/ui/textEditorTypes.js +2 -0
- package/dist/ui/textFieldControl.d.ts +100 -0
- package/dist/ui/textFieldControl.js +260 -0
- package/dist/ui/theme.d.ts +64 -0
- package/dist/ui/theme.js +166 -0
- package/dist/ui/types.d.ts +285 -0
- package/dist/ui/types.js +2 -0
- package/dist/ui/uiRoot.d.ts +4 -0
- package/dist/ui/uiRoot.js +1526 -0
- package/dist/ui/uiRootDisplay.d.ts +46 -0
- package/dist/ui/uiRootDisplay.js +148 -0
- package/dist/ui/uiRootModel.d.ts +114 -0
- package/dist/ui/uiRootModel.js +66 -0
- package/dist/ui/virtualList.d.ts +20 -0
- package/dist/ui/virtualList.js +105 -0
- package/dist/video.d.ts +4 -0
- package/dist/video.js +4 -0
- package/dist/videoPlayer.d.ts +4 -0
- package/dist/videoPlayer.js +215 -0
- package/dist/videoTypes.d.ts +77 -0
- package/dist/videoTypes.js +2 -0
- package/dist/web/BrowserAssetBundle.d.ts +41 -0
- package/dist/web/BrowserAssetBundle.js +136 -0
- package/dist/web/BrowserAudio.d.ts +74 -0
- package/dist/web/BrowserAudio.js +503 -0
- package/dist/web/BrowserFrameLoop.d.ts +69 -0
- package/dist/web/BrowserFrameLoop.js +125 -0
- package/dist/web/BrowserGame.d.ts +174 -0
- package/dist/web/BrowserGame.js +473 -0
- package/dist/web/BrowserGamepadInput.d.ts +38 -0
- package/dist/web/BrowserGamepadInput.js +134 -0
- package/dist/web/BrowserHoverInput.d.ts +25 -0
- package/dist/web/BrowserHoverInput.js +266 -0
- package/dist/web/BrowserImageTextures.d.ts +72 -0
- package/dist/web/BrowserImageTextures.js +169 -0
- package/dist/web/BrowserInputActions.d.ts +63 -0
- package/dist/web/BrowserInputActions.js +345 -0
- package/dist/web/BrowserKeyboardInput.d.ts +4 -0
- package/dist/web/BrowserKeyboardInput.js +223 -0
- package/dist/web/BrowserPlay.d.ts +417 -0
- package/dist/web/BrowserPlay.js +1648 -0
- package/dist/web/BrowserPointerInput.d.ts +4 -0
- package/dist/web/BrowserPointerInput.js +511 -0
- package/dist/web/BrowserPreferences.d.ts +10 -0
- package/dist/web/BrowserPreferences.js +39 -0
- package/dist/web/BrowserSemantics.d.ts +36 -0
- package/dist/web/BrowserSemantics.js +418 -0
- package/dist/web/BrowserStatsPanel.d.ts +56 -0
- package/dist/web/BrowserStatsPanel.js +143 -0
- package/dist/web/BrowserStorage.d.ts +35 -0
- package/dist/web/BrowserStorage.js +132 -0
- package/dist/web/BrowserTextEditor.d.ts +52 -0
- package/dist/web/BrowserTextEditor.js +839 -0
- package/dist/web/BrowserTextInput.d.ts +89 -0
- package/dist/web/BrowserTextInput.js +524 -0
- package/dist/web/BrowserTextureProvider.d.ts +58 -0
- package/dist/web/BrowserTextureProvider.js +935 -0
- package/dist/web/BrowserVideo.d.ts +40 -0
- package/dist/web/BrowserVideo.js +237 -0
- package/dist/web/CanvasKitFontSetProvider.d.ts +32 -0
- package/dist/web/CanvasKitFontSetProvider.js +215 -0
- package/dist/web/CanvasKitParagraphProvider.d.ts +173 -0
- package/dist/web/CanvasKitParagraphProvider.js +692 -0
- package/dist/web/CanvasTextRasterProvider.d.ts +4 -0
- package/dist/web/CanvasTextRasterProvider.js +312 -0
- package/dist/web/ManifestByteSource.d.ts +151 -0
- package/dist/web/ManifestByteSource.js +975 -0
- package/dist/web/ManifestFontContentLoader.d.ts +13 -0
- package/dist/web/ManifestFontContentLoader.js +30 -0
- package/dist/web/MouseInput.d.ts +4 -0
- package/dist/web/MouseInput.js +218 -0
- package/dist/web/WebGL2Host.d.ts +5 -0
- package/dist/web/WebGL2Host.js +2711 -0
- package/dist/web/WebGPUHost.d.ts +100 -0
- package/dist/web/WebGPUHost.js +3430 -0
- package/dist/web/audio.d.ts +3 -0
- package/dist/web/audio.js +4 -0
- package/dist/web/canvasKitParagraphGeometry.d.ts +27 -0
- package/dist/web/canvasKitParagraphGeometry.js +83 -0
- package/dist/web/editing.d.ts +5 -0
- package/dist/web/editing.js +4 -0
- package/dist/web/editorFonts.d.ts +54 -0
- package/dist/web/editorFonts.js +297 -0
- package/dist/web/game.d.ts +5 -0
- package/dist/web/game.js +6 -0
- package/dist/web/gamepad.d.ts +3 -0
- package/dist/web/gamepad.js +4 -0
- package/dist/web/hover.d.ts +3 -0
- package/dist/web/hover.js +4 -0
- package/dist/web/image.d.ts +4 -0
- package/dist/web/image.js +4 -0
- package/dist/web/imageProjections2D.d.ts +16 -0
- package/dist/web/imageProjections2D.js +2 -0
- package/dist/web/input.d.ts +6 -0
- package/dist/web/input.js +4 -0
- package/dist/web/keyboard.d.ts +3 -0
- package/dist/web/keyboard.js +3 -0
- package/dist/web/kit.d.ts +11 -0
- package/dist/web/kit.js +8 -0
- package/dist/web/loop.d.ts +3 -0
- package/dist/web/loop.js +4 -0
- package/dist/web/paragraph.d.ts +6 -0
- package/dist/web/paragraph.js +4 -0
- package/dist/web/play.d.ts +10 -0
- package/dist/web/play.js +9 -0
- package/dist/web/playMusic.d.ts +33 -0
- package/dist/web/playMusic.js +194 -0
- package/dist/web/playSfx.d.ts +36 -0
- package/dist/web/playSfx.js +112 -0
- package/dist/web/playTouch.d.ts +45 -0
- package/dist/web/playTouch.js +141 -0
- package/dist/web/playWidgets.d.ts +71 -0
- package/dist/web/playWidgets.js +431 -0
- package/dist/web/pointer.d.ts +3 -0
- package/dist/web/pointer.js +3 -0
- package/dist/web/prepareSharedSubmission2D.d.ts +27 -0
- package/dist/web/prepareSharedSubmission2D.js +63 -0
- package/dist/web/resources.d.ts +29 -0
- package/dist/web/resources.js +96 -0
- package/dist/web/semantics.d.ts +3 -0
- package/dist/web/semantics.js +4 -0
- package/dist/web/sharedDeviceSession.d.ts +64 -0
- package/dist/web/sharedDeviceSession.js +2 -0
- package/dist/web/sharedEffectShaders.d.ts +13 -0
- package/dist/web/sharedEffectShaders.js +181 -0
- package/dist/web/sharedFrameUpload.d.ts +25 -0
- package/dist/web/sharedFrameUpload.js +2 -0
- package/dist/web/sharedHostDeferred.d.ts +7 -0
- package/dist/web/sharedHostDeferred.js +12 -0
- package/dist/web/sharedRetentionLedger.d.ts +85 -0
- package/dist/web/sharedRetentionLedger.js +391 -0
- package/dist/web/sharedSubmissionStorage2D.d.ts +56 -0
- package/dist/web/sharedSubmissionStorage2D.js +73 -0
- package/dist/web/sharedWebGL2Arena.d.ts +40 -0
- package/dist/web/sharedWebGL2Arena.js +77 -0
- package/dist/web/sharedWebGL2Options.d.ts +33 -0
- package/dist/web/sharedWebGL2Options.js +102 -0
- package/dist/web/sharedWebGL2Shaders.d.ts +33 -0
- package/dist/web/sharedWebGL2Shaders.js +408 -0
- package/dist/web/sharedWebGPUOptions.d.ts +41 -0
- package/dist/web/sharedWebGPUOptions.js +130 -0
- package/dist/web/text.d.ts +3 -0
- package/dist/web/text.js +3 -0
- package/dist/web/textInput.d.ts +3 -0
- package/dist/web/textInput.js +3 -0
- package/dist/web/video.d.ts +3 -0
- package/dist/web/video.js +4 -0
- package/dist/web/webgl2.d.ts +7 -0
- package/dist/web/webgl2.js +4 -0
- package/dist/web/webgl2LayerState.d.ts +50 -0
- package/dist/web/webgl2LayerState.js +102 -0
- package/dist/web/webgl2Native.d.ts +72 -0
- package/dist/web/webgl2Native.js +572 -0
- package/dist/web/webgl2Types.d.ts +150 -0
- package/dist/web/webgl2Types.js +2 -0
- package/dist/web/webgpu.d.ts +6 -0
- package/dist/web/webgpu.js +2 -0
- package/dist/web/webgpuBindingCache.d.ts +26 -0
- package/dist/web/webgpuBindingCache.js +223 -0
- package/dist/web/webgpuBufferPool.d.ts +51 -0
- package/dist/web/webgpuBufferPool.js +58 -0
- package/dist/web/webgpuImageResidency.d.ts +56 -0
- package/dist/web/webgpuImageResidency.js +65 -0
- package/dist/web/webgpuLifetime.d.ts +45 -0
- package/dist/web/webgpuLifetime.js +64 -0
- package/dist/web/webgpuRetentionPool.d.ts +66 -0
- package/dist/web/webgpuRetentionPool.js +85 -0
- package/dist/web/webgpuSharedPass.d.ts +7 -0
- package/dist/web/webgpuSharedPass.js +293 -0
- package/dist/web/webgpuSharedResolvePass.d.ts +3 -0
- package/dist/web/webgpuSharedResolvePass.js +34 -0
- package/dist/web/world3d/Audio3D.d.ts +85 -0
- package/dist/web/world3d/Audio3D.js +271 -0
- package/dist/web/world3d/BrowserPlay3D.d.ts +295 -0
- package/dist/web/world3d/BrowserPlay3D.js +1256 -0
- package/dist/web/world3d/FlyControls3D.d.ts +59 -0
- package/dist/web/world3d/FlyControls3D.js +162 -0
- package/dist/web/world3d/FollowControls3D.d.ts +31 -0
- package/dist/web/world3d/FollowControls3D.js +119 -0
- package/dist/web/world3d/OrbitControls3D.d.ts +58 -0
- package/dist/web/world3d/OrbitControls3D.js +189 -0
- package/dist/web/world3d/TransformControls3D.d.ts +37 -0
- package/dist/web/world3d/TransformControls3D.js +128 -0
- package/dist/web/world3d/WebGL2Renderer3D.d.ts +136 -0
- package/dist/web/world3d/WebGL2Renderer3D.js +1175 -0
- package/dist/web/world3d/WebGPURenderer3D.d.ts +112 -0
- package/dist/web/world3d/WebGPURenderer3D.js +1916 -0
- package/dist/web/world3d/World3DView.d.ts +54 -0
- package/dist/web/world3d/World3DView.js +79 -0
- package/dist/web/world3d/app3D.d.ts +143 -0
- package/dist/web/world3d/app3D.js +305 -0
- package/dist/web/world3d/assets3D.d.ts +39 -0
- package/dist/web/world3d/assets3D.js +112 -0
- package/dist/web/world3d/atmosphere3D.d.ts +56 -0
- package/dist/web/world3d/atmosphere3D.js +193 -0
- package/dist/web/world3d/canvasFont3D.d.ts +21 -0
- package/dist/web/world3d/canvasFont3D.js +165 -0
- package/dist/web/world3d/frame3D.d.ts +144 -0
- package/dist/web/world3d/frame3D.js +388 -0
- package/dist/web/world3d/gpuResources3D.d.ts +98 -0
- package/dist/web/world3d/gpuResources3D.js +901 -0
- package/dist/web/world3d/host3D.d.ts +24 -0
- package/dist/web/world3d/host3D.js +61 -0
- package/dist/web/world3d/index.d.ts +40 -0
- package/dist/web/world3d/index.js +24 -0
- package/dist/web/world3d/loaders3D.d.ts +27 -0
- package/dist/web/world3d/loaders3D.js +138 -0
- package/dist/web/world3d/markers3D.d.ts +53 -0
- package/dist/web/world3d/markers3D.js +130 -0
- package/dist/web/world3d/materialWebGL23D.d.ts +34 -0
- package/dist/web/world3d/materialWebGL23D.js +218 -0
- package/dist/web/world3d/overlay3D.d.ts +47 -0
- package/dist/web/world3d/overlay3D.js +187 -0
- package/dist/web/world3d/post3D.d.ts +116 -0
- package/dist/web/world3d/post3D.js +462 -0
- package/dist/web/world3d/postWebGL23D.d.ts +24 -0
- package/dist/web/world3d/postWebGL23D.js +237 -0
- package/dist/web/world3d/postWebGPU3D.d.ts +23 -0
- package/dist/web/world3d/postWebGPU3D.js +255 -0
- package/dist/web/world3d/probe3D.d.ts +15 -0
- package/dist/web/world3d/probe3D.js +66 -0
- package/dist/web/world3d/shaders3D.d.ts +43 -0
- package/dist/web/world3d/shaders3D.js +851 -0
- package/dist/web/world3d/stats3D.d.ts +34 -0
- package/dist/web/world3d/stats3D.js +81 -0
- package/dist/web/world3d/webGL2FrameMetrics3D.d.ts +71 -0
- package/dist/web/world3d/webGL2FrameMetrics3D.js +185 -0
- package/dist/web/world3d/webgpu.d.ts +37 -0
- package/dist/web/world3d/webgpu.js +23 -0
- package/dist/web/world3d/webgpuPacking3D.d.ts +13 -0
- package/dist/web/world3d/webgpuPacking3D.js +85 -0
- package/dist/web/world3d/wgsl3D.d.ts +68 -0
- package/dist/web/world3d/wgsl3D.js +781 -0
- package/package.json +220 -4
|
@@ -0,0 +1,1256 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// A whole small 3D game from one call. The picture is a World3D app; everything else is `play`, drawn clear over
|
|
3
|
+
// it on a canvas of its own: the keys, the pointer, sound, timers, tweens, text and buttons. What is added here is
|
|
4
|
+
// what a 3D game makes its world of: boxes, balls and a ground that are placed by plain numbers, a camera that
|
|
5
|
+
// follows, a sun, tests of distance and overlap, and pieces thrown out by a hit; things grouped to move as one,
|
|
6
|
+
// things that glow, lamps, and snow or rain.
|
|
7
|
+
import { EgretError } from '@egret/runtime';
|
|
8
|
+
import { play } from '../play.js';
|
|
9
|
+
import { Object3D, Scene3D } from '../../rendering/world3d/Object3D.js';
|
|
10
|
+
import { Mesh3D } from '../../rendering/world3d/Mesh3D.js';
|
|
11
|
+
import { createPerspectiveCamera3D } from '../../rendering/world3d/Camera3D.js';
|
|
12
|
+
import { createDirectionalLight3D, createHemisphereLight3D, createPointLight3D } from '../../rendering/world3d/Light3D.js';
|
|
13
|
+
import { GpuParticles3D } from '../../rendering/world3d/GpuParticles3D.js';
|
|
14
|
+
import { Texture3D } from '../../rendering/world3d/Texture3D.js';
|
|
15
|
+
import { createStandardMaterial3D, setColor3D } from '../../rendering/world3d/Material3D.js';
|
|
16
|
+
import { createBoxGeometry3D, createCylinderGeometry3D, createSphereGeometry3D } from '../../rendering/world3d/primitives3D.js';
|
|
17
|
+
import { linearColor3D } from '../../rendering/world3d/GeometryBuilder3D.js';
|
|
18
|
+
import { Raycaster3D } from '../../rendering/world3d/Raycast3D.js';
|
|
19
|
+
import { mat4Identity, mat4Multiply, quatToEuler, vec3TransformMat4 } from '../../rendering/world3d/math3D.js';
|
|
20
|
+
import { createWorld3DApp } from './app3D.js';
|
|
21
|
+
const invalid = (why) => new EgretError('PLAY_OPTIONS_INVALID', { cause: new TypeError(why) });
|
|
22
|
+
const finite = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
23
|
+
const above = (value) => finite(value) && value > 0;
|
|
24
|
+
const colour = (value) => typeof value === 'number' && Number.isInteger(value) && value >= 0 && value <= 0xffffff;
|
|
25
|
+
const RADIANS = Math.PI / 180;
|
|
26
|
+
// Primitive geometry is transferred to its thing. Loaded models remain borrowed; burst geometry belongs to the game.
|
|
27
|
+
const ownedGeometry = new WeakMap();
|
|
28
|
+
const throwCleanupFailures = (failures) => {
|
|
29
|
+
if (failures.length === 1)
|
|
30
|
+
throw failures[0];
|
|
31
|
+
if (failures.length > 1)
|
|
32
|
+
throw new AggregateError(failures, 'Play3D cleanup failed');
|
|
33
|
+
};
|
|
34
|
+
const cleanUp = (actions) => {
|
|
35
|
+
const failures = [];
|
|
36
|
+
for (const action of actions) {
|
|
37
|
+
try {
|
|
38
|
+
action();
|
|
39
|
+
}
|
|
40
|
+
catch (cause) {
|
|
41
|
+
failures.push(cause);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
throwCleanupFailures(failures);
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* A thing of the world: a box, a ball, a cylinder or a cone. It is placed, turned and sized by plain numbers, which
|
|
48
|
+
* can be set at any time and tweened, and it takes properties of the game's own.
|
|
49
|
+
*/
|
|
50
|
+
export class PlayThing3D {
|
|
51
|
+
/** What the thing is drawn with: its own mesh, or the model given to `game.add`. */
|
|
52
|
+
node;
|
|
53
|
+
/** Half its width, height and depth as made, before its scale. */
|
|
54
|
+
halfX;
|
|
55
|
+
halfY;
|
|
56
|
+
halfZ;
|
|
57
|
+
/**
|
|
58
|
+
* Where the middle of its box is from its place, before its scale: 0 for what `play3d` makes, and for a model
|
|
59
|
+
* whatever its geometry says, as for a figure that stands on its place.
|
|
60
|
+
*/
|
|
61
|
+
middleX;
|
|
62
|
+
middleY;
|
|
63
|
+
middleZ;
|
|
64
|
+
#material;
|
|
65
|
+
#color;
|
|
66
|
+
#glow = 0;
|
|
67
|
+
#shadow;
|
|
68
|
+
#turn = [0, 0, 0];
|
|
69
|
+
#removed = false;
|
|
70
|
+
/**
|
|
71
|
+
* A thing of its own geometry and colour, or one of a model made elsewhere: then `half` and `middle` are its box,
|
|
72
|
+
* and its colour is the model's own. Geometry passed directly is owned and released when the thing leaves;
|
|
73
|
+
* an Object3D and its geometry and materials stay owned by their caller.
|
|
74
|
+
*/
|
|
75
|
+
constructor(shape, color, half, middle = [0, 0, 0]) {
|
|
76
|
+
this.#color = color;
|
|
77
|
+
if (shape instanceof Object3D) {
|
|
78
|
+
this.node = shape;
|
|
79
|
+
quatToEuler(this.#turn, shape.quaternion);
|
|
80
|
+
for (const axis of [0, 1, 2])
|
|
81
|
+
this.#turn[axis] /= RADIANS;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
ownedGeometry.set(this, shape);
|
|
85
|
+
this.#material = createStandardMaterial3D({ color, roughness: 0.75 });
|
|
86
|
+
const mesh = new Mesh3D(shape, this.#material);
|
|
87
|
+
mesh.castShadow = true;
|
|
88
|
+
mesh.receiveShadow = true;
|
|
89
|
+
this.node = mesh;
|
|
90
|
+
}
|
|
91
|
+
[this.halfX, this.halfY, this.halfZ] = half;
|
|
92
|
+
[this.middleX, this.middleY, this.middleZ] = middle;
|
|
93
|
+
}
|
|
94
|
+
#place(index, value) {
|
|
95
|
+
if (!finite(value))
|
|
96
|
+
return;
|
|
97
|
+
this.node.position[index] = value;
|
|
98
|
+
this.node.markTransformDirty();
|
|
99
|
+
}
|
|
100
|
+
#size(index, value) {
|
|
101
|
+
if (!finite(value))
|
|
102
|
+
return;
|
|
103
|
+
this.node.scale[index] = value;
|
|
104
|
+
this.node.markTransformDirty();
|
|
105
|
+
}
|
|
106
|
+
#turned(index, degrees) {
|
|
107
|
+
if (!finite(degrees))
|
|
108
|
+
return;
|
|
109
|
+
this.#turn[index] = degrees;
|
|
110
|
+
this.node.setRotation(this.#turn[0] * RADIANS, this.#turn[1] * RADIANS, this.#turn[2] * RADIANS);
|
|
111
|
+
}
|
|
112
|
+
get x() {
|
|
113
|
+
return this.node.position[0];
|
|
114
|
+
}
|
|
115
|
+
set x(value) {
|
|
116
|
+
this.#place(0, value);
|
|
117
|
+
}
|
|
118
|
+
/** Up. */
|
|
119
|
+
get y() {
|
|
120
|
+
return this.node.position[1];
|
|
121
|
+
}
|
|
122
|
+
set y(value) {
|
|
123
|
+
this.#place(1, value);
|
|
124
|
+
}
|
|
125
|
+
/** Towards the camera as the game starts; away from it is less. */
|
|
126
|
+
get z() {
|
|
127
|
+
return this.node.position[2];
|
|
128
|
+
}
|
|
129
|
+
set z(value) {
|
|
130
|
+
this.#place(2, value);
|
|
131
|
+
}
|
|
132
|
+
/** Turns about its own axes, in degrees. */
|
|
133
|
+
get rotationX() {
|
|
134
|
+
return this.#turn[0];
|
|
135
|
+
}
|
|
136
|
+
set rotationX(value) {
|
|
137
|
+
this.#turned(0, value);
|
|
138
|
+
}
|
|
139
|
+
get rotationY() {
|
|
140
|
+
return this.#turn[1];
|
|
141
|
+
}
|
|
142
|
+
set rotationY(value) {
|
|
143
|
+
this.#turned(1, value);
|
|
144
|
+
}
|
|
145
|
+
get rotationZ() {
|
|
146
|
+
return this.#turn[2];
|
|
147
|
+
}
|
|
148
|
+
set rotationZ(value) {
|
|
149
|
+
this.#turned(2, value);
|
|
150
|
+
}
|
|
151
|
+
/** Its size as a share of the size it was made with; setting it sets all three of the ones below. */
|
|
152
|
+
get scale() {
|
|
153
|
+
return this.node.scale[0];
|
|
154
|
+
}
|
|
155
|
+
set scale(value) {
|
|
156
|
+
if (!finite(value))
|
|
157
|
+
return;
|
|
158
|
+
this.node.setScale(value);
|
|
159
|
+
}
|
|
160
|
+
get scaleX() {
|
|
161
|
+
return this.node.scale[0];
|
|
162
|
+
}
|
|
163
|
+
set scaleX(value) {
|
|
164
|
+
this.#size(0, value);
|
|
165
|
+
}
|
|
166
|
+
get scaleY() {
|
|
167
|
+
return this.node.scale[1];
|
|
168
|
+
}
|
|
169
|
+
set scaleY(value) {
|
|
170
|
+
this.#size(1, value);
|
|
171
|
+
}
|
|
172
|
+
get scaleZ() {
|
|
173
|
+
return this.node.scale[2];
|
|
174
|
+
}
|
|
175
|
+
set scaleZ(value) {
|
|
176
|
+
this.#size(2, value);
|
|
177
|
+
}
|
|
178
|
+
get visible() {
|
|
179
|
+
return this.node.visible;
|
|
180
|
+
}
|
|
181
|
+
set visible(value) {
|
|
182
|
+
this.node.visible = value === true;
|
|
183
|
+
}
|
|
184
|
+
/** Its colour, `0xRRGGBB`. What is not a colour is not taken; a model keeps the colours it has. */
|
|
185
|
+
get color() {
|
|
186
|
+
return this.#color;
|
|
187
|
+
}
|
|
188
|
+
set color(value) {
|
|
189
|
+
if (!colour(value) || this.#material === undefined)
|
|
190
|
+
return;
|
|
191
|
+
this.#color = value;
|
|
192
|
+
setColor3D(this.#material.color, value);
|
|
193
|
+
if (this.#glow > 0)
|
|
194
|
+
setColor3D(this.#material.emissive, value);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* How much it shines with its own colour, lit or not: 0 (the default) not at all, 1 as bright as its colour,
|
|
198
|
+
* more for a lamp or a screen. A thing that glows is not hidden by fog. A model keeps the materials it has.
|
|
199
|
+
*/
|
|
200
|
+
get glow() {
|
|
201
|
+
return this.#glow;
|
|
202
|
+
}
|
|
203
|
+
set glow(value) {
|
|
204
|
+
if (!finite(value) || value < 0 || this.#material === undefined)
|
|
205
|
+
return;
|
|
206
|
+
this.#glow = value;
|
|
207
|
+
setColor3D(this.#material.emissive, value > 0 ? this.#color : 0);
|
|
208
|
+
this.#material.emissiveIntensity = value;
|
|
209
|
+
// What glows is seen through the fog and the snow, as a lighthouse or a lantern is at night.
|
|
210
|
+
this.#material.fog = value === 0;
|
|
211
|
+
this.#blend();
|
|
212
|
+
}
|
|
213
|
+
/** A see-through thing that glows adds its light to what is behind it, as a beam does; any other mixes over it. */
|
|
214
|
+
#blend() {
|
|
215
|
+
if (this.#material === undefined)
|
|
216
|
+
return;
|
|
217
|
+
this.#material.blending = this.#glow > 0 && this.#material.opacity < 1 ? 'additive' : 'normal';
|
|
218
|
+
// Fog, blending and the alpha mode choose the shader, so a change of them is told to the material.
|
|
219
|
+
this.#material.touch();
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* How much of it is seen, 0 to 1: 1 (the default) is solid, less lets what is behind it show through, as glass, a
|
|
223
|
+
* ghost or, with `glow`, a beam of light, whose light is added to what is behind it. A see-through thing casts no
|
|
224
|
+
* shadow. A model keeps the materials it has.
|
|
225
|
+
*/
|
|
226
|
+
get opacity() {
|
|
227
|
+
return this.#material?.opacity ?? 1;
|
|
228
|
+
}
|
|
229
|
+
set opacity(value) {
|
|
230
|
+
if (!finite(value) || this.#material === undefined)
|
|
231
|
+
return;
|
|
232
|
+
const opacity = Math.min(1, Math.max(0, value)), solid = opacity >= 1;
|
|
233
|
+
this.#material.opacity = opacity;
|
|
234
|
+
this.#material.alphaMode = solid ? 'opaque' : 'blend';
|
|
235
|
+
this.#material.depthWrite = solid;
|
|
236
|
+
this.#blend();
|
|
237
|
+
if (this.node instanceof Mesh3D) {
|
|
238
|
+
// It keeps the shadow it had when it is made solid again.
|
|
239
|
+
if (!solid && this.#shadow === undefined) {
|
|
240
|
+
this.#shadow = this.node.castShadow;
|
|
241
|
+
this.node.castShadow = false;
|
|
242
|
+
}
|
|
243
|
+
else if (solid && this.#shadow !== undefined) {
|
|
244
|
+
this.node.castShadow = this.#shadow;
|
|
245
|
+
this.#shadow = undefined;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/** Whether `game.remove` has taken it out of the world. Setting it afterwards does nothing that is seen. */
|
|
250
|
+
get removed() {
|
|
251
|
+
return this.#removed;
|
|
252
|
+
}
|
|
253
|
+
/** Used by game removal and teardown; only resources made for this thing are retired. */
|
|
254
|
+
leave() {
|
|
255
|
+
if (this.#removed)
|
|
256
|
+
return;
|
|
257
|
+
this.#removed = true;
|
|
258
|
+
const geometry = ownedGeometry.get(this);
|
|
259
|
+
ownedGeometry.delete(this);
|
|
260
|
+
cleanUp([() => this.node.removeFromParent(), () => geometry?.dispose(), () => this.#material?.dispose()]);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* A lamp: light from a point of the world that fades out by `range`. It is placed and changed by plain numbers, and
|
|
265
|
+
* `game.remove` puts it out. Up to eight are lit fully; more are lit by a coarser way.
|
|
266
|
+
*/
|
|
267
|
+
export class PlayLamp3D {
|
|
268
|
+
node;
|
|
269
|
+
#color;
|
|
270
|
+
#removed = false;
|
|
271
|
+
constructor(color, intensity, range) {
|
|
272
|
+
this.#color = color;
|
|
273
|
+
this.node = createPointLight3D({ color, intensity, range });
|
|
274
|
+
}
|
|
275
|
+
get x() {
|
|
276
|
+
return this.node.position[0];
|
|
277
|
+
}
|
|
278
|
+
set x(value) {
|
|
279
|
+
if (finite(value))
|
|
280
|
+
this.node.setPosition(value, this.node.position[1], this.node.position[2]);
|
|
281
|
+
}
|
|
282
|
+
get y() {
|
|
283
|
+
return this.node.position[1];
|
|
284
|
+
}
|
|
285
|
+
set y(value) {
|
|
286
|
+
if (finite(value))
|
|
287
|
+
this.node.setPosition(this.node.position[0], value, this.node.position[2]);
|
|
288
|
+
}
|
|
289
|
+
get z() {
|
|
290
|
+
return this.node.position[2];
|
|
291
|
+
}
|
|
292
|
+
set z(value) {
|
|
293
|
+
if (finite(value))
|
|
294
|
+
this.node.setPosition(this.node.position[0], this.node.position[1], value);
|
|
295
|
+
}
|
|
296
|
+
get color() {
|
|
297
|
+
return this.#color;
|
|
298
|
+
}
|
|
299
|
+
set color(value) {
|
|
300
|
+
if (!colour(value))
|
|
301
|
+
return;
|
|
302
|
+
this.#color = value;
|
|
303
|
+
setColor3D(this.node.color, value);
|
|
304
|
+
}
|
|
305
|
+
/** How bright it is; 0 is dark. */
|
|
306
|
+
get intensity() {
|
|
307
|
+
return this.node.intensity;
|
|
308
|
+
}
|
|
309
|
+
set intensity(value) {
|
|
310
|
+
if (finite(value) && value >= 0)
|
|
311
|
+
this.node.intensity = value;
|
|
312
|
+
}
|
|
313
|
+
/** How far it reaches before it has faded out, in units of the world. */
|
|
314
|
+
get range() {
|
|
315
|
+
return this.node.range;
|
|
316
|
+
}
|
|
317
|
+
set range(value) {
|
|
318
|
+
if (above(value))
|
|
319
|
+
this.node.range = value;
|
|
320
|
+
}
|
|
321
|
+
get visible() {
|
|
322
|
+
return this.node.visible;
|
|
323
|
+
}
|
|
324
|
+
set visible(value) {
|
|
325
|
+
this.node.visible = value === true;
|
|
326
|
+
}
|
|
327
|
+
get removed() {
|
|
328
|
+
return this.#removed;
|
|
329
|
+
}
|
|
330
|
+
leave() {
|
|
331
|
+
if (this.#removed)
|
|
332
|
+
return;
|
|
333
|
+
this.#removed = true;
|
|
334
|
+
this.node.removeFromParent();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/** The looks every game is given, by name. */
|
|
338
|
+
export const PLAY_LOOKS_3D = Object.freeze({
|
|
339
|
+
day: {
|
|
340
|
+
background: 0x8ec5ff,
|
|
341
|
+
sky: 0xcfe3ff,
|
|
342
|
+
ground: 0x4a5a3a,
|
|
343
|
+
light: 0.9,
|
|
344
|
+
sun: 0xfff4e0,
|
|
345
|
+
sunIntensity: 2.4,
|
|
346
|
+
sunFrom: [8, 16, 10],
|
|
347
|
+
fog: 0.006
|
|
348
|
+
},
|
|
349
|
+
dusk: {
|
|
350
|
+
background: 0xd98a62,
|
|
351
|
+
sky: 0xffc18a,
|
|
352
|
+
ground: 0x2b2238,
|
|
353
|
+
light: 0.7,
|
|
354
|
+
sun: 0xff9a5a,
|
|
355
|
+
sunIntensity: 2,
|
|
356
|
+
sunFrom: [16, 5, 6],
|
|
357
|
+
fog: 0.018
|
|
358
|
+
},
|
|
359
|
+
night: {
|
|
360
|
+
background: 0x0b1020,
|
|
361
|
+
sky: 0x3a4a78,
|
|
362
|
+
ground: 0x08080c,
|
|
363
|
+
light: 0.22,
|
|
364
|
+
sun: 0x9fb8ff,
|
|
365
|
+
sunIntensity: 0.35,
|
|
366
|
+
sunFrom: [-6, 14, 8],
|
|
367
|
+
fog: 0.015
|
|
368
|
+
},
|
|
369
|
+
fog: {
|
|
370
|
+
background: 0xb8c0c8,
|
|
371
|
+
sky: 0xdfe5ea,
|
|
372
|
+
ground: 0x6b7076,
|
|
373
|
+
light: 0.9,
|
|
374
|
+
sun: 0xffffff,
|
|
375
|
+
sunIntensity: 1,
|
|
376
|
+
sunFrom: [8, 16, 10],
|
|
377
|
+
fog: 0.06
|
|
378
|
+
},
|
|
379
|
+
snow: {
|
|
380
|
+
background: 0xdfe8f2,
|
|
381
|
+
sky: 0xe6eef8,
|
|
382
|
+
ground: 0x8a96a6,
|
|
383
|
+
light: 0.7,
|
|
384
|
+
sun: 0xfff8f0,
|
|
385
|
+
sunIntensity: 1.4,
|
|
386
|
+
sunFrom: [8, 14, 12],
|
|
387
|
+
fog: 0.025
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
const PIECES = 96;
|
|
391
|
+
/** How deep a ground is under its top. */
|
|
392
|
+
const GROUND_DEPTH = 8;
|
|
393
|
+
/** The upright box of a thing in the world: its middle and half its size, scaled. */
|
|
394
|
+
function boxOf(thing) {
|
|
395
|
+
return [
|
|
396
|
+
thing.x + thing.middleX * thing.scaleX,
|
|
397
|
+
thing.y + thing.middleY * thing.scaleY,
|
|
398
|
+
thing.z + thing.middleZ * thing.scaleZ,
|
|
399
|
+
thing.halfX * Math.abs(thing.scaleX),
|
|
400
|
+
thing.halfY * Math.abs(thing.scaleY),
|
|
401
|
+
thing.halfZ * Math.abs(thing.scaleZ)
|
|
402
|
+
];
|
|
403
|
+
}
|
|
404
|
+
/** How far two things overlap on each axis; all three are above 0 when they overlap. */
|
|
405
|
+
export function overlap3D(a, b, shrink = 0) {
|
|
406
|
+
const p = boxOf(a), q = boxOf(b);
|
|
407
|
+
return [
|
|
408
|
+
p[3] + q[3] - 2 * shrink - Math.abs(p[0] - q[0]),
|
|
409
|
+
p[4] + q[4] - 2 * shrink - Math.abs(p[1] - q[1]),
|
|
410
|
+
p[5] + q[5] - 2 * shrink - Math.abs(p[2] - q[2])
|
|
411
|
+
];
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* The box of a model, in its own units and about its own place: half its size and where its middle is. Every mesh
|
|
415
|
+
* under it counts, placed as it is under it; the model's own place, turn and scale do not.
|
|
416
|
+
*/
|
|
417
|
+
export function modelBox3D(model) {
|
|
418
|
+
const low = [Infinity, Infinity, Infinity], high = [-Infinity, -Infinity, -Infinity], corner = new Float32Array(3);
|
|
419
|
+
const visit = (node, matrix) => {
|
|
420
|
+
const geometry = node.geometry;
|
|
421
|
+
if (geometry !== undefined && geometry.vertexCount > 0) {
|
|
422
|
+
const box = geometry.boundingBox;
|
|
423
|
+
for (let i = 0; i < 8; i++) {
|
|
424
|
+
corner[0] = i & 1 ? box[3] : box[0];
|
|
425
|
+
corner[1] = i & 2 ? box[4] : box[1];
|
|
426
|
+
corner[2] = i & 4 ? box[5] : box[2];
|
|
427
|
+
vec3TransformMat4(corner, corner, matrix);
|
|
428
|
+
for (let k = 0; k < 3; k++) {
|
|
429
|
+
low[k] = Math.min(low[k], corner[k]);
|
|
430
|
+
high[k] = Math.max(high[k], corner[k]);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
for (const child of node.children) {
|
|
435
|
+
child.updateWorldMatrix();
|
|
436
|
+
const placed = new Float32Array(16);
|
|
437
|
+
mat4Multiply(placed, matrix, child.matrix);
|
|
438
|
+
visit(child, placed);
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
visit(model, mat4Identity(new Float32Array(16)));
|
|
442
|
+
if (!(low[0] <= high[0]))
|
|
443
|
+
return { half: [0.5, 0.5, 0.5], middle: [0, 0, 0] };
|
|
444
|
+
return {
|
|
445
|
+
half: [(high[0] - low[0]) / 2, (high[1] - low[1]) / 2, (high[2] - low[2]) / 2],
|
|
446
|
+
middle: [(high[0] + low[0]) / 2, (high[1] + low[1]) / 2, (high[2] + low[2]) / 2]
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
/** The move that takes `a` out of `b` by the shortest way, or undefined when they are apart. */
|
|
450
|
+
export function pushOut3D(a, b) {
|
|
451
|
+
const [x, y, z] = overlap3D(a, b);
|
|
452
|
+
if (!(x > 0 && y > 0 && z > 0))
|
|
453
|
+
return undefined;
|
|
454
|
+
const p = boxOf(a), q = boxOf(b);
|
|
455
|
+
// Compare the same box centres as the overlap, including offset or mirrored models.
|
|
456
|
+
// Up or down first where the ways are as short as one another: a thing set down on a floor stands on it.
|
|
457
|
+
if (y <= x && y <= z)
|
|
458
|
+
return { x: 0, y: p[1] >= q[1] ? y : -y, z: 0 };
|
|
459
|
+
if (x <= z)
|
|
460
|
+
return { x: p[0] >= q[0] ? x : -x, y: 0, z: 0 };
|
|
461
|
+
return { x: 0, y: 0, z: p[2] >= q[2] ? z : -z };
|
|
462
|
+
}
|
|
463
|
+
function browserSurface(given, options) {
|
|
464
|
+
// The picture goes on a canvas of its own, under the game's, in the same place whatever the page does.
|
|
465
|
+
const over = given.canvas, holder = over.parentElement;
|
|
466
|
+
if (holder === null)
|
|
467
|
+
throw invalid('play3d needs its canvas in the page');
|
|
468
|
+
const page = over.ownerDocument, win = page.defaultView ?? globalThis;
|
|
469
|
+
const under = page.createElement('canvas');
|
|
470
|
+
under.style.cssText = 'position:absolute;left:0;top:0;width:0;height:0;display:block';
|
|
471
|
+
const initialPosition = over.style.position;
|
|
472
|
+
let observer;
|
|
473
|
+
const place = () => {
|
|
474
|
+
const outer = holder.getBoundingClientRect(), box = over.getBoundingClientRect();
|
|
475
|
+
under.style.left = box.left - outer.left + holder.scrollLeft + 'px';
|
|
476
|
+
under.style.top = box.top - outer.top + holder.scrollTop + 'px';
|
|
477
|
+
under.style.width = box.width + 'px';
|
|
478
|
+
under.style.height = box.height + 'px';
|
|
479
|
+
};
|
|
480
|
+
const unplace = () => cleanUp([() => win.removeEventListener('resize', place), () => observer?.disconnect()]);
|
|
481
|
+
try {
|
|
482
|
+
over.style.position = 'relative';
|
|
483
|
+
holder.insertBefore(under, over);
|
|
484
|
+
place();
|
|
485
|
+
const Resize = globalThis.ResizeObserver;
|
|
486
|
+
observer = Resize ? new Resize(place) : undefined;
|
|
487
|
+
observer?.observe(over);
|
|
488
|
+
win.addEventListener('resize', place);
|
|
489
|
+
const ready = createWorld3DApp(under, {
|
|
490
|
+
backend: options.backend === 'webgpu' ? 'webgpu' : 'webgl2',
|
|
491
|
+
...(options.onError === undefined ? {} : { onError: options.onError })
|
|
492
|
+
});
|
|
493
|
+
return { ready, unplace, remove: () => under.remove() };
|
|
494
|
+
}
|
|
495
|
+
catch (cause) {
|
|
496
|
+
// The caller can only retire a surface after we return it. Restore the DOM
|
|
497
|
+
// here even if a host listener or observer throws during registration.
|
|
498
|
+
try {
|
|
499
|
+
cleanUp([
|
|
500
|
+
unplace,
|
|
501
|
+
() => under.remove(),
|
|
502
|
+
() => {
|
|
503
|
+
over.style.position = initialPosition;
|
|
504
|
+
}
|
|
505
|
+
]);
|
|
506
|
+
}
|
|
507
|
+
catch (cleanup) {
|
|
508
|
+
throw new AggregateError([cause, cleanup], 'Play3D surface mount and rollback failed', { cause });
|
|
509
|
+
}
|
|
510
|
+
throw cause;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
/** Starts a 3D game in the page. See the guide `play3d.md`. */
|
|
514
|
+
export function play3d(options) {
|
|
515
|
+
return createPlay3DSession(options, play, browserSurface);
|
|
516
|
+
}
|
|
517
|
+
/** Internal host entry. The flat session owns input and UI; the surface presents the original scene. */
|
|
518
|
+
export async function createPlay3DSession(options, startFlat, attach) {
|
|
519
|
+
if (options === null || typeof options !== 'object')
|
|
520
|
+
throw invalid('play3d takes an object of options');
|
|
521
|
+
const { setup, update, background = 0x0b1020 } = options;
|
|
522
|
+
if (!colour(background))
|
|
523
|
+
throw invalid('background is a colour from 0 to 0xffffff');
|
|
524
|
+
if (setup !== undefined && typeof setup !== 'function')
|
|
525
|
+
throw invalid('setup is a function');
|
|
526
|
+
if (update !== undefined && typeof update !== 'function')
|
|
527
|
+
throw invalid('update is a function');
|
|
528
|
+
if (options.canvas !== undefined)
|
|
529
|
+
throw invalid('play3d makes its own canvases');
|
|
530
|
+
let game, app, creatingApp, surface;
|
|
531
|
+
let clearing = false, closing = false, generation = 0;
|
|
532
|
+
let disposal;
|
|
533
|
+
const admit = () => {
|
|
534
|
+
if (clearing || closing)
|
|
535
|
+
throw invalid('the 3D world is clearing or disposed');
|
|
536
|
+
};
|
|
537
|
+
const beginClosing = () => {
|
|
538
|
+
if (closing)
|
|
539
|
+
return;
|
|
540
|
+
closing = true;
|
|
541
|
+
generation++;
|
|
542
|
+
};
|
|
543
|
+
const scene = new Scene3D();
|
|
544
|
+
scene.background = linearColor3D(background);
|
|
545
|
+
const eye = createPerspectiveCamera3D({ fov: 55, near: 0.1, far: 500 });
|
|
546
|
+
const sky = createHemisphereLight3D({ color: 0xcfe3ff, groundColor: 0x3a3f4a, intensity: 0.9 });
|
|
547
|
+
const sun = createDirectionalLight3D({ color: 0xfff4e0, intensity: 2.4, castShadow: true });
|
|
548
|
+
sun.shadow.extent = 24;
|
|
549
|
+
sun.shadow.far = 120;
|
|
550
|
+
const things = new Set();
|
|
551
|
+
// The parts of each group, which leave with it; and the lamps that are lit.
|
|
552
|
+
const groups = new WeakMap(), grouped = new WeakSet();
|
|
553
|
+
const lamps = new Set();
|
|
554
|
+
// What falls, made when first asked for and kept over what the camera looks at.
|
|
555
|
+
// A soft round dot that every flake and drop is drawn with, made when first needed.
|
|
556
|
+
let dot, streak;
|
|
557
|
+
const dotTexture = () => {
|
|
558
|
+
if (dot !== undefined)
|
|
559
|
+
return dot;
|
|
560
|
+
const size = 16, data = new Uint8Array(4 * size * size);
|
|
561
|
+
for (let y = 0; y < size; y++)
|
|
562
|
+
for (let x = 0; x < size; x++) {
|
|
563
|
+
const d = Math.hypot(x + 0.5 - size / 2, y + 0.5 - size / 2) / (size / 2);
|
|
564
|
+
data.set([255, 255, 255, Math.round(255 * Math.max(0, Math.min(1, (1 - d) * 2.5)))], 4 * (y * size + x));
|
|
565
|
+
}
|
|
566
|
+
return (dot = new Texture3D({ width: size, height: size, data }, { wrapS: 'clamp', wrapT: 'clamp' }));
|
|
567
|
+
};
|
|
568
|
+
// A drop of rain is seen as a streak: a thin upright line, brightest in its middle and fading to its ends.
|
|
569
|
+
const streakTexture = () => {
|
|
570
|
+
if (streak !== undefined)
|
|
571
|
+
return streak;
|
|
572
|
+
const width = 8, height = 32, data = new Uint8Array(4 * width * height);
|
|
573
|
+
for (let y = 0; y < height; y++)
|
|
574
|
+
for (let x = 0; x < width; x++) {
|
|
575
|
+
const across = Math.max(0, 1 - Math.abs(x + 0.5 - width / 2) / 1.5), along = Math.sin((Math.PI * (y + 0.5)) / height);
|
|
576
|
+
data.set([255, 255, 255, Math.round(255 * Math.min(1, across) * along)], 4 * (y * width + x));
|
|
577
|
+
}
|
|
578
|
+
return (streak = new Texture3D({ width, height, data }, { wrapS: 'clamp', wrapT: 'clamp' }));
|
|
579
|
+
};
|
|
580
|
+
let falling, fallingKind = 'none', fallingAmount = 1;
|
|
581
|
+
const fall = (kind, amount) => {
|
|
582
|
+
if (kind === fallingKind && amount === fallingAmount)
|
|
583
|
+
return;
|
|
584
|
+
falling?.removeFromParent();
|
|
585
|
+
falling?.dispose();
|
|
586
|
+
falling = undefined;
|
|
587
|
+
fallingKind = kind;
|
|
588
|
+
fallingAmount = amount;
|
|
589
|
+
if (kind === 'none' || amount === 0)
|
|
590
|
+
return;
|
|
591
|
+
const snow = kind === 'snow';
|
|
592
|
+
// A box of sky over what is looked at; flakes are left where they fall as the camera moves.
|
|
593
|
+
falling = new GpuParticles3D({
|
|
594
|
+
count: Math.max(1, Math.round((snow ? 1600 : 2400) * amount)),
|
|
595
|
+
lifetime: snow ? [5, 7] : [0.9, 1.1],
|
|
596
|
+
speed: snow ? [0.4, 0.9] : [16, 20],
|
|
597
|
+
direction: [snow ? 0.15 : 0.05, -1, 0],
|
|
598
|
+
spread: snow ? 0.5 : 0.04,
|
|
599
|
+
gravity: [0, snow ? -0.4 : -6, 0],
|
|
600
|
+
startSize: snow ? [0.12, 0.22] : [0.3, 0.42],
|
|
601
|
+
startColor: snow ? 0xffffff : 0xa8bcd8,
|
|
602
|
+
startOpacity: snow ? 0.95 : 0.55,
|
|
603
|
+
endOpacity: snow ? 0.9 : 0.5,
|
|
604
|
+
fadeIn: 0.05,
|
|
605
|
+
fadeOut: 0.15,
|
|
606
|
+
shape: 'box',
|
|
607
|
+
extents: [22, 0.5, 22],
|
|
608
|
+
loop: true,
|
|
609
|
+
space: 'world',
|
|
610
|
+
seed: 7,
|
|
611
|
+
map: snow ? dotTexture() : streakTexture()
|
|
612
|
+
});
|
|
613
|
+
falling.castShadow = false;
|
|
614
|
+
falling.receiveShadow = false;
|
|
615
|
+
falling.setPosition(aim[0], aim[1] + (snow ? 9 : 16), aim[2]);
|
|
616
|
+
scene.addChild(falling);
|
|
617
|
+
};
|
|
618
|
+
// The camera: where the game put it, what it looks at, and how far a shake has it out of place.
|
|
619
|
+
const base = [0, 6, 12], aim = [0, 0, 0], shaken = [0, 0, 0];
|
|
620
|
+
let followed, turning = false, lookAbove = 0, offset = [0, 4, 8], smooth = 6, shakeAmount = 0, shakeLeft = 0, shakeSpan = 1;
|
|
621
|
+
// The side the sun shines from; a look changes it.
|
|
622
|
+
const sunFrom = [8, 16, 10];
|
|
623
|
+
const look = (given) => {
|
|
624
|
+
const chosen = typeof given === 'string' ? PLAY_LOOKS_3D[given] : given;
|
|
625
|
+
if (chosen === undefined || chosen === null || typeof chosen !== 'object')
|
|
626
|
+
throw invalid("look is 'day', 'dusk', 'night', 'fog' or 'snow', or { background, sky, ground, sun, fog }");
|
|
627
|
+
const hex = (value) => Number.isInteger(value) && value >= 0 && value <= 0xffffff;
|
|
628
|
+
// A strength given a colour, as 0x2a3550 for `light`, would light the world white: it is refused instead.
|
|
629
|
+
for (const key of ['light', 'sunIntensity'])
|
|
630
|
+
if (chosen[key] !== undefined && !(finite(chosen[key]) && chosen[key] >= 0 && chosen[key] <= 16))
|
|
631
|
+
throw invalid(key +
|
|
632
|
+
' is how strong the light is, 0 to 16 (night 0.22, day 0.9); its colour is ' +
|
|
633
|
+
(key === 'light' ? 'sky' : 'sun'));
|
|
634
|
+
if (hex(chosen.background)) {
|
|
635
|
+
scene.background = linearColor3D(chosen.background);
|
|
636
|
+
if (scene.fog !== undefined)
|
|
637
|
+
scene.fog = { ...scene.fog, color: linearColor3D(chosen.background) };
|
|
638
|
+
}
|
|
639
|
+
if (hex(chosen.sky))
|
|
640
|
+
setColor3D(sky.color, chosen.sky);
|
|
641
|
+
if (hex(chosen.ground))
|
|
642
|
+
setColor3D(sky.groundColor, chosen.ground);
|
|
643
|
+
if (finite(chosen.light) && chosen.light >= 0)
|
|
644
|
+
sky.intensity = chosen.light;
|
|
645
|
+
if (hex(chosen.sun))
|
|
646
|
+
setColor3D(sun.color, chosen.sun);
|
|
647
|
+
if (finite(chosen.sunIntensity) && chosen.sunIntensity >= 0)
|
|
648
|
+
sun.intensity = chosen.sunIntensity;
|
|
649
|
+
const from = chosen.sunFrom;
|
|
650
|
+
if (Array.isArray(from) && from.length === 3 && from.every(finite) && from[1] > 0) {
|
|
651
|
+
sunFrom[0] = from[0];
|
|
652
|
+
sunFrom[1] = from[1];
|
|
653
|
+
sunFrom[2] = from[2];
|
|
654
|
+
}
|
|
655
|
+
if (finite(chosen.fog) && chosen.fog >= 0) {
|
|
656
|
+
const color = Array.isArray(scene.background)
|
|
657
|
+
? scene.background
|
|
658
|
+
: linearColor3D(background);
|
|
659
|
+
scene.fog =
|
|
660
|
+
chosen.fog === 0
|
|
661
|
+
? undefined
|
|
662
|
+
: { kind: 'exponential', color: [...color], density: chosen.fog };
|
|
663
|
+
}
|
|
664
|
+
settle();
|
|
665
|
+
};
|
|
666
|
+
const settle = () => {
|
|
667
|
+
eye.setPosition(base[0] + shaken[0], base[1] + shaken[1], base[2] + shaken[2]);
|
|
668
|
+
eye.lookAt(aim[0], aim[1], aim[2]);
|
|
669
|
+
// The sun keeps its direction and stays over what is looked at, so its shadows are there.
|
|
670
|
+
sun.setPosition(aim[0] + sunFrom[0], aim[1] + sunFrom[1], aim[2] + sunFrom[2]);
|
|
671
|
+
sun.lookAt(aim[0], aim[1], aim[2]);
|
|
672
|
+
};
|
|
673
|
+
const camera = {
|
|
674
|
+
get x() {
|
|
675
|
+
return base[0];
|
|
676
|
+
},
|
|
677
|
+
set x(value) {
|
|
678
|
+
if (finite(value))
|
|
679
|
+
base[0] = value;
|
|
680
|
+
},
|
|
681
|
+
get y() {
|
|
682
|
+
return base[1];
|
|
683
|
+
},
|
|
684
|
+
set y(value) {
|
|
685
|
+
if (finite(value))
|
|
686
|
+
base[1] = value;
|
|
687
|
+
},
|
|
688
|
+
get z() {
|
|
689
|
+
return base[2];
|
|
690
|
+
},
|
|
691
|
+
set z(value) {
|
|
692
|
+
if (finite(value))
|
|
693
|
+
base[2] = value;
|
|
694
|
+
},
|
|
695
|
+
get fov() {
|
|
696
|
+
return eye.fov;
|
|
697
|
+
},
|
|
698
|
+
set fov(value) {
|
|
699
|
+
if (finite(value) && value > 1 && value < 170)
|
|
700
|
+
eye.fov = value;
|
|
701
|
+
},
|
|
702
|
+
lookAt(x, y, z) {
|
|
703
|
+
if (!finite(x) || !finite(y) || !finite(z))
|
|
704
|
+
return;
|
|
705
|
+
aim[0] = x;
|
|
706
|
+
aim[1] = y;
|
|
707
|
+
aim[2] = z;
|
|
708
|
+
},
|
|
709
|
+
node: eye
|
|
710
|
+
};
|
|
711
|
+
// Pieces of a burst: a few small boxes, made once and used again.
|
|
712
|
+
const pieceShape = createBoxGeometry3D({ width: 1, height: 1, depth: 1 });
|
|
713
|
+
const pieces = [];
|
|
714
|
+
let nextPiece = 0;
|
|
715
|
+
const make = (geometry, color, half, at) => {
|
|
716
|
+
admit();
|
|
717
|
+
if (!colour(color))
|
|
718
|
+
throw invalid('a colour is a number from 0 to 0xffffff');
|
|
719
|
+
if (at === null || typeof at !== 'object')
|
|
720
|
+
throw invalid('a place is an object with x, y and z');
|
|
721
|
+
const { x, y, z } = at;
|
|
722
|
+
// Read caller properties before allocating, then recheck in case a getter began teardown.
|
|
723
|
+
admit();
|
|
724
|
+
const thing = new PlayThing3D(geometry(), color, half);
|
|
725
|
+
thing.node.setPosition(finite(x) ? x : 0, finite(y) ? y : 0, finite(z) ? z : 0);
|
|
726
|
+
scene.addChild(thing.node);
|
|
727
|
+
things.add(thing);
|
|
728
|
+
return thing;
|
|
729
|
+
};
|
|
730
|
+
const isThing = (body) => body instanceof PlayThing3D;
|
|
731
|
+
// Taps on things: found by a ray from the camera through the pointer when it goes down.
|
|
732
|
+
const taps = new Map(), ray = new Raycaster3D();
|
|
733
|
+
// A tap reports errors to the scene that received it. A returned restart may reject after that scene is gone.
|
|
734
|
+
let tapErrors = {};
|
|
735
|
+
const tapped = (flat) => {
|
|
736
|
+
if (taps.size === 0 || !flat.pointer.pressed)
|
|
737
|
+
return;
|
|
738
|
+
for (const thing of taps.keys())
|
|
739
|
+
if (thing.removed)
|
|
740
|
+
taps.delete(thing);
|
|
741
|
+
eye.updateWorldMatrices();
|
|
742
|
+
scene.updateWorldMatrices();
|
|
743
|
+
ray.setFromCamera((flat.pointer.x / flat.width) * 2 - 1, 1 - (flat.pointer.y / flat.height) * 2, eye);
|
|
744
|
+
let hits;
|
|
745
|
+
try {
|
|
746
|
+
hits = ray.intersect(scene);
|
|
747
|
+
}
|
|
748
|
+
catch {
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
for (const hit of hits) {
|
|
752
|
+
// The thing a mesh that was hit belongs to: the nearest one up its line that has a tap.
|
|
753
|
+
for (let node = hit.object; node !== undefined; node = node.parent) {
|
|
754
|
+
const thing = [...taps.keys()].find(each => each.node === node);
|
|
755
|
+
if (thing === undefined || !thing.visible)
|
|
756
|
+
continue;
|
|
757
|
+
const callback = taps.get(thing);
|
|
758
|
+
const owner = tapErrors;
|
|
759
|
+
const fail = (cause) => {
|
|
760
|
+
owner.failure ??= { cause };
|
|
761
|
+
};
|
|
762
|
+
try {
|
|
763
|
+
const result = callback();
|
|
764
|
+
if (result !== null && result !== undefined && typeof result.then === 'function')
|
|
765
|
+
result.then(undefined, fail);
|
|
766
|
+
}
|
|
767
|
+
catch (cause) {
|
|
768
|
+
fail(cause);
|
|
769
|
+
}
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
const clear = () => {
|
|
775
|
+
if (clearing)
|
|
776
|
+
throw invalid('the 3D world is clearing or disposed');
|
|
777
|
+
clearing = true;
|
|
778
|
+
generation++;
|
|
779
|
+
try {
|
|
780
|
+
const leaving = [...things], unlit = [...lamps];
|
|
781
|
+
things.clear();
|
|
782
|
+
lamps.clear();
|
|
783
|
+
taps.clear();
|
|
784
|
+
tapErrors = {};
|
|
785
|
+
for (const piece of pieces) {
|
|
786
|
+
piece.life = 0;
|
|
787
|
+
piece.thing.visible = false;
|
|
788
|
+
}
|
|
789
|
+
followed = undefined;
|
|
790
|
+
base[0] = 0;
|
|
791
|
+
base[1] = 6;
|
|
792
|
+
base[2] = 12;
|
|
793
|
+
aim[0] = aim[1] = aim[2] = 0;
|
|
794
|
+
shakeLeft = 0;
|
|
795
|
+
shaken[0] = shaken[1] = shaken[2] = 0;
|
|
796
|
+
cleanUp([...leaving.map(thing => () => thing.leave()), ...unlit.map(lamp => () => lamp.leave())]);
|
|
797
|
+
}
|
|
798
|
+
finally {
|
|
799
|
+
clearing = false;
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
const disposeWorld = async () => {
|
|
803
|
+
beginClosing();
|
|
804
|
+
const failures = [];
|
|
805
|
+
// Join only the SDK factory. Application setup may still be waiting on an unrelated download.
|
|
806
|
+
if (creatingApp !== undefined) {
|
|
807
|
+
try {
|
|
808
|
+
await creatingApp;
|
|
809
|
+
}
|
|
810
|
+
catch (cause) {
|
|
811
|
+
failures.push(cause);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
try {
|
|
815
|
+
cleanUp([
|
|
816
|
+
clear,
|
|
817
|
+
...pieces.map(piece => () => piece.thing.leave()),
|
|
818
|
+
() => pieceShape.dispose(),
|
|
819
|
+
() => fall('none', 0),
|
|
820
|
+
() => dot?.dispose(),
|
|
821
|
+
() => streak?.dispose(),
|
|
822
|
+
() => surface?.unplace?.(),
|
|
823
|
+
() => app?.dispose(),
|
|
824
|
+
() => surface?.remove?.()
|
|
825
|
+
]);
|
|
826
|
+
}
|
|
827
|
+
catch (cause) {
|
|
828
|
+
failures.push(cause);
|
|
829
|
+
}
|
|
830
|
+
try {
|
|
831
|
+
// World3DApp is a synchronous Disposable. Its Engine owns the shared asynchronous close result.
|
|
832
|
+
await app?.engine.dispose();
|
|
833
|
+
}
|
|
834
|
+
catch (cause) {
|
|
835
|
+
failures.push(cause);
|
|
836
|
+
}
|
|
837
|
+
throwCleanupFailures(failures);
|
|
838
|
+
};
|
|
839
|
+
// Where the camera keeps to by a thing: its offset, turned with the thing when asked.
|
|
840
|
+
const placeBy = (thing) => {
|
|
841
|
+
let [x, y, z] = offset;
|
|
842
|
+
if (turning) {
|
|
843
|
+
const turn = thing.rotationY * RADIANS, c = Math.cos(turn), s = Math.sin(turn);
|
|
844
|
+
[x, z] = [x * c + z * s, -x * s + z * c];
|
|
845
|
+
}
|
|
846
|
+
return [thing.x + x, thing.y + y, thing.z + z];
|
|
847
|
+
};
|
|
848
|
+
const after = (seconds) => {
|
|
849
|
+
if (followed !== undefined && !followed.removed) {
|
|
850
|
+
const share = smooth > 0 ? 1 - Math.exp(-smooth * seconds) : 1, [x, y, z] = placeBy(followed);
|
|
851
|
+
base[0] = base[0] + (x - base[0]) * share;
|
|
852
|
+
base[1] = base[1] + (y - base[1]) * share;
|
|
853
|
+
base[2] = base[2] + (z - base[2]) * share;
|
|
854
|
+
aim[0] = followed.x;
|
|
855
|
+
aim[1] = followed.y + lookAbove;
|
|
856
|
+
aim[2] = followed.z;
|
|
857
|
+
}
|
|
858
|
+
if (shakeLeft > 0) {
|
|
859
|
+
shakeLeft -= seconds;
|
|
860
|
+
const reach = shakeLeft > 0 ? shakeAmount * (shakeLeft / shakeSpan) : 0;
|
|
861
|
+
for (let i = 0; i < 3; i++)
|
|
862
|
+
shaken[i] = (Math.random() * 2 - 1) * reach;
|
|
863
|
+
}
|
|
864
|
+
if (falling !== undefined) {
|
|
865
|
+
falling.setPosition(aim[0], aim[1] + (fallingKind === 'snow' ? 9 : 16), aim[2]);
|
|
866
|
+
falling.update(seconds);
|
|
867
|
+
}
|
|
868
|
+
for (const piece of pieces) {
|
|
869
|
+
if (piece.life <= 0)
|
|
870
|
+
continue;
|
|
871
|
+
piece.life -= seconds;
|
|
872
|
+
if (piece.life <= 0) {
|
|
873
|
+
piece.thing.visible = false;
|
|
874
|
+
continue;
|
|
875
|
+
}
|
|
876
|
+
piece.vy -= piece.gravity * seconds;
|
|
877
|
+
piece.thing.x += piece.vx * seconds;
|
|
878
|
+
piece.thing.y += piece.vy * seconds;
|
|
879
|
+
piece.thing.z += piece.vz * seconds;
|
|
880
|
+
piece.thing.scale = piece.size * (piece.life / piece.span);
|
|
881
|
+
}
|
|
882
|
+
settle();
|
|
883
|
+
};
|
|
884
|
+
const extend = (flat) => {
|
|
885
|
+
const added = {
|
|
886
|
+
look,
|
|
887
|
+
box(width, height, depth, color, at = {}) {
|
|
888
|
+
admit();
|
|
889
|
+
if (!above(width) || !above(height) || !above(depth))
|
|
890
|
+
throw invalid('a box has a width, height and depth above 0');
|
|
891
|
+
return make(() => createBoxGeometry3D({ width, height, depth }), color, [width / 2, height / 2, depth / 2], at);
|
|
892
|
+
},
|
|
893
|
+
sphere(radius, color, at = {}) {
|
|
894
|
+
admit();
|
|
895
|
+
if (!above(radius))
|
|
896
|
+
throw invalid('a sphere has a radius above 0');
|
|
897
|
+
return make(() => createSphereGeometry3D({ radius, widthSegments: 24, heightSegments: 16 }), color, [radius, radius, radius], at);
|
|
898
|
+
},
|
|
899
|
+
cylinder(radius, height, color, at = {}) {
|
|
900
|
+
admit();
|
|
901
|
+
if (!above(radius) || !above(height))
|
|
902
|
+
throw invalid('a cylinder has a radius and a height above 0');
|
|
903
|
+
return make(() => createCylinderGeometry3D({ radiusTop: radius, radiusBottom: radius, height, radialSegments: 24 }), color, [radius, height / 2, radius], at);
|
|
904
|
+
},
|
|
905
|
+
cone(radius, height, color, at = {}) {
|
|
906
|
+
admit();
|
|
907
|
+
if (!above(radius) || !above(height))
|
|
908
|
+
throw invalid('a cone has a radius and a height above 0');
|
|
909
|
+
return make(() => createCylinderGeometry3D({ radiusTop: 0, radiusBottom: radius, height, radialSegments: 24 }), color, [radius, height / 2, radius], at);
|
|
910
|
+
},
|
|
911
|
+
ground(size, color, y = 0) {
|
|
912
|
+
admit();
|
|
913
|
+
if (!above(size) || !finite(y))
|
|
914
|
+
throw invalid('a ground has a size above 0');
|
|
915
|
+
// Deep, so that a thing falling fast in a slow frame is still pushed back up out of it and not through it.
|
|
916
|
+
const thing = make(() => createBoxGeometry3D({ width: size, height: GROUND_DEPTH, depth: size }), color, [size / 2, GROUND_DEPTH / 2, size / 2], {
|
|
917
|
+
y: y - GROUND_DEPTH / 2
|
|
918
|
+
});
|
|
919
|
+
thing.node.castShadow = false;
|
|
920
|
+
return thing;
|
|
921
|
+
},
|
|
922
|
+
add(model, at = {}) {
|
|
923
|
+
admit();
|
|
924
|
+
if (!(model instanceof Object3D))
|
|
925
|
+
throw invalid('add takes a mesh, a group or a loaded model');
|
|
926
|
+
if (at === null || typeof at !== 'object')
|
|
927
|
+
throw invalid('a place is an object with x, y and z');
|
|
928
|
+
const { x, y, z } = at;
|
|
929
|
+
admit();
|
|
930
|
+
const { half, middle } = modelBox3D(model);
|
|
931
|
+
admit();
|
|
932
|
+
if (model.parent !== undefined)
|
|
933
|
+
model.removeFromParent();
|
|
934
|
+
const thing = new PlayThing3D(model, 0xffffff, half, middle);
|
|
935
|
+
if (finite(x))
|
|
936
|
+
thing.x = x;
|
|
937
|
+
if (finite(y))
|
|
938
|
+
thing.y = y;
|
|
939
|
+
if (finite(z))
|
|
940
|
+
thing.z = z;
|
|
941
|
+
scene.addChild(model);
|
|
942
|
+
things.add(thing);
|
|
943
|
+
return thing;
|
|
944
|
+
},
|
|
945
|
+
onTap(target, callback) {
|
|
946
|
+
if (typeof callback !== 'function')
|
|
947
|
+
throw invalid('onTap takes a thing or a sprite, and a function');
|
|
948
|
+
if (!isThing(target)) {
|
|
949
|
+
flat.onTap(target, callback);
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
taps.set(target, callback);
|
|
953
|
+
},
|
|
954
|
+
remove(body) {
|
|
955
|
+
if (body instanceof PlayLamp3D) {
|
|
956
|
+
lamps.delete(body);
|
|
957
|
+
body.leave();
|
|
958
|
+
}
|
|
959
|
+
else if (isThing(body)) {
|
|
960
|
+
things.delete(body);
|
|
961
|
+
taps.delete(body);
|
|
962
|
+
if (followed === body)
|
|
963
|
+
followed = undefined;
|
|
964
|
+
body.leave();
|
|
965
|
+
for (const part of groups.get(body) ?? [])
|
|
966
|
+
whole.remove(part);
|
|
967
|
+
}
|
|
968
|
+
else
|
|
969
|
+
flat.remove(body);
|
|
970
|
+
},
|
|
971
|
+
group(parts, at = {}) {
|
|
972
|
+
admit();
|
|
973
|
+
if (!Array.isArray(parts) || parts.length === 0 || !parts.every(isThing))
|
|
974
|
+
throw invalid('group takes a list of things');
|
|
975
|
+
if (parts.some(part => part.removed || !things.has(part) || grouped.has(part)) ||
|
|
976
|
+
new Set(parts).size !== parts.length)
|
|
977
|
+
throw invalid('a group is made of things of the world not in a group, each once');
|
|
978
|
+
if (at === null || typeof at !== 'object')
|
|
979
|
+
throw invalid('a place is an object with x, y and z');
|
|
980
|
+
const { x, y, z } = at;
|
|
981
|
+
admit();
|
|
982
|
+
// The box of the parts where they are, as the group's own.
|
|
983
|
+
const low = [Infinity, Infinity, Infinity], high = [-Infinity, -Infinity, -Infinity];
|
|
984
|
+
for (const part of parts) {
|
|
985
|
+
const box = boxOf(part);
|
|
986
|
+
for (let k = 0; k < 3; k++) {
|
|
987
|
+
low[k] = Math.min(low[k], box[k] - box[k + 3]);
|
|
988
|
+
high[k] = Math.max(high[k], box[k] + box[k + 3]);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
const node = new Object3D();
|
|
992
|
+
for (const part of parts) {
|
|
993
|
+
part.node.removeFromParent();
|
|
994
|
+
node.addChild(part.node);
|
|
995
|
+
}
|
|
996
|
+
const thing = new PlayThing3D(node, 0xffffff, [0, 1, 2].map(k => (high[k] - low[k]) / 2), [0, 1, 2].map(k => (high[k] + low[k]) / 2));
|
|
997
|
+
node.setPosition(finite(x) ? x : 0, finite(y) ? y : 0, finite(z) ? z : 0);
|
|
998
|
+
scene.addChild(node);
|
|
999
|
+
things.add(thing);
|
|
1000
|
+
groups.set(thing, [...parts]);
|
|
1001
|
+
for (const part of parts)
|
|
1002
|
+
grouped.add(part);
|
|
1003
|
+
return thing;
|
|
1004
|
+
},
|
|
1005
|
+
lamp(color, at = {}, o = {}) {
|
|
1006
|
+
admit();
|
|
1007
|
+
if (!colour(color))
|
|
1008
|
+
throw invalid('a colour is a number from 0 to 0xffffff');
|
|
1009
|
+
if (at === null || typeof at !== 'object')
|
|
1010
|
+
throw invalid('a place is an object with x, y and z');
|
|
1011
|
+
if (o === null || typeof o !== 'object')
|
|
1012
|
+
throw invalid('a lamp takes { intensity, range }');
|
|
1013
|
+
const { x, y, z } = at;
|
|
1014
|
+
const intensity = finite(o.intensity) && o.intensity >= 0 ? o.intensity : 4, range = above(o.range) ? o.range : 8;
|
|
1015
|
+
admit();
|
|
1016
|
+
const lamp = new PlayLamp3D(color, intensity, range);
|
|
1017
|
+
lamp.node.setPosition(finite(x) ? x : 0, finite(y) ? y : 0, finite(z) ? z : 0);
|
|
1018
|
+
scene.addChild(lamp.node);
|
|
1019
|
+
lamps.add(lamp);
|
|
1020
|
+
return lamp;
|
|
1021
|
+
},
|
|
1022
|
+
weather(kind, o = {}) {
|
|
1023
|
+
admit();
|
|
1024
|
+
if (kind !== 'snow' && kind !== 'rain' && kind !== 'none')
|
|
1025
|
+
throw invalid("weather is 'snow', 'rain' or 'none'");
|
|
1026
|
+
if (o === null || typeof o !== 'object')
|
|
1027
|
+
throw invalid('weather takes { amount }');
|
|
1028
|
+
const amount = finite(o.amount) ? Math.max(0, Math.min(3, o.amount)) : 1;
|
|
1029
|
+
admit();
|
|
1030
|
+
fall(kind, amount);
|
|
1031
|
+
},
|
|
1032
|
+
near(a, b, distance) {
|
|
1033
|
+
if (!isThing(a) || !isThing(b))
|
|
1034
|
+
return !isThing(a) && !isThing(b) && flat.near(a, b, distance ?? 0);
|
|
1035
|
+
if (a.removed || b.removed || !a.visible || !b.visible)
|
|
1036
|
+
return false;
|
|
1037
|
+
const reach = distance ??
|
|
1038
|
+
Math.max(a.halfX * Math.abs(a.scaleX), a.halfZ * Math.abs(a.scaleZ)) +
|
|
1039
|
+
Math.max(b.halfX * Math.abs(b.scaleX), b.halfZ * Math.abs(b.scaleZ));
|
|
1040
|
+
const dx = a.x - b.x, dy = a.y - b.y, dz = a.z - b.z;
|
|
1041
|
+
return dx * dx + dy * dy + dz * dz <= reach * reach;
|
|
1042
|
+
},
|
|
1043
|
+
overlaps(a, b, shrink = 0) {
|
|
1044
|
+
if (!isThing(a) || !isThing(b))
|
|
1045
|
+
return !isThing(a) && !isThing(b) && flat.overlaps(a, b, shrink);
|
|
1046
|
+
if (a === b || a.removed || b.removed || !a.visible || !b.visible)
|
|
1047
|
+
return false;
|
|
1048
|
+
const [x, y, z] = overlap3D(a, b, finite(shrink) ? shrink : 0);
|
|
1049
|
+
return x > 0 && y > 0 && z > 0;
|
|
1050
|
+
},
|
|
1051
|
+
hits(a, others, shrink = 0) {
|
|
1052
|
+
for (const other of others)
|
|
1053
|
+
if (other !== a && whole.overlaps(a, other, shrink))
|
|
1054
|
+
return other;
|
|
1055
|
+
return undefined;
|
|
1056
|
+
},
|
|
1057
|
+
hitsAll(a, others, shrink = 0) {
|
|
1058
|
+
return others.filter(other => other !== a && whole.overlaps(a, other, shrink));
|
|
1059
|
+
},
|
|
1060
|
+
pushOut(a, b) {
|
|
1061
|
+
if (!isThing(a) || !isThing(b) || a === b || a.removed || b.removed || !a.visible || !b.visible)
|
|
1062
|
+
return undefined;
|
|
1063
|
+
return pushOut3D(a, b);
|
|
1064
|
+
},
|
|
1065
|
+
follow(thing, where = {}) {
|
|
1066
|
+
if (thing !== undefined && !isThing(thing))
|
|
1067
|
+
throw invalid('follow takes a thing of the world');
|
|
1068
|
+
followed = thing;
|
|
1069
|
+
if (thing === undefined)
|
|
1070
|
+
return;
|
|
1071
|
+
offset = [finite(where.x) ? where.x : 0, finite(where.y) ? where.y : 4, finite(where.z) ? where.z : 8];
|
|
1072
|
+
smooth = finite(where.smooth) && where.smooth >= 0 ? where.smooth : 6;
|
|
1073
|
+
turning = where.turn === true;
|
|
1074
|
+
lookAbove = finite(where.lookY) ? where.lookY : 0;
|
|
1075
|
+
// It starts where it will stay, and does not fly in from where the camera was.
|
|
1076
|
+
[base[0], base[1], base[2]] = placeBy(thing);
|
|
1077
|
+
aim[0] = thing.x;
|
|
1078
|
+
aim[1] = thing.y + lookAbove;
|
|
1079
|
+
aim[2] = thing.z;
|
|
1080
|
+
},
|
|
1081
|
+
hitStop(seconds = 0.06) {
|
|
1082
|
+
flat.hitStop(seconds);
|
|
1083
|
+
if (above(seconds))
|
|
1084
|
+
generation++;
|
|
1085
|
+
},
|
|
1086
|
+
shake(amount = 0.3, seconds = 0.25) {
|
|
1087
|
+
if (!above(amount) || !above(seconds))
|
|
1088
|
+
return;
|
|
1089
|
+
shakeAmount = Math.max(shakeLeft > 0 ? shakeAmount : 0, Math.min(amount, 10));
|
|
1090
|
+
shakeLeft = shakeSpan = Math.min(seconds, 5);
|
|
1091
|
+
},
|
|
1092
|
+
burst(x, y, z, o = {}) {
|
|
1093
|
+
admit();
|
|
1094
|
+
if (!finite(x) || !finite(y) || !finite(z) || o === null || typeof o !== 'object')
|
|
1095
|
+
return;
|
|
1096
|
+
const count = Math.min(PIECES, Math.max(1, Math.floor(finite(o.count) ? o.count : 12))), speed = above(o.speed) ? o.speed : 5, life = above(o.life) ? Math.min(o.life, 10) : 0.6, size = above(o.size) ? o.size : 0.18, gravity = finite(o.gravity) ? o.gravity : 12, color = colour(o.color) ? o.color : 0xffffff;
|
|
1097
|
+
admit();
|
|
1098
|
+
for (let i = 0; i < count; i++) {
|
|
1099
|
+
let piece = pieces[nextPiece];
|
|
1100
|
+
if (piece === undefined) {
|
|
1101
|
+
const thing = new PlayThing3D(pieceShape, color, [0.5, 0.5, 0.5]);
|
|
1102
|
+
// Pieces own their separate material, while the game owns their one shared shape.
|
|
1103
|
+
ownedGeometry.delete(thing);
|
|
1104
|
+
thing.node.castShadow = false;
|
|
1105
|
+
scene.addChild(thing.node);
|
|
1106
|
+
piece = pieces[nextPiece] = { thing, vx: 0, vy: 0, vz: 0, life: 0, span: 1, gravity: 0, size: 1 };
|
|
1107
|
+
}
|
|
1108
|
+
nextPiece = (nextPiece + 1) % PIECES;
|
|
1109
|
+
// A direction over the whole ball, a little upwards.
|
|
1110
|
+
const turn = flat.random.range(0, 2 * Math.PI), lift = flat.random.range(-0.4, 1), flatness = Math.sqrt(Math.max(0, 1 - lift * lift)), pace = speed * flat.random.range(0.5, 1);
|
|
1111
|
+
piece.vx = Math.cos(turn) * flatness * pace;
|
|
1112
|
+
piece.vy = lift * pace;
|
|
1113
|
+
piece.vz = Math.sin(turn) * flatness * pace;
|
|
1114
|
+
piece.life = piece.span = life * flat.random.range(0.6, 1);
|
|
1115
|
+
piece.gravity = gravity;
|
|
1116
|
+
piece.size = size;
|
|
1117
|
+
piece.thing.color = color;
|
|
1118
|
+
piece.thing.x = x;
|
|
1119
|
+
piece.thing.y = y;
|
|
1120
|
+
piece.thing.z = z;
|
|
1121
|
+
piece.thing.scale = size;
|
|
1122
|
+
piece.thing.visible = true;
|
|
1123
|
+
}
|
|
1124
|
+
},
|
|
1125
|
+
dispose() {
|
|
1126
|
+
if (disposal !== undefined)
|
|
1127
|
+
return disposal;
|
|
1128
|
+
beginClosing();
|
|
1129
|
+
// Publish before callbacks, but stop the flat game synchronously so a motion cannot keep chaining.
|
|
1130
|
+
let resolve, reject;
|
|
1131
|
+
disposal = new Promise((done, fail) => {
|
|
1132
|
+
resolve = done;
|
|
1133
|
+
reject = fail;
|
|
1134
|
+
});
|
|
1135
|
+
void (async () => {
|
|
1136
|
+
const failures = [];
|
|
1137
|
+
try {
|
|
1138
|
+
await flat.dispose();
|
|
1139
|
+
}
|
|
1140
|
+
catch (cause) {
|
|
1141
|
+
failures.push(cause);
|
|
1142
|
+
}
|
|
1143
|
+
try {
|
|
1144
|
+
await disposeWorld();
|
|
1145
|
+
}
|
|
1146
|
+
catch (cause) {
|
|
1147
|
+
failures.push(cause);
|
|
1148
|
+
}
|
|
1149
|
+
throwCleanupFailures(failures);
|
|
1150
|
+
})().then(resolve, reject);
|
|
1151
|
+
return disposal;
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
// Everything of the flat game is there under these: its getters read the game's own state, whoever asks.
|
|
1155
|
+
const whole = Object.create(flat, {
|
|
1156
|
+
...Object.getOwnPropertyDescriptors(added),
|
|
1157
|
+
scene: { value: scene, enumerable: true },
|
|
1158
|
+
camera: { value: camera, enumerable: true },
|
|
1159
|
+
sun: { value: sun, enumerable: true },
|
|
1160
|
+
app: {
|
|
1161
|
+
get: () => app,
|
|
1162
|
+
enumerable: true
|
|
1163
|
+
}
|
|
1164
|
+
});
|
|
1165
|
+
return whole;
|
|
1166
|
+
};
|
|
1167
|
+
const flat = await startFlat({
|
|
1168
|
+
...options,
|
|
1169
|
+
// The interface is drawn by WebGL2 unless WebGPU is asked for, as the overlay of a World3D app is.
|
|
1170
|
+
backend: options.backend ?? 'webgl2',
|
|
1171
|
+
transparent: true,
|
|
1172
|
+
async setup(given) {
|
|
1173
|
+
admit();
|
|
1174
|
+
if (game === undefined) {
|
|
1175
|
+
surface = attach(given, options);
|
|
1176
|
+
creatingApp = surface.ready;
|
|
1177
|
+
try {
|
|
1178
|
+
app = await creatingApp;
|
|
1179
|
+
}
|
|
1180
|
+
finally {
|
|
1181
|
+
creatingApp = undefined;
|
|
1182
|
+
}
|
|
1183
|
+
if (closing || given.scope.state !== 'open')
|
|
1184
|
+
throw new EgretError('SCOPE_CLOSED');
|
|
1185
|
+
scene.addChild(eye);
|
|
1186
|
+
scene.addChild(sky);
|
|
1187
|
+
scene.addChild(sun);
|
|
1188
|
+
settle();
|
|
1189
|
+
app?.show(scene, eye);
|
|
1190
|
+
surface.show?.(scene, eye);
|
|
1191
|
+
game = extend(given);
|
|
1192
|
+
}
|
|
1193
|
+
else
|
|
1194
|
+
clear();
|
|
1195
|
+
try {
|
|
1196
|
+
await setup?.(game);
|
|
1197
|
+
if (closing || given.scope.state !== 'open')
|
|
1198
|
+
throw new EgretError('SCOPE_CLOSED');
|
|
1199
|
+
settle();
|
|
1200
|
+
}
|
|
1201
|
+
catch (cause) {
|
|
1202
|
+
// A failed setup may already have attached borrowed models and allocated owned primitives.
|
|
1203
|
+
if (!closing) {
|
|
1204
|
+
try {
|
|
1205
|
+
clear();
|
|
1206
|
+
}
|
|
1207
|
+
catch (cleanupFailure) {
|
|
1208
|
+
throw new AggregateError([cause, cleanupFailure], 'Play3D setup and rollback failed');
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
throw cause;
|
|
1212
|
+
}
|
|
1213
|
+
},
|
|
1214
|
+
update(seconds) {
|
|
1215
|
+
if (game === undefined)
|
|
1216
|
+
return;
|
|
1217
|
+
const current = game, version = generation;
|
|
1218
|
+
const running = () => generation === version &&
|
|
1219
|
+
!clearing &&
|
|
1220
|
+
!closing &&
|
|
1221
|
+
!current.paused &&
|
|
1222
|
+
current.scope.state === 'open' &&
|
|
1223
|
+
current.browser.loop.state === 'running';
|
|
1224
|
+
if (!running())
|
|
1225
|
+
return;
|
|
1226
|
+
if (tapErrors.failure !== undefined) {
|
|
1227
|
+
const { cause } = tapErrors.failure;
|
|
1228
|
+
delete tapErrors.failure;
|
|
1229
|
+
throw cause;
|
|
1230
|
+
}
|
|
1231
|
+
tapped(current);
|
|
1232
|
+
if (!running())
|
|
1233
|
+
return;
|
|
1234
|
+
update?.(seconds, current);
|
|
1235
|
+
if (!running())
|
|
1236
|
+
return;
|
|
1237
|
+
after(seconds);
|
|
1238
|
+
}
|
|
1239
|
+
}).catch(async (cause) => {
|
|
1240
|
+
try {
|
|
1241
|
+
// A callback may have closed the published game while its initial setup was pending.
|
|
1242
|
+
if (game === undefined)
|
|
1243
|
+
await disposeWorld();
|
|
1244
|
+
else
|
|
1245
|
+
await game.dispose();
|
|
1246
|
+
}
|
|
1247
|
+
catch (cleanupFailure) {
|
|
1248
|
+
if (cause === cleanupFailure)
|
|
1249
|
+
throw cause;
|
|
1250
|
+
throw new AggregateError([cause, cleanupFailure], 'Play3D setup and cleanup failed');
|
|
1251
|
+
}
|
|
1252
|
+
throw cause;
|
|
1253
|
+
});
|
|
1254
|
+
return game ?? extend(flat);
|
|
1255
|
+
}
|
|
1256
|
+
//# sourceMappingURL=BrowserPlay3D.js.map
|