webgl_clipping_intersection.html 3.7 KB

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