1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750 |
- define([
- '../Core/BoundingRectangle',
- '../Core/BoundingSphere',
- '../Core/Cartesian2',
- '../Core/Cartesian3',
- '../Core/Color',
- '../Core/ColorGeometryInstanceAttribute',
- '../Core/createGuid',
- '../Core/defaultValue',
- '../Core/defined',
- '../Core/defineProperties',
- '../Core/destroyObject',
- '../Core/DeveloperError',
- '../Core/EllipsoidGeometry',
- '../Core/Event',
- '../Core/GeographicProjection',
- '../Core/GeometryInstance',
- '../Core/GeometryPipeline',
- '../Core/Intersect',
- '../Core/Interval',
- '../Core/JulianDate',
- '../Core/Math',
- '../Core/Matrix4',
- '../Core/mergeSort',
- '../Core/Occluder',
- '../Core/ShowGeometryInstanceAttribute',
- '../Renderer/ClearCommand',
- '../Renderer/Context',
- '../Renderer/PassState',
- './Camera',
- './CreditDisplay',
- './CullingVolume',
- './FrameState',
- './FrustumCommands',
- './FXAA',
- './OIT',
- './OrthographicFrustum',
- './Pass',
- './PerformanceDisplay',
- './PerInstanceColorAppearance',
- './PerspectiveFrustum',
- './PerspectiveOffCenterFrustum',
- './Primitive',
- './PrimitiveCollection',
- './SceneMode',
- './SceneTransforms',
- './SceneTransitioner',
- './ScreenSpaceCameraController',
- './SunPostProcess',
- './TweenCollection'
- ], function(
- BoundingRectangle,
- BoundingSphere,
- Cartesian2,
- Cartesian3,
- Color,
- ColorGeometryInstanceAttribute,
- createGuid,
- defaultValue,
- defined,
- defineProperties,
- destroyObject,
- DeveloperError,
- EllipsoidGeometry,
- Event,
- GeographicProjection,
- GeometryInstance,
- GeometryPipeline,
- Intersect,
- Interval,
- JulianDate,
- CesiumMath,
- Matrix4,
- mergeSort,
- Occluder,
- ShowGeometryInstanceAttribute,
- ClearCommand,
- Context,
- PassState,
- Camera,
- CreditDisplay,
- CullingVolume,
- FrameState,
- FrustumCommands,
- FXAA,
- OIT,
- OrthographicFrustum,
- Pass,
- PerformanceDisplay,
- PerInstanceColorAppearance,
- PerspectiveFrustum,
- PerspectiveOffCenterFrustum,
- Primitive,
- PrimitiveCollection,
- SceneMode,
- SceneTransforms,
- SceneTransitioner,
- ScreenSpaceCameraController,
- SunPostProcess,
- TweenCollection) {
- "use strict";
-
- var Scene = function(options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
- var canvas = options.canvas;
- var contextOptions = options.contextOptions;
- var creditContainer = options.creditContainer;
-
- if (!defined(canvas)) {
- throw new DeveloperError('options and options.canvas are required.');
- }
-
- var context = new Context(canvas, contextOptions);
- if (!defined(creditContainer)) {
- creditContainer = document.createElement('div');
- creditContainer.style.position = 'absolute';
- creditContainer.style.bottom = '0';
- creditContainer.style['text-shadow'] = '0px 0px 2px #000000';
- creditContainer.style.color = '#ffffff';
- creditContainer.style['font-size'] = '10px';
- creditContainer.style['padding-right'] = '5px';
- canvas.parentNode.appendChild(creditContainer);
- }
- this._id = createGuid();
- this._frameState = new FrameState(new CreditDisplay(creditContainer));
- this._frameState.scene3DOnly = defaultValue(options.scene3DOnly, false);
- this._passState = new PassState(context);
- this._canvas = canvas;
- this._context = context;
- this._globe = undefined;
- this._primitives = new PrimitiveCollection();
- this._pickFramebuffer = undefined;
- this._tweens = new TweenCollection();
- this._shaderFrameCount = 0;
- this._sunPostProcess = undefined;
- this._commandList = [];
- this._frustumCommandsList = [];
- this._overlayCommandList = [];
- this._oit = defaultValue(options.orderIndependentTranslucency, true) ? new OIT(context) : undefined;
- this._executeOITFunction = undefined;
- this._fxaa = new FXAA();
- this._clearColorCommand = new ClearCommand({
- color : new Color(),
- owner : this
- });
- this._depthClearCommand = new ClearCommand({
- depth : 1.0,
- owner : this
- });
- this._transitioner = new SceneTransitioner(this);
- this._renderError = new Event();
- this._preRender = new Event();
- this._postRender = new Event();
-
- this.rethrowRenderErrors = false;
-
- this.completeMorphOnUserInput = true;
-
- this.morphStart = new Event();
-
- this.morphComplete = new Event();
-
- this.skyBox = undefined;
-
- this.skyAtmosphere = undefined;
-
- this.sun = undefined;
-
- this.sunBloom = true;
- this._sunBloom = undefined;
-
- this.moon = undefined;
-
- this.backgroundColor = Color.clone(Color.BLACK);
- this._mode = SceneMode.SCENE3D;
- this._mapProjection = defined(options.mapProjection) ? options.mapProjection : new GeographicProjection();
-
- this.morphTime = 1.0;
-
- this.farToNearRatio = 1000.0;
-
- this.debugCommandFilter = undefined;
-
- this.debugShowCommands = false;
-
- this.debugShowFrustums = false;
- this._debugFrustumStatistics = undefined;
-
- this.debugShowFramesPerSecond = false;
-
- this.fxaaOrderIndependentTranslucency = true;
-
- this.fxaa = false;
- this._performanceDisplay = undefined;
- this._debugSphere = undefined;
- var camera = new Camera(this);
- this._camera = camera;
- this._screenSpaceCameraController = new ScreenSpaceCameraController(this);
-
- var near = camera.frustum.near;
- var far = camera.frustum.far;
- var numFrustums = Math.ceil(Math.log(far / near) / Math.log(this.farToNearRatio));
- updateFrustums(near, far, this.farToNearRatio, numFrustums, this._frustumCommandsList);
-
- updateFrameState(this, 0.0, JulianDate.now());
- this.initializeFrame();
- };
- defineProperties(Scene.prototype, {
-
- canvas : {
- get : function() {
- return this._canvas;
- }
- },
-
- drawingBufferHeight : {
- get : function() {
- return this._context.drawingBufferHeight;
- }
- },
-
- drawingBufferWidth : {
- get : function() {
- return this._context.drawingBufferWidth;
- }
- },
-
- maximumAliasedLineWidth : {
- get : function() {
- return this._context.maximumAliasedLineWidth;
- }
- },
-
- globe : {
- get: function() {
- return this._globe;
- },
- set: function(globe) {
- this._globe = this._globe && this._globe.destroy();
- this._globe = globe;
- }
- },
-
- primitives : {
- get : function() {
- return this._primitives;
- }
- },
-
- camera : {
- get : function() {
- return this._camera;
- }
- },
-
-
- screenSpaceCameraController : {
- get : function() {
- return this._screenSpaceCameraController;
- }
- },
-
- mapProjection : {
- get: function() {
- return this._mapProjection;
- }
- },
-
- frameState : {
- get: function() {
- return this._frameState;
- }
- },
-
- tweens : {
- get : function() {
- return this._tweens;
- }
- },
-
- imageryLayers : {
- get : function() {
- return this.globe.imageryLayers;
- }
- },
-
- terrainProvider : {
- get : function() {
- return this.globe.terrainProvider;
- },
- set : function(terrainProvider) {
- this.globe.terrainProvider = terrainProvider;
- }
- },
-
- renderError : {
- get : function() {
- return this._renderError;
- }
- },
-
- preRender : {
- get : function() {
- return this._preRender;
- }
- },
-
- postRender : {
- get : function() {
- return this._postRender;
- }
- },
-
- context : {
- get : function() {
- return this._context;
- }
- },
-
- debugFrustumStatistics : {
- get : function() {
- return this._debugFrustumStatistics;
- }
- },
-
- scene3DOnly : {
- get : function() {
- return this._frameState.scene3DOnly;
- }
- },
-
- orderIndependentTranslucency : {
- get : function() {
- return defined(this._oit);
- }
- },
-
- id : {
- get : function() {
- return this._id;
- }
- },
-
- mode : {
- get : function() {
- return this._mode;
- },
- set : function(value) {
- if (this.scene3DOnly && value !== SceneMode.SCENE3D) {
- throw new DeveloperError('Only SceneMode.SCENE3D is valid when scene3DOnly is true.');
- }
- this._mode = value;
- }
- }
- });
- var scratchOccluderBoundingSphere = new BoundingSphere();
- var scratchOccluder;
- function getOccluder(scene) {
-
-
- var globe = scene.globe;
- if (scene._mode === SceneMode.SCENE3D && defined(globe)) {
- var ellipsoid = globe.ellipsoid;
- scratchOccluderBoundingSphere.radius = ellipsoid.minimumRadius;
- scratchOccluder = Occluder.fromBoundingSphere(scratchOccluderBoundingSphere, scene._camera.positionWC, scratchOccluder);
- return scratchOccluder;
- }
- return undefined;
- }
- function clearPasses(passes) {
- passes.render = false;
- passes.pick = false;
- }
- function updateFrameState(scene, frameNumber, time) {
- var camera = scene._camera;
- var frameState = scene._frameState;
- frameState.mode = scene._mode;
- frameState.morphTime = scene.morphTime;
- frameState.mapProjection = scene.mapProjection;
- frameState.frameNumber = frameNumber;
- frameState.time = JulianDate.clone(time, frameState.time);
- frameState.camera = camera;
- frameState.cullingVolume = camera.frustum.computeCullingVolume(camera.positionWC, camera.directionWC, camera.upWC);
- frameState.occluder = getOccluder(scene);
- frameState.afterRender.length = 0;
- clearPasses(frameState.passes);
- }
- function updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList) {
- frustumCommandsList.length = numFrustums;
- for (var m = 0; m < numFrustums; ++m) {
- var curNear = Math.max(near, Math.pow(farToNearRatio, m) * near);
- var curFar = Math.min(far, farToNearRatio * curNear);
- var frustumCommands = frustumCommandsList[m];
- if (!defined(frustumCommands)) {
- frustumCommands = frustumCommandsList[m] = new FrustumCommands(curNear, curFar);
- } else {
- frustumCommands.near = curNear;
- frustumCommands.far = curFar;
- }
- }
- }
- function insertIntoBin(scene, command, distance) {
- if (scene.debugShowFrustums) {
- command.debugOverlappingFrustums = 0;
- }
- var frustumCommandsList = scene._frustumCommandsList;
- var length = frustumCommandsList.length;
- for (var i = 0; i < length; ++i) {
- var frustumCommands = frustumCommandsList[i];
- var curNear = frustumCommands.near;
- var curFar = frustumCommands.far;
- if (distance.start > curFar) {
- continue;
- }
- if (distance.stop < curNear) {
- break;
- }
- if (command.pass === Pass.OPAQUE || command instanceof ClearCommand) {
- frustumCommands.opaqueCommands[frustumCommands.opaqueIndex++] = command;
- } else if (command.pass === Pass.TRANSLUCENT){
- frustumCommands.translucentCommands[frustumCommands.translucentIndex++] = command;
- }
- if (scene.debugShowFrustums) {
- command.debugOverlappingFrustums |= (1 << i);
- }
- if (command.executeInClosestFrustum) {
- break;
- }
- }
- if (scene.debugShowFrustums) {
- var cf = scene._debugFrustumStatistics.commandsInFrustums;
- cf[command.debugOverlappingFrustums] = defined(cf[command.debugOverlappingFrustums]) ? cf[command.debugOverlappingFrustums] + 1 : 1;
- ++scene._debugFrustumStatistics.totalCommands;
- }
- }
- var scratchCullingVolume = new CullingVolume();
- var distances = new Interval();
- function createPotentiallyVisibleSet(scene) {
- var commandList = scene._commandList;
- var overlayList = scene._overlayCommandList;
- var cullingVolume = scene._frameState.cullingVolume;
- var camera = scene._camera;
- var direction = camera.directionWC;
- var position = camera.positionWC;
- if (scene.debugShowFrustums) {
- scene._debugFrustumStatistics = {
- totalCommands : 0,
- commandsInFrustums : {}
- };
- }
- var frustumCommandsList = scene._frustumCommandsList;
- var numberOfFrustums = frustumCommandsList.length;
- for (var n = 0; n < numberOfFrustums; ++n) {
- frustumCommandsList[n].opaqueIndex = 0;
- frustumCommandsList[n].translucentIndex = 0;
- }
- var near = Number.MAX_VALUE;
- var far = Number.MIN_VALUE;
- var undefBV = false;
- var occluder;
- if (scene._frameState.mode === SceneMode.SCENE3D) {
- occluder = scene._frameState.occluder;
- }
-
- var planes = scratchCullingVolume.planes;
- for (var m = 0; m < 5; ++m) {
- planes[m] = cullingVolume.planes[m];
- }
- cullingVolume = scratchCullingVolume;
- var length = commandList.length;
- for (var i = 0; i < length; ++i) {
- var command = commandList[i];
- var pass = command.pass;
- if (pass === Pass.OVERLAY) {
- overlayList.push(command);
- } else {
- var boundingVolume = command.boundingVolume;
- if (defined(boundingVolume)) {
- if (command.cull &&
- ((cullingVolume.computeVisibility(boundingVolume) === Intersect.OUTSIDE) ||
- (defined(occluder) && !occluder.isBoundingSphereVisible(boundingVolume)))) {
- continue;
- }
- distances = BoundingSphere.computePlaneDistances(boundingVolume, position, direction, distances);
- near = Math.min(near, distances.start);
- far = Math.max(far, distances.stop);
- } else {
-
-
-
- distances.start = camera.frustum.near;
- distances.stop = camera.frustum.far;
- undefBV = !(command instanceof ClearCommand);
- }
- insertIntoBin(scene, command, distances);
- }
- }
- if (undefBV) {
- near = camera.frustum.near;
- far = camera.frustum.far;
- } else {
-
-
-
- near = Math.min(Math.max(near, camera.frustum.near), camera.frustum.far);
- far = Math.max(Math.min(far, camera.frustum.far), near);
- }
-
-
- var farToNearRatio = scene.farToNearRatio;
- var numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio));
- if (near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 &&
- (near < frustumCommandsList[0].near || far > frustumCommandsList[numberOfFrustums - 1].far)))) {
- updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList);
- createPotentiallyVisibleSet(scene);
- }
- }
- function getAttributeLocations(shaderProgram) {
- var attributeLocations = {};
- var attributes = shaderProgram.vertexAttributes;
- for (var a in attributes) {
- if (attributes.hasOwnProperty(a)) {
- attributeLocations[a] = attributes[a].index;
- }
- }
- return attributeLocations;
- }
- function createDebugFragmentShaderProgram(command, scene, shaderProgram) {
- var context = scene.context;
- var sp = defaultValue(shaderProgram, command.shaderProgram);
- var fs = sp.fragmentShaderSource.clone();
- fs.sources = fs.sources.map(function(source) {
- source = source.replace(/void\s+main\s*\(\s*(?:void)?\s*\)/g, 'void czm_Debug_main()');
- return source;
- });
- var newMain =
- 'void main() \n' +
- '{ \n' +
- ' czm_Debug_main(); \n';
- if (scene.debugShowCommands) {
- if (!defined(command._debugColor)) {
- command._debugColor = Color.fromRandom();
- }
- var c = command._debugColor;
- newMain += ' gl_FragColor.rgb *= vec3(' + c.red + ', ' + c.green + ', ' + c.blue + '); \n';
- }
- if (scene.debugShowFrustums) {
-
-
- var r = (command.debugOverlappingFrustums & (1 << 0)) ? '1.0' : '0.0';
- var g = (command.debugOverlappingFrustums & (1 << 1)) ? '1.0' : '0.0';
- var b = (command.debugOverlappingFrustums & (1 << 2)) ? '1.0' : '0.0';
- newMain += ' gl_FragColor.rgb *= vec3(' + r + ', ' + g + ', ' + b + '); \n';
- }
- newMain += '}';
- fs.sources.push(newMain);
- var attributeLocations = getAttributeLocations(sp);
- return context.createShaderProgram(sp.vertexShaderSource, fs, attributeLocations);
- }
- function executeDebugCommand(command, scene, passState, renderState, shaderProgram) {
- if (defined(command.shaderProgram) || defined(shaderProgram)) {
-
- var sp = createDebugFragmentShaderProgram(command, scene, shaderProgram);
- command.execute(scene.context, passState, renderState, sp);
- sp.destroy();
- }
- }
- var transformFrom2D = new Matrix4(0.0, 0.0, 1.0, 0.0,
- 1.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0,
- 0.0, 0.0, 0.0, 1.0);
- transformFrom2D = Matrix4.inverseTransformation(transformFrom2D, transformFrom2D);
- function executeCommand(command, scene, context, passState, renderState, shaderProgram, debugFramebuffer) {
- if ((defined(scene.debugCommandFilter)) && !scene.debugCommandFilter(command)) {
- return;
- }
- if (scene.debugShowCommands || scene.debugShowFrustums) {
- executeDebugCommand(command, scene, passState, renderState, shaderProgram);
- } else {
- command.execute(context, passState, renderState, shaderProgram);
- }
- if (command.debugShowBoundingVolume && (defined(command.boundingVolume))) {
-
-
- if (defined(scene._debugSphere)) {
- scene._debugSphere.destroy();
- }
- var frameState = scene._frameState;
- var boundingVolume = command.boundingVolume;
- var radius = boundingVolume.radius;
- var center = boundingVolume.center;
- var geometry = GeometryPipeline.toWireframe(EllipsoidGeometry.createGeometry(new EllipsoidGeometry({
- radii : new Cartesian3(radius, radius, radius),
- vertexFormat : PerInstanceColorAppearance.FLAT_VERTEX_FORMAT
- })));
- if (frameState.mode !== SceneMode.SCENE3D) {
- center = Matrix4.multiplyByPoint(transformFrom2D, center, center);
- var projection = frameState.mapProjection;
- var centerCartographic = projection.unproject(center);
- center = projection.ellipsoid.cartographicToCartesian(centerCartographic);
- }
- scene._debugSphere = new Primitive({
- geometryInstances : new GeometryInstance({
- geometry : geometry,
- modelMatrix : Matrix4.multiplyByTranslation(Matrix4.IDENTITY, center, new Matrix4()),
- attributes : {
- color : new ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 1.0)
- }
- }),
- appearance : new PerInstanceColorAppearance({
- flat : true,
- translucent : false
- }),
- asynchronous : false
- });
- var commandList = [];
- scene._debugSphere.update(context, frameState, commandList);
- var framebuffer;
- if (defined(debugFramebuffer)) {
- framebuffer = passState.framebuffer;
- passState.framebuffer = debugFramebuffer;
- }
- commandList[0].execute(context, passState);
- if (defined(framebuffer)) {
- passState.framebuffer = framebuffer;
- }
- }
- }
- function isVisible(command, frameState) {
- if (!defined(command)) {
- return;
- }
- var occluder = (frameState.mode === SceneMode.SCENE3D) ? frameState.occluder: undefined;
- var cullingVolume = frameState.cullingVolume;
-
- var planes = scratchCullingVolume.planes;
- for (var k = 0; k < 5; ++k) {
- planes[k] = cullingVolume.planes[k];
- }
- cullingVolume = scratchCullingVolume;
- var boundingVolume = command.boundingVolume;
- return ((defined(command)) &&
- ((!defined(command.boundingVolume)) ||
- !command.cull ||
- ((cullingVolume.computeVisibility(boundingVolume) !== Intersect.OUTSIDE) &&
- (!defined(occluder) || occluder.isBoundingSphereVisible(boundingVolume)))));
- }
- function translucentCompare(a, b, position) {
- return BoundingSphere.distanceSquaredTo(b.boundingVolume, position) - BoundingSphere.distanceSquaredTo(a.boundingVolume, position);
- }
- function executeTranslucentCommandsSorted(scene, executeFunction, passState, commands) {
- var context = scene.context;
- mergeSort(commands, translucentCompare, scene._camera.positionWC);
- var length = commands.length;
- for (var j = 0; j < length; ++j) {
- executeFunction(commands[j], scene, context, passState);
- }
- }
- var scratchPerspectiveFrustum = new PerspectiveFrustum();
- var scratchPerspectiveOffCenterFrustum = new PerspectiveOffCenterFrustum();
- var scratchOrthographicFrustum = new OrthographicFrustum();
- function executeCommands(scene, passState, clearColor, picking) {
- var frameState = scene._frameState;
- var camera = scene._camera;
- var context = scene.context;
- var us = context.uniformState;
- var frustum;
- if (defined(camera.frustum.fov)) {
- frustum = camera.frustum.clone(scratchPerspectiveFrustum);
- } else if (defined(camera.frustum.infiniteProjectionMatrix)){
- frustum = camera.frustum.clone(scratchPerspectiveOffCenterFrustum);
- } else {
- frustum = camera.frustum.clone(scratchOrthographicFrustum);
- }
- if (defined(scene.sun) && scene.sunBloom !== scene._sunBloom) {
- if (scene.sunBloom) {
- scene._sunPostProcess = new SunPostProcess();
- } else if(defined(scene._sunPostProcess)){
- scene._sunPostProcess = scene._sunPostProcess.destroy();
- }
- scene._sunBloom = scene.sunBloom;
- } else if (!defined(scene.sun) && defined(scene._sunPostProcess)) {
- scene._sunPostProcess = scene._sunPostProcess.destroy();
- scene._sunBloom = false;
- }
- var skyBoxCommand = (frameState.passes.render && defined(scene.skyBox)) ? scene.skyBox.update(context, frameState) : undefined;
- var skyAtmosphereCommand = (frameState.passes.render && defined(scene.skyAtmosphere)) ? scene.skyAtmosphere.update(context, frameState) : undefined;
- var sunCommand = (frameState.passes.render && defined(scene.sun)) ? scene.sun.update(scene) : undefined;
- var sunVisible = isVisible(sunCommand, frameState);
- var clear = scene._clearColorCommand;
- Color.clone(clearColor, clear.color);
- clear.execute(context, passState);
- var renderTranslucentCommands = false;
- var i;
- var frustumCommandsList = scene._frustumCommandsList;
- var numFrustums = frustumCommandsList.length;
- for (i = 0; i < numFrustums; ++i) {
- if (frustumCommandsList[i].translucentIndex > 0) {
- renderTranslucentCommands = true;
- break;
- }
- }
- var useOIT = !picking && renderTranslucentCommands && defined(scene._oit) && scene._oit.isSupported();
- if (useOIT) {
- scene._oit.update(context);
- scene._oit.clear(context, passState, clearColor);
- useOIT = useOIT && scene._oit.isSupported();
- }
- var useFXAA = !picking && (scene.fxaa || (useOIT && scene.fxaaOrderIndependentTranslucency));
- if (useFXAA) {
- scene._fxaa.update(context);
- scene._fxaa.clear(context, passState, clearColor);
- }
- var opaqueFramebuffer = passState.framebuffer;
- if (useOIT) {
- opaqueFramebuffer = scene._oit.getColorFramebuffer();
- } else if (useFXAA) {
- opaqueFramebuffer = scene._fxaa.getColorFramebuffer();
- }
- if (sunVisible && scene.sunBloom) {
- passState.framebuffer = scene._sunPostProcess.update(context);
- } else {
- passState.framebuffer = opaqueFramebuffer;
- }
-
-
- frustum.near = camera.frustum.near;
- frustum.far = camera.frustum.far;
- us.updateFrustum(frustum);
- if (defined(skyBoxCommand)) {
- executeCommand(skyBoxCommand, scene, context, passState);
- }
- if (defined(skyAtmosphereCommand)) {
- executeCommand(skyAtmosphereCommand, scene, context, passState);
- }
- if (defined(sunCommand) && sunVisible) {
- sunCommand.execute(context, passState);
- if (scene.sunBloom) {
- scene._sunPostProcess.execute(context, opaqueFramebuffer);
- passState.framebuffer = opaqueFramebuffer;
- }
- }
- var clearDepth = scene._depthClearCommand;
- var executeTranslucentCommands;
- if (useOIT) {
- if (!defined(scene._executeOITFunction)) {
- scene._executeOITFunction = function(scene, executeFunction, passState, commands) {
- scene._oit.executeCommands(scene, executeFunction, passState, commands);
- };
- }
- executeTranslucentCommands = scene._executeOITFunction;
- } else {
- executeTranslucentCommands = executeTranslucentCommandsSorted;
- }
- for (i = 0; i < numFrustums; ++i) {
- var index = numFrustums - i - 1;
- var frustumCommands = frustumCommandsList[index];
- frustum.near = frustumCommands.near;
- frustum.far = frustumCommands.far;
- if (index !== 0) {
-
- frustum.near *= 0.99;
- }
- us.updateFrustum(frustum);
- clearDepth.execute(context, passState);
- var commands = frustumCommands.opaqueCommands;
- var length = frustumCommands.opaqueIndex;
- for (var j = 0; j < length; ++j) {
- executeCommand(commands[j], scene, context, passState);
- }
- frustum.near = frustumCommands.near;
- us.updateFrustum(frustum);
- commands = frustumCommands.translucentCommands;
- commands.length = frustumCommands.translucentIndex;
- executeTranslucentCommands(scene, executeCommand, passState, commands);
- }
- if (useOIT) {
- passState.framebuffer = useFXAA ? scene._fxaa.getColorFramebuffer() : undefined;
- scene._oit.execute(context, passState);
- }
- if (useFXAA) {
- passState.framebuffer = undefined;
- scene._fxaa.execute(context, passState);
- }
- }
- function executeOverlayCommands(scene, passState) {
- var context = scene.context;
- var commandList = scene._overlayCommandList;
- var length = commandList.length;
- for (var i = 0; i < length; ++i) {
- commandList[i].execute(context, passState);
- }
- }
- function updatePrimitives(scene) {
- var context = scene.context;
- var frameState = scene._frameState;
- var commandList = scene._commandList;
- if (scene._globe) {
- scene._globe.update(context, frameState, commandList);
- }
- scene._primitives.update(context, frameState, commandList);
- if (defined(scene.moon)) {
- scene.moon.update(context, frameState, commandList);
- }
- }
- function callAfterRenderFunctions(frameState) {
-
-
- var functions = frameState.afterRender;
- for (var i = 0, length = functions.length; i < length; ++i) {
- functions[i]();
- }
- functions.length = 0;
- }
-
- Scene.prototype.initializeFrame = function() {
-
- if (this._shaderFrameCount++ === 120) {
- this._shaderFrameCount = 0;
- this._context.shaderCache.destroyReleasedShaderPrograms();
- }
- this._tweens.update();
- this._camera.update(this._mode);
- this._screenSpaceCameraController.update();
- };
- function render(scene, time) {
- if (!defined(time)) {
- time = JulianDate.now();
- }
- scene._preRender.raiseEvent(scene, time);
- var us = scene.context.uniformState;
- var frameState = scene._frameState;
- var frameNumber = CesiumMath.incrementWrap(frameState.frameNumber, 15000000.0, 1.0);
- updateFrameState(scene, frameNumber, time);
- frameState.passes.render = true;
- frameState.creditDisplay.beginFrame();
- var context = scene.context;
- us.update(context, frameState);
- scene._commandList.length = 0;
- scene._overlayCommandList.length = 0;
- updatePrimitives(scene);
- createPotentiallyVisibleSet(scene);
- var passState = scene._passState;
- executeCommands(scene, passState, defaultValue(scene.backgroundColor, Color.BLACK));
- executeOverlayCommands(scene, passState);
- frameState.creditDisplay.endFrame();
- if (scene.debugShowFramesPerSecond) {
- if (!defined(scene._performanceDisplay)) {
- var performanceContainer = document.createElement('div');
- performanceContainer.className = 'cesium-performanceDisplay';
- performanceContainer.style.position = 'absolute';
- performanceContainer.style.top = '50px';
- performanceContainer.style.right = '10px';
- var container = scene._canvas.parentNode;
- container.appendChild(performanceContainer);
- var performanceDisplay = new PerformanceDisplay({container: performanceContainer});
- scene._performanceDisplay = performanceDisplay;
- scene._performanceContainer = performanceContainer;
- }
- scene._performanceDisplay.update();
- } else if (defined(scene._performanceDisplay)) {
- scene._performanceDisplay = scene._performanceDisplay && scene._performanceDisplay.destroy();
- scene._performanceContainer.parentNode.removeChild(scene._performanceContainer);
- }
- context.endFrame();
- callAfterRenderFunctions(frameState);
- scene._postRender.raiseEvent(scene, time);
- }
-
- Scene.prototype.render = function(time) {
- try {
- render(this, time);
- } catch (error) {
- this._renderError.raiseEvent(this, error);
- if (this.rethrowRenderErrors) {
- throw error;
- }
- }
- };
-
- Scene.prototype.clampLineWidth = function(width) {
- var context = this._context;
- return Math.max(context.minimumAliasedLineWidth, Math.min(width, context.maximumAliasedLineWidth));
- };
- var orthoPickingFrustum = new OrthographicFrustum();
- var scratchOrigin = new Cartesian3();
- var scratchDirection = new Cartesian3();
- var scratchBufferDimensions = new Cartesian2();
- var scratchPixelSize = new Cartesian2();
- function getPickOrthographicCullingVolume(scene, drawingBufferPosition, width, height) {
- var camera = scene._camera;
- var frustum = camera.frustum;
- var drawingBufferWidth = scene.drawingBufferWidth;
- var drawingBufferHeight = scene.drawingBufferHeight;
- var x = (2.0 / drawingBufferWidth) * drawingBufferPosition.x - 1.0;
- x *= (frustum.right - frustum.left) * 0.5;
- var y = (2.0 / drawingBufferHeight) * (drawingBufferHeight - drawingBufferPosition.y) - 1.0;
- y *= (frustum.top - frustum.bottom) * 0.5;
- var origin = Cartesian3.clone(camera.position, scratchOrigin);
- Cartesian3.multiplyByScalar(camera.right, x, scratchDirection);
- Cartesian3.add(scratchDirection, origin, origin);
- Cartesian3.multiplyByScalar(camera.up, y, scratchDirection);
- Cartesian3.add(scratchDirection, origin, origin);
- Cartesian3.fromElements(origin.z, origin.x, origin.y, origin);
- scratchBufferDimensions.x = drawingBufferWidth;
- scratchBufferDimensions.y = drawingBufferHeight;
- var pixelSize = frustum.getPixelSize(scratchBufferDimensions, undefined, scratchPixelSize);
- var ortho = orthoPickingFrustum;
- ortho.right = pixelSize.x * 0.5;
- ortho.left = -ortho.right;
- ortho.top = pixelSize.y * 0.5;
- ortho.bottom = -ortho.top;
- ortho.near = frustum.near;
- ortho.far = frustum.far;
- return ortho.computeCullingVolume(origin, camera.directionWC, camera.upWC);
- }
- var perspPickingFrustum = new PerspectiveOffCenterFrustum();
- function getPickPerspectiveCullingVolume(scene, drawingBufferPosition, width, height) {
- var camera = scene._camera;
- var frustum = camera.frustum;
- var near = frustum.near;
- var drawingBufferWidth = scene.drawingBufferWidth;
- var drawingBufferHeight = scene.drawingBufferHeight;
- var tanPhi = Math.tan(frustum.fovy * 0.5);
- var tanTheta = frustum.aspectRatio * tanPhi;
- var x = (2.0 / drawingBufferWidth) * drawingBufferPosition.x - 1.0;
- var y = (2.0 / drawingBufferHeight) * (drawingBufferHeight - drawingBufferPosition.y) - 1.0;
- var xDir = x * near * tanTheta;
- var yDir = y * near * tanPhi;
- scratchBufferDimensions.x = drawingBufferWidth;
- scratchBufferDimensions.y = drawingBufferHeight;
- var pixelSize = frustum.getPixelSize(scratchBufferDimensions, undefined, scratchPixelSize);
- var pickWidth = pixelSize.x * width * 0.5;
- var pickHeight = pixelSize.y * height * 0.5;
- var offCenter = perspPickingFrustum;
- offCenter.top = yDir + pickHeight;
- offCenter.bottom = yDir - pickHeight;
- offCenter.right = xDir + pickWidth;
- offCenter.left = xDir - pickWidth;
- offCenter.near = near;
- offCenter.far = frustum.far;
- return offCenter.computeCullingVolume(camera.positionWC, camera.directionWC, camera.upWC);
- }
- function getPickCullingVolume(scene, drawingBufferPosition, width, height) {
- if (scene._mode === SceneMode.SCENE2D) {
- return getPickOrthographicCullingVolume(scene, drawingBufferPosition, width, height);
- }
- return getPickPerspectiveCullingVolume(scene, drawingBufferPosition, width, height);
- }
-
- var rectangleWidth = 3.0;
- var rectangleHeight = 3.0;
- var scratchRectangle = new BoundingRectangle(0.0, 0.0, rectangleWidth, rectangleHeight);
- var scratchColorZero = new Color(0.0, 0.0, 0.0, 0.0);
- var scratchPosition = new Cartesian2();
-
- Scene.prototype.pick = function(windowPosition) {
-
- if(!defined(windowPosition)) {
- throw new DeveloperError('windowPosition is undefined.');
- }
-
- var context = this._context;
- var us = context.uniformState;
- var frameState = this._frameState;
- var drawingBufferPosition = SceneTransforms.transformWindowToDrawingBuffer(this, windowPosition, scratchPosition);
- if (!defined(this._pickFramebuffer)) {
- this._pickFramebuffer = context.createPickFramebuffer();
- }
-
- updateFrameState(this, frameState.frameNumber, frameState.time);
- frameState.cullingVolume = getPickCullingVolume(this, drawingBufferPosition, rectangleWidth, rectangleHeight);
- frameState.passes.pick = true;
- us.update(context, frameState);
- this._commandList.length = 0;
- updatePrimitives(this);
- createPotentiallyVisibleSet(this);
- scratchRectangle.x = drawingBufferPosition.x - ((rectangleWidth - 1.0) * 0.5);
- scratchRectangle.y = (this.drawingBufferHeight - drawingBufferPosition.y) - ((rectangleHeight - 1.0) * 0.5);
- executeCommands(this, this._pickFramebuffer.begin(scratchRectangle), scratchColorZero, true);
- var object = this._pickFramebuffer.end(scratchRectangle);
- context.endFrame();
- callAfterRenderFunctions(frameState);
- return object;
- };
-
- Scene.prototype.drillPick = function(windowPosition) {
-
-
-
-
- if (!defined(windowPosition)) {
- throw new DeveloperError('windowPosition is undefined.');
- }
-
- var pickedObjects = [];
- var pickedResult = this.pick(windowPosition);
- while (defined(pickedResult) && defined(pickedResult.primitive)) {
- var primitive = pickedResult.primitive;
- pickedObjects.push(pickedResult);
-
- if (defined(primitive.show)) {
- primitive.show = false;
- } else if (typeof primitive.getGeometryInstanceAttributes === 'function') {
- var attributes = primitive.getGeometryInstanceAttributes(pickedResult.id);
- if (defined(attributes) && defined(attributes.show)) {
- attributes.show = ShowGeometryInstanceAttribute.toValue(false);
- }
- }
- pickedResult = this.pick(windowPosition);
- }
-
- for (var i = 0; i < pickedObjects.length; ++i) {
- var p = pickedObjects[i].primitive;
- if (defined(p.show)) {
- p.show = true;
- } else if (typeof p.getGeometryInstanceAttributes === 'function') {
- var attr = p.getGeometryInstanceAttributes(pickedObjects[i].id);
- if (defined(attr) && defined(attr.show)) {
- attr.show = ShowGeometryInstanceAttribute.toValue(true);
- }
- }
- }
- return pickedObjects;
- };
-
- Scene.prototype.completeMorph = function(){
- this._transitioner.completeMorph();
- };
-
- Scene.prototype.morphTo2D = function(duration) {
- var ellipsoid;
- var globe = this.globe;
- if (defined(globe)) {
- ellipsoid = globe.ellipsoid;
- } else {
- ellipsoid = this.mapProjection.ellipsoid;
- }
- duration = defaultValue(duration, 2.0);
- this._transitioner.morphTo2D(duration, ellipsoid);
- };
-
- Scene.prototype.morphToColumbusView = function(duration) {
- var ellipsoid;
- var globe = this.globe;
- if (defined(globe)) {
- ellipsoid = globe.ellipsoid;
- } else {
- ellipsoid = this.mapProjection.ellipsoid;
- }
- duration = defaultValue(duration, 2.0);
- this._transitioner.morphToColumbusView(duration, ellipsoid);
- };
-
- Scene.prototype.morphTo3D = function(duration) {
- var ellipsoid;
- var globe = this.globe;
- if (defined(globe)) {
- ellipsoid = globe.ellipsoid;
- } else {
- ellipsoid = this.mapProjection.ellipsoid;
- }
- duration = defaultValue(duration, 2.0);
- this._transitioner.morphTo3D(duration, ellipsoid);
- };
-
- Scene.prototype.isDestroyed = function() {
- return false;
- };
-
- Scene.prototype.destroy = function() {
- this._tweens.removeAll();
- this._screenSpaceCameraController = this._screenSpaceCameraController && this._screenSpaceCameraController.destroy();
- this._pickFramebuffer = this._pickFramebuffer && this._pickFramebuffer.destroy();
- this._primitives = this._primitives && this._primitives.destroy();
- this._globe = this._globe && this._globe.destroy();
- this.skyBox = this.skyBox && this.skyBox.destroy();
- this.skyAtmosphere = this.skyAtmosphere && this.skyAtmosphere.destroy();
- this._debugSphere = this._debugSphere && this._debugSphere.destroy();
- this.sun = this.sun && this.sun.destroy();
- this._sunPostProcess = this._sunPostProcess && this._sunPostProcess.destroy();
- this._transitioner.destroy();
- if (defined(this._oit)) {
- this._oit.destroy();
- }
- this._fxaa.destroy();
- this._context = this._context && this._context.destroy();
- this._frameState.creditDisplay.destroy();
- if (defined(this._performanceDisplay)){
- this._performanceDisplay = this._performanceDisplay && this._performanceDisplay.destroy();
- this._performanceContainer.parentNode.removeChild(this._performanceContainer);
- }
- return destroyObject(this);
- };
- return Scene;
- });
|