-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
269 lines (174 loc) · 5.54 KB
/
app.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// these need to be accessed inside more than one function so we'll declare them first
let container;
let camera;
let controls;
let renderer;
let scene;
let mesh;
let mesh2;
var boxes = [];
var numBoxes = 9;
var bigmat;
var rgbToHex = function (rgb) {
var hex = Number(rgb).toString(16);
if (hex.length < 2) {
hex = "0" + hex;
}
return hex;
};
var fullColorHex = function(r,g,b) {
var red = rgbToHex(r);
var green = rgbToHex(g);
var blue = rgbToHex(b);
return red+green+blue;
};
function init() {
container = document.querySelector( '#scene-container' );
scene = new THREE.Scene();
//scene.background = new THREE.Color( 0x8FBCD4 );
createCamera();
//createControls();
createLights();
createMeshes();
createRenderer();
renderer.setAnimationLoop( () => {
update();
render();
} );
}
function createCamera() {
camera = new THREE.PerspectiveCamera(
35, // FOV
container.clientWidth / container.clientHeight, // aspect
0.1, // near clipping plane
1000, // far clipping plane
);
camera.position.set( 51 ,-18, 40 )
camera.rotation.y += math.pi/4;
camera.rotation.z += math.pi/2;
}
function createControls() {
controls = new THREE.OrbitControls( camera, container );
}
function createLights() {
// Create a directional light
const light = new THREE.DirectionalLight( 0xffffff, 3.0 );
//0xff9317
const ambientLight = new THREE.HemisphereLight(
0xffffff, // bright sky color
0x000000, // dim ground color
3, // intensity
);
scene.add( ambientLight );
// move the light back and up a bit
light.position.set( 50, 10, 10 );
// remember to add the light to the scene
scene.add( light );
}
function createMeshes2() {
const geometry = new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 );
const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load( 'https://cdn.glitch.com/9cc367a0-18ab-4af4-a31d-4a9272e2439f%2FScreenshot%202019-10-17%20at%2012.52.09%20AM.png?v=1573336761732' );
texture.encoding = THREE.sRGBEncoding;
texture.anisotropy = 16;
const material = new THREE.MeshStandardMaterial( {
map: texture,
} );
mesh = new THREE.Mesh( geometry, material );
mesh2 = new THREE.Mesh( geometry, material );
scene.add( mesh );
scene.add( mesh2 );
}
abs = math.abs
cos = math.cos
sqrt = math.sqrt
function setBoxPosZ(box, step) {
let x = box.position.x;
let y = box.position.y;
let md = abs(x) + abs(y);
let d = sqrt(x*x + y*y);
box.position.z = x;
box.position.z = cos(x/2) + cos(y/2);
box.position.z = cos(d);
box.position.z = cos(d + step);
box.position.z = d;
}
function setBoxScaleZ(box, step) {
let x = box.position.x;
let y = box.position.y;
let md = abs(x) + abs(y);
let d = sqrt(x*x + y*y);
box.scale.z = (d);
box.scale.x = 1+d/20;
box.scale.y = 1+d/20;
//position.z = d;//cos(abs(x) - abs(y) + step);
}
function createMeshes(){
var geometry = new THREE.BoxGeometry( 1,1,1);
// var boxes = [];
var numBoxes = 9;
for (let x = 0; x <= numBoxes; x++) {
for (let y = 0; y <= numBoxes; y++) {
var material = new THREE.MeshStandardMaterial( );
var cube = new THREE.Mesh( geometry, material );
cube.position.x = x * 1.2;
cube.position.y = y * 1.2;
cube.position.z = 1
boxes.push(cube)
scene.add(cube);
}
}
// for (var box of boxes) {
// var x = box.position.x;
// var y = box.position.y;
// //setBoxPosZ(box,10)
// //setBoxScaleZ(box,10)
// var wpVector = new THREE.Vector3();
// //box.material.color.r = math.abs(box.getWorldPosition().x)/2;
// //box.material.color.g = math.abs(box.getWorldPosition().y)/2;
// box.material.color.r = math.abs(box.getWorldPosition(wpVector).z)/25;
// box.material.color.g = math.abs(box.getWorldPosition(wpVector).z)/50;
//}
}
function createRenderer() {
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( container.clientWidth, container.clientHeight );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.gammaFactor = 2.2;
renderer.gammaOutput = true;
renderer.physicallyCorrectLights = true;
container.appendChild( renderer.domElement );
}
// perform any updates to the scene, called once per frame
// avoid heavy computation here
function update() {
var len = bigmat._size[0];
for(let i=0;i<len;i++){
for(let j=0;j<len;j++){
box = boxes[i*len + j]
if(box.position.z-(bigmat._data[i][j]*10)>0.1){
box.position.z -= 0.1
box.material.color.r -= 0.1
}
else if(box.position.z-(bigmat._data[i][j]*10)<-0.1){
box.position.z += 0.1
box.material.color.r += 0.1
}
}
}
// increase the mesh's rotation each frame
}
function render() {
renderer.render( scene, camera );
}
function onWindowResize() {
// set the aspect ratio to match the new browser window aspect ratio
camera.aspect = container.clientWidth / container.clientHeight;
// update the camera's frustum
camera.updateProjectionMatrix();
// update the size of the renderer AND the canvas
renderer.setSize( container.clientWidth, container.clientHeight );
}
window.addEventListener( 'resize', onWindowResize );
// call the init function to set everything up
init();