-
Notifications
You must be signed in to change notification settings - Fork 0
/
toys.js
234 lines (210 loc) · 6.7 KB
/
toys.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
'use strict';
const toys = {
data: {
renderer: {},
scene: {},
camera: {},
train: {}
},
init: function() {
this.settings();
this.ambient();
this.train();
this.renderScene();
},
settings: function() {
const canvas = document.querySelector('#c');
// Establecemos el motor para que renderice todo en el <canvas>
this.renderer = new THREE.WebGLRenderer({canvas});
this.renderer.shadowMap.enabled = true;
// parámetros de la cámara
const fov = 45; // campo de visión en grados
const aspect = window.innerWidth / window.innerHeight; // proporción de la escena
const near = 0.1; // punto más cercano del campo de visión
const far = 500; // punto más lejano del campo de visión
this.camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
this.camera.position.set(0, 10, 30); // (x, y, z)
// controles de la cámara para movernos por la escena
const controls = new THREE.OrbitControls(this.camera, canvas);
controls.target.set(0, 5, 0);
controls.update();
// Creamos la escena donde se va a renderizar todo
this.scene = new THREE.Scene();
},
renderScene: function() {
const renderer = this.renderer
const camera = this.camera
const scene = this.scene
// actualizamos el render con respecto al navegador
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
// refresco de render
function render() {
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
renderer.render(scene, camera);
// se utiliza para indicar al navegador que queremos animal algo
requestAnimationFrame(render);
}
requestAnimationFrame(render);
},
ambient: function() {
// color de fondo
this.scene.background = new THREE.Color('black');
// luz de ambiente
const skyColor = 0xddeeff;
const groundColor = 0x0f0e0d;
const intensity = 1;
const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
this.scene.add(light);
//suelo
const planeSize = 40;
// textura suelo
const loader = new THREE.TextureLoader();
const texture = loader.load('https://threejsfundamentals.org/threejs/resources/images/checker.png');
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.magFilter = THREE.NearestFilter;
const repeats = planeSize / 2;
texture.repeat.set(repeats, repeats);
// geometría del suelo
const planeGeo = new THREE.PlaneBufferGeometry(planeSize, planeSize);
// material del suelo
const planeMat = new THREE.MeshPhongMaterial({
map: texture,
side: THREE.DoubleSide,
});
// malla del suelo
const mesh = new THREE.Mesh(planeGeo, planeMat);
mesh.rotation.x = Math.PI * -.5;
this.scene.add(mesh);
},
train: function() {
this.train = new THREE.OBJLoader2();
this.train.loadMtl('models/train.mtl', null, (materials) => {
this.train.setMaterials(materials);
this.train.castShadow = true;
this.train.load('models/train.obj', (event) => {
const root = event.detail.loaderRootNode;
this.scene.add(root);
});
});
const cubes = new THREE.OBJLoader2();
cubes.loadMtl('models/cubes.mtl', null, (materials) => {
cubes.setMaterials(materials);
cubes.castShadow = true;
cubes.load('models/cubes.obj', (event) => {
const root = event.detail.loaderRootNode;
this.scene.add(root);
});
});
},
headlights: function() {
const bulbMat = new THREE.MeshStandardMaterial( {
emissive: 0xffffee,
emissiveIntensity: 1,
color: 0x000000
} );
const bulbGeometry = new THREE.SphereBufferGeometry( 0.1, 16, 20, 10 );
const headlightsSettings = {
color: 0xffee88,
intensity: 2,
position: {
x: 0.8,
y: 1.1,
z: 1.3
},
target: {
position: {
x: 1,
y: 0,
z: 3
}
},
angle: 0.5,
distance: 15,
penumbra: 0.5,
castShadow: true
};
const headlights1 = new THREE.SpotLight(
headlightsSettings.color,
headlightsSettings.intensity,
headlightsSettings.distance,
headlightsSettings.angle,
headlightsSettings.penumbra
);
headlights1.name = 'headlights1'
headlights1.add( new THREE.Mesh( bulbGeometry, bulbMat ) );
headlights1.position.set(
headlightsSettings.position.x,
headlightsSettings.position.y,
headlightsSettings.position.z
);
headlights1.target.position.set(
headlightsSettings.target.position.x,
headlightsSettings.target.position.y,
headlightsSettings.target.position.z
);
headlights1.castShadow = headlightsSettings.castShadow;
const helper1 = new THREE.SpotLightHelper(headlights1);
// scene.add(helper1);
const headlights2 = new THREE.SpotLight(
headlightsSettings.color,
headlightsSettings.intensity,
headlightsSettings.distance,
headlightsSettings.angle,
headlightsSettings.penumbra
);
headlights2.name = 'headlights2'
headlights2.add( new THREE.Mesh( bulbGeometry, bulbMat ) );
headlights2.position.set(
-headlightsSettings.position.x,
headlightsSettings.position.y,
headlightsSettings.position.z
);
headlights2.target.position.set(
-headlightsSettings.target.position.x,
headlightsSettings.target.position.y,
headlightsSettings.target.position.z
);
headlights2.castShadow = headlightsSettings.castShadow;
const helper2 = new THREE.SpotLightHelper(headlights2);
// scene.add(helper2);
this.scene.add( headlights1 );
this.scene.add( headlights2 );
function updateLight() {
headlights1.target.updateMatrixWorld();
helper1.update();
headlights2.target.updateMatrixWorld();
helper2.update();
}
updateLight();
// helperLights(headlights1, updateLight());
},
deleteObj(name) {
let selectedObj = this.scene.getObjectByName(name);
this.scene.remove(selectedObj);
this.renderScene();
},
actions: function() {
const lights = document.querySelector('#lightsCheckbox').checked;
if (lights) {
this.headlights();
} else {
this.deleteObj('headlights1');
this.deleteObj('headlights2');
}
}
}
toys.init();