webgl_clipping_intersection.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, 1.5 );
  53. light.position.set( - 1.25, 1, 1.25 );
  54. scene.add( light );
  55. // const helper = new THREE.CameraHelper( light.shadow.camera );
  56. // scene.add( helper );
  57. //
  58. const group = new THREE.Group();
  59. for ( let i = 1; i <= 30; i += 2 ) {
  60. const geometry = new THREE.SphereGeometry( i / 30, 48, 24 );
  61. const material = new THREE.MeshLambertMaterial( {
  62. color: new THREE.Color().setHSL( Math.random(), 0.5, 0.5 ),
  63. side: THREE.DoubleSide,
  64. clippingPlanes: clipPlanes,
  65. clipIntersection: params.clipIntersection
  66. } );
  67. group.add( new THREE.Mesh( geometry, material ) );
  68. }
  69. scene.add( group );
  70. // helpers
  71. const helpers = new THREE.Group();
  72. helpers.add( new THREE.PlaneHelper( clipPlanes[ 0 ], 2, 0xff0000 ) );
  73. helpers.add( new THREE.PlaneHelper( clipPlanes[ 1 ], 2, 0x00ff00 ) );
  74. helpers.add( new THREE.PlaneHelper( clipPlanes[ 2 ], 2, 0x0000ff ) );
  75. helpers.visible = false;
  76. scene.add( helpers );
  77. // gui
  78. const gui = new GUI();
  79. gui.add( params, 'clipIntersection' ).name( 'clip intersection' ).onChange( function ( value ) {
  80. const children = group.children;
  81. for ( let i = 0; i < children.length; i ++ ) {
  82. children[ i ].material.clipIntersection = value;
  83. }
  84. render();
  85. } );
  86. gui.add( params, 'planeConstant', - 1, 1 ).step( 0.01 ).name( 'plane constant' ).onChange( function ( value ) {
  87. for ( let j = 0; j < clipPlanes.length; j ++ ) {
  88. clipPlanes[ j ].constant = value;
  89. }
  90. render();
  91. } );
  92. gui.add( params, 'showHelpers' ).name( 'show helpers' ).onChange( function ( value ) {
  93. helpers.visible = value;
  94. render();
  95. } );
  96. //
  97. window.addEventListener( 'resize', onWindowResize );
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. render();
  104. }
  105. function render() {
  106. renderer.render( scene, camera );
  107. }
  108. </script>
  109. </body>
  110. </html>