Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Detect mouse over abstract shape

Discussion in 'Scripting' started by jtsmith1287, Sep 25, 2014.

  1. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    I am planning on having a sphere (planet) that players can rotate and select a territory. My artist will draw a map of this planet and I'm guessing I'll need to manually trace some shapes around the territory markers and then some way of detecting if the mouse is in that shape. I ... ya I really have no clue how to start this. Some pointers on getting started would be hawt, and I'll post my cde/setup once I've got something down. Thanks in advance! Also I'd want to highlight the borders of the territory so I'm guessing some kind of physical shape will be needed. Maybe not.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Quite interesting topic.
    My first attempts would be:

    Either:
    Use the sphere for the territory selection, then try to take a shader with an additional texture slot (e.g. detail texture) and assign a texture with the outlines of the currently selected border to it, depending on the current selection. No idea of how to track exactly which territory the mouse will be hovering over though, in this case i would probably make it easy and use some kind of approximation or get some information from the texture in order to find the correct territory.

    Or:
    Try to use a more complex (modelled) mesh and not a standard sphere. If you split the planet into pieces and generate mesh colliders for each territory, you could determine the terrortiy that your mouse is hovering over via raycasts. The outlines could be done the same way OR using a rimlight shader. But I'm not really sure if that would work very well, it may be worth a try anyway.

    There are probably even better solutions, but these are the first that came to my mind.
     
  3. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Oh nice. Thank you for the response. The complex sphere object idea hadn't crossed my mind at all. That's a good idea. Well, I THINK it's a good idea. Who knows, haha. I look forward to additional responses.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Okay, forget about that mesh thingy. That will probably be total overkill as it's just too much.

    I just did some tests and you can do everything with texture information, no complicated math involved.

    You can use your map texture and assign it to the mesh. Raycast from mouse to world and if you hit the map-mesh, get its renderers main texture, ( the size of that texture is actually everything you need from it for a simple calculation).
    You can then get the texture coordinates from the spot that your raycast hit, those are normalized.
    As a next step, multiply them by the texture size. Now you got the pixel of the texture that you need for the following step.

    Now you'll need a reference texture, kinda like a look-up texture which will hold solid color information for every territory. With the pixel information you can find the color of the reference texture at the exact same pixel. Take it, run some checks and assign the appropriate overlay/detail texture to the material in order to highlight the borders.

    *Edit I used the example here as a quick guide. That's not exactly what you want to do, but it's very easy to adjust the script for the method i posted.
     
    Last edited: Sep 25, 2014