Skip to content

Commit 726f2bb

Browse files
committed
Examples: Improved webgl_instancing_*
1 parent 66c519c commit 726f2bb

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

examples/files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ var files = {
4747
"webgl_geometry_text_shapes",
4848
"webgl_geometry_text_stroke",
4949
"webgl_helpers",
50-
"webgl_instancing_suzanne",
5150
"webgl_instancing_raycast",
51+
"webgl_instancing_suzanne",
5252
"webgl_interactive_buffergeometry",
5353
"webgl_interactive_cubes",
5454
"webgl_interactive_cubes_gpu",

examples/webgl_instancing_raycast.html

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
var camera, scene, renderer, stats;
1919

20-
var mesh, geometry;
21-
var amount = parseInt( window.location.search.substr( 1 ) ) || 3;
20+
var mesh;
21+
var amount = parseInt( window.location.search.substr( 1 ) ) || 10;
2222
var count = Math.pow( amount, 3 );
23-
var object = new THREE.Object3D();
2423

25-
var intersection;
2624
var raycaster = new THREE.Raycaster();
2725
var mouse = new THREE.Vector2( 1, 1 );
2826

@@ -36,33 +34,40 @@
3634

3735
function init() {
3836

39-
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
37+
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
4038
camera.position.set( amount, amount, amount );
4139
camera.lookAt( 0, 0, 0 );
4240

4341
scene = new THREE.Scene();
4442

45-
geometry = new THREE.TorusKnotBufferGeometry( 0.5, 0.2, 16, 4, 2, 3 );
46-
geometry.scale( 0.5, 0.5, 0.5 );
43+
var light = new THREE.HemisphereLight( 0xffffff, 0x000088 );
44+
light.position.set( - 1, 1.5, 1 );
45+
scene.add( light );
4746

48-
var material = new THREE.MeshNormalMaterial( { flatShading: true } );
47+
var light = new THREE.HemisphereLight( 0xffffff, 0x880000, 0.5 );
48+
light.position.set( - 1, - 1.5, - 1 );
49+
scene.add( light );
50+
51+
var geometry = new THREE.SphereBufferGeometry( 0.5 );
52+
var material = new THREE.MeshPhongMaterial( { flatShading: true } );
4953

5054
mesh = new THREE.InstancedMesh( geometry, material, count );
5155

5256
var i = 0;
53-
var offset = amount / 2;
57+
var offset = ( amount - 1 ) / 2;
58+
59+
var transform = new THREE.Object3D();
5460

5561
for ( var x = 0; x < amount; x ++ ) {
5662

5763
for ( var y = 0; y < amount; y ++ ) {
5864

5965
for ( var z = 0; z < amount; z ++ ) {
6066

61-
object.position.set( offset - x, offset - y, offset - z );
62-
63-
object.updateMatrix();
67+
transform.position.set( offset - x, offset - y, offset - z );
68+
transform.updateMatrix();
6469

65-
mesh.setMatrixAt( i ++, object.matrix );
70+
mesh.setMatrixAt( i ++, transform.matrix );
6671

6772
}
6873

@@ -82,7 +87,7 @@
8287
renderer.setSize( window.innerWidth, window.innerHeight );
8388
document.body.appendChild( renderer.domElement );
8489

85-
var orbitControls = new OrbitControls( camera, renderer.domElement );
90+
new OrbitControls( camera, renderer.domElement );
8691

8792
stats = new Stats();
8893
document.body.appendChild( stats.dom );
@@ -122,7 +127,7 @@
122127

123128
raycaster.setFromCamera( mouse, camera );
124129

125-
intersection = raycaster.intersectObjects( scene.children );
130+
var intersection = raycaster.intersectObject( mesh );
126131

127132
if ( intersection.length > 0 ) {
128133

examples/webgl_instancing_suzanne.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
function init() {
2929

30-
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
30+
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
3131
camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
3232
camera.lookAt( 0, 0, 0 );
3333

@@ -102,7 +102,7 @@
102102
mesh.rotation.y = Math.sin( time / 2 );
103103

104104
var i = 0;
105-
var offset = amount / 2;
105+
var offset = ( amount - 1 ) / 2;
106106

107107
for ( var x = 0; x < amount; x ++ ) {
108108

0 commit comments

Comments
 (0)