make faces and vertexes implementation independent

beta.r3js.org
-=yb4f310 2017-09-02 12:54:49 +02:00
parent 69a0556424
commit c5bb684272
3 changed files with 72 additions and 42 deletions

View File

@ -11,7 +11,6 @@
* @param vertexColors * @param vertexColors
* @param vertexNormals * @param vertexNormals
* @param normal * @param normal
* @param parentEntity GameLib.Entity
* @constructor * @constructor
*/ */
GameLib.D3.API.Face = function( GameLib.D3.API.Face = function(
@ -84,6 +83,10 @@ GameLib.D3.API.Face = function(
this.normal = normal; this.normal = normal;
}; };
/**
* We don't inherit from component - it makes the entitymanager too heavy - all faces end up in the register etc..
*/
// GameLib.D3.API.Face.prototype = Object.create(GameLib.Component.prototype); // GameLib.D3.API.Face.prototype = Object.create(GameLib.Component.prototype);
// GameLib.D3.API.Face.prototype.constructor = GameLib.D3.API.Face; // GameLib.D3.API.Face.prototype.constructor = GameLib.D3.API.Face;

View File

@ -1,15 +1,22 @@
/** /**
* Face * Face
* @constructor * @constructor
* @param graphics * @param implementation
* @param apiFace * @param apiFace
*/ */
GameLib.D3.Face = function Face( GameLib.D3.Face = function Face(
graphics, implementation,
apiFace apiFace
) { ) {
this.graphics = graphics;
this.graphics.isNotThreeThrow(); this.implementation = implementation;
if (implementation instanceof GameLib.D3.Graphics) {
this.implementation.isNotThreeThrow();
} else if (implementation instanceof GameLib.D3.Physics) {
this.implementation.isNotCannonThrow();
} else {
throw new Error('Unhandled implementation : ' + implementation);
}
if (GameLib.Utils.UndefinedOrNull(apiFace)) { if (GameLib.Utils.UndefinedOrNull(apiFace)) {
apiFace = {}; apiFace = {};
@ -34,27 +41,33 @@ GameLib.D3.Face = function Face(
apiFace.normal apiFace.normal
); );
this.color = new GameLib.Color( if (this.implementation instanceof GameLib.D3.Graphics) {
this.graphics, /**
this.color, * physics faces have no color... a little sad right?
this */
); this.color = new GameLib.Color(
this.implementation,
this.color,
this
);
this.vertexColors = this.vertexColors.map(function(vertexColor){ this.vertexColors = this.vertexColors.map(function(vertexColor){
if (vertexColor instanceof GameLib.Color) { if (vertexColor instanceof GameLib.Color) {
return vertexColor; return vertexColor;
} }
if (vertexColor instanceof GameLib.API.Color) { if (vertexColor instanceof GameLib.API.Color) {
return new GameLib.Color( return new GameLib.Color(
this.graphics, this.implementation,
vertexColor, vertexColor,
this this
) )
} }
console.warn('unknown vertex color type', vertexColor); console.warn('unknown vertex color type', vertexColor);
}.bind(this)); }.bind(this));
}
this.vertexNormals = this.vertexNormals.map(function(vertexNormal){ this.vertexNormals = this.vertexNormals.map(function(vertexNormal){
if (vertexNormal instanceof GameLib.Vector3) { if (vertexNormal instanceof GameLib.Vector3) {
@ -73,7 +86,7 @@ GameLib.D3.Face = function Face(
}.bind(this)); }.bind(this));
this.normal = new GameLib.Vector3( this.normal = new GameLib.Vector3(
this.graphics, this.implementation,
this.normal, this.normal,
this this
); );
@ -111,9 +124,15 @@ GameLib.D3.Face.prototype.toApiObject = function() {
); );
}; };
GameLib.D3.Face.FromObject = function(graphics, objectFace) { /**
* @param implementation
* @param objectFace
* @returns {GameLib.D3.Face}
* @constructor
*/
GameLib.D3.Face.FromObject = function(implementation, objectFace) {
return new GameLib.D3.Face( return new GameLib.D3.Face(
graphics, implementation,
GameLib.D3.API.Face.FromObject(objectFace) GameLib.D3.API.Face.FromObject(objectFace)
); );
}; };

View File

@ -1,15 +1,21 @@
/** /**
* Runtime Vertex * Runtime Vertex
* @constructor * @constructor
* @param graphics * @param implementation
* @param apiVertex * @param apiVertex
*/ */
GameLib.D3.Vertex = function Vertex( GameLib.D3.Vertex = function Vertex(
graphics, implementation,
apiVertex apiVertex
) { ) {
this.graphics = graphics; this.implementation = implementation;
this.graphics.isNotThreeThrow(); if (implementation instanceof GameLib.D3.Graphics) {
this.implementation.isNotThreeThrow();
} else if (implementation instanceof GameLib.D3.Physics) {
this.implementation.isNotCannonThrow();
} else {
throw new Error('Unhandled implementation : ' + implementation);
}
if (GameLib.Utils.UndefinedOrNull(apiVertex)) { if (GameLib.Utils.UndefinedOrNull(apiVertex)) {
apiVertex = {}; apiVertex = {};
@ -26,19 +32,21 @@ GameLib.D3.Vertex = function Vertex(
); );
this.position = new GameLib.Vector3( this.position = new GameLib.Vector3(
this.graphics, this.implementation,
this.position, this.position,
null null
); );
this.boneWeights = this.boneWeights.map( if (implementation instanceof GameLib.D3.Graphics) {
function(apiBoneWeight) { this.boneWeights = this.boneWeights.map(
return new GameLib.D3.BoneWeight( function(apiBoneWeight) {
this.graphics, return new GameLib.D3.BoneWeight(
apiBoneWeight this.implementation,
) apiBoneWeight
}.bind(this) )
) }.bind(this)
)
}
}; };
GameLib.D3.Vertex.prototype = Object.create(GameLib.D3.API.Vertex.prototype); GameLib.D3.Vertex.prototype = Object.create(GameLib.D3.API.Vertex.prototype);
@ -61,19 +69,19 @@ GameLib.D3.Vertex.prototype.toApiObject = function() {
/** /**
* Returns a GameLib.D3.Vertex from a vertex Object * Returns a GameLib.D3.Vertex from a vertex Object
* @param graphics GameLib.D3.Graphics * @param implementation (graphics or physics object)
* @param objectVertex Object * @param objectVertex Object
* @returns {GameLib.D3.Vertex} * @returns {GameLib.D3.Vertex}
* @constructor * @constructor
*/ */
GameLib.D3.Vertex.FromObject = function( GameLib.D3.Vertex.FromObject = function(
graphics, implementation,
objectVertex objectVertex
) { ) {
var apiVertex = GameLib.D3.API.Vertex.FromObject(objectVertex); var apiVertex = GameLib.D3.API.Vertex.FromObject(objectVertex);
return new GameLib.D3.Vertex( return new GameLib.D3.Vertex(
graphics, implementation,
apiVertex apiVertex
); );
}; };