cesium things simple - control the aircraft flight

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <! - and the introduction of js css ->
    <script type="text/javascript" src="./js/ctx.js"></script>
</head>
<body>
<div id="cesiumContainer">
</div>



<script>
    var viewer = new Cesium.Viewer('cesiumContainer',  {
        selectionIndicator : false,
        infoBox : false,
        shouldAnimate:true
       // terrainProvider : Cesium.createWorldTerrain()
    });


    var scene = viewer.scene;
    // declare a dynamic path, as the online demo draw, produce aircraft tails, feel difficult to understand can be removed
    var pathPosition = new Cesium.SampledPositionProperty();
    // add the path entity
    var entityPath = viewer.entities.add({
        position : pathPosition,
        name : 'path',
        path : {
            show : true,
            leadTime : 0,
            trailTime : 60,
            width : 10,
            resolution : 1,
            material : new Cesium.PolylineGlowMaterialProperty({
                glowPower: 0.3,
                color : Cesium.Color.PALEGOLDENROD
            })
        }
    });

    var hpRoll = new Cesium.HeadingPitchRoll();
    var speed = 100;

    var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, 5000.0);
    var position2= Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, 20000.0);
    viewer.camera.setView({
        destination :position
    });
    // set no secondary perspective, appear to be scaling entity will
    viewer.camera.setView({
        destination :position2
    });
    // speed vector
    var speedVector = new Cesium.Cartesian3();
    // ellipsoid of a 4x4 transformation matrix from the reference coordinate system to provide a fixed reference coordinate system
    var fixedFrameTransform = Cesium.Transforms.localFrameToFixedFrameGenerator('north', 'west');

    var planePrimitive = scene.primitives.add(Cesium.Model.fromGltf({
        url : '../data/models/CesiumAir/Cesium_Air.glb',
        // roll angle to a fixed reference frame from a given ellipsoid centered at the origin of a given head,
        // 4x4 transformation matrix is ​​calculated from the coordinate system with the coordinate axes.
        // heading north to the local rotation, increasing its positive angle to the east.
        // pitch is the rotation from the local plane over the northeast.
        // positive pitch angle above the plane. A negative pitch angle or less in a plane.
        // roll is partially about the first rotation axis east
        modelMatrix : Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, fixedFrameTransform),
        minimumPixelSize : 128
    }));
    console.log(planePrimitive.modelMatrix)
    //viewer.camera.viewBoundingSphere(planePrimitive.boundingSphere);
  // Render scene of the incident to preUpdate listen, before each preUpdate triggers, similar to the JS requestAnimation
    viewer.scene.preUpdate.addEventListener(function(scene, time) {
        Scalar Cartesian coordinate components // will provide multiplied by the offer of
        speedVector = Cesium.Cartesian3.multiplyByScalar(Cesium.Cartesian3.UNIT_X, speed / 10, speedVector);
        // Calculate the product matrix and Cartesian coordinates.
        // This is equivalent to calling matrix x4. W multiplied by the vector component of Cartesian4 1, but instead returns Cartesian3 Cartesian4

        position = Cesium.Matrix4.multiplyByPoint(planePrimitive.modelMatrix, speedVector, position);

        // The aircraft passed through the point position pathPosition, dynamic refresh line
        pathPosition.addSample(Cesium.JulianDate.now(), position);
        // Change the position of the aircraft model matrix
        Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, fixedFrameTransform, planePrimitive.modelMatrix);

    });
    var deltaRadians = Cesium.Math.toRadians(3.0);
    // respond to keyboard events, hpRoll, in fact, changing aircraft attitude
    document.addEventListener('keydown', function(e) {
        switch (e.keyCode) {
            case 40:
                if (e.shiftKey) {
                    // speed down
                    speed = Math.max(--speed, 1);
                } else {
                    // pitch down
                    hpRoll.pitch -= deltaRadians;
                    if (hpRoll.pitch < -Cesium.Math.TWO_PI) {
                        hpRoll.pitch += Cesium.Math.TWO_PI;
                    }
                }
                break;
            case 38:
                if (e.shiftKey) {
                    // speed up
                    speed = Math.min(++speed, 100);
                } else {
                    // pitch up
                    hpRoll.pitch += deltaRadians;
                    if (hpRoll.pitch > Cesium.Math.TWO_PI) {
                        hpRoll.pitch -= Cesium.Math.TWO_PI;
                    }
                }
                break;
            case 39:
                if (e.shiftKey) {
                    // roll right
                    hpRoll.roll + = deltaRadians;
                    if (hpRoll.roll > Cesium.Math.TWO_PI) {
                        hpRoll.roll -= Cesium.Math.TWO_PI;
                    }
                } else {
                    // turn right
                    hpRoll.heading += deltaRadians;
                    if (hpRoll.heading > Cesium.Math.TWO_PI) {
                        hpRoll.heading -= Cesium.Math.TWO_PI;
                    }
                }
                break;
            case 37:
                if (e.shiftKey) {
                    // roll left until
                    hpRoll.roll - = deltaRadians;
                    if (hpRoll.roll < 0.0) {
                        hpRoll.roll += Cesium.Math.TWO_PI;
                    }
                } else {
                    // turn left
                    hpRoll.heading -= deltaRadians;
                    if (hpRoll.heading < 0.0) {
                        hpRoll.heading += Cesium.Math.TWO_PI;
                    }
                }
                break;
            default:
        }
    });



</script>
</body>
</html>
Published 243 original articles · won praise 36 · Views 140,000 +

Guess you like

Origin blog.csdn.net/A873054267/article/details/103512086