Skip to content

Commit 56373c4

Browse files
authored
Merge pull request #8493 from AnalyticalGraphicsInc/light-direction
Custom light source
2 parents df63f96 + cc4afc7 commit 56373c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1035
-200
lines changed
Binary file not shown.

Apps/Sandcastle/gallery/Interpolation.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
terrainProvider: Cesium.createWorldTerrain()
3232
});
3333

34-
//Enable lighting based on sun/moon positions
34+
//Enable lighting based on the sun position
3535
viewer.scene.globe.enableLighting = true;
3636

3737
//Enable depth testing so things behind the terrain disappear.

Apps/Sandcastle/gallery/Lighting.html

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
7+
<meta name="description" content="Set custom light direction and color.">
8+
<meta name="cesium-sandcastle-labels" content="Showcases">
9+
<title>Cesium Demo</title>
10+
<script type="text/javascript" src="../Sandcastle-header.js"></script>
11+
<script type="text/javascript" src="../../../Build/CesiumUnminified/Cesium.js" nomodule></script>
12+
<script type="module" src="../load-cesium-es6.js"></script>
13+
</head>
14+
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
15+
<style>
16+
@import url(../templates/bucket.css);
17+
</style>
18+
<div id="cesiumContainer" class="fullSize"></div>
19+
<div id="loadingOverlay"><h1>Loading...</h1></div>
20+
<div id="toolbar"></div>
21+
<script id="cesium_sandcastle_script">
22+
function startup(Cesium) {
23+
'use strict';
24+
//Sandcastle_Begin
25+
var viewer = new Cesium.Viewer('cesiumContainer', {
26+
terrainProvider : Cesium.createWorldTerrain({
27+
requestWaterMask: true,
28+
requestVertexNormals: true
29+
})
30+
});
31+
32+
var scene = viewer.scene;
33+
scene.globe.enableLighting = true;
34+
35+
var scratchIcrfToFixed = new Cesium.Matrix3();
36+
var scratchMoonPosition = new Cesium.Cartesian3();
37+
var scratchMoonDirection = new Cesium.Cartesian3();
38+
39+
function getMoonDirection(result) {
40+
result = Cesium.defined(result) ? result : new Cesium.Cartesian3();
41+
var icrfToFixed = scratchIcrfToFixed;
42+
var date = viewer.clock.currentTime;
43+
if (!Cesium.defined(Cesium.Transforms.computeIcrfToFixedMatrix(date, icrfToFixed))) {
44+
Cesium.Transforms.computeTemeToPseudoFixedMatrix(date, icrfToFixed);
45+
}
46+
var moonPosition = Cesium.Simon1994PlanetaryPositions.computeMoonPositionInEarthInertialFrame(date, scratchMoonPosition);
47+
Cesium.Matrix3.multiplyByVector(icrfToFixed, moonPosition, moonPosition);
48+
var moonDirection = Cesium.Cartesian3.normalize(moonPosition, scratchMoonDirection);
49+
return Cesium.Cartesian3.negate(moonDirection, result);
50+
}
51+
52+
var directionalLight = new Cesium.DirectionalLight({
53+
direction : new Cesium.Cartesian3(0.2454278300540191, 0.8842635425193919, 0.39729481195458805)
54+
});
55+
56+
var flashlight = new Cesium.DirectionalLight({
57+
direction : scene.camera.directionWC // Updated every frame
58+
});
59+
60+
var moonLight = new Cesium.DirectionalLight({
61+
direction : getMoonDirection(), // Updated every frame
62+
color : new Cesium.Color(0.9, 0.925, 1.0),
63+
intensity : 0.5
64+
});
65+
66+
var sunLight = new Cesium.SunLight();
67+
68+
var customColorLight = new Cesium.DirectionalLight({
69+
direction : new Cesium.Cartesian3(-0.2454278300540191, 0.8842635425193919, 0.39729481195458805),
70+
color : Cesium.Color.fromCssColorString('#deca7c')
71+
});
72+
73+
scene.preRender.addEventListener(function(scene, time) {
74+
if (scene.light === flashlight) {
75+
scene.light.direction = Cesium.Cartesian3.clone(scene.camera.directionWC, scene.light.direction);
76+
} else if (scene.light === moonLight) {
77+
scene.light.direction = getMoonDirection(scene.light.direction);
78+
}
79+
});
80+
81+
viewer.entities.add({
82+
position : Cesium.Cartesian3.fromRadians(-2.1463338399937277, 0.6677959688982861, 32.18991401746337),
83+
model : {
84+
uri : '../../SampleData/models/CesiumBalloon/CesiumBalloon.glb',
85+
scale : 7.0
86+
}
87+
});
88+
89+
viewer.entities.add({
90+
position : Cesium.Cartesian3.fromRadians(-2.14633449752228, 0.667796065242357, 24.47647034111423),
91+
cylinder : {
92+
length : 8.0,
93+
topRadius : 2.0,
94+
bottomRadius : 2.0,
95+
material : Cesium.Color.WHITE
96+
}
97+
});
98+
99+
viewer.entities.add({
100+
position : Cesium.Cartesian3.fromRadians(-2.1463332294173365, 0.6677959755384729, 26.2876064083145),
101+
ellipsoid : {
102+
radii : new Cesium.Cartesian3(2.5, 2.5, 2.5),
103+
material : Cesium.Color.WHITE.withAlpha(0.5)
104+
}
105+
});
106+
107+
function setTime(iso8601) {
108+
var currentTime = Cesium.JulianDate.fromIso8601(iso8601);
109+
var endTime = Cesium.JulianDate.addDays(currentTime, 2, new Cesium.JulianDate());
110+
111+
viewer.clock.currentTime = currentTime;
112+
viewer.timeline.zoomTo(currentTime, endTime);
113+
}
114+
115+
function reset() {
116+
// Set scene defaults
117+
scene.light = sunLight;
118+
scene.globe.dynamicAtmosphereLighting = true;
119+
scene.globe.dynamicAtmosphereLightingFromSun = false;
120+
setTime('2020-01-09T23:00:39.018261982600961346Z');
121+
}
122+
123+
viewer.scene.camera.setView({
124+
destination : new Cesium.Cartesian3(-2729490.8390059783, -4206389.878855597, 3928671.2763356343),
125+
orientation : new Cesium.HeadingPitchRoll(2.2482480507178426, -0.20084951548781982, 0.002593933673552762),
126+
endTransform : Cesium.Matrix4.IDENTITY
127+
});
128+
129+
var options = [{
130+
text : 'Fixed lighting',
131+
onselect : function() {
132+
reset();
133+
scene.light = directionalLight;
134+
}
135+
}, {
136+
text : 'Flashlight',
137+
onselect : function() {
138+
reset();
139+
scene.light = flashlight;
140+
scene.globe.dynamicAtmosphereLighting = false;
141+
}
142+
}, {
143+
text : 'Moonlight',
144+
onselect : function() {
145+
reset();
146+
scene.light = moonLight;
147+
scene.globe.dynamicAtmosphereLightingFromSun = true;
148+
setTime('2020-01-10T05:29:41.17946898164518643Z');
149+
}
150+
}, {
151+
text : 'Sunlight',
152+
onselect : function() {
153+
reset();
154+
}
155+
}, {
156+
text : 'Custom color',
157+
onselect : function() {
158+
reset();
159+
scene.light = customColorLight;
160+
}
161+
}];
162+
163+
Sandcastle.addToolbarMenu(options);
164+
//Sandcastle_End
165+
Sandcastle.finishedLoading();
166+
}
167+
if (typeof Cesium !== 'undefined') {
168+
window.startupCalled = true;
169+
startup(Cesium);
170+
}
171+
</script>
172+
</body>
173+
</html>

Apps/Sandcastle/gallery/Lighting.jpg

18.8 KB
Loading

CHANGES.md

+9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ Change Log
33

44
### 1.66.0 - 2020-02-03
55

6+
##### Deprecated :hourglass_flowing_sand:
7+
* The property `Scene.sunColor` has been deprecated and will be removed in Cesium 1.69. Use `scene.light.color` and `scene.light.intensity` instead. [#8493](https://github.com/AnalyticalGraphicsInc/cesium/pull/8493)
8+
69
##### Additions :tada:
710

11+
* Add more customization to Cesium's lighting system [#8493](https://github.com/AnalyticalGraphicsInc/cesium/pull/8493)
12+
* Added `Light`, `DirectionalLight`, and `SunLight` classes for creating custom light sources.
13+
* Added `Scene.light` for setting the scene's light source, which defaults to a `SunLight`.
14+
* Added `Globe.dynamicAtmosphereLighting` for enabling lighting effects on atmosphere and fog, such as day/night transitions. It is true by default but may be set to false if the atmosphere should stay unchanged regardless of the scene's light direction.
15+
* Added `Globe.dynamicAtmosphereLightingFromSun` for using the sun direction instead of the scene's light direction when `Globe.dynamicAtmosphereLighting` is enabled. See the moonlight example in the [Lighting Sandcastle example](https://cesiumjs.org/Cesium/Apps/Sandcastle/?src=Lighting.html).
16+
* Primitives and the globe are now shaded with the scene light's color.
817
* Added `Globe.showSkirts` to support the ability to hide terrain skirts when viewing terrain from below the surface. [#8489](https://github.com/AnalyticalGraphicsInc/cesium/pull/8489)
918
* Fixed `BoundingSphere.projectTo2D` when the bounding sphere’s center is at the origin. [#8482](https://github.com/AnalyticalGraphicsInc/cesium/pull/8482)
1019
* Added `minificationFilter` and `magnificationFilter` options to `Material` to control texture filtering. [#8473](https://github.com/AnalyticalGraphicsInc/cesium/pull/8473)

Source/DataSources/BoxGraphics.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
2121
* @param {Property} [options.outline=false] A boolean Property specifying whether the box is outlined.
2222
* @param {Property} [options.outlineColor=Color.BLACK] A Property specifying the {@link Color} of the outline.
2323
* @param {Property} [options.outlineWidth=1.0] A numeric Property specifying the width of the outline.
24-
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the box casts or receives shadows from each light source.
24+
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the box casts or receives shadows from light sources.
2525
* @param {Property} [options.distanceDisplayCondition] A Property specifying at what distance from the camera that this box will be displayed.
2626
*
2727
* @demo {@link https://sandcastle.cesium.com/index.html?src=Box.html|Cesium Sandcastle Box Demo}
@@ -130,7 +130,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
130130

131131
/**
132132
* Get or sets the enum Property specifying whether the box
133-
* casts or receives shadows from each light source.
133+
* casts or receives shadows from light sources.
134134
* @memberof BoxGraphics.prototype
135135
* @type {Property}
136136
* @default ShadowMode.DISABLED

Source/DataSources/CorridorGraphics.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
2929
* @param {Property} [options.outline=false] A boolean Property specifying whether the corridor is outlined.
3030
* @param {Property} [options.outlineColor=Color.BLACK] A Property specifying the {@link Color} of the outline.
3131
* @param {Property} [options.outlineWidth=1.0] A numeric Property specifying the width of the outline.
32-
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the corridor casts or receives shadows from each light source.
32+
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the corridor casts or receives shadows from light sources.
3333
* @param {Property} [options.distanceDisplayCondition] A Property specifying at what distance from the camera that this corridor will be displayed.
3434
* @param {Property} [options.classificationType=ClassificationType.BOTH] An enum Property specifying whether this corridor will classify terrain, 3D Tiles, or both when on the ground.
3535
* @param {ConstantProperty} [options.zIndex] A Property specifying the zIndex of the corridor, used for ordering. Only has an effect if height and extrudedHeight are undefined, and if the corridor is static.
@@ -205,7 +205,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
205205

206206
/**
207207
* Get or sets the enum Property specifying whether the corridor
208-
* casts or receives shadows from each light source.
208+
* casts or receives shadows from light sources.
209209
* @memberof CorridorGraphics.prototype
210210
* @type {Property}
211211
* @default ShadowMode.DISABLED

Source/DataSources/CylinderGraphics.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
2626
* @param {Property} [options.outlineWidth=1.0] A numeric Property specifying the width of the outline.
2727
* @param {Property} [options.numberOfVerticalLines=16] A numeric Property specifying the number of vertical lines to draw along the perimeter for the outline.
2828
* @param {Property} [options.slices=128] The number of edges around the perimeter of the cylinder.
29-
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the cylinder casts or receives shadows from each light source.
29+
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the cylinder casts or receives shadows from light sources.
3030
* @param {Property} [options.distanceDisplayCondition] A Property specifying at what distance from the camera that this cylinder will be displayed.
3131
*/
3232
function CylinderGraphics(options) {
@@ -172,7 +172,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
172172

173173
/**
174174
* Get or sets the enum Property specifying whether the cylinder
175-
* casts or receives shadows from each light source.
175+
* casts or receives shadows from light sources.
176176
* @memberof CylinderGraphics.prototype
177177
* @type {Property}
178178
* @default ShadowMode.DISABLED

Source/DataSources/EllipseGraphics.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
3232
* @param {Property} [options.outlineColor=Color.BLACK] A Property specifying the {@link Color} of the outline.
3333
* @param {Property} [options.outlineWidth=1.0] A numeric Property specifying the width of the outline.
3434
* @param {Property} [options.numberOfVerticalLines=16] A numeric Property specifying the number of vertical lines to draw along the perimeter for the outline.
35-
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the ellipse casts or receives shadows from each light source.
35+
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the ellipse casts or receives shadows from light sources.
3636
* @param {Property} [options.distanceDisplayCondition] A Property specifying at what distance from the camera that this ellipse will be displayed.
3737
* @param {Property} [options.classificationType=ClassificationType.BOTH] An enum Property specifying whether this ellipse will classify terrain, 3D Tiles, or both when on the ground.
3838
* @param {ConstantProperty} [options.zIndex=0] A property specifying the zIndex of the Ellipse. Used for ordering ground geometry. Only has an effect if the ellipse is constant and neither height or exturdedHeight are specified.
@@ -227,7 +227,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
227227

228228
/**
229229
* Get or sets the enum Property specifying whether the ellipse
230-
* casts or receives shadows from each light source.
230+
* casts or receives shadows from light sources.
231231
* @memberof EllipseGraphics.prototype
232232
* @type {Property}
233233
* @default ShadowMode.DISABLED

Source/DataSources/EllipsoidGraphics.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
2929
* @param {Property} [options.stackPartitions=64] A Property specifying the number of stacks.
3030
* @param {Property} [options.slicePartitions=64] A Property specifying the number of radial slices.
3131
* @param {Property} [options.subdivisions=128] A Property specifying the number of samples per outline ring, determining the granularity of the curvature.
32-
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the ellipsoid casts or receives shadows from each light source.
32+
* @param {Property} [options.shadows=ShadowMode.DISABLED] An enum Property specifying whether the ellipsoid casts or receives shadows from light sources.
3333
* @param {Property} [options.distanceDisplayCondition] A Property specifying at what distance from the camera that this ellipsoid will be displayed.
3434
*
3535
* @demo {@link https://sandcastle.cesium.com/index.html?src=Spheres%20and%20Ellipsoids.html|Cesium Sandcastle Spheres and Ellipsoids Demo}
@@ -219,7 +219,7 @@ import createPropertyDescriptor from './createPropertyDescriptor.js';
219219

220220
/**
221221
* Get or sets the enum Property specifying whether the ellipsoid
222-
* casts or receives shadows from each light source.
222+
* casts or receives shadows from light sources.
223223
* @memberof EllipsoidGraphics.prototype
224224
* @type {Property}
225225
* @default ShadowMode.DISABLED

Source/DataSources/GeometryUpdater.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ import Property from './Property.js';
189189
},
190190
/**
191191
* Gets the property specifying whether the geometry
192-
* casts or receives shadows from each light source.
192+
* casts or receives shadows from light sources.
193193
* @memberof GeometryUpdater.prototype
194194
*
195195
* @type {Property}

Source/DataSources/ModelGraphics.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ import PropertyBag from './PropertyBag.js';
3939
* @param {Property} [options.incrementallyLoadTextures=true] Determine if textures may continue to stream in after the model is loaded.
4040
* @param {Property} [options.runAnimations=true] A boolean Property specifying if glTF animations specified in the model should be started.
4141
* @param {Property} [options.clampAnimations=true] A boolean Property specifying if glTF animations should hold the last pose for time durations with no keyframes.
42-
* @param {Property} [options.shadows=ShadowMode.ENABLED] An enum Property specifying whether the model casts or receives shadows from each light source.
42+
* @param {Property} [options.shadows=ShadowMode.ENABLED] An enum Property specifying whether the model casts or receives shadows from light sources.
4343
* @param {Property} [options.heightReference=HeightReference.NONE] A Property specifying what the height is relative to.
4444
* @param {Property} [options.silhouetteColor=Color.RED] A Property specifying the {@link Color} of the silhouette.
4545
* @param {Property} [options.silhouetteSize=0.0] A numeric Property specifying the size of the silhouette in pixels.
4646
* @param {Property} [options.color=Color.WHITE] A Property specifying the {@link Color} that blends with the model's rendered color.
4747
* @param {Property} [options.colorBlendMode=ColorBlendMode.HIGHLIGHT] An enum Property specifying how the color blends with the model.
4848
* @param {Property} [options.colorBlendAmount=0.5] A numeric Property specifying the color strength when the <code>colorBlendMode</code> is <code>MIX</code>. A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with any value in-between resulting in a mix of the two.
4949
* @param {Property} [options.imageBasedLightingFactor=new Cartesian2(1.0, 1.0)] A property specifying the contribution from diffuse and specular image-based lighting.
50-
* @param {Property} [options.lightColor] A property specifying the light color to use when shading the model. The default sun light color will be used when <code>undefined</code>.
50+
* @param {Property} [options.lightColor] A property specifying the light color when shading the model. When <code>undefined</code> the scene's light color are used instead.
5151
* @param {Property} [options.distanceDisplayCondition] A Property specifying at what distance from the camera that this model will be displayed.
5252
* @param {PropertyBag} [options.nodeTransformations] An object, where keys are names of nodes, and values are {@link TranslationRotationScale} Properties describing the transformation to apply to that node. The transformation is applied after the node's existing transformation as specified in the glTF, and does not replace the node's existing transformation.
5353
* @param {PropertyBag} [options.articulations] An object, where keys are composed of an articulation name, a single space, and a stage name, and the values are numeric properties.
@@ -188,7 +188,7 @@ import PropertyBag from './PropertyBag.js';
188188

189189
/**
190190
* Get or sets the enum Property specifying whether the model
191-
* casts or receives shadows from each light source.
191+
* casts or receives shadows from light sources.
192192
* @memberof ModelGraphics.prototype
193193
* @type {Property}
194194
* @default ShadowMode.ENABLED
@@ -253,7 +253,7 @@ import PropertyBag from './PropertyBag.js';
253253
imageBasedLightingFactor : createPropertyDescriptor('imageBasedLightingFactor'),
254254

255255
/**
256-
* A property specifying the {@link Cartesian3} color of the light source when shading the model.
256+
* A property specifying the {@link Cartesian3} light color when shading the model. When <code>undefined</code> the scene's light color are used instead.
257257
* @memberOf ModelGraphics.prototype
258258
* @type {Property}
259259
*/

0 commit comments

Comments
 (0)