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

152 lines
3.3 KiB
JavaScript

/**
* Renders a scene with a camera
* @param graphics GameLib.D3.Graphics
* @param apiRenderer GameLib.D3.API.Renderer
* @constructor
*/
GameLib.D3.Renderer = function (
graphics,
apiRenderer
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiRenderer)) {
apiRenderer = {};
}
if (apiRenderer instanceof GameLib.D3.Renderer) {
return apiRenderer;
}
GameLib.D3.API.Renderer.call(
this,
apiRenderer.id,
apiRenderer.name,
apiRenderer.autoClear,
apiRenderer.localClipping,
apiRenderer.width,
apiRenderer.height,
apiRenderer.preserveDrawingBuffer,
apiRenderer.domElement,
apiRenderer.clearColor,
apiRenderer.parentEntity
);
this.clearColor = new GameLib.Color(
this.graphics,
this.clearColor,
this
);
if (this.domElement instanceof GameLib.API.DomElement) {
this.domElement = new GameLib.DomElement(
this.domElement
);
}
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_RENDERER,
{
'domElement' : GameLib.DomElement
}
);
};
GameLib.D3.Renderer.prototype = Object.create(GameLib.D3.API.Renderer.prototype);
GameLib.D3.Renderer.prototype.constructor = GameLib.D3.Renderer;
/**
* Create Renderer Instance
* @param update
* @returns {*}
*/
GameLib.D3.Renderer.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
} else {
instance = new THREE.WebGLRenderer({
canvas : this.domElement.instance
});
}
instance.localClippingEnabled = this.localClipping;
instance.setSize(
this.width,
this.height
);
instance.setClearColor(
new THREE.Color(
this.clearColor.r,
this.clearColor.g,
this.clearColor.b
),
1 - this.clearColor.a
);
instance.domElement.width = this.width;
instance.domElement.height = this.height;
instance.autoClear = this.autoClear;
instance.preserveDrawingBuffer = this.preserveDrawingBuffer;
return instance;
};
GameLib.D3.Renderer.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
GameLib.D3.Renderer.prototype.toApiObject = function() {
var apiRenderer = new GameLib.D3.API.Renderer(
this.id,
this.name,
this.autoClear,
this.localClipping,
this.width,
this.height,
this.preserveDrawingBuffer,
this.domElement.toApiObject(),
GameLib.Utils.IdOrNull(this.parentEntity)
);
return apiRenderer;
};
/**
*
* @param graphics
* @param objectComponent
* @returns {GameLib.D3.Renderer}
* @constructor
*/
GameLib.D3.Renderer.FromObject = function(graphics, objectComponent) {
var apiRenderer = GameLib.D3.API.Renderer.FromObject(objectComponent);
return new GameLib.D3.Renderer(
graphics,
apiRenderer
);
};
GameLib.D3.Renderer.prototype.render = function() {
this.instance.render(this.scene.instance, this.camera.instance);
};
GameLib.D3.Renderer.prototype.setSize = function(width, height) {
this.width = width;
this.height = height;
this.updateInstance();
};