DirectionalLight

← prev index next →

The base concept for a light: what illuminates the scene so surfaces are shaded. This element is the directional (sun-like) light.

Remarks

Lights are scene nodes that decide how bright each surface is. Skenra has two kinds, and a scene provides sensible defaults until you declare your own.

A directional light (<sk-directional-light>) is a distant, sun-like source whose rays are all parallel: its intensity and color set the light, and its direction comes from the node's orientation — pitch it with p, turn it with h — so rotating the node moves the sun. It can cast shadows (cast-shadow) and aim at a target.

The other light, an ambient light (sk-ambient-light), adds flat, directionless fill that lifts the shadows evenly; it has only intensity and color, casts no shadows, and combines additively with the directional key light.

The example lights a sphere with a single sun pitched down from above and arcs it across the sky.

Constructor

constructor(config?: { id?: string; intensity?: number; castShadow?: boolean; color?: ColorInput; x?: number; y?: number; z?: number; h?: number; p?: number; b?: number; quaternion?: Quaternion; sx?: number; sy?: number; sz?: number; s?: number; class?: string; style?: string })

Properties

Config Properties

PropertyTypeDefaultDescriptionDeclared by
idconfig-only string Unique identifier for the light node. DirectionalLightConfig
intensityanimatable number 1.0 Gets the light intensity. DirectionalLight
castShadow boolean true Gets whether this light generates shadows. DirectionalLight
coloranimatable ColorInput "white" Gets the light color. DirectionalLight
xanimatable number 0 Gets the X position component. SceneNode
yanimatable number 0 Gets the Y position component. SceneNode
zanimatable number 0 Gets the Z position component. SceneNode
hanimatable number 0 Gets the heading (Y-axis) rotation in radians. SceneNode
panimatable number 0 Gets the pitch (X-axis) rotation in radians. SceneNode
banimatable number 0 Gets the bank (Z-axis) rotation in radians. SceneNode
quaternionanimatable Quaternion Gets the rotation as a quaternion. SceneNode
sxanimatable number 1 Gets the X-axis scale factor. SceneNode
syanimatable number 1 Gets the Y-axis scale factor. SceneNode
szanimatable number 1 Gets the Z-axis scale factor. SceneNode
sconfig-only number Uniform scale factor for all axes (overrides sx, sy, sz if provided) SceneNodeConfig
classconfig-only string Standard HTML class applied to the node host at construction SceneNodeConfig
styleconfig-only string Standard inline style applied to the node host at construction SceneNodeConfig

Other Properties

PropertyTypeDefaultDescriptionDeclared by
intensityBasebase value number Gets the base (non-animated) value of intensity. DirectionalLight
colorBasebase value ColorBase Gets the base (non-animated) value of the light color. DirectionalLight
target string | [number, number, number] | null The point this light aims at — a world-space [x, y, z] or the id of DirectionalLight
lightDirectionread-only Float32Array<ArrayBufferLike> Gets the light's direction vector in world space. DirectionalLight
hRad number 0 Gets the heading (Y-axis) rotation in radians. SceneNode
pRad number 0 Gets the pitch (X-axis) rotation in radians. SceneNode
bRad number 0 Gets the bank (Z-axis) rotation in radians. SceneNode
hDeg number 0 Gets the heading (Y-axis) rotation in degrees. SceneNode
pDeg number 0 Gets the pitch (X-axis) rotation in degrees. SceneNode
bDeg number 0 Gets the bank (Z-axis) rotation in degrees. SceneNode
xBasebase value number Gets the base (non-animated) value of X position. SceneNode
yBasebase value number Gets the base (non-animated) value of Y position. SceneNode
zBasebase value number Gets the base (non-animated) value of Z position. SceneNode
hBasebase value number 0 Gets the base (non-animated) value of heading (Y-axis rotation). SceneNode
pBasebase value number 0 Gets the base (non-animated) value of pitch (X-axis rotation). SceneNode
bBasebase value number 0 Gets the base (non-animated) value of bank (Z-axis rotation). SceneNode
quaternionBasebase value Quaternion Gets the base (non-animated) quaternion rotation. SceneNode
sxBasebase value number Gets the base (non-animated) value of X-axis scale. SceneNode
syBasebase value number Gets the base (non-animated) value of Y-axis scale. SceneNode
szBasebase value number Gets the base (non-animated) value of Z-axis scale. SceneNode
draggable boolean false Gets whether this node can be dragged. SceneNode
parentread-only SceneNode | null The parent node in the scene graph, or null at the root. SceneNode
sceneread-only any Gets the Scene this node belongs to, if any. SceneNode
posread-only Point3D Gets the world-space position of this node. SceneNode
matrixread-only Float32Array<ArrayBufferLike> Gets the computed 4x4 transformation matrix. SceneNode
animationsread-only SkenraAnimation[] Public getter for the animations affecting this target. SkenraAnimatable

Methods

MethodDescriptionDeclared by
addMaterialPaint(material: Material, paintTarget: PaintTarget, options?: any): Paint Applies a material to this node for a specific paint target. SceneNode
getPaints(): ReadonlyMap<PaintTarget, Paint> SceneNode
getOwnPaints(): readonly Paint[] Gets only the paints directly applied to this node (not inherited). SceneNode
removePaint(paint: Paint): void Removes a paint from this node. SceneNode
getWorldPosition(): number[] Returns this node’s origin in world space. SceneNode
getWorldMatrix(): Float32Array<ArrayBufferLike> Returns this node’s world transform — its local transform composed with all ancestor transforms. SceneNode
setViewportPosition(camera: AbstractCamera, x: number, y: number, z: number = 0.5): boolean Sets the node's local translation so that it appears at the specified viewport position. SceneNode
getViewportPosition(camera?: AbstractCamera): [number, number, number] | null Returns the node's current world position projected to viewport CSS pixel coordinates. SceneNode
setNDCPosition(camera: AbstractCamera, ndcX: number, ndcY: number, ndcZ: number = 0.5): boolean Sets the node's local translation so that it appears at the specified NDC position in the viewport. SceneNode
getBoundsInSpace(targetSpaceMatrix: Float32Array): { minX: number; maxX: number; minY: number; maxY: number; minZ: number; maxZ: number; } | null Computes the axis-aligned bounding box (AABB) of this node and its descendants SceneNode
animate(firstArg: TypedKeyframe<this>[] | TypedPropertyIndexedKeyframes<this> | Record<string, any> | globalThis.Keyframe[] | globalThis.PropertyIndexedKeyframes | null, secondArg?: number | KeyframeAnimationOptions | globalThis.KeyframeAnimationOptions): SkenraAnimation & Animation Creates a new SkenraAnimation for this target per Web Animations API. SkenraAnimatable
removeAnimation(animation: SkenraAnimation): boolean Removes a specific animation from this object. SkenraAnimatable
constrain(type: K, config: ConstraintConfigMap[K]): ConstraintReturnMap[K] Creates a constraint on this Animatable using a fluent API. SkenraAnimatable

Example

const scene = new Scene();
scene.appendChild(new Sphere({ id: 'lit', radius: 1.5 }));

// Sunlight pitched from above; the direction comes from the node's rotation.
const sun = new DirectionalLight({ id: 'sun', intensity: 1.0 });
sun.p = -Math.PI / 4; // 45° from above (pitch / X-axis)
scene.appendChild(sun);

// Arc it across the sky (id lives in the options, per the WAAPI signature).
sun.animate([
  { offset: 0, p: -Math.PI / 6 },   // low
  { offset: 0.5, p: -Math.PI / 2 }, // overhead
  { offset: 1, p: -5 * Math.PI / 6 } // low, other side
], { id: 'sun-arc', duration: 10000, iterations: Infinity, direction: 'alternate' });