<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three.js Dynamic Ellipsoid Animation</title>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// 必要なThree.jsの要素を作成
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 楕円球の作成
const geometry = new THREE.SphereGeometry(1, 32, 32);
const material = new THREE.MeshPhongMaterial({ color: 0xffffff, shininess: 100 });
const ellipse = new THREE.Mesh(geometry, material);
scene.add(ellipse);
// ランダムな色を生成する関数
function getRandomColor() {
return Math.random() * 0xffffff;
}
// ライトを追加(カラフルな描画のために追加)
const light1 = new THREE.PointLight(0xffffff, 1);
light1.position.set(5, 5, 5);
scene.add(light1);
const light2 = new THREE.PointLight(0xffffff, 1);
light2.position.set(-5, -5, -5);
scene.add(light2);
// カメラの位置を変更してズーム
camera.position.z = 4;
// アニメーション用の変数
let time = 0;
// レンダリングループ
function animate() {
requestAnimationFrame(animate);
// サイン・コサイン関数でスケールを変化
const scaleX = Math.abs(Math.sin(time)) + 0.5; // 0.5〜1.5の範囲で変化
const scaleY = Math.abs(Math.cos(time + Math.PI / 4)) + 0.5; // 0.5〜1.5の範囲で変化
const scaleZ = Math.abs(Math.sin(time + Math.PI / 2)) + 0.5; // 0.5〜1.5の範囲で変化
ellipse.scale.set(scaleX, scaleY, scaleZ);
// 位置をサイン関数で変化
ellipse.position.x = Math.sin(time) * 2;
ellipse.position.y = Math.cos(time) * 2;
// 回転をサイン関数で変化
ellipse.rotation.x += 0.02;
ellipse.rotation.y += 0.01;
// カラフルにするためにランダムな色を適用
ellipse.material.color.setHex(getRandomColor());
// 光の強さをサイン関数で変化
light1.intensity = Math.abs(Math.sin(time)) + 0.5;
light2.intensity = Math.abs(Math.cos(time)) + 0.5;
// 時間を進める
time += 0.02;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>