webgl_clipping_intersection.html 4.0 KB

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