MarchingCubes.js 38 KB

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