webgl_marchingcubes.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - marching cubes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #fff;
  10. font-family: Monospace;
  11. font-size: 13px;
  12. text-align: center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. color: #ffffff;
  19. position: absolute;
  20. top: 0px;
  21. width: 100%;
  22. padding: 5px;
  23. }
  24. a {
  25. color: gold;
  26. }
  27. #webglmessage {
  28. font-family: monospace;
  29. font-size: 13px;
  30. text-align: center;
  31. background: rgb(0, 0, 50);
  32. color: #fff;
  33. padding: 1em;
  34. width: 475px;
  35. margin: 5em auto 0;
  36. display: none;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="container"></div>
  42. <div id="info">
  43. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  44. marching cubes -
  45. [based on greggman's <a href="https://webglsamples.org/blob/blob.html">blob</a>, original code by Henrik Rydgård]
  46. </div>
  47. <script src="../build/three.js"></script>
  48. <script src="js/controls/OrbitControls.js"></script>
  49. <script src="js/shaders/CopyShader.js"></script>
  50. <script src="js/shaders/FXAAShader.js"></script>
  51. <script src="js/shaders/HorizontalTiltShiftShader.js"></script>
  52. <script src="js/shaders/VerticalTiltShiftShader.js"></script>
  53. <script src="js/postprocessing/EffectComposer.js"></script>
  54. <script src="js/postprocessing/RenderPass.js"></script>
  55. <script src="js/postprocessing/BloomPass.js"></script>
  56. <script src="js/postprocessing/ShaderPass.js"></script>
  57. <script src="js/postprocessing/MaskPass.js"></script>
  58. <script src="js/postprocessing/SavePass.js"></script>
  59. <script src="js/MarchingCubes.js"></script>
  60. <script src="js/ShaderToon.js"></script>
  61. <script src="js/WebGL.js"></script>
  62. <script src="js/libs/stats.min.js"></script>
  63. <script src="js/libs/dat.gui.min.js"></script>
  64. <script>
  65. if ( WEBGL.isWebGLAvailable() === false ) {
  66. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  67. }
  68. var MARGIN = 0;
  69. var SCREEN_WIDTH = window.innerWidth;
  70. var SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  71. var container, stats;
  72. var camera, scene, renderer;
  73. var mesh, texture, geometry, materials, material, current_material;
  74. var light, pointLight, ambientLight;
  75. var effect, resolution, numBlobs;
  76. var composer, effectFXAA, hblur, vblur;
  77. var effectController;
  78. var controls;
  79. var time = 0;
  80. var clock = new THREE.Clock();
  81. init();
  82. animate();
  83. function init() {
  84. container = document.getElementById( 'container' );
  85. // CAMERA
  86. camera = new THREE.PerspectiveCamera( 45, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  87. camera.position.set( -500, 500, 1500 );
  88. // SCENE
  89. scene = new THREE.Scene();
  90. scene.background = new THREE.Color( 0x050505 );
  91. // LIGHTS
  92. light = new THREE.DirectionalLight( 0xffffff );
  93. light.position.set( 0.5, 0.5, 1 );
  94. scene.add( light );
  95. pointLight = new THREE.PointLight( 0xff3300 );
  96. pointLight.position.set( 0, 0, 100 );
  97. scene.add( pointLight );
  98. ambientLight = new THREE.AmbientLight( 0x080808 );
  99. scene.add( ambientLight );
  100. // MATERIALS
  101. materials = generateMaterials();
  102. current_material = "shiny";
  103. // MARCHING CUBES
  104. resolution = 28;
  105. numBlobs = 10;
  106. effect = new THREE.MarchingCubes( resolution, materials[ current_material ].m, true, true );
  107. effect.position.set( 0, 0, 0 );
  108. effect.scale.set( 700, 700, 700 );
  109. effect.enableUvs = false;
  110. effect.enableColors = false;
  111. scene.add( effect );
  112. // RENDERER
  113. renderer = new THREE.WebGLRenderer();
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  116. renderer.domElement.style.position = "absolute";
  117. renderer.domElement.style.top = MARGIN + "px";
  118. renderer.domElement.style.left = "0px";
  119. container.appendChild( renderer.domElement );
  120. //
  121. renderer.gammaInput = true;
  122. renderer.gammaOutput = true;
  123. // CONTROLS
  124. controls = new THREE.OrbitControls( camera, renderer.domElement );
  125. // STATS
  126. stats = new Stats();
  127. container.appendChild( stats.dom );
  128. // COMPOSER
  129. renderer.autoClear = false;
  130. var renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  131. var renderTarget = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, renderTargetParameters );
  132. effectFXAA = new THREE.ShaderPass( THREE.FXAAShader );
  133. hblur = new THREE.ShaderPass( THREE.HorizontalTiltShiftShader );
  134. vblur = new THREE.ShaderPass( THREE.VerticalTiltShiftShader );
  135. var bluriness = 8;
  136. hblur.uniforms[ 'h' ].value = bluriness / SCREEN_WIDTH;
  137. vblur.uniforms[ 'v' ].value = bluriness / SCREEN_HEIGHT;
  138. hblur.uniforms[ 'r' ].value = vblur.uniforms[ 'r' ].value = 0.5;
  139. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / SCREEN_WIDTH, 1 / SCREEN_HEIGHT );
  140. var renderModel = new THREE.RenderPass( scene, camera );
  141. vblur.renderToScreen = true;
  142. //effectFXAA.renderToScreen = true;
  143. composer = new THREE.EffectComposer( renderer, renderTarget );
  144. composer.addPass( renderModel );
  145. composer.addPass( effectFXAA );
  146. composer.addPass( hblur );
  147. composer.addPass( vblur );
  148. // GUI
  149. setupGui();
  150. // EVENTS
  151. window.addEventListener( 'resize', onWindowResize, false );
  152. }
  153. //
  154. function onWindowResize( event ) {
  155. SCREEN_WIDTH = window.innerWidth;
  156. SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  157. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  158. camera.updateProjectionMatrix();
  159. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  160. composer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  161. hblur.uniforms[ 'h' ].value = 4 / SCREEN_WIDTH;
  162. vblur.uniforms[ 'v' ].value = 4 / SCREEN_HEIGHT;
  163. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / SCREEN_WIDTH, 1 / SCREEN_HEIGHT );
  164. }
  165. function generateMaterials() {
  166. // environment map
  167. var path = "textures/cube/SwedishRoyalCastle/";
  168. var format = '.jpg';
  169. var urls = [
  170. path + 'px' + format, path + 'nx' + format,
  171. path + 'py' + format, path + 'ny' + format,
  172. path + 'pz' + format, path + 'nz' + format
  173. ];
  174. var cubeTextureLoader = new THREE.CubeTextureLoader();
  175. var reflectionCube = cubeTextureLoader.load( urls );
  176. reflectionCube.format = THREE.RGBFormat;
  177. var refractionCube = cubeTextureLoader.load( urls );
  178. reflectionCube.format = THREE.RGBFormat;
  179. refractionCube.mapping = THREE.CubeRefractionMapping;
  180. // toons
  181. var toonMaterial1 = createShaderMaterial( "toon1", light, ambientLight ),
  182. toonMaterial2 = createShaderMaterial( "toon2", light, ambientLight ),
  183. hatchingMaterial = createShaderMaterial( "hatching", light, ambientLight ),
  184. hatchingMaterial2 = createShaderMaterial( "hatching", light, ambientLight ),
  185. dottedMaterial = createShaderMaterial( "dotted", light, ambientLight ),
  186. dottedMaterial2 = createShaderMaterial( "dotted", light, ambientLight );
  187. hatchingMaterial2.uniforms.uBaseColor.value.setRGB( 0, 0, 0 );
  188. hatchingMaterial2.uniforms.uLineColor1.value.setHSL( 0, 0.8, 0.5 );
  189. hatchingMaterial2.uniforms.uLineColor2.value.setHSL( 0, 0.8, 0.5 );
  190. hatchingMaterial2.uniforms.uLineColor3.value.setHSL( 0, 0.8, 0.5 );
  191. hatchingMaterial2.uniforms.uLineColor4.value.setHSL( 0.1, 0.8, 0.5 );
  192. dottedMaterial2.uniforms.uBaseColor.value.setRGB( 0, 0, 0 );
  193. dottedMaterial2.uniforms.uLineColor1.value.setHSL( 0.05, 1.0, 0.5 );
  194. var texture = new THREE.TextureLoader().load( "textures/UV_Grid_Sm.jpg" );
  195. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  196. var materials = {
  197. "chrome" :
  198. {
  199. m: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } ),
  200. h: 0, s: 0, l: 1
  201. },
  202. "liquid" :
  203. {
  204. m: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: refractionCube, refractionRatio: 0.85 } ),
  205. h: 0, s: 0, l: 1
  206. },
  207. "shiny" :
  208. {
  209. m: new THREE.MeshStandardMaterial( { color: 0x550000, envMap: reflectionCube, roughness: 0.1, metalness: 1.0 } ),
  210. h: 0, s: 0.8, l: 0.2
  211. },
  212. "matte" :
  213. {
  214. m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x111111, shininess: 1 } ),
  215. h: 0, s: 0, l: 1
  216. },
  217. "flat" :
  218. {
  219. m: new THREE.MeshLambertMaterial( { color: 0x000000, flatShading: true } ),
  220. h: 0, s: 0, l: 1
  221. },
  222. "textured" :
  223. {
  224. m: new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 1, map: texture } ),
  225. h: 0, s: 0, l: 1
  226. },
  227. "colors" :
  228. {
  229. m: new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 2, vertexColors: THREE.VertexColors } ),
  230. h: 0, s: 0, l: 1
  231. },
  232. "plastic" :
  233. {
  234. m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x888888, shininess: 250 } ),
  235. h: 0.6, s: 0.8, l: 0.1
  236. },
  237. "toon1" :
  238. {
  239. m: toonMaterial1,
  240. h: 0.2, s: 1, l: 0.75
  241. },
  242. "toon2" :
  243. {
  244. m: toonMaterial2,
  245. h: 0.4, s: 1, l: 0.75
  246. },
  247. "hatching" :
  248. {
  249. m: hatchingMaterial,
  250. h: 0.2, s: 1, l: 0.9
  251. },
  252. "hatching2" :
  253. {
  254. m: hatchingMaterial2,
  255. h: 0.0, s: 0.8, l: 0.5
  256. },
  257. "dotted" :
  258. {
  259. m: dottedMaterial,
  260. h: 0.2, s: 1, l: 0.9
  261. },
  262. "dotted2" :
  263. {
  264. m: dottedMaterial2,
  265. h: 0.1, s: 1, l: 0.5
  266. }
  267. };
  268. return materials;
  269. }
  270. function createShaderMaterial( id, light, ambientLight ) {
  271. var shader = THREE.ShaderToon[ id ];
  272. var u = THREE.UniformsUtils.clone( shader.uniforms );
  273. var vs = shader.vertexShader;
  274. var fs = shader.fragmentShader;
  275. var material = new THREE.ShaderMaterial( { uniforms: u, vertexShader: vs, fragmentShader: fs } );
  276. material.uniforms.uDirLightPos.value = light.position;
  277. material.uniforms.uDirLightColor.value = light.color;
  278. material.uniforms.uAmbientLightColor.value = ambientLight.color;
  279. return material;
  280. }
  281. //
  282. function setupGui() {
  283. var createHandler = function( id ) {
  284. return function() {
  285. var mat_old = materials[ current_material ];
  286. mat_old.h = m_h.getValue();
  287. mat_old.s = m_s.getValue();
  288. mat_old.l = m_l.getValue();
  289. current_material = id;
  290. var mat = materials[ id ];
  291. effect.material = mat.m;
  292. m_h.setValue( mat.h );
  293. m_s.setValue( mat.s );
  294. m_l.setValue( mat.l );
  295. effect.enableUvs = (current_material === "textured") ? true : false;
  296. effect.enableColors = (current_material === "colors") ? true : false;
  297. };
  298. };
  299. effectController = {
  300. material: "shiny",
  301. speed: 1.0,
  302. numBlobs: 10,
  303. resolution: 28,
  304. isolation: 80,
  305. floor: true,
  306. wallx: false,
  307. wallz: false,
  308. hue: 0.0,
  309. saturation: 0.8,
  310. lightness: 0.1,
  311. lhue: 0.04,
  312. lsaturation: 1.0,
  313. llightness: 0.5,
  314. lx: 0.5,
  315. ly: 0.5,
  316. lz: 1.0,
  317. postprocessing: false,
  318. dummy: function() {
  319. }
  320. };
  321. var h, m_h, m_s, m_l;
  322. var gui = new dat.GUI();
  323. // material (type)
  324. h = gui.addFolder( "Materials" );
  325. for ( var m in materials ) {
  326. effectController[ m ] = createHandler( m );
  327. h.add( effectController, m ).name( m );
  328. }
  329. // material (color)
  330. h = gui.addFolder( "Material color" );
  331. m_h = h.add( effectController, "hue", 0.0, 1.0, 0.025 );
  332. m_s = h.add( effectController, "saturation", 0.0, 1.0, 0.025 );
  333. m_l = h.add( effectController, "lightness", 0.0, 1.0, 0.025 );
  334. // light (point)
  335. h = gui.addFolder( "Point light color" );
  336. h.add( effectController, "lhue", 0.0, 1.0, 0.025 ).name("hue");
  337. h.add( effectController, "lsaturation", 0.0, 1.0, 0.025 ).name("saturation");
  338. h.add( effectController, "llightness", 0.0, 1.0, 0.025 ).name("lightness");
  339. // light (directional)
  340. h = gui.addFolder( "Directional light orientation" );
  341. h.add( effectController, "lx", -1.0, 1.0, 0.025 ).name("x");
  342. h.add( effectController, "ly", -1.0, 1.0, 0.025 ).name("y");
  343. h.add( effectController, "lz", -1.0, 1.0, 0.025 ).name("z");
  344. // simulation
  345. h = gui.addFolder( "Simulation" );
  346. h.add( effectController, "speed", 0.1, 8.0, 0.05 );
  347. h.add( effectController, "numBlobs", 1, 50, 1 );
  348. h.add( effectController, "resolution", 14, 100, 1 );
  349. h.add( effectController, "isolation", 10, 300, 1 );
  350. h.add( effectController, "floor" );
  351. h.add( effectController, "wallx" );
  352. h.add( effectController, "wallz" );
  353. // rendering
  354. h = gui.addFolder( "Rendering" );
  355. h.add( effectController, "postprocessing" );
  356. }
  357. // this controls content of marching cubes voxel field
  358. function updateCubes( object, time, numblobs, floor, wallx, wallz ) {
  359. object.reset();
  360. // fill the field with some metaballs
  361. var i, ballx, bally, ballz, subtract, strength;
  362. subtract = 12;
  363. strength = 1.2 / ( ( Math.sqrt( numblobs ) - 1 ) / 4 + 1 );
  364. for ( i = 0; i < numblobs; i ++ ) {
  365. ballx = Math.sin( i + 1.26 * time * ( 1.03 + 0.5 * Math.cos( 0.21 * i ) ) ) * 0.27 + 0.5;
  366. bally = Math.abs( Math.cos( i + 1.12 * time * Math.cos( 1.22 + 0.1424 * i ) ) ) * 0.77; // dip into the floor
  367. ballz = Math.cos( i + 1.32 * time * 0.1 * Math.sin( ( 0.92 + 0.53 * i ) ) ) * 0.27 + 0.5;
  368. object.addBall(ballx, bally, ballz, strength, subtract);
  369. }
  370. if ( floor ) object.addPlaneY( 2, 12 );
  371. if ( wallz ) object.addPlaneZ( 2, 12 );
  372. if ( wallx ) object.addPlaneX( 2, 12 );
  373. }
  374. //
  375. function animate() {
  376. requestAnimationFrame( animate );
  377. render();
  378. stats.update();
  379. }
  380. function render() {
  381. var delta = clock.getDelta();
  382. time += delta * effectController.speed * 0.5;
  383. // marching cubes
  384. if ( effectController.resolution !== resolution ) {
  385. resolution = effectController.resolution;
  386. effect.init( Math.floor( resolution ) );
  387. }
  388. if ( effectController.isolation !== effect.isolation ) {
  389. effect.isolation = effectController.isolation;
  390. }
  391. updateCubes( effect, time, effectController.numBlobs, effectController.floor, effectController.wallx, effectController.wallz );
  392. // materials
  393. if ( effect.material instanceof THREE.ShaderMaterial ) {
  394. if ( current_material === "dotted2" ) {
  395. effect.material.uniforms.uLineColor1.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  396. } else if ( current_material === "hatching2" ) {
  397. var u = effect.material.uniforms;
  398. u.uLineColor1.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  399. u.uLineColor2.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  400. u.uLineColor3.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  401. u.uLineColor4.value.setHSL( ( effectController.hue + 0.2 % 1.0 ), effectController.saturation, effectController.lightness );
  402. } else {
  403. effect.material.uniforms.uBaseColor.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  404. }
  405. } else {
  406. effect.material.color.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  407. }
  408. // lights
  409. light.position.set( effectController.lx, effectController.ly, effectController.lz );
  410. light.position.normalize();
  411. pointLight.color.setHSL( effectController.lhue, effectController.lsaturation, effectController.llightness );
  412. // render
  413. if ( effectController.postprocessing ) {
  414. composer.render( delta );
  415. } else {
  416. renderer.clear();
  417. renderer.render( scene, camera );
  418. }
  419. }
  420. </script>
  421. </body>
  422. </html>