-
Notifications
You must be signed in to change notification settings - Fork 3
/
example4.html
188 lines (108 loc) · 4.64 KB
/
example4.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
<html>
<head>
<title>PixelBox Example 4</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.js physics -->
<script src="js/cannon.min.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">
Click to PEW
</div>
<script>
/*
This example shows how create instances of a template,
and how to dispose / recycle an object.
*/
document.addEventListener( 'DOMContentLoaded', function() {
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/example4.scene' ],
// when done, display scene
done: function() {
// instantiate our ExampleScene
exampleScene = new ExampleScene();
// set it as current for renderer
renderer.setScene( exampleScene );
// click will spawn an instance of a template
document.addEventListener( 'mouseup', function() {
// create an instance of 'pew' template from sceneDef
var pew = exampleScene.instantiate( 'pew', {
position: exampleScene.camera.position
} );
// pick a random color
var color = new THREE.Color();
color.setHSL( Math.random(), 0.5, 0.5 );
// tint plasma ( part of 'pew' template )
pew.plasma.tint.copy( color );
// tint point light ( part of 'pew' template )
pew.pointlight.color.copy( color );
// add it to the scene
exampleScene.add( pew );
// set timer to recycle it after 4 sec
setTimeout( function() { exampleScene.recycle( pew ); }, 4000);
// get camera's forward direction -500 units along Z
var forwardDirection = exampleScene.camera.localToWorld( new THREE.Vector3( 0, 0, -500 ) ).sub( exampleScene.camera.position );
// set pew's body velocity to that direction
pew.body.velocity.copy( forwardDirection );
} ); // end onclick
} // end done:function
} ); // end assets.loadAssets
} ); // end document.ready
/*
ExampleScene
*/
function ExampleScene() {
// call parent constructor
THREE.PixelBoxScene.call( this );
// get scene definition from assets cache
var sceneDef = assets.get( 'example4' );
// populate this scene with the definition
this.populateWith( sceneDef );
// 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 asteroids from template
for ( var i = 0; i < 150; i ++ ) {
// specify position, rotation, scale in options param of instantiate
var asteroid = this.instantiate( 'asteroid', {
position: new THREE.Vector3( Math.random() * 600 - 300, Math.random() * 600 - 300, Math.random() * 600 - 300 ),
rotation: new THREE.Euler ( Math.random() * Math.PI * 2, Math.random() * Math.PI * 2, Math.random() * Math.PI * 2 )
} );
// set random frame
asteroid.frame = Math.floor( Math.random() * asteroid.totalFrames );
// set random angular velocity
// body is an instance of CANNON.Body - http://schteppe.github.io/cannon.js/docs/classes/Body.html
asteroid.body.angularVelocity.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
// add to scene
this.add( asteroid );
}
}
// ExampleScene subclasses THREE.PixelBoxScene
ExampleScene.prototype = Object.create( THREE.PixelBoxScene.prototype );
ExampleScene.prototype.constructor = ExampleScene;
</script>
</body>
</html>