r3-legacy/src/game-lib-d3-pass.js

143 lines
3.3 KiB
JavaScript

/**
* Renders a scene with a camera
* @param graphics GameLib.GraphicsRuntime
* @param apiPass GameLib.D3.API.Pass
* @constructor
*/
GameLib.D3.Pass = function (
graphics,
apiPass
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiPass)) {
apiPass = {};
}
GameLib.D3.API.Pass.call(
this,
apiPass.id,
apiPass.name,
apiPass.passType,
apiPass.camera,
apiPass.scene,
apiPass.renderToScreen,
apiPass.parentEntity
);
GameLib.Component.call(
this,
{
'camera' : GameLib.D3.Camera,
'scene' : GameLib.D3.Scene
}
);
};
GameLib.D3.Pass.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.Pass.prototype.constructor = GameLib.D3.Pass;
GameLib.D3.Pass.PASS_TYPE_RENDER = 0x1;
GameLib.D3.Pass.PASS_TYPE_COPY_SHADER = 0x2;
/**
* Create Pass instance
* @returns {*}
*/
GameLib.D3.Pass.prototype.createInstance = function() {
if (this.passType === GameLib.D3.Pass.PASS_TYPE_RENDER) {
if (GameLib.Utils.UndefinedOrNull(this.scene)) {
throw new Error('no scene object');
}
if (GameLib.Utils.UndefinedOrNull(this.camera)) {
throw new Error('no camera object');
}
if (GameLib.Utils.UndefinedOrNull(THREE.RenderPass)) {
throw new Error('no render pass library')
}
this.instance = new THREE.RenderPass(
this.scene.instance,
this.camera.instance
);
} else if (this.passType === GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) {
if (GameLib.Utils.UndefinedOrNull(THREE.CopyShader)) {
throw new Error('no copyshader library')
}
this.instance = THREE.CopyShader;
} else {
console.warn('Render pass not supported yet: ' + this.passType);
throw new Error('Render pass not supported yet: ' + this.passType);
}
this.instance.renderToScreen = this.renderToScreen;
GameLib.Component.prototype.createInstance.call(this);
};
/**
* Update Pass instance
*/
GameLib.D3.Pass.prototype.updateInstance = function() {
if (
this.passType === GameLib.D3.Pass.PASS_TYPE_RENDER && !(this.instance instanceof THREE.RenderPass)){
this.createInstance();
}
if (
this.passType === GameLib.D3.Pass.PASS_TYPE_COPY_SHADER && !(this.instance instanceof THREE.CopyShader)){
this.createInstance();
}
this.instance.renderToScreen = this.renderToScreen;
};
/**
* GameLib.D3.Pass to GameLib.D3.API.Pass
* @returns {GameLib.D3.API.Pass}
*/
GameLib.D3.Pass.prototype.toApiObject = function() {
var apiPass = new GameLib.D3.API.Pass(
this.id,
this.name,
this.passType,
GameLib.Utils.IdOrNull(this.camera),
GameLib.Utils.IdOrNull(this.scene),
this.renderToScreen,
GameLib.Utils.IdOrNull(this.parentEntity)
);
return apiPass;
};
/**
* GameLib.D3.Pass from Object
* @param graphics
* @param objectComponent
* @returns {GameLib.D3.Pass}
* @constructor
*/
GameLib.D3.Pass.FromObject = function(graphics, objectComponent) {
var apiPass = GameLib.D3.API.Pass.FromObject(objectComponent);
return new GameLib.D3.Pass(
graphics,
apiPass
);
};