webgl_clipping_intersection.html 4.4 KB

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