-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
185 lines (151 loc) · 4.86 KB
/
app.js
File metadata and controls
185 lines (151 loc) · 4.86 KB
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
// ------------------------------------------------
// BASIC SETUP
// ------------------------------------------------
const loader = new THREE.GLTF2Loader();
const models = [
"2CylinderEngine.glb",
"AntiqueCamera.glb",
"Avocado.glb",
"BarramundiFish.glb",
"BoomBox.glb",
"Buggy.glb",
"Corset.glb",
"DamagedHelmet.glb",
"DragonAttenuation.glb",
"Lantern.glb",
"MetalRoughSpheres.glb",
"MosquitoInAmber.glb",
"SpecGlossVsMetalRough.glb",
"WaterBottle.glb",
];
function nextModel() {
const model = models.shift();
models.push(model);
return models[0];
}
function addModel(scene, callback) {
loader.load(window.location + nextModel(), function (gltf) {
const box = new THREE.Box3().setFromObject(gltf.scene);
const sphere = box.getBoundingSphere();
const radius = sphere.radius;
const scale = 1000.0 / radius;
gltf.scene.position.x = Math.random() * 500;
gltf.scene.position.y = Math.random() * 500;
gltf.scene.position.z = Math.random() * 500;
gltf.scene.scale.x = scale;
gltf.scene.scale.y = scale;
gltf.scene.scale.z = scale;
scene.add(gltf.scene);
if (callback)
callback();
});
}
function estimateBytesUsed(geometry) {
var memory = 0;
for(const name in geometry.attributes) {
const attr = geometry.getAttribute(name);
memory += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
}
const indices = geometry.getIndex();
memory += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;
return memory;
}
function totalMemory(scene)
{
const guess = 2.5;
var memory = 0;
scene.traverse(function (e) {
if (e instanceof THREE.Mesh) {
memory += estimateBytesUsed(e.geometry);
}
});
return memory * guess;
}
function setupLighting(scene) {
// Add an ambient light
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); // Soft white light
scene.add(ambientLight);
// Add a directional light
const directionalLight = new THREE.DirectionalLight(0xffffff, 1); // Bright white light
directionalLight.position.set(5, 10, 7.5);
scene.add(directionalLight);
// Add a headlight (point light that follows the camera)
const headLight = new THREE.PointLight(0xffffff, 1, 100); // Bright white light
scene.add(headLight);
return headLight;
}
// Create an empty scene
var scene = new THREE.Scene();
// Create a basic perspective camera
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 5000);
camera.position.z = 1500;
// Create a renderer with Antialiasing
var renderer = new THREE.WebGLRenderer({ antialias: true });
// Configure renderer clear color
renderer.setClearColor("#000000");
// Configure renderer size
renderer.setSize(window.innerWidth, window.innerHeight);
// Append Renderer to DOM
document.body.appendChild(renderer.domElement);
// ------------------------------------------------
// FUN STARTS HERE
// ------------------------------------------------
const headLight = setupLighting(scene);
// createCube(scene)
addModel(scene, null);
addModel(scene, null);
addModel(scene, null);
import GUI from './lil-gui.esm.js';
const gui = new GUI();
const myObject = {
modelAdd: function () {
},
modelCount: 0,
memoryUsed: 0
};
const modelAdd = gui.add(myObject, 'modelAdd');
const modelCount = gui.add(myObject, 'modelCount');
const memoryUsed = gui.add(myObject, 'modelCount');
modelAdd.name("Add model");
modelAdd.setValue(function(){
addModel(scene, function(){
var count = 0;
scene.traverse(function (e) {
if (e instanceof THREE.Scene) {
count++;
}
});
modelCount.setValue(count);
if (self && self.performance && self.performance.memory)
memoryUsed.setValue(Math.ceil(performance.memory.usedJSHeapSize / 1024 / 1024));
else
memoryUsed.setValue(Math.ceil(totalMemory(scene) / 1024 / 1024));
});
});
modelCount.name("Model count");
modelCount.disable();
modelCount.setValue(3);
memoryUsed.name("Total memory");
memoryUsed.disable();
if (self && self.performance && self.performance.memory)
memoryUsed.setValue(Math.ceil(performance.memory.usedJSHeapSize / 1024 / 1024));
else
memoryUsed.setValue(Math.ceil(totalMemory(scene) / 1024 / 1024));
const stats = Stats();
document.body.appendChild(stats.dom);
stats.showPanel(2);
// Render Loop
var render = function () {
requestAnimationFrame(render);
stats.update();
headLight.position.copy(camera.position);
scene.traverse(function (e) {
if (e instanceof THREE.Scene) {
e.rotation.x += 0.01
e.rotation.y += 0.01;
}
});
// Render the scene
renderer.render(scene, camera);
};
render();