castle-web-cli 0.4.236 → 0.4.238

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 (218) hide show
  1. package/dist/api.d.ts +2 -0
  2. package/dist/bundle.d.ts +24 -0
  3. package/dist/bundle.js +447 -4
  4. package/dist/bundleLatin1.d.ts +6 -0
  5. package/dist/bundleLatin1.js +333 -0
  6. package/dist/castle-host/host.d.ts +10 -1
  7. package/dist/castle-host/host.js +78 -9
  8. package/dist/castleJson.d.ts +20 -0
  9. package/dist/castleJson.js +40 -0
  10. package/dist/deckExports.d.ts +35 -0
  11. package/dist/deckExports.js +365 -0
  12. package/dist/importCheckHook.d.ts +1 -0
  13. package/dist/importCheckHook.js +1 -1
  14. package/dist/index.js +5 -0
  15. package/dist/init.js +2 -1
  16. package/dist/save-deck.js +18 -13
  17. package/kits/base/castle.json +1 -1
  18. package/kits/base/sdk/README.md +47 -0
  19. package/kits/base/sdk/castle.d.ts +3 -1
  20. package/kits/base/sdk/castle.js +1 -0
  21. package/kits/base/sdk/commands.d.ts +25 -1
  22. package/kits/base/sdk/commands.js +29 -1
  23. package/kits/base/sdk/hub.d.ts +17 -0
  24. package/kits/base/sdk/hub.js +33 -9
  25. package/kits/base/sdk/localCache.d.ts +27 -0
  26. package/kits/base/sdk/localCache.js +115 -0
  27. package/kits/base/sdk/localCacheStore.d.ts +15 -0
  28. package/kits/base/sdk/localCacheStore.js +358 -0
  29. package/kits/base/sdk/transport.d.ts +1 -0
  30. package/kits/base/sdk/transport.js +4 -5
  31. package/kits/hub-game/CLAUDE.md +22 -2
  32. package/kits/hub-game/castle.json +4 -1
  33. package/kits/hub-game/code/client/act.js +66 -91
  34. package/kits/hub-game/code/client/actCore.js +134 -0
  35. package/kits/hub-game/code/client/car.js +35 -0
  36. package/kits/hub-game/code/client/chrome.js +207 -381
  37. package/kits/hub-game/code/client/crowdNames.js +2 -2
  38. package/kits/hub-game/code/client/emoteWheel.js +109 -0
  39. package/kits/hub-game/code/client/hats.js +2 -0
  40. package/kits/hub-game/code/client/hookLayer.js +99 -48
  41. package/kits/hub-game/code/client/hostGo.js +77 -0
  42. package/kits/hub-game/code/client/hubControls.js +125 -0
  43. package/kits/hub-game/code/client/hubEntry.js +187 -0
  44. package/kits/hub-game/code/client/hudSurface.js +148 -0
  45. package/kits/hub-game/code/client/hudTree.js +96 -0
  46. package/kits/hub-game/code/client/idle.js +59 -13
  47. package/kits/hub-game/code/client/invites.js +13 -4
  48. package/kits/hub-game/code/client/menuIcons.js +8 -0
  49. package/kits/hub-game/code/client/noTrips.js +38 -0
  50. package/kits/hub-game/code/client/qualityEntry.js +6 -1
  51. package/kits/hub-game/code/client/rehello.js +3 -1
  52. package/kits/hub-game/code/client/riders.js +26 -0
  53. package/kits/hub-game/code/client/role.js +79 -0
  54. package/kits/hub-game/code/client/safeLayout.js +32 -3
  55. package/kits/hub-game/code/client/sound.js +10 -3
  56. package/kits/hub-game/code/client/soundscape.js +2 -2
  57. package/kits/hub-game/code/client/soundscapeAmbience.js +38 -8
  58. package/kits/hub-game/code/client/soundscapeBaked.js +24 -5
  59. package/kits/hub-game/code/client/soundscapeCues.js +2 -1
  60. package/kits/hub-game/code/client/state.js +19 -0
  61. package/kits/hub-game/code/client/stuck.js +2 -1
  62. package/kits/hub-game/code/client/talkSurface.js +178 -0
  63. package/kits/hub-game/code/client/talkTree.js +238 -0
  64. package/kits/hub-game/code/client/tiers.js +8 -5
  65. package/kits/hub-game/code/client/uiDom.js +600 -0
  66. package/kits/hub-game/code/client/uiTree.js +359 -0
  67. package/kits/hub-game/code/client/vehicle.js +154 -0
  68. package/kits/hub-game/code/client/wear.js +27 -5
  69. package/kits/hub-game/code/client/worldLabels.js +3 -2
  70. package/kits/hub-game/code/client/yours.js +10 -3
  71. package/kits/hub-game/code/server/actors.js +4 -3
  72. package/kits/hub-game/code/server/analytics.js +56 -6
  73. package/kits/hub-game/code/server/decor.js +56 -16
  74. package/kits/hub-game/code/server/hub.js +258 -46
  75. package/kits/hub-game/code/server/idle.js +118 -5
  76. package/kits/hub-game/code/server/manifest.js +46 -4
  77. package/kits/hub-game/code/server/npcs.js +4 -2
  78. package/kits/hub-game/code/server/players.js +3 -0
  79. package/kits/hub-game/code/server/role.js +142 -0
  80. package/kits/hub-game/code/server/standing.js +6 -4
  81. package/kits/hub-game/code/server/vehicle.js +126 -0
  82. package/kits/hub-game/code/shared/car.js +52 -0
  83. package/kits/hub-game/code/shared/deckFlags.js +34 -0
  84. package/kits/hub-game/code/shared/deviceClass.js +39 -0
  85. package/kits/hub-game/code/shared/expression.js +9 -0
  86. package/kits/hub-game/code/systems/hub.js +300 -85
  87. package/kits/hub-game/docs/analytics.md +11 -2
  88. package/kits/hub-game/docs/contract.md +353 -14
  89. package/kits/hub-game/docs/vehicle-preset.md +132 -0
  90. package/kits/hub-game/models/hats/baker-cap.pxmodel +89 -0
  91. package/kits/hub-game/models/hats/chef-toque.pxmodel +60 -0
  92. package/kits/hub-game/models/hats/delivery-cap.pxmodel +105 -0
  93. package/kits/hub-game/models/hats/fire-helmet.pxmodel +87 -0
  94. package/kits/hub-game/models/hats/hard-hat.pxmodel +71 -0
  95. package/kits/hub-game/models/hats/nurse-cap.pxmodel +61 -0
  96. package/kits/hub-game/models/hats/police-cap.pxmodel +105 -0
  97. package/kits/hub-game/models/hats/teacher-beret.pxmodel +52 -0
  98. package/kits/hub-game/models/role/square/top-apron.pxmodel +109 -0
  99. package/kits/hub-game/models/role/square/top-cardigan.pxmodel +141 -0
  100. package/kits/hub-game/models/role/square/top-coat.pxmodel +157 -0
  101. package/kits/hub-game/models/role/square/top-scrubs.pxmodel +93 -0
  102. package/kits/hub-game/models/role/square/top-tabard.pxmodel +109 -0
  103. package/kits/hub-game/models/role/square/top-vest.pxmodel +109 -0
  104. package/kits/hub-game/models/role/tool-bag.pxmodel +75 -0
  105. package/kits/hub-game/models/role/tool-bell.pxmodel +71 -0
  106. package/kits/hub-game/models/role/tool-hose.pxmodel +60 -0
  107. package/kits/hub-game/models/role/tool-parcel.pxmodel +61 -0
  108. package/kits/hub-game/models/role/tool-torch.pxmodel +55 -0
  109. package/kits/hub-game/models/role/tool-tray.pxmodel +75 -0
  110. package/kits/hub-game/models/role/top-apron.pxmodel +70 -0
  111. package/kits/hub-game/models/role/top-cardigan.pxmodel +102 -0
  112. package/kits/hub-game/models/role/top-coat.pxmodel +79 -0
  113. package/kits/hub-game/models/role/top-scrubs.pxmodel +54 -0
  114. package/kits/hub-game/models/role/top-tabard.pxmodel +70 -0
  115. package/kits/hub-game/models/role/top-vest.pxmodel +70 -0
  116. package/kits/hub-game/package-lock.json +1 -1
  117. package/kits/hub-game/package.json +1 -1
  118. package/kits/hub-game/scripts/check.mjs +7 -4
  119. package/kits/hub-game/scripts/fixtures/car-vectors.json +279 -0
  120. package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +2 -1
  121. package/kits/hub-game/scripts/fixtures/hud-surface.json +670 -0
  122. package/kits/hub-game/scripts/fixtures/talk-surface.json +3930 -0
  123. package/kits/hub-game/scripts/gen-hats.mjs +46 -2
  124. package/kits/hub-game/scripts/gen-role.mjs +80 -0
  125. package/kits/hub-game/scripts/test-act-core.mjs +167 -0
  126. package/kits/hub-game/scripts/test-actions-client.mjs +34 -0
  127. package/kits/hub-game/scripts/test-actors.mjs +48 -0
  128. package/kits/hub-game/scripts/test-analytics.mjs +99 -5
  129. package/kits/hub-game/scripts/test-apply-deck.mjs +122 -0
  130. package/kits/hub-game/scripts/test-back-visibility.mjs +3 -1
  131. package/kits/hub-game/scripts/test-ballot.mjs +2 -0
  132. package/kits/hub-game/scripts/test-car.mjs +43 -0
  133. package/kits/hub-game/scripts/test-decor-client.mjs +2 -1
  134. package/kits/hub-game/scripts/test-decor.mjs +77 -0
  135. package/kits/hub-game/scripts/test-device-class.mjs +83 -0
  136. package/kits/hub-game/scripts/test-emote-wheel.mjs +374 -0
  137. package/kits/hub-game/scripts/test-hats.mjs +5 -4
  138. package/kits/hub-game/scripts/test-host-go.mjs +265 -0
  139. package/kits/hub-game/scripts/test-hub-controls.mjs +108 -0
  140. package/kits/hub-game/scripts/test-hub-entry.mjs +531 -0
  141. package/kits/hub-game/scripts/test-hub-ops.mjs +75 -0
  142. package/kits/hub-game/scripts/test-hub-trip.mjs +85 -0
  143. package/kits/hub-game/scripts/test-hud-clear.mjs +12 -2
  144. package/kits/hub-game/scripts/test-hud-rate.mjs +6 -2
  145. package/kits/hub-game/scripts/test-hud-surface.mjs +272 -0
  146. package/kits/hub-game/scripts/test-idle.mjs +347 -1
  147. package/kits/hub-game/scripts/test-interest-cells.mjs +8 -1
  148. package/kits/hub-game/scripts/test-invites-client.mjs +42 -1
  149. package/kits/hub-game/scripts/test-manifest.mjs +30 -0
  150. package/kits/hub-game/scripts/test-no-trips.mjs +42 -0
  151. package/kits/hub-game/scripts/test-placement-cache.mjs +1 -1
  152. package/kits/hub-game/scripts/test-quality.mjs +13 -0
  153. package/kits/hub-game/scripts/test-rehello.mjs +258 -3
  154. package/kits/hub-game/scripts/test-resume-pose-server.mjs +203 -0
  155. package/kits/hub-game/scripts/test-resync.mjs +183 -0
  156. package/kits/hub-game/scripts/test-role-client.mjs +421 -0
  157. package/kits/hub-game/scripts/test-role.mjs +350 -0
  158. package/kits/hub-game/scripts/test-safe-layout.mjs +50 -1
  159. package/kits/hub-game/scripts/test-soundscape.mjs +124 -7
  160. package/kits/hub-game/scripts/test-standing.mjs +15 -1
  161. package/kits/hub-game/scripts/test-talk-surface.mjs +296 -0
  162. package/kits/hub-game/scripts/test-tiers.mjs +13 -0
  163. package/kits/hub-game/scripts/test-ui-dom-dom.mjs +96 -0
  164. package/kits/hub-game/scripts/test-ui-tree.mjs +711 -0
  165. package/kits/hub-game/scripts/test-vehicle-client.mjs +159 -0
  166. package/kits/hub-game/scripts/test-vehicle.mjs +135 -0
  167. package/kits/hub-game/scripts/test-yours.mjs +33 -0
  168. package/kits/hub-game/tools/hubScene/export.mjs +613 -0
  169. package/kits/hub-game/tools/hubScene/hubdata.mjs +921 -0
  170. package/kits/hub-game/tools/hubScene/merge-root.mjs +39 -0
  171. package/kits/hub-game/tools/hubScene/nodeworld.mjs +227 -0
  172. package/kits/hub-game/tools/hubScene/test/export-fixture.mjs +798 -0
  173. package/kits/hub-game/tools/hubScene/test/fixture.json +637 -0
  174. package/kits/hub-game/tools/hubScene/test/paint-export.mjs +40 -0
  175. package/kits/hub-game/tools/hubScene/test/upload.test.mjs +655 -0
  176. package/kits/hub-game/tools/hubScene/upload.mjs +636 -0
  177. package/kits/hub-game/tools/hubScene/validate-manifest.mjs +291 -0
  178. package/kits/multiplayer-3d/castle.json +4 -4
  179. package/kits/multiplayer-3d/code/client/resumeGate.js +30 -2
  180. package/kits/multiplayer-3d/code/client/steer.js +3 -1
  181. package/kits/multiplayer-3d/code/systems/multiplayer.js +30 -2
  182. package/kits/multiplayer-3d/package-lock.json +1 -1
  183. package/kits/multiplayer-3d/scripts/test-resume-gate.mjs +74 -2
  184. package/kits/physics-3d/CLAUDE.md +1 -1
  185. package/kits/physics-3d/behaviors/Player.jsx +37 -27
  186. package/kits/physics-3d/castle.json +2 -2
  187. package/kits/physics-3d/docs/engine-geometry-memory.md +23 -0
  188. package/kits/physics-3d/engine3d/memoryprofile.js +17 -0
  189. package/kits/physics-3d/engine3d/playerRun.js +6 -1
  190. package/kits/physics-3d/engine3d/pxmodel.js +6 -1
  191. package/kits/physics-3d/engine3d/world3d.js +6 -3
  192. package/kits/physics-3d/package.json +1 -1
  193. package/kits/physics-3d/play/behaviors/Player.js +40 -28
  194. package/kits/physics-3d/scripts/test-memory-profile.mjs +237 -0
  195. package/kits/physics-3d/systems/physics3d.js +5 -2
  196. package/kits/real-time/castle.json +2 -2
  197. package/kits/real-time/code/client/messages.js +5 -0
  198. package/kits/real-time/code/server/session.js +28 -1
  199. package/kits/real-time/package-lock.json +1 -1
  200. package/package.json +5 -4
  201. package/vendor/three/LICENSE +21 -0
  202. package/vendor/three/build/three.core.js +58773 -0
  203. package/vendor/three/build/three.module.js +18251 -0
  204. package/vendor/three/examples/jsm/controls/OrbitControls.js +1860 -0
  205. package/vendor/three/examples/jsm/controls/TransformControls.js +1918 -0
  206. package/vendor/three/examples/jsm/exporters/GLTFExporter.js +3595 -0
  207. package/vendor/three/examples/jsm/geometries/RoundedBoxGeometry.js +216 -0
  208. package/vendor/three/examples/jsm/math/MeshSurfaceSampler.js +315 -0
  209. package/vendor/three/examples/jsm/postprocessing/EffectComposer.js +363 -0
  210. package/vendor/three/examples/jsm/postprocessing/MaskPass.js +195 -0
  211. package/vendor/three/examples/jsm/postprocessing/OutputPass.js +139 -0
  212. package/vendor/three/examples/jsm/postprocessing/Pass.js +191 -0
  213. package/vendor/three/examples/jsm/postprocessing/RenderPass.js +183 -0
  214. package/vendor/three/examples/jsm/postprocessing/ShaderPass.js +135 -0
  215. package/vendor/three/examples/jsm/shaders/CopyShader.js +52 -0
  216. package/vendor/three/examples/jsm/shaders/OutputShader.js +103 -0
  217. package/vendor/three/examples/jsm/utils/BufferGeometryUtils.js +1435 -0
  218. package/vendor/three/package.json +134 -0
@@ -0,0 +1,216 @@
1
+ import {
2
+ BoxGeometry,
3
+ Vector3
4
+ } from 'three';
5
+
6
+ const _tempNormal = new Vector3();
7
+
8
+ function getUv( faceDirVector, normal, uvAxis, projectionAxis, radius, sideLength ) {
9
+
10
+ const totArcLength = 2 * Math.PI * radius / 4;
11
+
12
+ // length of the planes between the arcs on each axis
13
+ const centerLength = Math.max( sideLength - 2 * radius, 0 );
14
+ const halfArc = Math.PI / 4;
15
+
16
+ // Get the vector projected onto the Y plane
17
+ _tempNormal.copy( normal );
18
+ _tempNormal[ projectionAxis ] = 0;
19
+ _tempNormal.normalize();
20
+
21
+ // total amount of UV space alloted to a single arc
22
+ const arcUvRatio = 0.5 * totArcLength / ( totArcLength + centerLength );
23
+
24
+ // the distance along one arc the point is at
25
+ const arcAngleRatio = 1.0 - ( _tempNormal.angleTo( faceDirVector ) / halfArc );
26
+
27
+ if ( Math.sign( _tempNormal[ uvAxis ] ) === 1 ) {
28
+
29
+ return arcAngleRatio * arcUvRatio;
30
+
31
+ } else {
32
+
33
+ // total amount of UV space alloted to the plane between the arcs
34
+ const lenUv = centerLength / ( totArcLength + centerLength );
35
+ return lenUv + arcUvRatio + arcUvRatio * ( 1.0 - arcAngleRatio );
36
+
37
+ }
38
+
39
+ }
40
+
41
+ /**
42
+ * A special type of box geometry with rounded corners and edges.
43
+ *
44
+ * ```js
45
+ * const geometry = new THREE.RoundedBoxGeometry();
46
+ * const material = new THREE.MeshStandardMaterial( { color: 0x00ff00 } );
47
+ * const cube = new THREE.Mesh( geometry, material );
48
+ * scene.add( cube );
49
+ * ```
50
+ *
51
+ * @augments BoxGeometry
52
+ * @three_import import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
53
+ */
54
+ class RoundedBoxGeometry extends BoxGeometry {
55
+
56
+ /**
57
+ * Constructs a new rounded box geometry.
58
+ *
59
+ * @param {number} [width=1] - The width. That is, the length of the edges parallel to the X axis.
60
+ * @param {number} [height=1] - The height. That is, the length of the edges parallel to the Y axis.
61
+ * @param {number} [depth=1] - The depth. That is, the length of the edges parallel to the Z axis.
62
+ * @param {number} [segments=2] - Number of segments that form the rounded corners.
63
+ * @param {number} [radius=0.1] - The radius of the rounded corners.
64
+ */
65
+ constructor( width = 1, height = 1, depth = 1, segments = 2, radius = 0.1 ) {
66
+
67
+ // calculate total segments needed &
68
+ // ensure it's odd so that we have a plane connecting the rounded corners
69
+ const totalSegments = segments * 2 + 1;
70
+
71
+ // ensure radius isn't bigger than shortest side
72
+ radius = Math.min( width / 2, height / 2, depth / 2, radius );
73
+
74
+ // start with a unit box geometry, its vertices will be modified to form the rounded box
75
+ super( 1, 1, 1, totalSegments, totalSegments, totalSegments );
76
+
77
+ this.type = 'RoundedBoxGeometry';
78
+
79
+ /**
80
+ * Holds the constructor parameters that have been
81
+ * used to generate the geometry. Any modification
82
+ * after instantiation does not change the geometry.
83
+ *
84
+ * @type {Object}
85
+ */
86
+ this.parameters = {
87
+ width: width,
88
+ height: height,
89
+ depth: depth,
90
+ segments: segments,
91
+ radius: radius,
92
+ };
93
+
94
+ // if totalSegments is 1, no rounding is needed - return regular box
95
+ if ( totalSegments === 1 ) return;
96
+
97
+ const geometry2 = this.toNonIndexed();
98
+
99
+ this.index = null;
100
+ this.attributes.position = geometry2.attributes.position;
101
+ this.attributes.normal = geometry2.attributes.normal;
102
+ this.attributes.uv = geometry2.attributes.uv;
103
+
104
+ //
105
+
106
+ const position = new Vector3();
107
+ const normal = new Vector3();
108
+
109
+ const box = new Vector3( width, height, depth ).divideScalar( 2 ).subScalar( radius );
110
+
111
+ const positions = this.attributes.position.array;
112
+ const normals = this.attributes.normal.array;
113
+ const uvs = this.attributes.uv.array;
114
+
115
+ const faceTris = positions.length / 6;
116
+ const faceDirVector = new Vector3();
117
+ const halfSegmentSize = 0.5 / totalSegments;
118
+
119
+ for ( let i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
120
+
121
+ position.fromArray( positions, i );
122
+ normal.copy( position );
123
+ normal.x -= Math.sign( normal.x ) * halfSegmentSize;
124
+ normal.y -= Math.sign( normal.y ) * halfSegmentSize;
125
+ normal.z -= Math.sign( normal.z ) * halfSegmentSize;
126
+ normal.normalize();
127
+
128
+ positions[ i + 0 ] = box.x * Math.sign( position.x ) + normal.x * radius;
129
+ positions[ i + 1 ] = box.y * Math.sign( position.y ) + normal.y * radius;
130
+ positions[ i + 2 ] = box.z * Math.sign( position.z ) + normal.z * radius;
131
+
132
+ normals[ i + 0 ] = normal.x;
133
+ normals[ i + 1 ] = normal.y;
134
+ normals[ i + 2 ] = normal.z;
135
+
136
+ const side = Math.floor( i / faceTris );
137
+
138
+ switch ( side ) {
139
+
140
+ case 0: // right
141
+
142
+ // generate UVs along Z then Y
143
+ faceDirVector.set( 1, 0, 0 );
144
+ uvs[ j + 0 ] = getUv( faceDirVector, normal, 'z', 'y', radius, depth );
145
+ uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'z', radius, height );
146
+ break;
147
+
148
+ case 1: // left
149
+
150
+ // generate UVs along Z then Y
151
+ faceDirVector.set( - 1, 0, 0 );
152
+ uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'z', 'y', radius, depth );
153
+ uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'z', radius, height );
154
+ break;
155
+
156
+ case 2: // top
157
+
158
+ // generate UVs along X then Z
159
+ faceDirVector.set( 0, 1, 0 );
160
+ uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'z', radius, width );
161
+ uvs[ j + 1 ] = getUv( faceDirVector, normal, 'z', 'x', radius, depth );
162
+ break;
163
+
164
+ case 3: // bottom
165
+
166
+ // generate UVs along X then Z
167
+ faceDirVector.set( 0, - 1, 0 );
168
+ uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'z', radius, width );
169
+ uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'z', 'x', radius, depth );
170
+ break;
171
+
172
+ case 4: // front
173
+
174
+ // generate UVs along X then Y
175
+ faceDirVector.set( 0, 0, 1 );
176
+ uvs[ j + 0 ] = 1.0 - getUv( faceDirVector, normal, 'x', 'y', radius, width );
177
+ uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'x', radius, height );
178
+ break;
179
+
180
+ case 5: // back
181
+
182
+ // generate UVs along X then Y
183
+ faceDirVector.set( 0, 0, - 1 );
184
+ uvs[ j + 0 ] = getUv( faceDirVector, normal, 'x', 'y', radius, width );
185
+ uvs[ j + 1 ] = 1.0 - getUv( faceDirVector, normal, 'y', 'x', radius, height );
186
+ break;
187
+
188
+ }
189
+
190
+ }
191
+
192
+ }
193
+
194
+ /**
195
+ * Factory method for creating an instance of this class from the given
196
+ * JSON object.
197
+ *
198
+ * @param {Object} data - A JSON object representing the serialized geometry.
199
+ * @returns {RoundedBoxGeometry} A new instance.
200
+ */
201
+ static fromJSON( data ) {
202
+
203
+ return new RoundedBoxGeometry(
204
+ data.width,
205
+ data.height,
206
+ data.depth,
207
+ data.segments,
208
+ data.radius
209
+ );
210
+
211
+ }
212
+
213
+
214
+ }
215
+
216
+ export { RoundedBoxGeometry };
@@ -0,0 +1,315 @@
1
+ import {
2
+ Triangle,
3
+ Vector2,
4
+ Vector3
5
+ } from 'three';
6
+
7
+ const _face = new Triangle();
8
+ const _color = new Vector3();
9
+ const _uva = new Vector2(), _uvb = new Vector2(), _uvc = new Vector2();
10
+
11
+ /**
12
+ * Utility class for sampling weighted random points on the surface of a mesh.
13
+ *
14
+ * Building the sampler is a one-time O(n) operation. Once built, any number of
15
+ * random samples may be selected in O(logn) time. Memory usage is O(n).
16
+ *
17
+ * References:
18
+ * - {@link http://www.joesfer.com/?p=84}
19
+ * - {@link https://stackoverflow.com/a/4322940/1314762}
20
+ *
21
+ * ```js
22
+ * const sampler = new MeshSurfaceSampler( surfaceMesh )
23
+ * .setWeightAttribute( 'color' )
24
+ * .build();
25
+ *
26
+ * const mesh = new THREE.InstancedMesh( sampleGeometry, sampleMaterial, 100 );
27
+ *
28
+ * const position = new THREE.Vector3();
29
+ * const matrix = new THREE.Matrix4();
30
+ *
31
+ * // Sample randomly from the surface, creating an instance of the sample geometry at each sample point.
32
+ *
33
+ * for ( let i = 0; i < 100; i ++ ) {
34
+ *
35
+ * sampler.sample( position );
36
+ * matrix.makeTranslation( position.x, position.y, position.z );
37
+ * mesh.setMatrixAt( i, matrix );
38
+ *
39
+ * }
40
+ *
41
+ * scene.add( mesh );
42
+ * ```
43
+ *
44
+ * @three_import import { MeshSurfaceSampler } from 'three/addons/math/MeshSurfaceSampler.js';
45
+ */
46
+ class MeshSurfaceSampler {
47
+
48
+ /**
49
+ * Constructs a mesh surface sampler.
50
+ *
51
+ * @param {Mesh} mesh - Surface mesh from which to sample.
52
+ */
53
+ constructor( mesh ) {
54
+
55
+ this.geometry = mesh.geometry;
56
+ this.randomFunction = Math.random;
57
+
58
+ this.indexAttribute = this.geometry.index;
59
+ this.positionAttribute = this.geometry.getAttribute( 'position' );
60
+ this.normalAttribute = this.geometry.getAttribute( 'normal' );
61
+ this.colorAttribute = this.geometry.getAttribute( 'color' );
62
+ this.uvAttribute = this.geometry.getAttribute( 'uv' );
63
+ this.weightAttribute = null;
64
+
65
+ this.distribution = null;
66
+
67
+ }
68
+
69
+ /**
70
+ * Specifies a vertex attribute to be used as a weight when sampling from the surface.
71
+ * Faces with higher weights are more likely to be sampled, and those with weights of
72
+ * zero will not be sampled at all. For vector attributes, only .x is used in sampling.
73
+ *
74
+ * If no weight attribute is selected, sampling is randomly distributed by area.
75
+ *
76
+ * @param {string} name - The attribute name.
77
+ * @return {MeshSurfaceSampler} A reference to this sampler.
78
+ */
79
+ setWeightAttribute( name ) {
80
+
81
+ this.weightAttribute = name ? this.geometry.getAttribute( name ) : null;
82
+
83
+ return this;
84
+
85
+ }
86
+
87
+ /**
88
+ * Processes the input geometry and prepares to return samples. Any configuration of the
89
+ * geometry or sampler must occur before this method is called. Time complexity is O(n)
90
+ * for a surface with n faces.
91
+ *
92
+ * @return {MeshSurfaceSampler} A reference to this sampler.
93
+ */
94
+ build() {
95
+
96
+ const indexAttribute = this.indexAttribute;
97
+ const positionAttribute = this.positionAttribute;
98
+ const weightAttribute = this.weightAttribute;
99
+
100
+ const totalFaces = indexAttribute ? ( indexAttribute.count / 3 ) : ( positionAttribute.count / 3 );
101
+ const faceWeights = new Float32Array( totalFaces );
102
+
103
+ // Accumulate weights for each mesh face.
104
+
105
+ for ( let i = 0; i < totalFaces; i ++ ) {
106
+
107
+ let faceWeight = 1;
108
+
109
+ let i0 = 3 * i;
110
+ let i1 = 3 * i + 1;
111
+ let i2 = 3 * i + 2;
112
+
113
+ if ( indexAttribute ) {
114
+
115
+ i0 = indexAttribute.getX( i0 );
116
+ i1 = indexAttribute.getX( i1 );
117
+ i2 = indexAttribute.getX( i2 );
118
+
119
+ }
120
+
121
+ if ( weightAttribute ) {
122
+
123
+ faceWeight = weightAttribute.getX( i0 )
124
+ + weightAttribute.getX( i1 )
125
+ + weightAttribute.getX( i2 );
126
+
127
+ }
128
+
129
+ _face.a.fromBufferAttribute( positionAttribute, i0 );
130
+ _face.b.fromBufferAttribute( positionAttribute, i1 );
131
+ _face.c.fromBufferAttribute( positionAttribute, i2 );
132
+ faceWeight *= _face.getArea();
133
+
134
+ faceWeights[ i ] = faceWeight;
135
+
136
+ }
137
+
138
+ // Store cumulative total face weights in an array, where weight index
139
+ // corresponds to face index.
140
+
141
+ const distribution = new Float32Array( totalFaces );
142
+ let cumulativeTotal = 0;
143
+
144
+ for ( let i = 0; i < totalFaces; i ++ ) {
145
+
146
+ cumulativeTotal += faceWeights[ i ];
147
+ distribution[ i ] = cumulativeTotal;
148
+
149
+ }
150
+
151
+ this.distribution = distribution;
152
+ return this;
153
+
154
+ }
155
+
156
+ /**
157
+ * Allows to set a custom random number generator. Default is `Math.random()`.
158
+ *
159
+ * @param {Function} randomFunction - A random number generator.
160
+ * @return {MeshSurfaceSampler} A reference to this sampler.
161
+ */
162
+ setRandomGenerator( randomFunction ) {
163
+
164
+ this.randomFunction = randomFunction;
165
+ return this;
166
+
167
+ }
168
+
169
+ /**
170
+ * Selects a random point on the surface of the input geometry, returning the
171
+ * position and optionally the normal vector, color and UV Coordinate at that point.
172
+ * Time complexity is O(log n) for a surface with n faces.
173
+ *
174
+ * @param {Vector3} targetPosition - The target object holding the sampled position.
175
+ * @param {Vector3} targetNormal - The target object holding the sampled normal.
176
+ * @param {Color} targetColor - The target object holding the sampled color.
177
+ * @param {Vector2} targetUV - The target object holding the sampled uv coordinates.
178
+ * @return {MeshSurfaceSampler} A reference to this sampler.
179
+ */
180
+ sample( targetPosition, targetNormal, targetColor, targetUV ) {
181
+
182
+ const faceIndex = this._sampleFaceIndex();
183
+ return this._sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV );
184
+
185
+ }
186
+
187
+ // private
188
+
189
+ _sampleFaceIndex() {
190
+
191
+ const cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
192
+ return this._binarySearch( this.randomFunction() * cumulativeTotal );
193
+
194
+ }
195
+
196
+ _binarySearch( x ) {
197
+
198
+ const dist = this.distribution;
199
+ let start = 0;
200
+ let end = dist.length - 1;
201
+
202
+ let index = - 1;
203
+
204
+ while ( start <= end ) {
205
+
206
+ const mid = Math.ceil( ( start + end ) / 2 );
207
+
208
+ if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
209
+
210
+ index = mid;
211
+
212
+ break;
213
+
214
+ } else if ( x < dist[ mid ] ) {
215
+
216
+ end = mid - 1;
217
+
218
+ } else {
219
+
220
+ start = mid + 1;
221
+
222
+ }
223
+
224
+ }
225
+
226
+ return index;
227
+
228
+ }
229
+
230
+ _sampleFace( faceIndex, targetPosition, targetNormal, targetColor, targetUV ) {
231
+
232
+ let u = this.randomFunction();
233
+ let v = this.randomFunction();
234
+
235
+ if ( u + v > 1 ) {
236
+
237
+ u = 1 - u;
238
+ v = 1 - v;
239
+
240
+ }
241
+
242
+ // get the vertex attribute indices
243
+ const indexAttribute = this.indexAttribute;
244
+ let i0 = faceIndex * 3;
245
+ let i1 = faceIndex * 3 + 1;
246
+ let i2 = faceIndex * 3 + 2;
247
+ if ( indexAttribute ) {
248
+
249
+ i0 = indexAttribute.getX( i0 );
250
+ i1 = indexAttribute.getX( i1 );
251
+ i2 = indexAttribute.getX( i2 );
252
+
253
+ }
254
+
255
+ _face.a.fromBufferAttribute( this.positionAttribute, i0 );
256
+ _face.b.fromBufferAttribute( this.positionAttribute, i1 );
257
+ _face.c.fromBufferAttribute( this.positionAttribute, i2 );
258
+
259
+ targetPosition
260
+ .set( 0, 0, 0 )
261
+ .addScaledVector( _face.a, u )
262
+ .addScaledVector( _face.b, v )
263
+ .addScaledVector( _face.c, 1 - ( u + v ) );
264
+
265
+ if ( targetNormal !== undefined ) {
266
+
267
+ if ( this.normalAttribute !== undefined ) {
268
+
269
+ _face.a.fromBufferAttribute( this.normalAttribute, i0 );
270
+ _face.b.fromBufferAttribute( this.normalAttribute, i1 );
271
+ _face.c.fromBufferAttribute( this.normalAttribute, i2 );
272
+ targetNormal.set( 0, 0, 0 ).addScaledVector( _face.a, u ).addScaledVector( _face.b, v ).addScaledVector( _face.c, 1 - ( u + v ) ).normalize();
273
+
274
+ } else {
275
+
276
+ _face.getNormal( targetNormal );
277
+
278
+ }
279
+
280
+ }
281
+
282
+ if ( targetColor !== undefined && this.colorAttribute !== undefined ) {
283
+
284
+ _face.a.fromBufferAttribute( this.colorAttribute, i0 );
285
+ _face.b.fromBufferAttribute( this.colorAttribute, i1 );
286
+ _face.c.fromBufferAttribute( this.colorAttribute, i2 );
287
+
288
+ _color
289
+ .set( 0, 0, 0 )
290
+ .addScaledVector( _face.a, u )
291
+ .addScaledVector( _face.b, v )
292
+ .addScaledVector( _face.c, 1 - ( u + v ) );
293
+
294
+ targetColor.r = _color.x;
295
+ targetColor.g = _color.y;
296
+ targetColor.b = _color.z;
297
+
298
+ }
299
+
300
+ if ( targetUV !== undefined && this.uvAttribute !== undefined ) {
301
+
302
+ _uva.fromBufferAttribute( this.uvAttribute, i0 );
303
+ _uvb.fromBufferAttribute( this.uvAttribute, i1 );
304
+ _uvc.fromBufferAttribute( this.uvAttribute, i2 );
305
+ targetUV.set( 0, 0 ).addScaledVector( _uva, u ).addScaledVector( _uvb, v ).addScaledVector( _uvc, 1 - ( u + v ) );
306
+
307
+ }
308
+
309
+ return this;
310
+
311
+ }
312
+
313
+ }
314
+
315
+ export { MeshSurfaceSampler };