webgl_clipping_intersection.html 3.7 KB

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