@unithree/core
v1.0.2
Published
Unity3D inspired ThreeJS framework
Downloads
3
Maintainers
Readme
Unithree
Overview
A 3D framework built on the highly performant ThreeJS with the intention of being easy to pickup for users of Unity 3D. This is not a recreation of Unity 3D but a highly extensible framework inspired by it and entity/component models.
Documentation
Setup
This is a ThreeJS based project so when installing via NPM please use:
npm install three @types/three @unithree/core
Core Concepts
Unithree is built around a central state that houses the frame loop. This provides the processing of
Entities calling their OnStart
, OnUpdate
, OnLateUpdate
, and OnDestroy
events.
An Entity
can be basic, just providing a means to ensure any Object3D
is can respond to the core events.
Entities can also have Components attached.
A Component
is a simple interface only requiring a reference to their parent Entity. Beyond that the implementation
is up to the Component
. This means you can add any functionality to the parent Entity
, such as physics,
interaction, animations, etc...
Finally, we have ProcessorPlugins. A ProcessorPlugin
is meant to add whatever functionality you desire to the main
loop. This is done via initialize
, update
, lateUpdate
, and dispose
events.
This can include the processing of specific custom Component
types.
With these simple pieces you can build a robust real-time interactive application on top of one of the most widely used WebGL rendering engines.
Library
Unithree State
The state maintains the scene, active camera, and renderer elements. It is responsible for calling the core events for
Entity
and ProcessorPlugin
types.
initialize
- Initializes the Unithree state clearing anything previously in the state. This takes optional camera camera and renderer options returning the HTML Canvas Element. By default, a newPerspectiveCamera
and aWebGlRenderer
will be created.start
- Starts the frame loop.stop
- Stops the frame loop.isPaused
- This property gets and sets the paused state of the system. This is reported to everyProcessorPlugin
andEntity
.addPlugin
- Adds 1 or moreProcessorPlugin
objects into the system.clearPlugins
- Clears all plugins added to the system. This will call dispose on all plugins.getPluginByTypeName
- Looks for a plugin by the name of the class (e.g. "Input") and will return it or null if the requested plugin was not found.getScene
- Gets the scene containing allThreeJS
objects includingEntity
objects. (Do NOT perform scene management here. Object lifecycle should be handled by the state. SeeinstantiateObject
andEntity.destroy
)getCamera
- Gets the active camera of the system.setCamera
- Sets the active camera of the system.getRenderer
- Gets the current renderer.getClock
- Gets the clock maintained by the system. NOTE: the time is given to all update events so this is not recommended for direct useinstantiateObject
- Creates a new object and adds it to the scene root or optionalObject3D
orEntity
.findObjectByName
- Finds the first object orEntity
that has a matching name in the scene hierarchy.findObjectsByName
- Finds the all objects or entities that have a matching name in the scene hierarchy.findEntityByName
- Finds the firstEntity
that has a matching name in the scene hierarchy.findEntitiesByName
- Finds the allEntity
objects that have a matching name in the scene hierarchy.getEntities
- Gets a list of allEntity
objects tracked by the system.dispose
- Stops and cleans up the system ensuring dispose id called on allEntity
andProcessorPlugin
objects.
Processor Plugin
Meant to extend the functionality of the core render loop. These are also used to process custom Component
objects.
enabled
- A flag determining whether the plugin is enabled.initialized
- A flag representing whether the plugin has initialized.initialize
- Executed one time when the plugin starts. This will NOT occur unless the state has been initialized and started.update
- Executed once per frame.lateUpdate
- Executed after allProcessorPlugin
andEntity
object updates.dispose
- Executed when the system state shutdown has been requested. Use this for clean up of itself and any
controlledComponent
objects if they need it.
Entity
Extending Object3D
this object provides state-based events and allows them to attach Component
objects
didStart
- A flag representing whether theEntity
has started.isDead
- Get whether theEntity
has died.enabled
- A flag representing whether theEntity
is enabled.components
- Gets a list of the attachedComponent
objects.addComponents
- Add a 1 or moreComponent
objects to anEntity
. Auto-sets theEntity
/Component
relationship.destroy
- Tells the system to destroy theEntity
on the next update.OnStart
- Event called once on anEntity
when it starts.OnUpdate
- Event once per frame post render.OnLateUpdate
- Event called once per frame after the update event.OnDestroy
- Event called when the system processed theEntity
death. (Use for any cleanup)
Component
Interface requiring an entity as its parent object. A Component
can add any extra processing and events supported
by a ProcessorPlugin
. An Entity
does NOT know nor assume it has any Component
objects by default.
Input Plugin
Plugin that provides an easy way to process input without setting up your own input handlers. The Input
plugin
provides support for Keyboard, Mouse, Pen, Touch, and Gamepads
Input
getPointerCoordinates
- Get the specified pointer coordinates in reference to the windowgetPointerDelta
- Gets the pointer coordinate delta since last framegetPrimaryPointerState
- Gets the primary pointer state object for the specified input typegetPointerStates
- Gets a list of pointer states for the given input typetouchCount
- Gets the number of touches currently active in the systemgetMouseScrollDelta
- Gets the delta of the mouse scroll wheel since last framemouseScrollDeltaMode
- Gets the mode of the scroll wheelgetGamepad
- Gets the gamepad state object for the specified player indexgetGamepads
- Gets the active gamepad state objectsgetKeyHeld
- True if the key has been held but not pressed this framegetKeyPressed
- True if the key has been pressed this framegetKeyDown
- True if the key was pressed or is being heldgetKeyUp
- True if the key was released this frame
Pointer State
type
- The type of the inputisPrimary
- True if the pointer is the primary pointer of its typecoordinates
- The pointer's coordinates in reference to the windowdelta
- The pointer's coordinate delta this framebuttonStates
- Map of pointer buttons to stategetButtonState
- Gets the state of the specific pointer buttonsetButtonState
- Sets the state of the specific pointer buttongetButtonHeld
- True if the button is held but has not been pressed this framegetButtonPressed
- True if the button is pressed this framegetButtonDown
- True if the button was pressed or is heldgetButtonUp
- True if the button was released this frame
Gamepad State
playerIndex
- The index for which player the gamepad is associated withlastUpdated
- The timestamp of the most recent updatetotalAxes
- The number of axes available on the gamepadtotalButtons
- The number of buttons available on the gamepadhasStandardMapping
- Whether or not standard mappings are supported. (This is commonly PlayStation and XBox controllers)buttonStates
- Gets a map of buttons to stategetAxis
- Gets the axis value from -1 to 1 of the specified axisgetThumbstickValue
- Gets aVector2
of the thumb stick on a standard gamepadgetButtonTouchedState
- Gets the state foa button if it is being touched (capacitive buttons only otherwise it matches button state)getButtonValue
- Gets the value of the specified button between 0 and 1getButtonState
- Gets the state of the specified buttonsetButtonState
- Sets the state of the specified buttongetButtonHeld
- True if the button is being held but eas not pressed this framegetButtonPressed
- True if the button was pressed this framegetButtonDown
- True if the button was pressed or is being heldgetButtonUp
- True if the button was released this frame
Misc
ButtonState
- Pressed, Held or ReleasedPointerButton
- Primary, Secondary or AuxiliaryInputType
- Mouse, Pen or TouchThumbStick
- Left or Right (Used for standard controllers)PlaystationButtonMapping
- Mapping for a PlayStation controllerXBoxButtonMapping
- Mapping for an XBox controllerSwitchButtonMapping
- Mapping for a Nintendo Switch Controller
Math Utility Functions
Small set of Utility functions related to Math
EPSILON
- A value of 10-5.PI_2
- A value of PI * 2.PI_HALF
- A value of PI / 2.FPS_60
- A value representing 60 FPS.ORIGIN
- AVector3
representing the origin.AXIS_X
- AVector3
representing the X-axis.AXIS_Y
- AVector3
representing the Y-axis.AXIS_Z
- AVector3
representing the Z-axis.approximatelyZero
- Compares a number with approximately zero.approximatelyEquals
- Compares two numbers for approximate equality.sphericalEquals
- Compares twoSpherical
objects for equality.approximatelySphericalEquals
- Compares twoSpherical
objects for approximate equality.approximatelyVector3Equals
- Compares twoVector3
objects for approximate equality.
Unity 3D to Unithree Concepts
Below is a quick guide to aid those familiar with Unity 3D
concepts make the connection to similar concepts in
Unithree.
Prefabs and Scripts
Since a prefab is an easily instantiated preconfigured game object, with set values, children, components, etc...
the best way to replicate this is making a new class and extending the Entity
class. In this new class, you
would create and add children and components in the constructor so that these objects are always associated.
As for adding custom scripting, such as adding a MonoBehaviour
, the custom new Entity
class can have the
behaviors built into it.
Components
In Unity 3D
adding a component extends the game object adding new functionality, such as adding physics,
collision data, and more allowing for the system to perform extra handling on the object. In Unithree components
work the same way. They however, require a ProcessorPlugin
or implementation within an Entity
to cause them to
be executed by the core system. A ProcessorPlugin
is the preferred implementation as this makes the component work
the same across all Entity
objects versus just a specific class and its inheritors, but sometimes this may be the
best option.
Object Lifecycle Management
In Unity 3D
the only way to instantiate an object into the scene via code was to use the Object.Instantiate
method and to destroy was calling an objects Object.Destroy
method. Similarly, this is how we handle things in
Unithree. Using the Unithree state's instantiateObject
and the destroy
method on the Entity objects.
If you do not use this lifecycle for Entity
based objects the Entity
may not be processed. All standard ThreeJS
objects can be added and removed however you see fit. WARNING: If a standard ThreeJS
object has a child Entity
not using the built-in lifecycle management functions you can have undesired behavior.