|
| 1 | +import * as THREE from 'three' |
| 2 | +import { version } from '../helpers/constants' |
| 3 | + |
| 4 | +export type StarsProps = { |
| 5 | + /** The radius of the starfield (default 100) */ |
| 6 | + radius?: number |
| 7 | + /** The depth of the starfield (default 50) */ |
| 8 | + depth?: number |
| 9 | + /** The number of stars (default 5000) */ |
| 10 | + count?: number |
| 11 | + /** The factor by which to scale each star (default 4) */ |
| 12 | + factor?: number |
| 13 | + /** The saturation of the stars (default 0) */ |
| 14 | + saturation?: number |
| 15 | + /** Whether to fade the edge of each star (default false) */ |
| 16 | + fade?: boolean |
| 17 | + /** The speed of the stars pulsing (default 1) */ |
| 18 | + speed?: number |
| 19 | +} |
| 20 | + |
| 21 | +class StarfieldMaterial extends THREE.ShaderMaterial { |
| 22 | + constructor() { |
| 23 | + super({ |
| 24 | + uniforms: { time: { value: 0.0 }, fade: { value: 1.0 } }, |
| 25 | + vertexShader: /* glsl */ ` |
| 26 | + uniform float time; |
| 27 | + attribute float size; |
| 28 | + varying vec3 vColor; |
| 29 | + void main() { |
| 30 | + vColor = color; |
| 31 | + vec4 mvPosition = modelViewMatrix * vec4(position, 0.5); |
| 32 | + gl_PointSize = size * (30.0 / -mvPosition.z) * (3.0 + sin(time + 100.0)); |
| 33 | + gl_Position = projectionMatrix * mvPosition; |
| 34 | + }`, |
| 35 | + fragmentShader: /* glsl */ ` |
| 36 | + uniform sampler2D pointTexture; |
| 37 | + uniform float fade; |
| 38 | + varying vec3 vColor; |
| 39 | + void main() { |
| 40 | + float opacity = 1.0; |
| 41 | + if (fade == 1.0) { |
| 42 | + float d = distance(gl_PointCoord, vec2(0.5, 0.5)); |
| 43 | + opacity = 1.0 / (1.0 + exp(16.0 * (d - 0.25))); |
| 44 | + } |
| 45 | + gl_FragColor = vec4(vColor, opacity); |
| 46 | +
|
| 47 | + #include <tonemapping_fragment> |
| 48 | + #include <${version >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}> |
| 49 | + }`, |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +const genStar = (r: number) => { |
| 55 | + return new THREE.Vector3().setFromSpherical( |
| 56 | + new THREE.Spherical(r, Math.acos(1 - Math.random() * 2), Math.random() * 2 * Math.PI) |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +export class Stars extends THREE.Points { |
| 61 | + speed: number |
| 62 | + /** |
| 63 | + * Pulsing/Blinking shader-based starfield. |
| 64 | + */ |
| 65 | + constructor({ |
| 66 | + radius = 100, |
| 67 | + depth = 50, |
| 68 | + count = 5000, |
| 69 | + saturation = 0, |
| 70 | + factor = 4, |
| 71 | + fade = false, |
| 72 | + speed = 1, |
| 73 | + }: StarsProps = {}) { |
| 74 | + super(new THREE.BufferGeometry(), new StarfieldMaterial()) |
| 75 | + |
| 76 | + this.speed = speed |
| 77 | + |
| 78 | + const material = this.material as StarfieldMaterial |
| 79 | + material.blending = THREE.AdditiveBlending |
| 80 | + material.uniforms.fade.value = fade |
| 81 | + material.depthWrite = false |
| 82 | + material.transparent = true |
| 83 | + material.vertexColors = true |
| 84 | + material.needsUpdate = true |
| 85 | + |
| 86 | + this.rebuildAttributes({ radius, depth, count, saturation, factor, fade }) |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Recreates the geometry buffers with the parameters. |
| 91 | + * Expensive operation - use for setup/configuration, not for animation. |
| 92 | + */ |
| 93 | + rebuildAttributes({ |
| 94 | + radius = 100, |
| 95 | + depth = 50, |
| 96 | + count = 5000, |
| 97 | + saturation = 0, |
| 98 | + factor = 4, |
| 99 | + fade = false, |
| 100 | + speed = 1, |
| 101 | + }: StarsProps) { |
| 102 | + this.speed = speed |
| 103 | + const material = this.material as StarfieldMaterial |
| 104 | + material.uniforms.fade.value = fade |
| 105 | + |
| 106 | + const positions: number[] = [] |
| 107 | + const colors: number[] = [] |
| 108 | + const sizes = Array.from({ length: count }, () => (0.5 + 0.5 * Math.random()) * factor) |
| 109 | + const color = new THREE.Color() |
| 110 | + let r = radius + depth |
| 111 | + const increment = depth / count |
| 112 | + for (let i = 0; i < count; i++) { |
| 113 | + r -= increment * Math.random() |
| 114 | + positions.push(...genStar(r).toArray()) |
| 115 | + color.setHSL(i / count, saturation, 0.9) |
| 116 | + colors.push(color.r, color.g, color.b) |
| 117 | + } |
| 118 | + |
| 119 | + this.geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3)) |
| 120 | + this.geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3)) |
| 121 | + this.geometry.setAttribute('size', new THREE.BufferAttribute(new Float32Array(sizes), 1)) |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Makes the stars pulse by updating its time uniform. |
| 126 | + * Call this in your animation loop. |
| 127 | + * @param elapsedTime Total elapsed time in seconds. |
| 128 | + */ |
| 129 | + update(elapsedTime: number): void { |
| 130 | + const material = this.material as StarfieldMaterial |
| 131 | + material.uniforms.time.value = elapsedTime * this.speed |
| 132 | + } |
| 133 | +} |
0 commit comments