many updates - starting to make stable

beta.r3js.org
Theunis J. Botha 2016-11-18 16:00:13 +01:00
parent eefd4a9603
commit be64682142
9 changed files with 784 additions and 468 deletions

View File

@ -1,20 +1,227 @@
/** /**
* Creates a camera object * Creates a camera object
* @param graphics * @param graphics GameLib.D3.Graphics
* @param apiCamera GameLib.D3.Camera.API
* @constructor * @constructor
*/ */
GameLib.D3.Camera = function(graphics) { GameLib.D3.Camera = function(
graphics,
apiCamera
) {
for (var property in apiCamera) {
if (apiCamera.hasOwnProperty(property)) {
this[property] = apiCamera[property];
}
}
this.graphics = graphics; this.graphics = graphics;
this.graphics.isNotThreeThrow(); this.graphics.isNotThreeThrow();
this.instance = this.createInstance(); this.instance = this.createInstance();
this.needsUpdate = false;
} ; } ;
/**
* Raw Camera API object - should always correspond with the Camera Schema
* @param id
* @param name
* @param cameraType GameLib.D3.Camera.CAMERA_TYPE_*
* @param fov
* @param aspect
* @param near
* @param far
* @param position GameLib.D3.Vector3
* @param lookAt GameLib.D3.Vector3
* @param minX
* @param maxX
* @param minY
* @param maxY
* @param minZ
* @param maxZ
* @constructor
*/
GameLib.D3.Camera.API = function(
id,
cameraType,
name,
fov,
aspect,
near,
far,
position,
lookAt,
minX,
maxX,
minY,
maxY,
minZ,
maxZ
) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(cameraType)) {
cameraType = GameLib.D3.Material.CAMERA_TYPE_PERSPECTIVE;
}
this.cameraType = cameraType;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Camera (' + cameraType + ')';
}
this.name = name;
if (GameLib.D3.Utils.UndefinedOrNull(fov)) {
fov = 75;
}
this.fov = fov;
if (GameLib.D3.Utils.UndefinedOrNull(aspect)) {
aspect = window.innerWidth / window.innerHeight;
}
this.aspect = aspect;
if (GameLib.D3.Utils.UndefinedOrNull(near)) {
near = 0.1;
}
this.near = near;
if (GameLib.D3.Utils.UndefinedOrNull(far)) {
far = 1000;
}
this.far = far;
if (GameLib.D3.Utils.UndefinedOrNull(position)) {
position = new GameLib.D3.Vector3(
15,
15,
15
);
}
this.position = position;
if (GameLib.D3.Utils.UndefinedOrNull(lookAt)) {
lookAt = new GameLib.D3.Vector3(
0,
0,
0
);
}
this.lookAt = lookAt;
if (GameLib.D3.Utils.UndefinedOrNull(minX)) {
minX = -100;
}
this.minX = minX;
if (GameLib.D3.Utils.UndefinedOrNull(maxX)) {
maxX = 100;
}
this.maxX = maxX;
if (GameLib.D3.Utils.UndefinedOrNull(minY)) {
minY = -100;
}
this.minY = minY;
if (GameLib.D3.Utils.UndefinedOrNull(maxY)) {
maxY = 100;
}
this.maxY = maxY;
if (GameLib.D3.Utils.UndefinedOrNull(minZ)) {
minZ = -100;
}
this.minZ = minZ;
if (GameLib.D3.Utils.UndefinedOrNull(maxZ)) {
maxZ = 100;
}
this.maxZ = maxZ;
};
/**
* Camera types
* @type {number}
*/
GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE = 0x1;
GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL = 0x2;
GameLib.D3.Camera.CAMERA_TYPE_STEREO = 0x3;
/** /**
* Creates a camera instance of 'graphics' type (only THREE for now) * Creates a camera instance of 'graphics' type (only THREE for now)
* @returns {THREE.Camera} * @returns {THREE.Camera}
*/ */
GameLib.D3.Camera.prototype.createInstance = function() { GameLib.D3.Camera.prototype.createInstance = function(update) {
return new this.graphics.instance.Camera();
var instance = null;
if (update) {
instance = this.instance;
}
if (!instance ||
(instance && this.cameraType != instance.cameraType)
) {
if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE) {
instance = new this.graphics.instance.PerspectiveCamera(
this.fov,
this.aspect,
this.near,
this.far
);
} else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
instance = new this.graphics.instance.OrthographicCamera(
this.minX,
this.maxX,
this.minY,
this.maxY,
this.minZ,
this.maxZ
);
}
}
if (update && this.cameraType == instance.cameraType) {
instance.minX = this.minX;
instance.maxX = this.maxX;
instance.minY = this.minY;
instance.maxY = this.maxY;
instance.minZ = this.minZ;
instance.maxZ = this.maxZ;
instance.fov = this.fov;
instance.aspect = this.aspect;
instance.near = this.near;
instance.far = this.far;
}
if (update) {
instance.cameraType = this.cameraType;
}
instance.position.x = this.position.x;
instance.position.y = this.position.y;
instance.position.z = this.position.z;
instance.lookAt(
new this.graphics.instance.Vector3(
this.lookAt.x,
this.lookAt.y,
this.lookAt.z
)
);
instance.updateProjectionMatrix();
return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Camera.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
}; };

View File

@ -11,4 +11,17 @@ GameLib.D3.Color = function(r, g, b, a) {
this.g = g; this.g = g;
this.b = b; this.b = b;
this.a = a; this.a = a;
};
/**
* Converts the current color to HTML hex format (ex. #ffffff)
* @returns {string}
*/
GameLib.D3.Color.prototype.toHex = function() {
var rf = Math.floor(this.r == 1? 255 : this.r * 256.0).toString(16);
var gf = Math.floor(this.g == 1? 255 : this.g * 256.0).toString(16);
var bf = Math.floor(this.b == 1? 255 : this.b * 256.0).toString(16);
return '#' + rf + gf + bf;
}; };

View File

@ -21,7 +21,7 @@ GameLib.D3.ImageFactory = function (graphics) {
return promiseList[url]; return promiseList[url];
} }
var defer = q.defer(); var defer = Q.defer();
promiseList[url] = defer.promise; promiseList[url] = defer.promise;

View File

@ -1,12 +1,10 @@
/** /**
* Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created
* @param id
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param apiLight GameLib.D3.Light.API * @param apiLight GameLib.D3.Light.API
* @constructor * @constructor
*/ */
GameLib.D3.Light = function( GameLib.D3.Light = function(
id,
graphics, graphics,
apiLight apiLight
) { ) {
@ -73,11 +71,18 @@ GameLib.D3.Light.API = function(
} }
this.name = name; this.name = name;
if (GameLib.D3.Utils.UndefinedOrNull(color)) {
color = new GameLib.D3.Color(1,1,1,1);
}
this.color = color; this.color = color;
if (GameLib.D3.Utils.UndefinedOrNull(intensity)) {
intensity = 1;
}
this.intensity = intensity; this.intensity = intensity;
if (typeof position == 'undefined') { if (typeof position == 'undefined') {
position = new GameLib.D3.Vector3(0,0,0); position = new GameLib.D3.Vector3(0,10,0);
} }
this.position = position; this.position = position;
@ -140,34 +145,72 @@ GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4;
* Creates a light instance * Creates a light instance
* @returns {*} * @returns {*}
*/ */
GameLib.D3.Light.createInstance = function() { GameLib.D3.Light.prototype.createInstance = function(update) {
var instance = null; var instance = null;
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) { if (update) {
instance = new this.graphics.instance.AmbientLight(this.color, this.intensity); instance = this.instance;
} }
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { if ((!instance) ||
instance = new this.graphics.instance.DirectionalLight(this.color, this.intensity); (instance && instance.lightType != this.lightType)) {
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) { if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) {
instance = new this.graphics.instance.PointLight(this.color, this.intensity); instance = new this.graphics.instance.AmbientLight(
instance.distance = this.distance; new this.graphics.instance.Color(
instance.decay = this.decay; this.color.r,
} this.color.g,
this.color.b
),
this.intensity
);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) { if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
instance = new this.graphics.instance.SpotLight(this.color, this.intensity); instance = new this.graphics.instance.DirectionalLight(
instance.distance = this.distance; new this.graphics.instance.Color(
instance.angle = this.angle; this.color.r,
instance.penumbra = this.penumbra; this.color.g,
instance.decay = this.decay; this.color.b
),
this.intensity
);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) {
instance = new this.graphics.instance.PointLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
instance.distance = this.distance;
instance.decay = this.decay;
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) {
instance = new this.graphics.instance.SpotLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
instance.distance = this.distance;
instance.angle = this.angle;
instance.penumbra = this.penumbra;
instance.decay = this.decay;
}
} }
instance.gameLibObject = this; instance.gameLibObject = this;
instance.name = this.name;
instance.position.x = this.position.x; instance.position.x = this.position.x;
instance.position.y = this.position.y; instance.position.y = this.position.y;
instance.position.z = this.position.z; instance.position.z = this.position.z;
@ -177,4 +220,11 @@ GameLib.D3.Light.createInstance = function() {
instance.rotation.z = this.rotation.z; instance.rotation.z = this.rotation.z;
return instance; return instance;
}; };
/**
* Updates the instance with the current state
*/
GameLib.D3.Light.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};

View File

@ -20,6 +20,8 @@ GameLib.D3.Material = function(
this.graphics.isNotThreeThrow(); this.graphics.isNotThreeThrow();
this.instance = this.createInstance(); this.instance = this.createInstance();
this.needsUpdate = false;
}; };
/** /**
@ -147,7 +149,7 @@ GameLib.D3.Material.API = function(
} }
this.id = id; this.id = id;
if (typeof materialType == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(materialType)) {
materialType = GameLib.D3.Material.MATERIAL_TYPE_STANDARD; materialType = GameLib.D3.Material.MATERIAL_TYPE_STANDARD;
} }
this.materialType = materialType; this.materialType = materialType;
@ -157,272 +159,272 @@ GameLib.D3.Material.API = function(
} }
this.name = name; this.name = name;
if (typeof opacity == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(opacity)) {
opacity = 1.0; opacity = 1.0;
} }
this.opacity = opacity; this.opacity = opacity;
if (typeof side == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(side)) {
side = GameLib.D3.Material.TYPE_FRONT_SIDE; side = GameLib.D3.Material.TYPE_FRONT_SIDE;
} }
this.side = side; this.side = side;
if (typeof transparent == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(transparent)) {
transparent = false; transparent = false;
} }
this.transparent = transparent; this.transparent = transparent;
if (typeof maps == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(maps)) {
maps = new GameLib.D3.TextureMapTemplate.API(); maps = GameLib.D3.TextureMapTemplate.API();
} }
this.maps = maps; this.maps = maps;
if (typeof specular == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(specular)) {
specular = new GameLib.D3.Color(0.06, 0.06, 0.06, 0.06); specular = new GameLib.D3.Color(0.06, 0.06, 0.06, 0.06);
} }
this.specular = specular; this.specular = specular;
if (typeof lightMapIntensity == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(lightMapIntensity)) {
lightMapIntensity = 1; lightMapIntensity = 1;
} }
this.lightMapIntensity = lightMapIntensity; this.lightMapIntensity = lightMapIntensity;
if (typeof aoMapIntensity == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(aoMapIntensity)) {
aoMapIntensity = 1; aoMapIntensity = 1;
} }
this.aoMapIntensity = aoMapIntensity; this.aoMapIntensity = aoMapIntensity;
if (typeof color == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(color)) {
color = new GameLib.D3.Color(1, 1, 1, 1) color = new GameLib.D3.Color(1, 1, 1, 1)
} }
this.color = color; this.color = color;
if (typeof emissive == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(emissive)) {
emissive = new GameLib.D3.Color(0, 0, 0, 0); emissive = new GameLib.D3.Color(0, 0, 0, 0);
} }
this.emissive = emissive; this.emissive = emissive;
if (typeof emissiveIntensity == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(emissiveIntensity)) {
emissiveIntensity = 1; emissiveIntensity = 1;
} }
this.emissiveIntensity = emissiveIntensity; this.emissiveIntensity = emissiveIntensity;
if (typeof combine == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(combine)) {
combine = GameLib.D3.Material.TYPE_MULTIPLY_OPERATION; combine = GameLib.D3.Material.TYPE_MULTIPLY_OPERATION;
} }
this.combine = combine; this.combine = combine;
if (typeof shininess == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(shininess)) {
shininess = 30; shininess = 30;
} }
this.shininess = shininess; this.shininess = shininess;
if (typeof reflectivity == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(reflectivity)) {
reflectivity = 1; reflectivity = 1;
} }
this.reflectivity = reflectivity; this.reflectivity = reflectivity;
if (typeof refractionRatio == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(refractionRatio)) {
refractionRatio = 0.98; refractionRatio = 0.98;
} }
this.refractionRatio = refractionRatio; this.refractionRatio = refractionRatio;
if (typeof fog == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(fog)) {
fog = true; fog = true;
} }
this.fog = fog; this.fog = fog;
if (typeof wireframe == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(wireframe)) {
wireframe = false; wireframe = false;
} }
this.wireframe = wireframe; this.wireframe = wireframe;
if (typeof wireframeLineWidth == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(wireframeLineWidth)) {
wireframeLineWidth = 1; wireframeLineWidth = 1;
} }
this.wireframeLineWidth = wireframeLineWidth; this.wireframeLineWidth = wireframeLineWidth;
if (typeof wireframeLineCap == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(wireframeLineCap)) {
wireframeLineCap = 'round'; wireframeLineCap = 'round';
} }
this.wireframeLineCap = wireframeLineCap; this.wireframeLineCap = wireframeLineCap;
if (typeof wireframeLineJoin == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(wireframeLineJoin)) {
wireframeLineJoin = 'round'; wireframeLineJoin = 'round';
} }
this.wireframeLineJoin = wireframeLineJoin; this.wireframeLineJoin = wireframeLineJoin;
if (typeof vertexColors == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(vertexColors)) {
vertexColors = GameLib.D3.Material.TYPE_NO_COLORS; vertexColors = GameLib.D3.Material.TYPE_NO_COLORS;
} }
this.vertexColors = vertexColors; this.vertexColors = vertexColors;
if (typeof skinning == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(skinning)) {
skinning = false; skinning = false;
} }
this.skinning = skinning; this.skinning = skinning;
if (typeof morphTargets == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(morphTargets)) {
morphTargets = false; morphTargets = false;
} }
this.morphTargets = morphTargets; this.morphTargets = morphTargets;
if (typeof morphNormals == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(morphNormals)) {
morphNormals = false; morphNormals = false;
} }
this.morphNormals = morphNormals; this.morphNormals = morphNormals;
if (typeof overdraw == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(overdraw)) {
overdraw = 0; overdraw = 0;
} }
this.overdraw = overdraw; this.overdraw = overdraw;
if (typeof lineWidth == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(lineWidth)) {
lineWidth = 1; lineWidth = 1;
} }
this.lineWidth = lineWidth; this.lineWidth = lineWidth;
if (typeof lineCap == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(lineCap)) {
lineCap = 'round'; lineCap = 'round';
} }
this.lineCap = lineCap; this.lineCap = lineCap;
if (typeof lineJoin == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(lineJoin)) {
lineJoin = 'round'; lineJoin = 'round';
} }
this.lineJoin = lineJoin; this.lineJoin = lineJoin;
if (typeof dashSize == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(dashSize)) {
dashSize = 3; dashSize = 3;
} }
this.dashSize = dashSize; this.dashSize = dashSize;
if (typeof gapWidth == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(gapWidth)) {
gapWidth = 1; gapWidth = 1;
} }
this.gapWidth = gapWidth; this.gapWidth = gapWidth;
if (typeof blending == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(blending)) {
blending = GameLib.D3.Material.TYPE_NORMAL_BLENDING; blending = GameLib.D3.Material.TYPE_NORMAL_BLENDING;
} }
this.blending = blending; this.blending = blending;
if (typeof blendSrc == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(blendSrc)) {
blendSrc = GameLib.D3.Material.TYPE_SRC_ALPHA_FACTOR; blendSrc = GameLib.D3.Material.TYPE_SRC_ALPHA_FACTOR;
} }
this.blendSrc = blendSrc; this.blendSrc = blendSrc;
if (typeof blendDst == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(blendDst)) {
blendDst = GameLib.D3.Material.TYPE_ONE_MINUS_SRC_ALPHA_FACTOR; blendDst = GameLib.D3.Material.TYPE_ONE_MINUS_SRC_ALPHA_FACTOR;
} }
this.blendDst = blendDst; this.blendDst = blendDst;
if (typeof blendEquation == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(blendEquation)) {
blendEquation = GameLib.D3.Material.TYPE_ADD_EQUATION; blendEquation = GameLib.D3.Material.TYPE_ADD_EQUATION;
} }
this.blendEquation = blendEquation; this.blendEquation = blendEquation;
if (typeof depthTest == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(depthTest)) {
depthTest = true; depthTest = true;
} }
this.depthTest = depthTest; this.depthTest = depthTest;
if (typeof depthFunc == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(depthFunc)) {
depthFunc = GameLib.D3.Material.TYPE_LESS_EQUAL_DEPTH; depthFunc = GameLib.D3.Material.TYPE_LESS_EQUAL_DEPTH;
} }
this.depthFunc = depthFunc; this.depthFunc = depthFunc;
if (typeof depthWrite == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(depthWrite)) {
depthWrite = true; depthWrite = true;
} }
this.depthWrite = depthWrite; this.depthWrite = depthWrite;
if (typeof polygonOffset == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(polygonOffset)) {
polygonOffset = false; polygonOffset = false;
} }
this.polygonOffset = polygonOffset; this.polygonOffset = polygonOffset;
if (typeof polygonOffsetFactor == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(polygonOffsetFactor)) {
polygonOffsetFactor = 1; polygonOffsetFactor = 1;
} }
this.polygonOffsetFactor = polygonOffsetFactor; this.polygonOffsetFactor = polygonOffsetFactor;
if (typeof polygonOffsetUnits == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(polygonOffsetUnits)) {
polygonOffsetUnits = 1; polygonOffsetUnits = 1;
} }
this.polygonOffsetUnits = polygonOffsetUnits; this.polygonOffsetUnits = polygonOffsetUnits;
if (typeof alphaTest == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(alphaTest)) {
alphaTest = 0; alphaTest = 0;
} }
this.alphaTest = alphaTest; this.alphaTest = alphaTest;
if (typeof clippingPlanes == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(clippingPlanes)) {
clippingPlanes = []; clippingPlanes = [];
} }
this.clippingPlanes = clippingPlanes; this.clippingPlanes = clippingPlanes;
if (typeof clipShadows == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(clipShadows)) {
clipShadows = false; clipShadows = false;
} }
this.clipShadows = clipShadows; this.clipShadows = clipShadows;
if (typeof visible == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(visible)) {
visible = true; visible = true;
} }
this.visible = visible; this.visible = visible;
if (typeof shading == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(shading)) {
shading = GameLib.D3.Material.TYPE_FLAT_SHADING; shading = GameLib.D3.Material.TYPE_FLAT_SHADING;
} }
this.shading = shading; this.shading = shading;
if (typeof bumpScale == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(bumpScale)) {
bumpScale = 1; bumpScale = 1;
} }
this.bumpScale = bumpScale; this.bumpScale = bumpScale;
if (typeof normalScale == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(normalScale)) {
normalScale = 1; normalScale = 1;
} }
this.normalScale = normalScale; this.normalScale = normalScale;
if (typeof displacementScale == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(displacementScale)) {
displacementScale = 1; displacementScale = 1;
} }
this.displacementScale = displacementScale; this.displacementScale = displacementScale;
if (typeof displacementBias == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(displacementBias)) {
displacementBias = 0; displacementBias = 0;
} }
this.displacementBias = displacementBias; this.displacementBias = displacementBias;
if (typeof roughness == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(roughness)) {
roughness = 0.5; roughness = 0.5;
} }
this.roughness = roughness; this.roughness = roughness;
if (typeof metalness == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(metalness)) {
metalness = 0.5; metalness = 0.5;
} }
this.metalness = metalness; this.metalness = metalness;
if (typeof pointSize == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(pointSize)) {
pointSize = 1; pointSize = 1;
} }
this.pointSize = pointSize; this.pointSize = pointSize;
if (typeof pointSizeAttenuation == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(pointSizeAttenuation)) {
pointSizeAttenuation = true; pointSizeAttenuation = true;
} }
this.pointSizeAttenuation = pointSizeAttenuation; this.pointSizeAttenuation = pointSizeAttenuation;
if (typeof spriteRotation == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(spriteRotation)) {
spriteRotation = 0; spriteRotation = 0;
} }
this.spriteRotation = spriteRotation; this.spriteRotation = spriteRotation;
if (typeof envMapIntensity == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(envMapIntensity)) {
envMapIntensity = 1.0; envMapIntensity = 1.0;
} }
this.envMapIntensity = envMapIntensity; this.envMapIntensity = envMapIntensity;
@ -528,124 +530,141 @@ GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial";
* Material instance * Material instance
* @returns {*} * @returns {*}
*/ */
GameLib.D3.Material.prototype.createInstance = function() { GameLib.D3.Material.prototype.createInstance = function(update) {
var instance = null; var instance = null;
if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { if (update) {
instance = this.instance;
}
instance = new this.graphics.instance.MeshStandardMaterial({ if (
name: this.name, (!instance) ||
opacity: this.opacity, (instance && instance.materialType != this.materialType)
transparent: this.transparent, ) {
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
roughness: this.roughness,
metalness: this.metalness,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
emissive: new this.graphics.instance.Color(
this.emissive.r,
this.emissive.g,
this.emissive.b
),
emissiveIntensity: this.emissiveIntensity,
bumpScale: this.bumpScale,
normalScale: this.normalScale,
displacementScale: this.displacementScale,
refractionRatio: this.refractionRatio,
fog: this.fog,
shading: this.shading,
wireframe: this.wireframe,
wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: this.wireframeLineJoin,
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
morphNormals: this.morphNormals
});
} else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) { if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) {
instance = new this.graphics.instance.MeshPhongMaterial({ instance = new this.graphics.instance.MeshStandardMaterial({
name: this.name, name: this.name,
opacity: this.opacity, opacity: this.opacity,
transparent: this.transparent, transparent: this.transparent,
blending: this.blending, blending: this.blending,
blendSrc: this.blendSrc, blendSrc: this.blendSrc,
blendDst: this.blendDst, blendDst: this.blendDst,
blendEquation: this.blendEquation, blendEquation: this.blendEquation,
depthTest: this.depthTest, depthTest: this.depthTest,
depthFunc: this.depthFunc, depthFunc: this.depthFunc,
depthWrite: this.depthWrite, depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset, polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor, polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits, polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest, alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes, clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows, clipShadows: this.clipShadows,
overdraw: this.overdraw, overdraw: this.overdraw,
visible: this.visible, visible: this.visible,
side: this.side, side: this.side,
color: new this.graphics.instance.Color( color: new this.graphics.instance.Color(
this.color.r, this.color.r,
this.color.g, this.color.g,
this.color.b this.color.b
), ),
specular: new this.graphics.instance.Color( roughness: this.roughness,
this.specular.r, metalness: this.metalness,
this.specular.g, lightMapIntensity: this.lightMapIntensity,
this.specular.b aoMapIntensity: this.aoMapIntensity,
), emissive: new this.graphics.instance.Color(
shininess: this.shininess, this.emissive.r,
lightMapIntensity: this.lightMapIntensity, this.emissive.g,
aoMapIntensity: this.aoMapIntensity, this.emissive.b
emissive: new this.graphics.instance.Color( ),
this.emissive.r, emissiveIntensity: this.emissiveIntensity,
this.emissive.g, bumpScale: this.bumpScale,
this.emissive.b normalScale: this.normalScale,
), displacementScale: this.displacementScale,
emissiveIntensity: this.emissiveIntensity, refractionRatio: this.refractionRatio,
bumpScale: this.bumpScale, fog: this.fog,
normalScale: this.normalScale, shading: this.shading,
displacementScale: this.displacementScale, wireframe: this.wireframe,
combine: this.combine, wireframeLinewidth: this.wireframeLineWidth,
refractionRatio: this.refractionRatio, wireframeLinecap: this.wireframeLineCap,
fog: this.fog, wireframeLinejoin: this.wireframeLineJoin,
shading: this.shading, vertexColors: this.vertexColors,
wireframe: this.wireframe, skinning: this.skinning,
wireframeLinewidth: this.wireframeLineWidth, morphTargets: this.morphTargets,
wireframeLinecap: this.wireframeLineCap, morphNormals: this.morphNormals
wireframeLinejoin: this.wireframeLineJoin, });
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
morphNormals: this.morphNormals
});
} else { } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) {
console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up");
instance = new this.graphics.instance.MeshPhongMaterial({
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
specular: new this.graphics.instance.Color(
this.specular.r,
this.specular.g,
this.specular.b
),
shininess: this.shininess,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
emissive: new this.graphics.instance.Color(
this.emissive.r,
this.emissive.g,
this.emissive.b
),
emissiveIntensity: this.emissiveIntensity,
bumpScale: this.bumpScale,
normalScale: this.normalScale,
displacementScale: this.displacementScale,
combine: this.combine,
refractionRatio: this.refractionRatio,
fog: this.fog,
shading: this.shading,
wireframe: this.wireframe,
wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: this.wireframeLineJoin,
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
morphNormals: this.morphNormals
});
} else {
console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up");
}
} }
return instance; return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Material.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
}; };

View File

@ -1,20 +1,15 @@
/** /**
* Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created * Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created
* @param id String
* @param name String
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param computeNormals Boolean * @param computeNormals Boolean
* @param apiMesh GameLib.D3.Mesh.API * @param apiMesh GameLib.D3.Mesh.API
* @constructor * @constructor
*/ */
GameLib.D3.Mesh = function( GameLib.D3.Mesh = function(
id,
name,
graphics, graphics,
computeNormals, computeNormals,
apiMesh apiMesh
) { ) {
for (var property in apiMesh) { for (var property in apiMesh) {
if (apiMesh.hasOwnProperty(property)) { if (apiMesh.hasOwnProperty(property)) {
this[property] = apiMesh[property]; this[property] = apiMesh[property];
@ -27,7 +22,9 @@ GameLib.D3.Mesh = function(
this.computeNormals = computeNormals; this.computeNormals = computeNormals;
this.instance = this.createInstance(); this.instance = this.createInstance(false);
this.needsUpdate = false;
}; };
/** /**
@ -95,62 +92,62 @@ GameLib.D3.Mesh.API = function(
} }
this.faces = faces; this.faces = faces;
if (typeof parentMeshId == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(parentMeshId)) {
parentMeshId = null; parentMeshId = null;
} }
this.parentMeshId = parentMeshId; this.parentMeshId = parentMeshId;
if (typeof parentSceneId == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(parentSceneId)) {
parentSceneId = null; parentSceneId = null;
} }
this.parentSceneId = parentSceneId; this.parentSceneId = parentSceneId;
if (typeof skeleton == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(skeleton)) {
skeleton = null; skeleton = null;
} }
this.skeleton = skeleton; this.skeleton = skeleton;
if (typeof faceVertexUvs == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(faceVertexUvs)) {
faceVertexUvs = []; faceVertexUvs = [];
} }
this.faceVertexUvs = faceVertexUvs; this.faceVertexUvs = faceVertexUvs;
if (typeof skinIndices == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(skinIndices)) {
skinIndices = []; skinIndices = [];
} }
this.skinIndices = skinIndices; this.skinIndices = skinIndices;
if (typeof skinWeights == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(skinWeights)) {
skinWeights = []; skinWeights = [];
} }
this.skinWeights = skinWeights; this.skinWeights = skinWeights;
if (typeof materials == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(materials)) {
materials = []; materials = [];
} }
this.materials = materials; this.materials = materials;
if (typeof position == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(position)) {
position = new GameLib.D3.Vector3(0,0,0); position = new GameLib.D3.Vector3(0,0,0);
} }
this.position = position; this.position = position;
if (typeof quaternion == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(quaternion)) {
new GameLib.D3.Vector4(); quaternion = new GameLib.D3.Vector4();
} }
this.quaternion = quaternion; this.quaternion = quaternion;
if (typeof rotation == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(rotation)) {
rotation = new GameLib.D3.Vector3(0,0,0); rotation = new GameLib.D3.Vector3(0,0,0);
} }
this.rotation = rotation; this.rotation = rotation;
if (typeof scale == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(scale)) {
scale = new GameLib.D3.Vector3(1,1,1); scale = new GameLib.D3.Vector3(1,1,1);
} }
this.scale = scale; this.scale = scale;
if (typeof up == 'undefined') { if (GameLib.D3.Utils.UndefinedOrNull(up)) {
up = new GameLib.D3.Vector3(0,1,0); up = new GameLib.D3.Vector3(0,1,0);
} }
this.up = up; this.up = up;
@ -164,239 +161,243 @@ GameLib.D3.Mesh.TYPE_NORMAL = 0;
GameLib.D3.Mesh.TYPE_SKINNED = 1; GameLib.D3.Mesh.TYPE_SKINNED = 1;
/** /**
* Creates a mesh instance * Creates a mesh instance or updates it
*/ */
GameLib.D3.Mesh.prototype.createInstance = function() { GameLib.D3.Mesh.prototype.createInstance = function(update) {
var instance = null; var instance = null;
var instanceGeometry = new this.graphics.instance.Geometry(); if (update) {
instance = this.instance;
/**
* Setup vertices
*/
for (var v = 0; v < this.vertices.length; v++) {
instanceGeometry.vertices.push(
new this.graphics.instance.Vector3(
this.vertices[v].position.x,
this.vertices[v].position.y,
this.vertices[v].position.z
)
)
} }
/** if (!update) {
* Setup faces var instanceGeometry = new this.graphics.instance.Geometry();
*/
for (var f = 0; f < this.faces.length; f++) {
var face = new this.graphics.instance.Face3( /**
this.faces[f].v0, * Setup vertices
this.faces[f].v1, */
this.faces[f].v2, for (var v = 0; v < this.vertices.length; v++) {
new this.graphics.instance.Vector3( instanceGeometry.vertices.push(
new this.graphics.instance.Vector3(
this.vertices[v].position.x,
this.vertices[v].position.y,
this.vertices[v].position.z
)
)
}
/**
* Setup faces
*/
for (var f = 0; f < this.faces.length; f++) {
var face = new this.graphics.instance.Face3(
this.faces[f].v0,
this.faces[f].v1,
this.faces[f].v2,
new this.graphics.instance.Vector3(
this.faces[f].normal.x,
this.faces[f].normal.y,
this.faces[f].normal.z
),
new this.graphics.instance.Color(
this.faces[f].color.r,
this.faces[f].color.g,
this.faces[f].color.b
),
this.faces[f].materialIndex
);
face.vertexColors = [
new this.graphics.instance.Color(
this.faces[f].vertexColors[0].r,
this.faces[f].vertexColors[0].g,
this.faces[f].vertexColors[0].b
),
new this.graphics.instance.Color(
this.faces[f].vertexColors[1].r,
this.faces[f].vertexColors[1].g,
this.faces[f].vertexColors[1].b
),
new this.graphics.instance.Color(
this.faces[f].vertexColors[2].r,
this.faces[f].vertexColors[2].g,
this.faces[f].vertexColors[2].b
)
];
face.normal = new this.graphics.instance.Vector3(
this.faces[f].normal.x, this.faces[f].normal.x,
this.faces[f].normal.y, this.faces[f].normal.y,
this.faces[f].normal.z this.faces[f].normal.z
),
new this.graphics.instance.Color(
this.faces[f].color.r,
this.faces[f].color.g,
this.faces[f].color.b
),
this.faces[f].materialIndex
);
face.vertexColors = [
new this.graphics.instance.Color(
this.faces[f].vertexColors[0].r,
this.faces[f].vertexColors[0].g,
this.faces[f].vertexColors[0].b
),
new this.graphics.instance.Color(
this.faces[f].vertexColors[1].r,
this.faces[f].vertexColors[1].g,
this.faces[f].vertexColors[1].b
),
new this.graphics.instance.Color(
this.faces[f].vertexColors[2].r,
this.faces[f].vertexColors[2].g,
this.faces[f].vertexColors[2].b
)
];
face.normal = new this.graphics.instance.Vector3(
this.faces[f].normal.x,
this.faces[f].normal.y,
this.faces[f].normal.z
);
face.vertexNormals = [
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[0].x,
this.faces[f].vertexNormals[0].y,
this.faces[f].vertexNormals[0].z
),
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[1].x,
this.faces[f].vertexNormals[1].y,
this.faces[f].vertexNormals[1].z
),
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[2].x,
this.faces[f].vertexNormals[2].y,
this.faces[f].vertexNormals[2].z
)
];
instanceGeometry.faces.push(face);
}
instanceGeometry.faceVertexUvs = [];
/**
* Setup face UVs
*/
for (var fm = 0; fm < this.faceVertexUvs.length; fm++) {
var faceMaterialVertexUvs = this.faceVertexUvs[fm];
instanceGeometry.faceVertexUvs[fm] = [];
for (var fuv = 0; fuv < faceMaterialVertexUvs.length; fuv++) {
instanceGeometry.faceVertexUvs[fm][fuv] = [];
instanceGeometry.faceVertexUvs[fm][fuv].push(
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][0].x,
faceMaterialVertexUvs[fuv][0].y
),
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][1].x,
faceMaterialVertexUvs[fuv][1].y
),
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][2].x,
faceMaterialVertexUvs[fuv][2].y
)
); );
face.vertexNormals = [
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[0].x,
this.faces[f].vertexNormals[0].y,
this.faces[f].vertexNormals[0].z
),
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[1].x,
this.faces[f].vertexNormals[1].y,
this.faces[f].vertexNormals[1].z
),
new this.graphics.instance.Vector3(
this.faces[f].vertexNormals[2].x,
this.faces[f].vertexNormals[2].y,
this.faces[f].vertexNormals[2].z
)
];
instanceGeometry.faces.push(face);
} }
}
/** instanceGeometry.faceVertexUvs = [];
* Re-calculate normals (if we have to)
* @type {Array}
*/
if (this.computeNormals) {
instanceGeometry.computeFaceNormals();
instanceGeometry.computeVertexNormals();
}
var instanceMaterial = this.materials[0].instance;
if (this.meshType == GameLib.D3.Mesh.TYPE_NORMAL) {
instance = new this.graphics.instance.Mesh(instanceGeometry, instanceMaterial);
}
if (this.meshType == GameLib.D3.Mesh.TYPE_SKINNED) {
var bones = this.skeleton.bones;
var instanceBones = [];
for (var bi = 0; bi < bones.length; bi++) {
var bone = new this.graphics.instance.Bone();
bone.name = bones[bi].name;
bone.position.x = bones[bi].position.x;
bone.position.y = bones[bi].position.y;
bone.position.z = bones[bi].position.z;
bone.rotation.x = bones[bi].rotation.x;
bone.rotation.y = bones[bi].rotation.y;
bone.rotation.z = bones[bi].rotation.z;
bone.quaternion.x = bones[bi].quaternion.x;
bone.quaternion.y = bones[bi].quaternion.y;
bone.quaternion.z = bones[bi].quaternion.z;
bone.quaternion.w = bones[bi].quaternion.w;
bone.scale.x = bones[bi].scale.x;
bone.scale.y = bones[bi].scale.y;
bone.scale.z = bones[bi].scale.z;
bone.up.x = bones[bi].up.x;
bone.up.y = bones[bi].up.y;
bone.up.z = bones[bi].up.z;
instanceBones.push(bone);
}
/** /**
* Setup the bone relationships * Setup face UVs
*/ */
for (var br = 0; br < bones.length; br++) { for (var fm = 0; fm < this.faceVertexUvs.length; fm++) {
for (var cbi = 0; cbi < bones[br].childBoneIds.length; cbi++) {
instanceBones[br].add(instanceBones[bones[br].childBoneIds[cbi]]); var faceMaterialVertexUvs = this.faceVertexUvs[fm];
instanceGeometry.faceVertexUvs[fm] = [];
for (var fuv = 0; fuv < faceMaterialVertexUvs.length; fuv++) {
instanceGeometry.faceVertexUvs[fm][fuv] = [];
instanceGeometry.faceVertexUvs[fm][fuv].push(
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][0].x,
faceMaterialVertexUvs[fuv][0].y
),
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][1].x,
faceMaterialVertexUvs[fuv][1].y
),
new this.graphics.instance.Vector2(
faceMaterialVertexUvs[fuv][2].x,
faceMaterialVertexUvs[fuv][2].y
)
);
} }
} }
/** /**
* Setup bones (indexes) * Re-calculate normals (if we have to)
* @type {Array}
*/ */
for (var si = 0; si < this.skinIndices.length; si++) { if (this.computeNormals) {
instanceGeometry.skinIndices.push( instanceGeometry.computeFaceNormals();
new this.graphics.instance.Vector4( instanceGeometry.computeVertexNormals();
this.skinIndices[si].x,
this.skinIndices[si].y,
this.skinIndices[si].z,
this.skinIndices[si].w
)
);
} }
/** var instanceMaterial = this.materials[0].instance;
* Setup bones (weights)
*/ if (this.meshType == GameLib.D3.Mesh.TYPE_NORMAL) {
for (var sw = 0; sw < this.skinWeights.length; sw++) { instance = new this.graphics.instance.Mesh(instanceGeometry, instanceMaterial);
instanceGeometry.skinWeights.push(
new this.graphics.instance.Vector4(
this.skinWeights[sw].x,
this.skinWeights[sw].y,
this.skinWeights[sw].z,
this.skinWeights[sw].w
)
);
} }
instance = new this.graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial); if (this.meshType == GameLib.D3.Mesh.TYPE_SKINNED) {
var skeleton = new this.graphics.instance.Skeleton(instanceBones); var bones = this.skeleton.bones;
skeleton.useVertexTexture = this.skeleton.useVertexTexture; var instanceBones = [];
for (var i = 0; i < bones.length; i++) { for (var bi = 0; bi < bones.length; bi++) {
if (bones[i].parentBoneId === null) {
instance.add(instanceBones[i]); var bone = new this.graphics.instance.Bone();
break;
bone.name = bones[bi].name;
bone.position.x = bones[bi].position.x;
bone.position.y = bones[bi].position.y;
bone.position.z = bones[bi].position.z;
bone.rotation.x = bones[bi].rotation.x;
bone.rotation.y = bones[bi].rotation.y;
bone.rotation.z = bones[bi].rotation.z;
bone.quaternion.x = bones[bi].quaternion.x;
bone.quaternion.y = bones[bi].quaternion.y;
bone.quaternion.z = bones[bi].quaternion.z;
bone.quaternion.w = bones[bi].quaternion.w;
bone.scale.x = bones[bi].scale.x;
bone.scale.y = bones[bi].scale.y;
bone.scale.z = bones[bi].scale.z;
bone.up.x = bones[bi].up.x;
bone.up.y = bones[bi].up.y;
bone.up.z = bones[bi].up.z;
instanceBones.push(bone);
} }
/**
* Setup the bone relationships
*/
for (var br = 0; br < bones.length; br++) {
for (var cbi = 0; cbi < bones[br].childBoneIds.length; cbi++) {
instanceBones[br].add(instanceBones[bones[br].childBoneIds[cbi]]);
}
}
/**
* Setup bones (indexes)
*/
for (var si = 0; si < this.skinIndices.length; si++) {
instanceGeometry.skinIndices.push(
new this.graphics.instance.Vector4(
this.skinIndices[si].x,
this.skinIndices[si].y,
this.skinIndices[si].z,
this.skinIndices[si].w
)
);
}
/**
* Setup bones (weights)
*/
for (var sw = 0; sw < this.skinWeights.length; sw++) {
instanceGeometry.skinWeights.push(
new this.graphics.instance.Vector4(
this.skinWeights[sw].x,
this.skinWeights[sw].y,
this.skinWeights[sw].z,
this.skinWeights[sw].w
)
);
}
instance = new this.graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial);
var skeleton = new this.graphics.instance.Skeleton(instanceBones);
skeleton.useVertexTexture = this.skeleton.useVertexTexture;
for (var i = 0; i < bones.length; i++) {
if (bones[i].parentBoneId === null) {
instance.add(instanceBones[i]);
break;
}
}
instance.bind(skeleton);
instance.pose();
instance.skeleton.skeletonHelper = new this.graphics.instance.SkeletonHelper(instance);
instance.skeleton.skeletonHelper.material.linewidth = 5;
} }
instance.bind(skeleton); if (instance == null) {
console.log('cannot handle meshes of type ' + this.meshType + ' yet.');
instance.pose(); }
instance.skeleton.skeletonHelper = new this.graphics.instance.SkeletonHelper(instance);
instance.skeleton.skeletonHelper.material.linewidth = 5;
} }
if (instance == null) {
console.log('cannot handle meshes of type ' + this.meshType + ' yet.');
}
this.threeMeshId = instance.id;
instance.name = this.name; instance.name = this.name;
instance.gameLibObject = this; instance.gameLibObject = this;
@ -421,6 +422,10 @@ GameLib.D3.Mesh.prototype.createInstance = function() {
return instance; return instance;
}; };
GameLib.D3.Mesh.updateInstance = function() {
this.instance = this.createInstance(true);
};
GameLib.D3.prototype.invertWindingOrder = function(triangles) { GameLib.D3.prototype.invertWindingOrder = function(triangles) {
for (var i = 0; i < triangles.length; i++) { for (var i = 0; i < triangles.length; i++) {

View File

@ -30,6 +30,8 @@ GameLib.D3.Scene = function(
this.progressCallback = progressCallback; this.progressCallback = progressCallback;
this.instance = this.createInstance(); this.instance = this.createInstance();
this.needsUpdate = false;
}; };
/** /**
@ -184,6 +186,7 @@ GameLib.D3.Scene.prototype.createInstance = function() {
* @param graphics * @param graphics
* @param uploadUrl * @param uploadUrl
* @param progressCallback * @param progressCallback
* @param computeNormals
* @constructor * @constructor
*/ */
GameLib.D3.Scene.LoadScene = function( GameLib.D3.Scene.LoadScene = function(
@ -191,7 +194,8 @@ GameLib.D3.Scene.LoadScene = function(
onLoaded, onLoaded,
graphics, graphics,
uploadUrl, uploadUrl,
progressCallback progressCallback,
computeNormals
) { ) {
var entities = []; var entities = [];
@ -281,7 +285,7 @@ GameLib.D3.Scene.LoadScene = function(
apiMaterial.opacity, apiMaterial.opacity,
apiMaterial.side, apiMaterial.side,
apiMaterial.transparent, apiMaterial.transparent,
null, GameLib.D3.TextureMapTemplate.API(),
new GameLib.D3.Color( new GameLib.D3.Color(
apiMaterial.specular.r, apiMaterial.specular.r,
apiMaterial.specular.g, apiMaterial.specular.g,
@ -402,46 +406,48 @@ GameLib.D3.Scene.LoadScene = function(
} }
var gameLibMesh = new GameLib.D3.Mesh( var gameLibMesh = new GameLib.D3.Mesh(
apiMesh.id,
apiMesh.name,
graphics, graphics,
false, computeNormals,
apiMesh.meshType, new GameLib.D3.Mesh.API(
apiMesh.vertices, apiMesh.id,
apiMesh.faces, apiMesh.meshType,
apiMesh.skeleton, apiMesh.name,
apiMesh.faceVertexUvs, apiMesh.vertices,
apiMesh.skinIndices, apiMesh.faces,
apiMesh.skinWeights, apiMesh.faceVertexUvs,
gameLibMaterials, gameLibMaterials,
new GameLib.D3.Vector3( apiMesh.parentMeshId,
apiMesh.position.x, apiMesh.parentSceneId,
apiMesh.position.y, apiMesh.skeleton,
apiMesh.position.z apiMesh.skinIndices,
), apiMesh.skinWeights,
new GameLib.D3.Vector3( new GameLib.D3.Vector3(
apiMesh.quaternion.w, apiMesh.position.x,
apiMesh.quaternion.x, apiMesh.position.y,
apiMesh.quaternion.y, apiMesh.position.z
apiMesh.quaternion.z ),
), new GameLib.D3.Vector4(
new GameLib.D3.Vector3( apiMesh.quaternion.x,
apiMesh.rotation.x, apiMesh.quaternion.y,
apiMesh.rotation.y, apiMesh.quaternion.z,
apiMesh.rotation.z apiMesh.quaternion.w
), ),
new GameLib.D3.Vector3( new GameLib.D3.Vector3(
apiMesh.scale.x, apiMesh.rotation.x,
apiMesh.scale.y, apiMesh.rotation.y,
apiMesh.scale.z apiMesh.rotation.z
), ),
new GameLib.D3.Vector3( new GameLib.D3.Vector3(
apiMesh.up.x, apiMesh.scale.x,
apiMesh.up.y, apiMesh.scale.y,
apiMesh.up.z apiMesh.scale.z
), ),
apiMesh.parentMeshId, new GameLib.D3.Vector3(
apiMesh.parentSceneId apiMesh.up.x,
apiMesh.up.y,
apiMesh.up.z
)
)
); );
gameLibMeshes.push(gameLibMesh); gameLibMeshes.push(gameLibMesh);
@ -454,7 +460,7 @@ GameLib.D3.Scene.LoadScene = function(
scene.id, scene.id,
scene.path, scene.path,
scene.name, scene.name,
scene.meshes, gameLibMeshes,
new GameLib.D3.Vector4( new GameLib.D3.Vector4(
scene.quaternion.x, scene.quaternion.x,
scene.quaternion.y, scene.quaternion.y,
@ -534,7 +540,7 @@ GameLib.D3.Scene.LoadSceneFromApi = function(
var scene = response.scene[0]; var scene = response.scene[0];
GameLib.D3.Scene.LoadScene(scene, onLoaded, graphics, uploadUrl, progressCallback); GameLib.D3.Scene.LoadScene(scene, onLoaded, graphics, uploadUrl, progressCallback, false);
} }
} }
}(xhr); }(xhr);

View File

@ -38,6 +38,9 @@ GameLib.D3.Texture = function(
function (imageInstance){ function (imageInstance){
this.imageInstance = imageInstance; this.imageInstance = imageInstance;
this.instance = this.createInstance(); this.instance = this.createInstance();
this.needsUpdate = false;
this.parentMaterial[this.parentMaterialInstanceMapId] = this.instance; this.parentMaterial[this.parentMaterialInstanceMapId] = this.instance;
this.parentMaterial.needsUpdate = true; this.parentMaterial.needsUpdate = true;
}.bind(this), }.bind(this),
@ -267,19 +270,25 @@ GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256.
* Creates an instance of our texture object * Creates an instance of our texture object
* @returns {GameLib.D3.Texture|THREE.SoftwareRenderer.Texture|THREE.Texture|*|Texture} * @returns {GameLib.D3.Texture|THREE.SoftwareRenderer.Texture|THREE.Texture|*|Texture}
*/ */
GameLib.D3.Texture.prototype.createInstance = function() { GameLib.D3.Texture.prototype.createInstance = function(update) {
var instance = new this.graphics.instance.Texture( var instance = null;
this.imageInstance,
this.mapping, if (update) {
this.wrapS, instance = this.instance;
this.wrapT, } else {
this.magFilter, instance = new this.graphics.instance.Texture(
this.minFilter, this.imageInstance,
undefined, //format and textureType is different on different archs this.mapping,
undefined, this.wrapS,
this.anisotropy this.wrapT,
); this.magFilter,
this.minFilter,
undefined, //format and textureType is different on different archs
undefined,
this.anisotropy
);
}
instance.name = this.name; instance.name = this.name;
instance.flipY = this.flipY; instance.flipY = this.flipY;
@ -294,3 +303,10 @@ GameLib.D3.Texture.prototype.createInstance = function() {
return instance; return instance;
}; };
/**
* Updates the instance with the current state
*/
GameLib.D3.Texture.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};

View File

@ -3,7 +3,7 @@ GameLib.D3.Vector4 = function(x, y, z, w) {
this.x = 0; this.x = 0;
this.y = 0; this.y = 0;
this.z = 0; this.z = 0;
this.w = 0; this.w = 1;
if (x) { if (x) {
this.x = x; this.x = x;