cameraIndex
Provides the unsigned integer index of the camera currently being used for rendering, a key node for multi-camera effects like VR and split-screen.
Core Advantages
Automated Data Transfer & Logic Encapsulation. It automatically retrieves the camera index from the renderer (e.g., an ArrayCamera) and seamlessly passes it from vertex to fragment shader via .toVarying(), eliminating manual uniform/varying management and greatly simplifying multi-view rendering logic.
Common Uses
VR/Stereoscopic Rendering: Differentiates between left and right eyes (e.g., index 0 and 1) to apply parallax offsets for a 3D effect.
Split-screen Multiplayer: Acts as a player identifier to apply specific effects or UI to different players' screens.
Editor Tools: Conditionally renders gizmos or debug info based on the view type (e.g., editor vs. game preview).
Picture-in-Picture: Applies different shading logic for main and sub-views, such as simplifying rendering in the sub-view for performance.
How to adjust
This node's value is a discrete integer (0, 1, 2...) automatically switched by the renderer when rendering different camera views, not adjusted smoothly. For example, in VR, the renderer sets the index to 0 for the left eye, then to 1 for the right eye. The shader uses this 'switch' to execute different logic branches, resulting in two distinct outputs rather than a continuous transition.
Code Examples
1
2// Calculate a stereo offset based on the camera index (e.g., 0=left, 1=right)
3const stereoOffset = cameraIndex.equal( 1u ).cond(
4 vec3( 0.01, 0, 0 ), // Right eye offset
5 vec3( 0, 0, 0 ) // Left eye (no offset)
6);
7
8// Apply the offset to the transformed vertex position
9transformed.addAssign( stereoOffset );
10