shaders
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseShaders
Shaders
Shaders is a WebGPU component library. One package, , with components that compose like layers in Figma. Everything below is free and MIT licensed. Pro, where it applies, is marked.
shadersShaders 是一个 WebGPU 组件库。只需一个 包,其组件的组合方式就像 Figma 中的图层。以下所有内容均免费,并采用 MIT 许可证。Pro 功能(如有)都会明确标注。
shadersSetting up a project
项目设置
Run the CLI inside the user's project before writing any shader code:
bash
npx shaders connectIt detects the framework, installs with the project's package manager, writes , signs the user in through their browser, connects the codebase to a Shaders project (a canvas in the design editor), and installs this skill. Always use explicit subcommands like these: bare is an interactive menu for humans. In CI or without a browser, set to a personal API key. skips the sign-in, skips every prompt.
shadersshaders.config.tsnpx shadersSHADERS_API_KEY--no-auth--yesOnce connected, three commands cover the workflow:
bash
npx shaders install # pick shaders from the connected project; writes component files
npx shaders search "<look>" # search the preset library by describing the look (free, no sign-in)
npx shaders preview offsets-1 # open a preset on shaders.com so the user can see it running
npx shaders install offsets-1 # a preset from the library, by name (Pro)
npx shaders update # pull in what the user changed in the design editor
npx shaders open # open the connected project in the design editorEach install writes one component file into (recorded in ) with the full component tree and every prop inline. Treat it as ordinary source: edit props, bind them to state, compose it with other effects. leaves files with local edits alone unless is passed.
outDirshaders.lock.jsonupdate--forceWhen a user asks for an effect:
- If a component or two can make it (a gradient with a cursor trail, a blurred image, glass over a logo, noise behind text), compose it by hand from the components below. Try this first.
- Otherwise search the preset library: . Results carry a name, a description, a match score and a page URL. Let the user see a candidate running with
npx shaders search "liquid chrome hero background" --json(opens shaders.com), then install it withnpx shaders preview <name>(Pro, see below).npx shaders install <name> - If the user already designed it in the editor: and pick it.
npx shaders install
The MCP server () gives you tools to search presets, read a shader's props, and generate SDF files from SVGs. Use it when installed.
npx shaders install-mcp在编写任何 shader 代码之前,在用户的项目中运行 CLI:
bash
npx shaders connect它会检测所用框架,使用项目的包管理器安装 ,写入 ,通过浏览器让用户登录,将代码库连接到一个 Shaders 项目(即设计编辑器中的画布),并安装此 skill。务必使用如下显式子命令:直接运行 会打开供人工操作的交互式菜单。在 CI 或无法使用浏览器时,将 设置为个人 API key。 会跳过登录, 会跳过所有提示。
shadersshaders.config.tsnpx shadersSHADERS_API_KEY--no-auth--yes连接完成后,以下三个命令覆盖整个工作流:
bash
npx shaders install # pick shaders from the connected project; writes component files
npx shaders search "<look>" # search the preset library by describing the look (free, no sign-in)
npx shaders preview offsets-1 # open a preset on shaders.com so the user can see it running
npx shaders install offsets-1 # a preset from the library, by name (Pro)
npx shaders update # pull in what the user changed in the design editor
npx shaders open # open the connected project in the design editor每次安装都会在 中写入一个组件文件, 记录在 中;文件包含完整的组件树,并将所有 props 内联。将其视为普通源代码:可以编辑 props、将其绑定到 state,也可以与其他效果组合。 会保留存在本地编辑的文件,除非传入 。
outDiroutDirshaders.lock.jsonupdate--force当用户要求某种效果时:
- 如果一两个组件就能实现该效果(例如带光标拖尾的渐变、模糊图片、覆盖 logo 的玻璃效果、文字背后的噪声),请使用下方组件手动组合。始终先尝试这种方式。
- 否则搜索预设库: 。结果包含名称、描述、匹配分数和页面 URL。让用户通过
npx shaders search "liquid chrome hero background" --json查看候选效果的运行情况(会打开 shaders.com),然后使用npx shaders preview <name>安装(Pro,见下文)。npx shaders install <name> - 如果用户已经在编辑器中完成设计: 运行 并进行选择。
npx shaders install
MCP 服务器()提供工具,用于搜索预设、读取 shader 的 props,以及从 SVG 生成 SDF 文件。安装后即可使用。
npx shaders install-mcpComposing an effect
组合效果
<Shader><canvas>jsx
import { Shader, LinearGradient, CursorTrail } from 'shaders/react'
<Shader className="w-full h-64">
<LinearGradient colorA="#0f172a" colorB="#7c3aed" angle={45} colorSpace="oklch" />
<CursorTrail />
</Shader>Rules that hold for every component:
- Size the canvas with CSS on (
<Shader>,classorclassName). The canvas has no size of its own and resizes with its container. Never style the innerstyle.<canvas> - Effects apply to what's drawn before them. A component that transforms imagery (blurs, distortions, adjustments, stylize, shape effects, transitions) is an effect. Placed as a sibling, it applies to every layer above it in the tree, so a flat stack is the default: image, then Blur, then Vignette. Nest layers inside an effect only when it must touch those layers and nothing else (a ripple on one grid while the swirl behind it stays still). Effects are marked "needs input" in ; they need something drawn before them or nested inside them. Generators (textures, shapes) draw on their own.
components.md - Universal props on every component: (
blendMode,normal,multiply,screen,overlay,softLight,hardLight,linearDodge,linearBurn,colorDodge,colorBurn,darken,lighten,difference,exclusion,hue,saturation,color),luminosity(0–1),opacity,visible+maskSource,maskType,boundingBox.absolute - Masking: give the mask layer an , set
idon it, and setvisible={false}on the layer to mask. Hidden layers still exist in the tree, so they work as masks.maskSource="thatId" - Placement: .
boundingBox={{ x: { unit: 'px', value: 24 }, y: { unit: 'uv', value: 0.1 }, width: ..., height: ..., origin: 'top-left', rotation: 15 }}is a 0–1 fraction of the canvas,uvis CSS pixels and survives resizes.px - Group is a container. Its prop (
flow) stacks its children like flexbox.{ mode: 'column' | 'row', gap, align } - Color props take CSS hex strings. Components with a prop blend in
colorSpace,linear,oklch,oklab,hslorhsv.lchandoklabkeep gradient midpoints bright.oklch
<Shader><canvas>jsx
import { Shader, LinearGradient, CursorTrail } from 'shaders/react'
<Shader className="w-full h-64">
<LinearGradient colorA="#0f172a" colorB="#7c3aed" angle={45} colorSpace="oklch" />
<CursorTrail />
</Shader>以下规则适用于每个组件:
- 使用 CSS 在 上设置 canvas 尺寸(
<Shader>、class或className)。canvas 自身没有尺寸,会随容器调整大小。绝不要设置内部style的样式。<canvas> - 效果会作用于此前绘制的内容。对图像进行变换的组件(模糊、扭曲、调整、风格化、形状效果、转场)是一种 effect。作为同级组件放置时,它会作用于组件树中排在它之前的每一层,因此默认采用扁平堆叠:图片、然后 Blur、然后 Vignette。仅当某个效果必须作用于特定图层而不影响其他图层时,才将图层嵌套在该效果内;例如,让涟漪作用于一个网格,同时让其后的漩涡保持静止。会将需要输入的组件标记为 “needs input”;它们前面需要有已绘制的内容,或需要将内容嵌套在其中。生成器(纹理、形状)会自行绘制内容。
components.md - 每个组件的通用 props:(
blendMode、normal、multiply、screen、overlay、softLight、hardLight、linearDodge、linearBurn、colorDodge、colorBurn、darken、lighten、difference、exclusion、hue、saturation、color)、luminosity(0–1)、opacity、visible+maskSource、maskType、boundingBox。absolute - 遮罩:为 mask 图层设置 ,将其设为
id,并在目标图层上设置visible={false}以应用遮罩。隐藏图层仍存在于组件树中,因此可用作 mask。maskSource="thatId" - 放置:。
boundingBox={{ x: { unit: 'px', value: 24 }, y: { unit: 'uv', value: 0.1 }, width: ..., height: ..., origin: 'top-left', rotation: 15 }}是 canvas 的 0–1 比例,uv是 CSS 像素,并会在尺寸变化后保持不变。px - Group 是一个容器。其 prop(
flow)会像 flexbox 一样堆叠子组件。{ mode: 'column' | 'row', gap, align } - 颜色 props 接受 CSS 十六进制字符串。带有 prop 的组件可使用
colorSpace、linear、oklch、oklab、hsl或hsv进行混合。lch和oklab可让渐变中点保持明亮。oklch
Dynamic props
动态 Props
Pass an object with a in place of a number or value. No animation code needed:
type{x, y}jsx
<Circle radius={{ type: 'auto-animate', mode: 'ping-pong', outputMin: 0.2, outputMax: 0.6, speed: 1 }} />
<Circle center={{ type: 'mouse-position', smoothing: 0.12, momentum: 0.2 }} />
<Blur intensity={{ type: 'mouse', axis: 'x', outputMin: 0, outputMax: 40, smoothing: 0.1 }} />
<LinearGradient id="grad" colorA="#000000" colorB="#ffffff" />
<Circle radius={{ type: 'map', source: 'grad', channel: 'luminance', inputMin: 0, inputMax: 1, outputMin: 0.02, outputMax: 0.12 }} />mouse-position{x, y}centerpositionoffsetmousemapid在数字或 值的位置传入一个带 的对象。无需编写动画代码:
{x, y}typejsx
<Circle radius={{ type: 'auto-animate', mode: 'ping-pong', outputMin: 0.2, outputMax: 0.6, speed: 1 }} />
<Circle center={{ type: 'mouse-position', smoothing: 0.12, momentum: 0.2 }} />
<Blur intensity={{ type: 'mouse', axis: 'x', outputMin: 0, outputMax: 40, smoothing: 0.1 }} />
<LinearGradient id="grad" colorA="#000000" colorB="#ffffff" />
<Circle radius={{ type: 'map', source: 'grad', channel: 'luminance', inputMin: 0, inputMax: 1, outputMin: 0.02, outputMax: 0.12 }} />mouse-position{x, y}centerpositionoffsetmousemapidFramework idioms
框架惯用法
| Framework | Import | Notes |
|---|---|---|
| React | | |
| Vue | | kebab-case attributes, |
| Svelte | | plain props; SvelteKit needs nothing extra, |
| Solid | | pass accessor values ( |
| Vanilla | | |
Shaders needs a browser with WebGPU. It can't render on the server, so keep client-only in meta-frameworks.
<Shader>| 框架 | 导入 | 说明 |
|---|---|---|
| React | | 使用 |
| Vue | | 使用 kebab-case 属性,非字符串值前加 |
| Svelte | | 使用普通 props;SvelteKit 无需额外配置, |
| Solid | | 传入 accessor 值( |
| Vanilla | | |
Shaders 需要支持 WebGPU 的浏览器。它无法在服务端渲染,因此在 meta-frameworks 中应让 保持 client-only。
<Shader>Recipes
配方
Four trees that cover most requests. Props are real; change the values, keep the structure.
Hero background. Generators stack as siblings, noise at low opacity adds grain, the trail sits on top:
jsx
<Shader className="absolute inset-0 -z-10">
<MeshGradient colorA="#1a0533" colorB="#ffdf8e" colorSpace="oklab" speed={0.6} />
<SimplexNoise scale={3} speed={0.5} opacity={0.12} blendMode="softLight" />
<CursorTrail colorA="#ffffff" colorB="#e04b9e" radius={0.3} length={0.4} opacity={0.5} blendMode="screen" />
</Shader>Glass over a logo. Glass comes after the image, so it refracts everything drawn before it. The shape is a circle SDF that follows the cursor:
jsx
<Shader className="w-full h-96">
<ImageTexture url="/logo.png" objectFit="contain" />
<Glass
shape={{ type: 'circleSDF', radius: 0.35 }}
center={{ type: 'mouse-position', smoothing: 0.15, momentum: 0.2 }}
refraction={1.3}
thickness={0.6}
aberration={0.4}
fresnel={0.25}
/>
</Shader>Blurred image behind text. A flat stack: image, then Blur, then Vignette, each applying to what's above it. The text is ordinary HTML layered above the canvas with CSS, not a component:
jsx
<div className="relative">
<Shader className="absolute inset-0 -z-10">
<ImageTexture url="/hero.jpg" objectFit="cover" />
<Blur intensity={60} />
<Vignette intensity={0.8} radius={0.6} />
</Shader>
<h1 className="relative">Headline</h1>
</div>Masked reveal. The circle is hidden and only shapes the gradient. Animate to reveal:
radiusjsx
<Shader className="w-full h-64">
<Circle id="reveal" visible={false} radius={{ type: 'auto-animate', mode: 'ping-pong', outputMin: 0.1, outputMax: 0.9, speed: 0.5 }} softness={0.2} />
<LinearGradient colorA="#0f172a" colorB="#7c3aed" angle={45} colorSpace="oklch" maskSource="reveal" />
</Shader>以下四种组件树可覆盖大多数请求。Props 均为真实可用属性;请修改属性值并保留结构。
Hero 背景。 生成器以同级方式堆叠,低不透明度噪声添加颗粒感,光标拖尾置于顶层:
jsx
<Shader className="absolute inset-0 -z-10">
<MeshGradient colorA="#1a0533" colorB="#ffdf8e" colorSpace="oklab" speed={0.6} />
<SimplexNoise scale={3} speed={0.5} opacity={0.12} blendMode="softLight" />
<CursorTrail colorA="#ffffff" colorB="#e04b9e" radius={0.3} length={0.4} opacity={0.5} blendMode="screen" />
</Shader>Logo 上方的玻璃效果。 Glass 位于图片之后,因此会折射此前绘制的一切。形状是一个跟随光标的圆形 SDF:
jsx
<Shader className="w-full h-96">
<ImageTexture url="/logo.png" objectFit="contain" />
<Glass
shape={{ type: 'circleSDF', radius: 0.35 }}
center={{ type: 'mouse-position', smoothing: 0.15, momentum: 0.2 }}
refraction={1.3}
thickness={0.6}
aberration={0.4}
fresnel={0.25}
/>
</Shader>文字背后的模糊图片。 扁平堆叠:先图片,再 Blur,最后 Vignette;每个效果都作用于其上方的内容。文字是普通 HTML,通过 CSS 叠放在 canvas 上,而不是组件:
jsx
<div className="relative">
<Shader className="absolute inset-0 -z-10">
<ImageTexture url="/hero.jpg" objectFit="cover" />
<Blur intensity={60} />
<Vignette intensity={0.8} radius={0.6} />
</Shader>
<h1 className="relative">Headline</h1>
</div>遮罩显现。 圆形图层处于隐藏状态,仅用于塑造渐变。动画化 以实现显现效果:
radiusjsx
<Shader className="w-full h-64">
<Circle id="reveal" visible={false} radius={{ type: 'auto-animate', mode: 'ping-pong', outputMin: 0.1, outputMax: 0.9, speed: 0.5 }} softness={0.2} />
<LinearGradient colorA="#0f172a" colorB="#7c3aed" angle={45} colorSpace="oklch" maskSource="reveal" />
</Shader>Picking components
选择组件
<!-- catalog:start -->
next to this file has a one-line description of every component and marks the effects that need input. For a component's exact props, defaults and ranges, fetch its docs page with an header:
199 components, by category:
- Textures (54): Aurora, Beam, Blob, BlockNoise, BlueNoise, BrickPattern, Checkerboard, Chevron, ColorWheel, ConicGradient, CurlNoise, DiamondGradient, DotGrid, ErosionNoise, FallingLines, FloatingParticles, FlowingGradient, FractalNoise, GaborNoise, Godrays, Grid, HexGrid, HTMLInCanvas, ImageTexture, IsometricCubes, LinearGradient, Marble, MeshGradient, MultiPointGradient, PerlinNoise, Plasma, Prism, RadialGradient, Ripples, Scratches, SimplexNoise, SineWave, SolidColor, Spiral, Strands, Stripes, StudioBackground, SunBurst, Swirl, Text, TriangularGrid, Truchet, VideoTexture, Voronoi, Waveform, WaveletNoise, Weave, WebcamTexture, WorleyNoise
- Shapes (16): Arc, Circle, Crescent, Cross, Ellipse, Flower, Heart, Line, Parallelogram, Polygon, Ring, RoundedRect, Star, Teardrop, Trapezoid, Vesica
- Shape Effects (23): BrushedMetal, CarbonFiber, Chrome, Crystal, Emboss, Frost, Glass, Goo, Heatmap, Hologram, Holographic, Irradiance, LightEdge, LiquidMetal, Nebula, Neon, Obsidian, Particles, Plastic, SmokeFill, ThinFilm, Voxels, Water
- Blurs (9): AngularBlur, Blur, BokehBlur, ChannelBlur, DiffuseBlur, LinearBlur, ProgressiveBlur, TiltShift, ZoomBlur
- Distortions (22): BarShift, Bend, Bulge, ConcentricSpin, CornerPin, DisplacementMap, Flip, FlowField, FlutedGlass, Form3D, GlassTiles, Kaleidoscope, Mirror, Perspective, PolarCoordinates, RectangularCoordinates, Repeater, Spherize, Stretch, Surface3D, Twirl, WaveDistortion
- Adjustments (14): BrightnessContrast, Duotone, Exposure, FilmStock, Grayscale, HueShift, Invert, Posterize, Saturation, Sharpness, Solarize, Tint, Tritone, Vibrance
- Stylize (31): Ascii, Chalkboard, ChromaticAberration, CompressionArtifacts, ContourLines, CRTScreen, DataMosh, Dither, DropShadow, Engraving, FilmGrain, Glitch, Glow, GradientMap, Halftone, KeyFrames, LensDistortion, LensFlare, LightLeak, ObjectTracker, Paper, ParticleField, Pixelate, ReflectivePlane, Sparkle, Stone, TimeTrail, VHS, Vignette, Watercolor, Wool
- Interactive (16): Boids, ChromaFlow, CursorRipples, CursorTrail, Fog, GridDistortion, InkFlow, Liquify, MagneticFilings, ParticleFlow, PixelSort, PixelThrow, ReactionDiffusion, Shatter, Smoke, SmokeFlow
- Transitions (13): BarnDoors, BlockDissolve, CheckerWipe, DiamondWipe, IrisWipe, LinearWipe, NoiseDissolve, PagePeel, RadialWipe, RandomBars, RippleWipe, SliceWipe, VenetianBlinds
- Utilities (1): Group
components.mdAccept: text/markdownhttps://shaders.com/docs/components/<Name>The whole reference in one file is https://shaders.com/llms-full.txt (large). Guides live at https://shaders.com/docs/guide (composing, blending and masking, dynamic props, layout, transforms, shape effects, performance).
Shape effects (Glass, Neon, Chrome, LiquidMetal, Frost, ...) draw inside a prop: a 2D SDF such as or , , , a 3D solid such as , , , or the user's own SVG converted to an SDF file (the MCP tool does this). See https://shaders.com/docs/guide/shape-effects.
shape{ type: 'circleSDF', radius: 0.5 }roundedRectSDFstarSDFpolygonSDF{ type: 'sphere3D' }cube3Dtorus3Dgenerate-sdf按类别划分的 199 个组件:
- Textures (54): Aurora, Beam, Blob, BlockNoise, BlueNoise, BrickPattern, Checkerboard, Chevron, ColorWheel, ConicGradient, CurlNoise, DiamondGradient, DotGrid, ErosionNoise, FallingLines, FloatingParticles, FlowingGradient, FractalNoise, GaborNoise, Godrays, Grid, HexGrid, HTMLInCanvas, ImageTexture, IsometricCubes, LinearGradient, Marble, MeshGradient, MultiPointGradient, PerlinNoise, Plasma, Prism, RadialGradient, Ripples, Scratches, SimplexNoise, SineWave, SolidColor, Spiral, Strands, Stripes, StudioBackground, SunBurst, Swirl, Text, TriangularGrid, Truchet, VideoTexture, Voronoi, Waveform, WaveletNoise, Weave, WebcamTexture, WorleyNoise
- Shapes (16): Arc, Circle, Crescent, Cross, Ellipse, Flower, Heart, Line, Parallelogram, Polygon, Ring, RoundedRect, Star, Teardrop, Trapezoid, Vesica
- Shape Effects (23): BrushedMetal, CarbonFiber, Chrome, Crystal, Emboss, Frost, Glass, Goo, Heatmap, Hologram, Holographic, Irradiance, LightEdge, LiquidMetal, Nebula, Neon, Obsidian, Particles, Plastic, SmokeFill, ThinFilm, Voxels, Water
- Blurs (9): AngularBlur, Blur, BokehBlur, ChannelBlur, DiffuseBlur, LinearBlur, ProgressiveBlur, TiltShift, ZoomBlur
- Distortions (22): BarShift, Bend, Bulge, ConcentricSpin, CornerPin, DisplacementMap, Flip, FlowField, FlutedGlass, Form3D, GlassTiles, Kaleidoscope, Mirror, Perspective, PolarCoordinates, RectangularCoordinates, Repeater, Spherize, Stretch, Surface3D, Twirl, WaveDistortion
- Adjustments (14): BrightnessContrast, Duotone, Exposure, FilmStock, Grayscale, HueShift, Invert, Posterize, Saturation, Sharpness, Solarize, Tint, Tritone, Vibrance
- Stylize (31): Ascii, Chalkboard, ChromaticAberration, CompressionArtifacts, ContourLines, CRTScreen, DataMosh, Dither, DropShadow, Engraving, FilmGrain, Glitch, Glow, GradientMap, Halftone, KeyFrames, LensDistortion, LensFlare, LightLeak, ObjectTracker, Paper, ParticleField, Pixelate, ReflectivePlane, Sparkle, Stone, TimeTrail, VHS, Vignette, Watercolor, Wool
- Interactive (16): Boids, ChromaFlow, CursorRipples, CursorTrail, Fog, GridDistortion, InkFlow, Liquify, MagneticFilings, ParticleFlow, PixelSort, PixelThrow, ReactionDiffusion, Shatter, Smoke, SmokeFlow
- Transitions (13): BarnDoors, BlockDissolve, CheckerWipe, DiamondWipe, IrisWipe, LinearWipe, NoiseDissolve, PagePeel, RadialWipe, RandomBars, RippleWipe, SliceWipe, VenetianBlinds
- Utilities (1): Group
与本文件同目录的 提供了每个组件的单行说明,并会标记需要输入的 effects。要获取某个组件的准确 props、默认值和取值范围,请使用 请求头获取其文档页面:
components.mdAccept: text/markdownhttps://shaders.com/docs/components/<Name>完整参考资料集中在一个文件中:https://shaders.com/llms-full.txt(文件较大)。指南位于 https://shaders.com/docs/guide,涵盖组合、混合与遮罩、动态 props、布局、变换、形状效果和性能。
形状效果(Glass、Neon、Chrome、LiquidMetal、Frost 等)会在 prop 内部绘制:可以是 2D SDF,例如 或 、、;也可以是 3D 实体,例如 、、;还可以是用户自己的 SVG 转换成的 SDF 文件(MCP 的 工具可完成此操作)。参见 https://shaders.com/docs/guide/shape-effects。
shape{ type: 'circleSDF', radius: 0.5 }roundedRectSDFstarSDFpolygonSDF{ type: 'sphere3D' }cube3Dtorus3Dgenerate-sdfPresets and sections (Shaders Pro)
预设与区块(Shaders Pro)
The preset library at https://shaders.com/presets holds over a thousand designed effects in collections, plus complete website sections. Each preset page shows its name (collection slug plus variant number, such as ), a live render, and its code. Installing one needs a Shaders Pro subscription on the signed-in account:
offsets-1bash
npx shaders install offsets-1Without Pro the CLI says so and offers to open the pricing page, sign in to a different account, or install a watermarked preview component so the user can see the preset in place. Once the account has Pro, swaps the preview for the real component. Don't work around this: tell the user the preset is Pro, install the preview if they want to evaluate it, and move on.
npx shaders updateWhen the look takes more than a couple of components, search the library before building it from scratch. A preset is a designed, tuned tree; say so and name it when one fits.
https://shaders.com/presets 上的预设库包含一千多个按集合分类的设计效果,以及完整的网站区块。每个预设页面会显示其名称(集合 slug 加变体编号,例如 )、实时渲染效果及其代码。安装其中一个预设需要已登录账号订阅 Shaders Pro:
offsets-1bash
npx shaders install offsets-1如果没有 Pro,CLI 会说明这一点,并提供以下选项:打开定价页、改用其他账号登录,或安装带水印的预览组件,让用户能在实际页面中查看预设。账号拥有 Pro 后,运行 会将预览组件替换为正式组件。不要绕过此限制:告知用户该预设为 Pro 功能;如果用户想评估效果,可以安装预览,然后继续。
npx shaders update当所需效果无法仅用少数几个组件实现时,先搜索预设库,再从头构建。预设是经过设计和调校的组件树;若某个预设适合需求,应明确说明并说出其名称。
Writing a component that doesn't exist
编写尚不存在的组件
If no component or preset produces the look, write one. A component is a plain object passed to from : a name, its props, and what to draw at each pixel, built from the same primitives the library uses, or a raw WGSL body. Mount it with in any framework, no build step. Read https://shaders.com/docs/guide/custom-shaders first and the primitives reference at https://shaders.com/docs/primitives (or https://shaders.com/std/llms.txt as one document).
defineShadershaders/std<CustomShader src={MyShader}>如果没有任何组件或预设能实现该效果,就自行编写一个。组件是一个传给 中 的普通对象:包含名称、props,以及每个像素要绘制的内容;它可以使用该库所用的相同 primitives 构建,也可以直接使用原始 WGSL 代码。在任意框架中,都可以通过 挂载,无需构建步骤。请先阅读 https://shaders.com/docs/guide/custom-shaders,以及 https://shaders.com/docs/primitives 的 primitives 参考文档(或将 https://shaders.com/std/llms.txt 作为一份完整文档阅读)。
shaders/stddefineShader<CustomShader src={MyShader}>Keeping effects fast
保持效果高性能
- Build backgrounds from generators (gradients, noise, plasma); they cost almost nothing.
- Keep effects as siblings in a flat stack. Nesting adds a render pass per level, and a sibling effect already covers everything drawn before it.
- Hide layers with , not
visible={false}. A hidden layer costs nothing; a transparent one still renders.opacity={0} - Animate runtime props. A few props are compile-time (component pages flag them) and recompile the effect when changed.
- Offscreen canvases drop to about 1fps on their own; nothing to do.
- 使用 generators(渐变、噪声、plasma)构建背景;其成本几乎为零。
- 将 effects 保持为扁平堆叠中的同级组件。每嵌套一层都会增加一次 render pass,而同级 effect 已经能覆盖此前绘制的一切。
- 使用 隐藏图层,而不是
visible={false}。隐藏图层不产生任何成本;透明图层仍会被渲染。opacity={0} - 对 runtime props 做动画。少数 props 属于 compile-time(组件页面会标记),修改它们会重新编译效果。
- 离屏 canvas 自行渲染时帧率会降至约 1fps;无需处理。
Don't
禁止事项
- Don't install ,
three,@react-three/fiberor write raw WebGL for an effect a component covers.ogl - Don't invent prop names. Check , then the component's docs page.
components.md - Don't nest layers inside an effect when a sibling stack gives the same result. Nest only to scope an effect to specific layers.
- Don't style or query the inner ; its markup can change between releases.
<canvas>
- 对于已有组件可以覆盖的效果,不要安装 、
three、@react-three/fiber或编写原始 WebGL 代码。ogl - 不要臆造 prop 名称。先查看 ,再查看该组件的文档页面。
components.md - 当同级堆叠能实现相同效果时,不要将图层嵌套在 effect 内。仅在需要将 effect 限定于特定图层时才嵌套。
- 不要设置内部 的样式或查询它;其标记结构可能在版本发布后发生变化。
<canvas>