2
0

webgl_clipping_intersection.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.localClippingEnabled = true;
  43. document.body.appendChild( renderer.domElement );
  44. scene = new THREE.Scene();
  45. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 200 );
  46. camera.position.set( - 1.5, 2.5, 3.0 );
  47. const controls = new OrbitControls( camera, renderer.domElement );
  48. controls.addEventListener( 'change', render ); // use only if there is no animation loop
  49. controls.minDistance = 1;
  50. controls.maxDistance = 10;
  51. controls.enablePan = false;
  52. const light = new THREE.HemisphereLight( 0xffffff, 0x080808, 4.5 );
  53. light.position.set( - 1.25, 1, 1.25 );
  54. scene.add( light );
  55. //
  56. const group = new THREE.Group();
  57. for ( let i = 1; i <= 30; i += 2 ) {
  58. const geometry = new THREE.SphereGeometry( i / 30, 48, 24 );
  59. const material = new THREE.MeshLambertMaterial( {
  60. color: new THREE.Color().setHSL( Math.random(), 0.5, 0.5, THREE.SRGBColorSpace ),
  61. side: THREE.DoubleSide,
  62. clippingPlanes: clipPlanes,
  63. clipIntersection: params.clipIntersection
  64. } );
  65. group.add( new THREE.Mesh( geometry, material ) );
  66. }
  67. scene.add( group );
  68. // helpers
  69. const helpers = new THREE.Group();
  70. helpers.add( new THREE.PlaneHelper( clipPlanes[ 0 ], 2, 0xff0000 ) );
  71. helpers.add( new THREE.PlaneHelper( clipPlanes[ 1 ], 2, 0x00ff00 ) );
  72. helpers.add( new THREE.PlaneHelper( clipPlanes[ 2 ], 2, 0x0000ff ) );
  73. helpers.visible = false;
  74. scene.add( helpers );
  75. // gui
  76. const gui = new GUI();
  77. gui.add( params, 'clipIntersection' ).name( 'clip intersection' ).onChange( function ( value ) {
  78. const children = group.children;
  79. for ( let i = 0; i < children.length; i ++ ) {
  80. children[ i ].material.clipIntersection = value;
  81. }
  82. render();
  83. } );
  84. gui.add( params, 'planeConstant', - 1, 1 ).step( 0.01 ).name( 'plane constant' ).onChange( function ( value ) {
  85. for ( let j = 0; j < clipPlanes.length; j ++ ) {
  86. clipPlanes[ j ].constant = value;
  87. }
  88. render();
  89. } );
  90. gui.add( params, 'showHelpers' ).name( 'show helpers' ).onChange( function ( value ) {
  91. helpers.visible = value;
  92. render();
  93. } );
  94. //
  95. window.addEventListener( 'resize', onWindowResize );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. render();
  102. }
  103. function render() {
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>