Skip to content

Commit d89b6ba

Browse files
committed
Added Mesh methods, camera and projection matrix.
1 parent 302559f commit d89b6ba

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

v3/src/gameobjects/graphics/Graphics.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ var Ellipse = require('../../geom/ellipse/');
77
var GameObject = require('../GameObject');
88
var GetValue = require('../../utils/object/GetValue');
99
var MATH_CONST = require('../../math/const');
10+
var Matrix4 = require('../../math/Matrix4');
11+
var Mesh = require('../../geom/mesh/Mesh');
1012
var Render = require('./GraphicsRender');
13+
var Vector3 = require('../../math/Vector3');
1114

1215
var Graphics = new Class({
1316

@@ -49,6 +52,22 @@ var Graphics = new Class({
4952

5053
this.setDefaultStyles(options);
5154

55+
// Mesh viewport camera
56+
57+
this.viewportWidth = scene.sys.game.config.width;
58+
this.viewportHeight = scene.sys.game.config.height;
59+
60+
this.camera = {
61+
position: new Vector3(),
62+
target: new Vector3()
63+
};
64+
65+
this.up = new Vector3().up();
66+
this.projectionMatrix = new Matrix4();
67+
this.viewMatrix = new Matrix4().lookAt(this.camera.position, this.camera.target, this.up);
68+
69+
this.setViewport(this.viewportWidth, this.viewportHeight);
70+
5271
var resourceManager = scene.sys.game.renderer.resourceManager;
5372

5473
if (resourceManager !== undefined)
@@ -398,6 +417,151 @@ var Graphics = new Class({
398417
return this;
399418
},
400419

420+
// MESH + VIEWPORT + CAMERA
421+
422+
cameraX: {
423+
424+
get: function ()
425+
{
426+
return this.camera.position.x;
427+
},
428+
429+
set: function (value)
430+
{
431+
this.camera.position.x = value;
432+
this.viewMatrix.lookAt(this.camera.position, this.camera.target, this.up);
433+
}
434+
435+
},
436+
437+
cameraY: {
438+
439+
get: function ()
440+
{
441+
return this.camera.position.y;
442+
},
443+
444+
set: function (value)
445+
{
446+
this.camera.position.y = value;
447+
this.viewMatrix.lookAt(this.camera.position, this.camera.target, this.up);
448+
}
449+
450+
},
451+
452+
cameraZ: {
453+
454+
get: function ()
455+
{
456+
return this.camera.position.z;
457+
},
458+
459+
set: function (value)
460+
{
461+
this.camera.position.z = value;
462+
this.viewMatrix.lookAt(this.camera.position, this.camera.target, this.up);
463+
}
464+
465+
},
466+
467+
cameraTargetX: {
468+
469+
get: function ()
470+
{
471+
return this.camera.target.x;
472+
},
473+
474+
set: function (value)
475+
{
476+
this.camera.target.x = value;
477+
this.viewMatrix.lookAt(this.camera.position, this.camera.target, this.up);
478+
}
479+
480+
},
481+
482+
cameraTargetY: {
483+
484+
get: function ()
485+
{
486+
return this.camera.target.y;
487+
},
488+
489+
set: function (value)
490+
{
491+
this.camera.target.y = value;
492+
this.viewMatrix.lookAt(this.camera.position, this.camera.target, this.up);
493+
}
494+
495+
},
496+
497+
cameraTargetZ: {
498+
499+
get: function ()
500+
{
501+
return this.camera.target.z;
502+
},
503+
504+
set: function (value)
505+
{
506+
this.camera.target.z = value;
507+
this.viewMatrix.lookAt(this.camera.position, this.camera.target, this.up);
508+
}
509+
510+
},
511+
512+
setCameraPosition: function (x, y, z)
513+
{
514+
this.camera.position.set(x, y, z);
515+
516+
this.viewMatrix.lookAt(this.camera.position, this.camera.target, this.up);
517+
518+
return this;
519+
},
520+
521+
setCameraTarget: function (x, y, z)
522+
{
523+
this.camera.target.set(x, y, z);
524+
525+
this.viewMatrix.lookAt(this.camera.position, this.camera.target, this.up);
526+
527+
return this;
528+
},
529+
530+
// @param {number} fovy Vertical field of view in radians
531+
// @param {number} near Near bound of the frustum
532+
// @param {number} far Far bound of the frustum
533+
setViewport: function (width, height, fov, near, far)
534+
{
535+
if (fov === undefined) { fov = 0.8; }
536+
if (near === undefined) { near = 0.01; }
537+
if (far === undefined) { far = 1; }
538+
539+
this.viewportWidth = width;
540+
this.viewportHeight = height;
541+
542+
// fov, aspect, near, far
543+
this.projectionMatrix.perspective(fov, width / height, near, far);
544+
545+
return this;
546+
},
547+
548+
// Allow key to be a data array OR object containing the rest of the properties + color etc
549+
createMesh: function (key, x, y, z)
550+
{
551+
var data = this.scene.sys.cache.obj.get(key);
552+
553+
var mesh = new Mesh(data, x, y, z);
554+
555+
return mesh;
556+
},
557+
558+
strokeMesh: function (mesh)
559+
{
560+
mesh.draw(this);
561+
562+
return this;
563+
},
564+
401565
// TRANSFORM
402566

403567
save: function ()

0 commit comments

Comments
 (0)