123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- define([
- '../Core/BoxGeometry',
- '../Core/Cartesian3',
- '../Core/defaultValue',
- '../Core/defined',
- '../Core/destroyObject',
- '../Core/DeveloperError',
- '../Core/GeometryPipeline',
- '../Core/Matrix4',
- '../Core/VertexFormat',
- '../Renderer/BufferUsage',
- '../Renderer/DrawCommand',
- '../Renderer/loadCubeMap',
- '../Shaders/SkyBoxFS',
- '../Shaders/SkyBoxVS',
- './BlendingState',
- './SceneMode'
- ], function(
- BoxGeometry,
- Cartesian3,
- defaultValue,
- defined,
- destroyObject,
- DeveloperError,
- GeometryPipeline,
- Matrix4,
- VertexFormat,
- BufferUsage,
- DrawCommand,
- loadCubeMap,
- SkyBoxFS,
- SkyBoxVS,
- BlendingState,
- SceneMode) {
- "use strict";
-
- var SkyBox = function(options) {
-
- this.sources = options.sources;
- this._sources = undefined;
-
- this.show = defaultValue(options.show, true);
- this._command = new DrawCommand({
- modelMatrix : Matrix4.clone(Matrix4.IDENTITY),
- owner : this
- });
- this._cubeMap = undefined;
- };
-
- SkyBox.prototype.update = function(context, frameState) {
- if (!this.show) {
- return undefined;
- }
- if ((frameState.mode !== SceneMode.SCENE3D) &&
- (frameState.mode !== SceneMode.MORPHING)) {
- return undefined;
- }
-
- if (!frameState.passes.render) {
- return undefined;
- }
- if (this._sources !== this.sources) {
- this._sources = this.sources;
- var sources = this.sources;
-
- if ((!defined(sources.positiveX)) ||
- (!defined(sources.negativeX)) ||
- (!defined(sources.positiveY)) ||
- (!defined(sources.negativeY)) ||
- (!defined(sources.positiveZ)) ||
- (!defined(sources.negativeZ))) {
- throw new DeveloperError('this.sources is required and must have positiveX, negativeX, positiveY, negativeY, positiveZ, and negativeZ properties.');
- }
- if ((typeof sources.positiveX !== typeof sources.negativeX) ||
- (typeof sources.positiveX !== typeof sources.positiveY) ||
- (typeof sources.positiveX !== typeof sources.negativeY) ||
- (typeof sources.positiveX !== typeof sources.positiveZ) ||
- (typeof sources.positiveX !== typeof sources.negativeZ)) {
- throw new DeveloperError('this.sources properties must all be the same type.');
- }
-
- if (typeof sources.positiveX === 'string') {
-
- loadCubeMap(context, this._sources).then(function(cubeMap) {
- that._cubeMap = that._cubeMap && that._cubeMap.destroy();
- that._cubeMap = cubeMap;
- });
- } else {
- this._cubeMap = this._cubeMap && this._cubeMap.destroy();
- this._cubeMap = context.createCubeMap({
- source : sources
- });
- }
- }
- var command = this._command;
- if (!defined(command.vertexArray)) {
- var that = this;
- command.uniformMap = {
- u_cubeMap: function() {
- return that._cubeMap;
- }
- };
- var geometry = BoxGeometry.createGeometry(BoxGeometry.fromDimensions({
- dimensions : new Cartesian3(2.0, 2.0, 2.0),
- vertexFormat : VertexFormat.POSITION_ONLY
- }));
- var attributeLocations = GeometryPipeline.createAttributeLocations(geometry);
- command.vertexArray = context.createVertexArrayFromGeometry({
- geometry: geometry,
- attributeLocations: attributeLocations,
- bufferUsage: BufferUsage.STATIC_DRAW
- });
- command.shaderProgram = context.createShaderProgram(SkyBoxVS, SkyBoxFS, attributeLocations);
- command.renderState = context.createRenderState({
- blending : BlendingState.ALPHA_BLEND
- });
- }
- if (!defined(this._cubeMap)) {
- return undefined;
- }
- return command;
- };
-
- SkyBox.prototype.isDestroyed = function() {
- return false;
- };
-
- SkyBox.prototype.destroy = function() {
- var command = this._command;
- command.vertexArray = command.vertexArray && command.vertexArray.destroy();
- command.shaderProgram = command.shaderProgram && command.shaderProgram.destroy();
- this._cubeMap = this._cubeMap && this._cubeMap.destroy();
- return destroyObject(this);
- };
- return SkyBox;
- });
|