当传统情书还在纸墨间流淌爱意时,Three.js已让文字在三维空间中起舞。某开发者用300行代码实现的3D情书,粒子化的字母如星河流动,立体的玫瑰随心跳绽放,引发GitHub万人星标。本文将拆解这种数字浪漫的核心技术,从飘动的文字到交互式花瓣雨,揭示如何用WebGL打造令人屏息的情感表达新范式。
场景构建:打造爱的宇宙基本法则
Three.js的浪漫场景始于三大核心组件——场景(Scene)、相机(Camera)和渲染器(Renderer),它们定义了爱的视觉宇宙规则。
1. 初始化三维世界
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
</head>
<body>
<script>
// 创建场景容器
const scene = new THREE.Scene();
// 透视相机:模拟人眼视角
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 20);
// WebGL渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
</script>
</body>
</html>
通过THREE.Scene
构建情感空间,PerspectiveCamera
设定恋人视角,WebGLRenderer
将抽象情感具象化为像素。
2. 环境氛围设计
添加环境光与点光源,让文字如萤火般温柔:
// 月光般的环境光
const ambientLight = new THREE.AmbientLight(0xccffff, 0.3);
scene.add(ambientLight);
// 心形点光源
const heartLight = new THREE.PointLight(0xff3366, 1.5);
heartLight.position.set(0, 10, 0);
scene.add(heartLight);
通过光源位置与颜色的配合,营造出晨曦微光或午夜星空的氛围差异。
动态元素:让文字与模型诉说爱意
1. 飘动的3D文字
使用TextGeometry
生成可交互文字,结合正弦函数实现浮动效果:
const loader = newTHREE.FontLoader();
loader.load('fonts/gentilis_bold.typeface.json', (font) => {
const textGeo = newTHREE.TextGeometry('I Love You', {
font: font,
size: 3,
height: 0.2,
curveSegments: 12
});
const textMaterial = newTHREE.MeshPhongMaterial({ color: 0xff99cc });
const textMesh = newTHREE.Mesh(textGeo, textMaterial);
textMesh.position.set(-10, 0, 0);
scene.add(textMesh);
// 浮动动画
functionanimate() {
requestAnimationFrame(animate);
textMesh.position.y = Math.sin(Date.now() * 0.002) * 2;
renderer.render(scene, camera);
}
animate();
});
文字材质选择MeshPhongMaterial
以反射心形光源,位置波动模拟心跳节奏。
2. 粒子化字母雨
将文字分解为独立粒子,实现星河倾泻效果:
const particles = newTHREE.BufferGeometry();
const positions = [];
for (let i = 0; i < 1000; i++) {
positions.push(
Math.random() * 40 - 20,
Math.random() * 40 - 20,
Math.random() * 40 - 20
);
}
particles.setAttribute('position', newTHREE.Float32BufferAttribute(positions, 3));
const particleMaterial = newTHREE.PointsMaterial({
size: 0.2,
color: 0xff66b2,
transparent: true
});
const particleSystem = newTHREE.Points(particles, particleMaterial);
scene.add(particleSystem);
通过Points
对象将离散坐标转化为视觉粒子,营造浪漫的随机美学。
情感增强:光影与材质的化学反应
1. 渐变材质
使用ShaderMaterial
实现色彩流动:
// 顶点着色器
varyingvec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
// 片段着色器
uniformfloat time;
varyingvec2 vUv;
void main() {
vec3 color1 = vec3(0.9, 0.3, 0.6);
vec3 color2 = vec3(0.3, 0.6, 0.9);
float gradient = sin(vUv.x * 3.0 + time) * 0.5 + 0.5;
gl_FragColor = vec4(mix(color1, color2, gradient), 1.0);
}
在JavaScript中传递时间变量,使文字表面呈现呼吸般的色彩过渡。
2. 交互式花瓣
结合物理引擎实现点击生成花瓣:
import { GLTFLoader } from'three/examples/jsm/loaders/GLTFLoader';
const loader = newGLTFLoader();
loader.load('models/rose.glb', (gltf) => {
const rose = gltf.scene;
rose.scale.set(0.5, 0.5, 0.5);
document.addEventListener('click', (e) => {
const clone = rose.clone();
clone.position.set(
(e.clientX / window.innerWidth) * 40 - 20,
-(e.clientY / window.innerHeight) * 40 + 20,
0
);
scene.add(clone);
});
});
每次点击屏幕时克隆玫瑰模型,将用户交互转化为可视化的情感积累。
终极浪漫:多感官交互设计
1. 音频可视化
将背景音乐与粒子运动绑定:
const audioListener = newTHREE.AudioListener();
camera.add(audioListener);
const audio = newTHREE.Audio(audioListener);
const loader = newTHREE.AudioLoader();
loader.load('sounds/love.mp3', (buffer) => {
audio.setBuffer(buffer);
audio.play();
const analyser = newTHREE.AudioAnalyser(audio, 256);
functionanimate() {
const data = analyser.getFrequencyData();
particleSystem.geometry.attributes.position.array.forEach((_, i) => {
if (i % 3 === 1) {
particleSystem.geometry.attributes.position.array[i] = data[i % 64] * 0.1;
}
});
particleSystem.geometry.attributes.position.needsUpdate = true;
requestAnimationFrame(animate);
}
animate();
});
通过音频分析器驱动粒子高度,让音乐节奏可视化。
2. VR情书模式
启用WebXR实现沉浸式体验:
renderer.xr.enabled = true;
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
document.body.appendChild(
WEBXR.createButton(renderer, { requiredFeatures: ['local-floor'] })
);
佩戴VR设备后,用户可走进三维文字组成的爱情迷宫。
总结
Three.js的情书开发是一场理性与感性的共舞:几何体构建情感骨架,材质与光源渲染氛围温度,物理引擎赋予互动生命。从飘动的ASCII字符到粒子化的心跳光谱,每一行代码都是数字时代的诗意表达。当技术邂逅艺术,最冰冷的数学公式也能绽放最炽热的浪漫——这或许就是程序员的终极浪漫主义。