SkyBoxOnGround.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. const BoxGeometry = Cesium.BoxGeometry;
  4. const Cartesian3 = Cesium.Cartesian3;
  5. const defaultValue = Cesium.defaultValue;
  6. const defined = Cesium.defined;
  7. const destroyObject = Cesium.destroyObject;
  8. const DeveloperError = Cesium.DeveloperError;
  9. const GeometryPipeline = Cesium.GeometryPipeline;
  10. const Matrix3 = Cesium.Matrix3;
  11. const Matrix4 = Cesium.Matrix4;
  12. const Transforms = Cesium.Transforms;
  13. const VertexFormat = Cesium.VertexFormat;
  14. const BufferUsage = Cesium.BufferUsage;
  15. const CubeMap = Cesium.CubeMap;
  16. const DrawCommand = Cesium.DrawCommand;
  17. const loadCubeMap = Cesium.loadCubeMap;
  18. const RenderState = Cesium.RenderState;
  19. const VertexArray = Cesium.VertexArray;
  20. const BlendingState = Cesium.BlendingState;
  21. const SceneMode = Cesium.SceneMode;
  22. const ShaderProgram = Cesium.ShaderProgram;
  23. const ShaderSource = Cesium.ShaderSource;
  24. //片元着色器,直接从源码复制
  25. const SkyBoxFS =
  26. "uniform samplerCube u_cubeMap;\n\
  27. varying vec3 v_texCoord;\n\
  28. void main()\n\
  29. {\n\
  30. vec4 color = textureCube(u_cubeMap, normalize(v_texCoord));\n\
  31. gl_FragColor = vec4(czm_gammaCorrect(color).rgb, czm_morphTime);\n\
  32. }\n\
  33. ";
  34. //顶点着色器有修改,主要是乘了一个旋转矩阵
  35. const SkyBoxVS =
  36. "attribute vec3 position;\n\
  37. varying vec3 v_texCoord;\n\
  38. uniform mat3 u_rotateMatrix;\n\
  39. void main()\n\
  40. {\n\
  41. vec3 p = czm_viewRotation * u_rotateMatrix * (czm_temeToPseudoFixed * (czm_entireFrustum.y * position));\n\
  42. gl_Position = czm_projection * vec4(p, 1.0);\n\
  43. v_texCoord = position.xyz;\n\
  44. }\n\
  45. ";
  46. /**
  47. * 为了兼容高版本的Cesium,因为新版cesium中getRotation被移除
  48. */
  49. if (!Cesium.defined(Cesium.Matrix4.getRotation)) {
  50. Cesium.Matrix4.getRotation = Cesium.Matrix4.getMatrix3;
  51. }
  52. /**
  53. * 近景天空盒
  54. * @ignore 忽略注释,注释不生成Doc
  55. */
  56. class SkyBoxOnGround {
  57. /**
  58. * 近景天空盒 初始化
  59. * @ignore
  60. */
  61. constructor(options) {
  62. /**
  63. * 近景天空盒
  64. * @type Object
  65. * @default undefined
  66. */
  67. this.sources = options.sources;
  68. this._sources = undefined;
  69. /**
  70. * Determines if the sky box will be shown.
  71. * @ignore 忽略注释,注释不生成Doc
  72. * @type {Boolean}
  73. * @default true
  74. */
  75. this.show = defaultValue(options.show, true);
  76. this._command = new DrawCommand({
  77. modelMatrix: Matrix4.clone(Matrix4.IDENTITY),
  78. owner: this
  79. });
  80. this._cubeMap = undefined;
  81. this._attributeLocations = undefined;
  82. this._useHdr = undefined;
  83. }
  84. }
  85. const skyboxMatrix3 = new Matrix3();
  86. SkyBoxOnGround.prototype.update = function(frameState, useHdr) {
  87. const that = this;
  88. if (!this.show) {
  89. return undefined;
  90. }
  91. if (
  92. frameState.mode !== SceneMode.SCENE3D &&
  93. frameState.mode !== SceneMode.MORPHING
  94. ) {
  95. return undefined;
  96. }
  97. if (!frameState.passes.render) {
  98. return undefined;
  99. }
  100. const context = frameState.context;
  101. if (this._sources !== this.sources) {
  102. this._sources = this.sources;
  103. const sources = this.sources;
  104. if (
  105. !defined(sources.positiveX) ||
  106. !defined(sources.negativeX) ||
  107. !defined(sources.positiveY) ||
  108. !defined(sources.negativeY) ||
  109. !defined(sources.positiveZ) ||
  110. !defined(sources.negativeZ)
  111. ) {
  112. throw new DeveloperError(
  113. "this.sources is required and must have positiveX, negativeX, positiveY, negativeY, positiveZ, and negativeZ properties."
  114. );
  115. }
  116. if (
  117. typeof sources.positiveX !== typeof sources.negativeX ||
  118. typeof sources.positiveX !== typeof sources.positiveY ||
  119. typeof sources.positiveX !== typeof sources.negativeY ||
  120. typeof sources.positiveX !== typeof sources.positiveZ ||
  121. typeof sources.positiveX !== typeof sources.negativeZ
  122. ) {
  123. throw new DeveloperError(
  124. "this.sources properties must all be the same type."
  125. );
  126. }
  127. if (typeof sources.positiveX === "string") {
  128. // Given urls for cube-map images. Load them.
  129. loadCubeMap(context, this._sources).then(function(cubeMap) {
  130. that._cubeMap = that._cubeMap && that._cubeMap.destroy();
  131. that._cubeMap = cubeMap;
  132. });
  133. } else {
  134. this._cubeMap = this._cubeMap && this._cubeMap.destroy();
  135. this._cubeMap = new CubeMap({
  136. context: context,
  137. source: sources
  138. });
  139. }
  140. }
  141. const command = this._command;
  142. command.modelMatrix = Transforms.eastNorthUpToFixedFrame(
  143. frameState.camera._positionWC
  144. );
  145. if (!defined(command.vertexArray)) {
  146. command.uniformMap = {
  147. u_cubeMap: function() {
  148. return that._cubeMap;
  149. },
  150. u_rotateMatrix: function() {
  151. return Matrix4.getRotation(command.modelMatrix, skyboxMatrix3);
  152. }
  153. };
  154. const geometry = BoxGeometry.createGeometry(
  155. BoxGeometry.fromDimensions({
  156. dimensions: new Cartesian3(2.0, 2.0, 2.0),
  157. vertexFormat: VertexFormat.POSITION_ONLY
  158. })
  159. );
  160. const attributeLocations = (this._attributeLocations = GeometryPipeline.createAttributeLocations(
  161. geometry
  162. ));
  163. command.vertexArray = VertexArray.fromGeometry({
  164. context: context,
  165. geometry: geometry,
  166. attributeLocations: attributeLocations,
  167. bufferUsage: BufferUsage._DRAW
  168. });
  169. command.renderState = RenderState.fromCache({
  170. blending: BlendingState.ALPHA_BLEND
  171. });
  172. }
  173. if (!defined(command.shaderProgram) || this._useHdr !== useHdr) {
  174. const fs = new ShaderSource({
  175. defines: [useHdr ? "HDR" : ""],
  176. sources: [SkyBoxFS]
  177. });
  178. command.shaderProgram = ShaderProgram.fromCache({
  179. context: context,
  180. vertexShaderSource: SkyBoxVS,
  181. fragmentShaderSource: fs,
  182. attributeLocations: this._attributeLocations
  183. });
  184. this._useHdr = useHdr;
  185. }
  186. if (!defined(this._cubeMap)) {
  187. return undefined;
  188. }
  189. return command;
  190. };
  191. SkyBoxOnGround.prototype.isDestroyed = function() {
  192. return false;
  193. };
  194. SkyBoxOnGround.prototype.destroy = function() {
  195. const command = this._command;
  196. command.vertexArray = command.vertexArray && command.vertexArray.destroy();
  197. command.shaderProgram =
  198. command.shaderProgram && command.shaderProgram.destroy();
  199. this._cubeMap = this._cubeMap && this._cubeMap.destroy();
  200. return destroyObject(this);
  201. };
  202. export default SkyBoxOnGround;