1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| var viewer = new Cesium.Viewer('cesiumContainer'); var CC3=Cesium.Cartesian3; var camera = viewer.scene.camera; var ellipsoid = viewer.scene.globe.ellipsoid; var rad = 6371000; //Earth average radius var entityN = viewer.entities.add({ label : { show : false } }); var entityW = viewer.entities.add({ label : { show : false } }); var entityS = viewer.entities.add({ label : { show : false } }); var entityE = viewer.entities.add({ label : { show : false } }); Sandcastle.addToolbarButton('horizon points', function() { //h 大专栏 cesiumjsorizon basics var dist=CC3.magnitude(camera.position); var angle=Math.asin(rad/dist); var toSurf=Math.sqrt(Math.pow(dist,2)-Math.pow(rad,2)); //'horizon arm' var rotatee = camera.position.clone(); rotatee = CC3.negate(rotatee,new CC3()); CC3.normalize(rotatee,rotatee); var rotater = new CC3(0,0,1); CC3.cross(rotatee,rotater,rotater); var rotated = new CC3();var cartesian = new CC3(); rotated = rotate(rotatee,rotater,angle); cartesian = scaleVector(toSurf,rotated); CC3.add(cartesian,camera.position,cartesian);
//north dropOne(entityN,cartesian); //east cartesian = rotate(cartesian,rotatee,Math.PI/2); //rotatee now rotater dropOne(entityE,cartesian); //south cartesian = rotate(cartesian,rotatee,Math.PI/2); //rotatee now rotater dropOne(entityS,cartesian); //west cartesian = rotate(cartesian,rotatee,Math.PI/2); //rotatee now rotater dropOne(entityW,cartesian); }); function dropOne(entity,cartesian) { if (cartesian) { var cartographic = ellipsoid.cartesianToCartographic(cartesian); var longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(2); var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(2);
entity.position = cartesian; entity.label.show = true; entity.label.text = '(' + longitudeString + ', ' + latitudeString + ')'; } else { entity.label.show = false; } } function rotate(rotatee,rotater,angle) { //rotater: unit vector, angle:radians //CCW looking from vector tip to vector base var CC3=Cesium.Cartesian3;var rotated=new CC3(); var c = Math.cos(angle);var s = Math.sin(angle); var dotScale = CC3.dot(rotatee,rotater,new CC3()); var rotaterScaled = scaleVector(dotScale,rotater); var vPerpAxis = CC3.subtract(rotatee,rotaterScaled,new CC3()); //using Pythagoras theorem var comp1 = scaleVector(c,vPerpAxis); var vPerpPerpAxis = CC3.cross(rotater,vPerpAxis,new CC3()); //perp to both of these var comp2 = scaleVector(s,vPerpPerpAxis); return addVectors([rotaterScaled,comp1,comp2]); } function scaleVector(scale,vector) { var temp = new Cesium.Cartesian3(); temp.x=scale*vector.x;temp.y=scale*vector.y;temp.z=scale*vector.z; return temp; } function addVectors(vectors) { var resultant=new Cesium.Cartesian3(0,0,0); var i=0;while(i<vectors.length) { resultant.x+=vectors[i].x; resultant.y+=vectors[i].y; resultant.z+=vectors[i].z; i+=1; } return resultant; }
|