123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- define([
- '../Core/BoundingSphere',
- '../Core/BoxGeometry',
- '../Core/Cartesian3',
- '../Core/combine',
- '../Core/defaultValue',
- '../Core/defined',
- '../Core/destroyObject',
- '../Core/DeveloperError',
- '../Core/Matrix4',
- '../Core/VertexFormat',
- '../Renderer/BufferUsage',
- '../Renderer/DrawCommand',
- '../Renderer/ShaderSource',
- '../Shaders/EllipsoidFS',
- '../Shaders/EllipsoidVS',
- './BlendingState',
- './CullFace',
- './Material',
- './Pass',
- './SceneMode'
- ], function(
- BoundingSphere,
- BoxGeometry,
- Cartesian3,
- combine,
- defaultValue,
- defined,
- destroyObject,
- DeveloperError,
- Matrix4,
- VertexFormat,
- BufferUsage,
- DrawCommand,
- ShaderSource,
- EllipsoidFS,
- EllipsoidVS,
- BlendingState,
- CullFace,
- Material,
- Pass,
- SceneMode) {
- "use strict";
- var attributeLocations = {
- position : 0
- };
-
- var EllipsoidPrimitive = function(options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
-
- this.center = Cartesian3.clone(defaultValue(options.center, Cartesian3.ZERO));
- this._center = new Cartesian3();
-
- this.radii = Cartesian3.clone(options.radii);
- this._radii = new Cartesian3();
- this._oneOverEllipsoidRadiiSquared = new Cartesian3();
- this._boundingSphere = new BoundingSphere();
-
- this.modelMatrix = Matrix4.clone(defaultValue(options.modelMatrix, Matrix4.IDENTITY));
- this._modelMatrix = new Matrix4();
- this._computedModelMatrix = new Matrix4();
-
- this.show = defaultValue(options.show, true);
-
- this.material = defaultValue(options.material, Material.fromType(Material.ColorType));
- this._material = undefined;
- this._translucent = undefined;
-
- this.id = options.id;
- this._id = undefined;
-
- this.debugShowBoundingVolume = defaultValue(options.debugShowBoundingVolume, false);
-
- this.onlySunLighting = defaultValue(options.onlySunLighting, false);
- this._onlySunLighting = false;
- this._sp = undefined;
- this._rs = undefined;
- this._va = undefined;
- this._pickSP = undefined;
- this._pickId = undefined;
- this._colorCommand = new DrawCommand({
- owner : defaultValue(options._owner, this)
- });
- this._pickCommand = new DrawCommand({
- owner : defaultValue(options._owner, this)
- });
- var that = this;
- this._uniforms = {
- u_radii : function() {
- return that.radii;
- },
- u_oneOverEllipsoidRadiiSquared : function() {
- return that._oneOverEllipsoidRadiiSquared;
- }
- };
- this._pickUniforms = {
- czm_pickColor : function() {
- return that._pickId.color;
- }
- };
- };
- function getVertexArray(context) {
- var vertexArray = context.cache.ellipsoidPrimitive_vertexArray;
- if (defined(vertexArray)) {
- return vertexArray;
- }
- var geometry = BoxGeometry.createGeometry(BoxGeometry.fromDimensions({
- dimensions : new Cartesian3(2.0, 2.0, 2.0),
- vertexFormat : VertexFormat.POSITION_ONLY
- }));
- vertexArray = context.createVertexArrayFromGeometry({
- geometry : geometry,
- attributeLocations : attributeLocations,
- bufferUsage : BufferUsage.STATIC_DRAW,
- interleave : true
- });
- context.cache.ellipsoidPrimitive_vertexArray = vertexArray;
- return vertexArray;
- }
-
- EllipsoidPrimitive.prototype.update = function(context, frameState, commandList) {
- if (!this.show ||
- (frameState.mode !== SceneMode.SCENE3D) ||
- (!defined(this.center)) ||
- (!defined(this.radii))) {
- return;
- }
-
- if (!defined(this.material)) {
- throw new DeveloperError('this.material must be defined.');
- }
-
- var translucent = this.material.isTranslucent();
- var translucencyChanged = this._translucent !== translucent;
- if (!defined(this._rs) || translucencyChanged) {
- this._translucent = translucent;
-
-
-
- this._rs = context.createRenderState({
-
-
- cull : {
- enabled : true,
- face : CullFace.FRONT
- },
- depthTest : {
- enabled : true
- },
-
-
-
- depthMask : !translucent && context.fragmentDepth,
- blending : translucent ? BlendingState.ALPHA_BLEND : undefined
- });
- }
- if (!defined(this._va)) {
- this._va = getVertexArray(context);
- }
- var boundingSphereDirty = false;
- var radii = this.radii;
- if (!Cartesian3.equals(this._radii, radii)) {
- Cartesian3.clone(radii, this._radii);
- var r = this._oneOverEllipsoidRadiiSquared;
- r.x = 1.0 / (radii.x * radii.x);
- r.y = 1.0 / (radii.y * radii.y);
- r.z = 1.0 / (radii.z * radii.z);
- boundingSphereDirty = true;
- }
- if (!Matrix4.equals(this.modelMatrix, this._modelMatrix) || !Cartesian3.equals(this.center, this._center)) {
- Matrix4.clone(this.modelMatrix, this._modelMatrix);
- Cartesian3.clone(this.center, this._center);
-
- Matrix4.multiplyByTranslation(this.modelMatrix, this.center, this._computedModelMatrix);
- boundingSphereDirty = true;
- }
- if (boundingSphereDirty) {
- Cartesian3.clone(Cartesian3.ZERO, this._boundingSphere.center);
- this._boundingSphere.radius = Cartesian3.maximumComponent(radii);
- BoundingSphere.transform(this._boundingSphere, this._computedModelMatrix, this._boundingSphere);
- }
- var materialChanged = this._material !== this.material;
- this._material = this.material;
- this._material.update(context);
- var lightingChanged = this.onlySunLighting !== this._onlySunLighting;
- this._onlySunLighting = this.onlySunLighting;
- var colorCommand = this._colorCommand;
- var vs;
- var fs;
-
- if (materialChanged || lightingChanged || translucencyChanged) {
- fs = new ShaderSource({
- sources : [this.material.shaderSource, EllipsoidFS]
- });
- if (this.onlySunLighting) {
- fs.defines.push('ONLY_SUN_LIGHTING');
- }
- if (!translucent && context.fragmentDepth) {
- fs.defines.push('WRITE_DEPTH');
- }
- this._sp = context.replaceShaderProgram(this._sp, EllipsoidVS, fs, attributeLocations);
- colorCommand.vertexArray = this._va;
- colorCommand.renderState = this._rs;
- colorCommand.shaderProgram = this._sp;
- colorCommand.uniformMap = combine(this._uniforms, this.material._uniforms);
- colorCommand.executeInClosestFrustum = translucent;
- }
- var passes = frameState.passes;
- if (passes.render) {
- colorCommand.boundingVolume = this._boundingSphere;
- colorCommand.debugShowBoundingVolume = this.debugShowBoundingVolume;
- colorCommand.modelMatrix = this._computedModelMatrix;
- colorCommand.pass = translucent ? Pass.TRANSLUCENT : Pass.OPAQUE;
- commandList.push(colorCommand);
- }
- if (passes.pick) {
- var pickCommand = this._pickCommand;
- if (!defined(this._pickId) || (this._id !== this.id)) {
- this._id = this.id;
- this._pickId = this._pickId && this._pickId.destroy();
- this._pickId = context.createPickId({
- primitive : this,
- id : this.id
- });
- }
-
- if (materialChanged || lightingChanged || !defined(this._pickSP)) {
- fs = new ShaderSource({
- sources : [this.material.shaderSource, EllipsoidFS],
- pickColorQualifier : 'uniform'
- });
- if (this.onlySunLighting) {
- fs.defines.push('ONLY_SUN_LIGHTING');
- }
- if (!translucent && context.fragmentDepth) {
- fs.defines.push('WRITE_DEPTH');
- }
- this._pickSP = context.replaceShaderProgram(this._pickSP, EllipsoidVS, fs, attributeLocations);
- pickCommand.vertexArray = this._va;
- pickCommand.renderState = this._rs;
- pickCommand.shaderProgram = this._pickSP;
- pickCommand.uniformMap = combine(combine(this._uniforms, this._pickUniforms), this.material._uniforms);
- pickCommand.executeInClosestFrustum = translucent;
- }
- pickCommand.boundingVolume = this._boundingSphere;
- pickCommand.modelMatrix = this._computedModelMatrix;
- pickCommand.pass = translucent ? Pass.TRANSLUCENT : Pass.OPAQUE;
- commandList.push(pickCommand);
- }
- };
-
- EllipsoidPrimitive.prototype.isDestroyed = function() {
- return false;
- };
-
- EllipsoidPrimitive.prototype.destroy = function() {
- this._sp = this._sp && this._sp.destroy();
- this._pickSP = this._pickSP && this._pickSP.destroy();
- this._pickId = this._pickId && this._pickId.destroy();
- return destroyObject(this);
- };
- return EllipsoidPrimitive;
- });
|