Skip to content

Commit

Permalink
Merge pull request #7172 from AnalyticalGraphicsInc/ibl
Browse files Browse the repository at this point in the history
Image-based lighting
  • Loading branch information
lilleyse authored Dec 19, 2018
2 parents ec5b99d + e37e417 commit 700bbb8
Show file tree
Hide file tree
Showing 33 changed files with 1,940 additions and 102 deletions.
Binary file not shown.
Binary file added Apps/SampleData/models/Pawns/Pawns.glb
Binary file not shown.
132 changes: 132 additions & 0 deletions Apps/Sandcastle/gallery/Image-Based Lighting.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="description" content="Use image-based lighting to light a model.">
<meta name="cesium-sandcastle-labels" content="Showcases">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
<script type="text/javascript">
require.config({
baseUrl : '../../../Source',
waitSeconds : 60
});
</script>
</head>
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
@import url(../templates/bucket.css);
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar">
<table><tbody>
<tr>
<td>Luminance at Zenith</td>
<td>
<input type="range" min="0.0" max="2.0" step="0.01" data-bind="value: luminanceAtZenith, valueUpdate: 'input'">
<input type="text" size="4" data-bind="value: luminanceAtZenith">
</td>
</tr>
</tbody></table>
</div>
<script id="cesium_sandcastle_script">
function startup(Cesium) {
'use strict';
//Sandcastle_Begin
var viewer = new Cesium.Viewer('cesiumContainer');

var environmentMapURL = '../../SampleData/EnvironmentMap/kiara_6_afternoon_2k_ibl.ktx';
var modelURL = '../../SampleData/models/Pawns/Pawns.glb';

// This environment map was processed using Google's Filament project. To process your own:
// 1 - Download the Filament release (https://github.com/google/filament/releases).
// 2 - Run `cmgen --type=ktx --deploy=/path/to/output /path/to/image.hdr`. Other formats are also supported. Run `cmgen --help` for all options.
// 3 - Take the generated coefficients and the KTX file and load them in CesiumJS as shown below.

var L00 = new Cesium.Cartesian3( 0.170455150831422, 0.163151083190219, 0.196966760289763);
var L1_1 = new Cesium.Cartesian3(-0.066550267689383, -0.022088055746048, 0.078835009246127);
var L10 = new Cesium.Cartesian3( 0.038364097478591, 0.045714300098753, 0.063498904606215);
var L11 = new Cesium.Cartesian3(-0.014365363312810, -0.026490613715151, -0.050189404066020);
var L2_2 = new Cesium.Cartesian3(-0.051532786917890, -0.050777795729986, -0.056449044453032);
var L2_1 = new Cesium.Cartesian3( 0.043454596136534, 0.046672590104157, 0.057530107646610);
var L20 = new Cesium.Cartesian3(-0.001640466274110, 0.001286638231156, 0.007228908989616);
var L21 = new Cesium.Cartesian3(-0.042260855700641, -0.046394335094707, -0.057562936365585);
var L22 = new Cesium.Cartesian3(-0.004953478914091, -0.000479681664876, 0.008508150106928);
var coefficients = [L00, L1_1, L10, L11, L2_2, L2_1, L20, L21, L22];

var height = 0.0;
var hpr = new Cesium.HeadingPitchRoll(0.0, 0.0, 0.0);
var origin = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, height);
var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(origin, hpr);

var model = viewer.scene.primitives.add(Cesium.Model.fromGltf({
url : modelURL,
modelMatrix : modelMatrix,
minimumPixelSize : 128
}));

model.readyPromise.then(function(model) {
var camera = viewer.camera;

// Zoom to model
var controller = viewer.scene.screenSpaceCameraController;
var r = 2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near);
controller.minimumZoomDistance = r * 0.5;

var center = Cesium.Matrix4.multiplyByPoint(model.modelMatrix, model.boundingSphere.center, new Cesium.Cartesian3());
var heading = Cesium.Math.toRadians(230.0);
var pitch = Cesium.Math.toRadians(-20.0);
camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, r * 2.0));
camera.lookAtTransform(Cesium.Matrix4.IDENTITY);

model.sphericalHarmonicCoefficients = coefficients;
model.specularEnvironmentMaps = environmentMapURL;

// The viewModel tracks the state of our mini application.
var viewModel = {
luminanceAtZenith : model.luminanceAtZenith
};
// Convert the viewModel members into knockout observables.
Cesium.knockout.track(viewModel);

// Bind the viewModel to the DOM elements of the UI that call for it.
var toolbar = document.getElementById('toolbar');
Cesium.knockout.applyBindings(viewModel, toolbar);

function subscribeParameter(name) {
Cesium.knockout.getObservable(viewModel, name).subscribe(
function(newValue) {
model[name] = newValue;
}
);
}

subscribeParameter('luminanceAtZenith');

Sandcastle.addToggleButton('Use procedural image', false, function(checked) {
if (!checked) {
model.sphericalHarmonicCoefficients = coefficients;
model.specularEnvironmentMaps = environmentMapURL;
} else {
model.sphericalHarmonicCoefficients = undefined;
model.specularEnvironmentMaps = undefined;
}
});
}).otherwise(function(error){
window.alert(error);
});
//Sandcastle_End
Sandcastle.finishedLoading();
}
if (typeof Cesium !== 'undefined') {
startup(Cesium);
} else if (typeof require === 'function') {
require(['Cesium'], startup);
}
</script>
</body>
</html>
Binary file added Apps/Sandcastle/gallery/Image-Based Lighting.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Change Log

### 1.53 - 2019-01-02

##### Additions :tada:
* Added image-based lighting for PBR models and 3D Tiles. [#7172](https://github.com/AnalyticalGraphicsInc/cesium/pull/7172)
* `Scene.specularEnvironmentMaps` is a url to a KTX file that contains the specular environment map and convoluted mipmaps for image-based lighting of all PBR models in the scene.
* `Scene.sphericalHarmonicCoefficients` is an array of 9 `Cartesian3` spherical harmonics coefficients for the diffuse irradiance of all PBR models in the scene.
* The `specularEnvironmentMaps` and `sphericalHarmonicCoefficients` properties of `Model` and `Cesium3DTileset` can be used to override the values from the scene for specific models and tilesets.
* The `luminanceAtZenith` property of `Model` and `Cesium3DTileset` adjusts the luminance of the procedural image-based lighting.

##### Fixes :wrench:
* Fixed 3D Tiles visibility checking when running multiple passes within the same frame. [#7289](https://github.com/AnalyticalGraphicsInc/cesium/pull/7289)
* Fixed contrast on imagery layers. [#7382](https://github.com/AnalyticalGraphicsInc/cesium/issues/7382)
Expand Down
50 changes: 36 additions & 14 deletions Source/Core/loadKTX.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ define([
'./defined',
'./PixelFormat',
'./Resource',
'./RuntimeError'
'./RuntimeError',
'./WebGLConstants'
], function(
when,
Check,
CompressedTextureBuffer,
defined,
PixelFormat,
Resource,
RuntimeError) {
RuntimeError,
WebGLConstants) {
'use strict';

/**
Expand Down Expand Up @@ -93,14 +95,16 @@ define([

var fileIdentifier = [0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A];
var endiannessTest = 0x04030201;
var faceOrder = ['positiveX', 'negativeX', 'positiveY', 'negativeY', 'positiveZ', 'negativeZ'];

var sizeOfUint32 = 4;

function parseKTX(data) {
var byteBuffer = new Uint8Array(data);

var isKTX = true;
for (var i = 0; i < fileIdentifier.length; ++i) {
var i;
for (i = 0; i < fileIdentifier.length; ++i) {
if (fileIdentifier[i] !== byteBuffer[i]) {
isKTX = false;
break;
Expand Down Expand Up @@ -170,9 +174,9 @@ define([

// Some tools use a sized internal format.
// See table 2: https://www.opengl.org/sdk/docs/man/html/glTexImage2D.xhtml
if (glInternalFormat === 0x8051) { // GL_RGB8
if (glInternalFormat === WebGLConstants.RGB8) {
glInternalFormat = PixelFormat.RGB;
} else if (glInternalFormat === 0x8058) { // GL_RGBA8
} else if (glInternalFormat === WebGLConstants.RGBA8) {
glInternalFormat = PixelFormat.RGBA;
}

Expand All @@ -190,6 +194,8 @@ define([
if (glFormat !== 0) {
throw new RuntimeError('glFormat must be zero when the texture is compressed.');
}
} else if (glType !== WebGLConstants.UNSIGNED_BYTE) {
throw new RuntimeError('Only unsigned byte buffers are supported.');
} else if (glBaseInternalFormat !== glFormat) {
throw new RuntimeError('The base internal format must be the same as the format for uncompressed textures.');
}
Expand All @@ -201,19 +207,35 @@ define([
if (numberOfArrayElements !== 0) {
throw new RuntimeError('Texture arrays are unsupported.');
}
if (numberOfFaces !== 1) {
throw new RuntimeError('Cubemaps are unsupported.');

var offset = texture.byteOffset;
var mipmaps = new Array(numberOfMipmapLevels);
for (i = 0; i < numberOfMipmapLevels; ++i) {
var level = mipmaps[i] = {};
for (var j = 0; j < numberOfFaces; ++j) {
var width = pixelWidth >> i;
var height = pixelHeight >> i;
var levelSize = PixelFormat.isCompressedFormat(glInternalFormat) ?
PixelFormat.compressedTextureSizeInBytes(glInternalFormat, width, height) :
PixelFormat.textureSizeInBytes(glInternalFormat, glType, width, height);
var levelBuffer = new Uint8Array(texture.buffer, offset, levelSize);
level[faceOrder[j]] = new CompressedTextureBuffer(glInternalFormat, width, height, levelBuffer);
offset += levelSize;
}
offset += 3 - ((offset + 3) % 4) + 4;
}

// Only use the level 0 mipmap
if (numberOfMipmapLevels > 1) {
var levelSize = PixelFormat.isCompressedFormat(glInternalFormat) ?
PixelFormat.compressedTextureSizeInBytes(glInternalFormat, pixelWidth, pixelHeight) :
PixelFormat.textureSizeInBytes(glInternalFormat, pixelWidth, pixelHeight);
texture = new Uint8Array(texture.buffer, texture.byteOffset, levelSize);
var result = mipmaps;
if (numberOfFaces === 1) {
for (i = 0; i < numberOfMipmapLevels; ++i) {
result[i] = result[i][faceOrder[0]];
}
}
if (numberOfMipmapLevels === 1) {
result = result[0];
}

return new CompressedTextureBuffer(glInternalFormat, pixelWidth, pixelHeight, texture);
return result;
}

return loadKTX;
Expand Down
76 changes: 76 additions & 0 deletions Source/Renderer/AutomaticUniforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,82 @@ define([
}
}),

/**
* An automatic GLSL uniform containing the specular environment map atlas used within the scene.
*
* @alias czm_specularEnvironmentMaps
* @namespace
* @glslUniform
*
* @example
* // GLSL declaration
* uniform sampler2D czm_specularEnvironmentMaps;
*/
czm_specularEnvironmentMaps : new AutomaticUniform({
size : 1,
datatype : WebGLConstants.SAMPLER_2D,
getValue : function(uniformState) {
return uniformState.specularEnvironmentMaps;
}
}),

/**
* An automatic GLSL uniform containing the size of the specular environment map atlas used within the scene.
*
* @alias czm_specularEnvironmentMapSize
* @namespace
* @glslUniform
*
* @example
* // GLSL declaration
* uniform vec2 czm_specularEnvironmentMapSize;
*/
czm_specularEnvironmentMapSize : new AutomaticUniform({
size : 1,
datatype : WebGLConstants.FLOAT_VEC2,
getValue : function(uniformState) {
return uniformState.specularEnvironmentMaps.dimensions;
}
}),

/**
* An automatic GLSL uniform containing the maximum level-of-detail of the specular environment map atlas used within the scene.
*
* @alias czm_specularEnvironmentMapsMaximumLOD
* @namespace
* @glslUniform
*
* @example
* // GLSL declaration
* uniform float czm_specularEnvironmentMapsMaximumLOD;
*/
czm_specularEnvironmentMapsMaximumLOD : new AutomaticUniform({
size : 1,
datatype : WebGLConstants.FLOAT,
getValue : function(uniformState) {
return uniformState.specularEnvironmentMapsMaximumLOD;
}
}),

/**
* An automatic GLSL uniform containing the spherical harmonic coefficients used within the scene.
*
* @alias czm_sphericalHarmonicCoefficients
* @namespace
* @glslUniform
*
* @example
* // GLSL declaration
* uniform vec3[9] czm_sphericalHarmonicCoefficients;
*/
czm_sphericalHarmonicCoefficients : new AutomaticUniform({
size : 9,
datatype : WebGLConstants.FLOAT_VEC3,
getValue : function(uniformState) {
return uniformState.sphericalHarmonicCoefficients;
}
}),

/**
* An automatic GLSL uniform representing a 3x3 rotation matrix that transforms
* from True Equator Mean Equinox (TEME) axes to the pseudo-fixed axes at the current scene time.
Expand Down
9 changes: 9 additions & 0 deletions Source/Renderer/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ define([
'./ShaderCache',
'./ShaderProgram',
'./Texture',
'./TextureCache',
'./UniformState',
'./VertexArray'
], function(
Expand Down Expand Up @@ -60,6 +61,7 @@ define([
ShaderCache,
ShaderProgram,
Texture,
TextureCache,
UniformState,
VertexArray) {
'use strict';
Expand Down Expand Up @@ -234,6 +236,7 @@ define([
this._throwOnWebGLError = false;

this._shaderCache = new ShaderCache(this);
this._textureCache = new TextureCache();

var gl = glContext;

Expand Down Expand Up @@ -469,6 +472,11 @@ define([
return this._shaderCache;
}
},
textureCache : {
get : function() {
return this._textureCache;
}
},
uniformState : {
get : function() {
return this._us;
Expand Down Expand Up @@ -1288,6 +1296,7 @@ define([
}

this._shaderCache = this._shaderCache.destroy();
this._textureCache = this._textureCache.destroy();
this._defaultTexture = this._defaultTexture && this._defaultTexture.destroy();
this._defaultCubeMap = this._defaultCubeMap && this._defaultCubeMap.destroy();

Expand Down
4 changes: 4 additions & 0 deletions Source/Renderer/CubeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ define([
function createFace(target, sourceFace, preMultiplyAlpha, flipY) {
// TODO: gl.pixelStorei(gl._UNPACK_ALIGNMENT, 4);
var arrayBufferView = sourceFace.arrayBufferView;
if (!defined(arrayBufferView)) {
arrayBufferView = sourceFace.bufferView;
}

if (arrayBufferView) {
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
Expand Down
Loading

0 comments on commit 700bbb8

Please sign in to comment.