|
6 | 6 | <title>Rigid body - Physijs</title>
|
7 | 7 |
|
8 | 8 |
|
9 |
| - |
10 | 9 | <script type="text/javascript" src="../libs/three.js"></script>
|
11 | 10 | <script type="text/javascript" src="../libs/stats.js"></script>
|
12 | 11 | <script type="text/javascript" src="../libs/physi.js"></script>
|
13 | 12 | <script type="text/javascript" src="../libs/chroma.js"></script>
|
14 | 13 | <script type="text/javascript" src="../libs/dat.gui.js"></script>
|
15 | 14 |
|
16 | 15 |
|
17 |
| - |
18 | 16 | <script type="text/javascript">
|
19 | 17 |
|
20 | 18 | 'use strict';
|
|
28 | 26 | ground_material, box_material,
|
29 | 27 | projector, renderer, render_stats, physics_stats, scene, ground, light, camera, box, boxes = [];
|
30 | 28 |
|
31 |
| - initScene = function() { |
| 29 | + initScene = function () { |
32 | 30 | projector = new THREE.Projector;
|
33 | 31 |
|
34 | 32 | renderer = new THREE.WebGLRenderer({ antialias: true });
|
35 |
| - renderer.setSize( window.innerWidth, window.innerHeight ); |
| 33 | + renderer.setSize(window.innerWidth, window.innerHeight); |
36 | 34 |
|
37 | 35 | renderer.setClearColorHex(0x000000);
|
38 |
| - document.getElementById( 'viewport' ).appendChild( renderer.domElement ); |
| 36 | + document.getElementById('viewport').appendChild(renderer.domElement); |
39 | 37 |
|
40 | 38 | render_stats = new Stats();
|
41 | 39 | render_stats.domElement.style.position = 'absolute';
|
42 | 40 | render_stats.domElement.style.top = '1px';
|
43 | 41 | render_stats.domElement.style.zIndex = 100;
|
44 |
| - document.getElementById( 'viewport' ).appendChild( render_stats.domElement ); |
| 42 | + document.getElementById('viewport').appendChild(render_stats.domElement); |
45 | 43 |
|
46 | 44 | physics_stats = new Stats();
|
47 | 45 | physics_stats.domElement.style.position = 'absolute';
|
48 | 46 | physics_stats.domElement.style.top = '50px';
|
49 | 47 | physics_stats.domElement.style.zIndex = 100;
|
50 |
| - document.getElementById( 'viewport' ).appendChild( physics_stats.domElement ); |
| 48 | + document.getElementById('viewport').appendChild(physics_stats.domElement); |
51 | 49 |
|
52 | 50 | scene = new Physijs.Scene;
|
53 |
| - scene.setGravity(new THREE.Vector3( 0, -50, 0 )); |
| 51 | + scene.setGravity(new THREE.Vector3(0, -50, 0)); |
54 | 52 |
|
55 | 53 | camera = new THREE.PerspectiveCamera(
|
56 | 54 | 35,
|
57 | 55 | window.innerWidth / window.innerHeight,
|
58 | 56 | 1,
|
59 | 57 | 1000
|
60 | 58 | );
|
61 |
| - camera.position.set( 50, 30, 50 ); |
62 |
| - camera.lookAt( new THREE.Vector3(10,0,10) ); |
63 |
| - scene.add( camera ); |
| 59 | + camera.position.set(50, 30, 50); |
| 60 | + camera.lookAt(new THREE.Vector3(10, 0, 10)); |
| 61 | + scene.add(camera); |
64 | 62 |
|
65 | 63 | // Light
|
66 |
| - light = new THREE.SpotLight( 0xFFFFFF ); |
67 |
| - light.position.set( 20, 100, 50 ); |
| 64 | + light = new THREE.SpotLight(0xFFFFFF); |
| 65 | + light.position.set(20, 100, 50); |
68 | 66 |
|
69 | 67 |
|
70 |
| - scene.add( light ); |
| 68 | + scene.add(light); |
71 | 69 |
|
72 | 70 | createGround();
|
73 | 71 |
|
74 | 72 | var points = getPoints();
|
75 | 73 | var stones = [];
|
76 | 74 |
|
77 | 75 |
|
78 |
| - requestAnimationFrame( render ); |
| 76 | + requestAnimationFrame(render); |
79 | 77 |
|
80 |
| - var controls = new function() { |
| 78 | + var controls = new function () { |
81 | 79 | this.gravityX = 0;
|
82 | 80 | this.gravityY = -50;
|
83 | 81 | this.gravityZ = 0;
|
84 | 82 |
|
85 | 83 |
|
86 |
| - this.resetScene = function() { |
| 84 | + this.resetScene = function () { |
87 | 85 |
|
88 |
| - scene.setGravity(new THREE.Vector3(controls.gravityX,controls.gravityY,controls.gravityZ)); |
89 |
| - stones.forEach(function(st){ |
| 86 | + scene.setGravity(new THREE.Vector3(controls.gravityX, controls.gravityY, controls.gravityZ)); |
| 87 | + stones.forEach(function (st) { |
90 | 88 | scene.remove(st)
|
91 | 89 | });
|
92 |
| - stones=[]; |
| 90 | + stones = []; |
93 | 91 |
|
94 | 92 |
|
95 |
| - points.forEach(function(point) { |
96 |
| - var stoneGeom = new THREE.CubeGeometry(0.6,6,2); |
| 93 | + points.forEach(function (point) { |
| 94 | + var stoneGeom = new THREE.CubeGeometry(0.6, 6, 2); |
97 | 95 |
|
98 | 96 | var stone = new Physijs.BoxMesh(stoneGeom, Physijs.createMaterial(new THREE.MeshPhongMaterial(
|
99 | 97 | {
|
100 | 98 | color: scale(Math.random()).hex(),
|
101 |
| - transparent:true, opacity:0.8, |
| 99 | + transparent: true, opacity: 0.8, |
102 | 100 | // map: THREE.ImageUtils.loadTexture( '../assets/textures/general/darker_wood.jpg' )
|
103 | 101 | })));
|
104 |
| - stone.position=point.clone(); |
| 102 | + stone.position = point.clone(); |
105 | 103 | stone.lookAt(scene.position);
|
106 | 104 | stone.__dirtyRotation = true;
|
107 |
| - stone.position.y=3.5; |
| 105 | + stone.position.y = 3.5; |
108 | 106 |
|
109 | 107 | scene.add(stone);
|
110 | 108 | stones.push(stone);
|
111 | 109 |
|
112 | 110 | });
|
113 | 111 |
|
114 | 112 | // let the first one fall down
|
115 |
| - stones[0].rotation.x=0.2; |
| 113 | + stones[0].rotation.x = 0.2; |
116 | 114 | stones[0].__dirtyRotation = true;
|
117 | 115 |
|
118 | 116 | };
|
119 | 117 | }
|
120 | 118 |
|
121 | 119 | var gui = new dat.GUI();
|
122 |
| - gui.add(controls, 'gravityX',-100,100); |
123 |
| - gui.add(controls, 'gravityY',-100,100); |
124 |
| - gui.add(controls, 'gravityZ',-100,100); |
| 120 | + gui.add(controls, 'gravityX', -100, 100); |
| 121 | + gui.add(controls, 'gravityY', -100, 100); |
| 122 | + gui.add(controls, 'gravityZ', -100, 100); |
125 | 123 | gui.add(controls, 'resetScene');
|
126 | 124 |
|
127 | 125 | controls.resetScene();
|
128 | 126 | };
|
129 | 127 |
|
130 | 128 | var stepX;
|
131 | 129 |
|
132 |
| - render = function() { |
133 |
| - requestAnimationFrame( render ); |
134 |
| - renderer.render( scene, camera ); |
| 130 | + render = function () { |
| 131 | + requestAnimationFrame(render); |
| 132 | + renderer.render(scene, camera); |
135 | 133 | render_stats.update();
|
136 | 134 |
|
137 |
| - scene.simulate(undefined,1 ); |
| 135 | + scene.simulate(undefined, 1); |
138 | 136 |
|
139 | 137 |
|
140 | 138 | };
|
141 | 139 |
|
142 | 140 |
|
143 | 141 | function getPoints() {
|
144 | 142 | var points = [];
|
145 |
| - var r=27; |
| 143 | + var r = 27; |
146 | 144 | var cX = 0;
|
147 | 145 | var cY = 0;
|
148 | 146 |
|
149 | 147 | var circleOffset = 0;
|
150 |
| - for (var i = 0 ; i < 1000 ; i+=6+circleOffset) { |
| 148 | + for (var i = 0; i < 1000; i += 6 + circleOffset) { |
151 | 149 |
|
152 |
| - circleOffset = 4.5*(i/360); |
| 150 | + circleOffset = 4.5 * (i / 360); |
153 | 151 |
|
154 |
| - var x = (r/1440)*(1440-i)*Math.cos(i*(Math.PI/180)) + cX; |
155 |
| - var z = (r/1440)*(1440-i)*Math.sin(i*(Math.PI/180)) + cY; |
| 152 | + var x = (r / 1440) * (1440 - i) * Math.cos(i * (Math.PI / 180)) + cX; |
| 153 | + var z = (r / 1440) * (1440 - i) * Math.sin(i * (Math.PI / 180)) + cY; |
156 | 154 | var y = 0;
|
157 | 155 |
|
158 |
| - points.push(new THREE.Vector3(x,y,z)); |
| 156 | + points.push(new THREE.Vector3(x, y, z)); |
159 | 157 | }
|
160 | 158 |
|
161 | 159 | return points;
|
162 | 160 | }
|
163 | 161 |
|
164 | 162 | function createGround() {
|
165 | 163 | var ground_material = Physijs.createMaterial(
|
166 |
| - new THREE.MeshPhongMaterial({ map: THREE.ImageUtils.loadTexture( '../assets/textures/general/wood-2.jpg' ) }), |
| 164 | + new THREE.MeshPhongMaterial({ map: THREE.ImageUtils.loadTexture('../assets/textures/general/wood-2.jpg') }), |
167 | 165 | .9, .3);
|
168 | 166 |
|
169 | 167 | var ground = new Physijs.BoxMesh(new THREE.CubeGeometry(60, 1, 60), ground_material, 0);
|
170 | 168 |
|
171 | 169 | var borderLeft = new Physijs.BoxMesh(new THREE.CubeGeometry(2, 3, 60), ground_material, 0);
|
172 |
| - borderLeft.position.x=-31; |
173 |
| - borderLeft.position.y=2; |
| 170 | + borderLeft.position.x = -31; |
| 171 | + borderLeft.position.y = 2; |
174 | 172 | ground.add(borderLeft);
|
175 | 173 |
|
176 | 174 | var borderRight = new Physijs.BoxMesh(new THREE.CubeGeometry(2, 3, 60), ground_material, 0);
|
177 |
| - borderRight.position.x=31; |
178 |
| - borderRight.position.y=2; |
| 175 | + borderRight.position.x = 31; |
| 176 | + borderRight.position.y = 2; |
179 | 177 | ground.add(borderRight);
|
180 | 178 |
|
181 |
| - var borderBottom = new Physijs.BoxMesh(new THREE.CubeGeometry(64, 3, 2), ground_material,0); |
182 |
| - borderBottom.position.z=30; |
183 |
| - borderBottom.position.y=2; |
| 179 | + var borderBottom = new Physijs.BoxMesh(new THREE.CubeGeometry(64, 3, 2), ground_material, 0); |
| 180 | + borderBottom.position.z = 30; |
| 181 | + borderBottom.position.y = 2; |
184 | 182 | ground.add(borderBottom);
|
185 | 183 |
|
186 | 184 | var borderTop = new Physijs.BoxMesh(new THREE.CubeGeometry(64, 3, 2), ground_material, 0);
|
187 |
| - borderTop.position.z=-30; |
188 |
| - borderTop.position.y=2; |
| 185 | + borderTop.position.z = -30; |
| 186 | + borderTop.position.y = 2; |
189 | 187 | ground.add(borderTop);
|
190 | 188 |
|
191 | 189 | scene.add(ground);
|
|
0 commit comments