Cesium快速上手2-Model圖元使用講解

Model示例講解鏈接地址 ,注意是開(kāi)發(fā)模式的示例
http://localhost:8080/Apps/Sandcastle/index.html?src=development%2F3D%20Models.html&label=Development

image.png

minimumPixelSize : 128 //保證不管地球縮放到多小,這個(gè)飛機(jī)依然能看得見(jiàn)

 model = scene.primitives.add(Cesium.Model.fromGltf({
        url : url,
        modelMatrix : modelMatrix,
        minimumPixelSize : 128 //保證不管地球縮放到多小,這個(gè)飛機(jī)依然能看得見(jiàn)
    }));

createModel具體使用講解

modelMatrix
Cesium.Transforms.headingPitchRollToFixedFrame
Model readyPromise
camera.lookAt

//origin 基于空間直角坐標(biāo)系,已地球的圓心為原點(diǎn)建的坐標(biāo)系,本初子午線為X軸;origin = 需要從經(jīng)緯度轉(zhuǎn)為空間直角坐標(biāo)系的值
//hpr 相對(duì)于飛機(jī)自身的三個(gè)旋轉(zhuǎn)角度,heading左右搖頭的角度,改變航向;Pitch 上下抬頭低頭角度,上正下負(fù);Roll相對(duì)于視線方向,從左到右旋轉(zhuǎn)的角度;
//在地球上的不同位置,飛機(jī)的初始姿態(tài)是不一樣的。需要根據(jù)不同的位置初始化飛機(jī)的位置
//異步創(chuàng)建,
// camera.lookAt 調(diào)整相機(jī)角度,才能看到飛機(jī)的位置;設(shè)置視覺(jué)中心點(diǎn),視覺(jué)角度;鼠標(biāo)坐標(biāo)拖動(dòng)時(shí)可以看到,是圍繞著視覺(jué)中心點(diǎn)進(jìn)行旋轉(zhuǎn)的,若需要解綁的話,需要用另外一個(gè)函數(shù)
//camera.lookAt Transform(Cesium.Matrix4.IDENTITY)可以完成解綁。

function createModel(url, height, heading, pitch, roll) {
    height = Cesium.defaultValue(height, 0.0);
    heading = Cesium.defaultValue(heading, 0.0);
    pitch = Cesium.defaultValue(pitch, 0.0);
    roll = Cesium.defaultValue(roll, 0.0);
    var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);

    var origin = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, height);
    var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(origin, hpr);

    scene.primitives.removeAll(); // Remove previous model
    model = scene.primitives.add(Cesium.Model.fromGltf({
        url : url,
        modelMatrix : modelMatrix,
        minimumPixelSize : 128
    }));

    model.readyPromise.then(function(model) {
        model.color = Cesium.Color.fromAlpha(getColor(viewModel.color), Number(viewModel.alpha));
        model.colorBlendMode = getColorBlendMode(viewModel.colorBlendMode);
        model.colorBlendAmount = viewModel.colorBlendAmount;
        // Play and loop all animations at half-speed
        model.activeAnimations.addAll({
            multiplier : 0.5,
            loop : Cesium.ModelAnimationLoop.REPEAT
        });

        var camera = viewer.camera;

        // Zoom to model
        var controller = scene.screenSpaceCameraController;
        var r = 2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near);
        controller.minimumZoomDistance = r * 0.5;

        var center = Cesium.Matrix4.multiplyByPoint(model.modelMatrix, model.boundingSphere.center, new Cesium.Cartesian3());
        var heading = Cesium.Math.toRadians(230.0);
        var pitch = Cesium.Math.toRadians(-20.0);
        camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, r * 2.0));
    }).otherwise(function(error){
        window.alert(error);
    });
}

注冊(cè)事件 ScreenSpaceEventHandler

scene.pick
scene.pick之后需要判斷的屬性字段根據(jù)你的需求而定,這里我們關(guān)注的是model,所以判斷model的屬性是否存在。
scene.pick之后判斷model的屬性是否存在

var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
    var pick = scene.pick(movement.endPosition);
    if (Cesium.defined(pick) && Cesium.defined(pick.node) && Cesium.defined(pick.mesh)) {
        // Output glTF node and mesh under the mouse.
        console.log('node: ' + pick.node.name + '. mesh: ' + pick.mesh.name);
    }
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

Sandcastle使用講解

cesium 是通過(guò)knockout.js完成頁(yè)面DOM和數(shù)據(jù)綁定操作的.關(guān)鍵語(yǔ)句如下:
toolbar
Cesium.knockout
Cesium.knockout.track(viewModel)
Cesium.knockout.applyBindings(viewModel, toolbar)

// 在html中
<table><tbody>
    <tr>
        <td>Mode</td>
        <td><select data-bind="options: colorBlendModes, value: colorBlendMode"></select></td>
    </tr>
// 在js中
// The viewModel tracks the state of our mini application.
var viewModel = {
    color : 'White',
    colors : ['White', 'Red', 'Green', 'Blue', 'Yellow', 'Gray'],
    alpha : 1.0,
    colorBlendMode : 'Highlight',
    colorBlendModes : ['Highlight', 'Replace', 'Mix'],
    colorBlendAmount : 0.5,
    colorBlendAmountEnabled : false
};

// Convert the viewModel members into knockout observables.
Cesium.knockout.track(viewModel);

// Bind the viewModel to the DOM elements of the UI that call for it.
var toolbar = document.getElementById('toolbar');
Cesium.knockout.applyBindings(viewModel, toolbar);


Cesium.knockout.getObservable(viewModel, 'colorBlendMode').subscribe(
    function(newValue) {
        var colorBlendMode = getColorBlendMode(newValue);
        model.colorBlendMode = colorBlendMode;
        viewModel.colorBlendAmountEnabled = (colorBlendMode === Cesium.ColorBlendMode.MIX);
    }
);

mode下的terrainProvider 屬性

image.png

terrainProvider 其實(shí)是屬于global的,這里是做了個(gè)快捷方式,直接訪問(wèn)了global.terrainProvider ;
還有很多類(lèi)似這樣的屬性 都是快捷方式。

ModelInstance示例講解

scene 要?jiǎng)?chuàng)建1024個(gè)飛機(jī),考慮渲染性能,一次把1024個(gè)飛機(jī)一次繪制出來(lái),降低繪制批次,優(yōu)化渲染性能。
ModelInstance 在文檔里面沒(méi)有,是因?yàn)镸odelInstance.js文檔里面有@private關(guān)鍵字,所以自動(dòng)生成文檔的時(shí)候沒(méi)有該關(guān)鍵字。
鏈接地址
http://localhost:8080/Apps/Sandcastle/index.html?src=development%2F3D%20Models%20Instancing.html&label=Development

image.png

 var collection = scene.primitives.add(new Cesium.ModelInstanceCollection({
        url : url,
        instances : instances
    }));

Model子節(jié)點(diǎn)控制

http://localhost:8080/Apps/Sandcastle/index.html?src=development%2F3D%20Models%20Node%20Explorer.html&label=Development

image.png
image.png

改動(dòng)子節(jié)點(diǎn)關(guān)鍵代碼,獲取子節(jié)點(diǎn)的名字model.getNode,改動(dòng)node.matrix

 // respond to viewmodel changes by applying the computed matrix
    Cesium.knockout.getObservable(viewModel, 'matrix').subscribe(function(newValue) {
        var node = model.getNode(viewModel.nodeName);
        if (!Cesium.defined(node.originalMatrix)) {
            node.originalMatrix = node.matrix.clone();
        }
        node.matrix = Cesium.Matrix4.multiply(node.originalMatrix, newValue, new Cesium.Matrix4());
    });
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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