Cesium近景天空

nx.png
ny.png
nz.png
px.png
py.png
pz.png
//片元著色器,直接從源碼復(fù)制
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);
    }
`;
 
//頂點(diǎn)著色器有修改瑞你,主要是乘了一個(gè)旋轉(zhuǎn)矩陣
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;
    }
`;
 
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 skyboxMatrix3 = new Matrix3();
 
 
/**
* 為了兼容高版本的Cesium绰播,因?yàn)樾掳鎐esium中g(shù)etRotation被移除
*/
if (!Cesium.defined(Cesium.Matrix4.getRotation)) {
    Cesium.Matrix4.getRotation = Cesium.Matrix4.getMatrix3
}
 
/**
* 近景天空盒
* @type Object
* @default undefined
*/
class SkyBoxOnGround {
    constructor(options){
        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;
    }
 
    update(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') {
                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;
    }
 
    setSkyBox(viewer){
 
        // 自帶的默認(rèn)天空盒
        let defaultSkybox = viewer.scene.skyBox;
 
        // 渲染前監(jiān)聽并判斷相機(jī)位置
        viewer.scene.preUpdate.addEventListener(() => {
            let position = viewer.scene.camera.position;
            let cameraHeight = Cesium.Cartographic.fromCartesian(position).height;
            if (cameraHeight < 240000) {
                viewer.scene.skyBox = this;
                viewer.scene.skyAtmosphere.show = false;
            } else {
                viewer.scene.skyBox = defaultSkybox;
                viewer.scene.skyAtmosphere.show = true;
            }
        })
    }
 
    isDestroyed(){
        return false
    }
 
    destroy(){
        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);
    }
}
 

調(diào)用

var viewer = new Cesium.Viewer("cesiumContainer");

    var groundSkybox = new SkyBoxOnGround({
      sources: {
        positiveX: "./skybox/px.png",
        negativeX: "./skybox/nx.png",
        positiveY: "./skybox/pz.png",
        negativeY: "./skybox/nz.png",
        positiveZ: "./skybox/py.png",
        negativeZ: "./skybox/ny.png",
      },
    });
    groundSkybox.setSkyBox(viewer);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末诱渤,一起剝皮案震驚了整個(gè)濱河市筹麸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件悼凑,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡璧瞬,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門渐夸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)嗤锉,“玉大人,你說(shuō)我怎么就攤上這事墓塌∥脸溃” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵苫幢,是天一觀的道長(zhǎng)访诱。 經(jīng)常有香客問(wèn)我,道長(zhǎng)韩肝,這世上最難降的妖魔是什么触菜? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮哀峻,結(jié)果婚禮上涡相,老公的妹妹穿的比我還像新娘。我一直安慰自己剩蟀,他們只是感情好催蝗,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著育特,像睡著了一般丙号。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上缰冤,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天犬缨,我揣著相機(jī)與錄音,去河邊找鬼锋谐。 笑死遍尺,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的涮拗。 我是一名探鬼主播乾戏,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼迂苛,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了鼓择?” 一聲冷哼從身側(cè)響起三幻,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎呐能,沒(méi)想到半個(gè)月后念搬,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡摆出,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年朗徊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片偎漫。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡爷恳,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出象踊,到底是詐尸還是另有隱情温亲,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布杯矩,位于F島的核電站栈虚,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏史隆。R本人自食惡果不足惜魂务,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望逆害。 院中可真熱鬧头镊,春花似錦、人聲如沸魄幕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)纯陨。三九已至坛芽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間翼抠,已是汗流浹背咙轩。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留阴颖,地道東北人活喊。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像量愧,于是被迫代替她去往敵國(guó)和親钾菊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子帅矗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容