webgl_clipping_intersection.html 4.0 KB

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