MarchingCubes.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. Color,
  5. DynamicDrawUsage,
  6. Mesh,
  7. Sphere,
  8. Vector3
  9. } from 'three';
  10. /**
  11. * A marching cubes implementation.
  12. *
  13. * Port of: {@link http://webglsamples.org/blob/blob.html}
  14. *
  15. * @three_import import { MarchingCubes } from 'three/addons/objects/MarchingCubes.js';
  16. */
  17. class MarchingCubes extends Mesh {
  18. /**
  19. * Constructs a new marching cubes instance.
  20. *
  21. * @param {number} resolution - The effect's resolution.
  22. * @param {Material} material - The cube's material.
  23. * @param {boolean} [enableUvs=false] - Whether texture coordinates should be animated or not.
  24. * @param {boolean} [enableColors=false] - Whether colors should be animated or not.
  25. * @param {number} [maxPolyCount=10000] - The maximum size of the geometry buffers.
  26. */
  27. constructor( resolution, material, enableUvs = false, enableColors = false, maxPolyCount = 10000 ) {
  28. const geometry = new BufferGeometry();
  29. super( geometry, material );
  30. /**
  31. * This flag can be used for type testing.
  32. *
  33. * @type {boolean}
  34. * @readonly
  35. * @default true
  36. */
  37. this.isMarchingCubes = true;
  38. const scope = this;
  39. // temp buffers used in polygonize
  40. const vlist = new Float32Array( 12 * 3 );
  41. const nlist = new Float32Array( 12 * 3 );
  42. const clist = new Float32Array( 12 * 3 );
  43. /**
  44. * Whether texture coordinates should be animated or not.
  45. *
  46. * @type {boolean}
  47. * @default false
  48. */
  49. this.enableUvs = enableUvs;
  50. /**
  51. * Whether colors should be animated or not.
  52. *
  53. * @type {boolean}
  54. * @default false
  55. */
  56. this.enableColors = enableColors;
  57. // functions have to be object properties
  58. // prototype functions kill performance
  59. // (tested and it was 4x slower !!!)
  60. this.init = function ( resolution ) {
  61. this.resolution = resolution;
  62. // parameters
  63. this.isolation = 80.0;
  64. // size of field, 32 is pushing it in Javascript :)
  65. this.size = resolution;
  66. this.size2 = this.size * this.size;
  67. this.size3 = this.size2 * this.size;
  68. this.halfsize = this.size / 2.0;
  69. // deltas
  70. this.delta = 2.0 / this.size;
  71. this.yd = this.size;
  72. this.zd = this.size2;
  73. this.field = new Float32Array( this.size3 );
  74. this.normal_cache = new Float32Array( this.size3 * 3 );
  75. this.palette = new Float32Array( this.size3 * 3 );
  76. //
  77. this.count = 0;
  78. const maxVertexCount = maxPolyCount * 3;
  79. this.positionArray = new Float32Array( maxVertexCount * 3 );
  80. const positionAttribute = new BufferAttribute( this.positionArray, 3 );
  81. positionAttribute.setUsage( DynamicDrawUsage );
  82. geometry.setAttribute( 'position', positionAttribute );
  83. this.normalArray = new Float32Array( maxVertexCount * 3 );
  84. const normalAttribute = new BufferAttribute( this.normalArray, 3 );
  85. normalAttribute.setUsage( DynamicDrawUsage );
  86. geometry.setAttribute( 'normal', normalAttribute );
  87. if ( this.enableUvs ) {
  88. this.uvArray = new Float32Array( maxVertexCount * 2 );
  89. const uvAttribute = new BufferAttribute( this.uvArray, 2 );
  90. uvAttribute.setUsage( DynamicDrawUsage );
  91. geometry.setAttribute( 'uv', uvAttribute );
  92. }
  93. if ( this.enableColors ) {
  94. this.colorArray = new Float32Array( maxVertexCount * 3 );
  95. const colorAttribute = new BufferAttribute( this.colorArray, 3 );
  96. colorAttribute.setUsage( DynamicDrawUsage );
  97. geometry.setAttribute( 'color', colorAttribute );
  98. }
  99. geometry.boundingSphere = new Sphere( new Vector3(), 1 );
  100. };
  101. ///////////////////////
  102. // Polygonization
  103. ///////////////////////
  104. function lerp( a, b, t ) {
  105. return a + ( b - a ) * t;
  106. }
  107. function VIntX( q, offset, isol, x, y, z, valp1, valp2, c_offset1, c_offset2 ) {
  108. const mu = ( isol - valp1 ) / ( valp2 - valp1 ),
  109. nc = scope.normal_cache;
  110. vlist[ offset + 0 ] = x + mu * scope.delta;
  111. vlist[ offset + 1 ] = y;
  112. vlist[ offset + 2 ] = z;
  113. nlist[ offset + 0 ] = lerp( nc[ q + 0 ], nc[ q + 3 ], mu );
  114. nlist[ offset + 1 ] = lerp( nc[ q + 1 ], nc[ q + 4 ], mu );
  115. nlist[ offset + 2 ] = lerp( nc[ q + 2 ], nc[ q + 5 ], mu );
  116. clist[ offset + 0 ] = lerp( scope.palette[ c_offset1 * 3 + 0 ], scope.palette[ c_offset2 * 3 + 0 ], mu );
  117. clist[ offset + 1 ] = lerp( scope.palette[ c_offset1 * 3 + 1 ], scope.palette[ c_offset2 * 3 + 1 ], mu );
  118. clist[ offset + 2 ] = lerp( scope.palette[ c_offset1 * 3 + 2 ], scope.palette[ c_offset2 * 3 + 2 ], mu );
  119. }
  120. function VIntY( q, offset, isol, x, y, z, valp1, valp2, c_offset1, c_offset2 ) {
  121. const mu = ( isol - valp1 ) / ( valp2 - valp1 ),
  122. nc = scope.normal_cache;
  123. vlist[ offset + 0 ] = x;
  124. vlist[ offset + 1 ] = y + mu * scope.delta;
  125. vlist[ offset + 2 ] = z;
  126. const q2 = q + scope.yd * 3;
  127. nlist[ offset + 0 ] = lerp( nc[ q + 0 ], nc[ q2 + 0 ], mu );
  128. nlist[ offset + 1 ] = lerp( nc[ q + 1 ], nc[ q2 + 1 ], mu );
  129. nlist[ offset + 2 ] = lerp( nc[ q + 2 ], nc[ q2 + 2 ], mu );
  130. clist[ offset + 0 ] = lerp( scope.palette[ c_offset1 * 3 + 0 ], scope.palette[ c_offset2 * 3 + 0 ], mu );
  131. clist[ offset + 1 ] = lerp( scope.palette[ c_offset1 * 3 + 1 ], scope.palette[ c_offset2 * 3 + 1 ], mu );
  132. clist[ offset + 2 ] = lerp( scope.palette[ c_offset1 * 3 + 2 ], scope.palette[ c_offset2 * 3 + 2 ], mu );
  133. }
  134. function VIntZ( q, offset, isol, x, y, z, valp1, valp2, c_offset1, c_offset2 ) {
  135. const mu = ( isol - valp1 ) / ( valp2 - valp1 ),
  136. nc = scope.normal_cache;
  137. vlist[ offset + 0 ] = x;
  138. vlist[ offset + 1 ] = y;
  139. vlist[ offset + 2 ] = z + mu * scope.delta;
  140. const q2 = q + scope.zd * 3;
  141. nlist[ offset + 0 ] = lerp( nc[ q + 0 ], nc[ q2 + 0 ], mu );
  142. nlist[ offset + 1 ] = lerp( nc[ q + 1 ], nc[ q2 + 1 ], mu );
  143. nlist[ offset + 2 ] = lerp( nc[ q + 2 ], nc[ q2 + 2 ], mu );
  144. clist[ offset + 0 ] = lerp( scope.palette[ c_offset1 * 3 + 0 ], scope.palette[ c_offset2 * 3 + 0 ], mu );
  145. clist[ offset + 1 ] = lerp( scope.palette[ c_offset1 * 3 + 1 ], scope.palette[ c_offset2 * 3 + 1 ], mu );
  146. clist[ offset + 2 ] = lerp( scope.palette[ c_offset1 * 3 + 2 ], scope.palette[ c_offset2 * 3 + 2 ], mu );
  147. }
  148. function compNorm( q ) {
  149. const q3 = q * 3;
  150. if ( scope.normal_cache[ q3 ] === 0.0 ) {
  151. scope.normal_cache[ q3 + 0 ] = scope.field[ q - 1 ] - scope.field[ q + 1 ];
  152. scope.normal_cache[ q3 + 1 ] =
  153. scope.field[ q - scope.yd ] - scope.field[ q + scope.yd ];
  154. scope.normal_cache[ q3 + 2 ] =
  155. scope.field[ q - scope.zd ] - scope.field[ q + scope.zd ];
  156. }
  157. }
  158. // Returns total number of triangles. Fills triangles.
  159. // (this is where most of time is spent - it's inner work of O(n3) loop )
  160. function polygonize( fx, fy, fz, q, isol ) {
  161. // cache indices
  162. const q1 = q + 1,
  163. qy = q + scope.yd,
  164. qz = q + scope.zd,
  165. q1y = q1 + scope.yd,
  166. q1z = q1 + scope.zd,
  167. qyz = q + scope.yd + scope.zd,
  168. q1yz = q1 + scope.yd + scope.zd;
  169. let cubeindex = 0;
  170. const field0 = scope.field[ q ],
  171. field1 = scope.field[ q1 ],
  172. field2 = scope.field[ qy ],
  173. field3 = scope.field[ q1y ],
  174. field4 = scope.field[ qz ],
  175. field5 = scope.field[ q1z ],
  176. field6 = scope.field[ qyz ],
  177. field7 = scope.field[ q1yz ];
  178. if ( field0 < isol ) cubeindex |= 1;
  179. if ( field1 < isol ) cubeindex |= 2;
  180. if ( field2 < isol ) cubeindex |= 8;
  181. if ( field3 < isol ) cubeindex |= 4;
  182. if ( field4 < isol ) cubeindex |= 16;
  183. if ( field5 < isol ) cubeindex |= 32;
  184. if ( field6 < isol ) cubeindex |= 128;
  185. if ( field7 < isol ) cubeindex |= 64;
  186. // if cube is entirely in/out of the surface - bail, nothing to draw
  187. const bits = edgeTable[ cubeindex ];
  188. if ( bits === 0 ) return 0;
  189. const d = scope.delta,
  190. fx2 = fx + d,
  191. fy2 = fy + d,
  192. fz2 = fz + d;
  193. // top of the cube
  194. if ( bits & 1 ) {
  195. compNorm( q );
  196. compNorm( q1 );
  197. VIntX( q * 3, 0, isol, fx, fy, fz, field0, field1, q, q1 );
  198. }
  199. if ( bits & 2 ) {
  200. compNorm( q1 );
  201. compNorm( q1y );
  202. VIntY( q1 * 3, 3, isol, fx2, fy, fz, field1, field3, q1, q1y );
  203. }
  204. if ( bits & 4 ) {
  205. compNorm( qy );
  206. compNorm( q1y );
  207. VIntX( qy * 3, 6, isol, fx, fy2, fz, field2, field3, qy, q1y );
  208. }
  209. if ( bits & 8 ) {
  210. compNorm( q );
  211. compNorm( qy );
  212. VIntY( q * 3, 9, isol, fx, fy, fz, field0, field2, q, qy );
  213. }
  214. // bottom of the cube
  215. if ( bits & 16 ) {
  216. compNorm( qz );
  217. compNorm( q1z );
  218. VIntX( qz * 3, 12, isol, fx, fy, fz2, field4, field5, qz, q1z );
  219. }
  220. if ( bits & 32 ) {
  221. compNorm( q1z );
  222. compNorm( q1yz );
  223. VIntY(
  224. q1z * 3,
  225. 15,
  226. isol,
  227. fx2,
  228. fy,
  229. fz2,
  230. field5,
  231. field7,
  232. q1z,
  233. q1yz
  234. );
  235. }
  236. if ( bits & 64 ) {
  237. compNorm( qyz );
  238. compNorm( q1yz );
  239. VIntX(
  240. qyz * 3,
  241. 18,
  242. isol,
  243. fx,
  244. fy2,
  245. fz2,
  246. field6,
  247. field7,
  248. qyz,
  249. q1yz
  250. );
  251. }
  252. if ( bits & 128 ) {
  253. compNorm( qz );
  254. compNorm( qyz );
  255. VIntY( qz * 3, 21, isol, fx, fy, fz2, field4, field6, qz, qyz );
  256. }
  257. // vertical lines of the cube
  258. if ( bits & 256 ) {
  259. compNorm( q );
  260. compNorm( qz );
  261. VIntZ( q * 3, 24, isol, fx, fy, fz, field0, field4, q, qz );
  262. }
  263. if ( bits & 512 ) {
  264. compNorm( q1 );
  265. compNorm( q1z );
  266. VIntZ( q1 * 3, 27, isol, fx2, fy, fz, field1, field5, q1, q1z );
  267. }
  268. if ( bits & 1024 ) {
  269. compNorm( q1y );
  270. compNorm( q1yz );
  271. VIntZ(
  272. q1y * 3,
  273. 30,
  274. isol,
  275. fx2,
  276. fy2,
  277. fz,
  278. field3,
  279. field7,
  280. q1y,
  281. q1yz
  282. );
  283. }
  284. if ( bits & 2048 ) {
  285. compNorm( qy );
  286. compNorm( qyz );
  287. VIntZ( qy * 3, 33, isol, fx, fy2, fz, field2, field6, qy, qyz );
  288. }
  289. cubeindex <<= 4; // re-purpose cubeindex into an offset into triTable
  290. let o1,
  291. o2,
  292. o3,
  293. numtris = 0,
  294. i = 0;
  295. // here is where triangles are created
  296. while ( triTable[ cubeindex + i ] != - 1 ) {
  297. o1 = cubeindex + i;
  298. o2 = o1 + 1;
  299. o3 = o1 + 2;
  300. posnormtriv(
  301. vlist,
  302. nlist,
  303. clist,
  304. 3 * triTable[ o1 ],
  305. 3 * triTable[ o2 ],
  306. 3 * triTable[ o3 ]
  307. );
  308. i += 3;
  309. numtris ++;
  310. }
  311. return numtris;
  312. }
  313. function posnormtriv( pos, norm, colors, o1, o2, o3 ) {
  314. const c = scope.count * 3;
  315. // positions
  316. scope.positionArray[ c + 0 ] = pos[ o1 ];
  317. scope.positionArray[ c + 1 ] = pos[ o1 + 1 ];
  318. scope.positionArray[ c + 2 ] = pos[ o1 + 2 ];
  319. scope.positionArray[ c + 3 ] = pos[ o2 ];
  320. scope.positionArray[ c + 4 ] = pos[ o2 + 1 ];
  321. scope.positionArray[ c + 5 ] = pos[ o2 + 2 ];
  322. scope.positionArray[ c + 6 ] = pos[ o3 ];
  323. scope.positionArray[ c + 7 ] = pos[ o3 + 1 ];
  324. scope.positionArray[ c + 8 ] = pos[ o3 + 2 ];
  325. // normals
  326. if ( scope.material.flatShading === true ) {
  327. const nx = ( norm[ o1 + 0 ] + norm[ o2 + 0 ] + norm[ o3 + 0 ] ) / 3;
  328. const ny = ( norm[ o1 + 1 ] + norm[ o2 + 1 ] + norm[ o3 + 1 ] ) / 3;
  329. const nz = ( norm[ o1 + 2 ] + norm[ o2 + 2 ] + norm[ o3 + 2 ] ) / 3;
  330. scope.normalArray[ c + 0 ] = nx;
  331. scope.normalArray[ c + 1 ] = ny;
  332. scope.normalArray[ c + 2 ] = nz;
  333. scope.normalArray[ c + 3 ] = nx;
  334. scope.normalArray[ c + 4 ] = ny;
  335. scope.normalArray[ c + 5 ] = nz;
  336. scope.normalArray[ c + 6 ] = nx;
  337. scope.normalArray[ c + 7 ] = ny;
  338. scope.normalArray[ c + 8 ] = nz;
  339. } else {
  340. scope.normalArray[ c + 0 ] = norm[ o1 + 0 ];
  341. scope.normalArray[ c + 1 ] = norm[ o1 + 1 ];
  342. scope.normalArray[ c + 2 ] = norm[ o1 + 2 ];
  343. scope.normalArray[ c + 3 ] = norm[ o2 + 0 ];
  344. scope.normalArray[ c + 4 ] = norm[ o2 + 1 ];
  345. scope.normalArray[ c + 5 ] = norm[ o2 + 2 ];
  346. scope.normalArray[ c + 6 ] = norm[ o3 + 0 ];
  347. scope.normalArray[ c + 7 ] = norm[ o3 + 1 ];
  348. scope.normalArray[ c + 8 ] = norm[ o3 + 2 ];
  349. }
  350. // uvs
  351. if ( scope.enableUvs ) {
  352. const d = scope.count * 2;
  353. scope.uvArray[ d + 0 ] = pos[ o1 + 0 ];
  354. scope.uvArray[ d + 1 ] = pos[ o1 + 2 ];
  355. scope.uvArray[ d + 2 ] = pos[ o2 + 0 ];
  356. scope.uvArray[ d + 3 ] = pos[ o2 + 2 ];
  357. scope.uvArray[ d + 4 ] = pos[ o3 + 0 ];
  358. scope.uvArray[ d + 5 ] = pos[ o3 + 2 ];
  359. }
  360. // colors
  361. if ( scope.enableColors ) {
  362. scope.colorArray[ c + 0 ] = colors[ o1 + 0 ];
  363. scope.colorArray[ c + 1 ] = colors[ o1 + 1 ];
  364. scope.colorArray[ c + 2 ] = colors[ o1 + 2 ];
  365. scope.colorArray[ c + 3 ] = colors[ o2 + 0 ];
  366. scope.colorArray[ c + 4 ] = colors[ o2 + 1 ];
  367. scope.colorArray[ c + 5 ] = colors[ o2 + 2 ];
  368. scope.colorArray[ c + 6 ] = colors[ o3 + 0 ];
  369. scope.colorArray[ c + 7 ] = colors[ o3 + 1 ];
  370. scope.colorArray[ c + 8 ] = colors[ o3 + 2 ];
  371. }
  372. scope.count += 3;
  373. }
  374. /////////////////////////////////////
  375. // Metaballs
  376. /////////////////////////////////////
  377. /**
  378. * Adds a reciprocal ball (nice and blobby) that, to be fast, fades to zero after
  379. * a fixed distance, determined by strength and subtract.
  380. *
  381. * @param {number} ballx - The x-coordinate of the ball.
  382. * @param {number} bally - The y-coordinate of the ball.
  383. * @param {number} ballz - The z-coordinate of the ball.
  384. * @param {number} strength - The strength factor.
  385. * @param {number} subtract - The subtract factor.
  386. * @param {Color} colors - The color.
  387. */
  388. this.addBall = function ( ballx, bally, ballz, strength, subtract, colors ) {
  389. const sign = Math.sign( strength );
  390. strength = Math.abs( strength );
  391. const userDefineColor = ! ( colors === undefined || colors === null );
  392. let ballColor = new Color( ballx, bally, ballz );
  393. if ( userDefineColor ) {
  394. try {
  395. ballColor =
  396. colors instanceof Color
  397. ? colors
  398. : Array.isArray( colors )
  399. ? new Color(
  400. Math.min( Math.abs( colors[ 0 ] ), 1 ),
  401. Math.min( Math.abs( colors[ 1 ] ), 1 ),
  402. Math.min( Math.abs( colors[ 2 ] ), 1 )
  403. )
  404. : new Color( colors );
  405. } catch ( err ) {
  406. ballColor = new Color( ballx, bally, ballz );
  407. }
  408. }
  409. // Let's solve the equation to find the radius:
  410. // 1.0 / (0.000001 + radius^2) * strength - subtract = 0
  411. // strength / (radius^2) = subtract
  412. // strength = subtract * radius^2
  413. // radius^2 = strength / subtract
  414. // radius = sqrt(strength / subtract)
  415. const radius = this.size * Math.sqrt( strength / subtract ),
  416. zs = ballz * this.size,
  417. ys = bally * this.size,
  418. xs = ballx * this.size;
  419. let min_z = Math.floor( zs - radius );
  420. if ( min_z < 1 ) min_z = 1;
  421. let max_z = Math.floor( zs + radius );
  422. if ( max_z > this.size - 1 ) max_z = this.size - 1;
  423. let min_y = Math.floor( ys - radius );
  424. if ( min_y < 1 ) min_y = 1;
  425. let max_y = Math.floor( ys + radius );
  426. if ( max_y > this.size - 1 ) max_y = this.size - 1;
  427. let min_x = Math.floor( xs - radius );
  428. if ( min_x < 1 ) min_x = 1;
  429. let max_x = Math.floor( xs + radius );
  430. if ( max_x > this.size - 1 ) max_x = this.size - 1;
  431. // Don't polygonize in the outer layer because normals aren't
  432. // well-defined there.
  433. let x, y, z, y_offset, z_offset, fx, fy, fz, fz2, fy2, val;
  434. for ( z = min_z; z < max_z; z ++ ) {
  435. z_offset = this.size2 * z;
  436. fz = z / this.size - ballz;
  437. fz2 = fz * fz;
  438. for ( y = min_y; y < max_y; y ++ ) {
  439. y_offset = z_offset + this.size * y;
  440. fy = y / this.size - bally;
  441. fy2 = fy * fy;
  442. for ( x = min_x; x < max_x; x ++ ) {
  443. fx = x / this.size - ballx;
  444. val = strength / ( 0.000001 + fx * fx + fy2 + fz2 ) - subtract;
  445. if ( val > 0.0 ) {
  446. this.field[ y_offset + x ] += val * sign;
  447. // optimization
  448. // http://www.geisswerks.com/ryan/BLOBS/blobs.html
  449. const ratio =
  450. Math.sqrt( ( x - xs ) * ( x - xs ) + ( y - ys ) * ( y - ys ) + ( z - zs ) * ( z - zs ) ) / radius;
  451. const contrib =
  452. 1 - ratio * ratio * ratio * ( ratio * ( ratio * 6 - 15 ) + 10 );
  453. this.palette[ ( y_offset + x ) * 3 + 0 ] += ballColor.r * contrib;
  454. this.palette[ ( y_offset + x ) * 3 + 1 ] += ballColor.g * contrib;
  455. this.palette[ ( y_offset + x ) * 3 + 2 ] += ballColor.b * contrib;
  456. }
  457. }
  458. }
  459. }
  460. };
  461. /**
  462. * Adds a plane along the x-axis.
  463. *
  464. * @param {number} strength - The strength factor.
  465. * @param {number} subtract - The subtract factor.
  466. */
  467. this.addPlaneX = function ( strength, subtract ) {
  468. // cache attribute lookups
  469. const size = this.size,
  470. yd = this.yd,
  471. zd = this.zd,
  472. field = this.field;
  473. let x,
  474. y,
  475. z,
  476. xx,
  477. val,
  478. xdiv,
  479. cxy,
  480. dist = size * Math.sqrt( strength / subtract );
  481. if ( dist > size ) dist = size;
  482. for ( x = 0; x < dist; x ++ ) {
  483. xdiv = x / size;
  484. xx = xdiv * xdiv;
  485. val = strength / ( 0.0001 + xx ) - subtract;
  486. if ( val > 0.0 ) {
  487. for ( y = 0; y < size; y ++ ) {
  488. cxy = x + y * yd;
  489. for ( z = 0; z < size; z ++ ) {
  490. field[ zd * z + cxy ] += val;
  491. }
  492. }
  493. }
  494. }
  495. };
  496. /**
  497. * Adds a plane along the y-axis.
  498. *
  499. * @param {number} strength - The strength factor.
  500. * @param {number} subtract - The subtract factor.
  501. */
  502. this.addPlaneY = function ( strength, subtract ) {
  503. // cache attribute lookups
  504. const size = this.size,
  505. yd = this.yd,
  506. zd = this.zd,
  507. field = this.field;
  508. let x,
  509. y,
  510. z,
  511. yy,
  512. val,
  513. ydiv,
  514. cy,
  515. cxy,
  516. dist = size * Math.sqrt( strength / subtract );
  517. if ( dist > size ) dist = size;
  518. for ( y = 0; y < dist; y ++ ) {
  519. ydiv = y / size;
  520. yy = ydiv * ydiv;
  521. val = strength / ( 0.0001 + yy ) - subtract;
  522. if ( val > 0.0 ) {
  523. cy = y * yd;
  524. for ( x = 0; x < size; x ++ ) {
  525. cxy = cy + x;
  526. for ( z = 0; z < size; z ++ ) field[ zd * z + cxy ] += val;
  527. }
  528. }
  529. }
  530. };
  531. /**
  532. * Adds a plane along the z-axis.
  533. *
  534. * @param {number} strength - The strength factor.
  535. * @param {number} subtract - The subtract factor.
  536. */
  537. this.addPlaneZ = function ( strength, subtract ) {
  538. // cache attribute lookups
  539. const size = this.size,
  540. yd = this.yd,
  541. zd = this.zd,
  542. field = this.field;
  543. let x,
  544. y,
  545. z,
  546. zz,
  547. val,
  548. zdiv,
  549. cz,
  550. cyz,
  551. dist = size * Math.sqrt( strength / subtract );
  552. if ( dist > size ) dist = size;
  553. for ( z = 0; z < dist; z ++ ) {
  554. zdiv = z / size;
  555. zz = zdiv * zdiv;
  556. val = strength / ( 0.0001 + zz ) - subtract;
  557. if ( val > 0.0 ) {
  558. cz = zd * z;
  559. for ( y = 0; y < size; y ++ ) {
  560. cyz = cz + y * yd;
  561. for ( x = 0; x < size; x ++ ) field[ cyz + x ] += val;
  562. }
  563. }
  564. }
  565. };
  566. /////////////////////////////////////
  567. // Updates
  568. /////////////////////////////////////
  569. /**
  570. * Sets the cell value for the given coordinates.
  571. *
  572. * @param {number} x - The x value.
  573. * @param {number} y - The y value.
  574. * @param {number} z - The z value.
  575. * @param {number} value - The value to set.
  576. */
  577. this.setCell = function ( x, y, z, value ) {
  578. const index = this.size2 * z + this.size * y + x;
  579. this.field[ index ] = value;
  580. };
  581. /**
  582. * Returns the cell value for the given coordinates.
  583. *
  584. * @param {number} x - The x value.
  585. * @param {number} y - The y value.
  586. * @param {number} z - The z value.
  587. * @return {number} The value.
  588. */
  589. this.getCell = function ( x, y, z ) {
  590. const index = this.size2 * z + this.size * y + x;
  591. return this.field[ index ];
  592. };
  593. /**
  594. * Applies a blur with the given intensity.
  595. *
  596. * @param {number} [intensity=1] - The intensity of the blur.
  597. */
  598. this.blur = function ( intensity = 1 ) {
  599. const field = this.field;
  600. const fieldCopy = field.slice();
  601. const size = this.size;
  602. const size2 = this.size2;
  603. for ( let x = 0; x < size; x ++ ) {
  604. for ( let y = 0; y < size; y ++ ) {
  605. for ( let z = 0; z < size; z ++ ) {
  606. const index = size2 * z + size * y + x;
  607. let val = fieldCopy[ index ];
  608. let count = 1;
  609. for ( let x2 = - 1; x2 <= 1; x2 += 2 ) {
  610. const x3 = x2 + x;
  611. if ( x3 < 0 || x3 >= size ) continue;
  612. for ( let y2 = - 1; y2 <= 1; y2 += 2 ) {
  613. const y3 = y2 + y;
  614. if ( y3 < 0 || y3 >= size ) continue;
  615. for ( let z2 = - 1; z2 <= 1; z2 += 2 ) {
  616. const z3 = z2 + z;
  617. if ( z3 < 0 || z3 >= size ) continue;
  618. const index2 = size2 * z3 + size * y3 + x3;
  619. const val2 = fieldCopy[ index2 ];
  620. count ++;
  621. val += intensity * ( val2 - val ) / count;
  622. }
  623. }
  624. }
  625. field[ index ] = val;
  626. }
  627. }
  628. }
  629. };
  630. /**
  631. * Resets the effect.
  632. */
  633. this.reset = function () {
  634. // wipe the normal cache
  635. for ( let i = 0; i < this.size3; i ++ ) {
  636. this.normal_cache[ i * 3 ] = 0.0;
  637. this.field[ i ] = 0.0;
  638. this.palette[ i * 3 ] = this.palette[ i * 3 + 1 ] = this.palette[
  639. i * 3 + 2
  640. ] = 0.0;
  641. }
  642. };
  643. /**
  644. * Updates the effect.
  645. */
  646. this.update = function () {
  647. this.count = 0;
  648. // Triangulate. Yeah, this is slow.
  649. const smin2 = this.size - 2;
  650. for ( let z = 1; z < smin2; z ++ ) {
  651. const z_offset = this.size2 * z;
  652. const fz = ( z - this.halfsize ) / this.halfsize; //+ 1
  653. for ( let y = 1; y < smin2; y ++ ) {
  654. const y_offset = z_offset + this.size * y;
  655. const fy = ( y - this.halfsize ) / this.halfsize; //+ 1
  656. for ( let x = 1; x < smin2; x ++ ) {
  657. const fx = ( x - this.halfsize ) / this.halfsize; //+ 1
  658. const q = y_offset + x;
  659. polygonize( fx, fy, fz, q, this.isolation );
  660. }
  661. }
  662. }
  663. // set the draw range to only the processed triangles
  664. this.geometry.setDrawRange( 0, this.count );
  665. // update geometry data
  666. geometry.getAttribute( 'position' ).needsUpdate = true;
  667. geometry.getAttribute( 'normal' ).needsUpdate = true;
  668. if ( this.enableUvs ) geometry.getAttribute( 'uv' ).needsUpdate = true;
  669. if ( this.enableColors ) geometry.getAttribute( 'color' ).needsUpdate = true;
  670. // safety check
  671. if ( this.count / 3 > maxPolyCount ) console.warn( 'THREE.MarchingCubes: Geometry buffers too small for rendering. Please create an instance with a higher poly count.' );
  672. };
  673. this.init( resolution );
  674. }
  675. }
  676. /////////////////////////////////////
  677. // Marching cubes lookup tables
  678. /////////////////////////////////////
  679. // These tables are straight from Paul Bourke's page:
  680. // http://paulbourke.net/geometry/polygonise/
  681. // who in turn got them from Cory Gene Bloyd.
  682. const edgeTable = new Int32Array( [
  683. 0x0, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c,
  684. 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,
  685. 0x190, 0x99, 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c,
  686. 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,
  687. 0x230, 0x339, 0x33, 0x13a, 0x636, 0x73f, 0x435, 0x53c,
  688. 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,
  689. 0x3a0, 0x2a9, 0x1a3, 0xaa, 0x7a6, 0x6af, 0x5a5, 0x4ac,
  690. 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,
  691. 0x460, 0x569, 0x663, 0x76a, 0x66, 0x16f, 0x265, 0x36c,
  692. 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
  693. 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff, 0x3f5, 0x2fc,
  694. 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,
  695. 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55, 0x15c,
  696. 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,
  697. 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc,
  698. 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,
  699. 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc,
  700. 0xcc, 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,
  701. 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c,
  702. 0x15c, 0x55, 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
  703. 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc,
  704. 0x2fc, 0x3f5, 0xff, 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,
  705. 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c,
  706. 0x36c, 0x265, 0x16f, 0x66, 0x76a, 0x663, 0x569, 0x460,
  707. 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac,
  708. 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa, 0x1a3, 0x2a9, 0x3a0,
  709. 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c,
  710. 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33, 0x339, 0x230,
  711. 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c,
  712. 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99, 0x190,
  713. 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c,
  714. 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0 ] );
  715. const triTable = new Int32Array( [
  716. - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  717. 0, 8, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  718. 0, 1, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  719. 1, 8, 3, 9, 8, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  720. 1, 2, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  721. 0, 8, 3, 1, 2, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  722. 9, 2, 10, 0, 2, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  723. 2, 8, 3, 2, 10, 8, 10, 9, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  724. 3, 11, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  725. 0, 11, 2, 8, 11, 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  726. 1, 9, 0, 2, 3, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  727. 1, 11, 2, 1, 9, 11, 9, 8, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  728. 3, 10, 1, 11, 10, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  729. 0, 10, 1, 0, 8, 10, 8, 11, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  730. 3, 9, 0, 3, 11, 9, 11, 10, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  731. 9, 8, 10, 10, 8, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  732. 4, 7, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  733. 4, 3, 0, 7, 3, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  734. 0, 1, 9, 8, 4, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  735. 4, 1, 9, 4, 7, 1, 7, 3, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  736. 1, 2, 10, 8, 4, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  737. 3, 4, 7, 3, 0, 4, 1, 2, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  738. 9, 2, 10, 9, 0, 2, 8, 4, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  739. 2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, - 1, - 1, - 1, - 1,
  740. 8, 4, 7, 3, 11, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  741. 11, 4, 7, 11, 2, 4, 2, 0, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  742. 9, 0, 1, 8, 4, 7, 2, 3, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  743. 4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, - 1, - 1, - 1, - 1,
  744. 3, 10, 1, 3, 11, 10, 7, 8, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  745. 1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, - 1, - 1, - 1, - 1,
  746. 4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, - 1, - 1, - 1, - 1,
  747. 4, 7, 11, 4, 11, 9, 9, 11, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  748. 9, 5, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  749. 9, 5, 4, 0, 8, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  750. 0, 5, 4, 1, 5, 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  751. 8, 5, 4, 8, 3, 5, 3, 1, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  752. 1, 2, 10, 9, 5, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  753. 3, 0, 8, 1, 2, 10, 4, 9, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  754. 5, 2, 10, 5, 4, 2, 4, 0, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  755. 2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, - 1, - 1, - 1, - 1,
  756. 9, 5, 4, 2, 3, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  757. 0, 11, 2, 0, 8, 11, 4, 9, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  758. 0, 5, 4, 0, 1, 5, 2, 3, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  759. 2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, - 1, - 1, - 1, - 1,
  760. 10, 3, 11, 10, 1, 3, 9, 5, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  761. 4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, - 1, - 1, - 1, - 1,
  762. 5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, - 1, - 1, - 1, - 1,
  763. 5, 4, 8, 5, 8, 10, 10, 8, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  764. 9, 7, 8, 5, 7, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  765. 9, 3, 0, 9, 5, 3, 5, 7, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  766. 0, 7, 8, 0, 1, 7, 1, 5, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  767. 1, 5, 3, 3, 5, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  768. 9, 7, 8, 9, 5, 7, 10, 1, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  769. 10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, - 1, - 1, - 1, - 1,
  770. 8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, - 1, - 1, - 1, - 1,
  771. 2, 10, 5, 2, 5, 3, 3, 5, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  772. 7, 9, 5, 7, 8, 9, 3, 11, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  773. 9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, - 1, - 1, - 1, - 1,
  774. 2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, - 1, - 1, - 1, - 1,
  775. 11, 2, 1, 11, 1, 7, 7, 1, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  776. 9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, - 1, - 1, - 1, - 1,
  777. 5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, - 1,
  778. 11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, - 1,
  779. 11, 10, 5, 7, 11, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  780. 10, 6, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  781. 0, 8, 3, 5, 10, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  782. 9, 0, 1, 5, 10, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  783. 1, 8, 3, 1, 9, 8, 5, 10, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  784. 1, 6, 5, 2, 6, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  785. 1, 6, 5, 1, 2, 6, 3, 0, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  786. 9, 6, 5, 9, 0, 6, 0, 2, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  787. 5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, - 1, - 1, - 1, - 1,
  788. 2, 3, 11, 10, 6, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  789. 11, 0, 8, 11, 2, 0, 10, 6, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  790. 0, 1, 9, 2, 3, 11, 5, 10, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  791. 5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, - 1, - 1, - 1, - 1,
  792. 6, 3, 11, 6, 5, 3, 5, 1, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  793. 0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, - 1, - 1, - 1, - 1,
  794. 3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, - 1, - 1, - 1, - 1,
  795. 6, 5, 9, 6, 9, 11, 11, 9, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  796. 5, 10, 6, 4, 7, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  797. 4, 3, 0, 4, 7, 3, 6, 5, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  798. 1, 9, 0, 5, 10, 6, 8, 4, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  799. 10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, - 1, - 1, - 1, - 1,
  800. 6, 1, 2, 6, 5, 1, 4, 7, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  801. 1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, - 1, - 1, - 1, - 1,
  802. 8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, - 1, - 1, - 1, - 1,
  803. 7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, - 1,
  804. 3, 11, 2, 7, 8, 4, 10, 6, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  805. 5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, - 1, - 1, - 1, - 1,
  806. 0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, - 1, - 1, - 1, - 1,
  807. 9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, - 1,
  808. 8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, - 1, - 1, - 1, - 1,
  809. 5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, - 1,
  810. 0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, - 1,
  811. 6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, - 1, - 1, - 1, - 1,
  812. 10, 4, 9, 6, 4, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  813. 4, 10, 6, 4, 9, 10, 0, 8, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  814. 10, 0, 1, 10, 6, 0, 6, 4, 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  815. 8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, - 1, - 1, - 1, - 1,
  816. 1, 4, 9, 1, 2, 4, 2, 6, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  817. 3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, - 1, - 1, - 1, - 1,
  818. 0, 2, 4, 4, 2, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  819. 8, 3, 2, 8, 2, 4, 4, 2, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  820. 10, 4, 9, 10, 6, 4, 11, 2, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  821. 0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, - 1, - 1, - 1, - 1,
  822. 3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, - 1, - 1, - 1, - 1,
  823. 6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, - 1,
  824. 9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, - 1, - 1, - 1, - 1,
  825. 8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, - 1,
  826. 3, 11, 6, 3, 6, 0, 0, 6, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  827. 6, 4, 8, 11, 6, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  828. 7, 10, 6, 7, 8, 10, 8, 9, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  829. 0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, - 1, - 1, - 1, - 1,
  830. 10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, - 1, - 1, - 1, - 1,
  831. 10, 6, 7, 10, 7, 1, 1, 7, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  832. 1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, - 1, - 1, - 1, - 1,
  833. 2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, - 1,
  834. 7, 8, 0, 7, 0, 6, 6, 0, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  835. 7, 3, 2, 6, 7, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  836. 2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, - 1, - 1, - 1, - 1,
  837. 2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, - 1,
  838. 1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, - 1,
  839. 11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, - 1, - 1, - 1, - 1,
  840. 8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, - 1,
  841. 0, 9, 1, 11, 6, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  842. 7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, - 1, - 1, - 1, - 1,
  843. 7, 11, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  844. 7, 6, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  845. 3, 0, 8, 11, 7, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  846. 0, 1, 9, 11, 7, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  847. 8, 1, 9, 8, 3, 1, 11, 7, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  848. 10, 1, 2, 6, 11, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  849. 1, 2, 10, 3, 0, 8, 6, 11, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  850. 2, 9, 0, 2, 10, 9, 6, 11, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  851. 6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, - 1, - 1, - 1, - 1,
  852. 7, 2, 3, 6, 2, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  853. 7, 0, 8, 7, 6, 0, 6, 2, 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  854. 2, 7, 6, 2, 3, 7, 0, 1, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  855. 1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, - 1, - 1, - 1, - 1,
  856. 10, 7, 6, 10, 1, 7, 1, 3, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  857. 10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, - 1, - 1, - 1, - 1,
  858. 0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, - 1, - 1, - 1, - 1,
  859. 7, 6, 10, 7, 10, 8, 8, 10, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  860. 6, 8, 4, 11, 8, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  861. 3, 6, 11, 3, 0, 6, 0, 4, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  862. 8, 6, 11, 8, 4, 6, 9, 0, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  863. 9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, - 1, - 1, - 1, - 1,
  864. 6, 8, 4, 6, 11, 8, 2, 10, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  865. 1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, - 1, - 1, - 1, - 1,
  866. 4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, - 1, - 1, - 1, - 1,
  867. 10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, - 1,
  868. 8, 2, 3, 8, 4, 2, 4, 6, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  869. 0, 4, 2, 4, 6, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  870. 1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, - 1, - 1, - 1, - 1,
  871. 1, 9, 4, 1, 4, 2, 2, 4, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  872. 8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, - 1, - 1, - 1, - 1,
  873. 10, 1, 0, 10, 0, 6, 6, 0, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  874. 4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, - 1,
  875. 10, 9, 4, 6, 10, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  876. 4, 9, 5, 7, 6, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  877. 0, 8, 3, 4, 9, 5, 11, 7, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  878. 5, 0, 1, 5, 4, 0, 7, 6, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  879. 11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, - 1, - 1, - 1, - 1,
  880. 9, 5, 4, 10, 1, 2, 7, 6, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  881. 6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, - 1, - 1, - 1, - 1,
  882. 7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, - 1, - 1, - 1, - 1,
  883. 3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, - 1,
  884. 7, 2, 3, 7, 6, 2, 5, 4, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  885. 9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, - 1, - 1, - 1, - 1,
  886. 3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, - 1, - 1, - 1, - 1,
  887. 6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, - 1,
  888. 9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, - 1, - 1, - 1, - 1,
  889. 1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, - 1,
  890. 4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, - 1,
  891. 7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, - 1, - 1, - 1, - 1,
  892. 6, 9, 5, 6, 11, 9, 11, 8, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  893. 3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, - 1, - 1, - 1, - 1,
  894. 0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, - 1, - 1, - 1, - 1,
  895. 6, 11, 3, 6, 3, 5, 5, 3, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  896. 1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, - 1, - 1, - 1, - 1,
  897. 0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, - 1,
  898. 11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, - 1,
  899. 6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, - 1, - 1, - 1, - 1,
  900. 5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, - 1, - 1, - 1, - 1,
  901. 9, 5, 6, 9, 6, 0, 0, 6, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  902. 1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, - 1,
  903. 1, 5, 6, 2, 1, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  904. 1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, - 1,
  905. 10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, - 1, - 1, - 1, - 1,
  906. 0, 3, 8, 5, 6, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  907. 10, 5, 6, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  908. 11, 5, 10, 7, 5, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  909. 11, 5, 10, 11, 7, 5, 8, 3, 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  910. 5, 11, 7, 5, 10, 11, 1, 9, 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  911. 10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, - 1, - 1, - 1, - 1,
  912. 11, 1, 2, 11, 7, 1, 7, 5, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  913. 0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, - 1, - 1, - 1, - 1,
  914. 9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, - 1, - 1, - 1, - 1,
  915. 7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, - 1,
  916. 2, 5, 10, 2, 3, 5, 3, 7, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  917. 8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, - 1, - 1, - 1, - 1,
  918. 9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, - 1, - 1, - 1, - 1,
  919. 9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, - 1,
  920. 1, 3, 5, 3, 7, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  921. 0, 8, 7, 0, 7, 1, 1, 7, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  922. 9, 0, 3, 9, 3, 5, 5, 3, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  923. 9, 8, 7, 5, 9, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  924. 5, 8, 4, 5, 10, 8, 10, 11, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  925. 5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, - 1, - 1, - 1, - 1,
  926. 0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, - 1, - 1, - 1, - 1,
  927. 10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, - 1,
  928. 2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, - 1, - 1, - 1, - 1,
  929. 0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, - 1,
  930. 0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, - 1,
  931. 9, 4, 5, 2, 11, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  932. 2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, - 1, - 1, - 1, - 1,
  933. 5, 10, 2, 5, 2, 4, 4, 2, 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  934. 3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, - 1,
  935. 5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, - 1, - 1, - 1, - 1,
  936. 8, 4, 5, 8, 5, 3, 3, 5, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  937. 0, 4, 5, 1, 0, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  938. 8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, - 1, - 1, - 1, - 1,
  939. 9, 4, 5, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  940. 4, 11, 7, 4, 9, 11, 9, 10, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  941. 0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, - 1, - 1, - 1, - 1,
  942. 1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, - 1, - 1, - 1, - 1,
  943. 3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, - 1,
  944. 4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, - 1, - 1, - 1, - 1,
  945. 9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, - 1,
  946. 11, 7, 4, 11, 4, 2, 2, 4, 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  947. 11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, - 1, - 1, - 1, - 1,
  948. 2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, - 1, - 1, - 1, - 1,
  949. 9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, - 1,
  950. 3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, - 1,
  951. 1, 10, 2, 8, 7, 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  952. 4, 9, 1, 4, 1, 7, 7, 1, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  953. 4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, - 1, - 1, - 1, - 1,
  954. 4, 0, 3, 7, 4, 3, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  955. 4, 8, 7, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  956. 9, 10, 8, 10, 11, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  957. 3, 0, 9, 3, 9, 11, 11, 9, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  958. 0, 1, 10, 0, 10, 8, 8, 10, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  959. 3, 1, 10, 11, 3, 10, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  960. 1, 2, 11, 1, 11, 9, 9, 11, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  961. 3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, - 1, - 1, - 1, - 1,
  962. 0, 2, 11, 8, 0, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  963. 3, 2, 11, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  964. 2, 3, 8, 2, 8, 10, 10, 8, 9, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  965. 9, 10, 2, 0, 9, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  966. 2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, - 1, - 1, - 1, - 1,
  967. 1, 10, 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  968. 1, 3, 8, 9, 1, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  969. 0, 9, 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  970. 0, 3, 8, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1,
  971. - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1 ] );
  972. export { MarchingCubes, edgeTable, triTable };