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

150 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.D3.Graphics
* @param apiComposer GameLib.D3.API.Composer
* @constructor
*/
GameLib.D3.Composer = function (
graphics,
apiComposer
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2017-01-12 17:40:17 +01:00
if (GameLib.Utils.UndefinedOrNull(apiComposer)) {
apiComposer = {};
2017-01-12 17:40:17 +01:00
}
if (apiComposer instanceof GameLib.D3.Composer) {
return apiComposer;
}
2017-01-10 17:04:30 +01:00
GameLib.D3.API.Composer.call(
this,
apiComposer.id,
apiComposer.name,
apiComposer.renderer,
apiComposer.renderTarget,
apiComposer.passes,
apiComposer.parentEntity
);
2017-02-21 18:55:18 +01:00
if (this.renderer instanceof GameLib.D3.API.Renderer) {
this.renderer = new GameLib.D3.Renderer(
this.graphics,
this.renderer
)
}
if (this.renderTarget instanceof GameLib.D3.API.RenderTarget) {
this.renderTarget = new GameLib.D3.RenderTarget(
this.graphics,
this.renderTarget
)
}
this.passes = this.passes.map(
function (apiPass) {
if (apiPass instanceof GameLib.D3.API.Pass) {
return GameLib.D3.Pass(
this.graphics,
apiPass
)
} else {
console.warn('apiPass not of type API.Pass');
throw new Error('apiPass not of type API.Pass');
}
}.bind(this)
);
2017-01-19 17:50:11 +01:00
this.buildIdToObject();
2017-01-10 17:04:30 +01:00
this.instance = this.createInstance();
};
GameLib.D3.Composer.prototype = Object.create(GameLib.D3.API.Composer.prototype);
GameLib.D3.Composer.prototype.constructor = GameLib.D3.Composer;
/**
* Creates a Composer instance
* @param update
* @returns {*}
*/
GameLib.D3.Composer.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
2017-01-19 17:50:11 +01:00
}
if (this.renderer &&
this.renderTarget) {
2017-01-10 17:04:30 +01:00
if (!THREE.EffectComposer) {
console.warn('No THREE.EffectComposer');
throw new Error('No THREE.EffectComposer');
}
instance = new THREE.EffectComposer(
this.renderer.instance,
this.renderTarget.instance
);
this.passes.map(
function(pass) {
this.instance.addPass(pass.instance);
}.bind(this)
);
}
return instance;
};
/**
* Updates Composer instance
*/
GameLib.D3.Composer.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
* GameLib.D3.Composer to GameLib.D3.API.Composer
* @returns {GameLib.D3.API.Composer}
*/
2017-05-16 14:51:57 +02:00
GameLib.D3.Composer.prototype.toApiObject = function() {
2017-01-10 17:04:30 +01:00
var apiComposer = new GameLib.D3.API.Composer(
this.id,
this.name,
GameLib.Utils.IdOrNull(this.renderer),
GameLib.Utils.IdOrNull(this.renderTarget),
this.passes.map(
function(pass) {
return GameLib.Utils.IdOrNull(pass);
}
),
GameLib.Utils.IdOrNull(this.parentEntity)
);
return apiComposer;
};
/**
*
* @param graphics
* @param objectComponent
* @returns {GameLib.D3.Composer}
* @constructor
*/
2017-06-14 14:21:57 +02:00
GameLib.D3.Composer.FromObject = function(graphics, objectComponent) {
2017-01-10 17:04:30 +01:00
2017-06-14 14:21:57 +02:00
var apiComposer = GameLib.D3.API.Composer.FromObject(objectComponent);
2017-01-10 17:04:30 +01:00
return new GameLib.D3.Composer(
graphics,
apiComposer
);
};