Skip to content

Commit

Permalink
Reuse single renderer in webgl_multiple_* examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Jun 8, 2017
1 parent 3c2f707 commit f906eef
Show file tree
Hide file tree
Showing 3 changed files with 374 additions and 416 deletions.
244 changes: 118 additions & 126 deletions examples/webgl_multiple_canvases_circle.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
height: 100%;
}

a {
color: yellow;
}

#container {
width: 100%;
height: 700px;
Expand All @@ -27,6 +31,7 @@
perspective: 800px;
perspective-origin: 50% 225px;
}

#stage {
width: 100%;
height: 100%;
Expand All @@ -51,6 +56,7 @@

.ring {
position: absolute;
display: block;
height: 300px;
width: 200px;
text-align: center;
Expand Down Expand Up @@ -120,21 +126,20 @@
<div id="container">
<div id="stage">
<div id="shape" class="ring backfaces">
<div id="container1" class="ring r1"></div>
<div id="container2" class="ring r2"></div>
<div id="container3" class="ring r3"></div>
<div id="container4" class="ring r4"></div>
<div id="container5" class="ring r5"></div>
<canvas id="canvas1" class="ring r1"></canvas>
<canvas id="canvas2" class="ring r2"></canvas>
<canvas id="canvas3" class="ring r3"></canvas>
<canvas id="canvas4" class="ring r4"></canvas>
<canvas id="canvas5" class="ring r5"></canvas>
</div>
</div>
</div>

<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - multiple canvases - circle</div>
<div id="help" gstyle="display: none">
<div>
This example is shows how to setup multi-monitor displays like <a href="http://code.google.com/p/liquid-galaxy/">Google's Liquid Galaxy using three.js</a>.
Here 5 monitors are simulated using 3d css. WebGL is then rendered onto each one.<br/>
Note: 3d css support required! Use Chrome, Safari or Firefox 10
This example is shows how to setup multi-monitor displays like <a href="https://www.google.com/earth/explore/showcase/liquidgalaxy.html">Google's Liquid Galaxy</a>.
Here 5 monitors are simulated using 3d css. WebGL is then rendered onto each one.
</div>
</div>

Expand All @@ -146,194 +151,181 @@

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var apps = [];

init();
animate();

function init() {

function degToRad( d ) {

return d * Math.PI / 180;

}

var rot = degToRad( 30 );
var views = [];

var fudge = 0.45; // I don't know why this is needed :-(
var scene, renderer;

apps.push( new App( 'container1', rot * -2 * fudge ) );
apps.push( new App( 'container2', rot * -1 * fudge ) );
apps.push( new App( 'container3', rot * 0 * fudge ) );
apps.push( new App( 'container4', rot * 1 * fudge ) );
apps.push( new App( 'container5', rot * 2 * fudge ) );
var mouseX = 0, mouseY = 0;

}
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

function animate() {
init();
animate();

for ( var i = 0; i < apps.length; ++ i ) {
//

apps[ i ].animate();
function View( canvas, rotateY ) {

}
canvas.width = canvas.clientWidth * window.devicePixelRatio;
canvas.height = canvas.clientHeight * window.devicePixelRatio;

requestAnimationFrame( animate );
var context = canvas.getContext( '2d' );

}
var camera = new THREE.PerspectiveCamera( 20, canvas.clientWidth / canvas.clientHeight, 1, 20000 );
camera.rotation.y = rotateY;

function App( containerId, rotateY ) {
// Think of the virtual camera as a post with 5 cameras on it (even though those cameras happen to live in difference scenes)
// You need to move the post (ie, the virtualCamera) to move all 5 cameras together.

var container;
var virtualCamera = new THREE.Camera();
virtualCamera.add( camera );

var virtualCamera, camera, scene, renderer;
this.render = function () {

var mesh1, light;
virtualCamera.position.x = - mouseX * 4;
virtualCamera.position.y = - mouseY * 4;
virtualCamera.position.z = 1800;

var mouseX = 0, mouseY = 0;
var cameraZ = 1800;
virtualCamera.lookAt( scene.position );
virtualCamera.updateMatrixWorld( true );

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
renderer.render( scene, camera );

init();
context.drawImage( renderer.domElement, 0, 0 );

function init() {
};

container = document.getElementById( containerId );
}

camera = new THREE.PerspectiveCamera( 20, container.clientWidth / container.clientHeight, 1, 20000 );
camera.rotation.y = rotateY;
function init() {

// Think of the virtual camera as a post with 5 cameras on it (even though those cameras happen to live in difference scenes)
// You need to move the post (ie, the virtualCamera) to move all 5 cameras together.
var canvas1 = document.getElementById( 'canvas1' );
var canvas2 = document.getElementById( 'canvas2' );
var canvas3 = document.getElementById( 'canvas3' );
var canvas4 = document.getElementById( 'canvas4' );
var canvas5 = document.getElementById( 'canvas5' );

virtualCamera = new THREE.Camera();
virtualCamera.add( camera );
virtualCamera.position.z = cameraZ;
var fudge = 0.45; // I don't know why this is needed :-(
var rot = 30 * THREE.Math.DEG2RAD;

scene = new THREE.Scene();
views.push( new View( canvas1, rot * -2 * fudge ) );
views.push( new View( canvas2, rot * -1 * fudge ) );
views.push( new View( canvas3, rot * 0 * fudge ) );
views.push( new View( canvas4, rot * 1 * fudge ) );
views.push( new View( canvas5, rot * 2 * fudge ) );

scene.add( virtualCamera );
//

light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0, 0, 1 ).normalize();
scene.add( light );
scene = new THREE.Scene();

var noof_balls = 51;
var light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0, 0, 1 ).normalize();
scene.add( light );

// shadow
var noof_balls = 51;

var canvas = document.createElement( 'canvas' );
canvas.width = 128;
canvas.height = 128;
// shadow

var context = canvas.getContext( '2d' );
var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
var canvas = document.createElement( 'canvas' );
canvas.width = 128;
canvas.height = 128;

context.fillStyle = gradient;
context.fillRect( 0, 0, canvas.width, canvas.height );
var context = canvas.getContext( '2d' );
var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
gradient.addColorStop( 1, 'rgba(255,255,255,1)' );

var shadowTexture = new THREE.CanvasTexture( canvas );
context.fillStyle = gradient;
context.fillRect( 0, 0, canvas.width, canvas.height );

var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );
var shadowTexture = new THREE.CanvasTexture( canvas );

for ( var i = 0; i < noof_balls; i ++ ) { // create shadows
var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );

var mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
mesh.position.x = - ( noof_balls - 1 ) / 2 *400 + i * 400;
mesh.position.y = - 250;
mesh.rotation.x = - Math.PI / 2;
scene.add( mesh );
for ( var i = 0; i < noof_balls; i ++ ) { // create shadows

}
var mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
mesh.position.x = - ( noof_balls - 1 ) / 2 *400 + i * 400;
mesh.position.y = - 250;
mesh.rotation.x = - Math.PI / 2;
scene.add( mesh );

var faceIndices = [ 'a', 'b', 'c' ];
}

var color, f1, p, vertexIndex,
var faceIndices = [ 'a', 'b', 'c' ];

radius = 200,
geometry1 = new THREE.IcosahedronGeometry( radius, 1 );
var radius = 200,

for ( var i = 0; i < geometry1.faces.length; i ++ ) {
geometry1 = new THREE.IcosahedronGeometry( radius, 1 );

f1 = geometry1.faces[ i ];
for ( var i = 0; i < geometry1.faces.length; i ++ ) {

for( var j = 0; j < 3; j ++ ) {
var f1 = geometry1.faces[ i ];

vertexIndex = f1[ faceIndices[ j ] ];
for( var j = 0; j < 3; j ++ ) {

p = geometry1.vertices[ vertexIndex ];
var vertexIndex = f1[ faceIndices[ j ] ];

color = new THREE.Color( 0xffffff );
color.setHSL( ( p.y / radius + 1 ) / 2, 1.0, 0.5 );
var p = geometry1.vertices[ vertexIndex ];

f1.vertexColors[ j ] = color;
var color = new THREE.Color( 0xffffff );
color.setHSL( ( p.y / radius + 1 ) / 2, 1.0, 0.5 );

color = new THREE.Color( 0xffffff );
color.setHSL( 0.0, ( p.y / radius + 1 ) / 2, 0.5 );
f1.vertexColors[ j ] = color;

}
var color = new THREE.Color( 0xffffff );
color.setHSL( 0.0, ( p.y / radius + 1 ) / 2, 0.5 );

}

}

var materials = [

new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors, shininess: 0 } ),
new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )

];

for ( var i = 0; i < noof_balls; i ++ ) { // create balls
var materials = [

var mesh = THREE.SceneUtils.createMultiMaterialObject( geometry1, materials );
new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors, shininess: 0 } ),
new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )

mesh.position.x = - ( noof_balls - 1 ) / 2 *400 + i *400;
mesh.rotation.x = i * 0.5;
scene.add( mesh );
];

}
for ( var i = 0; i < noof_balls; i ++ ) { // create balls

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setClearColor( 0xffffff );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( container.clientWidth, container.clientHeight );
container.appendChild( renderer.domElement );
var mesh = THREE.SceneUtils.createMultiMaterialObject( geometry1, materials );

document.addEventListener( 'mousemove', onDocumentMouseMove, false );
mesh.position.x = - ( noof_balls - 1 ) / 2 *400 + i *400;
mesh.rotation.x = i * 0.5;
scene.add( mesh );

}

function onDocumentMouseMove ( event ) {
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setClearColor( 0xffffff );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( 200, 300 );

mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );

}
}

this.animate = function() {
function onDocumentMouseMove ( event ) {

render();
mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;

};

function render() {
}

virtualCamera.position.x = -mouseX * 4;
virtualCamera.position.y = -mouseY * 4;
virtualCamera.position.z = cameraZ;
function animate() {

virtualCamera.lookAt( scene.position );
for ( var i = 0; i < views.length; ++ i ) {

renderer.render( scene, camera );
views[ i ].render();

}

requestAnimationFrame( animate );

}

</script>
Expand Down
Loading

0 comments on commit f906eef

Please sign in to comment.