Skip to content

Commit abf7756

Browse files
committed
Starting to flesh out the Layer3D Plugin basics
1 parent 1c2c479 commit abf7756

24 files changed

Lines changed: 1792 additions & 2 deletions

src/layer3d/GameObject3D.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var Class = require('../utils/Class');
8+
var Euler = require('../math/Euler');
9+
var Matrix4 = require('../math/Matrix4');
10+
var Quaternion = require('../math/Quaternion');
11+
var Vector3 = require('../math/Vector3');
12+
13+
var id = 0;
14+
var tempMatrix = new Matrix4();
15+
16+
var GameObject3D = new Class({
17+
18+
initialize:
19+
20+
function GameObject3D ()
21+
{
22+
this.id = id++;
23+
this.name = '';
24+
this.type = '';
25+
26+
this.position = new Vector3();
27+
this.scale = new Vector3(1, 1, 1);
28+
29+
this.euler = new Euler();
30+
this.quaternion = new Quaternion();
31+
32+
this.matrix = new Matrix4();
33+
this.worldMatrix = new Matrix4();
34+
35+
this.children = [];
36+
this.parent = null;
37+
38+
this.castShadow = false;
39+
this.receiveShadow = false;
40+
this.shadowType = 0;
41+
42+
this.frustumCulled = true;
43+
this.visible = true;
44+
45+
this.renderOrder = 0;
46+
47+
this.matrixAutoupdate = true;
48+
this.matrixNeedsUpdate = true;
49+
this.worldMatrixNeedsUpdate = true;
50+
51+
var euler = this.euler;
52+
var quat = this.quaternion;
53+
54+
euler.onChangeCallback = function ()
55+
{
56+
quat.setFromEuler(euler, false);
57+
};
58+
59+
quat.onChangeCallback = function ()
60+
{
61+
euler.setFromQuaternion(quat);
62+
};
63+
},
64+
65+
// Swap for already created functions
66+
add: function (child)
67+
{
68+
if (child !== this)
69+
{
70+
if (child.parent !== null)
71+
{
72+
child.parent.remove(child);
73+
}
74+
75+
child.parent = this;
76+
77+
this.children.push(child);
78+
79+
child.worldMatrixNeedsUpdate = true;
80+
}
81+
82+
return this;
83+
},
84+
85+
// Swap for already created functions
86+
remove: function (child)
87+
{
88+
var index = this.children.indexOf(child);
89+
90+
if (index > -1)
91+
{
92+
child.parent = null;
93+
94+
this.children.splice(index, 1);
95+
96+
child.worldMatrixNeedsUpdate = true;
97+
}
98+
99+
return this;
100+
},
101+
102+
updateMatrix: function (force)
103+
{
104+
if (this.matrixAutoupdate || this.matrixNeedsUpdate)
105+
{
106+
this.matrix.transform(this.position, this.scale, this.quaternion);
107+
108+
this.matrixNeedsUpdate = false;
109+
this.worldMatrixNeedsUpdate = true;
110+
}
111+
112+
if (this.worldMatrixNeedsUpdate || force)
113+
{
114+
this.worldMatrix.copy(this.matrix);
115+
116+
if (this.parent)
117+
{
118+
var parentMatrix = this.parent.worldMatrix;
119+
120+
this.worldMatrix.premultiply(parentMatrix);
121+
}
122+
123+
this.worldMatrixNeedsUpdate = false;
124+
force = true;
125+
}
126+
127+
var children = this.children;
128+
129+
for (var i = 0; i < children.length; i++)
130+
{
131+
children[i].updateMatrix(force);
132+
}
133+
},
134+
135+
getWorldDirection: function (target)
136+
{
137+
if (target === undefined) { target = new Vector3(); }
138+
139+
var world = this.worldMatrix.val;
140+
141+
return target.set(world[8], world[9], world[10]).normalize();
142+
},
143+
144+
lookAt: function (target, up)
145+
{
146+
tempMatrix.lookAtRH(target, this.position, up);
147+
148+
this.quaternion.setFromRotationMatrix(tempMatrix);
149+
},
150+
151+
destroy: function ()
152+
{
153+
154+
}
155+
156+
});
157+
158+
module.exports = GameObject3D;

src/layer3d/Layer3DScene.js

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var Class = require('../utils/Class');
8+
var Components = require('../gameobjects/components');
9+
var CONST = require('../renderer/webgl/pipelines/const');
10+
var GameObject = require('../gameobjects/GameObject');
11+
var GameObject3D = require('./GameObject3D');
12+
var GameObjectEvents = require('../gameobjects/events');
13+
var Layer3DRender = require('./Layer3DSceneRender');
14+
var RenderList = require('./render/RenderList');
15+
16+
// Namespace: Phaser.Layer3D
17+
// Class: Layer3DScene
18+
19+
var Layer3DScene = new Class({
20+
21+
Extends: GameObject,
22+
23+
Mixins: [
24+
Components.AlphaSingle,
25+
Components.BlendMode,
26+
Components.Depth,
27+
Components.Mask,
28+
Components.Pipeline,
29+
Components.Transform,
30+
Components.Visible,
31+
Components.ScrollFactor,
32+
Components.Size,
33+
Layer3DRender
34+
],
35+
36+
initialize:
37+
38+
function Layer3DScene (scene, x, y)
39+
{
40+
GameObject.call(this, scene, 'Layer3DScene');
41+
42+
var renderer = scene.sys.renderer;
43+
44+
this.world = new GameObject3D();
45+
46+
this.overrideMaterial = null;
47+
48+
this.fog = null;
49+
50+
this.clippingPlanes = [];
51+
52+
// this.lights = new LightCache();
53+
54+
this.renderListMap = new WeakMap();
55+
56+
// This will be an fbo:
57+
this.setPosition(x, y);
58+
this.setSize(renderer.width, renderer.height);
59+
this.initPipeline(CONST.MULTI_PIPELINE);
60+
61+
this.on(GameObjectEvents.ADDED_TO_SCENE, this.addedToScene, this);
62+
this.on(GameObjectEvents.REMOVED_FROM_SCENE, this.removedFromScene, this);
63+
},
64+
65+
// Overrides Game Object method
66+
addedToScene: function ()
67+
{
68+
this.scene.sys.updateList.add(this);
69+
},
70+
71+
// Overrides Game Object method
72+
removedFromScene: function ()
73+
{
74+
this.scene.sys.updateList.remove(this);
75+
},
76+
77+
updateRenderList: function (camera)
78+
{
79+
if (!this.renderListMap.has(camera))
80+
{
81+
this.renderListMap.set(camera, new RenderList());
82+
}
83+
84+
var renderList = this.renderListMap.get(camera);
85+
86+
renderList.startCount();
87+
88+
this.doUpdateRenderList(this, camera, renderList);
89+
90+
renderList.endCount();
91+
92+
renderList.sort();
93+
94+
return renderList;
95+
},
96+
97+
getRenderList: function (camera)
98+
{
99+
return this.renderListMap.get(camera);
100+
},
101+
102+
updateLights: function ()
103+
{
104+
var lights = this.lights;
105+
106+
lights.startCount();
107+
108+
this.doUpdateLights(this);
109+
110+
lights.endCount();
111+
112+
return lights;
113+
},
114+
115+
doUpdateRenderList: function (object, camera, renderList)
116+
{
117+
if (!object.visible)
118+
{
119+
return;
120+
}
121+
122+
if (!!object.geometry && !!object.material)
123+
{
124+
renderList.add(object, camera);
125+
}
126+
127+
// if (object.type === OBJECT_TYPE.CANVAS2D)
128+
// {
129+
// return;
130+
// }
131+
132+
// Handle children by recursion
133+
var children = object.children;
134+
135+
for (var i = 0; i < children.length; i++)
136+
{
137+
this.doUpdateRenderList(children[i], camera, renderList);
138+
}
139+
},
140+
141+
doUpdateLights: function (object)
142+
{
143+
if (!object.visible)
144+
{
145+
return;
146+
}
147+
148+
if (object.type === 'light')
149+
{
150+
this.lights.add(object);
151+
}
152+
153+
// if (object.type === OBJECT_TYPE.CANVAS2D)
154+
// {
155+
// return;
156+
// }
157+
158+
var children = object.children;
159+
160+
for (var i = 0; i < children.length; i++)
161+
{
162+
this.doUpdateLights(children[i]);
163+
}
164+
},
165+
166+
/**
167+
* The destroy step for this Layer3D, which removes all models, destroys the camera and
168+
* nulls external references.
169+
*
170+
* @method Phaser.GameObjects.Layer3D#preDestroy
171+
* @private
172+
* @since 3.50.0
173+
*/
174+
preDestroy: function ()
175+
{
176+
// this.clearModels();
177+
178+
// this.camera.destroy();
179+
// this.light.destroy();
180+
181+
// this.camera = null;
182+
// this.light = null;
183+
}
184+
185+
});
186+
187+
module.exports = Layer3DScene;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* This is a stub function for Layer3D.Render. There is no Canvas renderer for Layer3D objects.
9+
*
10+
* @method Phaser.Layer3D.Layer3DScene#renderCanvas
11+
* @since 3.50.0
12+
* @private
13+
*
14+
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.
15+
* @param {Phaser.Layer3D.Layer3DScene} src - The Game Object being rendered in this call.
16+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
17+
*/
18+
var Layer3DSceneCanvasRenderer = function ()
19+
{
20+
};
21+
22+
module.exports = Layer3DSceneCanvasRenderer;

0 commit comments

Comments
 (0)