|
| 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 | + |
| 12 | + <div id="info"> |
| 13 | + <a href="https://threejs.org" target="_blank" rel="noopener" >three.js</a> - vertex shader clipping via |
| 14 | + <a href="https://registry.khronos.org/webgl/extensions/WEBGL_clip_cull_distance/" target="_blank" rel="noopener" >WEBGL_clip_cull_distance</a> |
| 15 | + <div id="notSupported" style="display:none">WEBGL_clip_cull_distance not supported</div> |
| 16 | + </div> |
| 17 | + |
| 18 | + <script id="vertexShader" type="x-shader/x-vertex"> |
| 19 | + |
| 20 | + uniform float time; |
| 21 | + |
| 22 | + varying vec4 vColor; |
| 23 | + |
| 24 | + void main() { |
| 25 | + |
| 26 | + vColor = color; |
| 27 | + |
| 28 | + #ifdef USE_CLIP_DISTANCE |
| 29 | + vec4 worldPosition = modelMatrix * vec4( position, 1.0 ); |
| 30 | + gl_ClipDistance[ 0 ] = worldPosition.x - sin( time ) * ( 0.5 ); |
| 31 | + #endif |
| 32 | + |
| 33 | + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + </script> |
| 38 | + |
| 39 | + <script id="fragmentShader" type="x-shader/x-fragment"> |
| 40 | + |
| 41 | + varying vec4 vColor; |
| 42 | + |
| 43 | + void main() { |
| 44 | + |
| 45 | + gl_FragColor = vColor; |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + </script> |
| 50 | + |
| 51 | + <script type="importmap"> |
| 52 | + { |
| 53 | + "imports": { |
| 54 | + "three": "../build/three.module.js", |
| 55 | + "three/addons/": "./jsm/" |
| 56 | + } |
| 57 | + } |
| 58 | + </script> |
| 59 | + |
| 60 | + <script type="module"> |
| 61 | + import * as THREE from 'three'; |
| 62 | + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 63 | + import Stats from 'three/addons/libs/stats.module.js'; |
| 64 | + |
| 65 | + let camera, controls, clock, scene, renderer, stats; |
| 66 | + |
| 67 | + let material; |
| 68 | + |
| 69 | + init(); |
| 70 | + animate(); |
| 71 | + |
| 72 | + function init() { |
| 73 | + |
| 74 | + camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 ); |
| 75 | + camera.position.z = 2; |
| 76 | + |
| 77 | + scene = new THREE.Scene(); |
| 78 | + |
| 79 | + clock = new THREE.Clock(); |
| 80 | + |
| 81 | + // |
| 82 | + |
| 83 | + renderer = new THREE.WebGLRenderer( { antialias: true } ); |
| 84 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 85 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 86 | + document.body.appendChild( renderer.domElement ); |
| 87 | + |
| 88 | + if ( renderer.extensions.has( 'WEBGL_clip_cull_distance' ) === false ) { |
| 89 | + |
| 90 | + document.getElementById( 'notSupported' ).style.display = ''; |
| 91 | + return; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + const ext = renderer |
| 96 | + .getContext() |
| 97 | + .getExtension( 'WEBGL_clip_cull_distance' ); |
| 98 | + const gl = renderer.getContext(); |
| 99 | + |
| 100 | + gl.enable( ext.CLIP_DISTANCE0_WEBGL ); |
| 101 | + |
| 102 | + // geometry |
| 103 | + |
| 104 | + const vertexCount = 200 * 3; |
| 105 | + |
| 106 | + const geometry = new THREE.BufferGeometry(); |
| 107 | + |
| 108 | + const positions = []; |
| 109 | + const colors = []; |
| 110 | + |
| 111 | + for ( let i = 0; i < vertexCount; i ++ ) { |
| 112 | + |
| 113 | + // adding x,y,z |
| 114 | + positions.push( Math.random() - 0.5 ); |
| 115 | + positions.push( Math.random() - 0.5 ); |
| 116 | + positions.push( Math.random() - 0.5 ); |
| 117 | + |
| 118 | + // adding r,g,b,a |
| 119 | + colors.push( Math.random() * 255 ); |
| 120 | + colors.push( Math.random() * 255 ); |
| 121 | + colors.push( Math.random() * 255 ); |
| 122 | + colors.push( Math.random() * 255 ); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + const positionAttribute = new THREE.Float32BufferAttribute( positions, 3 ); |
| 127 | + const colorAttribute = new THREE.Uint8BufferAttribute( colors, 4 ); |
| 128 | + colorAttribute.normalized = true; |
| 129 | + |
| 130 | + geometry.setAttribute( 'position', positionAttribute ); |
| 131 | + geometry.setAttribute( 'color', colorAttribute ); |
| 132 | + |
| 133 | + // material |
| 134 | + |
| 135 | + material = new THREE.ShaderMaterial( { |
| 136 | + |
| 137 | + uniforms: { |
| 138 | + time: { value: 1.0 } |
| 139 | + }, |
| 140 | + vertexShader: document.getElementById( 'vertexShader' ).textContent, |
| 141 | + fragmentShader: document.getElementById( 'fragmentShader' ).textContent, |
| 142 | + side: THREE.DoubleSide, |
| 143 | + transparent: true, |
| 144 | + vertexColors: true |
| 145 | + |
| 146 | + } ); |
| 147 | + |
| 148 | + material.extensions.clipCullDistance = true; |
| 149 | + |
| 150 | + const mesh = new THREE.Mesh( geometry, material ); |
| 151 | + scene.add( mesh ); |
| 152 | + |
| 153 | + // |
| 154 | + |
| 155 | + controls = new OrbitControls( camera, renderer.domElement ); |
| 156 | + |
| 157 | + // |
| 158 | + |
| 159 | + stats = new Stats(); |
| 160 | + document.body.appendChild( stats.dom ); |
| 161 | + |
| 162 | + window.addEventListener( 'resize', onWindowResize ); |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + function onWindowResize() { |
| 167 | + |
| 168 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 169 | + camera.updateProjectionMatrix(); |
| 170 | + |
| 171 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + function animate() { |
| 176 | + |
| 177 | + requestAnimationFrame( animate ); |
| 178 | + |
| 179 | + controls.update(); |
| 180 | + stats.update(); |
| 181 | + |
| 182 | + material.uniforms.time.value = clock.getElapsedTime(); |
| 183 | + |
| 184 | + renderer.render( scene, camera ); |
| 185 | + |
| 186 | + } |
| 187 | + </script> |
| 188 | + </body> |
| 189 | +</html> |
0 commit comments