var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;
var trigonometryRectanglePrimitive;
var rectangleWidth = 0.001;
function addRectanglePrimitive(cartographic) {
var rectangle = new Cesium.Rectangle(cartographic.longitude - rectangleWidth, cartographic.latitude - rectangleWidth, cartographic.longitude + rectangleWidth, cartographic.latitude + rectangleWidth);
// Compute spherical coordinates of the rectangle's NorthEast and SouthWest corners
var southWestCartesian = Cesium.Cartesian3.fromRadians(rectangle.west, rectangle.south);
var northEastCartesian = Cesium.Cartesian3.fromRadians(rectangle.east, rectangle.north);
var southWestNormal = Cesium.Cartesian3.normalize(southWestCartesian, new Cesium.Cartesian3());
var northEastNormal = Cesium.Cartesian3.normalize(northEastCartesian, new Cesium.Cartesian3());
var westApproximation = Math.atan2(southWestNormal.y, southWestNormal.x);
var southApproximation = Math.asin(southWestNormal.z);
var azimuthExtent = Math.atan2(northEastNormal.y, northEastNormal.x) - westApproximation;
var altitudeExtent = Math.asin(northEastNormal.z) - southApproximation;
var fragmentShaderSource =
'float southApproximation = ' + southApproximation + ';' +
'float westApproximation = ' + westApproximation + ';' +
'float altitudeExtent = ' + altitudeExtent + ';' +
'float azimuthExtent = ' + azimuthExtent + ';' +
'void main()' +
'{' +
' vec4 worldCoord = czm_inverseView * czm_windowToEyeCoordinates(gl_FragCoord.xy, gl_FragCoord.z);' +
' vec3 normal = normalize(worldCoord.xyz / worldCoord.w);' +
' float altitude = asin(normal.z);' +
' float azimuth = atan(normal.y, normal.x);' +
' float u = (altitude - southApproximation) / altitudeExtent;' +
' float v = (azimuth - westApproximation) / azimuthExtent;' +
' vec4 color = vec4(u, v, 0.0, 1.0);' +
' if (0.0 <= u && u <= 1.0 && 0.0 <= v && v <= 1.0) {' +
' gl_FragColor = color;' +
' } else { ' +
' gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);' +
' }' +
'}';
trigonometryRectanglePrimitive = new Cesium.Primitive({
geometryInstances : new Cesium.GeometryInstance({
geometry : new Cesium.RectangleGeometry({
rectangle : rectangle
})
}),
appearance : new Cesium.MaterialAppearance({
fragmentShaderSource : fragmentShaderSource
})
});
scene.primitives.add(trigonometryRectanglePrimitive);
}
var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
scene.primitives.remove(trigonometryRectanglePrimitive);
if (scene.mode !== Cesium.SceneMode.MORPHING) {
if (scene.pickPositionSupported) {
var cartesian = viewer.camera.pickEllipsoid(movement.position);
if (Cesium.defined(cartesian)) {
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
addRectanglePrimitive(cartographic);
}
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
var initialLongitude = -80.1;
var initialLatitude = 0.0;
addRectanglePrimitive(Cesium.Cartographic.fromDegrees(initialLongitude, initialLatitude));
viewer.camera.lookAt(Cesium.Cartesian3.fromDegrees(initialLongitude, initialLatitude), new Cesium.Cartesian3(0.0, 0.0, 50000.0));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
댓글