-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
WebGLRenderer: Add WEBGL_clip_cull_distance support.
#27371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
38c26fc
init WEBGL_clip_cull_distance support
RenaudRohlinger 782856b
remove unecessary conditions webgl2
RenaudRohlinger 8e66730
try fix directx
RenaudRohlinger e7d990d
feedbacks
RenaudRohlinger 166bd84
remove unecessary code
RenaudRohlinger 07e4156
Update webgl_clipculldistance.html
Mugen87 f7167f3
Update WebGLExtensions.js
Mugen87 95f1f47
Update webgl_clipculldistance.html
Mugen87 16a75ad
replace material with shader
RenaudRohlinger 40bf47d
remove unused time const
RenaudRohlinger cc1f6f8
Update webgl_clipculldistance.html
Mugen87 a4fedcb
Update clipping example.
Mugen87 6b8373f
Revert builds.
Mugen87 5fcf517
ShaderMaterial: Update `extensions` and docs.
Mugen87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>three.js webgl2 - clip cull distance</title> | ||
| <meta charset="utf-8" /> | ||
| <meta | ||
| name="viewport" | ||
| content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" /> | ||
| <link | ||
| type="text/css" | ||
| rel="stylesheet" | ||
| href="main.css" /> | ||
| </head> | ||
| <body> | ||
| <div id="container"></div> | ||
|
|
||
| <div id="info"> | ||
| <a | ||
| href="https://threejs.org" | ||
| target="_blank" | ||
| rel="noopener" | ||
| >three.js</a | ||
| > | ||
| - clip cull distance | ||
| <a | ||
| href="https://registry.khronos.org/webgl/extensions/EXT_clip_cull_distance/" | ||
| target="_blank" | ||
| rel="noopener" | ||
| >EXT_clip_cull_distance</a | ||
| > | ||
| <div id="notSupported" style="display:none">WEBGL_clip_cull_distance not supported</div> | ||
| </div> | ||
|
|
||
| <script type="importmap"> | ||
| { | ||
| "imports": { | ||
| "three": "../build/three.module.js", | ||
| "three/addons/": "./jsm/" | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <script type="module"> | ||
| import * as THREE from 'three'; | ||
| import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; | ||
| import Stats from 'three/addons/libs/stats.module.js'; | ||
|
|
||
| let stats; | ||
| let camera, controls, scene, renderer; | ||
| let geometries, mesh, material; | ||
| const ids = []; | ||
| const matrix = new THREE.Matrix4(); | ||
|
|
||
| // | ||
|
|
||
| const position = new THREE.Vector3(); | ||
| const rotation = new THREE.Euler(); | ||
| const quaternion = new THREE.Quaternion(); | ||
| const scale = new THREE.Vector3(); | ||
|
|
||
| const api = { | ||
| count: 10000, | ||
| clipCullExtension: true | ||
| }; | ||
|
|
||
| const time = new THREE.Uniform( 0 ); | ||
|
|
||
| // | ||
|
|
||
| init(); | ||
| animate(); | ||
|
|
||
| function init() { | ||
|
|
||
| const container = document.getElementById( 'container' ); | ||
|
|
||
| const width = window.innerWidth; | ||
| const height = window.innerHeight; | ||
|
|
||
| camera = new THREE.PerspectiveCamera( 70, width / height, 1, 100 ); | ||
| camera.position.z = 30; | ||
|
|
||
| scene = new THREE.Scene(); | ||
|
|
||
| renderer = new THREE.WebGLRenderer(); | ||
| renderer.setPixelRatio( 1 ); | ||
| renderer.setSize( window.innerWidth, window.innerHeight ); | ||
| container.appendChild( renderer.domElement ); | ||
|
|
||
| if ( renderer.extensions.has( 'WEBGL_clip_cull_distance' ) === false ) { | ||
|
|
||
| document.getElementById( 'notSupported' ).style.display = ''; | ||
| return; | ||
|
|
||
| } | ||
|
|
||
| // controls | ||
| controls = new OrbitControls( camera, renderer.domElement ); | ||
|
|
||
| // stats | ||
| stats = new Stats(); | ||
| document.body.appendChild( stats.dom ); | ||
|
|
||
|
|
||
| const ext = renderer | ||
| .getContext() | ||
| .getExtension( 'WEBGL_clip_cull_distance' ); | ||
| const gl = renderer.getContext(); | ||
|
|
||
| gl.enable( ext.CLIP_DISTANCE0_WEBGL ); | ||
|
|
||
|
|
||
| initGeometries(); | ||
| initBatchedMesh(); | ||
| onWindowResize(); | ||
|
|
||
| window.addEventListener( 'resize', onWindowResize ); | ||
|
|
||
| } | ||
|
|
||
| function onWindowResize() { | ||
|
|
||
| camera.aspect = window.innerWidth / window.innerHeight; | ||
| camera.updateProjectionMatrix(); | ||
|
|
||
| renderer.setSize( window.innerWidth, window.innerHeight ); | ||
|
|
||
| } | ||
|
|
||
| function randomizeMatrix( matrix ) { | ||
|
|
||
| position.x = Math.random() * 40 - 20; | ||
| position.y = Math.random() * 40 - 20; | ||
| position.z = Math.random() * 40 - 20; | ||
|
|
||
| rotation.x = Math.random() * 2 * Math.PI; | ||
| rotation.y = Math.random() * 2 * Math.PI; | ||
| rotation.z = Math.random() * 2 * Math.PI; | ||
|
|
||
| quaternion.setFromEuler( rotation ); | ||
|
|
||
| scale.x = scale.y = scale.z = 0.5 + Math.random() * 0.5; | ||
|
|
||
| return matrix.compose( position, quaternion, scale ); | ||
|
|
||
| } | ||
|
|
||
| function randomizeRotationSpeed( rotation ) { | ||
|
|
||
| rotation.x = Math.random() * 0.01; | ||
| rotation.y = Math.random() * 0.01; | ||
| rotation.z = Math.random() * 0.01; | ||
| return rotation; | ||
|
|
||
| } | ||
|
|
||
| function initGeometries() { | ||
|
|
||
| geometries = [ | ||
| new THREE.ConeGeometry( 1.0, 2.0 ), | ||
| new THREE.BoxGeometry( 2.0, 2.0, 2.0 ), | ||
| new THREE.SphereGeometry( 1.0, 16, 8 ), | ||
| ]; | ||
|
|
||
| } | ||
|
|
||
| function initBatchedMesh() { | ||
|
|
||
| const geometryCount = api.count; | ||
| const vertexCount = api.count * 512; | ||
| const indexCount = api.count * 1024; | ||
|
|
||
| const euler = new THREE.Euler(); | ||
| const matrix = new THREE.Matrix4(); | ||
|
|
||
| material = new THREE.MeshNormalMaterial(); | ||
| material.extensions = { | ||
| clipCullDistance: true, | ||
| }; | ||
|
|
||
| material.onBeforeCompile = function ( shader ) { | ||
|
|
||
| shader.vertexShader = shader.vertexShader.replace( | ||
| '#include <common>', | ||
| ` | ||
| #include <common> | ||
| uniform float time; | ||
| ` | ||
| ); | ||
| shader.vertexShader = shader.vertexShader.replace( | ||
| '#include <project_vertex>', | ||
| ` | ||
| #include <project_vertex> | ||
| #ifdef USE_CLIP_DISTANCE | ||
| vec4 worldPosition = modelMatrix * batchingMatrix * vec4(transformed, 1.); | ||
| gl_ClipDistance[ 0 ] = worldPosition.x - sin(time) * (20. + 1.5); | ||
| #endif | ||
| ` | ||
| ); | ||
|
|
||
| shader.uniforms.time = time; | ||
|
|
||
| }; | ||
|
|
||
| mesh = new THREE.BatchedMesh( | ||
| geometryCount, | ||
| vertexCount, | ||
| indexCount, | ||
| material | ||
| ); | ||
| mesh.userData.rotationSpeeds = []; | ||
|
|
||
| mesh.isMesh = false; | ||
| mesh.isPoints = true; | ||
| mesh.frustumCulled = false; | ||
|
|
||
| ids.length = 0; | ||
|
|
||
| for ( let i = 0; i < api.count; i ++ ) { | ||
|
|
||
| const id = mesh.addGeometry( geometries[ i % geometries.length ] ); | ||
| mesh.setMatrixAt( id, randomizeMatrix( matrix ) ); | ||
|
|
||
| const rotationMatrix = new THREE.Matrix4(); | ||
| rotationMatrix.makeRotationFromEuler( randomizeRotationSpeed( euler ) ); | ||
| mesh.userData.rotationSpeeds.push( rotationMatrix ); | ||
|
|
||
| ids.push( id ); | ||
|
|
||
| } | ||
|
|
||
| scene.add( mesh ); | ||
|
|
||
| } | ||
|
|
||
| function animateMeshes() { | ||
|
|
||
| const loopNum = Math.min( api.count, api.dynamic ); | ||
|
|
||
| for ( let i = 0; i < loopNum; i ++ ) { | ||
|
|
||
| const rotationMatrix = mesh.userData.rotationSpeeds[ i ]; | ||
| const id = ids[ i ]; | ||
|
|
||
| mesh.getMatrixAt( id, matrix ); | ||
| matrix.multiply( rotationMatrix ); | ||
| mesh.setMatrixAt( id, matrix ); | ||
|
|
||
| } | ||
|
|
||
| } | ||
| // | ||
|
|
||
| function animate() { | ||
|
|
||
| requestAnimationFrame( animate ); | ||
|
|
||
| animateMeshes(); | ||
| controls.update(); | ||
| stats.update(); | ||
| time.value = performance.now() * 0.001; | ||
|
|
||
| renderer.render( scene, camera ); | ||
|
|
||
| } | ||
| </script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.