performance optimization for WebGL icosahedron
parent
ee9194da73
commit
f5c5ebbb66
|
@ -199,7 +199,7 @@ window.onload = function() {
|
||||||
const length = direction.length();
|
const length = direction.length();
|
||||||
|
|
||||||
// Create cylinder
|
// Create cylinder
|
||||||
const geometry = new THREE.CylinderGeometry(radius, radius, length*lengthmultiplier, 5, 1);
|
const geometry = new THREE.CylinderGeometry(radius, radius, length*lengthmultiplier, 3, 1);
|
||||||
|
|
||||||
// By default, cylinder is along Y-axis, so rotate it
|
// By default, cylinder is along Y-axis, so rotate it
|
||||||
geometry.rotateX(Math.PI / 2);
|
geometry.rotateX(Math.PI / 2);
|
||||||
|
@ -222,20 +222,30 @@ window.onload = function() {
|
||||||
// Animation state
|
// Animation state
|
||||||
let animating = true;
|
let animating = true;
|
||||||
|
|
||||||
|
let lastFrameTime = 0;
|
||||||
|
const targetFPS = 20;
|
||||||
|
const animMultiplier = 30/targetFPS;
|
||||||
|
const frameDuration = 1000 / targetFPS;
|
||||||
|
|
||||||
// Animation function
|
// Animation function
|
||||||
function animate() {
|
function animate(now) {
|
||||||
requestAnimationFrame(animate);
|
requestAnimationFrame(animate);
|
||||||
|
|
||||||
// Rotate if animation is enabled
|
const delta = now - lastFrameTime;
|
||||||
if (animating) {
|
if (delta < frameDuration) return;
|
||||||
icosahedronGroup.rotation.x -= 0.002;
|
|
||||||
icosahedronGroup.rotation.y += 0.004;
|
lastFrameTime = now;
|
||||||
icosahedronGroup.rotation.z -= 0.001;
|
|
||||||
}
|
// Rotate if animation is enabled
|
||||||
|
if (animating) {
|
||||||
// Render
|
icosahedronGroup.rotation.x -= 0.002 * animMultiplier;
|
||||||
renderer.render(scene, camera);
|
icosahedronGroup.rotation.y += 0.004 * animMultiplier;
|
||||||
|
icosahedronGroup.rotation.z -= 0.001 * animMultiplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
renderer.render(scene, camera);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Set up controls
|
// Set up controls
|
||||||
const toggleAnimationBtn = document.getElementById('toggle-animation');
|
const toggleAnimationBtn = document.getElementById('toggle-animation');
|
||||||
|
|
Loading…
Reference in New Issue