webgl_clipculldistance.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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, clock, 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. clock = new THREE.Clock();
  54. //
  55. renderer = new THREE.WebGLRenderer( { antialias: true } );
  56. renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. renderer.setAnimationLoop( animate );
  59. document.body.appendChild( renderer.domElement );
  60. if ( renderer.extensions.has( 'WEBGL_clip_cull_distance' ) === false ) {
  61. document.getElementById( 'notSupported' ).style.display = '';
  62. return;
  63. }
  64. const ext = renderer
  65. .getContext()
  66. .getExtension( 'WEBGL_clip_cull_distance' );
  67. const gl = renderer.getContext();
  68. gl.enable( ext.CLIP_DISTANCE0_WEBGL );
  69. // geometry
  70. const vertexCount = 200 * 3;
  71. const geometry = new THREE.BufferGeometry();
  72. const positions = [];
  73. const colors = [];
  74. for ( let i = 0; i < vertexCount; i ++ ) {
  75. // adding x,y,z
  76. positions.push( Math.random() - 0.5 );
  77. positions.push( Math.random() - 0.5 );
  78. positions.push( Math.random() - 0.5 );
  79. // adding r,g,b,a
  80. colors.push( Math.random() * 255 );
  81. colors.push( Math.random() * 255 );
  82. colors.push( Math.random() * 255 );
  83. colors.push( Math.random() * 255 );
  84. }
  85. const positionAttribute = new THREE.Float32BufferAttribute( positions, 3 );
  86. const colorAttribute = new THREE.Uint8BufferAttribute( colors, 4 );
  87. colorAttribute.normalized = true;
  88. geometry.setAttribute( 'position', positionAttribute );
  89. geometry.setAttribute( 'color', colorAttribute );
  90. // material
  91. material = new THREE.ShaderMaterial( {
  92. uniforms: {
  93. time: { value: 1.0 }
  94. },
  95. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  96. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  97. side: THREE.DoubleSide,
  98. transparent: true,
  99. vertexColors: true
  100. } );
  101. material.extensions.clipCullDistance = true;
  102. const mesh = new THREE.Mesh( geometry, material );
  103. scene.add( mesh );
  104. //
  105. controls = new OrbitControls( camera, renderer.domElement );
  106. //
  107. stats = new Stats();
  108. document.body.appendChild( stats.dom );
  109. window.addEventListener( 'resize', onWindowResize );
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. function animate() {
  117. controls.update();
  118. stats.update();
  119. material.uniforms.time.value = clock.getElapsedTime();
  120. renderer.render( scene, camera );
  121. }
  122. </script>
  123. </body>
  124. </html>