-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
269 lines (223 loc) · 9.34 KB
/
index.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!DOCTYPE html>
<script src='./node_modules/requirejs/require.js'></script>
<script src='./node_modules/three/three.min.js'></script>
<body style='margin: 0px; background-color: #bbbbbb; overflow: hidden;'><script>
require([ './package.require.js'
, './bower_components/threex.planets/package.require.js'
, './bower_components/threex.crates/package.require.js'
, './bower_components/threex.grassground/package.require.js'
, './bower_components/threex.sportballs/package.require.js'
, './bower_components/threex.daynight/package.require.js'
] , function(){
var renderer = new THREE.WebGLRenderer({
antialiasing : true
});
renderer.shadowMapEnabled = true
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var onRenderFcts= [];
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 10000)
camera.position.z = 30
//////////////////////////////////////////////////////////////////////////////////
// set 3 point lighting //
//////////////////////////////////////////////////////////////////////////////////
;(function(){
// add a ambient light
var light = new THREE.AmbientLight( 0x020202 )
scene.add( light )
// add a light front right
var light = new THREE.DirectionalLight('white', 1)
light.position.set(0.5, 0.5, 2).multiplyScalar(10)
scene.add( light )
light.castShadow = true
light.shadowCameraNear = 0.01
light.shadowCameraFar = 250
light.shadowCameraFov = 45
light.shadowCameraLeft = -20
light.shadowCameraRight = 20
light.shadowCameraTop = 30
light.shadowCameraBottom= -30
// light.shadowCameraVisible = true
light.shadowBias = 0.001
light.shadowDarkness = 0.6
light.shadowMapWidth = 2048
light.shadowMapHeight = 2048
// // add a light behind
// var light = new THREE.DirectionalLight('white', 0.75)
// light.position.set(-0.5, -0.5, -2)
})()
//////////////////////////////////////////////////////////////////////////////////
// oimo world //
//////////////////////////////////////////////////////////////////////////////////
var world = new OIMO.World(1/360, 2, 8*6)
setInterval(function(){
world.step()
}, 1000/60);
//////////////////////////////////////////////////////////////////////////////////
// add the grass ground //
//////////////////////////////////////////////////////////////////////////////////
;(function(){
// return
var geometry = new THREE.BoxGeometry(300,20,200);
var material = new THREEx.GrassGround({
repeatX : 30,
repeatY : 30,
}).material
// var material = new THREE.MeshNormalMaterial()
var mesh = new THREE.Mesh( geometry, material );
mesh.receiveShadow = true
mesh.position.y = -geometry.parameters.height/2
var stepBody = THREEx.Oimo.createBodyFromMesh(world, mesh, {
move: false
})
scene.add(mesh)
})()
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
;(function(){
// return
// build crate model
var crate = THREEx.Crates.createCrate2();
crate.receiveShadow = true
crate.castShadow = true
crate.scale.multiplyScalar(2)
for(var x =-5; x <= 5; x++ ){
for(var y = 0; y < 10; y++ ){
for(var z =-2; z <= 1; z++ ){
(function(){
var mesh = crate.clone()
scene.add( mesh )
mesh.position.x = (x)*crate.scale.x
mesh.position.y = (y+crate.geometry.parameters.height/2)*crate.scale.y
mesh.position.z = (z*crate.scale.z)*3 - 10
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
// create IOMO.Body from mesh
var body = THREEx.Oimo.createBodyFromMesh(world, mesh)
// add an updater for them
onRenderFcts.push(function(delta){
THREEx.Oimo.updateObject3dWithBody(mesh, body)
})
})()
}
}
}
})()
//////////////////////////////////////////////////////////////////////////////////
// soccer ball
//////////////////////////////////////////////////////////////////////////////////
;(function(){
// create mesh
// var mesh = THREEx.Planets.createEarth()
var mesh = THREEx.SportBalls.createFootball()
window.ball = mesh
mesh.receiveShadow = true
mesh.castShadow = true
mesh.scale.multiplyScalar(10)
mesh.position.z = 40
// mesh.position.z = 10
mesh.position.y = mesh.geometry.parameters.radius*mesh.scale.y
scene.add( mesh )
// create IOMO.Body from mesh
var body = THREEx.Oimo.createBodyFromMesh(world, mesh)
// add an updater for them
onRenderFcts.push(function(delta){
THREEx.Oimo.updateObject3dWithBody(mesh, body)
})
//
body.body.linearVelocity.x = 0
body.body.linearVelocity.y = 1
body.body.linearVelocity.z = -5
body.body.angularVelocity.x = -Math.PI*2
// body.body.angularVelocity.y = 0
// body.body.angularVelocity.z = 0
})()
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
var iomoStats = new THREEx.Oimo.Stats(world)
document.body.appendChild(iomoStats.domElement)
onRenderFcts.push(function(delta){
iomoStats.update()
})
//////////////////////////////////////////////////////////////////////////////////
// Camera Controls //
//////////////////////////////////////////////////////////////////////////////////
var mouse = {x : 0, y : 0}
document.addEventListener('mousemove', function(event){
mouse.x = (event.clientX / window.innerWidth ) - 0.5
mouse.y = (event.clientY / window.innerHeight) - 0.5
}, false)
onRenderFcts.push(function(delta, now){
camera.position.x += (mouse.x*10 - camera.position.x-10) * (delta*3)
camera.position.y += (mouse.y*5 - (camera.position.y-5)) * (delta*3)
camera.lookAt( new THREE.Vector3(0,5,0) )
})
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
var sunAngle = -2/6*Math.PI*2;
// var sunAngle = -3/6*Math.PI*2;
onRenderFcts.push(function(delta, now){
var dayDuration = 40 // nb seconds for a full day cycle
sunAngle += delta/dayDuration * Math.PI*2
})
//////////////////////////////////////////////////////////////////////////////////
// starfield //
//////////////////////////////////////////////////////////////////////////////////
var starField = new THREEx.DayNight.StarField()
scene.add(starField.object3d)
onRenderFcts.push(function(delta, now){
starField.update(sunAngle)
})
//////////////////////////////////////////////////////////////////////////////////
// sunSphere //
//////////////////////////////////////////////////////////////////////////////////
var sunSphere = new THREEx.DayNight.SunSphere()
scene.add( sunSphere.object3d )
onRenderFcts.push(function(delta, now){
sunSphere.update(sunAngle)
})
//////////////////////////////////////////////////////////////////////////////////
// directionalLight //
//////////////////////////////////////////////////////////////////////////////////
var sunLight = new THREEx.DayNight.SunLight()
scene.add( sunLight.object3d )
onRenderFcts.push(function(delta, now){
sunLight.update(sunAngle)
})
//////////////////////////////////////////////////////////////////////////////////
// skydom //
//////////////////////////////////////////////////////////////////////////////////
var skydom = new THREEx.DayNight.Skydom()
scene.add( skydom.object3d )
onRenderFcts.push(function(delta, now){
skydom.update(sunAngle)
})
//////////////////////////////////////////////////////////////////////////////////
// render the scene //
//////////////////////////////////////////////////////////////////////////////////
onRenderFcts.push(function(){
renderer.render( scene, camera );
})
//////////////////////////////////////////////////////////////////////////////////
// loop runner //
//////////////////////////////////////////////////////////////////////////////////
var lastTimeMsec= null
requestAnimationFrame(function animate(nowMsec){
// keep looping
requestAnimationFrame( animate );
// measure time
lastTimeMsec = lastTimeMsec || nowMsec-1000/60
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
onRenderFcts.forEach(function(onRenderFct){
onRenderFct(deltaMsec/1000, nowMsec/1000)
})
})
})
</script></body>