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