Fix game crash with lights when the device is lacking WebGL support (#1979)

This commit is contained in:
Harsimran Singh Virk
2020-10-03 04:16:37 +05:30
committed by GitHub
parent 4dd001951c
commit 238bf27671
2 changed files with 33 additions and 1 deletions

View File

@@ -55,6 +55,13 @@ gdjs.LightRuntimeObjectPixiRenderer = function (runtimeObject, runtimeScene) {
runtimeObject.getHitBoxes()[0].vertices[i]
);
}
// Objects will be added in lighting layer, this is just to maintain consistency.
if (this._light)
runtimeScene
.getLayer('')
.getRenderer()
.addRendererObject(this.getRendererObject(), runtimeObject.getZOrder());
};
gdjs.LightRuntimeObjectRenderer = gdjs.LightRuntimeObjectPixiRenderer; //Register the class to let the engine use it.
@@ -168,6 +175,12 @@ gdjs.LightRuntimeObjectPixiRenderer.prototype.ensureUpToDate = function () {
};
gdjs.LightRuntimeObjectPixiRenderer.prototype.updateMesh = function () {
if (!PIXI.utils.isWebGLSupported()) {
console.warn(
'This device does not support webgl, which is required for Lighting Extension.'
);
return;
}
this.updateTexture();
var fragmentShader =
this._texture === null
@@ -200,11 +213,15 @@ gdjs.LightRuntimeObjectPixiRenderer.prototype.updateMesh = function () {
};
gdjs.LightRuntimeObjectPixiRenderer.prototype.updateRadius = function () {
if (!this._light) return;
this._radius = this._object.getRadius();
this._light.shader.uniforms.radius = this._radius;
};
gdjs.LightRuntimeObjectPixiRenderer.prototype.updateColor = function () {
if (!this._light) return;
var objectColor = this._object._color;
this._color = [
objectColor[0] / 255,
@@ -215,6 +232,7 @@ gdjs.LightRuntimeObjectPixiRenderer.prototype.updateColor = function () {
};
gdjs.LightRuntimeObjectPixiRenderer.prototype.updateTexture = function () {
if (!this._light) return;
var texture = this._object.getTexture();
this._texture =
texture !== ''
@@ -223,6 +241,8 @@ gdjs.LightRuntimeObjectPixiRenderer.prototype.updateTexture = function () {
};
gdjs.LightRuntimeObjectPixiRenderer.prototype.updateDebugMode = function () {
if (!this._light) return;
this._debugMode = this._object.getDebugMode();
if (!this._debugLight && (this._isPreview || this._debugMode)) {
this._debugLight = new PIXI.Container();
@@ -293,9 +313,10 @@ gdjs.LightRuntimeObjectPixiRenderer.prototype._updateDebugGraphics = function ()
};
gdjs.LightRuntimeObjectPixiRenderer.prototype._updateBuffers = function () {
if (!this._light) return;
this._center[0] = this._object.x;
this._center[1] = this._object.y;
this._light.shader.uniforms.center = this._center;
var vertices = this._computeLightVertices();
// Fallback to simple quad when there are no obstacles around.
@@ -309,6 +330,7 @@ gdjs.LightRuntimeObjectPixiRenderer.prototype._updateBuffers = function () {
this._defaultVertexBuffer[6] = this._object.x - this._radius;
this._defaultVertexBuffer[7] = this._object.y - this._radius;
this._light.shader.uniforms.center = this._center;
this._light.geometry
.getBuffer('aVertexPosition')
.update(this._defaultVertexBuffer);
@@ -364,6 +386,7 @@ gdjs.LightRuntimeObjectPixiRenderer.prototype._updateBuffers = function () {
else this._indexBuffer[i + 2] = 1;
}
this._light.shader.uniforms.center = this._center;
if (!isSubArrayUsed) {
this._light.geometry
.getBuffer('aVertexPosition')

View File

@@ -53,6 +53,7 @@ const addLightObstacle = (runtimeScene, width, height) => {
};
describe('gdjs.LightRuntimeObject', function () {
PIXI.settings.FAIL_IF_MAJOR_PERFORMANCE_CAVEAT = false;
const runtimeGame = new gdjs.RuntimeGame({
variables: [],
resources: {
@@ -61,6 +62,13 @@ describe('gdjs.LightRuntimeObject', function () {
properties: { windowWidth: 800, windowHeight: 600 },
});
const runtimeScene = new gdjs.RuntimeScene(runtimeGame);
runtimeScene.loadFromScene({
layers: [{ name: '', visibility: true, effects: [] }],
variables: [],
behaviorsSharedData: [],
objects: [],
instances: [],
});
const lightObj = addLightObject(runtimeScene, 100);
lightObj.setPosition(200, 200);
@@ -85,6 +93,7 @@ describe('gdjs.LightRuntimeObject', function () {
});
describe('Light with obstacles around it', function () {
PIXI.settings.FAIL_IF_MAJOR_PERFORMANCE_CAVEAT = false;
const runtimeGame = new gdjs.RuntimeGame({
variables: [],
resources: {