123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- define([
- '../Core/BoundingRectangle',
- '../Core/BoundingSphere',
- '../Core/Cartesian2',
- '../Core/Cartesian3',
- '../Core/Cartesian4',
- '../Core/Color',
- '../Core/ComponentDatatype',
- '../Core/defined',
- '../Core/defineProperties',
- '../Core/destroyObject',
- '../Core/IndexDatatype',
- '../Core/Math',
- '../Core/Matrix4',
- '../Core/PixelFormat',
- '../Core/PrimitiveType',
- '../Renderer/BufferUsage',
- '../Renderer/ClearCommand',
- '../Renderer/DrawCommand',
- '../Shaders/SunFS',
- '../Shaders/SunTextureFS',
- '../Shaders/SunVS',
- './BlendingState',
- './SceneMode',
- './SceneTransforms'
- ], function(
- BoundingRectangle,
- BoundingSphere,
- Cartesian2,
- Cartesian3,
- Cartesian4,
- Color,
- ComponentDatatype,
- defined,
- defineProperties,
- destroyObject,
- IndexDatatype,
- CesiumMath,
- Matrix4,
- PixelFormat,
- PrimitiveType,
- BufferUsage,
- ClearCommand,
- DrawCommand,
- SunFS,
- SunTextureFS,
- SunVS,
- BlendingState,
- SceneMode,
- SceneTransforms) {
- "use strict";
-
- var Sun = function() {
-
- this.show = true;
- this._command = new DrawCommand({
- primitiveType : PrimitiveType.TRIANGLES,
- boundingVolume : new BoundingSphere(),
- owner : this
- });
- this._boundingVolume = new BoundingSphere();
- this._boundingVolume2D = new BoundingSphere();
- this._texture = undefined;
- this._drawingBufferWidth = undefined;
- this._drawingBufferHeight = undefined;
- this._radiusTS = undefined;
- this._size = undefined;
- this.glowFactor = 1.0;
- this._glowFactorDirty = false;
- var that = this;
- this._uniformMap = {
- u_texture : function() {
- return that._texture;
- },
- u_size : function() {
- return that._size;
- }
- };
- };
- defineProperties(Sun.prototype, {
-
- glowFactor : {
- get : function () { return this._glowFactor; },
- set : function (glowFactor) {
- glowFactor = Math.max(glowFactor, 0.0);
- this._glowFactor = glowFactor;
- this._glowFactorDirty = true;
- }
- }
- });
- var scratchPositionWC = new Cartesian2();
- var scratchLimbWC = new Cartesian2();
- var scratchPositionEC = new Cartesian4();
- var scratchCartesian4 = new Cartesian4();
-
- Sun.prototype.update = function(scene) {
- var frameState = scene.frameState;
- var context = scene.context;
- if (!this.show) {
- return undefined;
- }
- var mode = frameState.mode;
- if (mode === SceneMode.SCENE2D || mode === SceneMode.MORPHING) {
- return undefined;
- }
- if (!frameState.passes.render) {
- return undefined;
- }
- var drawingBufferWidth = scene.drawingBufferWidth;
- var drawingBufferHeight = scene.drawingBufferHeight;
- if (!defined(this._texture) ||
- drawingBufferWidth !== this._drawingBufferWidth ||
- drawingBufferHeight !== this._drawingBufferHeight ||
- this._glowFactorDirty) {
- this._texture = this._texture && this._texture.destroy();
- this._drawingBufferWidth = drawingBufferWidth;
- this._drawingBufferHeight = drawingBufferHeight;
- this._glowFactorDirty = false;
- var size = Math.max(drawingBufferWidth, drawingBufferHeight);
- size = Math.pow(2.0, Math.ceil(Math.log(size) / Math.log(2.0)) - 2.0);
- this._texture = context.createTexture2D({
- width : size,
- height : size,
- pixelFormat : PixelFormat.RGBA
- });
- var fbo = context.createFramebuffer({
- colorTextures : [this._texture]
- });
- fbo.destroyAttachments = false;
- var clearCommand = new ClearCommand({
- color : new Color(0.0, 0.0, 0.0, 0.0),
- framebuffer : fbo
- });
- var rs = context.createRenderState({
- viewport : new BoundingRectangle(0.0, 0.0, size, size)
- });
- this._glowLengthTS = this._glowFactor * 5.0;
- this._radiusTS = (1.0 / (1.0 + 2.0 * this._glowLengthTS)) * 0.5;
- var that = this;
- var uniformMap = {
- u_glowLengthTS : function() {
- return that._glowLengthTS;
- },
- u_radiusTS : function() {
- return that._radiusTS;
- }
- };
- var drawCommand = context.createViewportQuadCommand(SunTextureFS, {
- renderState : rs,
- uniformMap : uniformMap,
- framebuffer : fbo,
- owner : this
- });
- clearCommand.execute(context);
- drawCommand.execute(context);
- drawCommand.shaderProgram.destroy();
- fbo.destroy();
- }
- var command = this._command;
- if (!defined(command.vertexArray)) {
- var attributeLocations = {
- direction : 0
- };
- var directions = new Uint8Array(4 * 2);
- directions[0] = 0;
- directions[1] = 0;
- directions[2] = 255;
- directions[3] = 0.0;
- directions[4] = 255;
- directions[5] = 255;
- directions[6] = 0.0;
- directions[7] = 255;
- var vertexBuffer = context.createVertexBuffer(directions, BufferUsage.STATIC_DRAW);
- var attributes = [{
- index : attributeLocations.direction,
- vertexBuffer : vertexBuffer,
- componentsPerAttribute : 2,
- normalize : true,
- componentDatatype : ComponentDatatype.UNSIGNED_BYTE
- }];
-
- var indexBuffer = context.createIndexBuffer(new Uint16Array([0, 1, 2, 0, 2, 3]), BufferUsage.STATIC_DRAW, IndexDatatype.UNSIGNED_SHORT);
- command.vertexArray = context.createVertexArray(attributes, indexBuffer);
- command.shaderProgram = context.createShaderProgram(SunVS, SunFS, attributeLocations);
- command.renderState = context.createRenderState({
- blending : BlendingState.ALPHA_BLEND
- });
- command.uniformMap = this._uniformMap;
- }
- var sunPosition = context.uniformState.sunPositionWC;
- var sunPositionCV = context.uniformState.sunPositionColumbusView;
- var boundingVolume = this._boundingVolume;
- var boundingVolume2D = this._boundingVolume2D;
- Cartesian3.clone(sunPosition, boundingVolume.center);
- boundingVolume2D.center.x = sunPositionCV.z;
- boundingVolume2D.center.y = sunPositionCV.x;
- boundingVolume2D.center.z = sunPositionCV.y;
- boundingVolume.radius = CesiumMath.SOLAR_RADIUS + CesiumMath.SOLAR_RADIUS * this._glowLengthTS;
- boundingVolume2D.radius = boundingVolume.radius;
- if (mode === SceneMode.SCENE3D) {
- BoundingSphere.clone(boundingVolume, command.boundingVolume);
- } else if (mode === SceneMode.COLUMBUS_VIEW) {
- BoundingSphere.clone(boundingVolume2D, command.boundingVolume);
- }
- var position = SceneTransforms.computeActualWgs84Position(frameState, sunPosition, scratchCartesian4);
- var dist = Cartesian3.magnitude(Cartesian3.subtract(position, scene.camera.position, scratchCartesian4));
- var projMatrix = context.uniformState.projection;
- var positionEC = scratchPositionEC;
- positionEC.x = 0;
- positionEC.y = 0;
- positionEC.z = -dist;
- positionEC.w = 1;
- var positionCC = Matrix4.multiplyByVector(projMatrix, positionEC, scratchCartesian4);
- var positionWC = SceneTransforms.clipToDrawingBufferCoordinates(scene, positionCC, scratchPositionWC);
- positionEC.x = CesiumMath.SOLAR_RADIUS;
- var limbCC = Matrix4.multiplyByVector(projMatrix, positionEC, scratchCartesian4);
- var limbWC = SceneTransforms.clipToDrawingBufferCoordinates(scene, limbCC, scratchLimbWC);
- this._size = Math.ceil(Cartesian2.magnitude(Cartesian2.subtract(limbWC, positionWC, scratchCartesian4)));
- this._size = 2.0 * this._size * (1.0 + 2.0 * this._glowLengthTS);
- return command;
- };
-
- Sun.prototype.isDestroyed = function() {
- return false;
- };
-
- Sun.prototype.destroy = function() {
- var command = this._command;
- command.vertexArray = command.vertexArray && command.vertexArray.destroy();
- command.shaderProgram = command.shaderProgram && command.shaderProgram.destroy();
- this._texture = this._texture && this._texture.destroy();
- return destroyObject(this);
- };
- return Sun;
- });
|