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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Masking Instances Through a Texture Map

Discussion in 'Scripting' started by RussellT, Feb 23, 2015.

  1. RussellT

    RussellT

    Joined:
    Feb 1, 2015
    Posts:
    18
    Hi All,

    I have got a set of instances working well across the surface of an object, currently they are set to spawn on the vertices.

    What I would like to do is define their placement with a map. I.E Black = None. White = Spawn anywhere you want in this area. I've been looking into GetPixel & GetBilinear Pixel but can't see how I could make that work for now...

    Any help greatly appreciated!
    Cheers,
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    Loop through the pixels on your texture using GetPixels , if the colour is white spawn at that location by getting it's position as a percentage of the texture size and multiplying it by your landscape size.
     
  3. RussellT

    RussellT

    Joined:
    Feb 1, 2015
    Posts:
    18
    Hey Strategos thanks for replying - How am I passing in the position to place it on the object though? I am getting the texture from my object saying: gameObject.renderer.material.mainTexture; but that doesn't really relate to anything 3D space does it....

    Although maybe thats what you mean when you are talking about multiplying it by the landscape size?

    Cheers,
     
  4. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    If you look in the docs, their example is using the players position to find the matching point on the texture

    http://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html

    if you loop through each row of your texture each iteration will have an x and a y coordinate.

    For each white pixel take x and y and divide them by texture width and height respectively.

    Then multiply them by your landscape width and height , and you have a position on your landscape which equates to the white pixel on your texture.
     
  5. RussellT

    RussellT

    Joined:
    Feb 1, 2015
    Posts:
    18
    Hey Stategos,
    Thanks spent a while understanding it and I'm there now, But unless im missing something I don't think it will work for what I need...

    Imagine I had a tunnel through a mountain, I wanted to cover the mountain in grass so I would paint it white. But then I want nothing throughout the tunnel, So I paint it black....

    From what I have implemented from the Unity doc, this system wouldn't allow that?

    Cheers,