sphere meshes
parent
1eeeecdd66
commit
f9c18837e6
|
@ -15,6 +15,7 @@ GameLib.Event.Subscriptions = {};
|
|||
* Events we can subscribe to and publish
|
||||
*/
|
||||
GameLib.Event.WINDOW_RESIZE = 0x1;
|
||||
GameLib.Event.PARENT_SCENE_CHANGE = 0x2;
|
||||
|
||||
/**
|
||||
* Subscribe to some events
|
||||
|
|
|
@ -31,7 +31,7 @@ GameLib.Component = function(
|
|||
|
||||
this.built = true;
|
||||
|
||||
//this.linkedObjects.parentEntity = GameLib.Entity;
|
||||
this.linkedObjects.parentEntity = GameLib.Entity;
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(guiProperties)) {
|
||||
guiProperties = {};
|
||||
|
|
|
@ -189,14 +189,29 @@ GameLib.D3.Input.Editor.prototype.onMouseDown = function(entity, entityManager)
|
|||
this.mouse.x = (event.offsetX / renderer.instance.domElement.width) * 2 - 1;
|
||||
this.mouse.y = -(event.offsetY / renderer.instance.domElement.height) * 2 + 1;
|
||||
|
||||
this.raycaster.instance.setFromCamera(
|
||||
this.mouse,
|
||||
this.camera.instance
|
||||
);
|
||||
var cameras = entity.getComponents(GameLib.D3.Camera);
|
||||
|
||||
var meshes = entity.getComponents(GameLib.D3.Mesh);
|
||||
|
||||
var intersects = this.raycaster.getIntersectedObjects(meshes);
|
||||
var intersects = cameras.reduce(
|
||||
|
||||
function(result, camera){
|
||||
|
||||
this.raycaster.instance.setFromCamera(
|
||||
this.mouse,
|
||||
camera.instance
|
||||
);
|
||||
|
||||
intersects = this.raycaster.getIntersectedObjects(meshes);
|
||||
|
||||
intersects.map(function(intersect){
|
||||
result.push(intersect);
|
||||
})
|
||||
|
||||
return result;
|
||||
}.bind(this),
|
||||
[]
|
||||
);
|
||||
|
||||
if (intersects.length > 0) {
|
||||
|
||||
|
@ -252,6 +267,12 @@ GameLib.D3.Input.Editor.prototype.onMouseDown = function(entity, entityManager)
|
|||
|
||||
scene.instance.add(helper.instance);
|
||||
|
||||
scene.subscribe(GameLib.Event.PARENT_SCENE_CHANGE, function(data) {
|
||||
if (data.old === scene) {
|
||||
scene.instance.remove(helper.instance);
|
||||
}
|
||||
});
|
||||
|
||||
gui.addObject(intersects[0]);
|
||||
|
||||
} else {
|
||||
|
|
|
@ -161,29 +161,30 @@ GameLib.D3.Mesh.prototype.constructor = GameLib.D3.Mesh;
|
|||
* Mesh Type
|
||||
* @type {number}
|
||||
*/
|
||||
GameLib.D3.Mesh.TYPE_NORMAL = 0;
|
||||
GameLib.D3.Mesh.TYPE_SKINNED = 1;
|
||||
GameLib.D3.Mesh.TYPE_CURVE = 2;
|
||||
GameLib.D3.Mesh.TYPE_NORMAL = 0x0;
|
||||
GameLib.D3.Mesh.TYPE_SKINNED = 0x1;
|
||||
GameLib.D3.Mesh.TYPE_CURVE = 0x2;
|
||||
|
||||
/**
|
||||
* Creates a mesh instance or updates it
|
||||
* Mesh Shape (predefined or custom)
|
||||
* @type {number}
|
||||
*/
|
||||
GameLib.D3.Mesh.prototype.createInstance = function(update) {
|
||||
GameLib.D3.Mesh.SHAPE_CUSTOM = 0x1;
|
||||
GameLib.D3.Mesh.SHAPE_SPHERE = 0x2;
|
||||
|
||||
var instance = null;
|
||||
/**
|
||||
* Creates custom geometry
|
||||
* @returns {THREE.Geometry}
|
||||
*/
|
||||
GameLib.D3.Mesh.prototype.customGeometry = function(){
|
||||
|
||||
if (update) {
|
||||
instance = this.instance;
|
||||
}
|
||||
|
||||
if (!update) {
|
||||
var instanceGeometry = new this.graphics.instance.Geometry();
|
||||
var geometry = new THREE.Geometry();
|
||||
|
||||
/**
|
||||
* Setup vertices
|
||||
*/
|
||||
for (var v = 0; v < this.vertices.length; v++) {
|
||||
instanceGeometry.vertices.push(
|
||||
geometry.vertices.push(
|
||||
new this.graphics.instance.Vector3(
|
||||
this.vertices[v].position.x,
|
||||
this.vertices[v].position.y,
|
||||
|
@ -256,10 +257,10 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) {
|
|||
)
|
||||
];
|
||||
|
||||
instanceGeometry.faces.push(face);
|
||||
geometry.faces.push(face);
|
||||
}
|
||||
|
||||
instanceGeometry.faceVertexUvs = [];
|
||||
geometry.faceVertexUvs = [];
|
||||
|
||||
/**
|
||||
* Setup face UVs
|
||||
|
@ -268,11 +269,11 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) {
|
|||
|
||||
var faceMaterialVertexUvs = this.faceVertexUvs[fm];
|
||||
|
||||
instanceGeometry.faceVertexUvs[fm] = [];
|
||||
geometry.faceVertexUvs[fm] = [];
|
||||
|
||||
for (var fuv = 0; fuv < faceMaterialVertexUvs.length; fuv++) {
|
||||
instanceGeometry.faceVertexUvs[fm][fuv] = [];
|
||||
instanceGeometry.faceVertexUvs[fm][fuv].push(
|
||||
geometry.faceVertexUvs[fm][fuv] = [];
|
||||
geometry.faceVertexUvs[fm][fuv].push(
|
||||
new this.graphics.instance.Vector2(
|
||||
faceMaterialVertexUvs[fuv][0].x,
|
||||
faceMaterialVertexUvs[fuv][0].y
|
||||
|
@ -289,31 +290,56 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) {
|
|||
}
|
||||
}
|
||||
|
||||
return geometry;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a mesh instance or updates it
|
||||
*/
|
||||
GameLib.D3.Mesh.prototype.createInstance = function(update) {
|
||||
|
||||
var instance = null;
|
||||
|
||||
if (update) {
|
||||
instance = this.instance;
|
||||
}
|
||||
|
||||
if (!update) {
|
||||
|
||||
var geometry = null;
|
||||
|
||||
if (this instanceof GameLib.D3.Mesh.Sphere) {
|
||||
geometry = this.sphereGeometry();
|
||||
} else {
|
||||
geometry = this.customGeometry();
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-calculate normals (if we have to)
|
||||
* @type {Array}
|
||||
*/
|
||||
if (this.computeNormals) {
|
||||
instanceGeometry.computeFaceNormals();
|
||||
instanceGeometry.computeVertexNormals();
|
||||
geometry.computeFaceNormals();
|
||||
geometry.computeVertexNormals();
|
||||
this.computeNormals = false;
|
||||
}
|
||||
|
||||
if (this.meshType == GameLib.D3.Mesh.TYPE_NORMAL) {
|
||||
instance = new THREE.Mesh(instanceGeometry);
|
||||
if (this.meshType === GameLib.D3.Mesh.TYPE_NORMAL) {
|
||||
instance = new THREE.Mesh(geometry);
|
||||
}
|
||||
|
||||
if (this.meshType == GameLib.D3.Mesh.TYPE_CURVE) {
|
||||
instance = new THREE.Points(instanceGeometry);
|
||||
if (this.meshType === GameLib.D3.Mesh.TYPE_CURVE) {
|
||||
instance = new THREE.Points(geometry);
|
||||
}
|
||||
|
||||
if (this.meshType == GameLib.D3.Mesh.TYPE_SKINNED) {
|
||||
if (this.meshType === GameLib.D3.Mesh.TYPE_SKINNED) {
|
||||
|
||||
/**
|
||||
* Setup bones (indexes)
|
||||
*/
|
||||
for (var si = 0; si < this.skinIndices.length; si++) {
|
||||
instanceGeometry.skinIndices.push(
|
||||
geometry.skinIndices.push(
|
||||
new THREE.Vector4(
|
||||
this.skinIndices[si].x,
|
||||
this.skinIndices[si].y,
|
||||
|
@ -327,7 +353,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) {
|
|||
* Setup bones (weights)
|
||||
*/
|
||||
for (var sw = 0; sw < this.skinWeights.length; sw++) {
|
||||
instanceGeometry.skinWeights.push(
|
||||
geometry.skinWeights.push(
|
||||
new THREE.Vector4(
|
||||
this.skinWeights[sw].x,
|
||||
this.skinWeights[sw].y,
|
||||
|
@ -337,14 +363,14 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) {
|
|||
);
|
||||
}
|
||||
|
||||
instance = new THREE.SkinnedMesh(instanceGeometry);
|
||||
instance = new THREE.SkinnedMesh(geometry);
|
||||
|
||||
instance.add(this.skeleton.rootBoneInstance);
|
||||
|
||||
instance.bind(this.skeleton.instance);
|
||||
}
|
||||
|
||||
if (instance == null) {
|
||||
if (instance === null) {
|
||||
console.log('cannot handle meshes of type ' + this.meshType + ' yet.');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
/**
|
||||
* Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created
|
||||
* @param graphics GameLib.D3.Graphics
|
||||
* @param apiMesh GameLib.D3.API.Mesh
|
||||
* @param radius
|
||||
* @param widthSegments
|
||||
* @param heightSegments
|
||||
* @constructor
|
||||
*/
|
||||
GameLib.D3.Mesh.Sphere = function (
|
||||
graphics,
|
||||
apiMesh,
|
||||
radius,
|
||||
widthSegments,
|
||||
heightSegments
|
||||
) {
|
||||
this.graphics = graphics;
|
||||
this.graphics.isNotThreeThrow();
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(radius)) {
|
||||
radius = 1;
|
||||
}
|
||||
this.radius = radius;
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(widthSegments)) {
|
||||
widthSegments = 5;
|
||||
}
|
||||
this.widthSegments = widthSegments;
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(heightSegments)) {
|
||||
heightSegments = 5;
|
||||
}
|
||||
this.heightSegments = heightSegments;
|
||||
|
||||
GameLib.D3.Mesh.call(
|
||||
this,
|
||||
this.graphics,
|
||||
apiMesh,
|
||||
null,
|
||||
true
|
||||
);
|
||||
};
|
||||
|
||||
GameLib.D3.Mesh.Sphere.prototype = Object.create(GameLib.D3.Mesh.prototype);
|
||||
GameLib.D3.Mesh.Sphere.prototype.constructor = GameLib.D3.Mesh.Sphere;
|
||||
|
||||
|
||||
GameLib.D3.Mesh.Sphere.prototype.sphereGeometry = function() {
|
||||
return new THREE.SphereGeometry(
|
||||
this.radius,
|
||||
this.widthSegments,
|
||||
this.heightSegments
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts a GameLib.D3.Mesh to a GameLib.D3.API.Mesh
|
||||
* @returns {GameLib.D3.API.Mesh}
|
||||
*/
|
||||
GameLib.D3.Mesh.Sphere.prototype.toApiObject = function() {
|
||||
|
||||
var apiMesh = GameLib.D3.Mesh.prototype.toApiObject.call(this);
|
||||
|
||||
apiMesh.radius = this.radius;
|
||||
apiMesh.widthSegments = this.widthSegments;
|
||||
apiMesh.heightSegments = this.heightSegments;
|
||||
|
||||
return apiMesh;
|
||||
};
|
||||
|
||||
/**
|
||||
* TODO fix all this weird loading shit
|
||||
* Converts a standard object mesh to a GameLib.D3.Mesh
|
||||
* @param graphics GameLib.D3.Graphics
|
||||
* @param objectMesh {Object}
|
||||
* @param computeNormals boolean to indicate whether or not to recalculate normals
|
||||
* @param imageFactory GameLib.D3.ImageFactory
|
||||
* @constructor
|
||||
*/
|
||||
GameLib.D3.Mesh.Sphere.FromObjectMesh = function(graphics, objectMesh, computeNormals, imageFactory) {
|
||||
|
||||
var apiMesh = GameLib.D3.API.Mesh.FromObjectMesh(objectMesh);
|
||||
|
||||
return new GameLib.D3.Mesh.Sphere(
|
||||
graphics,
|
||||
apiMesh,
|
||||
objectMesh.radius,
|
||||
objectMesh.widthSegments,
|
||||
objectMesh.heightSegments
|
||||
);
|
||||
|
||||
};
|
|
@ -283,3 +283,56 @@ GameLib.D3.Scene.FromObjectScene = function(
|
|||
computeNormals
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Adds a mesh to the scene
|
||||
* @param mesh GameLib.D3.Mesh
|
||||
*/
|
||||
GameLib.D3.Scene.prototype.addMesh = function(mesh) {
|
||||
|
||||
if (mesh instanceof GameLib.D3.Mesh) {
|
||||
this.meshes.push(mesh);
|
||||
} else if (mesh instanceof GameLib.D3.API.Mesh) {
|
||||
mesh = new GameLib.D3.Mesh(
|
||||
this.graphics,
|
||||
mesh,
|
||||
mesh.imageFactory,
|
||||
mesh.computeNormals
|
||||
)
|
||||
}
|
||||
|
||||
mesh.parentScene = this;
|
||||
|
||||
this.instance.add(mesh.instance);
|
||||
|
||||
this.buildIdToObject();
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mesh
|
||||
*/
|
||||
GameLib.D3.Scene.prototype.removeMesh = function(mesh) {
|
||||
|
||||
var index = -1;
|
||||
|
||||
if (mesh instanceof GameLib.D3.Mesh) {
|
||||
index = this.meshes.indexOf(mesh);
|
||||
} else {
|
||||
console.warn('Cannot remove this mesh - what is this ?' + mesh.toString());
|
||||
throw new Error('Cannot remove this mesh - what is this ?' + mesh.toString())
|
||||
}
|
||||
|
||||
this.instance.remove(mesh.instance);
|
||||
|
||||
if (index !== -1) {
|
||||
this.meshes.splice(index, 1);
|
||||
}
|
||||
|
||||
if (mesh.parentScene === this) {
|
||||
mesh.parentScene = null;
|
||||
}
|
||||
|
||||
this.buildIdToObject();
|
||||
};
|
|
@ -196,18 +196,24 @@ GameLib.Entity.prototype.updateInstance = function() {
|
|||
*/
|
||||
GameLib.Entity.prototype.toApiObject = function() {
|
||||
|
||||
var apiComponents = this.components.map(
|
||||
function(component) {
|
||||
return component.toApiObject();
|
||||
var apiComponents = this.components.reduce(
|
||||
function(result, component) {
|
||||
if (typeof component.toApiObject === 'function') {
|
||||
result.push(component.toApiObject());
|
||||
} else {
|
||||
console.log('ignored runtime component : ' + component.name);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
return new GameLib.API.Entity(
|
||||
this.id,
|
||||
this.name,
|
||||
apiComponents,
|
||||
this.parentEntity.id,
|
||||
this.parentEntityManager.id
|
||||
GameLib.Utils.IdOrNull(this.parentEntity),
|
||||
GameLib.Utils.IdOrNull(this.parentEntityManager)
|
||||
);
|
||||
|
||||
};
|
||||
|
|
|
@ -745,16 +745,109 @@ GameLib.GUI.prototype.buildSelectControl = function(folder, object, property, en
|
|||
object,
|
||||
property,
|
||||
options
|
||||
).name(property).listen().onChange(function(value){
|
||||
).name(property).listen().onChange(
|
||||
|
||||
function(gui) {
|
||||
|
||||
return function (value) {
|
||||
|
||||
if (value !== 'null') {
|
||||
object[property] = idToObject[value];
|
||||
} else {
|
||||
object[property] = null;
|
||||
}
|
||||
|
||||
if (property === 'parentScene') {
|
||||
/**
|
||||
* New way of doing things
|
||||
*/
|
||||
GameLib.Event.Emit(
|
||||
GameLib.Event.PARENT_SCENE_CHANGE,
|
||||
{
|
||||
old: this.initialValue,
|
||||
current: object[property],
|
||||
object: object,
|
||||
property: property
|
||||
}
|
||||
);
|
||||
} else {
|
||||
/**
|
||||
* Old way of doing things
|
||||
*/
|
||||
parentObject.buildIdToObject();
|
||||
object.updateInstance();
|
||||
this.build(entityManager);
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Properties changed - rebuild GUI
|
||||
*/
|
||||
gui.build(entityManager);
|
||||
};
|
||||
|
||||
}(this)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
GameLib.GUI.prototype.buildCurrentSelectionControlFromArray = function(
|
||||
folder,
|
||||
object,
|
||||
property,
|
||||
name
|
||||
) {
|
||||
|
||||
name = name || 'current ' + property;
|
||||
|
||||
object['current' + property] = null;
|
||||
|
||||
var options = object[property].reduce(
|
||||
function(result, obj){
|
||||
if (obj instanceof Object) {
|
||||
result[obj.name] = obj;
|
||||
} else {
|
||||
console.log('not an object');
|
||||
}
|
||||
return result;
|
||||
},
|
||||
{
|
||||
'none' : null
|
||||
}
|
||||
);
|
||||
|
||||
folder.add(object, 'current' + property, options).name(name).listen()
|
||||
};
|
||||
|
||||
GameLib.GUI.prototype.buildEntitySelectionControlFromArray = function(
|
||||
folder,
|
||||
object,
|
||||
property,
|
||||
entityManager
|
||||
) {
|
||||
|
||||
var options = entityManager.entities.reduce(
|
||||
function(result, obj){
|
||||
if (obj instanceof Object) {
|
||||
result[obj.name] = obj;
|
||||
} else {
|
||||
console.log('not an object');
|
||||
}
|
||||
return result;
|
||||
},
|
||||
{
|
||||
'none' : null
|
||||
}
|
||||
);
|
||||
|
||||
folder.add(object, property, options).name(property).listen().onChange(
|
||||
function(value) {
|
||||
GameLib.Event.Emit(
|
||||
GameLib.Event.PARENT_ENTITY_CHANGE,
|
||||
{}
|
||||
);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -775,7 +868,6 @@ GameLib.GUI.prototype.build = function(entityManager) {
|
|||
if (object.idToObject) {
|
||||
for (var property in object.idToObject) {
|
||||
if (object.idToObject.hasOwnProperty(property)) {
|
||||
|
||||
if (discoveredObjects.indexOf(object.idToObject[property]) === -1) {
|
||||
discoveredObjects.push(object.idToObject[property]);
|
||||
}
|
||||
|
@ -792,7 +884,11 @@ GameLib.GUI.prototype.build = function(entityManager) {
|
|||
|
||||
var apiObject = object.toApiObject();
|
||||
|
||||
try {
|
||||
var folder = this.instance.addFolder(apiObject.name);
|
||||
} catch (e) {
|
||||
//todo increase name count
|
||||
}
|
||||
|
||||
var property;
|
||||
|
||||
|
@ -805,9 +901,20 @@ GameLib.GUI.prototype.build = function(entityManager) {
|
|||
object.linkedObjects &&
|
||||
object.linkedObjects[property]
|
||||
) {
|
||||
|
||||
if (object.linkedObjects[property] instanceof Array) {
|
||||
if (property === 'parentEntity') {
|
||||
this.buildEntitySelectionControlFromArray(
|
||||
folder,
|
||||
object,
|
||||
property,
|
||||
entityManager
|
||||
)
|
||||
} else if (object.linkedObjects[property] instanceof Array) {
|
||||
console.log('ignored array : ' + property);
|
||||
this.buildCurrentSelectionControlFromArray(
|
||||
folder,
|
||||
object,
|
||||
property
|
||||
)
|
||||
} else {
|
||||
this.buildSelectControl(folder, object, property, entityManager, object.linkedObjects[property], parentObject);
|
||||
}
|
||||
|
|
|
@ -147,7 +147,6 @@ GameLib.System.prototype.start = function() {
|
|||
var guis = this.entityManager.queryComponents(GameLib.GUI);
|
||||
|
||||
guis.map(function(gui){
|
||||
gui.resize();
|
||||
gui.domElement.instance.parentElement.appendChild(gui.instance.domElement);
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in New Issue