webgl_clipping_intersection.html 4.1 KB

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