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

135 lines
3.0 KiB
JavaScript

/**
* Renders a scene with a camera
* @param graphics GameLib.D3.Graphics
* @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;
}
GameLib.D3.API.Pass.call(
this,
apiPass.id,
apiPass.name,
apiPass.passType,
apiPass.camera,
apiPass.scene,
apiPass.renderToScreen,
apiPass.parentEntity
);
this.buildIdToObject();
this.instance = this.createInstance();
};
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
* @param update
* @returns {*}
*/
GameLib.D3.Pass.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
}
if (this.passType == GameLib.D3.Pass.PASS_TYPE_RENDER) {
if (this.scene && this.camera) {
if (!THREE.RenderPass) {
console.warn('No THREE.RenderPass');
throw new Error('No THREE.RenderPass');
}
instance = new THREE.RenderPass(
this.scene.instance,
this.camera.instance
);
}
} else if (this.passType == GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) {
if (!THREE.CopyShader) {
console.warn('No THREE.CopyShader');
throw new Error('No THREE.CopyShader');
}
instance = THREE.CopyShader;
} else {
console.warn('Render pass not supported yet: ' + this.passType);
throw new Error('Render pass not supported yet: ' + this.passType);
}
if (instance) {
instance.renderToScreen = this.renderToScreen;
}
return instance;
};
/**
* Update Pass instance
*/
GameLib.D3.Pass.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
* GameLib.D3.Pass to GameLib.D3.API.Pass
* @returns {GameLib.D3.API.Pass}
*/
GameLib.D3.Pass.prototype.toApiComponent = 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.FromObjectComponent = function(graphics, objectComponent) {
var apiPass = GameLib.D3.API.Pass.FromObjectComponent(objectComponent);
return new GameLib.D3.Pass(
graphics,
apiPass
);
};