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
| import * as THREE from "three"; import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
let camera, scene, renderer, labelRenderer;
const clock = new THREE.Clock(); const textureLoader = new THREE.TextureLoader();
let moon; let earth; let curve;
init(); animate();
function init() { const EARTH_RADIUS = 1; const MOON_RADIUS = 0.27;
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 ); camera.position.set(0, 5, -10);
scene = new THREE.Scene();
const dirLight = new THREE.DirectionalLight(0xffffff); dirLight.position.set(0, 0, -1); scene.add(dirLight); const light = new THREE.AmbientLight(0xffffff, 0.5); scene.add(light);
const earthGeometry = new THREE.SphereGeometry(EARTH_RADIUS, 16, 16); const earthMaterial = new THREE.MeshPhongMaterial({ specular: 0x333333, shininess: 5, map: textureLoader.load("textures/planets/earth_atmos_2048.jpg"), specularMap: textureLoader.load("textures/planets/earth_specular_2048.jpg"), normalMap: textureLoader.load("textures/planets/earth_normal_2048.jpg"), normalScale: new THREE.Vector2(0.85, 0.85), });
earth = new THREE.Mesh(earthGeometry, earthMaterial); scene.add(earth);
const moonGeometry = new THREE.SphereGeometry(MOON_RADIUS, 16, 16); const moonMaterial = new THREE.MeshPhongMaterial({ shininess: 5, map: textureLoader.load("textures/planets/moon_1024.jpg"), }); moon = new THREE.Mesh(moonGeometry, moonMaterial); scene.add(moon);
curve = new THREE.CatmullRomCurve3( [ new THREE.Vector3(-10, 0, 10), new THREE.Vector3(-5, 5, 5), new THREE.Vector3(0, 0, 5), new THREE.Vector3(5, -5, 5), new THREE.Vector3(10, 0, 10), ], true );
const points = curve.getPoints(500); console.log(points); const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({ color: 0xff0000 });
const curveObject = new THREE.Line(geometry, material); scene.add(curveObject);
renderer = new THREE.WebGLRenderer(); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement); controls.minDistance = 5; controls.maxDistance = 100;
window.addEventListener("resize", onWindowResize); }
function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight); }
function animate() { requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime(); const time = elapsed / 10 % 1; const point = curve.getPoint(time); moon.position.copy(point); camera.position.copy(point); camera.lookAt(earth.position) renderer.render(scene, camera); }
|