leaflet-city-mapper 0.0.1 → 0.0.3

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.
Files changed (629) hide show
  1. package/README.md +61 -13
  2. package/cucumber.mjs +4 -0
  3. package/dist/leaflet-city-mapper.js +3 -1
  4. package/dist/leaflet-city-mapper.js.map +4 -4
  5. package/package.json +3 -4
  6. package/src/archives/decodeDirectory.js +32 -0
  7. package/src/archives/decodeHeader.js +19 -0
  8. package/src/archives/decompress.js +5 -0
  9. package/src/archives/getHeader.js +9 -0
  10. package/src/archives/getZxy.js +75 -0
  11. package/src/archives/loadDirectory.js +7 -0
  12. package/src/archives/loadSource.js +17 -0
  13. package/src/archives/read.js +29 -0
  14. package/src/archives/tileId.js +19 -0
  15. package/src/archives/varint.js +15 -0
  16. package/src/camera/angles.js +6 -0
  17. package/src/camera/animationPoint.js +8 -0
  18. package/src/camera/cameraMapReady.js +8 -0
  19. package/src/camera/cancelCameraAnimation.js +11 -0
  20. package/src/camera/cleanupCameraMap.js +11 -0
  21. package/src/camera/constrainCameraToBounds.js +3 -0
  22. package/src/camera/displayPoint.js +3 -0
  23. package/src/camera/fitCameraBounds.js +48 -0
  24. package/src/camera/groundBounds.js +11 -0
  25. package/src/camera/initializeCameraMap.js +25 -0
  26. package/src/camera/mapAnimateCamera.js +17 -0
  27. package/src/camera/mapCamera.js +123 -0
  28. package/src/camera/mapContainerPointToLayerPoint.js +7 -0
  29. package/src/camera/mapGetBearing.js +1 -0
  30. package/src/camera/mapGetBounds.js +14 -0
  31. package/src/camera/mapGetBoundsCenterZoom.js +7 -0
  32. package/src/camera/mapGetBoundsZoom.js +14 -0
  33. package/src/camera/mapGetCenterLayerPoint.js +5 -0
  34. package/src/camera/mapGetPitch.js +1 -0
  35. package/src/camera/mapLayerPointToContainerPoint.js +7 -0
  36. package/src/camera/mapLimitCenter.js +12 -0
  37. package/src/camera/mapLimitOffset.js +10 -0
  38. package/src/camera/mapPanBy.js +9 -0
  39. package/src/camera/mapPanInside.js +14 -0
  40. package/src/camera/mapResetNorth.js +1 -0
  41. package/src/camera/mapResetNorthPitch.js +1 -0
  42. package/src/camera/mapSetBearing.js +1 -0
  43. package/src/camera/mapSetPitch.js +1 -0
  44. package/src/camera/mapSetPitchAndBearing.js +14 -0
  45. package/src/camera/mapSetZoomAround.js +11 -0
  46. package/src/camera/mapStopPitchBearingAnimation.js +6 -0
  47. package/src/camera/mapTryAnimatedPan.js +9 -0
  48. package/src/camera/mapUpdateCameraOverlays.js +7 -0
  49. package/src/camera/pointOf.js +3 -0
  50. package/src/camera/project.js +20 -0
  51. package/src/camera/runCameraAnimations.js +28 -0
  52. package/src/camera/scheduleCameraAnimation.js +8 -0
  53. package/src/camera/updateSky.js +16 -0
  54. package/src/camera/verticalGroundY.js +4 -0
  55. package/src/collision/collectMeshes.js +5 -0
  56. package/src/collision/collider.js +63 -0
  57. package/src/collision/horizontalRay.js +12 -0
  58. package/src/collision/intersectMeshes.js +133 -0
  59. package/src/collision/sortHits.js +23 -0
  60. package/src/constants/CELL.js +4 -0
  61. package/src/constants/Compass.js +7 -0
  62. package/src/constants/ContainerResize.js +7 -0
  63. package/src/constants/HEIGHT.js +1 -0
  64. package/src/constants/PitchDrag.js +7 -0
  65. package/src/constants/ShiftRotate.js +7 -0
  66. package/src/constants/TILE_RADIUS.js +1 -0
  67. package/src/constants/TILE_SIZE.js +1 -0
  68. package/src/constants/TouchMode.js +6 -0
  69. package/src/constants/accessorSizes.js +1 -0
  70. package/src/constants/accessorTypes.js +8 -0
  71. package/src/constants/attachState.js +1 -0
  72. package/src/constants/cameraControlsState.js +1 -0
  73. package/src/constants/cityMapper.js +6 -0
  74. package/src/constants/colliders.js +2 -0
  75. package/src/constants/dark.js +1 -0
  76. package/src/constants/elevationWGSL.js +27 -0
  77. package/src/constants/glass.js +1 -0
  78. package/src/constants/headlight.js +1 -0
  79. package/src/constants/hull.js +1 -0
  80. package/src/constants/indexState.js +1 -0
  81. package/src/constants/limbs.js +9 -0
  82. package/src/constants/mapCameraState.js +1 -0
  83. package/src/constants/moves.js +11 -0
  84. package/src/constants/nativeEventMaps.js +1 -0
  85. package/src/constants/nativeLayerState.js +1 -0
  86. package/src/constants/nativeMethodHandlers.js +31 -0
  87. package/src/constants/onMapChangeState.js +1 -0
  88. package/src/constants/onZoomAnimState.js +1 -0
  89. package/src/constants/paints.js +10 -0
  90. package/src/constants/parts.js +19 -0
  91. package/src/constants/pendingUpdates.js +1 -0
  92. package/src/constants/pumpState.js +1 -0
  93. package/src/constants/renderers.js +2 -0
  94. package/src/constants/ringDirections.js +10 -0
  95. package/src/constants/roundProfile.js +1 -0
  96. package/src/constants/shirt.js +1 -0
  97. package/src/constants/skin.js +2 -0
  98. package/src/constants/speed.js +3 -0
  99. package/src/constants/taillight.js +1 -0
  100. package/src/constants/trafficShader.js +49 -0
  101. package/src/constants/trousers.js +1 -0
  102. package/src/constants/tyre.js +1 -0
  103. package/src/constants/underEntryState.js +1 -0
  104. package/src/constants/vehicles.js +40 -0
  105. package/src/constants/white.js +1 -0
  106. package/src/controls/acceptPitchDrag.js +4 -0
  107. package/src/controls/acceptRotateDrag.js +3 -0
  108. package/src/controls/adjustCameraDrag.js +10 -0
  109. package/src/controls/cameraControls.js +60 -0
  110. package/src/controls/cameraDragClick.js +9 -0
  111. package/src/controls/cameraDragContextMenu.js +8 -0
  112. package/src/controls/cameraDragDown.js +20 -0
  113. package/src/controls/cameraDragEnd.js +6 -0
  114. package/src/controls/cameraDragMove.js +23 -0
  115. package/src/controls/changeCameraTouch.js +14 -0
  116. package/src/controls/cleanupCameraTouch.js +24 -0
  117. package/src/controls/compassAddHooks.js +15 -0
  118. package/src/controls/compassOrientation.js +6 -0
  119. package/src/controls/compassRemoveHooks.js +5 -0
  120. package/src/controls/containerResizeAddHooks.js +8 -0
  121. package/src/controls/containerResizeRemoveHooks.js +5 -0
  122. package/src/controls/dragCamera.js +22 -0
  123. package/src/controls/editable.js +3 -0
  124. package/src/controls/endAllCameraDrags.js +7 -0
  125. package/src/controls/endAllCameraTouches.js +7 -0
  126. package/src/controls/endCameraTouch.js +11 -0
  127. package/src/controls/endCameraTouchEvent.js +5 -0
  128. package/src/controls/finishCameraDrag.js +9 -0
  129. package/src/controls/finishCompassPermission.js +16 -0
  130. package/src/controls/loadCameraControls.js +9 -0
  131. package/src/controls/moveCameraTouch.js +22 -0
  132. package/src/controls/notifyCameraTouchChange.js +1 -0
  133. package/src/controls/pitchDragAddHooks.js +6 -0
  134. package/src/controls/pitchDragRemoveHooks.js +5 -0
  135. package/src/controls/refreshRotateControl.js +7 -0
  136. package/src/controls/refreshRotateFocus.js +5 -0
  137. package/src/controls/removeCameraDrag.js +23 -0
  138. package/src/controls/resizeMap.js +3 -0
  139. package/src/controls/rotateButtonClick.js +7 -0
  140. package/src/controls/rotateButtonKeydown.js +11 -0
  141. package/src/controls/rotateControl.js +3 -0
  142. package/src/controls/rotateOnAdd.js +38 -0
  143. package/src/controls/rotateOnRemove.js +6 -0
  144. package/src/controls/shiftRotateAddHooks.js +8 -0
  145. package/src/controls/shiftRotateRemoveHooks.js +5 -0
  146. package/src/controls/shiftRotateWheel.js +9 -0
  147. package/src/controls/startCameraTouch.js +23 -0
  148. package/src/core/addImageState.js +7 -0
  149. package/src/core/addRasterImage.js +10 -0
  150. package/src/core/adjustCameraMarkerPan.js +22 -0
  151. package/src/core/beforeNativeRender.js +10 -0
  152. package/src/core/boundsQuad.js +11 -0
  153. package/src/core/bringNativeLayerToBack.js +3 -0
  154. package/src/core/bringNativeLayerToFront.js +3 -0
  155. package/src/core/cameraCircleBounds.js +6 -0
  156. package/src/core/cameraMarkerDrag.js +18 -0
  157. package/src/core/cameraMarkerOnDrag.js +8 -0
  158. package/src/core/cancelGridFade.js +10 -0
  159. package/src/core/closestCameraLayerPoint.js +7 -0
  160. package/src/core/compareNativeHits.js +3 -0
  161. package/src/core/current.js +3 -0
  162. package/src/core/disposeNativeLayer.js +35 -0
  163. package/src/core/fireRasterLoad.js +5 -0
  164. package/src/core/forwardNativeError.js +3 -0
  165. package/src/core/gridlayerGetEvents.js +7 -0
  166. package/src/core/gridlayerGetTiledPixelBounds.js +8 -0
  167. package/src/core/handleNativeMapEvent.js +47 -0
  168. package/src/core/hideTargets.js +16 -0
  169. package/src/core/initNativeGPU.js +21 -0
  170. package/src/core/initializeGridContainer.js +6 -0
  171. package/src/core/initializeNativeLayer.js +42 -0
  172. package/src/core/localPixel.js +5 -0
  173. package/src/core/localPixelForGeometry.js +6 -0
  174. package/src/core/localPosition.js +6 -0
  175. package/src/core/markerCameraIconPoint.js +9 -0
  176. package/src/core/markerGetEvents.js +5 -0
  177. package/src/core/markerInitInteraction.js +8 -0
  178. package/src/core/markerQuadCorners.js +31 -0
  179. package/src/core/markerSetIcon.js +9 -0
  180. package/src/core/markerSetPos.js +14 -0
  181. package/src/core/markerSetRotation.js +7 -0
  182. package/src/core/markerUpdateZIndex.js +8 -0
  183. package/src/core/nativeAddLatLng.js +3 -0
  184. package/src/core/nativeBringToBack.js +3 -0
  185. package/src/core/nativeBringToFront.js +3 -0
  186. package/src/core/nativeEvents.js +26 -0
  187. package/src/core/nativeImageError.js +8 -0
  188. package/src/core/nativeImageRefresh.js +7 -0
  189. package/src/core/nativeLayer.js +97 -0
  190. package/src/core/nativeLayerMethod.js +21 -0
  191. package/src/core/nativeRedraw.js +3 -0
  192. package/src/core/nativeSetBounds.js +3 -0
  193. package/src/core/nativeSetIcon.js +3 -0
  194. package/src/core/nativeSetLatLng.js +3 -0
  195. package/src/core/nativeSetLatLngs.js +3 -0
  196. package/src/core/nativeSetOpacity.js +3 -0
  197. package/src/core/nativeSetRadius.js +3 -0
  198. package/src/core/nativeSetStyle.js +3 -0
  199. package/src/core/nativeSetUrl.js +3 -0
  200. package/src/core/nativeSetZIndex.js +3 -0
  201. package/src/core/nativeSetZIndexOffset.js +3 -0
  202. package/src/core/popupAdjustPan.js +23 -0
  203. package/src/core/popupGetEvents.js +5 -0
  204. package/src/core/popupUpdatePosition.js +14 -0
  205. package/src/core/putNativeMesh.js +33 -0
  206. package/src/core/quadNativeMesh.js +15 -0
  207. package/src/core/readyNativeGPU.js +6 -0
  208. package/src/core/refreshNativeEvent.js +5 -0
  209. package/src/core/refreshNativeLayer.js +29 -0
  210. package/src/core/refreshNativeLayerContract.js +3 -0
  211. package/src/core/rejectNativeReady.js +3 -0
  212. package/src/core/removeImageState.js +8 -0
  213. package/src/core/removeNativeEvents.js +16 -0
  214. package/src/core/removeTargets.js +8 -0
  215. package/src/core/reorderNativeLayer.js +6 -0
  216. package/src/core/resolveNativeReady.js +3 -0
  217. package/src/core/restoreNativeEvents.js +6 -0
  218. package/src/core/runGridFades.js +14 -0
  219. package/src/core/runMarkerPanFrame.js +12 -0
  220. package/src/core/scheduleGridFade.js +9 -0
  221. package/src/core/scheduleMarkerPan.js +8 -0
  222. package/src/core/styleNativeMesh.js +10 -0
  223. package/src/core/syncGridLayer.js +22 -0
  224. package/src/core/syncImageOverlay.js +9 -0
  225. package/src/core/syncMarker.js +12 -0
  226. package/src/core/syncVectorLayer.js +26 -0
  227. package/src/core/synchronizeNativeLayer.js +35 -0
  228. package/src/core/textureFor.js +48 -0
  229. package/src/core/tooltipGetEvents.js +5 -0
  230. package/src/core/tooltipSetPosition.js +8 -0
  231. package/src/core/updateGridOpacity.js +23 -0
  232. package/src/core/updateNativeLayerView.js +6 -0
  233. package/src/core/uploadCanvasTexture.js +19 -0
  234. package/src/geometry/addBridges.js +88 -0
  235. package/src/geometry/below.js +3 -0
  236. package/src/geometry/box.js +3 -0
  237. package/src/geometry/bridgeElevation.js +19 -0
  238. package/src/geometry/bridgeLocal.js +4 -0
  239. package/src/geometry/build.js +19 -0
  240. package/src/geometry/circleAt.js +5 -0
  241. package/src/geometry/clipLine.js +23 -0
  242. package/src/geometry/clipSegment.js +19 -0
  243. package/src/geometry/computeBounds.js +33 -0
  244. package/src/geometry/conformModelToTerrain.js +67 -0
  245. package/src/geometry/cross.js +3 -0
  246. package/src/geometry/disk.js +11 -0
  247. package/src/geometry/diskAt.js +4 -0
  248. package/src/geometry/dot.js +6 -0
  249. package/src/geometry/emitter.js +8 -0
  250. package/src/geometry/lampPostGeometry.js +24 -0
  251. package/src/geometry/lampRing.js +11 -0
  252. package/src/geometry/lightGeometry.js +15 -0
  253. package/src/geometry/line.js +54 -0
  254. package/src/geometry/nativeGeometry.js +55 -0
  255. package/src/geometry/nativeTriangle.js +3 -0
  256. package/src/geometry/polygon.js +22 -0
  257. package/src/geometry/push.js +3 -0
  258. package/src/geometry/quad.js +4 -0
  259. package/src/geometry/roofCoverage.js +52 -0
  260. package/src/geometry/samePoint.js +3 -0
  261. package/src/geometry/segment.js +21 -0
  262. package/src/geometry/setVertexColor.js +15 -0
  263. package/src/geometry/sub.js +6 -0
  264. package/src/geometry/surface.js +17 -0
  265. package/src/geometry/treeGeometry.js +31 -0
  266. package/src/geometry/treeRing.js +14 -0
  267. package/src/geometry/triangle.js +12 -0
  268. package/src/geometry/vertexNormals.js +32 -0
  269. package/src/geometry/visit.js +9 -0
  270. package/src/index.js +98 -65
  271. package/src/lighting/cacheLightGrid.js +23 -0
  272. package/src/lighting/collectLightMeshes.js +6 -0
  273. package/src/lighting/lightGrid.js +42 -0
  274. package/src/lighting/updatePointLights.js +34 -0
  275. package/src/lighting/visitCells.js +15 -0
  276. package/src/lighting/worldLights.js +27 -0
  277. package/src/models/decodeModel.js +45 -0
  278. package/src/models/hasUnsupportedPrimitives.js +10 -0
  279. package/src/models/loadBuffer.js +6 -0
  280. package/src/models/loadImage.js +15 -0
  281. package/src/models/loadModel.js +69 -0
  282. package/src/models/materialFor.js +21 -0
  283. package/src/models/modelNode.js +93 -0
  284. package/src/models/nodeMatrix.js +22 -0
  285. package/src/models/readAccessor.js +53 -0
  286. package/src/models/readAccessorValues.js +16 -0
  287. package/src/models/textureFor.js +35 -0
  288. package/src/models/textureSampler.js +10 -0
  289. package/src/picking/collectVisibleMesh.js +4 -0
  290. package/src/picking/findFeature.js +49 -0
  291. package/src/picking/hit.js +23 -0
  292. package/src/picking/hitMesh.js +30 -0
  293. package/src/picking/onClick.js +13 -0
  294. package/src/picking/onPointerLeave.js +11 -0
  295. package/src/picking/onPointerMove.js +15 -0
  296. package/src/picking/pointerClick.js +5 -0
  297. package/src/picking/pointerLeave.js +5 -0
  298. package/src/picking/pointerMove.js +5 -0
  299. package/src/picking/raycast.js +59 -0
  300. package/src/picking/setHighlight.js +13 -0
  301. package/src/picking/shouldSkipOther.js +3 -0
  302. package/src/picking/showFeature.js +34 -0
  303. package/src/raster/addRasterTile.js +114 -0
  304. package/src/raster/comparePendingTiles.js +5 -0
  305. package/src/raster/createGeometry.js +22 -0
  306. package/src/raster/createRasterMesh.js +47 -0
  307. package/src/raster/finishAttachment.js +11 -0
  308. package/src/raster/finishRasterTile.js +47 -0
  309. package/src/raster/getRasterEvents.js +6 -0
  310. package/src/raster/getRasterTileSize.js +5 -0
  311. package/src/raster/initRaster.js +38 -0
  312. package/src/raster/isLoading.js +5 -0
  313. package/src/raster/loadRasterTile.js +10 -0
  314. package/src/raster/onPMTileAdd.js +10 -0
  315. package/src/raster/onPMTileRemove.js +8 -0
  316. package/src/raster/pruneRasterTiles.js +37 -0
  317. package/src/raster/pumpRasterTiles.js +22 -0
  318. package/src/raster/readyRaster.js +13 -0
  319. package/src/raster/registerTerrainTile.js +12 -0
  320. package/src/raster/releaseRasterTile.js +18 -0
  321. package/src/raster/releaseRasterTiles.js +15 -0
  322. package/src/raster/removeRasterTile.js +13 -0
  323. package/src/raster/reportError.js +6 -0
  324. package/src/raster/setPMTileOpacity.js +7 -0
  325. package/src/raster/setPMTileZIndex.js +7 -0
  326. package/src/raster/setRasterOpacity.js +11 -0
  327. package/src/raster/setRasterTerrain.js +21 -0
  328. package/src/raster/setRasterZIndex.js +11 -0
  329. package/src/raster/updateAttachedTiles.js +10 -0
  330. package/src/raster/updateRasterTiles.js +59 -0
  331. package/src/rendering/collectFrameMeshes.js +31 -0
  332. package/src/rendering/collectMeshes.js +36 -0
  333. package/src/rendering/compareMeshes.js +10 -0
  334. package/src/rendering/compareRoofTiles.js +4 -0
  335. package/src/rendering/composeRoofAtlas.js +52 -0
  336. package/src/rendering/createMesh.js +33 -0
  337. package/src/rendering/createProgram.js +92 -0
  338. package/src/rendering/destroyAfterSubmission.js +8 -0
  339. package/src/rendering/destroyTexture.js +6 -0
  340. package/src/rendering/disposeMesh.js +14 -0
  341. package/src/rendering/drawGeometry.js +129 -0
  342. package/src/rendering/drawSceneMeshes.js +51 -0
  343. package/src/rendering/ensureRoofBlit.js +40 -0
  344. package/src/rendering/prepareFrameMeshes.js +42 -0
  345. package/src/rendering/prepareFrameUniforms.js +49 -0
  346. package/src/rendering/rebuildRoofAtlas.js +67 -0
  347. package/src/rendering/render.js +57 -0
  348. package/src/rendering/reportCompilation.js +17 -0
  349. package/src/rendering/resizeRenderer.js +19 -0
  350. package/src/rendering/sortMeshes.js +6 -0
  351. package/src/rendering/synchronizeZoomView.js +34 -0
  352. package/src/rendering/textureBinding.js +7 -0
  353. package/src/rendering/updateMapCamera.js +32 -0
  354. package/src/rendering/updateRoofRaster.js +32 -0
  355. package/src/scene/attach.js +161 -0
  356. package/src/scene/attachLayer.js +10 -0
  357. package/src/scene/cancelUpdate.js +10 -0
  358. package/src/scene/emptyEvents.js +3 -0
  359. package/src/scene/emptyNode.js +11 -0
  360. package/src/scene/endZoom.js +16 -0
  361. package/src/scene/failAttachment.js +9 -0
  362. package/src/scene/get3DEvents.js +13 -0
  363. package/src/scene/init3D.js +30 -0
  364. package/src/scene/initializeLayer.js +16 -0
  365. package/src/scene/initializerFor.js +14 -0
  366. package/src/scene/layerInitialize.js +38 -0
  367. package/src/scene/layerLayerAdd.js +21 -0
  368. package/src/scene/onAdd3DLayer.js +35 -0
  369. package/src/scene/onMapChange.js +16 -0
  370. package/src/scene/onRemove3DLayer.js +69 -0
  371. package/src/scene/onZoomAnim.js +16 -0
  372. package/src/scene/placeEntry.js +11 -0
  373. package/src/scene/placeUnderEntry.js +8 -0
  374. package/src/scene/ready3D.js +25 -0
  375. package/src/scene/releaseRenderer.js +14 -0
  376. package/src/scene/removeEntry.js +6 -0
  377. package/src/scene/renderPendingMapChange.js +11 -0
  378. package/src/scene/renderPendingUpdates.js +19 -0
  379. package/src/scene/renderPendingZoomAnimation.js +7 -0
  380. package/src/scene/rendererDeviceError.js +10 -0
  381. package/src/scene/rendererDeviceLost.js +11 -0
  382. package/src/scene/replaceEntry.js +7 -0
  383. package/src/scene/reportError.js +3 -0
  384. package/src/scene/requestAdapter.js +12 -0
  385. package/src/scene/requestDevice.js +9 -0
  386. package/src/scene/resolveNextInitialization.js +12 -0
  387. package/src/scene/restoreObject.js +71 -0
  388. package/src/scene/runPlacements.js +7 -0
  389. package/src/scene/setParent.js +7 -0
  390. package/src/scene/showWebGPUWarning.js +16 -0
  391. package/src/scene/startZoom.js +5 -0
  392. package/src/scene/traverse.js +7 -0
  393. package/src/scene/underEntry.js +17 -0
  394. package/src/scene/update.js +19 -0
  395. package/src/scene/updateCamera.js +34 -0
  396. package/src/scene/updateWorldMatrix.js +48 -0
  397. package/src/{utils/shaders.js → shaders/materialShaders.js} +246 -237
  398. package/src/shadows/bindSunlight.js +12 -0
  399. package/src/shadows/drawShadowMeshes.js +48 -0
  400. package/src/shadows/ensureShadowPipeline.js +53 -0
  401. package/src/shadows/renderSunlight.js +46 -0
  402. package/src/shadows/resizeSunlight.js +15 -0
  403. package/src/shadows/shadowBounds.js +32 -0
  404. package/src/shadows/shadowCacheMatches.js +23 -0
  405. package/src/shadows/shadowMatrix.js +39 -0
  406. package/src/sources/addGeneratedTriangle.js +6 -0
  407. package/src/sources/addTriangle.js +17 -0
  408. package/src/sources/attachAreaMesh.js +58 -0
  409. package/src/sources/buildArea.js +42 -0
  410. package/src/sources/buildAreaFeature.js +41 -0
  411. package/src/sources/buildAreaPoint.js +25 -0
  412. package/src/sources/buildAreaPolygon.js +29 -0
  413. package/src/sources/compareAreaDistance.js +4 -0
  414. package/src/sources/generateTreeGeometry.js +11 -0
  415. package/src/sources/groundOffset.js +3 -0
  416. package/src/sources/initializeGeoJSON.js +8 -0
  417. package/src/sources/instanceAreaGeometry.js +29 -0
  418. package/src/sources/projectRing.js +20 -0
  419. package/src/sources/projectedTree.js +13 -0
  420. package/src/sources/pump.js +54 -0
  421. package/src/sources/releaseArea.js +22 -0
  422. package/src/sources/setGeoJSONData.js +12 -0
  423. package/src/sources/setGeoJSONLayerData.js +5 -0
  424. package/src/sources/sourceAreaContext.js +9 -0
  425. package/src/sources/updateArea.js +45 -0
  426. package/src/sources/updateStatus.js +12 -0
  427. package/src/sunlight/addTimeLayer.js +3 -0
  428. package/src/sunlight/addTo.js +10 -0
  429. package/src/sunlight/atmosphere.js +28 -0
  430. package/src/sunlight/horizontalPosition.js +16 -0
  431. package/src/sunlight/initSunlight.js +14 -0
  432. package/src/sunlight/initialize.js +11 -0
  433. package/src/sunlight/mix.js +6 -0
  434. package/src/sunlight/moonPosition.js +17 -0
  435. package/src/sunlight/onAdd.js +5 -0
  436. package/src/sunlight/onAddSun.js +66 -0
  437. package/src/sunlight/projectCelestialBody.js +13 -0
  438. package/src/sunlight/remove.js +7 -0
  439. package/src/sunlight/removeSun.js +15 -0
  440. package/src/sunlight/removeTimeLayer.js +3 -0
  441. package/src/sunlight/render.js +7 -0
  442. package/src/sunlight/rgb.js +6 -0
  443. package/src/sunlight/solarPosition.js +17 -0
  444. package/src/sunlight/styleTimeControl.js +7 -0
  445. package/src/sunlight/tickSun.js +5 -0
  446. package/src/sunlight/updateSky.js +20 -0
  447. package/src/sunlight/updateSun.js +34 -0
  448. package/src/sunlight/useLiveTime.js +6 -0
  449. package/src/sunlight/useSelectedTime.js +20 -0
  450. package/src/terrain/clearLayerTerrain.js +5 -0
  451. package/src/terrain/getElevation.js +10 -0
  452. package/src/terrain/initTerrain.js +20 -0
  453. package/src/terrain/loadReadyTerrain.js +5 -0
  454. package/src/terrain/loadTerrain.js +63 -0
  455. package/src/terrain/loadTerrainTiles.js +13 -0
  456. package/src/terrain/onTerrainAdd.js +21 -0
  457. package/src/terrain/onTerrainChange.js +14 -0
  458. package/src/terrain/onTerrainRemove.js +41 -0
  459. package/src/terrain/readBridges.js +4 -0
  460. package/src/terrain/sample.js +7 -0
  461. package/src/terrain/setLayerTerrain.js +5 -0
  462. package/src/terrain/setTerrainHeight.js +10 -0
  463. package/src/terrain/terrainAtlas.js +23 -0
  464. package/src/terrain/terrainElevation.js +20 -0
  465. package/src/terrain/terrainGrid.js +24 -0
  466. package/src/traffic/addTraffic.js +7 -0
  467. package/src/traffic/along.js +14 -0
  468. package/src/traffic/clockMinutes.js +6 -0
  469. package/src/traffic/compareDepartures.js +5 -0
  470. package/src/traffic/createTrafficMesh.js +7 -0
  471. package/src/traffic/destroyTrafficGpu.js +5 -0
  472. package/src/traffic/disposeTrafficObject.js +13 -0
  473. package/src/traffic/drawTrafficRoute.js +6 -0
  474. package/src/traffic/failTraffic.js +6 -0
  475. package/src/traffic/failedTrafficLoad.js +5 -0
  476. package/src/traffic/fetchTrafficLanes.js +10 -0
  477. package/src/traffic/groundTraffic.js +15 -0
  478. package/src/traffic/initTraffic.js +54 -0
  479. package/src/traffic/loadTraffic.js +15 -0
  480. package/src/traffic/loadedTrafficLanes.js +16 -0
  481. package/src/traffic/locateTraffic.js +9 -0
  482. package/src/traffic/moveTraffic.js +15 -0
  483. package/src/traffic/placeTraffic.js +21 -0
  484. package/src/traffic/prepareTraffic.js +25 -0
  485. package/src/traffic/readyTraffic.js +6 -0
  486. package/src/traffic/rebaseTraffic.js +58 -0
  487. package/src/traffic/rebuildTrafficMesh.js +28 -0
  488. package/src/traffic/removeTraffic.js +17 -0
  489. package/src/traffic/routeColor.js +6 -0
  490. package/src/traffic/routeGeometry.js +31 -0
  491. package/src/traffic/scheduledTrips.js +40 -0
  492. package/src/traffic/startTraffic.js +8 -0
  493. package/src/traffic/tickTraffic.js +57 -0
  494. package/src/traffic/toLanes.js +16 -0
  495. package/src/traffic/tooltip.js +11 -0
  496. package/src/traffic/trafficBounds.js +17 -0
  497. package/src/traffic/trafficNow.js +4 -0
  498. package/src/traffic/trafficStorage.js +5 -0
  499. package/src/traffic/tripsAt.js +27 -0
  500. package/src/traffic/updateTraffic.js +8 -0
  501. package/src/traffic/updateTrafficTerrain.js +12 -0
  502. package/src/traffic/vehicleOffsets.js +11 -0
  503. package/src/triangulation/area.js +9 -0
  504. package/src/triangulation/clean.js +18 -0
  505. package/src/triangulation/containsVertex.js +11 -0
  506. package/src/triangulation/cross.js +3 -0
  507. package/src/triangulation/crossesBoundary.js +8 -0
  508. package/src/triangulation/crossesRing.js +9 -0
  509. package/src/triangulation/equal.js +3 -0
  510. package/src/triangulation/inside.js +10 -0
  511. package/src/triangulation/insideHole.js +7 -0
  512. package/src/triangulation/intersects.js +11 -0
  513. package/src/triangulation/isClockWise.js +13 -0
  514. package/src/triangulation/minimumX.js +6 -0
  515. package/src/triangulation/on.js +4 -0
  516. package/src/triangulation/sortHoles.js +26 -0
  517. package/src/triangulation/triangulate.js +86 -0
  518. package/src/utils/channel.js +4 -0
  519. package/src/utils/compareDistance.js +3 -0
  520. package/src/utils/noOperation.js +1 -0
  521. package/src/utils/parseColor.js +42 -0
  522. package/src/utils/toSRGB.js +3 -0
  523. package/src/utils/wrap.js +3 -0
  524. package/src/vehicles/bicycleModel.js +23 -0
  525. package/src/vehicles/boatBenches.js +8 -0
  526. package/src/vehicles/boatModel.js +27 -0
  527. package/src/vehicles/busModel.js +6 -0
  528. package/src/vehicles/carModel.js +22 -0
  529. package/src/vehicles/carriage.js +19 -0
  530. package/src/vehicles/metroModel.js +9 -0
  531. package/src/vehicles/mirror.js +8 -0
  532. package/src/vehicles/pedestrianModel.js +17 -0
  533. package/src/vehicles/tramModel.js +9 -0
  534. package/src/walk/blocked.js +6 -0
  535. package/src/walk/createWalk.js +60 -0
  536. package/src/walk/initWalk.js +18 -0
  537. package/src/walk/initialize.js +6 -0
  538. package/src/walk/local.js +10 -0
  539. package/src/walk/onAdd.js +4 -0
  540. package/src/walk/onBlur.js +3 -0
  541. package/src/walk/onKey.js +12 -0
  542. package/src/walk/onPointer.js +13 -0
  543. package/src/walk/onRemove.js +4 -0
  544. package/src/walk/onTerrain.js +5 -0
  545. package/src/walk/place.js +18 -0
  546. package/src/walk/start.js +37 -0
  547. package/src/walk/step.js +53 -0
  548. package/src/walk/stop.js +27 -0
  549. package/src/walk/toggle.js +6 -0
  550. package/dist/scene/gpu.js +0 -3
  551. package/dist/scene/gpu.js.map +0 -7
  552. package/dist/scene/lighting.js +0 -9
  553. package/dist/scene/lighting.js.map +0 -7
  554. package/dist/scene/model.js +0 -2
  555. package/dist/scene/model.js.map +0 -7
  556. package/dist/scene/picking.js +0 -2
  557. package/dist/scene/picking.js.map +0 -7
  558. package/dist/scene/pmtiles.js +0 -2
  559. package/dist/scene/pmtiles.js.map +0 -7
  560. package/dist/scene/render.js +0 -15
  561. package/dist/scene/render.js.map +0 -7
  562. package/dist/scene/sources.js +0 -2
  563. package/dist/scene/sources.js.map +0 -7
  564. package/dist/types/3d.js +0 -2
  565. package/dist/types/3d.js.map +0 -7
  566. package/dist/types/geojson.js +0 -2
  567. package/dist/types/geojson.js.map +0 -7
  568. package/dist/types/pmtiles.js +0 -2
  569. package/dist/types/pmtiles.js.map +0 -7
  570. package/dist/types/sunlight.js +0 -2
  571. package/dist/types/sunlight.js.map +0 -7
  572. package/dist/types/terrain.js +0 -24
  573. package/dist/types/terrain.js.map +0 -7
  574. package/dist/types/traffic.js +0 -41
  575. package/dist/types/traffic.js.map +0 -7
  576. package/dist/types/walk.js +0 -2
  577. package/dist/types/walk.js.map +0 -7
  578. package/dist/utils/control.js +0 -2
  579. package/dist/utils/control.js.map +0 -7
  580. package/dist/utils/geometry.js +0 -2
  581. package/dist/utils/geometry.js.map +0 -7
  582. package/dist/utils/lighting.js +0 -2
  583. package/dist/utils/lighting.js.map +0 -7
  584. package/dist/utils/model.js +0 -2
  585. package/dist/utils/model.js.map +0 -7
  586. package/dist/utils/picking.js +0 -2
  587. package/dist/utils/picking.js.map +0 -7
  588. package/dist/utils/pmtiles.js +0 -2
  589. package/dist/utils/pmtiles.js.map +0 -7
  590. package/dist/utils/scene.js +0 -2
  591. package/dist/utils/scene.js.map +0 -7
  592. package/dist/utils/shaders.js +0 -203
  593. package/dist/utils/shaders.js.map +0 -7
  594. package/dist/utils/sun.js +0 -2
  595. package/dist/utils/sun.js.map +0 -7
  596. package/dist/utils/terrain.js +0 -2
  597. package/dist/utils/terrain.js.map +0 -7
  598. package/dist/utils/traffic.js +0 -2
  599. package/dist/utils/traffic.js.map +0 -7
  600. package/dist/utils/vehicles.js +0 -2
  601. package/dist/utils/vehicles.js.map +0 -7
  602. package/dist/utils/walk.js +0 -2
  603. package/dist/utils/walk.js.map +0 -7
  604. package/src/scene/gpu.js +0 -438
  605. package/src/scene/lighting.js +0 -193
  606. package/src/scene/model.js +0 -204
  607. package/src/scene/picking.js +0 -150
  608. package/src/scene/pmtiles.js +0 -94
  609. package/src/scene/render.js +0 -467
  610. package/src/scene/sources.js +0 -265
  611. package/src/types/3d.js +0 -337
  612. package/src/types/geojson.js +0 -42
  613. package/src/types/pmtiles.js +0 -143
  614. package/src/types/sunlight.js +0 -142
  615. package/src/types/terrain.js +0 -331
  616. package/src/types/traffic.js +0 -356
  617. package/src/types/walk.js +0 -219
  618. package/src/utils/control.js +0 -18
  619. package/src/utils/geometry.js +0 -256
  620. package/src/utils/lighting.js +0 -51
  621. package/src/utils/model.js +0 -178
  622. package/src/utils/picking.js +0 -119
  623. package/src/utils/pmtiles.js +0 -79
  624. package/src/utils/scene.js +0 -102
  625. package/src/utils/sun.js +0 -71
  626. package/src/utils/terrain.js +0 -78
  627. package/src/utils/traffic.js +0 -90
  628. package/src/utils/vehicles.js +0 -69
  629. package/src/utils/walk.js +0 -72
package/src/scene/gpu.js DELETED
@@ -1,438 +0,0 @@
1
- import { materialShaders } from '../utils/shaders.js';
2
- import { emptyNode, setParent, traverse } from '../utils/scene.js';
3
-
4
- /** WebGPU device attachment, materials/pipelines and draw calls. */
5
- /**
6
- * Join/create this map's shared scene and rebuild GPU objects if its context changed.
7
- * Buffer/program handles belong to one context; typed arrays and decoded images do not.
8
- * @private
9
- */
10
- export async function attach(backend, map) {
11
- let shared = L.Layer.types['3d']._renderers.get(map);
12
- if (shared?._rendererReady) {
13
- await shared._rendererReady.promise;
14
- }
15
- if (!shared) {
16
- const rendererReady = Promise.withResolvers();
17
- shared = backend;
18
- backend._rendererReady = rendererReady;
19
- rendererReady.promise.catch(() => {});
20
- L.Layer.types['3d']._renderers.set(map, shared);
21
- const fail = error => {
22
- rendererReady.reject(error);
23
- L.Layer.types['3d']._renderers.delete(map);
24
- throw error;
25
- };
26
- const canvas = document.createElement('canvas');
27
- if (!navigator.gpu) fail(new Error('Layer3D requires WebGPU'));
28
- const adapter = await navigator.gpu.requestAdapter().catch(fail);
29
- if (!adapter) fail(new Error('Unable to acquire a WebGPU adapter'));
30
- const device = await adapter.requestDevice().catch(fail);
31
- device.addEventListener('uncapturederror', event => {
32
- const owner = L.Layer.types['3d']._renderers.get(map);
33
- if (owner?.renderer?.device !== device) return;
34
- for (const layer of owner?._layers || []) layer.fire('error', { error: event.error });
35
- });
36
- device.lost.then(info => {
37
- const owner = L.Layer.types['3d']._renderers.get(map);
38
- if (!owner || owner.renderer?.device !== device) return;
39
- const error = new Error(`WebGPU device lost: ${info.message || info.reason}`);
40
- for (const layer of owner._layers) layer.fire('error', { error });
41
- });
42
- const context = canvas.getContext('webgpu');
43
- if (!context) {
44
- device.destroy();
45
- fail(new Error('Unable to create a WebGPU canvas context'));
46
- }
47
- const format = navigator.gpu.getPreferredCanvasFormat();
48
- context.configure({ device, format, alphaMode: 'premultiplied' });
49
- backend._shared = backend;
50
- backend._layers = new Set();
51
- backend._sceneOrigin = backend.location;
52
- backend.scene = emptyNode();
53
- // Perspective camera state: fov in degrees, near/far in scene metres.
54
- backend.camera = {
55
- position: [0, 0, 0],
56
- up: [0, 1, 0],
57
- target: [0, 0, 0],
58
- viewMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
59
- projectionMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
60
- projectionViewMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
61
- fov: 45,
62
- aspect: 1,
63
- near: 0.1,
64
- far: 10000,
65
- };
66
- backend.renderer = { device, context, format, canvas, dpr: Math.min(window.devicePixelRatio, 2) };
67
- backend._initSunlight();
68
- const emptyImage = document.createElement('canvas');
69
- emptyImage.width = 1;
70
- emptyImage.height = 1;
71
- backend.renderer.roofFallbackTexture = {
72
- device,
73
- texture: device.createTexture({
74
- size: [1, 1],
75
- format: 'rgba8unorm',
76
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
77
- }),
78
- sampler: device.createSampler({
79
- addressModeU: 'clamp-to-edge', addressModeV: 'clamp-to-edge',
80
- }),
81
- image: emptyImage, format: 'rgba8unorm', srgb: false, uploaded: null,
82
- };
83
- shared._size = null;
84
- canvas.className = 'leaflet-3d';
85
- canvas.style.cssText = 'position:absolute;top:0;left:0;pointer-events:none';
86
- canvas.setAttribute('role', 'img');
87
- canvas.setAttribute('aria-label', backend.options.ariaLabel);
88
- let pane = map.getPane('webgpuPane');
89
- if (!pane) {
90
- pane = map.createPane('webgpuPane', map.getContainer());
91
- pane.style.zIndex = 450;
92
- }
93
- pane.append(canvas);
94
- map.on('move zoom rotate pitch resize viewreset layeradd layerremove', shared._onMapChange, shared);
95
- map.on('zoomstart', shared._startZoom, shared);
96
- map.on('zoomanim', shared.update, shared);
97
- map.on('zoomend', shared._endZoom, shared);
98
- rendererReady.resolve();
99
- delete shared._rendererReady;
100
- shared._render();
101
- L.Layer.types['3d']._renderers.set(map, shared);
102
- }
103
- const origin = map.options.crs.project(shared._sceneOrigin);
104
- const point = map.options.crs.project(backend.location);
105
- const metres = Math.cos(shared._sceneOrigin.lat * Math.PI / 180);
106
- backend._terrainBaseY ??= backend.root.position[1];
107
- backend.root.position[0] = (point.x - origin.x) * metres;
108
- backend.root.position[1] = backend._terrainBaseY;
109
- backend.root.position[2] = -(point.y - origin.y) * metres;
110
- backend._setTerrainHeight();
111
- // Each group's metres are measured at its own latitude, not the shared camera origin.
112
- backend.root.scale.fill(metres / Math.cos(backend.location.lat * Math.PI / 180));
113
- backend._shared = shared;
114
- backend.renderer = shared.renderer;
115
- backend.scene = shared.scene;
116
- backend.camera = shared.camera;
117
- shared._layers.add(backend);
118
- setParent(backend.root, shared.scene);
119
- traverse(backend.root, object => {
120
- if (object.geometry && object.geometry.device !== shared.renderer.device) {
121
- const geometry = object.geometry;
122
- const material = object.program._layerMaterial;
123
- // Retain packed CPU attributes, never old-context GPU buffer handles.
124
- const attributes = {};
125
- for (const name of ['position', 'normal', 'color', 'uv', 'index']) {
126
- const attribute = geometry.attributes[name];
127
- if (!attribute) continue;
128
- const { data, size, count, normalized } = attribute;
129
- attributes[name] = { data: data.slice(0, count * size), size, normalized };
130
- }
131
- const device = shared.renderer.device;
132
- if (attributes.index?.data instanceof Uint8Array) {
133
- attributes.index.data = Uint16Array.from(attributes.index.data);
134
- }
135
- for (const [name, attribute] of Object.entries(attributes)) {
136
- attribute.size ||= 1;
137
- attribute.count = attribute.data.length / attribute.size;
138
- attribute.indexFormat = attribute.data instanceof Uint32Array ? 'uint32' : 'uint16';
139
- attribute.buffer = device.createBuffer({
140
- size: Math.max(4, attribute.data.byteLength),
141
- usage: name === 'index'
142
- ? GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST
143
- : GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
144
- });
145
- device.queue.writeBuffer(attribute.buffer, 0, attribute.data);
146
- }
147
- object.geometry = {
148
- device,
149
- attributes,
150
- drawRange: { start: 0, count: attributes.index?.count || attributes.position.count },
151
- };
152
- for (const attribute of Object.values(geometry.attributes)) attribute.buffer.destroy();
153
- object.bindGroup = null;
154
- object.boundTextures = [];
155
- for (const key of ['baseTexture', 'metalTexture', 'emissiveTexture', 'occlusionTexture']) {
156
- const texture = material[key];
157
- if (!texture || texture.device === device) continue;
158
- material[key] = {
159
- ...texture,
160
- device,
161
- texture: device.createTexture({
162
- size: [texture.image.width, texture.image.height],
163
- format: texture.format,
164
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
165
- }),
166
- sampler: device.createSampler(texture.samplerOptions),
167
- uploaded: null,
168
- };
169
- }
170
- backend._removeProgram(object.program);
171
- object.program = backend._createProgram(material);
172
- object.device = device;
173
- }
174
- if (object.texture && object.texture.device !== shared.renderer.device) {
175
- object.texture = {
176
- ...object.texture,
177
- device: shared.renderer.device,
178
- texture: shared.renderer.device.createTexture({
179
- size: [object.texture.image.width, object.texture.image.height],
180
- format: object.texture.format,
181
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
182
- }),
183
- sampler: shared.renderer.device.createSampler(object.texture.samplerOptions),
184
- uploaded: null,
185
- };
186
- }
187
- });
188
- shared.update();
189
- if (backend.header || backend._geojson) backend._updateArea();
190
- }
191
-
192
- export function destroyTexture(backend, texture) {
193
- backend.renderer.device.queue.onSubmittedWorkDone().then(() => texture.destroy(), error => {
194
- backend.fire('error', { error });
195
- texture.destroy();
196
- });
197
- }
198
-
199
- export function createProgram(backend, material = {}) {
200
- const device = backend.renderer.device;
201
- const sparkle = Boolean(backend.options.nightSparkle);
202
- const shaders = materialShaders(material, sparkle, material.terrain ? L.Layer.types.terrain.elevationWGSL : '');
203
- const { bindings, textured, hasTerrain } = shaders;
204
- const vertex = device.createShaderModule({ code: shaders.vertex });
205
- const fragment = device.createShaderModule({ code: shaders.fragment });
206
- Promise.all([vertex.getCompilationInfo(), fragment.getCompilationInfo()]).then(infos => {
207
- const errors = infos.flatMap(info => info.messages).filter(message => message.type === 'error');
208
- if (errors.length) {
209
- backend.fire('error', {
210
- error: new Error(`Unable to compile Layer3D WGSL: ${errors.map(error => error.message).join('\n')}`),
211
- });
212
- }
213
- }).catch(error => backend.fire('error', { error }));
214
- const vertexBuffers = [
215
- { arrayStride: 12, attributes: [{ shaderLocation: 0, offset: 0, format: 'float32x3' }] },
216
- { arrayStride: 12, attributes: [{ shaderLocation: 1, offset: 0, format: 'float32x3' }] },
217
- material.vertexColors ? { arrayStride: 12, attributes: [{ shaderLocation: 2, offset: 0, format: 'float32x3' }] } : null,
218
- textured ? { arrayStride: 8, attributes: [{ shaderLocation: 3, offset: 0, format: 'float32x2' }] } : null,
219
- ];
220
- const bindGroupEntries = [{
221
- binding: 0,
222
- visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
223
- buffer: { type: 'uniform', hasDynamicOffset: true },
224
- }, {
225
- binding: 1,
226
- visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
227
- buffer: { type: 'uniform' },
228
- }];
229
- if (hasTerrain) {
230
- bindGroupEntries.push({
231
- binding: 2,
232
- visibility: GPUShaderStage.VERTEX,
233
- texture: { sampleType: 'unfilterable-float', viewDimension: '2d' },
234
- });
235
- }
236
- for (const texture of bindings) {
237
- bindGroupEntries.push({
238
- binding: texture.texture,
239
- visibility: GPUShaderStage.FRAGMENT,
240
- texture: { sampleType: 'float', viewDimension: '2d' },
241
- });
242
- bindGroupEntries.push({
243
- binding: texture.sampler,
244
- visibility: GPUShaderStage.FRAGMENT,
245
- sampler: { type: 'filtering' },
246
- });
247
- }
248
- const bindGroupLayout = device.createBindGroupLayout({ entries: bindGroupEntries });
249
- const pipelineLayout = device.createPipelineLayout({ bindGroupLayouts: [bindGroupLayout, backend.renderer.sunlight.layout] });
250
- return {
251
- device,
252
- vertex,
253
- fragment,
254
- bindings,
255
- vertexBuffers,
256
- bindGroupLayout,
257
- pipelineLayout,
258
- pipelineCache: new Map(),
259
- cullMode: material.doubleSided ? 'none' : 'back',
260
- transparent: material.alphaMode === 'BLEND',
261
- depthWrite: material.alphaMode !== 'BLEND',
262
- depthTest: material.depthTest !== false,
263
- terrain: material.terrain,
264
- nightLight: Boolean(material.nightLight),
265
- nightSparkle: sparkle,
266
- uniforms: {
267
- baseColor: { value: material.color || [1, 1, 1] },
268
- metallic: { value: material.metallic ?? 0 },
269
- roughness: { value: material.roughness ?? 0.85 },
270
- opacity: { value: material.opacity ?? 1 },
271
- alphaCutoff: { value: material.alphaCutoff ?? 0.5 },
272
- occlusionStrength: { value: material.occlusionStrength ?? 1 },
273
- emissive: { value: material.emissive || [0, 0, 0] },
274
- ...(material.baseTexture ? { baseTexture: { value: material.baseTexture } } : {}),
275
- ...(material.metalTexture ? { metalTexture: { value: material.metalTexture } } : {}),
276
- ...(material.emissiveTexture ? { emissiveTexture: { value: material.emissiveTexture } } : {}),
277
- ...(material.occlusionTexture ? { occlusionTexture: { value: material.occlusionTexture } } : {}),
278
- ...(material.roofTexture ? { roofTexture: { value: null } } : {}),
279
- },
280
- _layerMaterial: material,
281
- };
282
- }
283
-
284
- export function removeProgram(program) {
285
- program.pipelineCache.clear();
286
- }
287
-
288
- /**
289
- * Add a non-indexed, vertex-coloured mesh from flat vec3 arrays `{ position, normal, color }` in layer-local metres.
290
- * `usage` adds GPUBufferUsage flags to the vertex buffers (e.g. STORAGE for compute passes).
291
- */
292
- export function createMesh(backend, data, material = {}, usage = 0) {
293
- const { device } = backend.renderer, attributes = {};
294
- for (const [name, values] of Object.entries(data)) {
295
- const array = new Float32Array(values);
296
- const buffer = device.createBuffer({ size: Math.max(4, array.byteLength), usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST | usage });
297
- device.queue.writeBuffer(buffer, 0, array);
298
- attributes[name] = { size: 3, data: array, count: array.length / 3, buffer };
299
- }
300
- const mesh = {
301
- ...emptyNode(),
302
- device,
303
- geometry: { device, attributes, drawRange: { start: 0, count: attributes.position.count } },
304
- program: backend._createProgram({ vertexColors: true, doubleSided: true, ...material }),
305
- id: L.Layer.types['3d']._nextId++, renderOrder: 0, extras: {},
306
- };
307
- backend.add(mesh);
308
- return mesh;
309
- }
310
-
311
- export function disposeMesh(backend, mesh) {
312
- if (!mesh) return;
313
- backend.remove(mesh);
314
- for (const attribute of Object.values(mesh.geometry.attributes)) attribute.buffer.destroy();
315
- backend._removeProgram(mesh.program);
316
- }
317
-
318
- /** Upload retained CPU data and draw one mesh in the active WebGPU render pass. */
319
- export function drawGeometry(backend, mesh, pass, cullMode, frontFace) {
320
- const { geometry, program } = mesh;
321
- const device = backend.renderer.device;
322
- const pipelineKey = `${cullMode}:${frontFace}`;
323
- let pipeline = program.pipelineCache.get(pipelineKey);
324
- if (!pipeline) {
325
- pipeline = device.createRenderPipeline({
326
- layout: program.pipelineLayout,
327
- vertex: {
328
- module: program.vertex,
329
- entryPoint: 'vertexMain',
330
- buffers: program.vertexBuffers,
331
- },
332
- fragment: {
333
- module: program.fragment,
334
- entryPoint: 'fragmentMain',
335
- targets: [{
336
- format: backend.renderer.format,
337
- blend: program.transparent ? {
338
- color: { srcFactor: 'src-alpha', dstFactor: 'one-minus-src-alpha', operation: 'add' },
339
- alpha: { srcFactor: 'one', dstFactor: 'one-minus-src-alpha', operation: 'add' },
340
- } : undefined,
341
- writeMask: GPUColorWrite.ALL,
342
- }],
343
- },
344
- primitive: { topology: 'triangle-list', cullMode, frontFace },
345
- depthStencil: {
346
- format: 'depth24plus',
347
- depthWriteEnabled: program.depthWrite,
348
- depthCompare: program.depthTest === false ? 'always' : 'less-equal',
349
- },
350
- });
351
- program.pipelineCache.set(pipelineKey, pipeline);
352
- }
353
-
354
- const uniforms = program.uniforms;
355
- const renderer = backend.renderer;
356
- const offset = renderer.uniformSlot++ * 256;
357
- const data = renderer.uniformData, base = offset / 4;
358
- data.set(mesh.worldMatrix, base);
359
- data.set(uniforms.baseColor.value, base + 16);
360
- data[base + 19] = 1;
361
- data[base + 20] = uniforms.metallic.value;
362
- data[base + 21] = uniforms.roughness.value;
363
- data[base + 22] = uniforms.opacity.value;
364
- data[base + 23] = uniforms.alphaCutoff.value;
365
- data.set(uniforms.emissive.value, base + 24);
366
- data[base + 27] = uniforms.occlusionStrength.value;
367
- data[base + 28] = mesh.terrain?.[0] ?? 0;
368
- data[base + 29] = mesh.terrain?.[1] ?? 0;
369
- data[base + 30] = mesh.terrain?.[2] ?? 0;
370
- data[base + 31] = mesh.terrain?.[3] ?? 0;
371
- data[base + 32] = mesh.highlight?.[0] ?? 0;
372
- data[base + 33] = mesh.highlight?.[1] ?? 0;
373
- data[base + 34] = program.nightSparkle ? data[55] : 0;
374
- data[base + 35] = 0;
375
-
376
- const textures = mesh._textures ||= [];
377
- textures.length = 0;
378
- for (const binding of program.bindings) {
379
- const texture = uniforms[binding.name]?.value;
380
- if (!texture) throw new Error(`Missing Layer3D texture: ${binding.name}`);
381
- if (texture.uploaded !== texture.image) {
382
- device.queue.copyExternalImageToTexture(
383
- { source: texture.image, flipY: Boolean(texture.flipY) },
384
- { texture: texture.texture },
385
- [texture.image.width, texture.image.height],
386
- );
387
- texture.uploaded = texture.image;
388
- }
389
- textures.push(texture);
390
- }
391
- let bindingsChanged = !mesh.bindGroup || mesh.boundBuffer !== renderer.uniformBuffer ||
392
- textures.length !== mesh.boundTextures.length;
393
- for (let i = 0; !bindingsChanged && i < textures.length; i++) {
394
- if (textures[i].texture !== mesh.boundTextures[i][0] ||
395
- textures[i].sampler !== mesh.boundTextures[i][1]) bindingsChanged = true;
396
- }
397
- if (bindingsChanged) {
398
- const entries = [
399
- { binding: 0, resource: { buffer: renderer.uniformBuffer, size: 144 } },
400
- { binding: 1, resource: { buffer: renderer.uniformBuffer, size: 240 } },
401
- ];
402
- if (program.terrain) entries.push({ binding: 2, resource: program.terrain.texture.createView() });
403
- mesh.boundBuffer = renderer.uniformBuffer;
404
- const boundTextures = mesh.boundTextures ||= [];
405
- boundTextures.length = textures.length;
406
- for (let i = 0; i < textures.length; i++) {
407
- const binding = program.bindings[i], texture = textures[i];
408
- entries.push({ binding: binding.texture, resource: texture.texture.createView() });
409
- entries.push({ binding: binding.sampler, resource: texture.sampler });
410
- boundTextures[i] ||= [];
411
- boundTextures[i][0] = texture.texture;
412
- boundTextures[i][1] = texture.sampler;
413
- }
414
- mesh.bindGroup = device.createBindGroup({
415
- layout: program.bindGroupLayout,
416
- entries,
417
- });
418
- }
419
- pass.setPipeline(pipeline);
420
- pass.setBindGroup(0, mesh.bindGroup, [offset]);
421
- pass.setVertexBuffer(0, geometry.attributes.position.buffer);
422
- pass.setVertexBuffer(1, geometry.attributes.normal.buffer);
423
- if (geometry.attributes.color) pass.setVertexBuffer(2, geometry.attributes.color.buffer);
424
- if (geometry.attributes.uv) pass.setVertexBuffer(3, geometry.attributes.uv.buffer);
425
- for (const attribute of Object.values(geometry.attributes)) {
426
- if (!attribute.needsUpdate) continue;
427
- device.queue.writeBuffer(attribute.buffer, 0, attribute.data);
428
- attribute.needsUpdate = false;
429
- geometry.bounds = null;
430
- }
431
- const index = geometry.attributes.index;
432
- if (index) {
433
- pass.setIndexBuffer(index.buffer, index.indexFormat);
434
- pass.drawIndexed(geometry.drawRange.count, 1, geometry.drawRange.start, 0, 0);
435
- } else {
436
- pass.draw(geometry.drawRange.count, 1, geometry.drawRange.start, 0);
437
- }
438
- }
@@ -1,193 +0,0 @@
1
- import { lightGrid, worldLights } from '../utils/lighting.js';
2
- import { computeBounds } from '../utils/geometry.js';
3
- import { traverse, updateCamera } from '../utils/scene.js';
4
-
5
- /** Sunlight shadow maps and night point lights. */
6
- export function initSunlight(backend) {
7
- const device = backend.renderer.device;
8
- const layout = device.createBindGroupLayout({ entries: [
9
- { binding: 0, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, buffer: { type: 'uniform' } },
10
- { binding: 1, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: 'depth' } },
11
- { binding: 2, visibility: GPUShaderStage.FRAGMENT, sampler: { type: 'comparison' } },
12
- { binding: 3, visibility: GPUShaderStage.FRAGMENT, buffer: { type: 'read-only-storage' } },
13
- ] });
14
- const buffer = device.createBuffer({ size: 80, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });
15
- const sampler = device.createSampler({ compare: 'less-equal', minFilter: 'linear', magFilter: 'linear' });
16
- const lights = device.createBuffer({ size: 32, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST });
17
- backend.renderer.sunlight = { layout, buffer, sampler, lights, size: 0 };
18
- const values = new Float32Array(20);
19
- values.set([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
20
- device.queue.writeBuffer(buffer, 0, values);
21
- backend._resizeSunlight(1);
22
- }
23
-
24
- export function resizeSunlight(backend, size) {
25
- const device = backend.renderer.device, sun = backend.renderer.sunlight;
26
- if (sun.size === size) return;
27
- if (sun.texture) backend._destroyTexture(sun.texture);
28
- sun.size = size;
29
- sun.texture = device.createTexture({
30
- size: [size, size], format: 'depth32float',
31
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC,
32
- });
33
- backend._bindSunlight();
34
- }
35
-
36
- export function bindSunlight(backend) {
37
- const sun = backend.renderer.sunlight;
38
- sun.bindGroup = backend.renderer.device.createBindGroup({ layout: sun.layout, entries: [
39
- { binding: 0, resource: { buffer: sun.buffer } },
40
- { binding: 1, resource: sun.texture.createView() },
41
- { binding: 2, resource: sun.sampler },
42
- { binding: 3, resource: { buffer: sun.lights } },
43
- ] });
44
- }
45
-
46
- /** A cached spatial grid limits fragment lighting to nearby lamps, without dropping dense cells. */
47
- export function updatePointLights(backend) {
48
- const { device, sunlight: sun } = backend.renderer;
49
- const meshes = [];
50
- for (const layer of backend._layers) {
51
- if (!layer.options.nightLight || !layer.root.visible) continue;
52
- traverse(layer.root, mesh => {
53
- if (!mesh.visible) return true;
54
- if (mesh.extras?.lights) meshes.push(mesh);
55
- });
56
- }
57
- if (sun.lightMeshes?.length === meshes.length && meshes.every((mesh, i) => sun.lightMeshes[i].mesh === mesh &&
58
- sun.lightMeshes[i].lights === mesh.extras.lights && mesh.worldMatrix.every((v, j) => v === sun.lightMeshes[i].matrix[j]))) return;
59
- // Per mesh, world-space [x, y, z, radius, r, g, b, intensity] per light: re-packed only when its lights or matrix change.
60
- const packed = meshes.map(mesh => {
61
- const cache = mesh._worldLights, m = mesh.worldMatrix, lights = mesh.extras.lights;
62
- if (cache?.lights === lights && m.every((v, j) => v === cache.matrix[j])) return cache.data;
63
- const values = worldLights(lights, m);
64
- mesh._worldLights = { lights, matrix: [...m], data: values };
65
- return values;
66
- });
67
- const { data, count } = lightGrid(packed, device.limits.maxStorageBufferBindingSize);
68
- if (sun.lights.size < data.byteLength) {
69
- backend._destroyTexture(sun.lights);
70
- sun.lights = device.createBuffer({ size: data.byteLength, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST });
71
- backend._bindSunlight();
72
- }
73
- device.queue.writeBuffer(sun.lights, 0, data);
74
- sun.lightCount = count;
75
- sun.lightMeshes = meshes.map(mesh => ({ mesh, lights: mesh.extras.lights, matrix: [...mesh.worldMatrix] }));
76
- }
77
-
78
- /** One shared directional depth map. Off-screen but loaded geometry can cast shadows. */
79
- export function renderSunlight(backend, encoder, meshes, sunlight) {
80
- const renderer = backend.renderer, device = renderer.device, sun = renderer.sunlight;
81
- if (!meshes.length || !sunlight || sunlight.altitude < 3) {
82
- if (!sun.enabled) return;
83
- const values = new Float32Array(20);
84
- values.set([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
85
- device.queue.writeBuffer(sun.buffer, 0, values);
86
- sun.enabled = false;
87
- return;
88
- }
89
- const values = new Float32Array(20);
90
- sun.enabled = true;
91
- backend._resizeSunlight(Math.min(2048, device.limits.maxTextureDimension2D));
92
- const min = [Infinity, Infinity, Infinity], max = [-Infinity, -Infinity, -Infinity];
93
- for (const mesh of meshes) {
94
- if (!mesh.geometry.bounds) computeBounds(mesh.geometry);
95
- const bounds = mesh.geometry.bounds, matrix = new DOMMatrix(mesh.worldMatrix);
96
- for (let i = 0; i < 8; i++) {
97
- const point = new DOMPoint(...min.map((_, axis) => (i & (1 << axis)) ? bounds.max[axis] : bounds.min[axis]), 1)
98
- .matrixTransform(matrix);
99
- for (let axis = 0; axis < 3; axis++) {
100
- const value = [point.x, point.y, point.z][axis] / point.w;
101
- min[axis] = Math.min(min[axis], value);
102
- max[axis] = Math.max(max[axis], value);
103
- }
104
- }
105
- }
106
- // Include ground receivers below/around the loaded buildings and trees.
107
- min[1] = Math.min(0, min[1]);
108
- const radius = Math.max(100, Math.hypot(...min.map((v, i) => max[i] - v)));
109
- const center = min.map((v, i) => (v + max[i]) / 2);
110
- const lightCamera = {
111
- position: center.map((v, i) => v + sunlight.direction[i] * radius * 2),
112
- target: center, up: [0, 0, -1], fov: 45, aspect: 1, near: 1, far: radius * 5,
113
- viewMatrix: [], projectionMatrix: [], projectionViewMatrix: [],
114
- };
115
- updateCamera(lightCamera);
116
- const view = new DOMMatrix(lightCamera.viewMatrix);
117
- const low = [Infinity, Infinity, Infinity], high = [-Infinity, -Infinity, -Infinity];
118
- for (let i = 0; i < 8; i++) {
119
- const point = new DOMPoint(...min.map((v, axis) => (i & (1 << axis)) ? max[axis] : v), 1).matrixTransform(view);
120
- [point.x, point.y, point.z].forEach((value, axis) => {
121
- low[axis] = Math.min(low[axis], value - 20);
122
- high[axis] = Math.max(high[axis], value + 20);
123
- });
124
- }
125
- const span = low.map((v, i) => high[i] - v);
126
- const projection = new DOMMatrix([
127
- 2 / span[0], 0, 0, 0, 0, 2 / span[1], 0, 0, 0, 0, -1 / span[2], 0,
128
- -(high[0] + low[0]) / span[0], -(high[1] + low[1]) / span[1], high[2] / span[2], 1,
129
- ]);
130
- values.set(projection.multiply(view).toFloat32Array());
131
- values[16] = 1;
132
- device.queue.writeBuffer(sun.buffer, 0, values);
133
- if (!sun.pipeline) {
134
- sun.meshLayout = device.createBindGroupLayout({ entries: [
135
- { binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: 'uniform', hasDynamicOffset: true } },
136
- ] });
137
- // Do not bind the sampled depth texture while it is a render attachment.
138
- sun.depthLayout = device.createBindGroupLayout({ entries: [
139
- { binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: 'uniform' } },
140
- ] });
141
- sun.depthBindGroup = device.createBindGroup({
142
- layout: sun.depthLayout, entries: [{ binding: 0, resource: { buffer: sun.buffer } }],
143
- });
144
- const shader = device.createShaderModule({ code: `
145
- struct Mesh { world: mat4x4<f32> };
146
- @group(0) @binding(0) var<uniform> mesh: Mesh;
147
- @group(1) @binding(0) var<uniform> matrix: mat4x4<f32>;
148
- @vertex fn main(@location(0) position: vec3<f32>) -> @builtin(position) vec4<f32> {
149
- return matrix * mesh.world * vec4<f32>(position, 1.0);
150
- }
151
- ` });
152
- sun.pipeline = device.createRenderPipeline({
153
- layout: device.createPipelineLayout({ bindGroupLayouts: [sun.meshLayout, sun.depthLayout] }),
154
- vertex: { module: shader, entryPoint: 'main', buffers: [
155
- { arrayStride: 12, attributes: [{ shaderLocation: 0, offset: 0, format: 'float32x3' }] },
156
- ] },
157
- primitive: { topology: 'triangle-list', cullMode: 'none' },
158
- depthStencil: { format: 'depth32float', depthWriteEnabled: true, depthCompare: 'less',
159
- depthBias: 2, depthBiasSlopeScale: 2 },
160
- });
161
- }
162
- const pass = encoder.beginRenderPass({
163
- colorAttachments: [],
164
- depthStencilAttachment: { view: sun.texture.createView(), depthClearValue: 1,
165
- depthLoadOp: 'clear', depthStoreOp: 'store' },
166
- });
167
- pass.setPipeline(sun.pipeline);
168
- pass.setBindGroup(1, sun.depthBindGroup);
169
- for (const mesh of meshes) {
170
- const offset = renderer.uniformSlot++ * 256;
171
- renderer.uniformData.set(mesh.worldMatrix, offset / 4);
172
- if (mesh._shadowBuffer !== renderer.uniformBuffer) {
173
- mesh._shadowBuffer = renderer.uniformBuffer;
174
- mesh._shadowBindGroup = device.createBindGroup({ layout: sun.meshLayout, entries: [
175
- { binding: 0, resource: { buffer: renderer.uniformBuffer, size: 64 } },
176
- ] });
177
- }
178
- for (const attribute of Object.values(mesh.geometry.attributes)) {
179
- if (attribute.needsUpdate) {
180
- device.queue.writeBuffer(attribute.buffer, 0, attribute.data);
181
- attribute.needsUpdate = false;
182
- }
183
- }
184
- pass.setBindGroup(0, mesh._shadowBindGroup, [offset]);
185
- pass.setVertexBuffer(0, mesh.geometry.attributes.position.buffer);
186
- const index = mesh.geometry.attributes.index, range = mesh.geometry.drawRange;
187
- if (index) {
188
- pass.setIndexBuffer(index.buffer, index.indexFormat);
189
- pass.drawIndexed(range.count, 1, range.start, 0, 0);
190
- } else pass.draw(range.count, 1, range.start, 0);
191
- }
192
- pass.end();
193
- }