LittleJS 1.30.0

Render3D

EngineObject3D

EngineObject3D - An EngineObject with a 3D transform and a mesh

  • Set pos3D, rotation3D and scale3D instead of the 2D pos, size and angle
  • Gets update, children, timers, destroy and renderOrder from EngineObject
  • velocity3D is added to pos3D each frame, slowed by damping, and render3D.gravity pulls it once it has a mass
  • Objects face -Z, the same way the camera does, so lookAt turns them to face a point
  • The 2D pos and velocity are still there but nothing draws them
  • These inherited fields are 2D only and do nothing here: angle, angleVelocity, additiveColor, drawSize and mirror; damping, angleDamping, clampSpeed, friction and groundObject work as in 2D, on velocity3D and angleVelocity3D
  • The inherited shader works here as in 2D, and with emissive at 1 its snippet does its own lighting
  • Set sync2D for a 2D game with 3D looks, pos and angle then drive pos3D and rotation3D, which is the one way those 2D fields reach a 3D object
  • setCollision takes the same flags as in 2D, but the solid collision happens in 3D against size3D
  • The solid box turns with rotation3D, so a turned wall or a ramp collides as it looks; resting on one no steeper than groundAngle stands there
  • Its tile and raycast halves are 2D only, collideLevel collides with the 3D level instead: the height maps, voxel maps and mesh colliders; a child sits solid collision out
  • A sync2D object collides in 2D instead, which needs the 2D size set as well as size3D
  • setMesh swaps the mesh and frees the old one, for text and terrain that get built again
  • addChild attaches the 3D transform, and a child's pos3D, rotation3D and scale3D are then relative to its parent, as in three.js and Godot; this differs from 2D, where a child's pos and angle stay in world space and localPos and localAngle hold the offset; getWorldPos3D, getWorldRotation3D and getMatrix give a child's world transform, and setWorldPos3D and setWorldRotation3D put it at a world position or rotation
  • attach keeps the child where it is and works the offset out, and removeChild leaves it where it was in the world
  • The 2D offset arguments of addChild do nothing here, set the child's pos3D

Extends EngineObject

Extended by CameraControl3D, FirstPersonCamera3D, GLTFObject, HeightMap, InstancedMesh3D, LensFlare3D, Light3D, MeshCollider, ParticleEmitter3D, Prefab3D, ThirdPersonCamera3D, Trail3D, VoxelMap

Constructor

new EngineObject3D(pos3D, mesh, tileInfo, color)render3dObject.js:46

Create a 3D object and add it to the object list

pos3D Vector3 optional

World space position

mesh Mesh optional

Mesh to draw, undefined draws nothing

tileInfo TileInfo|TextureInfo optional

Texture, mesh uvs map across the tile; a whole TextureInfo becomes the tile that covers it

color Color optional

Tint

class Spinner extends EngineObject3D
{
    constructor(pos) { super(pos, buildBox(), undefined, RED); }
    update() { this.rotation3D.y += .02; }
}

Members

Methods

Inherited from EngineObject

Members

additive : booleanrender3dObject.js:106

Additive blending, in the transparent stage

angleVelocity3D : Vector3render3dObject.js:81

Added to rotation3D each frame by the engine before update, slowed by angleDamping

castShadow : booleanrender3dObject.js:139

Draw into the shadow map when render3D.shadows is on; sprites and cut out textures cast their outline, an object faded below half its alpha casts nothing, a see through one casts only when textured, and additive objects never cast

collideAsSphere3D : booleanrender3dObject.js:142

Collide as the sphere that fits size3D instead of as the size3D box, so it rolls around corners

emissive : numberrender3dObject.js:109

How much it lights itself: 0 is lit as normal, 1 is its own color with no shading, for lamps and glowing things, between is partly self lit, and above 1 is brighter than its color, for bloom

emissiveMap : TextureInfo|undefinedrender3dObject.js:129

A texture of where it glows, added on top of the lit surface so it shows in the dark, like lit windows; read at the color texture's coordinates

emissiveMapColor : Colorrender3dObject.js:131

Multiplies the emissive map, as glTF's emissiveFactor

environment : CubeMap|undefinedrender3dObject.js:135

What it reflects in place of render3D.environment, as a mirror captures the scene from its own middle

groundAngle : numberrender3dObject.js:145

The steepest slope it stands on, in radians from level, PI/4 by default: resting on a solid within this of flat sets groundObject and holds it still, steeper it slides down

localMatrix : Matrix4|undefinedrender3dObject.js:157

The transform from its parent, used in place of pos3D, rotation3D and scale3D when set, for one they cannot hold like a glTF pose with shear; read every frame it is set

matrixParent : EngineObject3D|undefinedrender3dObject.js:163
mesh : Mesh|undefinedrender3dObject.js:84

Mesh to draw

normalMap : TextureInfo|undefinedrender3dObject.js:120

A normal map that bends the surface at each texel so it catches the light like bumps and grooves, green pointing up the image as OpenGL and glTF have it; read at the color texture's coordinates, see normalMapFromHeight to make one in code

normalScale : numberrender3dObject.js:122

How strongly the normal map bends the surface, 0 turns it off, as glTF's scale

pixelated : booleanrender3dObject.js:98

Keep this object's texture pixels hard edged, for pixel art that should not blur or bleed

Position in world space, or relative to the parent while attached to an EngineObject3D, unlike a 2D child's pos, which stays in world space; getWorldPos3D gives a child's world position

receiveShadow : booleanrender3dObject.js:147

Darkened by the shadow map when render3D.shadows is on

reflectivity : numberrender3dObject.js:125

How much it reflects the sky, 0 none and 1 a mirror of it; the edges seen at a glancing angle reflect more either way, as water and glass do

renderAfter2D : boolean|undefinedrender3dObject.js:153

Draw this object over the 2D scene, undefined uses render3D.renderAfter2D

Rotation vec3(pitch, yaw, roll) in radians, relative to the parent while attached to an EngineObject3D, like pos3D

roughness : numberrender3dObject.js:333

How rough the surface is, 0 a mirror to 1 matte, as glTF and three.js's MeshStandardMaterial have it: another way to set shininess, which is what is kept, shininess = 2 / roughness^4 - 2 and at least 1; it sets both the highlight and how blurred a reflection is; read back it is the same up to about .9, above which shininess is 1 and it reads .9

roundLighting : booleanrender3dObject.js:150

A sprite or particle lit as a ball bulging toward the camera, bright on the side toward each light, in place of the same all over; a normal map takes its place

Scale, relative to the parent while attached to an EngineObject3D, like pos3D

shininess : numberrender3dObject.js:115

The highlight's exponent, how small and sharp it is: 4 is broad like rubber, 16 the default, 100 sharp like polished metal; shows only with specular above 0

Size for solid collision and the collect and callback helpers, and of the sprite when there is a tileInfo and no mesh; starts at the size of the mesh's box, or 1 with no mesh, and setMesh leaves it as it is; the box is centered on pos3D, so center() a mesh whose origin is not its middle, like a model standing on its feet, or set size3D; scale3D and any parent's scale grow it, so drawing and picking agree

softShadow : numberrender3dObject.js:94

Diameter of a soft shadow drawn under the object on render3D.softShadowHeight, 0 for none; scale3D and a parent's scale grow it, so set it once for the unscaled object

specular : numberrender3dObject.js:112

Strength of the highlight where the sun and the Light3D objects reflect, 0 is none and 1 adds a light's full color at its brightest; shininess sets its size

sync2D : booleanrender3dObject.js:101

Copy the 2D pos and angle into pos3D and rotation3D each frame, for 2D games with 3D looks; set mass to use 2D physics, and pos3D.z stays yours to set or move with velocity3D.z

transparent : booleanrender3dObject.js:104

Draw in the transparent stage, blended and sorted far to near with depth writes off; on for a sprite

upright : booleanrender3dObject.js:96

A sprite stands on world up instead of tilting toward the camera

Added to pos3D each frame by the engine before update, like the 2D velocity, no super call needed; damping and render3D.gravity act on it once the object has a mass

Methods

addChild(child, localPos, localAngle) → Trender3dObject.js:357

Attaches a child, its pos3D, rotation3D and scale3D taken as the offset from this one; returns child for chaining

  • The 2D offset arguments do nothing for an EngineObject3D child, set its pos3D
child T
localPos Vector2 optional
localAngle number optional
Returns

The child object attached

attach(child) → Trender3dObject.js:373

Attaches a child without moving it: its pos3D, rotation3D and scale3D become what they have to be under this parent to keep its world transform, where addChild takes them as the offset; returns child for chaining

  • A parent scaled unevenly and a child turned under it make a shear, which those three values cannot hold, so the child comes out as close as they can get; a uniform scale is exact
child T
Returns

The child object attached

collideWithVoxel(type, cell) → booleanrender3dObject.js:266

Called by a VoxelMap to ask whether a block stops this object, a hook to let one through or react to it

type number

The block's type, 1 to 255

cell Vector3

The block's cell in the map

Returns
  • true to be stopped by it, every block stops it by default
getForward3D() → Vector3render3dObject.js:312

Returns the direction the object faces, its -Z axis in the world

Returns a copy of the object's world transform, the parent's included when attached to an EngineObject3D

  • The object keeps its matrix and rebuilds it only when its position, rotation or scale changed, so this is cheap to call

Returns the object's right axis in the world

Returns the object's up axis in the world

getWorldPos3D() → Vector3render3dObject.js:270

Returns the world position

getWorldRotation3D() → Vector3render3dObject.js:289

Returns the rotation in the world as vec3(pitch, yaw, roll), the parent's included on a child

  • Under a parent scaled unevenly a turned child is sheared, which no rotation is exactly, so it is the nearest
lookAt(target)render3dObject.js:342

Turn the object so its -Z axis points at a world space target, sets pitch and yaw and clears roll

target Vector3
removeChild(child)render3dObject.js:391

Removes a child from this one, it stays where it is in the world: its pos3D, rotation3D and scale3D become its world values, with the same shear caveat as attach; a child being destroyed is let go as it is

child EngineObject

2D rendering is skipped, the mesh is drawn by render3D during the 3D pass

render3D() → voidrender3dObject.js:422

Draw the object in 3D, called by the 3D pass with the draw state set from this object's flags, draws the mesh by default

setCollision(collideSolidObjects, isSolid, collideLevel, collideRaycast)render3dObject.js:259

Set how this object collides, the same flags as in 2D

  • Solid collision happens in 3D here, against size3D boxes or spheres; a child sits it out
  • The boxes turn with rotation3D; against height maps and voxel maps an object is still its upright box, and against a MeshCollider a capsule
  • A sync2D object collides in 2D instead, against the 2D size, so set that as well as size3D
collideSolidObjects boolean optional, default true

Take part in solid collision

isSolid boolean optional, default true

Block other objects, a pair where neither one blocks passes through; blocking needs collideSolidObjects, so isSolid on its own is not allowed

collideLevel boolean optional, default true

Collide with the level, the height maps, voxel maps and mesh colliders, or the 2D tile layers for a sync2D object

collideRaycast boolean optional, default false

Raycasts, 2D only; 3D has render3D.pick and engineObjectsRaycast3D

setMesh(mesh) → Mesh|undefinedrender3dObject.js:406

Draw a different mesh and free the GPU buffer of the one it replaces

  • For a mesh built again when something changes, like a score, a rebuilt terrain or a loaded model
  • A mesh another object is still drawing is left alone, since builders are often shared
  • Freeing one held somewhere else only costs it an upload, the points it was built from stay
mesh Mesh optional

The mesh to draw from now on, undefined to draw nothing

Returns
  • The mesh passed in
setWorldPos3D(pos)render3dObject.js:275

Move the object so its world position is pos, setting pos3D, which on a child is relative to the parent

  • Exact whatever the parent's turn and scale; a child with a localMatrix has the matrix moved instead
pos Vector3
setWorldRotation3D(rotation)render3dObject.js:296

Turn the object so its rotation in the world is rotation, setting rotation3D, which on a child is relative to the parent; it stays where it is

  • Exact unless the parent, or one above it, is scaled unevenly, the same limit as attach
  • A child with a localMatrix has the matrix built again from its position and scale with the new turn
rotation Vector3

vec3(pitch, yaw, roll) in radians

updatePhysics()render3dObject.js:176

Move by the 3D velocities and push out of solids, called automatically each frame before update, like the 2D physics

  • update runs once every object has moved and collided, so bounce off anything else there, it lands before the draw
  • Override this and call super to change how the object moves itself
  • A sync2D object runs the 2D physics as well, and collides there instead
updateTransforms(updateChildren)render3dObject.js:225

Move a child by its own velocities, bring a sync2D object's pos3D up to its 2D pos, then update the children, called automatically each frame

updateChildren boolean optional, default true

Also update the children's transforms