Cesium快速上手9-Camera和Scene中的其他函數使用

Camera

 viewer.camera 是快捷方式,訪問的是 viewer.scene.camera
camera.move... 相機平移
camera.look.. 相機旋轉

示例
http://localhost:8080/Apps/Sandcastle/index.html?src=Camera%20Tutorial.html&label=All

image.png

// disable the default event handlers  首先禁用原有默認操控方式
scene.screenSpaceCameraController.enableRotate = false;
scene.screenSpaceCameraController.enableTranslate = false;
scene.screenSpaceCameraController.enableZoom = false;
scene.screenSpaceCameraController.enableTilt = false;
scene.screenSpaceCameraController.enableLook = false;


。。。
//相機事件

viewer.clock.onTick.addEventListener(function(clock) {
    var camera = viewer.camera;

    if (flags.looking) {
        var width = canvas.clientWidth;
        var height = canvas.clientHeight;

        // Coordinate (0.0, 0.0) will be where the mouse was clicked.
        var x = (mousePosition.x - startMousePosition.x) / width;
        var y = -(mousePosition.y - startMousePosition.y) / height;

        var lookFactor = 0.05;
        camera.lookRight(x * lookFactor);
        camera.lookUp(y * lookFactor);
    }

    // Change movement speed based on the distance of the camera to the surface of the ellipsoid.
    var cameraHeight = ellipsoid.cartesianToCartographic(camera.position).height;
    var moveRate = cameraHeight / 100.0;

    if (flags.moveForward) {
        camera.moveForward(moveRate);
    }
    if (flags.moveBackward) {
        camera.moveBackward(moveRate);
    }
    if (flags.moveUp) {
        camera.moveUp(moveRate);
    }
    if (flags.moveDown) {
        camera.moveDown(moveRate);
    }
    if (flags.moveLeft) {
        camera.moveLeft(moveRate);
    }
    if (flags.moveRight) {
        camera.moveRight(moveRate);
    }
});

http://localhost:8080/Apps/Sandcastle/index.html?src=Camera.html&label=All

image.png

destination 目標點, 可以是一個點,也可是是一個區域
orientation 相機的目標點角度,默認北向
duration:
flyOverLongitude: 中途經過角度

// 監測相機移動事件
function cameraChanges() {
    Sandcastle.declare(cameraChanges);

    var i = 0;
    removeChanged = viewer.camera.changed.addEventListener(function(percentage) {
        ++i;
        cameraChanged.innerText = 'Camera Changed: ' + i + ', ' + percentage.toFixed(6);
        cameraChanged.style.display = 'block';
    });
}
function flyToHeadingPitchRoll() {
    Sandcastle.declare(flyToHeadingPitchRoll);
    viewer.camera.flyTo({
        destination : Cesium.Cartesian3.fromDegrees(-122.22, 46.12, 5000.0),
        orientation : {
            heading : Cesium.Math.toRadians(20.0),// 朝北為0,20度表示朝東20度
            pitch : Cesium.Math.toRadians(-35.0), //
            roll : 0.0
        }
    });
}
function flyToRectangle() {
    Sandcastle.declare(flyToRectangle);

    var west = -90.0;
    var south = 38.0;
    var east = -87.0;
    var north = 40.0;
    var rectangle = Cesium.Rectangle.fromDegrees(west, south, east, north);

    viewer.camera.flyTo({
        destination : rectangle
    });

    // Show the rectangle.  Not required; just for show.
    viewer.entities.add({
        rectangle : {
            coordinates : rectangle,
            fill : false,
            outline : true,
            outlineColor : Cesium.Color.WHITE
        }
    });
}

function setHeadingPitchRoll() {
    Sandcastle.declare(setHeadingPitchRoll);

    var camera = viewer.camera;
    camera.setView({
        destination : Cesium.Cartesian3.fromDegrees(-75.5847, 40.0397, 1000.0),
        orientation: {
            heading : -Cesium.Math.PI_OVER_TWO,
            pitch : -Cesium.Math.PI_OVER_FOUR,
            roll : 0.0
        }
    });
}
function losAngelesToTokyo(adjustPitch) {
    var camera = scene.camera;

    var tokyoOptions = {
        destination : Cesium.Cartesian3.fromDegrees(139.8148, 35.7142, 20000.0),
        orientation: {
            heading : Cesium.Math.toRadians(15.0),
            pitch : Cesium.Math.toRadians(-60),
            roll : 0.0
        },
        duration: 20,
        flyOverLongitude: Cesium.Math.toRadians(60.0) //控制飛行效果,途徑歐洲
    };

    var laOptions = {
        destination : Cesium.Cartesian3.fromDegrees(-117.729, 34.457, 10000.0),
        duration: 5,
        orientation: {
            heading : Cesium.Math.toRadians(-15.0),
            pitch : -Cesium.Math.PI_OVER_FOUR,
            roll : 0.0
        }
    };

    laOptions.complete = function() { //回調函數
        setTimeout(function() {
            camera.flyTo(tokyoOptions);
        }, 1000);
    };

    if (adjustPitch) {//飛起來時調整俯仰角達到垂直朝下看效果,越高度超過1000米,修改俯仰角朝正下
        tokyoOptions.pitchAdjustHeight = 1000;
        laOptions.pitchAdjustHeight = 1000;
    }

    camera.flyTo(laOptions);
}

camera.lookAtTransform 相當于 繞著參考坐標系transform的中心旋轉;
//若沒有自定義參考坐標系,默認的時單位矩陣,中心點在地球的正中心。


image.png
function setReferenceFrame() {
    Sandcastle.declare(setReferenceFrame);

    var center = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
    var transform = Cesium.Transforms.eastNorthUpToFixedFrame(center);//確定transform坐標系

    //lookAtTransform 第一個參數確定參考點 相當于 繞著參考坐標系transform的中心旋轉
   //lookAtTransform 第二個參數是相機的位置
    // View in east-north-up frame
    var camera = viewer.camera;
    camera.constrainedAxis = Cesium.Cartesian3.UNIT_Z;
    camera.lookAtTransform(transform, new Cesium.Cartesian3(-120000.0, -120000.0, 120000.0));

    // Show reference frame.  Not required.
    referenceFramePrimitive = scene.primitives.add(new Cesium.DebugModelMatrixPrimitive({
        modelMatrix : transform,
        length : 100000.0
    }));
}
camera.position 相對于參考坐標系的坐標值Cartesian3,格式xyz
camera.positionWC 相對于地球中心點的坐標Cartesian3,格式  xyz
camera.positionCartographic... 坐標格式Cartographic 經緯度
camera.transform 相機參考坐標系
image.png

image.png

viewInICRF

效果是 相機不動,地球在動;實際上還是改變的相機的Transform,保持camera.position不變

//實現時camera.position始終不變,改變camera.Transform
function icrf(scene, time) {
    if (scene.mode !== Cesium.SceneMode.SCENE3D) {
        return;
    }

    var icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(time);
    if (Cesium.defined(icrfToFixed)) {
        var camera = viewer.camera;
        var offset = Cesium.Cartesian3.clone(camera.position);
        var transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed);
        camera.lookAtTransform(transform, offset);
    }
}


function viewInICRF() {
    Sandcastle.declare(viewInICRF);

    viewer.camera.flyHome(0);

    clock.multiplier = 3 * 60 * 60;
    scene.postUpdate.addEventListener(icrf);
    scene.globe.enableLighting = true;
}

Scene的api文檔查看

https://cesium.com/downloads/cesiumjs/releases/1.57/Build/Documentation/Scene.html?classFilter=Scene

  • 1 調試類函數 debugShowFramesPerSecond/debugShowGlobeDepth/...


    image.png
  • 4 環境對象 sun/moon/skyBox/...


    image.png
  • 5 camera相機

  • 6 后處理相關示例

調試用的函數 debugShowFramesPerSecond debugShowDepthFrustum debugShowGlobeDepth

cartesianToCanvasCoordinates(position, result) 三維場景坐標 轉換到屏幕坐標
clampToHeight
clampToHeightMostDetailed 獲取最精細的高度,精度更準確,操作時間更長;獲取完數據再求交;
pickPosition 在屏幕看到的資源里面求交,不需要再次請求數據;
drillPick 射線求交時具有穿透力,
pick 射線求交時沒有穿透力,

打開源碼的方式二

image.png

image.png
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,967評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,273評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,870評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,742評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,527評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,010評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,108評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,250評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,769評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,656評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,853評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,371評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,103評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,472評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,717評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,487評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,815評論 2 372

推薦閱讀更多精彩內容