webgl_clipculldistance.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGL 2 - clip cull distance</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 2 - clip cull distance">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_clipculldistance.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_clipculldistance.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css" />
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener" >three.js</a> - vertex shader clipping via
  17. <a href="https://registry.khronos.org/webgl/extensions/WEBGL_clip_cull_distance/" target="_blank" rel="noopener" >WEBGL_clip_cull_distance</a>
  18. <div id="notSupported" style="display:none">WEBGL_clip_cull_distance not supported</div>
  19. </div>
  20. <script id="vertexShader" type="x-shader/x-vertex">
  21. uniform float time;
  22. varying vec4 vColor;
  23. void main() {
  24. vColor = color;
  25. #ifdef USE_CLIP_DISTANCE
  26. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  27. gl_ClipDistance[ 0 ] = worldPosition.x - sin( time ) * ( 0.5 );
  28. #endif
  29. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  30. }
  31. </script>
  32. <script id="fragmentShader" type="x-shader/x-fragment">
  33. varying vec4 vColor;
  34. void main() {
  35. gl_FragColor = vColor;
  36. }
  37. </script>
  38. <script type="importmap">
  39. {
  40. "imports": {
  41. "three": "../build/three.module.js",
  42. "three/addons/": "./jsm/"
  43. }
  44. }
  45. </script>
  46. <script type="module">
  47. import * as THREE from 'three';
  48. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  49. import Stats from 'three/addons/libs/stats.module.js';
  50. let camera, controls, timer, scene, renderer, stats;
  51. let material;
  52. init();
  53. function init() {
  54. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 );
  55. camera.position.z = 2;
  56. scene = new THREE.Scene();
  57. timer = new THREE.Timer();
  58. timer.connect( document );
  59. //
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. document.body.appendChild( renderer.domElement );
  65. if ( renderer.extensions.has( 'WEBGL_clip_cull_distance' ) === false ) {
  66. document.getElementById( 'notSupported' ).style.display = '';
  67. return;
  68. }
  69. const ext = renderer
  70. .getContext()
  71. .getExtension( 'WEBGL_clip_cull_distance' );
  72. const gl = renderer.getContext();
  73. gl.enable( ext.CLIP_DISTANCE0_WEBGL );
  74. // geometry
  75. const vertexCount = 200 * 3;
  76. const geometry = new THREE.BufferGeometry();
  77. const positions = [];
  78. const colors = [];
  79. for ( let i = 0; i < vertexCount; i ++ ) {
  80. // adding x,y,z
  81. positions.push( Math.random() - 0.5 );
  82. positions.push( Math.random() - 0.5 );
  83. positions.push( Math.random() - 0.5 );
  84. // adding r,g,b,a
  85. colors.push( Math.random() * 255 );
  86. colors.push( Math.random() * 255 );
  87. colors.push( Math.random() * 255 );
  88. colors.push( Math.random() * 255 );
  89. }
  90. const positionAttribute = new THREE.Float32BufferAttribute( positions, 3 );
  91. const colorAttribute = new THREE.Uint8BufferAttribute( colors, 4 );
  92. colorAttribute.normalized = true;
  93. geometry.setAttribute( 'position', positionAttribute );
  94. geometry.setAttribute( 'color', colorAttribute );
  95. // material
  96. material = new THREE.ShaderMaterial( {
  97. uniforms: {
  98. time: { value: 1.0 }
  99. },
  100. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  101. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  102. side: THREE.DoubleSide,
  103. transparent: true,
  104. vertexColors: true
  105. } );
  106. material.extensions.clipCullDistance = true;
  107. const mesh = new THREE.Mesh( geometry, material );
  108. scene.add( mesh );
  109. //
  110. controls = new OrbitControls( camera, renderer.domElement );
  111. //
  112. stats = new Stats();
  113. document.body.appendChild( stats.dom );
  114. window.addEventListener( 'resize', onWindowResize );
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function animate() {
  122. timer.update();
  123. controls.update();
  124. stats.update();
  125. material.uniforms.time.value = timer.getElapsed();
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>
粤ICP备19079148号