-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
401 lines (321 loc) · 10.3 KB
/
core.js
File metadata and controls
401 lines (321 loc) · 10.3 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import * as THREE from 'three';
import {OrbitControls} from './rip/OrbitControls.js';
import {GLTFLoader} from './rip/GLTFLoader.js';
import {RGBELoader} from './rip/RGBELoader.js';
import Stats from './rip/stats.module.js';
export let camera, controls, scene, renderer, model, animations, mixer, orbitObject, stats;
var lastTime;
export let sceneFile;
export let playSpeed = 1;
export let playPaused = false;
export let skinTexture;
export let sceneReady = false;
let applicationReadyFlag = true;
export function setPlaySpeed(value) {
playSpeed = value;
}
export function togglePause() {
playPaused = !playPaused;
return playPaused;
}
export function applicationReady(value) {
applicationReadyFlag = value;
}
export function setPixelRatio(v) {
renderer.setPixelRatio(v);
}
const canvasPreview = document.createElement('canvas');
canvasPreview.width = 64;
canvasPreview.height = 64;
canvasPreview.style.display = 'none';
const previewCtx = canvasPreview.getContext('2d');
function isRegionAllBlack(ctx, x, y, width, height) {
const data = ctx.getImageData(x, y, width, height).data;
for (let i = 0; i < data.length; i += 4) {
if (data[i] !== 0 || data[i + 1] !== 0 || data[i + 2] !== 0 || data[i + 3] !== 255) {
return false;
}
}
return true;
}
function copyMirroredRegion(sx, sy, dx, dy, sw, sh) {
previewCtx.save()
previewCtx.translate(dx + sw, dy);
// Flip horizontally
previewCtx.scale(-1, 1);
// Draw the copied section of the image
previewCtx.drawImage(canvasPreview, sx, sy, sw, sh, 0, 0, sw, sh);
// Restore to normal drawing
previewCtx.restore();;
}
function normalizeSkinImage(image, callback) {
const isOldFormat = image.height === 32 && image.width === 64;
previewCtx.clearRect(0, 0, 64, 64);
previewCtx.drawImage(image, 0, 0);
if (isRegionAllBlack(previewCtx, 32, 0, 32, 16)) {
previewCtx.clearRect(32, 0, 32, 16);
}
if (isOldFormat) {
// convert old format
// legs
copyMirroredRegion(0, 16, 16, 48, 12, 16);
previewCtx.clearRect(16, 48, 4, 4);
copyMirroredRegion(8, 16, 24, 48, 4, 4);
copyMirroredRegion(12, 20, 28, 52, 4, 12);
// arms
copyMirroredRegion(40, 16, 32, 48, 12, 16);
previewCtx.clearRect(32, 48, 4, 4);
copyMirroredRegion(48, 16, 40, 48, 4, 4);
copyMirroredRegion(52, 20, 44, 52, 4, 12);
}
const newImage = new Image();
newImage.onload = () => callback(newImage);
newImage.src = canvasPreview.toDataURL();
}
export function toggleModel(modelType) {
if (!model) return;
model.traverse((child) => {
if (child.name === "body_slim") {
child.visible = modelType === "Slim";
} else if (child.name === "body_wide") {
child.visible = modelType === "Wide";
}
});
}
export async function loadUsernameSkin(username, manualSelect="auto") {
if (!username) return;
try {
// Step 1: Get UUID
const profileRes = await fetch(`https://corsproxy.io/?https://api.mojang.com/users/profiles/minecraft/${username}`);
if (!profileRes.ok) throw new Error("Invalid username");
const profile = await profileRes.json();
// Step 2: Get session profile and skin URL
const sessionRes = await fetch(`https://corsproxy.io/?https://sessionserver.mojang.com/session/minecraft/profile/${profile.id}`);
const sessionData = await sessionRes.json();
const textureProp = sessionData.properties.find(p => p.name === "textures");
const decoded = JSON.parse(atob(textureProp.value));
const skinUrl = decoded.textures.SKIN.url.replace('http:', 'https:');
// Step 3: Determine model type
let modelType = "Wide"; // default
if (manualSelect === "auto") {
modelType = decoded.textures.SKIN.metadata?.model === "slim" ? "Slim" : "Wide";
} else {
modelType = manualSelect === "slim" ? "Slim" : "Wide";
}
// Step 4: Load and normalize skin image
const skinRes = await fetch(skinUrl);
const blob = await skinRes.blob();
const url = URL.createObjectURL(blob);
const img = new Image();
img.onload = () => {
normalizeSkinImage(img, (normalized) => {
applyTextureFromImage(normalized);
toggleModel(modelType);
});
};
img.src = url;
} catch (err) {
alert("Failed to load skin: " + err.message);
}
}
function applyTextureFromImage(image) {
const tex = new THREE.Texture(image);
tex.magFilter = THREE.NearestFilter;
tex.minFilter = THREE.LinearFilter;
tex.generateMipmaps = false;
tex.flipY = false;
tex.colorSpace = THREE.SRGBColorSpace;
tex.needsUpdate = true;
skinTexture = tex;
applySkinTexture();
}
function applySkinTexture() {
if (!model || !skinTexture) return;
model.traverse((child) => {
if (child.isMesh && child.material) {
const materials = Array.isArray(child.material) ? child.material : [child.material];
materials.forEach((mat) => {
if (mat.name.endsWith("_skin")) {
mat.map = skinTexture;
mat.transparent = true;
mat.alphaTest = 0.5;
mat.needsUpdate = true;
} else {
mat.color = new THREE.Color(1, 0.5, 0.5)
mat.needsUpdate = true;
}
});
}
});
}
export function initialiseDefaultScene(c) {
const quality = localStorage.getItem('quality') || 'high';
let antialias = (quality === 'high');
let pixelRatio = (quality === 'low') ? 0.5 : window.devicePixelRatio;
renderer = new THREE.WebGLRenderer({ antialias: antialias });
renderer.setPixelRatio(pixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.toneMapping = THREE.LinearToneMapping;
renderer.toneMappingExposure = 1;
if (quality === 'low') {
renderer.shadowMap.enabled = false;
}
c.appendChild(renderer.domElement);
}
function loaderReady(gltf) {
// this magical placement of the function
animate();
// makes it all work
// all hail chatgpt debugging
// do not move it lest ye turn to stone
model = gltf.scene;
model.traverse((obj) => {
if (obj.isMesh) {
obj.renderOrder = 0; // Set render order
obj.material.depthWrite = true; // Enable depth writing for the material
obj.material.alphaTest = 0.05;
if (obj.material.map)
obj.material.map.minFilter = THREE.LinearFilter;
}
});
if (sceneFile.startsWith("plyaer"))
loadUsernameSkin("codedcells");
if (skinTexture) applySkinTexture();
var orbitObject = model.getObjectByName("@orbit");
if (orbitObject) {
var ambient = orbitObject.userData.ambientLight;
if (!ambient)
ambient = "404047";
var ambientIntensity = orbitObject.userData.ambientIntensity;
if (!ambientIntensity)
ambientIntensity = 0.8;
var ambientLightColor = new THREE.Color("#" + ambient);
var ambientLight = new THREE.AmbientLight(ambientLightColor, ambientIntensity);
scene.add(ambientLight);
if (orbitObject.userData.sky) {
var skyTextureName = "/cc3d/skies/" + orbitObject.userData.sky + ".jpg";
var textureLoader = new THREE.TextureLoader();
var skyTexture = textureLoader.load(skyTextureName);
skyTexture.colorSpace = THREE.SRGBColorSpace;
skyTexture.mapping = THREE.EquirectangularReflectionMapping;
scene.background = skyTexture;
}
orbitObject = orbitObject.position;
controls.target.set(orbitObject.x, orbitObject.y, orbitObject.z);
} else
controls.target.set(0, 1.6, 0);
controls.update();
animations = gltf.animations;
mixer = new THREE.AnimationMixer(model); // Assuming 'model' is your GLTF scene
animations.forEach((clip) => {
mixer.clipAction(clip).play();
});
mixer.update(0);
const gltfCamera = gltf.cameras[0];
camera.position.copy(gltfCamera.position);
camera.rotation.copy(gltfCamera.rotation);
camera.fov = gltfCamera.fov;
camera.updateProjectionMatrix();
// wait until the model can be added to the scene without blocking due to shader compilation
scene.add(model);
sceneReady = true;
}
export function loadScene(fn, path, hash) {
sceneReady = false;
if (!stats) {
stats = new Stats();
stats.domElement.style.left = null;
stats.domElement.style.right = "0";
document.body.appendChild(stats.dom);
}
if (hash === undefined)
hash = Math.random()
sceneFile = fn;
if (!camera)
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.25, 500);
if (scene)
scene.remove.apply(scene, scene.children);
scene = new THREE.Scene();
if (controls)
controls.dispose();
playSpeed = 1;
playPaused = false;
controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render); // use if there is no animation loop
controls.minDistance = 0;
controls.maxDistance = 30;
if (path === undefined)
path = '/cc3d/scenes/';
// model
const loader = new GLTFLoader().setPath(path);
loader.load(fn + ".glb?v=" + hash, loaderReady,
function(xhr) {
// This function will be called while the model is loading, you can use it for progress updates if needed
//console.log(xhr.loaded, xhr.total);
},
function(error) {
// Handle errors, including 404 Not Found
if (fn != 'spin')
loadScene("error");
if (error.status === 404)
console.error('Model not found: ' + modelUrl);
else
console.error('Error loading model:', error);
}
);
}
function animate(now) {
requestAnimationFrame(animate);
if (!lastTime) {
lastTime = now;
}
var elapsed = now - lastTime;
lastTime = now;
if (mixer && playSpeed > 0 && !playPaused)
mixer.update(elapsed / 1000 * playSpeed); // 'deltaTime' is the time difference between frames
render();
}
export function setQuality(quality) {
localStorage.setItem('quality', quality);
location.reload();
}
function showLowFpsWarning() {
const popup = document.createElement('div');
popup.style.position = 'absolute';
popup.style.top = '10px';
popup.style.right = '10px';
popup.style.padding = '10px';
popup.style.backgroundColor = 'rgba(0,0,0,0.8)';
popup.style.color = 'white';
popup.style.zIndex = 1000;
popup.innerHTML = `
Low performance detected.<br/>
<a href="#" id="reduceQuality">Click here to lower quality</a>
`;
document.body.appendChild(popup);
document.getElementById('reduceQuality').onclick = function(e) {
e.preventDefault();
setQuality('low')
};
}
let frameTimes = [];
let lowFpsWarningShown = false;
export function render() {
if (!camera) return;
sceneReady = true;
if (!applicationReadyFlag) return;
stats.begin();
renderer.render(scene, camera);
stats.end();
// Track framerate
const now = performance.now();
frameTimes.push(now);
while (frameTimes.length > 0 && frameTimes[0] < now - 2000) {
frameTimes.shift();
}
const fps = frameTimes.length / 2;
if (frameTimes.length > 60 && fps < 20 && !lowFpsWarningShown) {
showLowFpsWarning();
lowFpsWarningShown = true;
}
}