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


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

image.png

minimumPixelSize : 128 //保證不管地球縮放到多小,這個飛機依然能看得見

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

createModel具體使用講解

modelMatrix
Cesium.Transforms.headingPitchRollToFixedFrame
Model readyPromise
camera.lookAt

//origin 基於空間直角坐標系,已地球的圓心為原點建的坐標系,本初子午線為X軸;origin = 需要從經緯度轉為空間直角坐標系的值
//hpr 相對於飛機自身的三個旋轉角度,heading左右搖頭的角度,改變航向;Pitch 上下抬頭低頭角度,上正下負;Roll相對於視線方向,從左到右旋轉的角度;
//在地球上的不同位置,飛機的初始姿態是不一樣的。需要根據不同的位置初始化飛機的位置
//異步創建,
// camera.lookAt 調整相機角度,才能看到飛機的位置;設置視覺中心點,視覺角度;鼠標坐標拖動時可以看到,是圍繞着視覺中心點進行旋轉的,若需要解綁的話,需要用另外一個函數
//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);
    });
}

注冊事件 ScreenSpaceEventHandler

scene.pick
scene.pick之后需要判斷的屬性字段根據你的需求而定,這里我們關注的是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 是通過knockout.js完成頁面DOM和數據綁定操作的.關鍵語句如下:
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 其實是屬於global的,這里是做了個快捷方式,直接訪問了global.terrainProvider ;
還有很多類似這樣的屬性 都是快捷方式。

ModelInstance示例講解

scene 要創建1024個飛機,考慮渲染性能,一次把1024個飛機一次繪制出來,降低繪制批次,優化渲染性能。
ModelInstance 在文檔里面沒有,是因為ModelInstance.js文檔里面有@private關鍵字,所以自動生成文檔的時候沒有該關鍵字。
鏈接地址
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子節點控制

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

image.png

image.png

改動子節點關鍵代碼,獲取子節點的名字model.getNode,改動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());
    });

本文轉自 https://www.jianshu.com/p/ca1fe4735b38,如有侵權,請聯系刪除。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM