zoukankan      html  css  js  c++  java
  • Cesium从入门到放弃9:近景天空盒(不改源码)[转]

    https://blog.csdn.net/xtfge0915/article/details/105371096#comments_13511194

    先上效果图

     Cesium近景默认的天空盒只有相机距离较远时才能看到,在很多小场景下(比如智慧城市等)默认的天空盒可能无法满足用户的需求,所以近景天空盒的需要就诞生了,如果想要不改源码实现Cesium近景天空盒就需要重写SkyBox类,下面贴出全部代码。

    //以下代码复制自Cesium源码的SkyBox,然后做了一点点修改。
    //SkyBoxOnGround.js
    (function () {
    const Cesium = window.Cesium;
    const BoxGeometry = Cesium.BoxGeometry;
    const Cartesian3 = Cesium.Cartesian3;
    const defaultValue = Cesium.defaultValue;
    const defined = Cesium.defined;
    const destroyObject = Cesium.destroyObject;
    const DeveloperError = Cesium.DeveloperError;
    const GeometryPipeline = Cesium.GeometryPipeline;
    const Matrix3 = Cesium.Matrix3;
    const Matrix4 = Cesium.Matrix4;
    const Transforms = Cesium.Transforms;
    const VertexFormat = Cesium.VertexFormat;
    const BufferUsage = Cesium.BufferUsage;
    const CubeMap = Cesium.CubeMap;
    const DrawCommand = Cesium.DrawCommand;
    const loadCubeMap = Cesium.loadCubeMap;
    const RenderState = Cesium.RenderState;
    const VertexArray = Cesium.VertexArray;
    const BlendingState = Cesium.BlendingState;
    const SceneMode = Cesium.SceneMode;
    const ShaderProgram = Cesium.ShaderProgram;
    const ShaderSource = Cesium.ShaderSource;
    //片元着色器,直接从源码复制
    const SkyBoxFS = "uniform samplerCube u_cubeMap;
    varying vec3 v_texCoord;
    void main()
    {
    vec4 color = textureCube(u_cubeMap, normalize(v_texCoord));
    gl_FragColor = vec4(czm_gammaCorrect(color).rgb, czm_morphTime);
    }
    ";

    //顶点着色器有修改,主要是乘了一个旋转矩阵
    const SkyBoxVS = "attribute vec3 position;
    varying vec3 v_texCoord;
    uniform mat3 u_rotateMatrix;
    void main()
    {
    vec3 p = czm_viewRotation * u_rotateMatrix * (czm_temeToPseudoFixed * (czm_entireFrustum.y * position));
    gl_Position = czm_projection * vec4(p, 1.0);
    v_texCoord = position.xyz;
    }
    ";
    /**
    * 为了兼容高版本的Cesium,因为新版cesium中getRotation被移除
    */
    if (!Cesium.defined(Cesium.Matrix4.getRotation)) {
    Cesium.Matrix4.getRotation = Cesium.Matrix4.getMatrix3
    }
    function SkyBoxOnGround(options) {
    /**
    * 近景天空盒
    * @type Object
    * @default undefined
    */
    this.sources = options.sources;
    this._sources = undefined;

    /**
    * Determines if the sky box will be shown.
    *
    * @type {Boolean}
    * @default true
    */
    this.show = defaultValue(options.show, true);

    this._command = new DrawCommand({
    modelMatrix: Matrix4.clone(Matrix4.IDENTITY),
    owner: this
    });
    this._cubeMap = undefined;

    this._attributeLocations = undefined;
    this._useHdr = undefined;
    }

    const skyboxMatrix3 = new Matrix3();
    SkyBoxOnGround.prototype.update = function (frameState, useHdr) {
    const that = this;

    if (!this.show) {
    return undefined;
    }

    if ((frameState.mode !== SceneMode.SCENE3D) &&
    (frameState.mode !== SceneMode.MORPHING)) {
    return undefined;
    }

    if (!frameState.passes.render) {
    return undefined;
    }

    const context = frameState.context;

    if (this._sources !== this.sources) {
    this._sources = this.sources;
    const 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') {
    // Given urls for cube-map images. Load them.
    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 = new CubeMap({
    context: context,
    source: sources
    });
    }
    }

    const command = this._command;

    command.modelMatrix = Transforms.eastNorthUpToFixedFrame(frameState.camera._positionWC);
    if (!defined(command.vertexArray)) {
    command.uniformMap = {
    u_cubeMap: function () {
    return that._cubeMap;
    },
    u_rotateMatrix: function () {
    return Matrix4.getRotation(command.modelMatrix, skyboxMatrix3);
    },
    };

    const geometry = BoxGeometry.createGeometry(BoxGeometry.fromDimensions({
    dimensions: new Cartesian3(2.0, 2.0, 2.0),
    vertexFormat: VertexFormat.POSITION_ONLY
    }));
    const attributeLocations = this._attributeLocations = GeometryPipeline.createAttributeLocations(geometry);

    command.vertexArray = VertexArray.fromGeometry({
    context: context,
    geometry: geometry,
    attributeLocations: attributeLocations,
    bufferUsage: BufferUsage._DRAW
    });

    command.renderState = RenderState.fromCache({
    blending: BlendingState.ALPHA_BLEND
    });
    }

    if (!defined(command.shaderProgram) || this._useHdr !== useHdr) {
    const fs = new ShaderSource({
    defines: [useHdr ? 'HDR' : ''],
    sources: [SkyBoxFS]
    });
    command.shaderProgram = ShaderProgram.fromCache({
    context: context,
    vertexShaderSource: SkyBoxVS,
    fragmentShaderSource: fs,
    attributeLocations: this._attributeLocations
    });
    this._useHdr = useHdr;
    }

    if (!defined(this._cubeMap)) {
    return undefined;
    }

    return command;
    };
    SkyBoxOnGround.prototype.isDestroyed = function () {
    return false
    };
    SkyBoxOnGround.prototype.destroy = function () {
    const 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);
    }
    window.Cesium.GroundSkyBox= SkyBoxOnGround
    })();

    使用方法

    //你自已的地址,在main.js中引入
    import './js/SkyBoxOnGround'
    viewer.scene.skyBox = new Cesium.GroundSkyBox({
    sources: {
    positiveX: require("./images/黄昏/px.png"),
    negativeX: require("./images/黄昏/nx.png"),
    positiveY: require("./images/黄昏/py.png"),
    negativeY: require("./images/黄昏/ny.png"),
    positiveZ: require("./images/黄昏/pz.png"),
    negativeZ: require("./images/黄昏/nz.png")
    });

  • 相关阅读:
    hdu5628 Clarke and math
    LOJ#2452. 「POI2010」反对称 Antisymmetry
    LOJ#2444. 「NOI2011」阿狸的打字机
    BZOJ2795: [Poi2012]A Horrible Poem
    LOJ#2427. 「POI2010」珍珠项链 Beads
    云主机文件系统readonly处理案例
    兼容性测试中如何切换和管理多个JDK版本
    Promise之你看得懂的Promise
    一次MySQL线上慢查询分析及索引使用
    考拉消息中心消息盒子处理重构(策略模式)
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/13856358.html
Copyright © 2011-2022 走看看