webgl_marchingcubes.html 8.8 KB

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