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

147 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-01-10 17:04:30 +01:00
/**
* Renders a scene with a camera
* @param graphics GameLib.GraphicsRuntime
2017-01-10 17:04:30 +01:00
* @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 = {};
}
if (apiPass instanceof GameLib.D3.Pass) {
return apiPass;
}
2017-01-10 17:04:30 +01:00
GameLib.D3.API.Pass.call(
this,
apiPass.id,
apiPass.name,
apiPass.passType,
apiPass.camera,
apiPass.scene,
apiPass.renderToScreen,
apiPass.parentEntity
);
2017-06-16 15:49:53 +02:00
GameLib.Component.call(
this,
{
'camera' : GameLib.D3.Camera,
'scene' : GameLib.D3.Scene
}
);
2017-01-10 17:04:30 +01:00
};
GameLib.D3.Pass.prototype = Object.create(GameLib.D3.API.Pass.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 {*}
*/
2017-10-23 14:52:35 +02:00
GameLib.D3.Pass.prototype.createInstance = function() {
2017-01-10 17:04:30 +01:00
2017-06-16 18:45:25 +02:00
if (this.passType === GameLib.D3.Pass.PASS_TYPE_RENDER) {
2017-01-10 17:04:30 +01:00
2017-10-23 14:52:35 +02:00
if (GameLib.Utils.UndefinedOrNull(this.scene)) {
throw new Error('no scene object');
}
if (GameLib.Utils.UndefinedOrNull(this.camera)) {
throw new Error('no camera object');
}
2017-01-10 17:04:30 +01:00
2017-10-23 14:52:35 +02:00
if (GameLib.Utils.UndefinedOrNull(THREE.RenderPass)) {
throw new Error('no render pass library')
2017-01-19 17:50:11 +01:00
}
2017-01-10 17:04:30 +01:00
2017-10-23 14:52:35 +02:00
this.instance = new THREE.RenderPass(
this.scene.instance,
this.camera.instance
);
2017-06-28 17:09:06 +02:00
} else if (this.passType === GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) {
2017-01-19 17:50:11 +01:00
2017-10-23 14:52:35 +02:00
if (GameLib.Utils.UndefinedOrNull(THREE.CopyShader)) {
throw new Error('no copyshader library')
2017-01-10 17:04:30 +01:00
}
2017-01-19 17:50:11 +01:00
2017-10-23 14:52:35 +02:00
this.instance = THREE.CopyShader;
2017-01-19 17:50:11 +01:00
} else {
console.warn('Render pass not supported yet: ' + this.passType);
throw new Error('Render pass not supported yet: ' + this.passType);
2017-01-10 17:04:30 +01:00
}
2017-10-23 14:52:35 +02:00
this.instance.renderToScreen = this.renderToScreen;
GameLib.Component.prototype.createInstance.call(this);
2017-01-10 17:04:30 +01:00
};
/**
* Update Pass instance
*/
GameLib.D3.Pass.prototype.updateInstance = function() {
2017-10-23 14:52:35 +02:00
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;
2017-01-10 17:04:30 +01:00
};
/**
* GameLib.D3.Pass to GameLib.D3.API.Pass
* @returns {GameLib.D3.API.Pass}
*/
2017-05-16 14:51:57 +02:00
GameLib.D3.Pass.prototype.toApiObject = function() {
2017-01-10 17:04:30 +01:00
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
*/
2017-06-14 14:21:57 +02:00
GameLib.D3.Pass.FromObject = function(graphics, objectComponent) {
2017-01-10 17:04:30 +01:00
2017-06-14 14:21:57 +02:00
var apiPass = GameLib.D3.API.Pass.FromObject(objectComponent);
2017-01-10 17:04:30 +01:00
return new GameLib.D3.Pass(
graphics,
apiPass
);
};