pub struct Shader {
pub asset_id: AssetId,
pub fragment: String,
pub vertex: Option<String>,
pub locator: Option<PayloadLocator>,
}Expand description
Replaces how surfaces are shaded, and optionally how vertices are placed, with functions of your own. Written in Slang, one source for every backend.
A Shader is entirely optional. The engine ships its own lighting and
projection and uses them for every draw a Shader does not claim, so a world
that wants standard lighting declares no Shader at all. The shadow pass and
the depth pre-pass are engine-internal and take no Shader stage; enable or
size shadows with shadow_map_size in GraphicsConfig.
// Custom shading only; the engine still places every vertex.
let water = Shader {
fragment: "assets/shaders/water.slang".into(),
..Default::default()
};
// Both hooks: a sway displacement, then the surface.
let reeds = Shader {
vertex: Some("assets/shaders/reeds_sway.slang".into()),
fragment: "assets/shaders/reeds.slang".into(),
..Default::default()
};
assert!(water.vertex.is_none() && reeds.vertex.is_some());§The two hooks
A Shader file defines a function, not an entry point. The engine owns every entry point, binding and pipeline on every backend, and calls the world’s functions from inside its own:
// the `fragment` file, required
float4 shade(VertexOut in, GpuObjectData od);
// the `vertex` file, optional; without one the engine projects the vertex itself
VertexOut transform(float4x4 model, float3 pos, float3 normal, float3 tangent,
float3 color, float2 uv);shade returns the surface’s linear-light colour with alpha. od is the
surface’s material record whichever path drew it: tint_roughness,
emissive_metallic, albedo_index, normal_index, emissive_map_index,
orm_map_index and bb_max_alpha_cutoff.w are the fields a surface
reads. transform receives the model matrix and the model-space
attributes, after skinning for a SkinnedMesh and per
instance for an InstancedProp, and returns the projected
vertex; the engine’s own is project_vertex, so a displacement is
return project_vertex(model, pos + offset, normal, tangent, color, uv);.
Both files are compiled inside the engine’s own main-pass source, so they see the same vocabulary the engine’s shading uses and declare no layout, binding, register, attribute or varying of their own:
shade_surface(in, od): the engine’s PBR lighting, soreturn shade_surface(in, od) * tint;starts from it.project_vertex(model, pos, normal, tangent, color, uv): the engine’s projection.pool_sample(index, uv): a texture from the world’s pool by the record’s index.decode_normal_map(rg): a tangent-space normal from a normal-map texel.shadow_factor_cascaded(world_pos, view_depth, screen_xy): the sun’s cascaded shadow term.environment_specular(world_pos, reflected, lod): the reflection environment.irradiance_sample(normal): the diffuse environment.VIEW: the view block, withvp,view_mat,elapsed,cam_x/cam_y/cam_zandsky_rot.LIGHTS: the light block, withdir[],pt[],num_dir,num_ptandambient_intensity.SKY_DIR(d): a world direction in the environment map’s frame.
VertexOut is the engine’s varying block: position (clip), world_pos,
normal, tangent, bitangent, uv, view_depth and color. A shade
must not read in.object_id; the record is od.
§More than one Shader
The first declared Shader is the world’s default: everything renders with it
unless a Material names another one through its shader field.
A world may declare up to 8 Shaders in total.
- Instanced, skinned, and voxel-chunk draws always use the world default. A Material naming a Shader cannot be used by an InstancedProp, a SkinnedMesh, or a VoxelWorld; give those a Material without one.
- At most 8 Shaders, the world default included.
Planar reflections are the one case with no build-time signal: a surface reflected in a mirror is drawn with the world default Shader regardless of its Material. Reflection probe cubes capture it the same way.
A Shader referenced only by materials belonging to one Scene is owned by that scene: its pipeline is built when the scene loads (behind the loading screen, alongside that scene’s textures and meshes) and released when the scene unloads. A Shader used across scenes, or by the world default, loads at startup.
§Compilation
cn build compiles both files for the backend it cooks for and stores the
result in the world; a player needs no shader compiler. A file that fails
to compile, or omits its hook, fails the build naming the Shader and the
hook. Under cn debug a save to either file recompiles it and swaps the
live pipelines.
Fields§
§asset_id: AssetIdAsset identity; injected via inject_name. Not part of args.
fragment: StringPath to the .slang file defining shade. Required.
vertex: Option<String>Path to the .slang file defining transform. Omit to keep the
engine’s own projection.
locator: Option<PayloadLocator>Injected at load time from BlobAssetDef::payload.
Implementations§
Trait Implementations§
Source§impl Component for Shader
impl Component for Shader
Source§fn from_baked(bytes: &[u8]) -> Result<Shader, CnResult>
fn from_baked(bytes: &[u8]) -> Result<Shader, CnResult>
Source§fn inject_locator(&mut self, locator: PayloadLocator)
fn inject_locator(&mut self, locator: PayloadLocator)
Source§fn inject_name(&mut self, id: AssetId)
fn inject_name(&mut self, id: AssetId)
Source§impl ComponentSlot for Shader
impl ComponentSlot for Shader
Source§const DISCRIMINANT: u8
const DISCRIMINANT: u8
ComponentId.Source§impl<'de> Deserialize<'de> for Shader
impl<'de> Deserialize<'de> for Shader
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Shader, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Shader, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl From<Shader> for ComponentAsset
impl From<Shader> for ComponentAsset
Source§fn from(c: Shader) -> ComponentAsset
fn from(c: Shader) -> ComponentAsset
impl RuntimeComponent for Shader
Source§impl Serialize for Shader
impl Serialize for Shader
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for Shader
impl RefUnwindSafe for Shader
impl Send for Shader
impl Sync for Shader
impl Unpin for Shader
impl UnsafeUnpin for Shader
impl UnwindSafe for Shader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
impl<S, T> Duplex<S> for Twhere
T: FromSample<S> + ToSample<S>,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().