-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the last commits. Resolved the conflict (again).
- Loading branch information
Showing
15 changed files
with
467 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<div id="smoke-bkg" class="fixed top-0 -z-10 h-full w-full"></div> | ||
|
||
<script> | ||
import * as THREE from "three" | ||
|
||
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)") | ||
|
||
if (!reducedMotion.matches) { | ||
const $bkg = document.getElementById("smoke-bkg") | ||
|
||
let w = window.innerWidth | ||
let h = window.innerHeight | ||
|
||
const THEME = { | ||
dark: { | ||
background: 0x666666, | ||
light: 0xffffff, | ||
opacity: 1, | ||
}, | ||
light: { | ||
background: 0xeeeeee, | ||
light: 0xffffff, | ||
opacity: 0.2, | ||
}, | ||
} as const | ||
|
||
const themePreference = window.getThemePreference() | ||
let currentTheme = THEME[themePreference] | ||
|
||
// inicializar Three.js | ||
// 3 cosas básicas: escena, cámara, renderizador | ||
|
||
// escena 🖼️ | ||
const scene = new THREE.Scene() | ||
|
||
// camara 📹 | ||
// 75 -> ángulo de visión | ||
const camera = new THREE.PerspectiveCamera(75, w / h, 1, 1000) | ||
camera.position.z = 10 | ||
scene.add(camera) | ||
|
||
// ▶️ renderizador | ||
const renderer = new THREE.WebGLRenderer() | ||
renderer.setSize(w, h) | ||
// color de fondo | ||
renderer.setClearColor(currentTheme.background, 1) | ||
|
||
$bkg?.appendChild(renderer.domElement) | ||
|
||
// añadir una luz directional | ||
const light = new THREE.DirectionalLight(currentTheme.light, 0.5) | ||
// posicion de la luz | ||
light.position.set(-1, 3, 1) | ||
scene.add(light) | ||
|
||
const smokeParticles: THREE.Mesh[] = [] | ||
|
||
const loader = new THREE.TextureLoader() | ||
loader.crossOrigin = "" // <- en localhost no pasa nada, pero si desplegamos a un servidor, puede ser necesario | ||
|
||
loader.load("/smoke.webp", (texture) => { | ||
// 1. geometria | ||
// crear un plano geométrico de 300x300 | ||
const smokeGeo = new THREE.PlaneGeometry(300, 300) | ||
|
||
// 2. material | ||
const smokeMaterial = new THREE.MeshLambertMaterial({ | ||
map: texture, | ||
transparent: true, | ||
opacity: currentTheme.opacity, | ||
}) | ||
|
||
const NUM_OF_PARTICLES = 300 | ||
for (let p = 0; p < NUM_OF_PARTICLES; p++) { | ||
// crear la malla con la geometria y el material | ||
const particle = new THREE.Mesh(smokeGeo, smokeMaterial) | ||
// posicionar aleatoriamente | ||
// en la x, y, z | ||
particle.position.set( | ||
Math.random() * 500 - 250, // X (de -250 a 250) | ||
Math.random() * 500 - 250, // Y (de -250 a 250) | ||
Math.random() * 1000 - 100 // Z (de -100 a 900) | ||
) | ||
// aleatoriamente la z | ||
particle.rotation.z = Math.random() * 360 | ||
// añadimos la particula en la escena | ||
scene.add(particle) | ||
// añadimos la particula al array | ||
smokeParticles.push(particle) | ||
} | ||
}) | ||
|
||
function resize() { | ||
h = window.innerHeight | ||
w = window.innerWidth | ||
camera.aspect = w / h | ||
camera.updateProjectionMatrix() // este metodo lo tenéis que ejecutar siempre que cambiais los parámetros de la cámara | ||
renderer.setSize(w, h) | ||
} | ||
|
||
function animate() { | ||
requestAnimationFrame(animate) | ||
|
||
smokeParticles.forEach((particle) => { | ||
particle.rotation.z += 0.001 | ||
}) | ||
|
||
renderer.render(scene, camera) | ||
} | ||
|
||
animate() | ||
|
||
// se va a disparar continuamente mientras hace el resize | ||
window.addEventListener("resize", resize) | ||
|
||
window.addEventListener("theme-changed", () => { | ||
const themePreference = window.getThemePreference() | ||
currentTheme = THEME[themePreference] | ||
light.color.setHex(currentTheme.light) | ||
renderer.setClearColor(currentTheme.background, 1) | ||
|
||
smokeParticles.forEach((particle) => { | ||
particle.material.opacity = currentTheme.opacity | ||
}) | ||
}) | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
/// <reference types="astro/client" /> | ||
|
||
interface Window { | ||
getThemePreference(): "dark" | "light" | ||
} |
Oops, something went wrong.