-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
Implemented THREE.InstancedMesh #17505
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
01c6bb4
Implemented InstancedMesh
mrdoob 6d8059c
InstancedMesh: Changed setMatrixAt parameter order.
mrdoob 9555f77
Merge branch 'dev' into instancing
mrdoob 114eb01
InstancedMesh: Compute instanceNormalMatrix in vertex shader.
mrdoob 06e83ff
WebGLRenderer.setupVertexAttributes: Simplified ANGLE_instanced_array…
mrdoob 0dbb815
Improved instancing example.
mrdoob 44a0b35
Merge branch 'dev' into instancing
mrdoob 038c576
Merge branch 'dev' into instancing
mrdoob e6f2567
WebGLRenderer: Simplified instancing primcount handling.
mrdoob 03ce4a9
WebGLRenderer: Minor clean up.
mrdoob afcbf1a
Improved instancing example.
mrdoob 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>three.js webgl - instancing</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> | ||
|
|
||
| <script type="module"> | ||
|
|
||
| import * as THREE from '../build/three.module.js'; | ||
|
|
||
| import Stats from './jsm/libs/stats.module.js'; | ||
|
|
||
| var camera, scene, renderer, stats; | ||
|
|
||
| var mesh; | ||
| var amount = 12; | ||
| var count = Math.pow( amount, 3 ); | ||
| var dummy = new THREE.Object3D(); | ||
|
|
||
| init(); | ||
| animate(); | ||
|
|
||
| function init() { | ||
|
|
||
| camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 ); | ||
| camera.position.set( 10, 10, 10 ); | ||
| camera.lookAt( 0, 0, 0 ); | ||
|
|
||
| scene = new THREE.Scene(); | ||
|
|
||
| var material = new THREE.MeshNormalMaterial(); | ||
| // check overdraw | ||
| // var material = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.1, transparent: true } ); | ||
|
|
||
|
|
||
| var loader = new THREE.BufferGeometryLoader(); | ||
| loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) { | ||
|
|
||
| geometry.computeVertexNormals(); | ||
| geometry.scale( 0.5, 0.5, 0.5 ); | ||
|
|
||
| mesh = new THREE.InstancedMesh( geometry, material, count ); | ||
| scene.add( mesh ); | ||
|
|
||
| } ); | ||
|
|
||
| // | ||
|
|
||
| renderer = new THREE.WebGLRenderer( { antialias: true } ); | ||
| renderer.setPixelRatio( window.devicePixelRatio ); | ||
| renderer.setSize( window.innerWidth, window.innerHeight ); | ||
| document.body.appendChild( renderer.domElement ); | ||
|
|
||
| // | ||
|
|
||
| stats = new Stats(); | ||
| document.body.appendChild( stats.dom ); | ||
|
|
||
| // | ||
|
|
||
| window.addEventListener( 'resize', onWindowResize, false ); | ||
|
|
||
| } | ||
|
|
||
| function onWindowResize() { | ||
|
|
||
| camera.aspect = window.innerWidth / window.innerHeight; | ||
| camera.updateProjectionMatrix(); | ||
|
|
||
| renderer.setSize( window.innerWidth, window.innerHeight ); | ||
|
|
||
| } | ||
|
|
||
| // | ||
|
|
||
| function animate() { | ||
|
|
||
| requestAnimationFrame( animate ); | ||
|
|
||
| render(); | ||
|
|
||
| stats.update(); | ||
|
|
||
| } | ||
|
|
||
| function render() { | ||
|
|
||
| if ( mesh ) { | ||
|
|
||
| var time = Date.now() * 0.001; | ||
|
|
||
| mesh.rotation.x = Math.sin( time / 4 ); | ||
| mesh.rotation.y = Math.sin( time / 2 ); | ||
|
|
||
| var i = 0; | ||
|
|
||
| for ( var x = 0; x < amount; x ++ ) { | ||
|
|
||
| for ( var y = 0; y < amount; y ++ ) { | ||
|
|
||
| for ( var z = 0; z < amount; z ++ ) { | ||
|
|
||
| dummy.position.set( 6 - x, 6 - y, 6 - z ); | ||
| dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) ); | ||
| dummy.rotation.z = dummy.rotation.y * 2; | ||
|
|
||
| dummy.updateMatrix(); | ||
|
|
||
| mesh.setMatrixAt( i ++, dummy.matrix ); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| mesh.instanceMatrix.needsUpdate = true; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: I've used mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); |
||
|
|
||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /** | ||
| * @author mrdoob / http://mrdoob.com/ | ||
| */ | ||
|
|
||
| import { BufferAttribute } from '../core/BufferAttribute.js'; | ||
| import { Mesh } from './Mesh.js'; | ||
|
|
||
| function InstancedMesh( geometry, material, count ) { | ||
|
|
||
| Mesh.call( this, geometry, material ); | ||
|
|
||
| this.instanceMatrix = new BufferAttribute( new Float32Array( count * 16 ), 16 ); | ||
|
|
||
| } | ||
|
|
||
| InstancedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), { | ||
|
|
||
| constructor: InstancedMesh, | ||
|
|
||
| isInstancedMesh: true, | ||
|
|
||
| raycast: function () {}, | ||
|
|
||
| setMatrixAt: function ( index, matrix ) { | ||
|
|
||
| matrix.toArray( this.instanceMatrix.array, index * 16 ); | ||
|
|
||
| }, | ||
|
|
||
| updateMorphTargets: function () {} | ||
|
|
||
| } ); | ||
|
|
||
| export { InstancedMesh }; |
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
10 changes: 9 additions & 1 deletion
10
src/renderers/shaders/ShaderChunk/defaultnormal_vertex.glsl.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| export default /* glsl */` | ||
| vec4 mvPosition = modelViewMatrix * vec4( transformed, 1.0 ); | ||
| vec4 mvPosition = vec4( transformed, 1.0 ); | ||
|
|
||
| #ifdef USE_INSTANCING | ||
|
|
||
| mvPosition = instanceMatrix * mvPosition; | ||
|
|
||
| #endif | ||
|
|
||
| mvPosition = modelViewMatrix * mvPosition; | ||
|
|
||
| gl_Position = projectionMatrix * mvPosition; | ||
| `; |
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 |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| export default /* glsl */` | ||
| #if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined ( USE_SHADOWMAP ) | ||
|
|
||
| vec4 worldPosition = modelMatrix * vec4( transformed, 1.0 ); | ||
| vec4 worldPosition = vec4( transformed, 1.0 ); | ||
|
|
||
| #ifdef USE_INSTANCING | ||
|
|
||
| worldPosition = instanceMatrix * worldPosition; | ||
|
|
||
| #endif | ||
|
|
||
| worldPosition = modelMatrix * worldPosition; | ||
|
|
||
| #endif | ||
| `; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should have only one geometry pushed to the GPU.
This works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example is still work in progress but... it actually shows that you can easily change geometries at any point, which is something that other PRs didn't make it easy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, I guess you should dispose of the old geometry...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, users copy these examples, so I still would suggest using the conventional pattern I posted above, and then add a GUI that allows the user to switch the geometry and/or material (if you feel it is important to demonstrate that capability). JMO.