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

152 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-12-19 17:44:15 +01:00
/**
* Renders a scene with a camera
* @param graphics GameLib.D3.Graphics
* @param apiRenderer GameLib.D3.API.Renderer
* @constructor
*/
2017-01-06 16:53:53 +01:00
GameLib.D3.Renderer = function (
2016-12-19 17:44:15 +01:00
graphics,
apiRenderer
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiRenderer)) {
apiRenderer = {};
}
if (apiRenderer instanceof GameLib.D3.Renderer) {
return apiRenderer;
}
2016-12-19 17:44:15 +01:00
GameLib.D3.API.Renderer.call(
this,
apiRenderer.id,
apiRenderer.name,
apiRenderer.autoClear,
apiRenderer.localClipping,
apiRenderer.width,
2017-01-02 17:05:40 +01:00
apiRenderer.height,
2017-01-31 15:23:38 +01:00
apiRenderer.preserveDrawingBuffer,
apiRenderer.domElement,
2017-06-07 11:32:52 +02:00
apiRenderer.clearColor,
2017-01-31 15:23:38 +01:00
apiRenderer.parentEntity
2016-12-19 17:44:15 +01:00
);
2017-06-07 11:32:52 +02:00
this.clearColor = new GameLib.Color(
this.graphics,
this.clearColor,
this
);
if (this.domElement instanceof GameLib.API.DomElement) {
this.domElement = new GameLib.DomElement(
this.domElement
);
}
2017-06-16 15:49:53 +02:00
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_RENDERER,
{
'domElement' : GameLib.DomElement
}
);
2016-12-19 17:44:15 +01:00
};
GameLib.D3.Renderer.prototype = Object.create(GameLib.D3.API.Renderer.prototype);
GameLib.D3.Renderer.prototype.constructor = GameLib.D3.Renderer;
2017-01-10 17:04:30 +01:00
/**
* Create Renderer Instance
* @param update
2017-01-10 17:04:30 +01:00
* @returns {*}
*/
2016-12-19 17:44:15 +01:00
GameLib.D3.Renderer.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
} else {
2017-05-11 17:52:33 +02:00
instance = new THREE.WebGLRenderer({
canvas : this.domElement.instance
});
2016-12-19 17:44:15 +01:00
}
instance.localClippingEnabled = this.localClipping;
2017-01-11 16:09:06 +01:00
instance.setSize(
this.width,
this.height
);
2017-06-07 11:32:52 +02:00
instance.setClearColor(
new THREE.Color(
this.clearColor.r,
this.clearColor.g,
this.clearColor.b
),
1 - this.clearColor.a
);
2017-05-09 15:44:29 +02:00
instance.domElement.width = this.width;
instance.domElement.height = this.height;
2016-12-19 17:44:15 +01:00
instance.autoClear = this.autoClear;
2017-01-09 15:20:48 +01:00
instance.preserveDrawingBuffer = this.preserveDrawingBuffer;
2016-12-19 17:44:15 +01:00
return instance;
};
GameLib.D3.Renderer.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
2017-05-16 14:51:57 +02:00
GameLib.D3.Renderer.prototype.toApiObject = function() {
2016-12-19 17:44:15 +01:00
var apiRenderer = new GameLib.D3.API.Renderer(
this.id,
this.name,
this.autoClear,
this.localClipping,
this.width,
2017-01-02 17:05:40 +01:00
this.height,
2017-01-31 15:23:38 +01:00
this.preserveDrawingBuffer,
2017-05-16 14:51:57 +02:00
this.domElement.toApiObject(),
2017-01-31 15:23:38 +01:00
GameLib.Utils.IdOrNull(this.parentEntity)
2016-12-19 17:44:15 +01:00
);
return apiRenderer;
};
2017-01-09 15:20:48 +01:00
/**
*
* @param graphics
* @param objectComponent
* @returns {GameLib.D3.Renderer}
* @constructor
*/
2017-06-14 14:21:57 +02:00
GameLib.D3.Renderer.FromObject = function(graphics, objectComponent) {
2016-12-19 17:44:15 +01:00
2017-06-14 14:21:57 +02:00
var apiRenderer = GameLib.D3.API.Renderer.FromObject(objectComponent);
2016-12-19 17:44:15 +01:00
return new GameLib.D3.Renderer(
graphics,
apiRenderer
);
};
2017-01-31 15:23:38 +01:00
GameLib.D3.Renderer.prototype.render = function() {
this.instance.render(this.scene.instance, this.camera.instance);
};
2017-05-09 15:44:29 +02:00
GameLib.D3.Renderer.prototype.setSize = function(width, height) {
this.width = width;
this.height = height;
this.updateInstance();
};