你自己的更改(每次首先阅读)
在执行任何其他操作之前,请先读取 ~/.solnest/website-builder-stack/my-instructions.md(如果存在)并遵循其中的内容。该文件保存了用户自己对此工具工作方式的更改,且其优先级高于本文件中的默认设置。
当用户要求更改此工具为其工作方式(措辞、语气、主题、步骤、默认值、包含或排除的内容)时,请将更改以纯文本形式写入该文件,如果不存在则创建它,并向用户回读。切勿为此编辑此工具自己的文件。 此工具会自动更新并替换其自己的文件,因此在该文件中进行的更改会在下次更新时丢失。用户的 21st.dev 密钥文件(~/solnest/website-builder-stack/.env)、其登录信息、其网站项目文件夹(包括技能保存在其中的任何 design-system/ 文件夹),以及技能自己的设置在文件夹内安装的任何内容(例如 node_modules)同样不会被更新触及。
WebGL 3D 对象 是做什么的?
使用场景
- 英雄区、特性区块或产品展示需要一个醒目的 3D 对象时。
- 视觉应展示真实的几何体、光照、高光和边缘。
- 有面网格应在网页布局内浮动或轻微旋转。
- CSS 变换、SVG 幻觉或平面渐变不足以满足需求时。
规则
- 使用真实的 3D 几何体:
IcosahedronGeometry、DodecahedronGeometry、BoxGeometry、自定义BufferGeometry或 glTF 网格。 - 使用透视相机,使对象具有深度和比例感。
- 使用 PBR 材质:
MeshStandardMaterial或MeshPhysicalMaterial。 - 调整
metalness、roughness和emissive以匹配品牌氛围。 - 使用至少一个定向光加上环境光或半球光来照亮对象。
- 仅对变换进行动画:微妙的旋转、浮动或视差效果。
- 处理大小调整并在卸载时释放几何体/材质/渲染器。
HTML 和 CSS
<div class="webgl-object-shell">
<canvas class="webgl-object-canvas" data-webgl-3d-object></canvas>
</div>
.webgl-object-shell {
position: relative;
width: min(100%, 720px);
aspect-ratio: 1 / 1;
}
.webgl-object-canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
Three.js 对象配方
import * as THREE from "three";
function initWebGL3DObject(canvas, options = {}) {
if (!canvas) return () => {};
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
});
renderer.setClearColor(0x000000, 0);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, options.maxDpr || 1.75));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = options.exposure || 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
camera.position.set(0, 0.15, 5.2);
const geometry = new THREE.IcosahedronGeometry(options.radius || 1.35, options.detail || 1);
const material = new THREE.MeshStandardMaterial({
color: options.color || 0x8aa4ff,
metalness: options.metalness ?? 0.48,
roughness: options.roughness ?? 0.34,
emissive: options.emissive || 0x101833,
emissiveIntensity: options.emissiveIntensity ?? 0.22,
flatShading: true,
});
const object = new THREE.Mesh(geometry, material);
object.castShadow = true;
object.receiveShadow = true;
scene.add(object);
const ambient = new THREE.AmbientLight(0xffffff, 0.38);
scene.add(ambient);
const key = new THREE.DirectionalLight(0xffffff, 2.15);
key.position.set(3.4, 4.2, 4.8);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
scene.add(key);
const rim = new THREE.DirectionalLight(options.rimColor || 0x7dd3fc, 0.82);
rim.position.set(-4.2, 1.2, -2.8);
scene.add(rim);
const shadowPlane = new THREE.Mesh(
new THREE.PlaneGeometry(5.2, 5.2),
new THREE.ShadowMaterial({ opacity: 0.18 })
);
shadowPlane.position.set(0, -1.65, 0);
shadowPlane.rotation.x = -Math.PI / 2;
shadowPlane.receiveShadow = true;
scene.add(shadowPlane);
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let rafId = 0;
function resize() {
const width = Math.max(1, canvas.clientWidth);
const height = Math.max(1, canvas.clientHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, options.maxDpr || 1.75));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
function render(time = 0) {
const t = time * 0.001;
object.rotation.x = -0.16 + Math.sin(t * 0.45) * 0.06;
object.rotation.y = t * 0.28;
object.rotation.z = Math.sin(t * 0.32) * 0.08;
object.position.y = reduceMotion ? 0 : Math.sin(t * 0.8) * 0.08;
renderer.render(scene, camera);
if (!reduceMotion) rafId = requestAnimationFrame(render);
}
function handleResize() {
cancelAnimationFrame(rafId);
resize();
render();
}
resize();
render();
window.addEventListener("resize", handleResize);
return () => {
cancelAnimationFrame(rafId);
window.removeEventListener("resize", handleResize);
geometry.dispose();
material.dispose();
shadowPlane.geometry.dispose();
shadowPlane.material.dispose();
renderer.dispose();
};
}
const cleanupObject = initWebGL3DObject(
document.querySelector("[data-webgl-3d-object]"),
{
color: 0x8aa4ff,
rimColor: 0x7dd3fc,
metalness: 0.48,
roughness: 0.34,
emissive: 0x101833,
}
);
材质默认值
- 高级金属质感:
metalness: 0.45-0.7,roughness: 0.25-0.45。 - 柔和陶瓷质感:
metalness: 0.0-0.15,roughness: 0.38-0.62。 - 发光科技感对象:低
emissive值,配合emissiveIntensity: 0.12-0.35。 - 有面对象:设置
flatShading: true;光滑产品对象:设置为false。
光照默认值
- 主光:定向光,高前侧角度,最强光源。
- 环境补光:低强度,使阴影保持可见。
- 边缘光:品牌色调或冷色光,从后方照亮边缘。
- 阴影:仅在对象需要落地深度时启用;保持贴图尺寸适中。
动画默认值
- 旋转:缓慢、连续,且次要于页面内容。
- 浮动:Y 轴方向
0.04至0.12单位。 - 减少动效:渲染静态帧,或仅允许直接交互。
- 除非对象是主要交互元素,否则避免相机移动。
避免
- 使用 CSS 3D 变换假装是 WebGL。
- 在需要真实光照和深度时使用无光照材质。
- 使用带渐变的平面代替实际几何体。
- 使用强烈的泛光或粒子效果,掩盖物体形态。
- 在移动端使用高 DPR、巨大的阴影贴图或过多灯光。
- 让对象与前景文案或行动号召按钮形成竞争。
快速检查
- 对象具有可见的形态、边缘、高光和阴影。
- 材质使用了
metalness、roughness和可选的emissive。 - 定向光和环境光同时存在。
- 相机是透视相机,而非意外地使用正交相机。
- 大小调整不会拉伸对象。
- 几何体、材质、事件监听器、RAF 和渲染器均已被清理。