webgl_clipping_intersection.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  23. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  24. let camera, scene, renderer;
  25. const params = {
  26. clipIntersection: true,
  27. planeConstant: 0,
  28. showHelpers: false
  29. };
  30. const clipPlanes = [
  31. new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 ),
  32. new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0 ),
  33. new THREE.Plane( new THREE.Vector3( 0, 0, - 1 ), 0 )
  34. ];
  35. init();
  36. render();
  37. function init() {
  38. renderer = new THREE.WebGLRenderer( { antialias: true } );
  39. renderer.setPixelRatio( window.devicePixelRatio );
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. renderer.localClippingEnabled = true;
  42. document.body.appendChild( renderer.domElement );
  43. scene = new THREE.Scene();
  44. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 200 );
  45. camera.position.set( - 1.5, 2.5, 3.0 );
  46. const controls = new OrbitControls( camera, renderer.domElement );
  47. controls.addEventListener( 'change', render ); // use only if there is no animation loop
  48. controls.minDistance = 1;
  49. controls.maxDistance = 10;
  50. controls.enablePan = false;
  51. const light = new THREE.HemisphereLight( 0xffffff, 0x080808, 1.5 );
  52. light.position.set( - 1.25, 1, 1.25 );
  53. scene.add( light );
  54. // const helper = new THREE.CameraHelper( light.shadow.camera );
  55. // scene.add( helper );
  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 ),
  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>