-
Notifications
You must be signed in to change notification settings - Fork 0
/
foresta.html
executable file
·297 lines (217 loc) · 8.73 KB
/
foresta.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html>
<head>
<title>Exercise 02</title>
<style>
body{
margin: 0;
overflow: hidden;
}
#stats { /* Align stats top-left */
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- JavaScript libraries -->
<script src="/Users/andreadodero/threeJs/build/three.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="/Users/andreadodero/threejs-crumbs/examples/assets/libs/TrackballControls.js"></script>
<!-- Javascript code that runs our Three.js examples -->
<script>
// once everything is loaded, we run our Three.js stuff.
$(function () {
var stats = initStats();
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
var trackballControls = new THREE.TrackballControls(camera);
renderer.setClearColor(new THREE.Color(0xEEEEEE,1.0));
renderer.setSize(window.innerWidth,window.innerHeight);
renderer.shadowMapEnabled = true;
var planeGeometry = new THREE.PlaneGeometry(200,200);
var planeMaterial = new THREE.MeshLambertMaterial({color: 0x008000, side:THREE.DoubleSide});
var plane = new THREE.Mesh(planeGeometry,planeMaterial);
plane.rotation.x = -0.5*Math.PI;
plane.position.set(0,0,0);
plane.receiveShadow = true;
scene.add(plane);
// position and point the camera to the center of the scene
//camera.rotation.x= Math.PI*-0.5;
camera.position.set(0,50,100);
camera.up = new THREE.Vector3(0,1,0);
camera.lookAt(scene.position);
// add subtle ambient lighting
var ambiColor = "#0c0c0c";
var ambientLight = new THREE.AmbientLight(ambiColor);
scene.add(ambientLight);
var pointColor = "#ff5808";
var directionalLight = new THREE.DirectionalLight(pointColor);
directionalLight.position.set(0, 100, 0);
directionalLight.castShadow = true;
directionalLight.shadowCameraNear = 10;
directionalLight.shadowCameraFar = 100;
directionalLight.shadowCameraLeft = -20;
directionalLight.shadowCameraRight = 20;
directionalLight.shadowCameraTop = 20;
directionalLight.shadowCameraBottom = -20;
directionalLight.intensity = 0.5;
directionalLight.shadowMapHeight = 1024;
directionalLight.shadowMapWidth = 1024;
scene.add(directionalLight);
// add a small sphere simulating the pointlight
var sphereLight = new THREE.SphereGeometry(0.2);
var sphereLightMaterial = new THREE.MeshBasicMaterial({color: 0xac6c25});
var sphereLightMesh = new THREE.Mesh(sphereLight, sphereLightMaterial);
sphereLightMesh.castShadow = true;
sphereLightMesh.position = new THREE.Vector3(0, -100, 3);
scene.add(sphereLightMesh);
var axisHelper = new THREE.AxisHelper(3);
scene.add(axisHelper);
var foresta = makeForest(10);
// add spotlight for the shadows
var spotLight2 = new THREE.SpotLight( 0xffffff );
spotLight2.position.set(10,-50,50);
spotLight2.intensity = 0.1;
spotLight2.castShadow = true;
foresta.add(spotLight2);
scene.add(foresta);
// add the output of the renderer to the html element
$('body').append(renderer.domElement);
// call the render function
//renderer.render(scene, camera);
// call the render function
var step = 0;
var controls = new function () {
this.positionX = 0.5;
this.positionZ = 0.5;
this.showAxis = true;
this.ambientColor = ambiColor;
this.pointColor = pointColor;
this.intensity = 2;
this.debug = false;
this.castShadow = true;
this.onlyShadow = false;
};
var gui = new dat.GUI();
gui.add(controls, 'positionX',0,1);
gui.add(controls, 'positionZ',0,1);
var axis_control = gui.add(controls, 'showAxis');
gui.addColor(controls, 'pointColor').onChange(function (e) {
directionalLight.color = new THREE.Color(e);
});
gui.add(controls, 'intensity', 0, 5).onChange(function (e) {
directionalLight.intensity = e;
});
gui.add(controls, 'debug').onChange(function (e) {
spotLight2.shadowCameraVisible = e;
});
gui.add(controls, 'castShadow').onChange(function (e) {
directionalLight.castShadow = e;
});
gui.add(controls, 'onlyShadow').onChange(function (e) {
directionalLight.onlyShadow = e;
});
gui.addColor(controls, 'ambientColor').onChange(function (e) {
ambientLight.color = new THREE.Color(e);
});
render();
var renderCamera = camera;
axis_control.onChange(function (value) {
axisHelper.visible = value;
});
function render() {
stats.update();
trackballControls.update();
step += 0.5;
sphereLightMesh.position.x = sphereLightMesh.position.x+30*Math.cos(step/3);
sphereLightMesh.position.y = sphereLightMesh.position.y+30*Math.sin(step/3);
sphereLightMesh.position.z = -8;
spotLight2.position = sphereLightMesh.position;
directionalLight.position = sphereLightMesh.position;
// render using requestAnimationFrame
requestAnimationFrame(render);
renderer.render(scene, camera);
// bounce the sphere up and down
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
$('body').append(stats.domElement);
return stats;
}
function genApple(chioma, n){
var i;
for(i=0;i<n;i++){
var lat = Math.floor((Math.random() * 360) + 1);
var lon = Math.floor((Math.random() * 360) + 1);
putApple(chioma, lat,lon);
}
}
function putApple(chioma, a,b){
var melaGeometry = new THREE.SphereGeometry(1,32,32);
var melaMaterial = new THREE.MeshLambertMaterial({color: 0xFF0000});
var mela = new THREE.Mesh(melaGeometry,melaMaterial);
mela.castShadow = true;
var x0=0;
var y0=25;
var z0=0;
var x = x0 + 10 * Math.sin(a)*Math.cos(b);
var y = y0 + 10 * Math.sin(a)*Math.sin(b);
var z = z0 + 10 * Math.cos(a);
mela.position.set(x,y,z);
chioma.add(mela);
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function makeThree(albero){
var chioma = new THREE.Object3D();
var geometry = new THREE.CylinderGeometry( 3, 3, 20, 32 );
var material = new THREE.MeshLambertMaterial( {color: 0x633D18} );
var cylinder = new THREE.Mesh( geometry, material );
cylinder.castShadow = true;
cylinder.position.set(0,10,0);
//scene.add( cylinder );
albero.add(cylinder);
var sphereGeometry = new THREE.SphereGeometry(10,32,32);
var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x458B00});
var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
sphere.castShadow = true;
// position the sphere
sphere.position.set(0,25,0);
genApple(chioma, 10);
// add the sphere to the scene
chioma.add(sphere);
albero.add(chioma);
albero.castShadow = true;
return albero;
}
function makeForest(n){
var i;
var forest = new THREE.Object3D();
for(i=0;i<n;i++){
var albero = new THREE.Object3D();
albero = makeThree(albero);
var x = getRandomInt(-100, 100);
var z= getRandomInt(-100, 100);
albero.position.x = x;
albero.position.z = z;
// add spotlight for the shadows
var spotLight = new THREE.SpotLight( 0xffffff );
spotLight.position.set(x-10,-50,z+50);
spotLight.intensity = 0.2;
spotLight.castShadow = true;
albero.add(spotLight);
forest.add(albero);
}
return forest;
}
});
</script>
</body>
</html>