phaser/src/cameras/sprite3d/Camera.js

1063 lines
26 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var Matrix4 = require('../../math/Matrix4');
2017-09-18 20:48:38 +00:00
var RandomXYZ = require('../../math/RandomXYZ');
var RandomXYZW = require('../../math/RandomXYZW');
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');
2017-09-16 01:31:33 +00:00
// Local cache vars
2017-09-15 03:04:51 +00:00
var tmpVec3 = new Vector3();
var tmpVec4 = new Vector4();
var dirvec = new Vector3();
var rightvec = new Vector3();
var billboardMatrix = new Matrix4();
2017-09-15 03:04:51 +00:00
// @author attribute https://github.com/mattdesl/cam3d/wiki
2018-02-07 15:27:21 +00:00
/**
* @classdesc
* [description]
*
* @class Camera
* @memberOf Phaser.Cameras.Sprite3D
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*/
var Camera = new Class({
2017-09-15 03:04:51 +00:00
2017-10-06 17:42:53 +00:00
initialize:
function Camera (scene)
2017-09-16 01:31:33 +00:00
{
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#scene
* @type {Phaser.Scene}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
this.scene = scene;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#displayList
2018-02-22 01:07:30 +00:00
* @type {Phaser.GameObjects.DisplayList}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
this.displayList = scene.sys.displayList;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#updateList
2018-02-22 01:07:30 +00:00
* @type {Phaser.GameObjects.UpdateList}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
this.updateList = scene.sys.updateList;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#name
* @type {string}
2018-01-25 05:11:52 +00:00
* @default ''
* @since 3.0.0
*/
this.name = '';
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#direction
* @type {Phaser.Math.Vector3}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.direction = new Vector3(0, 0, -1);
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#up
* @type {Phaser.Math.Vector3}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.up = new Vector3(0, 1, 0);
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#position
* @type {Phaser.Math.Vector3}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.position = new Vector3();
// The mapping from 3D size units to pixels.
// In the default case 1 3D unit = 128 pixels. So a sprite that is
// 256 x 128 px in size will be 2 x 1 units.
// Change to whatever best fits your game assets.
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#pixelScale
* @type {number}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
this.pixelScale = 128;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#projection
* @type {Phaser.Math.Matrix4}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.projection = new Matrix4();
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#view
* @type {Phaser.Math.Matrix4}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.view = new Matrix4();
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#combined
* @type {Phaser.Math.Matrix4}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.combined = new Matrix4();
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#invProjectionView
* @type {Phaser.Math.Matrix4}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.invProjectionView = new Matrix4();
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#near
* @type {number}
2018-01-25 05:11:52 +00:00
* @default 1
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.near = 1;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#far
* @type {number}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.far = 100;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#ray
2018-03-19 00:49:29 +00:00
* @type {{origin:Phaser.Math.Vector3,direction:Phaser.Math.Vector3}}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.ray = {
origin: new Vector3(),
direction: new Vector3()
};
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#viewportWidth
* @type {number}
2018-01-25 05:11:52 +00:00
* @default 0
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.viewportWidth = 0;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#viewportHeight
* @type {number}
2018-01-25 05:11:52 +00:00
* @default 0
* @since 3.0.0
*/
2017-09-15 03:04:51 +00:00
this.viewportHeight = 0;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#billboardMatrixDirty
* @type {boolean}
2018-01-25 05:11:52 +00:00
* @default true
* @since 3.0.0
*/
this.billboardMatrixDirty = true;
2018-01-25 05:11:52 +00:00
/**
* [description]
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cameras.Sprite3D#children
* @type {Phaser.Structs.Set}
2018-01-25 05:11:52 +00:00
* @since 3.0.0
*/
this.children = new Set();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#setPosition
* @since 3.0.0
*
2018-02-12 23:51:47 +00:00
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} z - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
setPosition: function (x, y, z)
{
this.position.set(x, y, z);
return this.update();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#setScene
* @since 3.0.0
*
2018-02-12 23:51:47 +00:00
* @param {Phaser.Scene} scene - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
setScene: function (scene)
{
this.scene = scene;
return this;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#setPixelScale
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number} value - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-18 20:48:38 +00:00
setPixelScale: function (value)
{
this.pixelScale = value;
return this.update();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#add
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {Phaser.GameObjects.Sprite3D} sprite3D - [description]
2018-01-25 05:11:52 +00:00
*
2018-03-19 00:49:29 +00:00
* @return {Phaser.GameObjects.Sprite3D} [description]
2018-01-25 05:11:52 +00:00
*/
add: function (sprite3D)
{
this.children.set(sprite3D);
this.updateChildren();
return sprite3D;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#remove
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {Phaser.GameObjects.GameObject} child - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
remove: function (child)
{
this.displayList.remove(child.gameObject);
this.updateList.remove(child.gameObject);
this.children.delete(child);
return this;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#clear
* @since 3.0.0
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
clear: function ()
{
var children = this.getChildren();
for (var i = 0; i < children.length; i++)
{
this.remove(children[i]);
}
return this;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#getChildren
* @since 3.0.0
*
2018-02-12 23:51:47 +00:00
* @return {array} [description]
2018-01-25 05:11:52 +00:00
*/
getChildren: function ()
{
return this.children.entries;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#create
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} z - [description]
* @param {string} key - [description]
* @param {string|number} frame - [description]
* @param {boolean} [visible=true] - [description]
2018-01-25 05:11:52 +00:00
*
2018-03-19 00:49:29 +00:00
* @return {Phaser.GameObjects.Sprite3D} [description]
2018-01-25 05:11:52 +00:00
*/
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.displayList.add(child.gameObject);
this.updateList.add(child.gameObject);
child.visible = visible;
this.children.set(child);
this.updateChildren();
return child;
2017-09-15 03:04:51 +00:00
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#createMultiple
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number} quantity - [description]
* @param {string} key - [description]
* @param {string|number} frame - [description]
* @param {boolean} [visible=true] - [description]
2018-01-25 05:11:52 +00:00
*
2018-03-19 00:49:29 +00:00
* @return {Phaser.GameObjects.Sprite3D[]} [description]
2018-01-25 05:11:52 +00:00
*/
createMultiple: function (quantity, key, frame, visible)
{
if (visible === undefined) { visible = true; }
var output = [];
for (var i = 0; i < quantity; i++)
{
var child = new Sprite3D(this.scene, 0, 0, 0, key, frame);
this.displayList.add(child.gameObject);
this.updateList.add(child.gameObject);
child.visible = visible;
this.children.set(child);
output.push(child);
}
return output;
},
2017-09-19 00:35:01 +00:00
// Create a bunch of Sprite3D objects in a rectangle
// size and spacing are Vec3s (or if integers are converted to vec3s)
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#createRect
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number|{x:number,y:number}} size - [description]
* @param {number|{x:number,y:number,z:number}} spacing - [description]
* @param {string} key - [description]
* @param {string|number} [frame] - [description]
2018-01-25 05:11:52 +00:00
*
2018-03-19 00:49:29 +00:00
* @return {Phaser.GameObjects.Sprite3D[]} [description]
2018-01-25 05:11:52 +00:00
*/
2017-09-19 00:35:01 +00:00
createRect: function (size, spacing, key, frame)
{
if (typeof size === 'number') { size = { x: size, y: size, z: size }; }
if (typeof spacing === 'number') { spacing = { x: spacing, y: spacing, z: spacing }; }
var quantity = size.x * size.y * size.z;
var sprites = this.createMultiple(quantity, key, frame);
var i = 0;
for (var z = 0.5 - (size.z / 2); z < (size.z / 2); z++)
{
for (var y = 0.5 - (size.y / 2); y < (size.y / 2); y++)
{
for (var x = 0.5 - (size.x / 2); x < (size.x / 2); x++)
{
var bx = (x * spacing.x);
var by = (y * spacing.y);
var bz = (z * spacing.z);
sprites[i].position.set(bx, by, bz);
i++;
}
}
}
this.update();
return sprites;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#randomSphere
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number} [radius=1] - [description]
* @param {Phaser.GameObjects.Sprite3D[]} [sprites] - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
randomSphere: function (radius, sprites)
{
if (sprites === undefined) { sprites = this.getChildren(); }
for (var i = 0; i < sprites.length; i++)
{
RandomXYZ(sprites[i].position, radius);
}
return this.update();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#randomCube
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {float} [scale=1] - [description]
* @param {Phaser.GameObjects.Sprite3D[]} [sprites] - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
randomCube: function (scale, sprites)
2017-09-18 20:48:38 +00:00
{
if (sprites === undefined) { sprites = this.getChildren(); }
2017-09-18 20:48:38 +00:00
for (var i = 0; i < sprites.length; i++)
2017-09-18 20:48:38 +00:00
{
RandomXYZW(sprites[i].position, scale);
2017-09-18 20:48:38 +00:00
}
return this.update();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#translateChildren
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {Phaser.Math.Vector3} vec3 - [description]
* @param {Phaser.GameObjects.Sprite3D[]} sprites - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
translateChildren: function (vec3, sprites)
2017-09-18 20:48:38 +00:00
{
if (sprites === undefined) { sprites = this.getChildren(); }
2017-09-18 20:48:38 +00:00
for (var i = 0; i < sprites.length; i++)
2017-09-18 20:48:38 +00:00
{
sprites[i].position.add(vec3);
2017-09-18 20:48:38 +00:00
}
return this.update();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#transformChildren
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {Phaser.Math.Matrix4} mat4 - [description]
* @param {Phaser.GameObjects.Sprite3D[]} sprites - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
transformChildren: function (mat4, sprites)
2017-09-18 20:48:38 +00:00
{
if (sprites === undefined) { sprites = this.getChildren(); }
2017-09-18 20:48:38 +00:00
for (var i = 0; i < sprites.length; i++)
2017-09-18 20:48:38 +00:00
{
sprites[i].position.transformMat4(mat4);
2017-09-18 20:48:38 +00:00
}
return this.update();
2017-09-18 20:48:38 +00:00
},
2017-09-15 03:04:51 +00:00
/**
2018-02-12 23:51:47 +00:00
* Sets the width and height of the viewport. Does not update any matrices.
2018-01-25 05:11:52 +00:00
*
* @method Phaser.Cameras.Sprite3D.Camera#setViewport
* @since 3.0.0
*
2018-02-12 23:51:47 +00:00
* @param {number} width - [description]
* @param {number} height - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
setViewport: function (width, height)
{
2017-09-15 03:04:51 +00:00
this.viewportWidth = width;
this.viewportHeight = height;
2017-09-16 01:31:33 +00:00
return this.update();
2017-09-15 03:04:51 +00:00
},
/**
* Translates this camera by a specified Vector3 object
* or x, y, z parameters. Any undefined x y z values will
* default to zero, leaving that component unaffected.
* If you wish to set the camera position directly call setPosition instead.
2017-09-15 03:04:51 +00:00
*
2018-01-25 05:11:52 +00:00
* @method Phaser.Cameras.Sprite3D.Camera#translate
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number|object} x - [description]
* @param {number} [y] - [description]
* @param {number} [z] - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
translate: function (x, y, z)
{
if (typeof x === 'object')
{
2017-09-15 03:04:51 +00:00
this.position.x += x.x || 0;
this.position.y += x.y || 0;
this.position.z += x.z || 0;
2017-09-16 01:31:33 +00:00
}
else
{
2017-09-15 03:04:51 +00:00
this.position.x += x || 0;
this.position.y += y || 0;
this.position.z += z || 0;
}
2017-09-16 01:31:33 +00:00
return this.update();
2017-09-15 03:04:51 +00:00
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#lookAt
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number|object} x - [description]
* @param {number} [y] - [description]
* @param {number} [z] - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
lookAt: function (x, y, z)
{
var dir = this.direction;
var up = this.up;
2017-09-15 03:04:51 +00:00
2017-09-16 01:31:33 +00:00
if (typeof x === 'object')
{
2017-09-15 03:04:51 +00:00
dir.copy(x);
2017-09-16 01:31:33 +00:00
}
else
{
2017-09-15 03:04:51 +00:00
dir.set(x, y, z);
}
dir.subtract(this.position).normalize();
2017-09-15 03:04:51 +00:00
2017-09-16 01:31:33 +00:00
// Calculate right vector
2017-09-15 03:04:51 +00:00
tmpVec3.copy(dir).cross(up).normalize();
2017-09-16 01:31:33 +00:00
// Calculate up vector
2017-09-15 03:04:51 +00:00
up.copy(tmpVec3).cross(dir).normalize();
2017-09-16 01:31:33 +00:00
return this.update();
2017-09-15 03:04:51 +00:00
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#rotate
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {float} radians - [description]
* @param {Phaser.Math.Vector3} axis - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
rotate: function (radians, axis)
{
RotateVec3(this.direction, axis, radians);
RotateVec3(this.up, axis, radians);
return this.update();
2017-09-15 03:04:51 +00:00
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#rotateAround
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {Phaser.Math.Vector3} point - [description]
* @param {float} radians - [description]
* @param {Phaser.Math.Vector3} axis - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
rotateAround: function (point, radians, axis)
{
tmpVec3.copy(point).subtract(this.position);
2017-09-16 01:31:33 +00:00
2017-09-16 03:00:45 +00:00
this.translate(tmpVec3);
2017-09-15 03:04:51 +00:00
this.rotate(radians, axis);
2017-09-16 03:00:45 +00:00
this.translate(tmpVec3.negate());
2017-09-16 01:31:33 +00:00
return this.update();
2017-09-15 03:04:51 +00:00
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#project
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {Phaser.Math.Vector3} vec - [description]
* @param {Phaser.Math.Vector4} out - [description]
2018-01-25 05:11:52 +00:00
*
2018-03-19 00:49:29 +00:00
* @return {Phaser.Math.Vector4} [description]
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
project: function (vec, out)
{
if (out === undefined) { out = new Vector4(); }
2017-09-15 03:04:51 +00:00
2017-09-16 01:31:33 +00:00
// TODO: support viewport XY
var viewportWidth = this.viewportWidth;
var viewportHeight = this.viewportHeight;
var n = Camera.NEAR_RANGE;
var f = Camera.FAR_RANGE;
2017-09-15 03:04:51 +00:00
2017-09-16 01:31:33 +00:00
// For useful Z and W values we should do the usual steps: clip space -> NDC -> window coords
2017-09-15 03:04:51 +00:00
2017-09-16 01:31:33 +00:00
// Implicit 1.0 for w component
2017-09-15 03:04:51 +00:00
tmpVec4.set(vec.x, vec.y, vec.z, 1.0);
2017-09-16 01:31:33 +00:00
// Transform into clip space
2017-09-15 03:04:51 +00:00
tmpVec4.transformMat4(this.combined);
// Avoid divide by zero when 0x0x0 camera projects to a 0x0x0 vec3
if (tmpVec4.w === 0)
{
tmpVec4.w = 1;
}
2017-09-16 01:31:33 +00:00
// Now into NDC
2017-09-15 03:04:51 +00:00
tmpVec4.x = tmpVec4.x / tmpVec4.w;
tmpVec4.y = tmpVec4.y / tmpVec4.w;
tmpVec4.z = tmpVec4.z / tmpVec4.w;
2017-09-16 01:31:33 +00:00
// And finally into window coordinates
2017-09-15 03:04:51 +00:00
out.x = viewportWidth / 2 * tmpVec4.x + (0 + viewportWidth / 2);
out.y = viewportHeight / 2 * tmpVec4.y + (0 + viewportHeight / 2);
out.z = (f - n) / 2 * tmpVec4.z + (f + n) / 2;
2017-09-16 01:31:33 +00:00
// If the out vector has a fourth component, we also store (1/clip.w), same idea as gl_FragCoord.w
2017-09-15 03:04:51 +00:00
if (out.w === 0 || out.w)
2017-09-16 01:31:33 +00:00
{
2017-09-15 03:04:51 +00:00
out.w = 1 / tmpVec4.w;
2017-09-16 01:31:33 +00:00
}
2017-09-15 03:04:51 +00:00
return out;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#unproject
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {Phaser.Math.Vector4} vec - [description]
* @param {Phaser.Math.Vector3} out - [description]
2018-01-25 05:11:52 +00:00
*
2018-03-19 00:49:29 +00:00
* @return {Phaser.Math.Vector3} [description]
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
unproject: function (vec, out)
{
if (out === undefined) { out = new Vector3(); }
2017-09-15 03:04:51 +00:00
var viewport = tmpVec4.set(0, 0, this.viewportWidth, this.viewportHeight);
2017-09-16 01:31:33 +00:00
2017-09-15 03:04:51 +00:00
return out.copy(vec).unproject(viewport, this.invProjectionView);
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#getPickRay
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number} x - [description]
* @param {number} [y] - [description]
2018-01-25 05:11:52 +00:00
*
2018-03-19 00:49:29 +00:00
* @return {{origin:Phaser.Math.Vector3,direction:Phaser.Math.Vector3}} [description]
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
getPickRay: function (x, y)
{
var origin = this.ray.origin.set(x, y, 0);
var direction = this.ray.direction.set(x, y, 1);
var viewport = tmpVec4.set(0, 0, this.viewportWidth, this.viewportHeight);
var mtx = this.invProjectionView;
2017-09-15 03:04:51 +00:00
origin.unproject(viewport, mtx);
2017-09-16 01:31:33 +00:00
2017-09-15 03:04:51 +00:00
direction.unproject(viewport, mtx);
direction.subtract(origin).normalize();
2017-09-16 01:31:33 +00:00
2017-09-15 03:04:51 +00:00
return this.ray;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#updateChildren
* @since 3.0.0
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
updateChildren: function ()
{
var children = this.children.entries;
for (var i = 0; i < children.length; i++)
{
children[i].project(this);
}
2017-09-18 20:48:38 +00:00
return this;
},
// Overriden by subclasses
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#update
* @since 3.0.0
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-16 01:31:33 +00:00
update: function ()
{
2017-09-18 20:48:38 +00:00
return this.updateChildren();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#updateBillboardMatrix
* @since 3.0.0
*/
updateBillboardMatrix: function ()
{
var dir = dirvec.set(this.direction).negate();
// Better view-aligned billboards might use this:
// var dir = tmp.set(camera.position).subtract(p).normalize();
2018-03-19 00:49:29 +00:00
var right = rightvec.set(this.up).cross(dir).normalize();
var up = tmpVec3.set(dir).cross(right).normalize();
var out = billboardMatrix.val;
out[0] = right.x;
out[1] = right.y;
out[2] = right.z;
out[3] = 0;
out[4] = up.x;
out[5] = up.y;
out[6] = up.z;
out[7] = 0;
out[8] = dir.x;
out[9] = dir.y;
out[10] = dir.z;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
this.billboardMatrixDirty = false;
},
2018-03-19 00:49:29 +00:00
/**
2018-03-19 00:49:29 +00:00
* This is a utility function for canvas 3D rendering,
* which determines the "point size" of a camera-facing
2018-03-19 00:49:29 +00:00
* sprite billboard given its 3D world position
* (origin at center of sprite) and its world width
2018-03-19 00:49:29 +00:00
* and height in x/y.
*
* We place into the output Vector2 the scaled width
* and height. If no `out` is specified, a new Vector2
2018-03-19 00:49:29 +00:00
* will be created for convenience (this should be avoided
* in tight loops).
2018-01-25 05:11:52 +00:00
*
* @method Phaser.Cameras.Sprite3D.Camera#getPointSize
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {Phaser.Math.Vector2} vec - The position of the 3D Sprite.
* @param {Phaser.Math.Vector2} size - The x and y dimensions.
* @param {Phaser.Math.Vector2} out - The result, scaled x and y dimensions.
2018-01-25 05:11:52 +00:00
*
2018-03-19 00:49:29 +00:00
* @return {Phaser.Math.Vector2} [description]
2018-01-25 05:11:52 +00:00
*/
getPointSize: function (vec, size, out)
{
if (out === undefined) { out = new Vector2(); }
2018-02-16 18:17:51 +00:00
// TODO: optimize this with a simple distance calculation:
// https://developer.valvesoftware.com/wiki/Field_of_View
if (this.billboardMatrixDirty)
{
this.updateBillboardMatrix();
}
var tmp = tmpVec3;
var dx = (size.x / this.pixelScale) / 2;
var dy = (size.y / this.pixelScale) / 2;
tmp.set(-dx, -dy, 0).transformMat4(billboardMatrix).add(vec);
this.project(tmp, tmp);
var tlx = tmp.x;
var tly = tmp.y;
tmp.set(dx, dy, 0).transformMat4(billboardMatrix).add(vec);
this.project(tmp, tmp);
var brx = tmp.x;
var bry = tmp.y;
// var w = Math.abs(brx - tlx);
// var h = Math.abs(bry - tly);
// Allow the projection to get negative ...
var w = brx - tlx;
var h = bry - tly;
return out.set(w, h);
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.children.clear();
this.scene = undefined;
this.children = undefined;
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#setX
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number} value - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-18 20:48:38 +00:00
setX: function (value)
{
this.position.x = value;
return this.update();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#setY
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number} value - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-18 20:48:38 +00:00
setY: function (value)
{
this.position.y = value;
return this.update();
},
2018-01-25 05:11:52 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Sprite3D.Camera#setZ
* @since 3.0.0
*
2018-03-19 00:49:29 +00:00
* @param {number} value - [description]
2018-01-25 05:11:52 +00:00
*
2018-02-12 23:51:47 +00:00
* @return {Phaser.Cameras.Sprite3D.Camera} This Camera object.
2018-01-25 05:11:52 +00:00
*/
2017-09-18 20:48:38 +00:00
setZ: function (value)
{
this.position.z = value;
return this.update();
},
2018-02-12 23:51:47 +00:00
/**
* [description]
*
* @name Phaser.Cameras.Sprite3D.Camera#x
* @type {number}
* @since 3.0.0
*/
x: {
get: function ()
{
return this.position.x;
},
set: function (value)
{
this.position.x = value;
this.update();
}
},
2018-02-12 23:51:47 +00:00
/**
* [description]
*
* @name Phaser.Cameras.Sprite3D.Camera#y
* @type {number}
* @since 3.0.0
*/
y: {
get: function ()
{
return this.position.y;
},
set: function (value)
{
this.position.y = value;
this.update();
}
},
2018-02-12 23:51:47 +00:00
/**
* [description]
*
* @name Phaser.Cameras.Sprite3D.Camera#z
* @type {number}
* @since 3.0.0
*/
z: {
get: function ()
{
return this.position.z;
},
set: function (value)
{
this.position.z = value;
this.update();
}
2017-09-15 03:04:51 +00:00
}
2017-09-15 03:04:51 +00:00
});
Camera.FAR_RANGE = 1.0;
Camera.NEAR_RANGE = 0.0;
2017-09-15 03:04:51 +00:00
module.exports = Camera;