MarchingCubes.js 39 KB

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