webgl_clipping_intersection.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - clipIntersection</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. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.module.js",
  14. "three/addons/": "./jsm/"
  15. }
  16. }
  17. </script>
  18. <script type="module">
  19. import * as THREE from 'three';
  20. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  21. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  22. let camera, scene, renderer;
  23. const params = {
  24. clipIntersection: true,
  25. planeConstant: 0,
  26. showHelpers: false
  27. };
  28. const clipPlanes = [
  29. new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 ),
  30. new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0 ),
  31. new THREE.Plane( new THREE.Vector3( 0, 0, - 1 ), 0 )
  32. ];
  33. init();
  34. render();
  35. function init() {
  36. renderer = new THREE.WebGLRenderer( { antialias: true } );
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. renderer.localClippingEnabled = true;
  40. document.body.appendChild( renderer.domElement );
  41. scene = new THREE.Scene();
  42. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 200 );
  43. camera.position.set( - 1.5, 2.5, 3.0 );
  44. const controls = new OrbitControls( camera, renderer.domElement );
  45. controls.addEventListener( 'change', render ); // use only if there is no animation loop
  46. controls.minDistance = 1;
  47. controls.maxDistance = 10;
  48. controls.enablePan = false;
  49. const light = new THREE.HemisphereLight( 0xffffff, 0x080808, 4.5 );
  50. light.position.set( - 1.25, 1, 1.25 );
  51. scene.add( light );
  52. //
  53. const group = new THREE.Group();
  54. for ( let i = 1; i <= 30; i += 2 ) {
  55. const geometry = new THREE.SphereGeometry( i / 30, 48, 24 );
  56. const material = new THREE.MeshLambertMaterial( {
  57. color: new THREE.Color().setHSL( Math.random(), 0.5, 0.5, THREE.SRGBColorSpace ),
  58. side: THREE.DoubleSide,
  59. clippingPlanes: clipPlanes,
  60. clipIntersection: params.clipIntersection
  61. } );
  62. group.add( new THREE.Mesh( geometry, material ) );
  63. }
  64. scene.add( group );
  65. // helpers
  66. const helpers = new THREE.Group();
  67. helpers.add( new THREE.PlaneHelper( clipPlanes[ 0 ], 2, 0xff0000 ) );
  68. helpers.add( new THREE.PlaneHelper( clipPlanes[ 1 ], 2, 0x00ff00 ) );
  69. helpers.add( new THREE.PlaneHelper( clipPlanes[ 2 ], 2, 0x0000ff ) );
  70. helpers.visible = false;
  71. scene.add( helpers );
  72. // gui
  73. const gui = new GUI();
  74. gui.add( params, 'clipIntersection' ).name( 'clip intersection' ).onChange( function ( value ) {
  75. const children = group.children;
  76. for ( let i = 0; i < children.length; i ++ ) {
  77. children[ i ].material.clipIntersection = value;
  78. }
  79. render();
  80. } );
  81. gui.add( params, 'planeConstant', - 1, 1 ).step( 0.01 ).name( 'plane constant' ).onChange( function ( value ) {
  82. for ( let j = 0; j < clipPlanes.length; j ++ ) {
  83. clipPlanes[ j ].constant = value;
  84. }
  85. render();
  86. } );
  87. gui.add( params, 'showHelpers' ).name( 'show helpers' ).onChange( function ( value ) {
  88. helpers.visible = value;
  89. render();
  90. } );
  91. //
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. render();
  99. }
  100. function render() {
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>