pixelToWorld Method

Returns the real world coordinates on the terrain, behind a specified pixel on the window. If the selected pixel is part of an object, the object is also returned.

pixelToWorld([pixelX], [pixelY], [type]);

 

Parameter

Description

pixelX

Optional, default: undefined. If this parameter is not specified, the method assumes that the x coordinate of the center of the window is requested

The left-right coordinate in pixels of a specified point on the window

pixelY

Optional, default: undefined. If this parameter is not specified, the method assumes that the y coordinate of the center of the window is requested

The top-bottom coordinate in pixels of a specified point on the window

type

Optional, default: sgPixelToWorldTypeAll

Can be one of the following values: sgPixelToWorldTypeTerrain –queries only the coordinates on the terrain. If there is a terrain object behind the specified screen pixel, the object is ignored.

sgPixelToWorldTypeAll –queries the terrain coordinates and the objects coordinates. If this flag is specified and there is a terrain object (e.g., polygon) behind the specified screen pixel, the return value will contain the object coordinates.

Return Value

Null if the pixel hits the sky, otherwise an object with three properties: coord, distance node.

The coord property is an SGCoord3D object that indicates where in the 3D world the pixel hit. The distance property indicates the distance from the pixel (camera) to the returned coordinate. The node property is null if behind the specified pixel there is only terrain. Otherwise, if a terrain object such as 3D polygon is behind the pixel, the node property of the return value contains an SGNode object that represents the terrain object.

Remarks

If none of the parameters is specified, the method returns information about the center pixel of the window

Example

The following example illustrates the use of the pixelToWorld method.

 

function centerWindowToWorld()

{

   var ret = globe.window.pixelToWorld();

 

   if (ret != null)

   {

      var terrainX = ret.coord.x.toFixed(5);

      var terrainY = ret.coord.y.toFixed(5);

      var distToTerrain = ret.distance.toFixed(1);

 

      var s  = "Terrain coordinate at window center:\n"

          s += terrainX + " , " + terrainY + "\n\n";  

          s += "Distance to terrain: "+ distToTerrain +" meters";

 

      alert(s);     

   }

   else

      alert("Window center coordinate hit the sky");

}