Beispiel #1
0
        /// <summary>
        /// Returns the world position of the given map coordinate.
        /// This takes into account the viewport and ground elevation is used,
        /// unless you pass -1 to height which will assume absolute 0 height.
        /// If viewport is enabled, you can use the ignoreViewport param to return the flat 2D Map position.
        /// Use heightOffsetMode to position wisely the height:
        /// - Absolute Altitude will return an absolute height irrespective of altitude at map point (it can cross ground)
        /// - Absolute Clamped will return either the ground altitude or the absolute height (the greater value)
        /// - Relative to the ground will simply add the height to the ground altitude
        /// BaseHeight is always refered to the lower bottom of a gameobject.
        /// If computing the position of a single point in space, pass 0 to pivotHeight. Otherwhise, pass the y position of the center of the game object to pivotHeight.
        /// For example, if want to position a sphere, pass the desired altitude to baseHeight and sphere.transform.localScale.y * 0.5f to pivotHeight.
        /// </summary>
        public Vector3 Map2DToWorldPosition(Vector2 position, float baseHeight, float pivotHeight, HEIGHT_OFFSET_MODE heightOffsetMode, bool ignoreViewport)
        {
            if (!renderViewportIsEnabled || ignoreViewport)
            {
                return(transform.TransformPoint(position));
            }

            Vector3 worldPos;

            if (_renderViewportIsTerrain)
            {
                // Terrain mode
                worldPos = transform.TransformPoint(position);
                switch (heightOffsetMode)
                {
                case HEIGHT_OFFSET_MODE.RELATIVE_TO_GROUND:
                    worldPos.y = terrain.SampleHeight(worldPos) + baseHeight;
                    break;

                case HEIGHT_OFFSET_MODE.ABSOLUTE_CLAMPED:
                    float y = baseHeight - WMSK_TERRAIN_MODE_Y_OFFSET;
                    worldPos.y = Mathf.Max(y, terrain.SampleHeight(worldPos));
                    break;

                case HEIGHT_OFFSET_MODE.ABSOLUTE_ALTITUDE:
                    worldPos.y += baseHeight - WMSK_TERRAIN_MODE_Y_OFFSET;
                    break;
                }
            }
            else
            {
                // Viewport
                baseHeight *= _renderViewportElevationFactor;
                switch (heightOffsetMode)
                {
                case HEIGHT_OFFSET_MODE.RELATIVE_TO_GROUND:
                    baseHeight += ComputeEarthHeight(position, true);
                    break;

                case HEIGHT_OFFSET_MODE.ABSOLUTE_CLAMPED:
                    baseHeight = Mathf.Max(baseHeight, ComputeEarthHeight(position, true));
                    break;

                case HEIGHT_OFFSET_MODE.ABSOLUTE_ALTITUDE:
                    break;
                }
                float height = baseHeight + pivotHeight;

                position = Map2DToRenderViewport(position);                                             // makes it compatible with wrapping mode
                worldPos = transform.TransformPoint(position);                                          // converts it to world position
                Vector3 viewportPos = _currentCamera.WorldToViewportPoint(worldPos);                    // maps to camera clip space which equals to current render viewport view

                viewportPos.x -= 0.5f;
                viewportPos.y -= 0.5f;
                if (currentCurvature != 0)
                {
                    height -= Mathf.Cos(viewportPos.x * 3.1415927f) * currentCurvature;
                }

                worldPos = _renderViewport.transform.TransformPoint(new Vector3(viewportPos.x, viewportPos.y, -height));                 // convert to world space again
            }
            return(worldPos);
        }
Beispiel #2
0
 /// <summary>
 /// Returns the world position of the given map coordinate.
 /// This takes into account the viewport and ground elevation is used,
 /// unless you pass -1 to height which will assume absolute 0 height.
 /// If viewport is enabled, you can use the ignoreViewport param to return the flat 2D Map position.
 /// Use heightOffsetMode to position wisely the height:
 /// - Absolute Altitude will return an absolute height irrespective of altitude at map point (it can cross ground)
 /// - Absolute Clamped will return either the ground altitude or the absolute height (the greater value)
 /// - Relative to the ground will simply add the height to the ground altitude
 /// </summary>
 public Vector3 Map2DToWorldPosition(Vector2 position, float height, HEIGHT_OFFSET_MODE heightOffsetMode, bool ignoreViewport)
 {
     return(Map2DToWorldPosition(position, height, 0, heightOffsetMode, ignoreViewport));
 }