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, along with render3D.gravity and damping 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, angleDamping, additiveColor, drawSize, mirror, clampSpeed, friction and groundObject
  • 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
  • Its tile and raycast halves are 2D only so they default off here, and 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 pos3D becomes an offset from the parent
  • The 2D offset arguments of addChild do nothing here, set the child's pos3D

Constructor

new EngineObject3D(pos3Dopt, meshopt, tileInfoopt, coloropt)

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

Parameters:
NameTypeAttributesDescription
pos3DVector3<optional>

World space position

meshMesh<optional>

Mesh to draw, undefined draws nothing

tileInfoTileInfo | TextureInfo<optional>

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

colorColor<optional>

Tint

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

Extends

  • EngineObject

Members

additive

Properties
TypeDescription
boolean

Additive blending, in the transparent stage

angleVelocity3D

Properties
TypeDescription
Vector3

Added to rotation3D each frame by the engine before update, angleDamping is 2D only

castShadow

Properties
TypeDescription
boolean

Draw into the shadow map when render3D.shadows is on; sprites and cut out textures cast their outline, additive objects never cast

collideAsSphere3D

Properties
TypeDescription
boolean

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

cullBackFaces

Properties
TypeDescription
boolean

Skip faces that point away from the camera, faster for closed meshes

emissive

Properties
TypeDescription
number

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

mesh :Mesh|undefined

Type:
  • Mesh | undefined
Properties
TypeDescription
Mesh | undefined

Mesh to draw

pixelated

Properties
TypeDescription
boolean

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

pos3D

Properties
TypeDescription
Vector3

World space position, local to the parent when attached to an EngineObject3D

receiveShadow

Properties
TypeDescription
boolean

Darkened by the shadow map when render3D.shadows is on

renderAfter2D :boolean|undefined

Type:
  • boolean | undefined
Properties
TypeDescription
boolean | undefined

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

rotation3D

Properties
TypeDescription
Vector3

Rotation vec3(pitch, yaw, roll) in radians, local to the parent when attached to an EngineObject3D

scale3D

Properties
TypeDescription
Vector3

Scale, local to the parent when attached to an EngineObject3D

size3D

Properties
TypeDescription
Vector3

Size for the collect and callback helpers, and of the sprite when there is a tileInfo and no mesh; scale3D and any parent's scale grow it, so drawing and picking agree

softShadow

Properties
TypeDescription
number

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

Properties
TypeDescription
number

Strength of the highlight where the directional light reflects, 0 is none and 1 adds the light's full color at its brightest; its size is fixed

sync2D

Properties
TypeDescription
boolean

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

Properties
TypeDescription
boolean

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

upright

Properties
TypeDescription
boolean

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

velocity3D

Properties
TypeDescription
Vector3

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

getForward3D() → {Vector3}

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

Returns:
Type: 
Vector3

getMatrix() → {Matrix4}

Returns the object's world transform, relative to the parent's when attached to an EngineObject3D

Returns:
Type: 
Matrix4

getRight3D() → {Vector3}

Returns the object's right axis in the world

Returns:
Type: 
Vector3

getUp3D() → {Vector3}

Returns the object's up axis in the world

Returns:
Type: 
Vector3

getWorldPos3D() → {Vector3}

Returns the world position

Returns:
Type: 
Vector3

lookAt(target)

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

Parameters:
NameTypeDescription
targetVector3

render()

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

render3D()

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(collideSolidObjectsopt, isSolidopt, collideTilesopt, collideRaycastopt)

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
  • A sync2D object collides in 2D instead, against the 2D size, so set that as well as size3D
Parameters:
NameTypeAttributesDefaultDescription
collideSolidObjectsboolean<optional>
true

Take part in solid collision

isSolidboolean<optional>
true

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

collideTilesboolean<optional>
false

Tile collision, 2D only so it needs sync2D

collideRaycastboolean<optional>
false

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

setMesh(meshopt) → {Mesh|undefined}

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
Parameters:
NameTypeAttributesDescription
meshMesh<optional>

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

Returns:
  • The mesh passed in
Type: 
Mesh | undefined

updatePhysics()

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()

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