Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[pull] dev from mrdoob:dev #602

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 3 commits into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
"webgpu_procedural_texture",
"webgpu_reflection",
"webgpu_reflection_blurred",
"webgpu_reflection_roughness",
"webgpu_refraction",
"webgpu_rendertarget_2d-array_3d",
"webgpu_rtt",
Expand Down
78 changes: 53 additions & 25 deletions examples/jsm/physics/RapierPhysics.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,19 @@ async function RapierPhysics() {
shape.setMass( mass );
shape.setRestitution( restitution );

const body = mesh.isInstancedMesh
const { body, collider } = mesh.isInstancedMesh
? createInstancedBody( mesh, mass, shape )
: createBody( mesh.position, mesh.quaternion, mass, shape );

if ( ! mesh.userData.physics ) mesh.userData.physics = {};

mesh.userData.physics.body = body;
mesh.userData.physics.collider = collider;

if ( mass > 0 ) {

meshes.push( mesh );
meshMap.set( mesh, body );
meshMap.set( mesh, { body, collider } );

}

Expand All @@ -161,22 +162,10 @@ async function RapierPhysics() {
if ( ! mesh.userData.physics ) return;

const body = mesh.userData.physics.body;
const collider = mesh.userData.physics.collider;

if ( ! body ) return;

if ( Array.isArray( body ) ) {

for ( let i = 0; i < body.length; i ++ ) {

world.removeRigidBody( body[ i ] );

}

} else {

world.removeRigidBody( body );

}
if ( body ) removeBody( body );
if ( collider ) removeCollider( collider );

}

Expand All @@ -187,15 +176,18 @@ async function RapierPhysics() {
const array = mesh.instanceMatrix.array;

const bodies = [];
const colliders = [];

for ( let i = 0; i < mesh.count; i ++ ) {

const position = _vector.fromArray( array, i * 16 + 12 );
bodies.push( createBody( position, null, mass, shape ) );
const { body, collider } = createBody( position, null, mass, shape );
bodies.push( body );
colliders.push( collider );

}

return bodies;
return { body: bodies, collider: colliders };

}

Expand All @@ -206,15 +198,51 @@ async function RapierPhysics() {
if ( quaternion !== null ) desc.setRotation( quaternion );

const body = world.createRigidBody( desc );
world.createCollider( shape, body );
const collider = world.createCollider( shape, body );

return body;
return { body, collider };

}

function removeBody( body ) {

if ( Array.isArray( body ) ) {

for ( let i = 0; i < body.length; i ++ ) {

world.removeRigidBody( body[ i ] );

}

} else {

world.removeRigidBody( body );

}

}

function removeCollider( collider ) {

if ( Array.isArray( collider ) ) {

for ( let i = 0; i < collider.length; i ++ ) {

world.removeCollider( collider[ i ] );

}

} else {

world.removeCollider( collider );

}

}

function setMeshPosition( mesh, position, index = 0 ) {

let body = meshMap.get( mesh );
let { body } = meshMap.get( mesh );

if ( mesh.isInstancedMesh ) {

Expand All @@ -230,7 +258,7 @@ async function RapierPhysics() {

function setMeshVelocity( mesh, velocity, index = 0 ) {

let body = meshMap.get( mesh );
let { body } = meshMap.get( mesh );

if ( mesh.isInstancedMesh ) {

Expand Down Expand Up @@ -278,7 +306,7 @@ async function RapierPhysics() {
if ( mesh.isInstancedMesh ) {

const array = mesh.instanceMatrix.array;
const bodies = meshMap.get( mesh );
const { body: bodies } = meshMap.get( mesh );

for ( let j = 0; j < bodies.length; j ++ ) {

Expand All @@ -296,7 +324,7 @@ async function RapierPhysics() {

} else {

const body = meshMap.get( mesh );
const { body } = meshMap.get( mesh );

mesh.position.copy( body.translation() );
mesh.quaternion.copy( body.rotation() );
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
168 changes: 168 additions & 0 deletions examples/webgpu_reflection_roughness.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - roughness reflection</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="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - dirty reflection
</div>

<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/webgpu": "../build/three.webgpu.js",
"three/tsl": "../build/three.tsl.js",
"three/addons/": "./jsm/"
}
}
</script>

<script type="module">

import * as THREE from 'three';
import { Fn, vec2, vec4, texture, uv, textureBicubic, rangeFogFactor, reflector, time } from 'three/tsl';

import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

import Stats from 'three/addons/libs/stats.module.js';

let camera, scene, renderer;
let controls;
let stats;

init();

async function init() {

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
camera.position.set( - 4, 2, 4 );
camera.lookAt( 0, .4, 0 );

scene = new THREE.Scene();

//

new RGBELoader()
.setPath( 'textures/equirectangular/' )
.load( 'moonless_golf_1k.hdr', function ( texture ) {

texture.mapping = THREE.EquirectangularReflectionMapping;

scene.background = texture;
scene.environment = texture;

} );

// textures

const textureLoader = new THREE.TextureLoader();

const uvMap = textureLoader.load( 'textures/uv_grid_directx.jpg' );
uvMap.colorSpace = THREE.SRGBColorSpace;

const perlinMap = textureLoader.load( './textures/noises/perlin/rgb-256x256.png' );
perlinMap.wrapS = THREE.RepeatWrapping;
perlinMap.wrapT = THREE.RepeatWrapping;
perlinMap.colorSpace = THREE.SRGBColorSpace;

// uv map for debugging

const uvMaterial = new THREE.MeshStandardNodeMaterial( {
map: uvMap,
emissiveMap: uvMap,
emissive: 0xffffff,
side: THREE.DoubleSide
} );

const uvMesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), uvMaterial );
uvMesh.position.set( 0, 1, 0 );
scene.add( uvMesh );

// reflection

const reflection = reflector( { resolution: .5, bounces: false, generateMipmaps: true } ); // 0.5 is half of the rendering view
reflection.target.rotateX( - Math.PI / 2 );
scene.add( reflection.target );

const animatedUV = uv().mul( 10 ).add( vec2( time.mul( .1 ), 0 ) );
const roughness = texture( perlinMap, animatedUV ).r.mul( 2 ).saturate();

const floorMaterial = new THREE.MeshStandardNodeMaterial();
floorMaterial.transparent = true;
floorMaterial.metalness = 1;
floorMaterial.roughnessNode = roughness.mul( .2 );
floorMaterial.colorNode = Fn( () => {

// blur reflection using textureBicubic()
const dirtyReflection = textureBicubic( reflection, roughness.mul( .9 ) ).rgb;

// falloff opacity by distance like an opacity-fog
const opacity = rangeFogFactor( 7, 25 ).oneMinus();

return vec4( dirtyReflection.rgb, opacity );

} )();

const floor = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), floorMaterial );
floor.position.set( 0, 0, 0 );
scene.add( floor );

// renderer

renderer = new THREE.WebGPURenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.toneMapping = THREE.NeutralToneMapping;
renderer.toneMappingExposure = 2;
document.body.appendChild( renderer.domElement );

stats = new Stats();
document.body.appendChild( stats.dom );

controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 1;
controls.maxDistance = 10;
controls.maxPolarAngle = Math.PI / 2;
controls.autoRotate = true;
controls.autoRotateSpeed = - .1;
controls.target.set( 0, .5, 0 );
controls.update();

// events

window.addEventListener( 'resize', onWindowResize );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

function animate() {

stats.update();

controls.update();

renderer.render( scene, camera );

}

</script>

</body>
</html>
10 changes: 2 additions & 8 deletions src/core/Clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Clock {
*/
start() {

this.startTime = now();
this.startTime = performance.now();

this.oldTime = this.startTime;
this.elapsedTime = 0;
Expand Down Expand Up @@ -110,7 +110,7 @@ class Clock {

if ( this.running ) {

const newTime = now();
const newTime = performance.now();

diff = ( newTime - this.oldTime ) / 1000;
this.oldTime = newTime;
Expand All @@ -125,10 +125,4 @@ class Clock {

}

function now() {

return performance.now();

}

export { Clock };