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
| import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 30 );
const textureLoader = new THREE.TextureLoader(); const particlesTexture = textureLoader.load("./textures/particles/1.png");
camera.position.set(0, 0, 10); scene.add(camera);
const params = { count: 10000, size: 0.1, radius: 5, branch: 3, color: "#ff6030", rotateScale: 0.3, endColor: "#1b3984", };
let geometry = null; let material = null; let points = null; const centerColor = new THREE.Color(params.color); const endColor = new THREE.Color(params.endColor); const generateGalaxy = () => { geometry = new THREE.BufferGeometry(); const positions = new Float32Array(params.count * 3); const colors = new Float32Array(params.count * 3);
for (let i = 0; i < params.count; i++) { const branchAngel = (i % params.branch) * ((2 * Math.PI) / params.branch);
const distance = Math.random() * params.radius * Math.pow(Math.random(), 3); const current = i * 3;
const randomX = (Math.pow(Math.random() * 2 - 1, 3) * (params.radius - distance)) / 5; const randomY = (Math.pow(Math.random() * 2 - 1, 3) * (params.radius - distance)) / 5; const randomZ = (Math.pow(Math.random() * 2 - 1, 3) * (params.radius - distance)) / 5;
positions[current] = Math.cos(branchAngel + distance * params.rotateScale) * distance + randomX; positions[current + 1] = 0 + randomY; positions[current + 2] = Math.sin(branchAngel + distance * params.rotateScale) * distance + randomZ;
const mixColor = centerColor.clone(); mixColor.lerp(endColor, distance / params.radius);
colors[current] = mixColor.r; colors[current + 1] = mixColor.g; colors[current + 2] = mixColor.b; }
geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3)); geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
material = new THREE.PointsMaterial({ size: params.size, sizeAttenuation: true, depthWrite: false, blending: THREE.AdditiveBlending, map: particlesTexture, alphaMap: particlesTexture, transparent: true, vertexColors: true, });
points = new THREE.Points(geometry, material); scene.add(points); }; generateGalaxy();
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true; renderer.physicallyCorrectLights = true;
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
const axesHelper = new THREE.AxesHelper(5); scene.add(axesHelper);
const clock = new THREE.Clock();
function render() { let time = clock.getElapsedTime();
controls.update(); renderer.render(scene, camera); requestAnimationFrame(render); }
render();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(window.devicePixelRatio); });
|