Skip to content

Commit

Permalink
fix camera helper
Browse files Browse the repository at this point in the history
  • Loading branch information
AlaricBaraou committed Dec 16, 2024
1 parent 724a8bf commit 9025950
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 13 deletions.
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
"webgpu_backdrop",
"webgpu_backdrop_area",
"webgpu_backdrop_water",
"webgpu_camera",
"webgpu_camera_logarithmicdepthbuffer",
"webgpu_clearcoat",
"webgpu_clipping",
Expand Down
Binary file added examples/screenshots/webgpu_camera.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
268 changes: 268 additions & 0 deletions examples/webgpu_camera.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - cameras</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">
<style>
b {
color: lightgreen;
}
</style>
</head>
<body>
<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - cameras<br/>
<b>O</b> orthographic <b>P</b> perspective
</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 { color } from 'three/tsl';

let SCREEN_WIDTH = window.innerWidth;
let SCREEN_HEIGHT = window.innerHeight;
let aspect = SCREEN_WIDTH / SCREEN_HEIGHT;

let container;
let camera, scene, renderer, mesh;
let cameraRig, activeCamera, activeHelper;
let cameraPerspective, cameraOrtho;
let cameraPerspectiveHelper, cameraOrthoHelper;
let backgroundNode;
const frustumSize = 600;

init();

function init() {

container = document.createElement( 'div' );
document.body.appendChild( container );

scene = new THREE.Scene();

//

camera = new THREE.PerspectiveCamera( 50, 0.5 * aspect, 1, 10000 );
camera.position.z = 2500;

cameraPerspective = new THREE.PerspectiveCamera( 50, 0.5 * aspect, 150, 1000 );

cameraPerspectiveHelper = new THREE.CameraHelper( cameraPerspective );
scene.add( cameraPerspectiveHelper );

//
cameraOrtho = new THREE.OrthographicCamera( 0.5 * frustumSize * aspect / - 2, 0.5 * frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 150, 1000 );

cameraOrthoHelper = new THREE.CameraHelper( cameraOrtho );
scene.add( cameraOrthoHelper );

//

activeCamera = cameraPerspective;
activeHelper = cameraPerspectiveHelper;


// counteract different front orientation of cameras vs rig

cameraOrtho.rotation.y = Math.PI;
cameraPerspective.rotation.y = Math.PI;

cameraRig = new THREE.Group();

cameraRig.add( cameraPerspective );
cameraRig.add( cameraOrtho );

scene.add( cameraRig );

//

mesh = new THREE.Mesh(
new THREE.SphereGeometry( 100, 16, 8 ),
new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } )
);
scene.add( mesh );

const mesh2 = new THREE.Mesh(
new THREE.SphereGeometry( 50, 16, 8 ),
new THREE.MeshBasicMaterial( { color: 0x00ff00, wireframe: true } )
);
mesh2.position.y = 150;
mesh.add( mesh2 );

const mesh3 = new THREE.Mesh(
new THREE.SphereGeometry( 5, 16, 8 ),
new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: true } )
);
mesh3.position.z = 150;
cameraRig.add( mesh3 );

//

const geometry = new THREE.BufferGeometry();
const vertices = [];

for ( let i = 0; i < 10000; i ++ ) {

vertices.push( THREE.MathUtils.randFloatSpread( 2000 ) ); // x
vertices.push( THREE.MathUtils.randFloatSpread( 2000 ) ); // y
vertices.push( THREE.MathUtils.randFloatSpread( 2000 ) ); // z

}

geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );

const particles = new THREE.Points( geometry, new THREE.PointsMaterial( { color: 0x888888 } ) );
scene.add( particles );

//

renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
renderer.setAnimationLoop( animate );
container.appendChild( renderer.domElement );

renderer.setScissorTest( true );
backgroundNode = color( 0x111111 );
renderer.setClearColor( 0x000000, 1 );

//

window.addEventListener( 'resize', onWindowResize );
document.addEventListener( 'keydown', onKeyDown );

}

//

function onKeyDown( event ) {

switch ( event.keyCode ) {

case 79: /*O*/

activeCamera = cameraOrtho;
activeHelper = cameraOrthoHelper;

break;

case 80: /*P*/

activeCamera = cameraPerspective;
activeHelper = cameraPerspectiveHelper;

break;

}

}

//

function onWindowResize() {

SCREEN_WIDTH = window.innerWidth;
SCREEN_HEIGHT = window.innerHeight;
aspect = SCREEN_WIDTH / SCREEN_HEIGHT;

renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );

camera.aspect = 0.5 * aspect;
camera.updateProjectionMatrix();

cameraPerspective.aspect = 0.5 * aspect;
cameraPerspective.updateProjectionMatrix();

cameraOrtho.left = - 0.5 * frustumSize * aspect / 2;
cameraOrtho.right = 0.5 * frustumSize * aspect / 2;
cameraOrtho.top = frustumSize / 2;
cameraOrtho.bottom = - frustumSize / 2;
cameraOrtho.updateProjectionMatrix();

}

//

function animate() {

render();

}


function render() {

const r = Date.now() * 0.0005;

mesh.position.x = 700 * Math.cos( r );
mesh.position.z = 700 * Math.sin( r );
mesh.position.y = 700 * Math.sin( r );

mesh.children[ 0 ].position.x = 70 * Math.cos( 2 * r );
mesh.children[ 0 ].position.z = 70 * Math.sin( r );

if ( activeCamera === cameraPerspective ) {

cameraPerspective.fov = 35 + 30 * Math.sin( 0.5 * r );
cameraPerspective.far = mesh.position.length();
cameraPerspective.updateProjectionMatrix();

cameraPerspectiveHelper.update();
cameraPerspectiveHelper.visible = true;

cameraOrthoHelper.visible = false;

} else {

cameraOrtho.far = mesh.position.length();
cameraOrtho.updateProjectionMatrix();

cameraOrthoHelper.update();
cameraOrthoHelper.visible = true;

cameraPerspectiveHelper.visible = false;

}

cameraRig.lookAt( mesh.position );

//

activeHelper.visible = false;

renderer.autoClear = true;

renderer.setScissor( 0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT );
renderer.setViewport( 0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT );
scene.backgroundNode = null;
renderer.render( scene, activeCamera );

//

activeHelper.visible = true;

renderer.setScissor( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT );
renderer.setViewport( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT );
renderer.autoClear = false;
scene.backgroundNode = backgroundNode;
renderer.render( scene, camera );

}

</script>

</body>
</html>
29 changes: 16 additions & 13 deletions src/helpers/CameraHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Color } from '../math/Color.js';
import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
import { WebGLCoordinateSystem } from '../constants.js';

const _vector = /*@__PURE__*/ new Vector3();
const _camera = /*@__PURE__*/ new Camera();
Expand Down Expand Up @@ -195,17 +196,19 @@ class CameraHelper extends LineSegments {

_camera.projectionMatrixInverse.copy( this.camera.projectionMatrixInverse );

// center / target
// Adjust z values based on coordinate system
const nearZ = this.camera.coordinateSystem === WebGLCoordinateSystem ? - 1 : 0;

setPoint( 'c', pointMap, geometry, _camera, 0, 0, - 1 );
// center / target
setPoint( 'c', pointMap, geometry, _camera, 0, 0, nearZ );
setPoint( 't', pointMap, geometry, _camera, 0, 0, 1 );

// near

setPoint( 'n1', pointMap, geometry, _camera, - w, - h, - 1 );
setPoint( 'n2', pointMap, geometry, _camera, w, - h, - 1 );
setPoint( 'n3', pointMap, geometry, _camera, - w, h, - 1 );
setPoint( 'n4', pointMap, geometry, _camera, w, h, - 1 );
setPoint( 'n1', pointMap, geometry, _camera, - w, - h, nearZ );
setPoint( 'n2', pointMap, geometry, _camera, w, - h, nearZ );
setPoint( 'n3', pointMap, geometry, _camera, - w, h, nearZ );
setPoint( 'n4', pointMap, geometry, _camera, w, h, nearZ );

// far

Expand All @@ -216,9 +219,9 @@ class CameraHelper extends LineSegments {

// up

setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, - 1 );
setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, - 1 );
setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, - 1 );
setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, nearZ );
setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, nearZ );
setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, nearZ );

// cross

Expand All @@ -227,10 +230,10 @@ class CameraHelper extends LineSegments {
setPoint( 'cf3', pointMap, geometry, _camera, 0, - h, 1 );
setPoint( 'cf4', pointMap, geometry, _camera, 0, h, 1 );

setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, - 1 );
setPoint( 'cn2', pointMap, geometry, _camera, w, 0, - 1 );
setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, - 1 );
setPoint( 'cn4', pointMap, geometry, _camera, 0, h, - 1 );
setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, nearZ );
setPoint( 'cn2', pointMap, geometry, _camera, w, 0, nearZ );
setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, nearZ );
setPoint( 'cn4', pointMap, geometry, _camera, 0, h, nearZ );

geometry.getAttribute( 'position' ).needsUpdate = true;

Expand Down

0 comments on commit 9025950

Please sign in to comment.