webgl_clipculldistance.html 4.4 KB

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