webgl_marchingcubes.html 9.1 KB

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