|
21 | 21 |
|
22 | 22 | <script>
|
23 | 23 |
|
24 |
| - |
| 24 | + var camera, scene, renderer; |
| 25 | + var group; |
| 26 | + |
| 27 | + init(); |
| 28 | + animate(); |
25 | 29 |
|
26 | 30 | function init() {
|
27 | 31 |
|
28 |
| - camera = new THREE.PerspectiveCamera( |
29 |
| - 40, window.innerWidth / window.innerHeight, 1, 800 ); |
| 32 | + camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 800 ); |
30 | 33 |
|
31 | 34 | camera.position.set( -20, 10, 50 );
|
32 | 35 | camera.lookAt( new THREE.Vector3( 0, 0, 0) );
|
|
38 | 41 | var light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 1 );
|
39 | 42 | scene.add( light );
|
40 | 43 |
|
41 |
| - var clipPlanes = [ new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 ), |
42 |
| - new THREE.Plane( new THREE.Vector3( 0, -1, 0 ), 0 ), |
43 |
| - new THREE.Plane( new THREE.Vector3( 0, 0, -1 ), 0 ) ] |
| 44 | + var clipPlanes = [ |
| 45 | + new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 ), |
| 46 | + new THREE.Plane( new THREE.Vector3( 0, -1, 0 ), 0 ), |
| 47 | + new THREE.Plane( new THREE.Vector3( 0, 0, -1 ), 0 ) |
| 48 | + ]; |
44 | 49 |
|
45 | 50 | scene.add( new THREE.AmbientLight( 0x505050 ) );
|
46 | 51 |
|
47 |
| - balls = new THREE.Object3D(); |
| 52 | + group = new THREE.Object3D(); |
48 | 53 |
|
49 |
| - for ( var i = 1; i < 25; i++ ) { |
50 |
| - balls.add( new THREE.Mesh( |
51 |
| - new THREE.SphereBufferGeometry( i / 2, 48, 48), |
52 |
| - new THREE.MeshStandardMaterial( { color: new THREE.Color( Math.sin( i * 0.5 ) * 0.5 + 0.5, |
53 |
| - Math.cos( i * 1.5 ) * 0.5 + 0.5, |
54 |
| - Math.sin( i * 4.5 + 0 ) * 0.5 + 0.5 ), |
55 |
| - roughness: 0.95, metalness: 0.0, side: THREE.DoubleSide, clippingPlanes: clipPlanes, clipIntersection: true } ) |
56 |
| - ) ); |
57 |
| - } |
| 54 | + for ( var i = 1; i < 25; i ++ ) { |
| 55 | + |
| 56 | + var geometry = new THREE.SphereBufferGeometry( i / 2, 48, 48 ); |
| 57 | + var material = new THREE.MeshStandardMaterial( { |
| 58 | + color: new THREE.Color( Math.sin( i * 0.5 ) * 0.5 + 0.5, Math.cos( i * 1.5 ) * 0.5 + 0.5, Math.sin( i * 4.5 + 0 ) * 0.5 + 0.5 ), |
| 59 | + roughness: 0.95, |
| 60 | + metalness: 0.0, |
| 61 | + side: THREE.DoubleSide, |
| 62 | + clippingPlanes: clipPlanes, |
| 63 | + clipIntersection: true |
| 64 | + } ); |
| 65 | + group.add( new THREE.Mesh( geometry, material ) ); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + scene.add( group ); |
58 | 70 |
|
59 |
| - scene.add( balls ); |
60 |
| - |
61 | 71 | // Renderer
|
62 | 72 |
|
63 | 73 | var container = document.body;
|
|
72 | 82 | window.addEventListener( 'resize', onWindowResize, false );
|
73 | 83 | container.appendChild( renderer.domElement );
|
74 | 84 |
|
75 |
| - |
76 | 85 | // Stats
|
77 | 86 |
|
78 | 87 | stats = new Stats();
|
|
84 | 93 | mode.clipIntersection = true;
|
85 | 94 | mode.clipPosition = 0;
|
86 | 95 | var gui = new dat.GUI();
|
87 |
| - gui.width = 800; |
88 | 96 | gui.add( mode, 'clipIntersection' ).onChange( function() {
|
89 | 97 | for (var i = 0; i < balls.children.length; i++) {
|
90 | 98 | balls.children[i].material.clipIntersection = !balls.children[i].material.clipIntersection;
|
|
119 | 127 |
|
120 | 128 | function animate() {
|
121 | 129 |
|
122 |
| - |
123 | 130 | requestAnimationFrame( animate );
|
124 | 131 |
|
| 132 | + var children = group.children; |
125 | 133 |
|
126 |
| - for ( var obj = 0; obj < balls.children.length; obj++ ) { |
127 |
| - var current = balls.children[ obj ].material; |
128 |
| - for ( var i = 0; i < current.clippingPlanes.length; i++ ) { |
129 |
| - var plane = current.clippingPlanes[ i ]; |
| 134 | + for ( var i = 0; i < children.length; i ++ ) { |
| 135 | + |
| 136 | + var current = children[ i ].material; |
| 137 | + |
| 138 | + for ( var j = 0; j < current.clippingPlanes.length; j ++ ) { |
| 139 | + |
| 140 | + var plane = current.clippingPlanes[ j ]; |
130 | 141 | plane.constant = ( 49 * plane.constant + mode.clipPosition ) / 50;
|
| 142 | + |
131 | 143 | }
|
132 |
| - } |
133 | 144 |
|
| 145 | + } |
134 | 146 |
|
135 | 147 | stats.begin();
|
136 | 148 | renderer.render( scene, camera );
|
137 | 149 | stats.end();
|
138 | 150 |
|
139 | 151 | }
|
140 | 152 |
|
141 |
| - init(); |
142 |
| - animate(); |
143 |
| - |
144 | 153 | </script>
|
145 | 154 |
|
146 | 155 | </body>
|
|
0 commit comments