Skip to content
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

Add size and position as parameters to the PMREMGenerator #8

Closed
wants to merge 18 commits into from
Closed
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
Binary file modified examples/screenshots/misc_exporter_usdz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgl_loader_gltf_dispersion.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgpu_pmrem_scene.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 12 additions & 3 deletions examples/webgpu_pmrem_scene.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,42 +63,51 @@
scene.background = await loader.loadAsync( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );

let model;
let center = new THREE.Vector3( 0.5, 0, 0 );


model = new THREE.Mesh( new THREE.SphereGeometry( .2, 64, 64 ), new THREE.MeshBasicMaterial( { color: 0x0000ff } ) );
model.position.copy( center );
model.position.z -= 1;
scene.add( model );

model = new THREE.Mesh( new THREE.SphereGeometry( .2, 64, 64 ), new THREE.MeshBasicMaterial( { color: 0xff0000 } ) );
model.position.copy( center );
model.position.z += 1;
scene.add( model );

model = new THREE.Mesh( new THREE.SphereGeometry( .2, 64, 64 ), new THREE.MeshBasicMaterial( { color: 0xff00ff } ) );
model.position.copy( center );
model.position.x += 1;
scene.add( model );

model = new THREE.Mesh( new THREE.SphereGeometry( .2, 64, 64 ), new THREE.MeshBasicMaterial( { color: 0x00ffff } ) );
model.position.copy( center );
model.position.x -= 1;
scene.add( model );

model = new THREE.Mesh( new THREE.SphereGeometry( .2, 64, 64 ), new THREE.MeshBasicMaterial( { color: 0xffff00 } ) );
model.position.copy( center );
model.position.y -= 1;
scene.add( model );

model = new THREE.Mesh( new THREE.SphereGeometry( .2, 64, 64 ), new THREE.MeshBasicMaterial( { color: 0x00ff00 } ) );
model.position.copy( center );
model.position.y += 1;
scene.add( model );

//while ( scene.children.length > 0 ) scene.remove( scene.children[ 0 ] );

const sceneRT = new THREE.PMREMGenerator( renderer ).fromScene( scene );

const sceneRT = new THREE.PMREMGenerator( renderer ).fromScene( scene, 0, 0.1, 100, 512, center );

//

const pmremRoughness = uniform( .5 );
const pmremNode = pmremTexture( sceneRT.texture, normalWorld, pmremRoughness );

scene.add( new THREE.Mesh( new THREE.SphereGeometry( .5, 64, 64 ), new THREE.MeshBasicNodeMaterial( { colorNode: pmremNode } ) ) );
const mirror = new THREE.Mesh( new THREE.SphereGeometry( .5, 64, 64 ), new THREE.MeshBasicNodeMaterial( { colorNode: pmremNode } ) );
mirror.position.copy( center );
scene.add( mirror );

// gui

Expand Down
19 changes: 12 additions & 7 deletions src/extras/PMREMGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ class PMREMGenerator {
* @param {number} sigma
* @param {number} near
* @param {number} far
* @param {Number} size
* @param {Number} position
* @return {WebGLRenderTarget}
*/
fromScene( scene, sigma = 0, near = 0.1, far = 100 ) {
fromScene( scene, sigma = 0, near = 0.1, far = 100, size = 256, position = new Vector3( 0, 0, 0 ) ) {

_oldTarget = this._renderer.getRenderTarget();
_oldActiveCubeFace = this._renderer.getActiveCubeFace();
Expand All @@ -118,12 +120,12 @@ class PMREMGenerator {

this._renderer.xr.enabled = false;

this._setSize( 256 );
this._setSize( size );

const cubeUVRenderTarget = this._allocateTargets();
cubeUVRenderTarget.depthBuffer = true;

this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget );
this._sceneToCubeUV( scene, near, far, position, cubeUVRenderTarget );

if ( sigma > 0 ) {

Expand Down Expand Up @@ -320,7 +322,7 @@ class PMREMGenerator {

}

_sceneToCubeUV( scene, near, far, cubeUVRenderTarget ) {
_sceneToCubeUV( scene, near, far, position, cubeUVRenderTarget ) {

const fov = 90;
const aspect = 1;
Expand Down Expand Up @@ -372,17 +374,20 @@ class PMREMGenerator {
if ( col === 0 ) {

cubeCamera.up.set( 0, upSign[ i ], 0 );
cubeCamera.lookAt( forwardSign[ i ], 0, 0 );
cubeCamera.position.set( position.x, position.y, position.z );
cubeCamera.lookAt( position.x + forwardSign[ i ], position.y, position.z );

} else if ( col === 1 ) {

cubeCamera.up.set( 0, 0, upSign[ i ] );
cubeCamera.lookAt( 0, forwardSign[ i ], 0 );
cubeCamera.position.set( position.x, position.y, position.z );
cubeCamera.lookAt( position.x, position.y + forwardSign[ i ], position.z );

} else {

cubeCamera.up.set( 0, upSign[ i ], 0 );
cubeCamera.lookAt( 0, 0, forwardSign[ i ] );
cubeCamera.position.set( position.x, position.y, position.z );
cubeCamera.lookAt( position.x, position.y, position.z + forwardSign[ i ] );

}

Expand Down
19 changes: 12 additions & 7 deletions src/renderers/common/extras/PMREMGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ class PMREMGenerator {
* @param {Number} [sigma=0] - The blur radius in radians.
* @param {Number} [near=0.1] - The near plane distance.
* @param {Number} [far=100] - The far plane distance.
* @param {Number} [size=256] - The size (resolution) of the cube texture,
* @param {Number} [position = new Vector3( 0, 0, 0 )] - The cubeCamera position
* @param {RenderTarget?} [renderTarget=null] - The render target to use.
* @return {RenderTarget} The resulting PMREM.
* @see fromSceneAsync
*/
fromScene( scene, sigma = 0, near = 0.1, far = 100, renderTarget = null ) {
fromScene( scene, sigma = 0, near = 0.1, far = 100, size = 256, position = new Vector3( 0, 0, 0 ), renderTarget = null ) {

this._setSize( 256 );
this._setSize( size );

if ( this._hasInitialized === false ) {

Expand All @@ -162,7 +164,7 @@ class PMREMGenerator {
const cubeUVRenderTarget = renderTarget || this._allocateTargets();
cubeUVRenderTarget.depthBuffer = true;

this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget );
this._sceneToCubeUV( scene, near, far, position, cubeUVRenderTarget );

if ( sigma > 0 ) {

Expand Down Expand Up @@ -458,7 +460,7 @@ class PMREMGenerator {

}

_sceneToCubeUV( scene, near, far, cubeUVRenderTarget ) {
_sceneToCubeUV( scene, near, far, position, cubeUVRenderTarget ) {

const cubeCamera = _cubeCamera;
cubeCamera.near = near;
Expand Down Expand Up @@ -528,17 +530,20 @@ class PMREMGenerator {
if ( col === 0 ) {

cubeCamera.up.set( 0, upSign[ i ], 0 );
cubeCamera.lookAt( forwardSign[ i ], 0, 0 );
cubeCamera.position.set( position.x, position.y, position.z );
cubeCamera.lookAt( position.x + forwardSign[ i ], position.y, position.z );

} else if ( col === 1 ) {

cubeCamera.up.set( 0, 0, upSign[ i ] );
cubeCamera.lookAt( 0, forwardSign[ i ], 0 );
cubeCamera.position.set( position.x, position.y, position.z );
cubeCamera.lookAt( position.x, position.y + forwardSign[ i ], position.z );

} else {

cubeCamera.up.set( 0, upSign[ i ], 0 );
cubeCamera.lookAt( 0, 0, forwardSign[ i ] );
cubeCamera.position.set( position.x, position.y, position.z );
cubeCamera.lookAt( position.x, position.y, position.z + forwardSign[ i ] );

}

Expand Down
23 changes: 1 addition & 22 deletions src/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,7 @@ const wgslTypeLib = {

mat2: 'mat2x2<f32>',
mat3: 'mat3x3<f32>',
mat4: 'mat4x4<f32>',

//because arrays with their two degrees of freedom are special
array( elementType, count ) {

const isValidType = ( type ) => !! this[ type ];

if ( ! isValidType( elementType ) ) {

throw new Error( `Unknown type: ${elementType}` );

}

if ( typeof count !== 'number' || ! Number.isInteger( count ) || count < 1 ) {

throw new Error( `Invalid size: ${count}. Size must be a positive integer` );

}

return `array<${this[ elementType ]}, ${count}>`;

}
mat4: 'mat4x4<f32>'
};

const wgslCodeCache = {};
Expand Down
Loading