Added Sprite3D Game Object and ability for Camera3D to manage them

This commit is contained in:
Richard Davey 2017-09-18 01:58:26 +01:00
parent 8363177369
commit 14e6f9f067
11 changed files with 249 additions and 20 deletions

View file

@ -1,6 +1,9 @@
var Class = require('../../utils/Class');
var Matrix4 = require('../../math/Matrix4');
var RotateVec3 = require('../../math/RotateVec3');
var Set = require('../../structs/Set');
var Sprite3D = require('../../gameobjects/sprite3d/Sprite3D');
var Vector2 = require('../../math/Vector2');
var Vector3 = require('../../math/Vector3');
var Vector4 = require('../../math/Vector4');
@ -13,8 +16,12 @@ var billboardMatrix = new Matrix4();
var Camera3D = new Class({
initialize: function ()
initialize: function (scene)
{
this.scene = scene;
this.name = '';
this.direction = new Vector3(0, 0, -1);
this.up = new Vector3(0, 1, 0);
this.position = new Vector3();
@ -36,6 +43,61 @@ var Camera3D = new Class({
this.viewportHeight = 0;
this.billboardMatrixDirty = true;
this.children = new Set();
},
setScene: function (scene)
{
this.scene = scene;
return this;
},
add: function (sprite3D)
{
this.children.set(sprite3D);
this.updateChildren();
return sprite3D;
},
remove: function (child)
{
this.children.delete(child);
return this;
},
clear: function ()
{
this.children.clear();
return this;
},
getChildren: function ()
{
return this.children.entries;
},
create: function (x, y, z, key, frame, visible)
{
if (visible === undefined) { visible = true; }
var child = new Sprite3D(this.scene, x, y, z, key, frame);
this.scene.sys.displayList.add(child.gameObject);
this.scene.sys.updateList.add(child.gameObject);
child.visible = visible;
this.children.set(child);
this.updateChildren();
return child;
},
setPosition: function (x, y, z)
@ -58,7 +120,7 @@ var Camera3D = new Class({
this.viewportWidth = width;
this.viewportHeight = height;
return this;
return this.update();
},
/**
@ -85,7 +147,7 @@ var Camera3D = new Class({
this.position.z += z || 0;
}
return this;
return this.update();
},
lookAt: function (x, y, z)
@ -110,7 +172,7 @@ var Camera3D = new Class({
// Calculate up vector
up.copy(tmpVec3).cross(dir).normalize();
return this;
return this.update();
},
rotate: function (radians, axis)
@ -118,7 +180,7 @@ var Camera3D = new Class({
RotateVec3(this.direction, axis, radians);
RotateVec3(this.up, axis, radians);
return this;
return this.update();
},
rotateAround: function (point, radians, axis)
@ -129,7 +191,7 @@ var Camera3D = new Class({
this.rotate(radians, axis);
this.translate(tmpVec3.negate());
return this;
return this.update();
},
project: function (vec, out)
@ -200,9 +262,21 @@ var Camera3D = new Class({
return this.ray;
},
updateChildren: function ()
{
var children = this.children.entries;
for (var i = 0; i < children.length; i++)
{
children[i].project(this);
}
},
// Overriden by subclasses
update: function ()
{
// Left empty for subclasses
this.updateChildren();
return this;
},
@ -295,6 +369,14 @@ var Camera3D = new Class({
return out.set(w, h);
},
destroy: function ()
{
this.children.clear();
this.scene = undefined;
this.children = undefined;
},
x: {
get: function ()
{

View file

@ -11,12 +11,12 @@ var OrthographicCamera = new Class({
Extends: Camera3D,
initialize: function (viewportWidth, viewportHeight)
initialize: function (scene, viewportWidth, viewportHeight)
{
if (viewportWidth === undefined) { viewportWidth = 0; }
if (viewportHeight === undefined) { viewportHeight = 0; }
Camera3D.call(this);
Camera3D.call(this, scene);
this.viewportWidth = viewportWidth;
this.viewportHeight = viewportHeight;
@ -80,6 +80,8 @@ var OrthographicCamera = new Class({
this.billboardMatrixDirty = true;
this.updateChildren();
return this;
},

View file

@ -13,13 +13,13 @@ var PerspectiveCamera = new Class({
Extends: Camera3D,
// FOV is converted to radians automatically
initialize: function (fieldOfView, viewportWidth, viewportHeight)
initialize: function (scene, fieldOfView, viewportWidth, viewportHeight)
{
if (fieldOfView === undefined) { fieldOfView = 80; }
if (viewportWidth === undefined) { viewportWidth = 0; }
if (viewportHeight === undefined) { viewportHeight = 0; }
Camera3D.call(this);
Camera3D.call(this, scene);
this.viewportWidth = viewportWidth;
this.viewportHeight = viewportHeight;
@ -28,6 +28,13 @@ var PerspectiveCamera = new Class({
this.update();
},
setFOV: function (value)
{
this.fieldOfView = value * Math.PI / 180;
return this;
},
update: function ()
{
var aspect = this.viewportWidth / this.viewportHeight;
@ -53,6 +60,8 @@ var PerspectiveCamera = new Class({
this.billboardMatrixDirty = true;
this.updateChildren();
return this;
}

View file

@ -29,7 +29,9 @@ var CameraManager = new Class({
},
add: require('./inc/Add2DCamera'),
add3D: require('./inc/Add3DCamera'),
add3D: require('./inc/AddPerspectiveCamera'),
addPerspectiveCamera: require('./inc/AddPerspectiveCamera'),
addOrthographicCamera: require('./inc/AddOrthographicCamera'),
addExisting: require('./inc/AddExisting'),
addKeyControl: require('./inc/AddKeyControl'),
addSmoothedKeyControl: require('./inc/AddsmoothedKeyControl'),

View file

@ -1,8 +0,0 @@
var OrthographicCamera = require('../../3d/OrthographicCamera');
var PerspectiveCamera = require('../../3d/PerspectiveCamera');
var Add3DCamera = function ()
{
};
module.exports = Add3DCamera;

View file

@ -0,0 +1,15 @@
var OrthographicCamera = require('../../3d/OrthographicCamera');
var AddOrthographicCamera = function (width, height)
{
var config = this.scene.sys.game.config;
if (width === undefined) { width = config.width; }
if (height === undefined) { height = config.height; }
var camra = new OrthographicCamera(this.scene, width, height);
return camera;
};
module.exports = AddOrthographicCamera;

View file

@ -0,0 +1,16 @@
var PerspectiveCamera = require('../../3d/PerspectiveCamera');
var AddPerspectiveCamera = function (fieldOfView, width, height)
{
var config = this.scene.sys.game.config;
if (fieldOfView === undefined) { fieldOfView = 80; }
if (width === undefined) { width = config.width; }
if (height === undefined) { height = config.height; }
var camera = new PerspectiveCamera(this.scene, fieldOfView, width, height);
return camera;
};
module.exports = AddPerspectiveCamera;

View file

@ -13,6 +13,7 @@ var GameObjects = {
ObjectPool: require('./pool/ObjectPool.js'),
ParticleEmitter: require('./emitter/ParticleEmitter'),
Sprite: require('./sprite/Sprite'),
Sprite3D: require('./sprite3d/Sprite3D'),
StaticTilemap: require('./tilemap/static/StaticTilemap'),
Text: require('./text/static/Text'),
Tilemap: require('./tilemap/dynamic/Tilemap'),
@ -30,6 +31,7 @@ var GameObjects = {
Image: require('./image/ImageFactory'),
ParticleEmitter: require('./emitter/ParticleEmitterFactory'),
Sprite: require('./sprite/SpriteFactory'),
Sprite3D: require('./sprite3d/Sprite3DFactory'),
StaticBitmapText: require('./bitmaptext/static/BitmapTextFactory'),
StaticTilemap: require('./tilemap/static/StaticTilemapFactory'),
Text: require('./text/static/TextFactory'),
@ -46,6 +48,7 @@ var GameObjects = {
Image: require('./image/ImageCreator'),
ParticleEmitter: require('./emitter/ParticleEmitterCreator'),
Sprite: require('./sprite/SpriteCreator'),
Sprite3D: require('./sprite3d/Sprite3DCreator'),
StaticBitmapText: require('./bitmaptext/static/BitmapTextCreator'),
StaticTilemap: require('./tilemap/static/StaticTilemapCreator'),
Text: require('./text/static/TextCreator'),

View file

@ -0,0 +1,63 @@
var Class = require('../../utils/Class');
var GameObject = require('../GameObject');
var Sprite = require('../sprite/Sprite');
var Vector2 = require('../../math/Vector2');
var Vector4 = require('../../math/Vector4');
var Sprite3D = new Class({
Extends: GameObject,
initialize:
function Sprite3D (scene, x, y, z, texture, frame)
{
GameObject.call(this, scene, 'Sprite3D');
this.gameObject = new Sprite(scene, 0, 0, texture, frame);
this.position = new Vector4(x, y, z);
this.size = new Vector2(1, 1);
this._visible = true;
},
project: function (camera)
{
var gameObject = this.gameObject;
camera.project(this.position, gameObject);
var scale = camera.getPointSize(this.position, this.size);
gameObject.setScale(scale.x, scale.y).setDepth(gameObject.z);
gameObject.setVisible((this.position.z > camera.z));
},
visible: {
get: function ()
{
return this._visible;
},
set: function (value)
{
this._visible = value;
this.gameObject.visible = value;
}
},
setVisible: function (value)
{
this.visible = value;
return this;
}
});
module.exports = Sprite3D;

View file

@ -0,0 +1,25 @@
var BuildGameObject = require('../BuildGameObject');
var BuildGameObjectAnimation = require('../BuildGameObjectAnimation');
var GameObjectCreator = require('../../scene/plugins/GameObjectCreator');
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
var Sprite3D = require('./Sprite3D');
// When registering a factory function 'this' refers to the GameObjectCreator context.
GameObjectCreator.register('sprite3D', function (config)
{
var key = GetAdvancedValue(config, 'key', null);
var frame = GetAdvancedValue(config, 'frame', null);
var sprite = new Sprite3D(this.scene, 0, 0, key, frame);
BuildGameObject(this.scene, sprite, config);
// Sprite specific config options:
BuildGameObjectAnimation(sprite, config);
// Physics, Input, etc to follow ...
return sprite;
});

View file

@ -0,0 +1,20 @@
var Sprite3D = require('./Sprite3D');
var GameObjectFactory = require('../../scene/plugins/GameObjectFactory');
// When registering a factory function 'this' refers to the GameObjectFactory context.
//
// There are several properties available to use:
//
// this.scene - a reference to the Scene that owns the GameObjectFactory
// this.displayList - a reference to the Display List the Scene owns
// this.updateList - a reference to the Update List the Scene owns
GameObjectFactory.register('sprite3D', function (x, y, z, key, frame)
{
var sprite = new Sprite3D(this.scene, x, y, z, key, frame);
this.displayList.add(sprite.gameObject);
this.updateList.add(sprite.gameObject);
return sprite;
});