Skip to content

Commit

Permalink
New engine built up for fungi. Cleaning up old code into a folder for…
Browse files Browse the repository at this point in the history
… the time being.
  • Loading branch information
sketchpunk committed Jan 17, 2019
1 parent 73ef1b1 commit 90fbf72
Show file tree
Hide file tree
Showing 123 changed files with 1,909 additions and 1,323 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 0 additions & 13 deletions ecs.txt

This file was deleted.

3 changes: 2 additions & 1 deletion fungi/engine/App.js
Expand Up @@ -15,7 +15,8 @@ import InputSystem from "./ecs/InputSystem.js";

/*
System Notes
001 - Input
001 - Input
100 - Misc : DynamicVerts
700 - Physics
800 - Transform
801 - Camera
Expand Down
2 changes: 1 addition & 1 deletion fungi/engine/Debug.js
Expand Up @@ -10,7 +10,7 @@ let eLine, ePoint;

//#########################################################################
class Debug{
static init( ecs, priority=1 ){
static init( ecs, priority=100 ){
ePoint = DVerts.build( App.newDraw( "Debug_Point" ), "Debug_Points", "VecWColor", 0 );
eLine = DVerts.build( App.newDraw( "Debug_Line" ), "Debug_Lines", "VecWColor", 1 );
DynamicVertsSystem.init( ecs, priority );
Expand Down
20 changes: 13 additions & 7 deletions fungi/engine/_test_page.html
@@ -1,7 +1,7 @@
<!DOCTYPE html><html><head><title>Fungi</title></head><script type="module">

import App from "./App.js";
//import FacedCube from "../primitives/FacedCube.js";
import FacedCube from "../primitives/FacedCube.js";
//import DVerts, { DynamicVertsSystem } from "./ecs/DynamicVerts.js";
//import Debug from "./Debug.js";

Expand All @@ -10,7 +10,7 @@
App.launch( onDraw, [
{ type:"shader", file:"/fungi/shaders/VecWColor.txt" },
]) //.then( ()=>App.loadModules( "./Debug.js" ) )
.then( ()=>App.loadScene( true ) )
.then( ()=>App.loadScene( false ) )
.then( ()=>init() )
.catch( (err)=>console.error( err ) );

Expand All @@ -23,10 +23,15 @@
.setRotAxis( [1,0,0], -15 * Math.PI / 180 );


App.debug.rawPoint(0, 0.5, 0, 2);
App.debug.line( [0,0,0], [0,0.5,0], 0 );
//App.debug.rawPoint(0, 0.5, 0, 2);
//App.debug.line( [0,0,0], [0,0.5,0], 0 );

cube = FacedCube("Cube");
cube2 = FacedCube("Cube2");
cube2.Node.setPos(0,1,0).setScl( 0.5 );

App.node.addChild( cube, cube2 );

//cube = FacedCube("Cube");
//let gfloor = GridFloor();

//let e = DVerts.build( App.newDraw( "DVE" ), "Points", "VecWColor", 1 );
Expand All @@ -46,10 +51,11 @@
App.loopState( true );
}

let cube;
let cube, cube2;
function onDraw( dt, ss ){

//cube.Node.setRotAxis( [0,1,0], Math.sin( ss ) * Math.PI );
cube.Node.setRotAxis( [0,1,0], Math.sin( ss ) * Math.PI );
cube2.Node.setRotAxis( [0,1,0], Math.sin( ss ) * Math.PI );

App.ecs.updateSystems();
}
Expand Down
2 changes: 1 addition & 1 deletion fungi/engine/ecs/DynamicVerts.js
Expand Up @@ -112,7 +112,7 @@ class DynamicVerts{
const QUERY_COM = ["DynamicVerts"];

class DynamicVertsSystem extends System{
static init( ecs, priority = 1 ){ ecs.addSystem( new DynamicVertsSystem(), priority ); }
static init( ecs, priority = 100 ){ ecs.addSystem( new DynamicVertsSystem(), priority ); }

constructor(){ super(); }
update( ecs ){
Expand Down
170 changes: 158 additions & 12 deletions fungi/engine/ecs/Node.js
@@ -1,37 +1,99 @@
/**
* @module fungi/engine/ecs/Node
* @version 1.0.0
* @author Pedro S. <sketchpunk@gmail.com>
*/

import { Components, System } from "../Ecs.js";
import Transform from "../../maths/Transform.js";
import Mat4 from "../../maths/Mat4.js";
import Quat from "../../maths/Quat.js";
import Vec3 from "../../maths/Vec3.js";

//#########################################################################

/** ECS Component to handle Transform Heirarchy of Entities */
class Node{
/**
* Create a Node Component
*/
constructor(){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Position / Rotation / Scale
this.isModified = true;
this.local = new Transform();
this.world = new Transform();
this.modelMatrix = new Mat4();
// Position / Rotation / Scale

/**
* A flag to tell systems that data has been modified and requires updating
* @public @type {boolean}
*/
this.isModified = true;

/**
* Local space transform data
* @public @type {Transform}
*/
this.local = new Transform();

/**
* World space transform data, calculated by NodeSystem Update based on Transform Hierachy
* @private @type {Transform}
*/
this.world = new Transform();

/**
* Model matrix used in shaders. Calculated by NodeSystem based on world space transform data
* @private @type {Mat4}
*/
this.modelMatrix = new Mat4();


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Heirachy
this.level = 0; // What level in the Hierachy
this.parent = null; // Entity reference of the parent
this.children = []; // List of Children Entities

/**
* Cache at what level of the hierachy the node exists. Used for Sorting in NodeSystem.
* @private @type {number}
*/
this.level = 0;

/**
* Reference to the Entity Parent. May be null if there is no parent.
* @private @type {?Entity}
*/
this.parent = null;

/**
* List to references of Entity Children
* @private @type {Entity[]}
*/
this.children = [];
}


////////////////////////////////////////////////////////////////////
// SETTERS
////////////////////////////////////////////////////////////////////
/**
* Set local space position and sets modified to true
* @param {(number|Vec3)} x - X local position or Vec3 to set x,y,z
* @param {?number} y - Y Local position
* @param {?number} z - Z Local position
* @public @return {Node}
*/
setPos( x, y, z ){
if( arguments.length == 1 ) this.local.pos.copy( x );
else this.local.pos.set( x, y, z);
else this.local.pos.set( x, y, z );

this.isModified = true;
return this;
}

/**
* Add values to the local position and sets modified to true
* @param {(number|Vec3)} x - X local position or Vec3 to set x,y,z
* @param {?number} y - Y Local position
* @param {?number} z - Z Local position
* @public @return {Node}
*/
addPos( x, y, z ){
if( arguments.length == 1 ) this.local.pos.add( x );
else{
Expand All @@ -43,16 +105,43 @@ class Node{
return this;
}

/**
* Set rotation based on an Axis and angle in radians
* @param {Vec3} axis - Unit vector of an axis of rotation
* @param {number} ang - Angle in radians
* @public @return {Node}
*/
setRotAxis( axis, ang ){
this.local.rot.setAxisAngle( axis, ang );
this.isModified = true;
return this;
}

/**
* Sets local scale and sets modified to true
* @param {number} x - X local scale, if the only value passed in then it will scale all axises by X
* @param {?number} y - Y Local scale
* @param {?number} z - Z Local scale
* @public @return {Node}
*/
setScl( x, y, z ){
if( arguments.length == 1 ) this.local.scl.set( x, x, x );
else this.local.scl.set( x, y, z );

this.isModified = true;
return this;
}


////////////////////////////////////////////////////////////////////
// Child Management
////////////////////////////////////////////////////////////////////
/**
* Create a Parent-Child relationship between two Entities
* @param {Entity} pe - Parent Entity
* @param {Entity} ce - Child Entity
* @public @return {Node}
*/
static addChild( pe, ce ){
let pn = pe.Node;

Expand All @@ -75,9 +164,15 @@ class Node{
//if child has its own children, update their level values
if(cn.children.length > 0) updateChildLevel( cn );

return TransformNode;
return Node;
}

/**
* Remove the Parent-Child relationship between two entities.
* @param {Entity} pe - Parent Entity
* @param {Entity} ce - Child Entity
* @public @return {Node}
*/
static removeChild( pe, ce ){
var idx = pe.Node.children.indexOf( ce );

Expand All @@ -97,6 +192,13 @@ class Node{
////////////////////////////////////////////////////////////////////
// WORLD SPACE
////////////////////////////////////////////////////////////////////
/**
* Get direction of an Entity based on its World Rotation
* @param {Entity} e - Entity that you want to get direction for.
* @param {number} [dir=0] - Which direction you want, Forward:0, Left:1, Up:2
* @param {?Vec3} [out=null] - Pass in a Vec3, else one will be created.
* @public @return {Vec3}
*/
static getDir( e, dir=0, out=null ){
let q = Node.getWorldRot( e );
out = out || new Vec3();
Expand All @@ -109,6 +211,12 @@ class Node{
return out;
}

/**
* Get the world rotation of an entity
* @param {Entity} e - Entity you want get the rotation of.
* @param {?Quat} [out=null] - Passin a Quaternion Reference, else a new one will be created.
* @public @return {Quat}
*/
static getWorldRot( e, out = null ){
out = out || new Quat();

Expand Down Expand Up @@ -155,13 +263,30 @@ class Node{
//#########################################################################
const QUERY_COM = [ "Node" ];


/**
* ECS System that handles updating Transform Hierachy data along with create ModelMatrix based on World Space Transform
* @extends System
*/
class NodeSystem extends System{

/**
* Setup the system automaticly to an ECS reference
* @param {Ecs} ecs - Instance of an Ecs object
* @param {number} [priority=] - What the priority the system has compared to others during update.
* @param {number} [priority2=] - Priority for the second system that handles cleanup for Nodes.
*/
static init( ecs, priority = 800, priority2 = 1000 ){
ecs.addSystem( new NodeSystem(), priority );
ecs.addSystem( new NodeCleanupSystem(), priority2 );
}

constructor(){ super(); }

/**
* System Update
* @param {Ecs} ecs
*/
update( ecs ){
let e, // Entity
cn, // Child Node ( only if parent node exists )
Expand Down Expand Up @@ -190,8 +315,17 @@ class NodeSystem extends System{


//#########################################################################
/**
* ECS System that handles updating Transform Hierachy data along with create ModelMatrix based on World Space Transform
* @extends System
*/
class NodeCleanupSystem extends System{
constructor(){ super(); }

/**
* System Update
* @param {Ecs} ecs
*/
update(ecs){
let e, ary = ecs.queryEntities( QUERY_COM, thSort );
for( e of ary ) if( e.Node.isModified ) e.Node.isModified = true;
Expand All @@ -200,7 +334,15 @@ class NodeCleanupSystem extends System{


//#########################################################################
//Compare function to sort entities based on the level of the hierarchy.
// HELPER

/**
* Compare function to sort entities based on the level of the hierarchy.
* @param {Entity} a
* @param {Entity} b
* @return {number}
* @private
*/
function thSort( a, b ){
//Sort by Hierarachy Levels so parents are calculated before children
let lvlA = a.Node.level,
Expand All @@ -211,7 +353,11 @@ function thSort( a, b ){
else return 1; // A > B
}


/**
* Update the level of all the child nodes of node
* @param {node} n
* @private
*/
function updateChildLevel( n ){
let c, cn;
for(c of n.children){
Expand Down

0 comments on commit 90fbf72

Please sign in to comment.