-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebGPURenderer: InstancedMesh Supports Instance Color and Morph Target (
#27928) * add instancing and some fix/feat * update example for webgpu * add example to files.json * fix skinning webgpu * cleanup example and pup * up shadow * add default normal instead
- Loading branch information
1 parent
83f7580
commit b0a6ea1
Showing
8 changed files
with
260 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,195 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>three.js webgpu - instancing - Morph Target Animations</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="importmap"> | ||
{ | ||
"imports": { | ||
"three": "../build/three.module.js", | ||
"three/addons/": "./jsm/", | ||
"three/nodes": "./jsm/nodes/Nodes.js" | ||
} | ||
} | ||
</script> | ||
|
||
<script type="module"> | ||
|
||
import * as THREE from 'three'; | ||
|
||
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; | ||
|
||
import Stats from 'three/addons/libs/stats.module.js'; | ||
|
||
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js'; | ||
import { MeshStandardNodeMaterial } from 'three/nodes'; | ||
|
||
let camera, scene, renderer, stats, mesh, mixer, dummy; | ||
|
||
const offset = 5000; | ||
|
||
const timeOffsets = new Float32Array( 1024 ); | ||
|
||
for ( let i = 0; i < 1024; i ++ ) { | ||
|
||
timeOffsets[ i ] = Math.random() * 3; | ||
|
||
} | ||
|
||
const clock = new THREE.Clock( true ); | ||
|
||
init(); | ||
|
||
function init() { | ||
|
||
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 10000 ); | ||
|
||
scene = new THREE.Scene(); | ||
|
||
scene.background = new THREE.Color( 0x99DDFF ); | ||
|
||
scene.fog = new THREE.Fog( 0x99DDFF, 5000, 10000 ); | ||
|
||
|
||
// | ||
|
||
stats = new Stats(); | ||
document.body.appendChild( stats.dom ); | ||
|
||
const light = new THREE.DirectionalLight( 0xffffff, 1 ); | ||
|
||
light.position.set( 200, 1000, 50 ); | ||
|
||
light.shadow.mapSize.width = 2048; | ||
light.shadow.mapSize.height = 2048; | ||
light.castShadow = true; | ||
|
||
light.shadow.camera.left = - 5000; | ||
light.shadow.camera.right = 5000; | ||
light.shadow.camera.top = 5000; | ||
light.shadow.camera.bottom = - 5000; | ||
light.shadow.camera.far = 2000; | ||
|
||
light.shadow.bias = - 0.01; | ||
|
||
light.shadow.camera.updateProjectionMatrix(); | ||
|
||
scene.add( light ); | ||
|
||
const hemi = new THREE.HemisphereLight( 0x99DDFF, 0x669933, 1 / 3 ); | ||
|
||
scene.add( hemi ); | ||
|
||
const ground = new THREE.Mesh( | ||
new THREE.PlaneGeometry( 1000000, 1000000 ), | ||
new THREE.MeshStandardMaterial( { color: 0x669933 } ) | ||
); | ||
|
||
ground.rotation.x = - Math.PI / 2; | ||
|
||
ground.receiveShadow = true; | ||
|
||
scene.add( ground ); | ||
|
||
const loader = new GLTFLoader(); | ||
|
||
loader.load( 'models/gltf/Horse.glb', function ( glb ) { | ||
|
||
dummy = glb.scene.children[ 0 ]; | ||
|
||
mesh = new THREE.InstancedMesh( dummy.geometry, new MeshStandardNodeMaterial( { | ||
flatShading: true, | ||
} ), 1024 ); | ||
|
||
mesh.castShadow = true; | ||
|
||
for ( let x = 0, i = 0; x < 32; x ++ ) { | ||
|
||
for ( let y = 0; y < 32; y ++ ) { | ||
|
||
dummy.position.set( offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y ); | ||
|
||
dummy.updateMatrix(); | ||
|
||
mesh.setMatrixAt( i, dummy.matrix ); | ||
|
||
mesh.setColorAt( i, new THREE.Color( `hsl(${Math.random() * 360}, 50%, 66%)` ) ); | ||
|
||
i ++; | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
scene.add( mesh ); | ||
|
||
mixer = new THREE.AnimationMixer( glb.scene ); | ||
|
||
const action = mixer.clipAction( glb.animations[ 0 ] ); | ||
|
||
action.play(); | ||
|
||
} ); | ||
|
||
|
||
// renderer | ||
|
||
renderer = new WebGPURenderer( { antialias: true } ); | ||
|
||
renderer.setAnimationLoop( animate ); | ||
renderer.setPixelRatio( window.devicePixelRatio ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
document.body.appendChild( renderer.domElement ); | ||
|
||
window.addEventListener( 'resize', onWindowResize ); | ||
|
||
} | ||
|
||
function onWindowResize() { | ||
|
||
camera.aspect = window.innerWidth / window.innerHeight; | ||
camera.updateProjectionMatrix(); | ||
|
||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
|
||
} | ||
|
||
// | ||
|
||
function animate() { | ||
|
||
const time = clock.getElapsedTime(); | ||
|
||
const r = 3000; | ||
camera.position.set( Math.sin( time / 10 ) * r, 1500 + 1000 * Math.cos( time / 5 ), Math.cos( time / 10 ) * r ); | ||
camera.lookAt( 0, 0, 0 ); | ||
|
||
if ( mesh ) { | ||
|
||
for ( let i = 0; i < 1024; i ++ ) { | ||
|
||
mixer.setTime( time + timeOffsets[ i ] ); | ||
|
||
mesh.setMorphAt( i, dummy ); | ||
|
||
} | ||
|
||
mesh.morphTexture.needsUpdate = true; | ||
|
||
} | ||
|
||
renderer.render( scene, camera ); | ||
|
||
stats.update(); | ||
|
||
} | ||
|
||
</script> | ||
|
||
</body> | ||
</html> |
This file contains 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