Capítulo 798 de 859

Chapter 798: TSL Built-in Inputs & Attributes

Core Idea

TSL exposes the shader stage's implicit inputs (vertex position/normal, camera matrices, screen coordinates, time, material properties) as ready-made node constants you reference by name instead of declaring varyings/uniforms by hand.

Key Concepts

  • positionLocal / positionWorld / positionView: vertex position in local, world, and view space respectively — the standard vertex-stage chain.
  • normalLocal / normalWorld / normalView / normalGeometry: analogous normal-space variants; transformedNormalView/transformedNormalWorld are post-normal-mapping.
  • cameraPosition / cameraViewMatrix / cameraProjectionMatrix: camera-space uniforms, auto-updated per frame.
  • screenUV / screenCoordinate / screenSize: fragment-stage screen-space helpers for post-processing and full-screen effects.
  • time / deltaTime / frameId: global animation clock uniforms, updated by the renderer each frame — no manual uniform wiring needed.
  • materialColor / materialRoughness / materialMetalness / …: the current material's PBR properties as readable nodes, useful when writing a custom lighting model that still respects material inputs.

Reference Tables

Position / geometry pipeline

NodeTypeSpace
positionGeometryvec3Raw geometry attribute, pre-skinning/morph
positionLocalvec3Local (object) space, post-skinning/morph
positionPreviousvec3Previous frame's local position (motion vectors)
positionWorldvec3World space
positionWorldDirectionvec3Normalized world-space direction from camera
positionViewvec3View (camera) space
positionViewDirectionvec3Normalized view-space direction

Normal / tangent pipeline

NodeNotes
normalGeometry, normalLocal, normalView, normalWorldRaw normal at each space, pre-normal-map
normalFlatFace-flat normal (derivative-based)
transformedNormalView, transformedNormalWorldPost-normal-map, what lighting should actually use
tangentGeometry, tangentLocal, tangentView, tangentWorldTangent vectors, mirror the normal set
bitangentGeometry/Local/View/WorldBitangent vectors

Camera / model uniforms

NodeDescription
cameraPosition, cameraNear, cameraFarCamera transform/frustum
cameraViewMatrix, cameraProjectionMatrix(Inverse), cameraWorldMatrix, cameraNormalMatrixCamera matrices
modelPosition, modelScale, modelDirection, modelRadiusCurrent object's transform
modelViewMatrix, modelWorldMatrix, modelWorldMatrixInverse, modelNormalMatrixModel transform matrices
modelViewProjectionCombined MVP, as a varying

Screen / viewport (fragment stage)

NodeDescription
screenUV, screenCoordinate, screenSize, screenDPRFull-canvas screen-space helpers
viewportUV, viewportCoordinate, viewportSize, viewportCurrent viewport (may differ from screen with multiple viewports)
viewportLinearDepthLinear (non-projective) fragment depth

Time / frame

NodeDescription
timeSeconds since start, auto-updated
deltaTimeSeconds since last frame
frameIdInteger frame counter

Compute-only builtins: instanceIndex, vertexIndex, drawIndex, workgroupId, localId, globalId, subgroupIndex, subgroupSize, numWorkgroups — used inside compute shaders (WebGPURenderer compute passes), not classic vertex/fragment materials.

Anti-patterns

  • Reading normalView/normalWorld for lighting instead of the transformed* variants: you'll get pre-normal-map shading, which looks flat wherever a normal map is applied.
  • Assuming compute-only builtins (instanceIndex, workgroupId, etc.) work in a regular material: they're only valid inside a compute shader context.

Key Takeaways

  1. There's a consistent naming pattern: <thing><Space> (position / normal / tangent × Geometry / Local / View / World) — learn the pattern once, it covers dozens of builtins.
  2. Use transformed* variants for lighting-relevant normals (they account for normal mapping); use the plain *View/*World variants when you need the un-perturbed geometric normal.
  3. time/deltaTime/frameId remove the need to manually create and update a uniform(0) clock every frame.
  4. Compute-stage builtins (workgroupId, globalId, atomics) only make sense inside WebGPURenderer compute passes, not standard material shaders.

Connects To

  • ch797 (TSL Language Basics): these constants are the typical leaves of a node graph built with Fn()/operators.
  • ch799 (TSL Utility Functions): higher-level functions that consume these builtins internally (e.g. reflection/refraction helpers use positionView/normalView).
  • MeshStandardNodeMaterial: exposes colorNode/normalNode/positionNode etc. as the attachment points for graphs built from these inputs.