-
Notifications
You must be signed in to change notification settings - Fork 3
/
example8.html
251 lines (148 loc) · 5.86 KB
/
example8.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<html>
<head>
<title>PixelBox Example 8</title>
<link rel="stylesheet" href="css/normalize.css"/>
<link rel="stylesheet" href="css/index.css"/>
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1"/>
<!-- THREE base includes -->
<script src="js/three.min.js"></script>
<!-- Cannon is required for physics -->
<script src="js/cannon.js"></script>
<!-- PixelBox include -->
<script src="js/pixelbox.js"></script>
<!-- OrbitControls from THREE's examples -->
<script src="js/OrbitControls.js"></script>
</head>
<body>
<a class="show-source" onclick="window.open('view-source:'+window.location.href.toString(),'_blank');">Source</a>
<span class="info">
<a href="https://github.com/kirilledelman/pixelbox" target="_blank">PixelBox</a>
by Kirill Edelman for <a href="http://threejs.org" target="_blank">three.js</a>
</span>
<div class="buttons">
Hold mouse button down to <a href="http://youtu.be/My2FRPA3Gf8" target="_blank">swing</a>.
</div>
<script>
/*
CANNON physics scene example.
*/
document.addEventListener( 'DOMContentLoaded', function() {
// init PixelBoxRenderer instance
if( !renderer.init() ){
var err = "Your browser doesn't support WebGL";
alert( err );
console.error( err );
return;
} else {
console.log( "WebGL initialized" );
}
// load scene definition using THREE.PixelBoxAssets
assets.loadAssets( {
scenes: [ 'assets/example8.scene' ],
// when done, display scene
done:function() {
// instantiate our ExampleScene
exampleScene = new ExampleScene();
// set it as current for renderer
renderer.setScene( exampleScene );
} // end done:function
} ); // end assets.loadAssets
} ); // end document on load
/*
ExampleScene extends THREE.PixelBoxScene
*/
function ExampleScene() {
// call superclass constructor
THREE.PixelBoxScene.call( this );
// this function will be assigned as collision callback for bricks
// collisionObject is CANNON.CollisionEquation instance
this.brickCollide = function ( otherObject, collisionObject ) {
if ( !this.body ) return; // can happen when removing objects mid-step
// if object overlap of the collision is high enough
if ( collisionObject.penetration < -0.7 ) {
// spawn a dust puff
var puff = exampleScene.instantiate( 'puff', {
position: this.getWorldPosition(),
rotation: new THREE.Euler( Math.random() * Math.PI, 0, 0 ),
scale: 1 + Math.random() * 0.5
});
// add to scene
exampleScene.add( puff );
// fade and remove puff after a second
puff.tweenFps = 3;
puff.tween( {
prop:'alpha',
to: 0,
duration: 1,
done: function(){ puff.remove(); }
} );
// change frame a random frame of 'broken' animation
if ( this.currentAnimation && this.currentAnimation.name != 'broken' ) {
this.gotoAndStop( 'broken', Math.floor( Math.random() * 3 ) );
// if this brick was already broken (and is one of the smaller bricks),
// let's turn this brick into a smaller fragment
} else if ( this.body.mass < 100 && Math.random() > 0.3 ) {
var fragment = exampleScene.instantiate( 'fragment', {
position: this.getWorldPosition(),
rotation: this.getWorldRotation(),
scale: this.getWorldScale()
} );
fragment.gotoAndStop( 'fragment', Math.floor( Math.random() * 3 ) );
exampleScene.add( fragment );
this.parent.remove( this );
}
}
};
// get scene definition from assets cache
var sceneDef = assets.get( 'example8' );
// populate, with callback
this.populateWith( sceneDef, {
// called after each object is created
initObject: function ( obj, def ) {
// all the bricks in the scene have .isBrick custom property set
if ( def.props && def.props.isBrick ) {
// set their collision callback to this function
obj.onCollideStart = this.brickCollide;
}
} // end init object
} );
// add controls for easy preview
// various controls classes can be found in three.js examples/js/controls folder
this.controls = new THREE.OrbitControls( this.camera );
// add mouse / touch events
function onPress( e ) {
exampleScene.mouseDown = true;
}
function onRelease( e ) {
exampleScene.mouseDown = false;
}
document.body.addEventListener( 'mousedown', onPress, false );
document.body.addEventListener( 'mouseup', onRelease, false );
document.body.addEventListener( 'touchstart', onPress, false );
document.body.addEventListener( 'touchend', onRelease, false );
// scene tick function will add momentum to the ball when mouse is down
this.tick = function ( delta ) {
if ( this.mouseDown ) {
// get ball velocity
var vel = new THREE.Vector3();
vel.copy( this.ball.body.velocity );
var velocityMagnitude = vel.length();
// nearly stationary
if ( velocityMagnitude <= 1 ) {
vel.x *= 2;
vel.z = 0.25 * (Math.random() - 0.5);
// rock
} else if( velocityMagnitude < 40 ){
vel.multiplyScalar( 1 + delta * 2 );
}
this.ball.body.velocity.copy ( vel );
}
};
}
// ExampleScene subclasses THREE.PixelBoxScene
ExampleScene.prototype = Object.create( THREE.PixelBoxScene.prototype );
ExampleScene.prototype.constructor = ExampleScene;
</script>
</body>
</html>