|
| 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> |
0 commit comments