webgl_marchingcubes.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  13. marching cubes<br/>
  14. based on greggman's <a href="https://webglsamples.org/blob/blob.html">blob</a>, original code by Henrik Rydgård
  15. </div>
  16. <!-- Import maps polyfill -->
  17. <!-- Remove this when import maps will be widely supported -->
  18. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { MarchingCubes } from 'three/addons/objects/MarchingCubes.js';
  33. import { ToonShader1, ToonShader2, ToonShaderHatching, ToonShaderDotted } from 'three/addons/shaders/ToonShader.js';
  34. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  35. let container, stats;
  36. let camera, scene, renderer;
  37. let materials, current_material;
  38. let light, pointLight, ambientLight;
  39. let effect, resolution;
  40. let effectController;
  41. let time = 0;
  42. const clock = new THREE.Clock();
  43. init();
  44. animate();
  45. function init() {
  46. container = document.getElementById( 'container' );
  47. // CAMERA
  48. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  49. camera.position.set( - 500, 500, 1500 );
  50. // SCENE
  51. scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0x050505 );
  53. // LIGHTS
  54. light = new THREE.DirectionalLight( 0xffffff );
  55. light.position.set( 0.5, 0.5, 1 );
  56. scene.add( light );
  57. pointLight = new THREE.PointLight( 0xff3300 );
  58. pointLight.position.set( 0, 0, 100 );
  59. scene.add( pointLight );
  60. ambientLight = new THREE.AmbientLight( 0x080808 );
  61. scene.add( ambientLight );
  62. // MATERIALS
  63. materials = generateMaterials();
  64. current_material = 'shiny';
  65. // MARCHING CUBES
  66. resolution = 28;
  67. effect = new MarchingCubes( resolution, materials[ current_material ], true, true, 100000 );
  68. effect.position.set( 0, 0, 0 );
  69. effect.scale.set( 700, 700, 700 );
  70. effect.enableUvs = false;
  71. effect.enableColors = false;
  72. scene.add( effect );
  73. // RENDERER
  74. renderer = new THREE.WebGLRenderer();
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. container.appendChild( renderer.domElement );
  78. // CONTROLS
  79. const controls = new OrbitControls( camera, renderer.domElement );
  80. controls.minDistance = 500;
  81. controls.maxDistance = 5000;
  82. // STATS
  83. stats = new Stats();
  84. container.appendChild( stats.dom );
  85. // GUI
  86. setupGui();
  87. // EVENTS
  88. window.addEventListener( 'resize', onWindowResize );
  89. }
  90. //
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function generateMaterials() {
  97. // environment map
  98. const path = 'textures/cube/SwedishRoyalCastle/';
  99. const format = '.jpg';
  100. const urls = [
  101. path + 'px' + format, path + 'nx' + format,
  102. path + 'py' + format, path + 'ny' + format,
  103. path + 'pz' + format, path + 'nz' + format
  104. ];
  105. const cubeTextureLoader = new THREE.CubeTextureLoader();
  106. const reflectionCube = cubeTextureLoader.load( urls );
  107. const refractionCube = cubeTextureLoader.load( urls );
  108. refractionCube.mapping = THREE.CubeRefractionMapping;
  109. // toons
  110. const toonMaterial1 = createShaderMaterial( ToonShader1, light, ambientLight );
  111. const toonMaterial2 = createShaderMaterial( ToonShader2, light, ambientLight );
  112. const hatchingMaterial = createShaderMaterial( ToonShaderHatching, light, ambientLight );
  113. const dottedMaterial = createShaderMaterial( ToonShaderDotted, light, ambientLight );
  114. const texture = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  115. texture.wrapS = THREE.RepeatWrapping;
  116. texture.wrapT = THREE.RepeatWrapping;
  117. const materials = {
  118. 'shiny': new THREE.MeshStandardMaterial( { color: 0x550000, envMap: reflectionCube, roughness: 0.1, metalness: 1.0 } ),
  119. 'chrome': new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } ),
  120. 'liquid': new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: refractionCube, refractionRatio: 0.85 } ),
  121. 'matte': new THREE.MeshPhongMaterial( { specular: 0x111111, shininess: 1 } ),
  122. 'flat': new THREE.MeshLambertMaterial( { /*TODO flatShading: true */ } ),
  123. 'textured': new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 1, map: texture } ),
  124. 'colors': new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 2, vertexColors: true } ),
  125. 'multiColors': new THREE.MeshPhongMaterial( { shininess: 2, vertexColors: true } ),
  126. 'plastic': new THREE.MeshPhongMaterial( { specular: 0x888888, shininess: 250 } ),
  127. 'toon1': toonMaterial1,
  128. 'toon2': toonMaterial2,
  129. 'hatching': hatchingMaterial,
  130. 'dotted': dottedMaterial
  131. };
  132. return materials;
  133. }
  134. function createShaderMaterial( shader, light, ambientLight ) {
  135. const u = THREE.UniformsUtils.clone( shader.uniforms );
  136. const vs = shader.vertexShader;
  137. const fs = shader.fragmentShader;
  138. const material = new THREE.ShaderMaterial( { uniforms: u, vertexShader: vs, fragmentShader: fs } );
  139. material.uniforms[ 'uDirLightPos' ].value = light.position;
  140. material.uniforms[ 'uDirLightColor' ].value = light.color;
  141. material.uniforms[ 'uAmbientLightColor' ].value = ambientLight.color;
  142. return material;
  143. }
  144. //
  145. function setupGui() {
  146. const createHandler = function ( id ) {
  147. return function () {
  148. current_material = id;
  149. effect.material = materials[ id ];
  150. effect.enableUvs = ( current_material === 'textured' ) ? true : false;
  151. effect.enableColors = ( current_material === 'colors' || current_material === 'multiColors' ) ? true : false;
  152. };
  153. };
  154. effectController = {
  155. material: 'shiny',
  156. speed: 1.0,
  157. numBlobs: 10,
  158. resolution: 28,
  159. isolation: 80,
  160. floor: true,
  161. wallx: false,
  162. wallz: false,
  163. dummy: function () {}
  164. };
  165. let h;
  166. const gui = new GUI();
  167. // material (type)
  168. h = gui.addFolder( 'Materials' );
  169. for ( const m in materials ) {
  170. effectController[ m ] = createHandler( m );
  171. h.add( effectController, m ).name( m );
  172. }
  173. // simulation
  174. h = gui.addFolder( 'Simulation' );
  175. h.add( effectController, 'speed', 0.1, 8.0, 0.05 );
  176. h.add( effectController, 'numBlobs', 1, 50, 1 );
  177. h.add( effectController, 'resolution', 14, 100, 1 );
  178. h.add( effectController, 'isolation', 10, 300, 1 );
  179. h.add( effectController, 'floor' );
  180. h.add( effectController, 'wallx' );
  181. h.add( effectController, 'wallz' );
  182. }
  183. // this controls content of marching cubes voxel field
  184. function updateCubes( object, time, numblobs, floor, wallx, wallz ) {
  185. object.reset();
  186. // fill the field with some metaballs
  187. const rainbow = [
  188. new THREE.Color( 0xff0000 ),
  189. new THREE.Color( 0xff7f00 ),
  190. new THREE.Color( 0xffff00 ),
  191. new THREE.Color( 0x00ff00 ),
  192. new THREE.Color( 0x0000ff ),
  193. new THREE.Color( 0x4b0082 ),
  194. new THREE.Color( 0x9400d3 )
  195. ];
  196. const subtract = 12;
  197. const strength = 1.2 / ( ( Math.sqrt( numblobs ) - 1 ) / 4 + 1 );
  198. for ( let i = 0; i < numblobs; i ++ ) {
  199. const ballx = Math.sin( i + 1.26 * time * ( 1.03 + 0.5 * Math.cos( 0.21 * i ) ) ) * 0.27 + 0.5;
  200. const bally = Math.abs( Math.cos( i + 1.12 * time * Math.cos( 1.22 + 0.1424 * i ) ) ) * 0.77; // dip into the floor
  201. const ballz = Math.cos( i + 1.32 * time * 0.1 * Math.sin( ( 0.92 + 0.53 * i ) ) ) * 0.27 + 0.5;
  202. if ( current_material === 'multiColors' ) {
  203. object.addBall( ballx, bally, ballz, strength, subtract, rainbow[ i % 7 ] );
  204. } else {
  205. object.addBall( ballx, bally, ballz, strength, subtract );
  206. }
  207. }
  208. if ( floor ) object.addPlaneY( 2, 12 );
  209. if ( wallz ) object.addPlaneZ( 2, 12 );
  210. if ( wallx ) object.addPlaneX( 2, 12 );
  211. object.update();
  212. }
  213. //
  214. function animate() {
  215. requestAnimationFrame( animate );
  216. render();
  217. stats.update();
  218. }
  219. function render() {
  220. const delta = clock.getDelta();
  221. time += delta * effectController.speed * 0.5;
  222. // marching cubes
  223. if ( effectController.resolution !== resolution ) {
  224. resolution = effectController.resolution;
  225. effect.init( Math.floor( resolution ) );
  226. }
  227. if ( effectController.isolation !== effect.isolation ) {
  228. effect.isolation = effectController.isolation;
  229. }
  230. updateCubes( effect, time, effectController.numBlobs, effectController.floor, effectController.wallx, effectController.wallz );
  231. // render
  232. renderer.render( scene, camera );
  233. }
  234. </script>
  235. </body>
  236. </html>