Topic: Map with spotlight = clipping issues?

I have a map object, and a spotlight object. When I shine the spotlight on the map with elevation enabled (I have mapbox enabled), the spotlight will often ignore the elevation in certain parts and show up on the other side of a mountain, even though the mountain should stop the light completely.
I've included an example of this with arrows pointing at some of the light that shouldnt be shining through. The light is coming from the top left of the scene. The parts where the spotlight disappears is where the mountain goes up, but when the mountain comes back down, the light reappears, even though it should be cut off.
Not sure what I can do to fix this problem.. If anyone knows what I can do to make sure the light stops wherever it touches elevation that would be great!

Post's attachments

Attachment icon Image3.png 438.71 kb, 29 downloads since 2023-08-23 

Re: Map with spotlight = clipping issues?

Hello.

To fix the problem:
1. Enable shadows in the Light component.
2. Go to Edit / Project Settings / Quality and increase Shadow Distance to the size of the map in the scene * 2.
You can also increase Shadow Resolution.
3. (Optional) In the OnlineMapsTileSetControl script, after the line "tilesetMesh.RecalculateBounds();" (line ~1200) add the line "tilesetMesh.RecalculateNormals();". But to be honest I didn't notice any difference.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Map with spotlight = clipping issues?

Thank you for the help! Step 3 actually fixed the problem for me, but I seem to have another problem from it. My light that's projected seems to be faceted, but i need the normals to be smooth. Currently my light looks like varying shades of squares depending on the shadows. Do you know how i can fix this?

Re: Map with spotlight = clipping issues?

Try this:

using System.Collections.Generic;
using UnityEngine;

namespace InfinityCode.OnlineMapsSupport
{
    public class TilesetSmoothNormals : MonoBehaviour
    {
        public OnlineMapsTileSetControl control;

        private void Start()
        {
            if (control == null) control = OnlineMapsTileSetControl.instance;
            control.OnMeshUpdated += OnMeshUpdated;
        }

        private void OnMeshUpdated()
        {
            Mesh mesh = control.meshFilter.sharedMesh;
            mesh.RecalculateNormals();

            Vector3[] vertices = mesh.vertices;
            Vector3[] normals = mesh.normals;

            Dictionary<int, Item> items = new Dictionary<int, Item>();

            for (int i = 0; i < vertices.Length; i++)
            {
                Vector3 vertex = vertices[i];
                int hash = vertex.GetHashCode();
                Item item = null;
                if (!items.TryGetValue(hash, out item))
                {
                    item = new Item();
                    items[hash] = item;
                }

                item.Add(i, normals[i]);
            }

            foreach (KeyValuePair<int, Item> pair in items)
            {
                Item item = pair.Value;
                item.FixNormals(normals);
            }

            mesh.normals = normals;
            control.meshFilter.sharedMesh = mesh;
        }

        internal class Item
        {
            public List<int> indices;
            private Vector3 sumOfNormals;

            public Item()
            {
                indices = new List<int>();
            }

            public void Add(int index, Vector3 normal)
            {
                indices.Add(index);
                sumOfNormals += normal;
            }

            public void FixNormals(Vector3[] normals)
            {
                Vector3 normal = (sumOfNormals / indices.Count).normalized;
                for (int i = 0; i < indices.Count; i++)
                {
                    int index = indices[i];
                    normals[index] = normal;
                }
            }
        }
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Map with spotlight = clipping issues?

That worked!!! Thank you so much for the help big_smile