<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Infinity Code Forum — Horizon camera tilt bug???]]></title>
	<link rel="self" href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=2032&amp;type=atom" />
	<updated>2022-09-23T21:58:43Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.infinity-code.com/viewtopic.php?id=2032</id>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8621#p8621" />
			<content type="html"><![CDATA[<p>Fantastic !! Thanks Alex</p>]]></content>
			<author>
				<name><![CDATA[LazzLab]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=669</uri>
			</author>
			<updated>2022-09-23T21:58:43Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8621#p8621</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8620#p8620" />
			<content type="html"><![CDATA[<p>For example, this can be fixed in this way (OnlineMapsCameraOrbit.UpdateCameraPosition):<br /></p><div class="codebox"><pre><code>public void UpdateCameraPosition()
{
    if (rotation.x &gt; maxRotationX) rotation.x = maxRotationX;
    else if (rotation.x &lt; 0) rotation.x = 0;

    if (activeCamera == null) return;

    float rx = 90 - rotation.x;
    if (rx &gt; 89.9) rx = 89.9f;

    double px = Math.Cos(rx * Mathf.Deg2Rad) * distance;
    double py = Math.Sin(rx * Mathf.Deg2Rad) * distance;
    double pz = Math.Cos(rotation.y * Mathf.Deg2Rad) * px;
    px = Math.Sin(rotation.y * Mathf.Deg2Rad) * px;
    
    double tlx = 0, tly = 0, brx = 0, bry = 0;
    float yScale = 0;

    Vector3 targetPosition;

    if (adjustTo == OnlineMapsCameraAdjust.gameObject &amp;&amp; adjustToGameObject != null)
    {
        targetPosition = adjustToGameObject.transform.position;
    }
    else
    {
        targetPosition = map.transform.position;
        Vector3 offset = new Vector3(sizeInScene.x / -2, 0, sizeInScene.y / 2);

        if (OnlineMapsElevationManagerBase.useElevation)
        {
            map.GetCorners(out tlx, out tly, out brx, out bry);
            yScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry);

            if (adjustTo == OnlineMapsCameraAdjust.maxElevationInArea) offset.y = OnlineMapsElevationManagerBase.instance.GetMaxElevation(yScale);
            else if (adjustTo == OnlineMapsCameraAdjust.averageCenter)
            {
                float ox = sizeInScene.x / 64;
                float oz = sizeInScene.y / 64;
                offset.y = OnlineMapsElevationManagerBase.GetElevation(targetPosition.x, targetPosition.z, yScale, tlx, tly, brx, bry) * 3;

                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x - ox, targetPosition.z - oz, yScale, tlx, tly, brx, bry) * 2;
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x, targetPosition.z - oz, yScale, tlx, tly, brx, bry) * 2;
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x + ox, targetPosition.z - oz, yScale, tlx, tly, brx, bry) * 2;
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x + ox, targetPosition.z, yScale, tlx, tly, brx, bry) * 2;
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x + ox, targetPosition.z + oz, yScale, tlx, tly, brx, bry) * 2;
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x, targetPosition.z + oz, yScale, tlx, tly, brx, bry) * 2;
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x - ox, targetPosition.z + oz, yScale, tlx, tly, brx, bry) * 2;
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x - ox, targetPosition.z, yScale, tlx, tly, brx, bry) * 2;

                ox *= 2;
                oz *= 2;

                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x - ox, targetPosition.z - oz, yScale, tlx, tly, brx, bry);
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x, targetPosition.z - oz, yScale, tlx, tly, brx, bry);
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x + ox, targetPosition.z - oz, yScale, tlx, tly, brx, bry);
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x + ox, targetPosition.z, yScale, tlx, tly, brx, bry);
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x + ox, targetPosition.z + oz, yScale, tlx, tly, brx, bry);
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x, targetPosition.z + oz, yScale, tlx, tly, brx, bry);
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x - ox, targetPosition.z + oz, yScale, tlx, tly, brx, bry);
                offset.y += OnlineMapsElevationManagerBase.GetElevation(targetPosition.x - ox, targetPosition.z, yScale, tlx, tly, brx, bry);

                offset.y /= 27;
            }
            else offset.y = OnlineMapsElevationManagerBase.GetElevation(targetPosition.x, targetPosition.z, yScale, tlx, tly, brx, bry);
        }

        offset.Scale(map.transform.lossyScale);
        
        targetPosition += map.transform.rotation * offset;
    }

    Vector3 oldPosition = activeCamera.transform.position;
    Vector3 newPosition = map.transform.rotation * new Vector3((float)px,  (float)py, (float)pz) + targetPosition;

    if (OnlineMapsElevationManagerBase.useElevation)
    {
        float y = OnlineMapsElevationManagerBase.GetElevation(newPosition.x, newPosition.z, yScale, tlx, tly, brx, bry);
        if (newPosition.y &lt; y + 200) newPosition.y = y + 200;
    }

    activeCamera.transform.position = newPosition;
    activeCamera.transform.LookAt(targetPosition);

    if (control.isMapDrag) control.UpdateLastPosition();

    if (oldPosition != newPosition &amp;&amp; OnCameraControl != null) OnCameraControl();
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2022-09-23T18:39:14Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8620#p8620</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8618#p8618" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Alex Vertax wrote:</cite><blockquote><p>Each provider has its own maximum zoom.<br />For most it&#039;s zoom 20.<br />For some it&#039;s zoom 19 or 18.<br />Some (Google Maps) in some regions of the Earth have tiles up to zoom 23.</p></blockquote></div><p>Ok, this probably my last question on the Tilt and zoom. When you go to the maximum zoom on an area with high elevations such as a mountain, if it is close enough, the camera can clip the horizon tile. </p><p>I tried giving the camera a parent that has a rigid body (with turned off gravity of course) and box collider. And changed the camera orbit a little bit so that the camera parent will follow the camera movement </p><div class="codebox"><pre><code>        Vector3 oldPosition = activeCamera.transform.parent.position;
        Vector3 newPosition = map.transform.rotation * new Vector3((float)px,  (float)py, (float)pz) + targetPosition;

        activeCamera.transform.parent.position = newPosition;
        activeCamera.transform.parent.LookAt(targetPosition);</code></pre></div><p>After that tried to give the horizon a mesh collider.</p><div class="codebox"><pre><code>go.AddComponent&lt;MeshCollider&gt;().sharedMesh = mesh;</code></pre></div><p>But it didn&#039;t seem to fix the cliiping. Do you have any workarounds on this??</p><p>Here&#039;s the example of the clipping <a href="https://drive.google.com/file/d/1aZNHVXGSAq_OzrYeY8cj9qLi8q2yli35/view?usp=sharing">https://drive.google.com/file/d/1aZNHVX … sp=sharing</a></p>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-23T02:39:20Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8618#p8618</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8617#p8617" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Alex Vertax wrote:</cite><blockquote><p>Each provider has its own maximum zoom.<br />For most it&#039;s zoom 20.<br />For some it&#039;s zoom 19 or 18.<br />Some (Google Maps) in some regions of the Earth have tiles up to zoom 23.</p></blockquote></div><p>Ah, got it. I tried to change it back from ArcGIS to Bing Maps and the zoom level can get up to 20. Thanks</p>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-23T02:00:42Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8617#p8617</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8616#p8616" />
			<content type="html"><![CDATA[<p>Each provider has its own maximum zoom.<br />For most it&#039;s zoom 20.<br />For some it&#039;s zoom 19 or 18.<br />Some (Google Maps) in some regions of the Earth have tiles up to zoom 23.</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2022-09-23T01:54:26Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8616#p8616</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8615#p8615" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Alex Vertax wrote:</cite><blockquote><p>Thanks for the project.<br />Bug fixed.<br />Horizon scripts have been updated in the corresponding thread.</p></blockquote></div><p>Thanks, got the update. One more question tho, at zoom 20, for some reason the map data became unavailable on the tile set, but not for the horizon.</p><p><a href="https://drive.google.com/file/d/1QK1b479W93Ni6BYyUB970e8Y6ze39qnO/view?usp=sharing">https://drive.google.com/file/d/1QK1b47 … sp=sharing</a></p>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-23T01:49:22Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8615#p8615</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8613#p8613" />
			<content type="html"><![CDATA[<p>Thanks for the project.<br />Bug fixed.<br />Horizon scripts have been updated in the corresponding thread.</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2022-09-22T10:56:33Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8613#p8613</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8612#p8612" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Alex Vertax wrote:</cite><blockquote><p>I understand that you cannot send me your working project. This is logical.<br />But you can make a new temporary project that only contains Online Maps and your HDRP settings so I can start the scene and see the problem.<br />This will help me a lot to focus on solving the problem and not looking for a black cat in a dark room, because what I wrote about RenderPipelineAsset is only a guess, and it is quite possible that the problem will be elsewhere.</p></blockquote></div><p>Ok got it, I&#039;ll be sending it to your email</p>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-22T09:36:21Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8612#p8612</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8611#p8611" />
			<content type="html"><![CDATA[<p>I understand that you cannot send me your working project. This is logical.<br />But you can make a new temporary project that only contains Online Maps and your HDRP settings so I can start the scene and see the problem.<br />This will help me a lot to focus on solving the problem and not looking for a black cat in a dark room, because what I wrote about RenderPipelineAsset is only a guess, and it is quite possible that the problem will be elsewhere.</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2022-09-21T12:07:33Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8611#p8611</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8604#p8604" />
			<content type="html"><![CDATA[<p>Third Image</p><p><span class="postimg"><img src="https://forum.infinity-code.com/misc.php?action=pun_attachment&amp;item=930&amp;download=1" alt="HDRP Settings_3" /></span></p>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-21T02:48:01Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8604#p8604</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8603#p8603" />
			<content type="html"><![CDATA[<p>Second Image</p><p><span class="postimg"><img src="https://forum.infinity-code.com/misc.php?action=pun_attachment&amp;item=930&amp;download=1" alt="HDRP Settings_2" /></span></p>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-21T02:46:40Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8603#p8603</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8602#p8602" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>moonflow123 wrote:</cite><blockquote><div class="quotebox"><cite>Alex Vertax wrote:</cite><blockquote><p>I just tested tilted horizon with HDRP and it works correctly on my side.<br />Maybe the problem is somewhere in the RenderPipelineAsset settings.<br />But to be honest, I don&#039;t have enough experience in RP configuration to say what&#039;s wrong.<br />If you want, you can send me your project by email (support@infinity-code.com) and I will try to find the cause of the problem.</p></blockquote></div><p>Sorry I can&#039;t give you my project because of my company policy, but I can give you the HDRP shader file.</p><p>Oh and I&#039;m also using BingMapElevationManager as instructed, but I can&#039;t seem to understand why do I need to add the mapbox key too because I already added a BingMapsKey in BingMapsElevationManager? Or should I use the BingMapHorizon script that you uploaded for the horizon post because i&#039;m using BingMapsElevation??? I&#039;m using the Horizon2_OM3.cs because that is the newest addition to your files.</p><p>Oh and also when I said zoom 18 to 20, I meant when in an area that has an elevation like in the video (For example a Mountain).</p><div class="quotebox"><cite>moonflow123 wrote:</cite><blockquote><p>When on a certain zoom (usually 18 to 20), when you tilt the camera this happens.</p><p><a href="https://drive.google.com/file/d/1sMRMkB5WvUv5cSiQ0HGEDX-zkWnU67bf/view?usp=sharing">Video Link</a></p><p>Is there anyway to fix this?</p></blockquote></div></blockquote></div><p>Here&#039;s also the HDRP Settings image</p><p><span class="postimg"><img src="https://forum.infinity-code.com/misc.php?action=pun_attachment&amp;item=929&amp;download=1" alt="HDRP Settings" /></span></p>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-21T02:40:23Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8602#p8602</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8601#p8601" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Alex Vertax wrote:</cite><blockquote><p>I just tested tilted horizon with HDRP and it works correctly on my side.<br />Maybe the problem is somewhere in the RenderPipelineAsset settings.<br />But to be honest, I don&#039;t have enough experience in RP configuration to say what&#039;s wrong.<br />If you want, you can send me your project by email (support@infinity-code.com) and I will try to find the cause of the problem.</p></blockquote></div><p>Sorry I can&#039;t give you my project because of my company policy, but I can give you the HDRP shader file.</p><p>Oh and I&#039;m also using BingMapElevationManager as instructed, but I can&#039;t seem to understand why do I need to add the mapbox key too because I already added a BingMapsKey in BingMapsElevationManager? Or should I use the BingMapHorizon script that you uploaded for the horizon post because i&#039;m using BingMapsElevation??? I&#039;m using the Horizon2_OM3.cs because that is the newest addition to your files.</p><p>Oh and also when I said zoom 18 to 20, I meant when in an area that has an elevation like in the video (For example a Mountain).</p><div class="quotebox"><cite>moonflow123 wrote:</cite><blockquote><p>When on a certain zoom (usually 18 to 20), when you tilt the camera this happens.</p><p><a href="https://drive.google.com/file/d/1sMRMkB5WvUv5cSiQ0HGEDX-zkWnU67bf/view?usp=sharing">Video Link</a></p><p>Is there anyway to fix this?</p></blockquote></div>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-21T02:25:53Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8601#p8601</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8600#p8600" />
			<content type="html"><![CDATA[<p>I just tested tilted horizon with HDRP and it works correctly on my side.<br />Maybe the problem is somewhere in the RenderPipelineAsset settings.<br />But to be honest, I don&#039;t have enough experience in RP configuration to say what&#039;s wrong.<br />If you want, you can send me your project by email (support@infinity-code.com) and I will try to find the cause of the problem.</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2022-09-20T17:01:21Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8600#p8600</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Horizon camera tilt bug???]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=8599#p8599" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Alex Vertax wrote:</cite><blockquote><p>Hello.</p><p>It looks like you&#039;re having some kind of problem with the horizon shader.<br />Please show a screenshot of your Horizon script.</p></blockquote></div><p>Here&#039;s the Horizon script</p><p><span class="postimg"><img src="https://forum.infinity-code.com/misc.php?action=pun_attachment&amp;item=926&amp;download=1" alt="screenshot" /></span></p><div class="codebox"><pre><code>using System;
using System.Text;
using UnityEngine;

public class Horizon2_OM3 : MonoBehaviour
{
    /// &lt;summary&gt;
    /// Mapbox Access Token for elevations.
    /// Leave blank if you do not want to use elevations.
    /// &lt;/summary&gt;
    [Tooltip(&quot;Mapbox Access Token for elevations.\nLeave blank if you do not want to use elevations.&quot;)]
    public string mapboxKey;

    /// &lt;summary&gt;
    /// Number of tiles horizontally (X axis).
    /// &lt;/summary&gt;
    [Tooltip(&quot;Number of tiles horizontally (X axis).&quot;)]
    public int countX = 3;

    /// &lt;summary&gt;
    /// Number of tiles vertically (Z axis).
    /// &lt;/summary&gt;
    [Tooltip(&quot;Number of tiles vertically (Z axis).&quot;)]
    public int countY = 3;

    /// &lt;summary&gt;
    /// Offset of the horizon mesh along the y-axis relative to the map. Keep it negative.
    /// &lt;/summary&gt;
    [Tooltip(&quot;Offset of the horizon mesh along the y-axis relative to the map. Keep it negative.&quot;)]
    public float positionYOffset = -5;

    /// &lt;summary&gt;
    /// Offset zoom of tiles relative to map.zoom. Keep positive.
    /// &lt;/summary&gt;
    [Tooltip(&quot;Offset zoom of tiles relative to map.zoom. Keep positive.&quot;)]
    public int zoomOffset = 3;

    /// &lt;summary&gt;
    /// Shader of the tiles mesh.
    /// &lt;/summary&gt;
    [Tooltip(&quot;Shader of the tiles mesh.&quot;)]
    public Shader shader;

    /// &lt;summary&gt;
    /// Offset of the render queue relative to render queue of the shader.
    /// &lt;/summary&gt;
    [Tooltip(&quot;Offset of the render queue relative to render queue of the shader.&quot;)]
    public int renderQueueOffset = 100;

    /// &lt;summary&gt;
    /// Tile resolution.
    /// &lt;/summary&gt;
    [Tooltip(&quot;Tile resolution.&quot;)]
    public int resolution = 32;

    private OnlineMapsTile[] tiles;
    private Mesh mesh;
    private Vector3[] vertices;
    private Vector3[] normals;
    private Vector2[] uv;
    private int[] triangles;
    private OnlineMaps map;
    private OnlineMapsTileSetControl control;
    private MeshRenderer meshRenderer;

    private Vector2 ctl;
    private Vector2 cbr;

    private void DownloadElevation(OnlineMapsTile tile)
    {
        if (string.IsNullOrEmpty(mapboxKey)) return;

        string url = new StringBuilder(&quot;https://api.mapbox.com/v4/mapbox.terrain-rgb/&quot;)
            .Append(tile.zoom).Append(&quot;/&quot;).Append(tile.x).Append(&quot;/&quot;).Append(tile.y)
            .Append(&quot;.pngraw?access_token=&quot;).Append(mapboxKey).ToString();
        OnlineMapsWWW www = new OnlineMapsWWW(url);
        www.OnComplete += delegate
        {
            tile[&quot;cdata&quot;] = new CData(www.bytes);
            OnGetElevation(ctl.x, ctl.y, cbr.x, cbr.y);
            UpdateMesh();
        };
    }

    private void InitMesh()
    {
        GameObject go = new GameObject(&quot;Horizon&quot;);
        go.transform.parent = map.gameObject.transform;
        go.transform.localPosition = Vector3.zero;
        go.transform.localRotation = Quaternion.Euler(Vector3.zero);
        go.transform.localScale = Vector3.one;

        MeshFilter meshFilter = go.AddComponent&lt;MeshFilter&gt;();
        meshRenderer = go.AddComponent&lt;MeshRenderer&gt;();
        int countTiles = countX * countY;

        mesh = new Mesh();
        mesh.name = &quot;Horizon&quot;;
        mesh.MarkDynamic();

        meshFilter.sharedMesh = mesh;

        tiles = new OnlineMapsTile[countTiles];

        int countVertices = (countX + 1) * (countY + 1) * resolution * resolution;
        vertices = new Vector3[countVertices];
        normals = new Vector3[countVertices];
        uv = new Vector2[countVertices];
        triangles = new int[6 * resolution * resolution];

        mesh.vertices = vertices;
        mesh.subMeshCount = countTiles;
        float r1 = resolution - 1;

        int index = 0;
        for (int i = 0; i &lt; (countX + 1) * (countY + 1); i++)
        {
            for (int x = 0; x &lt; resolution; x++)
            {
                for (int y = 0; y &lt; resolution; y++)
                {
                    normals[index] = Vector3.up;
                    uv[index++] = new Vector2(x / r1, 1 - y / r1);
                }
            }
        }

        mesh.uv = uv;
        mesh.normals = normals;

        Material[] materials = new Material[countTiles];

        for (int i = 0; i &lt; countTiles; i++)
        {
            int ti = 0;
            for (int x = 0; x &lt; resolution - 1; x++)
            {
                for (int y = 0; y &lt; resolution - 1; y++)
                {
                    int vi = i * resolution * resolution + x * resolution + y;
                    triangles[ti] = vi;
                    triangles[ti + 1] = vi + resolution + 1;
                    triangles[ti + 2] = vi + 1;
                    triangles[ti + 3] = vi;
                    triangles[ti + 4] = vi + resolution;
                    triangles[ti + 5] = vi + resolution + 1;
                    ti += 6;
                }
            }

            mesh.SetTriangles(triangles, i);

            Material material = new Material(shader);
            material.renderQueue = shader.renderQueue + renderQueueOffset;
            materials[i] = material;
        }

        meshRenderer.sharedMaterials = materials;

        UpdateMesh();

        mesh.RecalculateBounds();
    }

    private void OnGetElevation(double leftLng, double topLat, double rightLng, double bottomLat)
    {
        ctl = new Vector2((float)leftLng, (float)topLat);
        cbr = new Vector2((float)rightLng, (float)bottomLat);

        double tlx, tly, brx, bry;

        map.projection.CoordinatesToTile(ctl.x, ctl.y, map.zoom, out tlx, out tly);
        map.projection.CoordinatesToTile(cbr.x, cbr.y, map.zoom, out brx, out bry);

        int scale = 1 &lt;&lt; zoomOffset;

        int zoom = map.zoom - zoomOffset;

        short[,] heights = new short[32, 32];
        double rx = (brx - tlx) / 31;
        double ry = (bry - tly) / 31;

        for (int x = 0; x &lt; 32; x++)
        {
            double tx = (rx * x + tlx) / scale;

            for (int y = 0; y &lt; 32; y++)
            {
                double ty = (ry * y + tly) / scale;

                OnlineMapsTile tile = OnlineMapsTile.GetTile(zoom, (int) tx, (int) ty);
                if (tile == null)
                {
                    heights[x, y] = 0;
                    continue;
                }
                CData data = tile[&quot;cdata&quot;] as CData;
                if (data == null)
                {
                    heights[x, y] = 0;
                    continue;
                }
                heights[x, 31 - y] = data.GetElevation(tx, ty);
            }
        }

        if (OnlineMapsElevationManagerBase.isActive) 
        OnlineMapsBingMapsElevationManager.instance.SetElevationData(heights);
    }

    private void OnTileDownloaded(OnlineMapsTile tile)
    {
        for (int i = 0; i &lt; countX * countY; i++) if (tiles[i] == tile) meshRenderer.sharedMaterials[i].mainTexture 
        = tile.texture;
        DownloadElevation(tile);
    }

    private void Start()
    {
        if (zoomOffset &lt;= 0) throw new Exception(&quot;Zoom offset should be positive.&quot;);
        if (shader == null) shader = Shader.Find(&quot;Diffuse&quot;);

        map = OnlineMaps.instance;
        control = OnlineMapsTileSetControl.instance;
        OnlineMapsTile.OnTileDownloaded += OnTileDownloaded;
        if (OnlineMapsCache.instance != null)
        {
            OnlineMapsCache.instance.OnLoadedFromCache += OnTileDownloaded;
        }
        map.OnMapUpdated += UpdateMesh;
        if (OnlineMapsElevationManagerBase.isActive) OnlineMapsElevationManagerBase.instance.OnGetElevation +=
        OnGetElevation;

        InitMesh();
    }

    private void UpdateMesh()
    {
        int zoom = map.zoom - zoomOffset;
        if (zoom &lt; 3) zoom = 3;

        for (int i = 0; i &lt; countX * countY; i++) if (tiles[i] != null) tiles[i].Unblock(this);

        double tx, ty;
        map.GetTilePosition(out tx, out ty, zoom);

        int itx = Mathf.RoundToInt((float)(tx - countX / 2f));
        int ity = Mathf.RoundToInt((float)(ty - countY / 2f));

        Vector3 offset = new Vector3(0, positionYOffset, 0) - transform.position;

        int max = 1 &lt;&lt; zoom;
        Material[] materials = meshRenderer.sharedMaterials;

        float r1 = resolution - 1;
        int vi = 0;

        double tlx, tly, brx, bry;
        map.GetCorners(out tlx, out tly, out brx, out bry);
        float elevationScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry);

        int ox = countY * resolution * resolution - (resolution - 1) * resolution;
        int oz = resolution * (resolution - 1) + 1;

        for (int x = 0; x &lt; countX; x++)
        {
            int tileX = itx + x;
            int nextTileX = tileX + 1;
            if (tileX &gt;= max) tileX -= max;
            if (nextTileX &gt;= max) nextTileX -= max;

            for (int y = 0; y &lt; countY; y++)
            {
                int tileY = ity + y;
                int nextTileY = tileY + 1;

                if (tileY &gt;= max) tileY -= max;
                if (nextTileY &gt;= max) nextTileY -= max;

                OnlineMapsTile tile = OnlineMapsTile.GetTile(zoom, tileX, tileY);
                if (tile == null)
                {
                    OnlineMapsTile parentTile = OnlineMapsTile.GetTile(zoom - 1, tileX / 2, tileY / 2);
                    tile = new OnlineMapsRasterTile(tileX, tileY, zoom, map);
                    tile.parent = parentTile;
                }
                int tileIndex = x * countY + y;
                tiles[tileIndex] = tile;
                tile.Block(this);

                CData data = tile[&quot;cdata&quot;] as CData;
                bool hasElevation = data != null;

                double px, py;

                map.projection.TileToCoordinates(tileX, tileY, zoom, out px, out py);
                Vector3 v1 = control.GetWorldPosition(px, py) + offset;

                map.projection.TileToCoordinates(nextTileX, nextTileY, zoom, out px, out py);
                Vector3 v2 = control.GetWorldPosition(px, py) + offset;
                Vector3 ov = (v2 - v1) / r1;

                for (int vx = 0; vx &lt; resolution; vx++)
                {
                    for (int vz = 0; vz &lt; resolution; vz++)
                    {
                        Vector3 v = new Vector3(ov.x * vx + v1.x, 0, ov.z * vz + v1.z);
                        if (hasElevation)
                        {
                            if (vz == 0 &amp;&amp; y &gt; 0) v.y = vertices[vi - oz].y;
                            else if (vx == 0 &amp;&amp; x &gt; 0) v.y = vertices[vi - ox].y;
                            else
                            {
                                double evx = vx / r1;
                                double evz = vz / r1;
                                if (evx &gt;= 1) evx = 0.999;
                                if (evz &gt;= 1) evz = 0.999;
                                if (OnlineMapsElevationManagerBase.isActive) v.y = data.GetElevation(evx, evz) 
                                * elevationScale + offset.y;
                                else v.y = positionYOffset;
                            }
                        }
                        else v.y = positionYOffset;
                        vertices[vi++] = v;
                    }
                }

                materials[tileIndex].mainTexture = tile.texture;
                materials[tileIndex].color = new Color(1, 1, 1, tile.texture != null ? 1 : 0);
            }
        }

        mesh.vertices = vertices;
    }

    internal class CData
    {
        private short[,] heights;

        public CData(byte[] bytes)
        {
            Texture2D texture = new Texture2D(256, 256, TextureFormat.RGB24, false);
            texture.LoadImage(bytes);

            const int res = 256;

            if (texture.width != res || texture.height != res) return;

            Color[] colors = texture.GetPixels();

            heights = new short[res, res];

            for (int y = 0; y &lt; res; y++)
            {
                int py = (255 - y) * res;

                for (int x = 0; x &lt; res; x++)
                {
                    Color c = colors[py + x];

                    double height = -10000 + (c.r * 255 * 256 * 256 + c.g * 255 * 256 + c.b * 255) * 0.1;
                    heights[x, y] = (short)Math.Round(height);
                }
            }
        }

        public short GetElevation(double tx, double ty)
        {
            if (heights == null) return 0;

            double rx = tx - Math.Floor(tx);
            double ry = ty - Math.Floor(ty);
            int x = (int)Math.Round(rx * 256);
            int y = (int)Math.Round(ry * 256);
            if (x &gt; 255) x = 255;
            if (y &gt; 255) y = 255;
            return heights[x, y];
        }
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[moonflow123]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1980</uri>
			</author>
			<updated>2022-09-20T01:40:36Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=8599#p8599</id>
		</entry>
</feed>
