webgl_geometry_csg.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js geometry - csg</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. <style>
  9. body {
  10. background-color: #eeeeee;
  11. color: #333;
  12. }
  13. a {
  14. color: #009688;
  15. text-decoration: underline;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> bvh csg - <a href="https://github.com/gkjohnson/three-bvh-csg" target="_blank" rel="noopener">three-bvh-csg</a><br/>
  22. See <a href="https://github.com/gkjohnson/three-bvh-csg" target="_blank" rel="noopener">main project repository</a> for more information and examples on constructive solid geometry.
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.module.js",
  28. "three/addons/": "./jsm/",
  29. "three-mesh-bvh": "https://cdn.jsdelivr.net/npm/three-mesh-bvh@0.7.3/build/index.module.js",
  30. "three-bvh-csg": "https://cdn.jsdelivr.net/npm/three-bvh-csg@0.0.16/build/index.module.js"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  39. import { SUBTRACTION, INTERSECTION, ADDITION, Brush, Evaluator } from 'three-bvh-csg';
  40. let stats;
  41. let camera, scene, renderer;
  42. let baseBrush, brush;
  43. let core;
  44. let result, evaluator, wireframe;
  45. const params = {
  46. operation: SUBTRACTION,
  47. useGroups: true,
  48. wireframe: false,
  49. };
  50. init();
  51. function init() {
  52. // environment
  53. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100 );
  54. camera.position.set( - 1, 1, 1 ).normalize().multiplyScalar( 10 );
  55. scene = new THREE.Scene();
  56. scene.background = new THREE.Color( 0xfce4ec );
  57. // lights
  58. const ambient = new THREE.HemisphereLight( 0xffffff, 0xbfd4d2, 3 );
  59. scene.add( ambient );
  60. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.3 );
  61. directionalLight.position.set( 1, 4, 3 ).multiplyScalar( 3 );
  62. directionalLight.castShadow = true;
  63. directionalLight.shadow.mapSize.setScalar( 2048 );
  64. directionalLight.shadow.bias = - 1e-4;
  65. directionalLight.shadow.normalBias = 1e-4;
  66. scene.add( directionalLight );
  67. // renderer
  68. renderer = new THREE.WebGLRenderer( { antialias: true } );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.setAnimationLoop( animate );
  72. renderer.shadowMap.enabled = true;
  73. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  74. document.body.appendChild( renderer.domElement );
  75. stats = new Stats();
  76. document.body.appendChild( stats.dom );
  77. // add shadow plane
  78. const plane = new THREE.Mesh(
  79. new THREE.PlaneGeometry(),
  80. new THREE.ShadowMaterial( {
  81. color: 0xd81b60,
  82. transparent: true,
  83. opacity: 0.075,
  84. side: THREE.DoubleSide,
  85. } ),
  86. );
  87. plane.position.y = - 3;
  88. plane.rotation.x = - Math.PI / 2;
  89. plane.scale.setScalar( 10 );
  90. plane.receiveShadow = true;
  91. scene.add( plane );
  92. // create brushes
  93. evaluator = new Evaluator();
  94. baseBrush = new Brush(
  95. new THREE.IcosahedronGeometry( 2, 3 ),
  96. new THREE.MeshStandardMaterial( {
  97. flatShading: true,
  98. polygonOffset: true,
  99. polygonOffsetUnits: 1,
  100. polygonOffsetFactor: 1,
  101. } ),
  102. );
  103. brush = new Brush(
  104. new THREE.CylinderGeometry( 1, 1, 5, 45 ),
  105. new THREE.MeshStandardMaterial( {
  106. color: 0x80cbc4,
  107. polygonOffset: true,
  108. polygonOffsetUnits: 1,
  109. polygonOffsetFactor: 1,
  110. } ),
  111. );
  112. core = new Brush(
  113. new THREE.IcosahedronGeometry( 0.15, 1 ),
  114. new THREE.MeshStandardMaterial( {
  115. flatShading: true,
  116. color: 0xff9800,
  117. emissive: 0xff9800,
  118. emissiveIntensity: 0.35,
  119. polygonOffset: true,
  120. polygonOffsetUnits: 1,
  121. polygonOffsetFactor: 1,
  122. } ),
  123. );
  124. core.castShadow = true;
  125. scene.add( core );
  126. // create wireframe
  127. wireframe = new THREE.Mesh(
  128. undefined,
  129. new THREE.MeshBasicMaterial( { color: 0x009688, wireframe: true } ),
  130. );
  131. scene.add( wireframe );
  132. // controls
  133. const controls = new OrbitControls( camera, renderer.domElement );
  134. controls.minDistance = 5;
  135. controls.maxDistance = 75;
  136. // set up gui
  137. const gui = new GUI();
  138. gui.add( params, 'operation', { SUBTRACTION, INTERSECTION, ADDITION } );
  139. gui.add( params, 'wireframe' );
  140. gui.add( params, 'useGroups' );
  141. window.addEventListener( 'resize', onWindowResize );
  142. onWindowResize();
  143. }
  144. function updateCSG() {
  145. evaluator.useGroups = params.useGroups;
  146. result = evaluator.evaluate( baseBrush, brush, params.operation, result );
  147. result.castShadow = true;
  148. result.receiveShadow = true;
  149. scene.add( result );
  150. }
  151. function onWindowResize() {
  152. camera.aspect = window.innerWidth / window.innerHeight;
  153. camera.updateProjectionMatrix();
  154. renderer.setSize( window.innerWidth, window.innerHeight );
  155. }
  156. function animate() {
  157. // update the transforms
  158. const t = window.performance.now() + 9000;
  159. baseBrush.rotation.x = t * 0.0001;
  160. baseBrush.rotation.y = t * 0.00025;
  161. baseBrush.rotation.z = t * 0.0005;
  162. baseBrush.updateMatrixWorld();
  163. brush.rotation.x = t * - 0.0002;
  164. brush.rotation.y = t * - 0.0005;
  165. brush.rotation.z = t * - 0.001;
  166. const s = 0.5 + 0.5 * ( 1 + Math.sin( t * 0.001 ) );
  167. brush.scale.set( s, 1, s );
  168. brush.updateMatrixWorld();
  169. // update the csg
  170. updateCSG();
  171. wireframe.geometry = result.geometry;
  172. wireframe.visible = params.wireframe;
  173. renderer.render( scene, camera );
  174. stats.update();
  175. }
  176. </script>
  177. </body>
  178. </html>