-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
249 lines (167 loc) · 5.52 KB
/
index.js
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
import * as THREE from 'three';
import Stats from 'three/examples/jsm/libs/stats.module.js';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
// composer
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
import { SMAAPass } from 'three/examples/jsm/postprocessing/SMAAPass.js';
// non libary js
import { FOGPARAMS } from './src/const.js'
import { initGUI } from './src/GUI.js'
import LoadTextures from './src/LoadTextures.js'
import Mountain from './src/mountain/mountain.js'
import Snow from './src/snow/snow.js'
require('normalize.css/normalize.css');
require("./src/css/index.css");
//
// scene
let scene, camera, renderer, composer;
// general
let container, stats, clock, controls;
// objects
let mountain, snow, textures;
// lights
let hemiLight, directionalLight, spotLight;
//
window.onload = function () {
textures = new LoadTextures();
const promises = [textures.load()];
initScene();
initPostProcessing();
initFog();
initLights();
initControls();
initStats();
// ensure textures are fully loaded
Promise.all(promises).then(() => {
initObjects();
initGUI(scene, mountain, snow);
initEventListeners();
onWindowResize();
animate();
});
}
//
function initScene() {
scene = new THREE.Scene();
container = document.getElementById('canvas');
var width = container.offsetWidth;
var height = container.offsetHeight;
camera = new THREE.PerspectiveCamera(
75,
width / height,
0.1,
1000
);
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
}
//
function initPostProcessing() {
composer = new EffectComposer(renderer);
var renderPass = new RenderPass(scene, camera);
var smaaPass = new SMAAPass(window.innerWidth * renderer.getPixelRatio(), window.innerHeight * renderer.getPixelRatio());
renderPass.renderToScreen = false;
smaaPass.renderToScreen = true;
composer.addPass(renderPass);
composer.addPass(smaaPass);
}
//
function initLights() {
renderer.toneMapping = THREE.ReinhardToneMapping;
// renderer.toneMapping = THREE.ACESFilmicToneMapping; //decent with exposure of 0.3 darker
renderer.toneMappingExposure = 1.0;
renderer.shadowMap.enabled = true;
// hemiLight = new THREE.HemisphereLight(0xffeeb1, 0x080820, 4); //orange
hemiLight = new THREE.HemisphereLight(0x9db4ff, 0xedeeff, 4); // blue
scene.add(hemiLight);
spotLight = new THREE.SpotLight(0xffa95c, 4);
spotLight.castShadow = true;
spotLight.shadow.bias = -0.0001;
spotLight.shadow.mapSize = new THREE.Vector2(1024 * 2, 1024 * 2);
scene.add(spotLight);
directionalLight = new THREE.DirectionalLight( 0xffa95c, 2 );
directionalLight.castShadow = true;
directionalLight.shadow.bias = -0.0001;
directionalLight.shadow.mapSize = new THREE.Vector2(1024 * 2, 1024 * 2);
directionalLight.position.x = -0.5;
directionalLight.position.y = 0.5;
directionalLight.position.z = -1;
// scene.add(directionalLight);
}
function initFog() {
scene.background = new THREE.Color(FOGPARAMS.fogHorizonColor);
scene.fog = new THREE.FogExp2(FOGPARAMS.fogHorizonColor, FOGPARAMS.fogDensity);
}
//
function initStats() {
// var axesHelper = new THREE.AxesHelper(5);
// scene.add(axesHelper);
stats = new Stats();
document.body.appendChild(stats.dom);
clock = new THREE.Clock();
}
//
function initObjects() {
textures.rock.anisotropy = renderer.capabilities.getMaxAnisotropy();
mountain = new Mountain(textures);
scene.add(mountain);
snow = new Snow();
scene.add(snow)
}
//
function initControls() {
controls = new OrbitControls(camera, renderer.domElement);
controls.enablePan = true;
controls.enableZoom = true;
controls.enableRotate = true;
camera.position.set(-0.53, 1.6, -0.38);
controls.target = new THREE.Vector3(0.0, 1.15, 0.02);
}
//
function animate() {
requestAnimationFrame(animate);
let deltaTime = clock.getDelta();
stats.update();
spotLight.position.set(
camera.position.x + 1,
camera.position.y + 1,
camera.position.z + 1,
);
mountain.update(deltaTime);
snow.update(deltaTime);
// renderer.render(scene, camera);
composer.render(scene, camera);
controls.update();
}
//
// Event Listeners
function initEventListeners() {
window.addEventListener('resize', onWindowResize, false);
window.addEventListener('onmousemove', onMouseMove, false);
window.addEventListener('click', onClick, false);
}
//
function onWindowResize() {
container = document.getElementById('canvas');
var width = container.offsetWidth;
var height = container.offsetHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(width, height);
composer.setPixelRatio(window.devicePixelRatio);
composer.setSize(width, height);
}
//
function onMouseMove(e) {
//console.log();
}
//
function onClick(e) {
console.log("Camera Pos: " + camera.position.x + ", " + camera.position.y + ", " + camera.position.z)
console.log("Camera Looking: " + controls.target.x + ", " + controls.target.y + ", " + controls.target.z);
}
//