2017-01-11 16:09:06 +01:00
|
|
|
/**
|
|
|
|
* Viewport Runtime
|
|
|
|
* @param graphics GameLib.D3.Graphics
|
|
|
|
* @param apiViewport GameLib.D3.API.Viewport
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Viewport = function (
|
|
|
|
graphics,
|
|
|
|
apiViewport
|
|
|
|
) {
|
|
|
|
|
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
|
|
|
|
GameLib.D3.API.Viewport.call(
|
|
|
|
this,
|
|
|
|
apiViewport.id,
|
|
|
|
apiViewport.name,
|
|
|
|
apiViewport.width,
|
|
|
|
apiViewport.height,
|
|
|
|
apiViewport.x,
|
|
|
|
apiViewport.y,
|
|
|
|
apiViewport.composer,
|
|
|
|
apiViewport.scene,
|
|
|
|
apiViewport.camera,
|
|
|
|
apiViewport.parentEntity
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.composer instanceof GameLib.D3.API.Composer) {
|
|
|
|
this.composer = new GameLib.D3.Composer(
|
|
|
|
this.graphics,
|
|
|
|
this.composer
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.renderer instanceof GameLib.D3.API.Renderer) {
|
|
|
|
this.renderer = new GameLib.D3.Renderer(
|
|
|
|
this.graphics,
|
|
|
|
this.renderer
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.scene instanceof GameLib.D3.API.Scene) {
|
|
|
|
this.scene = new GameLib.D3.Scene(
|
|
|
|
this.graphics,
|
|
|
|
this.scene
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.camera instanceof GameLib.D3.API.Camera) {
|
|
|
|
this.camera = new GameLib.D3.Camera(
|
|
|
|
this.graphics,
|
|
|
|
this.camera
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.instance = this.createInstance();
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Viewport.prototype = Object.create(GameLib.D3.API.Viewport.prototype);
|
|
|
|
GameLib.D3.Viewport.prototype.constructor = GameLib.D3.Viewport;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param update
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Viewport.prototype.createInstance = function(update) {
|
|
|
|
|
|
|
|
var instance = null;
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
instance = this.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.renderer) {
|
2017-01-12 17:40:17 +01:00
|
|
|
this.renderer.width = this.width - this.x;
|
|
|
|
this.renderer.height = this.height - this.y;
|
2017-01-11 16:09:06 +01:00
|
|
|
this.renderer.updateInstance();
|
|
|
|
|
|
|
|
this.renderer.instance.setViewport(
|
|
|
|
this.x,
|
|
|
|
this.y,
|
|
|
|
this.width,
|
|
|
|
this.height
|
|
|
|
);
|
|
|
|
} else if (this.composer) {
|
2017-01-12 17:40:17 +01:00
|
|
|
this.composer.renderer.width = this.width - this.x;
|
|
|
|
this.composer.renderer.height = this.height - this.y;
|
2017-01-11 16:09:06 +01:00
|
|
|
this.composer.renderer.updateInstance();
|
|
|
|
|
|
|
|
this.composer.renderer.instance.setViewport(
|
|
|
|
this.x,
|
|
|
|
this.y,
|
|
|
|
this.width,
|
|
|
|
this.height
|
2017-01-12 17:40:17 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.composer.passes.map(
|
|
|
|
function(pass) {
|
|
|
|
if (pass.camera instanceof GameLib.D3.Camera) {
|
|
|
|
pass.camera.aspect = (this.width - this.x) / (this.height / this.y);
|
|
|
|
pass.camera.updateInstance();
|
|
|
|
}
|
|
|
|
}.bind(this)
|
2017-01-11 16:09:06 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:40:17 +01:00
|
|
|
if (this.camera) {
|
|
|
|
this.camera.aspect = (this.width - this.x) / (this.height / this.y);
|
|
|
|
this.camera.updateInstance();
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:09:06 +01:00
|
|
|
return instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
GameLib.D3.Viewport.prototype.updateInstance = function() {
|
|
|
|
this.instance = this.createInstance(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GameLib.D3.Viewport to GameLib.D3.API.Viewport
|
|
|
|
* @returns {GameLib.D3.API.Viewport}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Viewport.prototype.toApiComponent = function() {
|
|
|
|
|
|
|
|
var apiViewport = new GameLib.D3.API.Viewport(
|
|
|
|
this.id,
|
|
|
|
this.name,
|
|
|
|
this.width,
|
|
|
|
this.height,
|
|
|
|
this.x,
|
|
|
|
this.y,
|
|
|
|
GameLib.Utils.IdOrNull(this.composer),
|
|
|
|
GameLib.Utils.IdOrNull(this.scene),
|
|
|
|
GameLib.Utils.IdOrNull(this.camera),
|
|
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
|
|
|
);
|
|
|
|
|
|
|
|
return apiViewport;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GameLib.D3.Viewport from Object Viewport
|
|
|
|
* @param graphics
|
|
|
|
* @param objectComponent
|
|
|
|
* @returns {GameLib.D3.Viewport}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Viewport.FromObjectComponent = function(graphics, objectComponent) {
|
|
|
|
|
|
|
|
var apiViewport = GameLib.D3.API.Viewport.FromObjectComponent(objectComponent);
|
|
|
|
|
|
|
|
return new GameLib.D3.Viewport(
|
|
|
|
graphics,
|
|
|
|
apiViewport
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component update override
|
|
|
|
*/
|
|
|
|
GameLib.D3.Viewport.prototype.update = function() {
|
|
|
|
|
|
|
|
if (this.renderer && this.scene && this.camera) {
|
|
|
|
this.renderer.instance.render(
|
|
|
|
this.scene.instance,
|
|
|
|
this.camera.instance
|
|
|
|
)
|
|
|
|
} else if (this.composer) {
|
|
|
|
this.composer.instance.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|