Skip to content

Commit

Permalink
Added more tests and fixed IBL lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Dec 25, 2019
1 parent 3e92299 commit d7eba1d
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Source/Renderer/UniformState.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,8 +1160,8 @@ import SunLight from '../Scene/SunLight.js';

var light = defaultValue(frameState.light, defaultLight);
if (light instanceof SunLight) {
this._lightDirectionWC = Cartesian3.clone(this._sunDirectionWC);
this._lightDirectionEC = Cartesian3.clone(this._sunDirectionEC);
this._lightDirectionWC = Cartesian3.clone(this._sunDirectionWC, this._lightDirectionWC);
this._lightDirectionEC = Cartesian3.clone(this._sunDirectionEC, this._lightDirectionEC);
} else {
this._lightDirectionWC = Cartesian3.normalize(Cartesian3.negate(light.direction, this._lightDirectionWC), this._lightDirectionWC);
this._lightDirectionEC = Matrix3.multiplyByVector(this.viewRotation3D, this._lightDirectionWC, this._lightDirectionEC);
Expand Down
2 changes: 1 addition & 1 deletion Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -3270,7 +3270,7 @@ import View from './View.js';

var shadowMap = scene.shadowMap;
if (defined(shadowMap) && shadowMap.enabled) {
if (scene.light instanceof SunLight) {
if (!defined(scene.light) || scene.light instanceof SunLight) {
// Negate the sun direction so that it is from the Sun, not to the Sun
Cartesian3.negate(us.sunDirectionWC, scene._shadowMapCamera.direction);
} else {
Expand Down
6 changes: 3 additions & 3 deletions Source/Scene/SunLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import defined from '../Core/defined.js';
* A directional light source that originates from the Sun.
*
* @param {Object} [options] Object with the following properties:
* @param {Color} [options.color=new Color(1.0, 1.0, 1.0)] The light's color.
* @param {Color} [options.color=Color.WHITE] The light's color.
* @param {Number} [options.intensity=2.0] The light's intensity.
*
* @alias SunLight
Expand All @@ -17,9 +17,9 @@ import defined from '../Core/defined.js';
/**
* The color of the light.
* @type {Color}
* @default new Color(1.0, 1.0, 1.0)
* @default Color.WHITE
*/
this.color = defined(options.color) ? Color.clone(options.color) : new Color(1.0, 1.0, 1.0);
this.color = defined(options.color) ? Color.clone(options.color) : Color.WHITE;

/**
* The intensity of the light.
Expand Down
9 changes: 6 additions & 3 deletions Source/Scene/processPbrMaterials.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,9 @@ import ModelUtility from './ModelUtility.js';

// Generate fragment shader's lighting block
fragmentShader += '#ifndef USE_CUSTOM_LIGHT_COLOR \n';
fragmentShader += ' vec3 lightColor = czm_lightColorHdr;\n';
fragmentShader += ' vec3 lightColorHdr = czm_lightColorHdr;\n';
fragmentShader += '#else \n';
fragmentShader += ' vec3 lightColor = gltf_lightColor;\n';
fragmentShader += ' vec3 lightColorHdr = gltf_lightColor;\n';
fragmentShader += '#endif \n';
fragmentShader += ' vec3 l = normalize(czm_lightDirectionEC);\n';
fragmentShader += ' vec3 h = normalize(v + l);\n';
Expand Down Expand Up @@ -722,7 +722,7 @@ import ModelUtility from './ModelUtility.js';

fragmentShader += ' vec3 diffuseContribution = (1.0 - F) * lambertianDiffuse(diffuseColor);\n';
fragmentShader += ' vec3 specularContribution = F * G * D / (4.0 * NdotL * NdotV);\n';
fragmentShader += ' vec3 color = NdotL * lightColor * (diffuseContribution + specularContribution);\n';
fragmentShader += ' vec3 color = NdotL * lightColorHdr * (diffuseContribution + specularContribution);\n';

// Use the procedural IBL if there are no environment maps
fragmentShader += '#if defined(USE_IBL_LIGHTING) && !defined(DIFFUSE_IBL) && !defined(SPECULAR_IBL) \n';
Expand Down Expand Up @@ -781,6 +781,9 @@ import ModelUtility from './ModelUtility.js';

fragmentShader += ' vec2 brdfLut = texture2D(czm_brdfLut, vec2(NdotV, roughness)).rg;\n';
fragmentShader += ' vec3 IBLColor = (diffuseIrradiance * diffuseColor * gltf_iblFactor.x) + (specularIrradiance * SRGBtoLINEAR3(specularColor * brdfLut.x + brdfLut.y) * gltf_iblFactor.y);\n';

fragmentShader += ' float maximumComponent = max(max(lightColorHdr.x, lightColorHdr.y), lightColorHdr.z);\n';
fragmentShader += ' vec3 lightColor = lightColorHdr / max(maximumComponent, 1.0);\n';
fragmentShader += ' IBLColor *= lightColor;\n';

fragmentShader += '#ifdef USE_SUN_LUMINANCE \n';
Expand Down
49 changes: 49 additions & 0 deletions Specs/Scene/DirectionalLightSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Cartesian3, Color, DirectionalLight } from '../../Source/Cesium.js';

describe('Scene/DirectionalLight', function() {

it('constructs with default options', function() {
var light = new DirectionalLight({
direction : Cartesian3.UNIT_X
});

expect(light.direction).toEqual(Cartesian3.UNIT_X);
expect(light.direction).not.toBe(Cartesian3.UNIT_X);
expect(light.color).toEqual(Color.WHITE);
expect(light.intensity).toBe(1.0);
});

it('constructs with all options', function() {
var light = new DirectionalLight({
direction : Cartesian3.UNIT_X,
color : Color.RED,
intensity : 2.0
});
expect(light.direction).toEqual(Cartesian3.UNIT_X);
expect(light.color).toEqual(Color.RED);
expect(light.color).not.toBe(Color.RED);
expect(light.intensity).toBe(2.0);
});

it('throws if options is undefined', function() {
expect(function() {
return new DirectionalLight();
}).toThrowDeveloperError();
});

it('throws if options.direction is undefined', function() {
expect(function() {
return new DirectionalLight({
color : Color.RED
});
}).toThrowDeveloperError();
});

it('throws if options.direction is zero-length', function() {
expect(function() {
return new DirectionalLight({
direction : Cartesian3.ZERO
});
}).toThrowDeveloperError();
});
});
19 changes: 19 additions & 0 deletions Specs/Scene/GlobeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ describe('Scene/Globe', function() {
});
});

it('renders with dynamicAtmosphereLightingFromSun', function() {
globe.enableLighting = true;
globe.dynamicAtmosphereLighting = true;
globe.dynamicAtmosphereLightingFromSun = true;

var layerCollection = globe.imageryLayers;
layerCollection.removeAll();
layerCollection.addImageryProvider(new SingleTileImageryProvider({url : 'Data/Images/Red16x16.png'}));

scene.camera.setView({ destination : new Rectangle(0.0001, 0.0001, 0.0025, 0.0025) });

return updateUntilDone(globe).then(function() {
scene.globe.show = false;
expect(scene).toRender([0, 0, 0, 255]);
scene.globe.show = true;
expect(scene).notToRender([0, 0, 0, 255]);
});
});

it('renders with showWaterEffect set to false', function() {
globe.showWaterEffect = false;

Expand Down
14 changes: 14 additions & 0 deletions Specs/Scene/LightSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Light } from '../../Source/Cesium.js';

describe('Scene/Light', function() {

it('throws', function() {
var light = new Light();
expect(function() {
return light.color;
}).toThrowDeveloperError();
expect(function() {
return light.intensity;
}).toThrowDeveloperError();
});
});
47 changes: 46 additions & 1 deletion Specs/Scene/SceneSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BoundingSphere } from '../../Source/Cesium.js';
import { BoundingSphere, SunLight, DirectionalLight } from '../../Source/Cesium.js';
import { Cartesian2 } from '../../Source/Cesium.js';
import { Cartesian3 } from '../../Source/Cesium.js';
import { CesiumTerrainProvider } from '../../Source/Cesium.js';
Expand Down Expand Up @@ -1717,4 +1717,49 @@ describe('Scene/Scene', function() {
scene.destroyForSpecs();
});

it('sets light', function() {
var scene = createScene();
var uniformState = scene.context.uniformState;
var lightDirectionWC = uniformState._lightDirectionWC;
var sunDirectionWC = uniformState._sunDirectionWC;
var lightColor = uniformState._lightColor;
var lightColorHdr = uniformState._lightColorHdr;

// Default light is a sun light
scene.renderForSpecs();
expect(lightDirectionWC).toEqual(sunDirectionWC);
expect(lightColor).toEqual(new Cartesian3(1.0, 1.0, 1.0));
expect(lightColorHdr).toEqual(new Cartesian3(2.0, 2.0, 2.0));

// Test directional light
scene.light = new DirectionalLight({
direction : new Cartesian3(1.0, 0.0, 0.0),
color : Color.RED,
intensity : 2.0
});
scene.renderForSpecs();
expect(lightDirectionWC).toEqual(new Cartesian3(-1.0, 0.0, 0.0)); // Negated because the uniform is the direction to the light, not from the light
expect(lightColor).toEqual(new Cartesian3(1.0, 0.0, 0.0));
expect(lightColorHdr).toEqual(new Cartesian3(2.0, 0.0, 0.0));

// Test sun light
scene.light = new SunLight({
color : Color.BLUE,
intensity : 0.5
});
scene.renderForSpecs();
expect(lightDirectionWC).toEqual(sunDirectionWC);
expect(lightColor).toEqual(new Cartesian3(0.0, 0.0, 0.5));
expect(lightColorHdr).toEqual(new Cartesian3(0.0, 0.0, 0.5));

// Test light set to undefined
scene.light = undefined;
scene.renderForSpecs();
expect(lightDirectionWC).toEqual(sunDirectionWC);
expect(lightColor).toEqual(new Cartesian3(1.0, 1.0, 1.0));
expect(lightColorHdr).toEqual(new Cartesian3(2.0, 2.0, 2.0));

scene.destroyForSpecs();
});

}, 'WebGL');
21 changes: 21 additions & 0 deletions Specs/Scene/SunLightSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Color, SunLight } from '../../Source/Cesium.js';

describe('Scene/SunLight', function() {

it('constructs with default options', function() {
var light = new SunLight();

expect(light.color).toEqual(Color.WHITE);
expect(light.intensity).toBe(2.0);
});

it('constructs with all options', function() {
var light = new SunLight({
color : Color.RED,
intensity : 2.0
});
expect(light.color).toEqual(Color.RED);
expect(light.color).not.toBe(Color.RED);
expect(light.intensity).toBe(2.0);
});
});

0 comments on commit d7eba1d

Please sign in to comment.