Search Unity

Is this possible to pick the color from a point through script ?

Discussion in 'Scripting' started by joxthebest314, Jun 17, 2020.

  1. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    I would like to get the color from a specific part of the ground in order to make particles the same color. The ground is a giant sprite, so I was wondering is there is a way to pick the color in a specific point of a sprite and then paste it to particles.

    If anyone know, I would really appreciate help.
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    you can use texture2d.getpixel
     
    joxthebest314 likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    joxthebest314 likes this.
  4. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95

    Thanks a lot, I will manage to make it work !
     
  5. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    I see that the textureCoord needs a mesh collider to work... I assume there is no other way to do it, because none of my scene objects or background have this type of collider.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    As a disclaimer, i dont really work with sprites. So this may be a horrible, or not even working way to achieve your goal. However, i assume that a bounding box would pretty accurately represent the boundries of your sprite. So for any given world position, you could check where it is relative to the bounding box, or rather its origin. When representing this as a percentage value for height and width, we could then multiply the height and width of the sprite image with these values and cast the result to an int, which would approximately represent the height and width inputs for GetPixel to return the pixel under the given world position.

    Example:
    You have a 100m*300m ground plane. Your sprite has a resolution of 128*128. The position we are trying to get the corresponding pixel for is at (10m,50m) relative to the bounding box origin. That is roughly 10% and 16.67% of its size, or (0.1, 0.1667). We can now multiply the texture size with these values to get (12.8, 21.33). Casting (or probably better: rounding) the values to integers we would get something like (13,21), which should approximately be the correct pixel location on the sprite.

    You'd have to do some testing for how well it works tho.. and i cant exactly vouch for this approach as i just made it up. Again, i dont really work with sprites, so maybe there is some easy way as well.
     
    joxthebest314 likes this.
  7. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    I forgot to mention that there is more than one sprite... I will show it with an image to be 100% clear.

    Eai9hfCXkAIXM3R.jpg

    For example, I want to get the pixels from the ground and the plants, wich are different sprites and they don't even have colliders, because we are in top-down view...

    The final objective is to have small particles corresponding to what the tank is rolling over.
    Your method seems interessant, but I don't think this is possible to do with multiple sprites.

    I know what a ask might be impossible or very hard, but I maybe someone has got an idea.
     
  8. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    what you need to do is calculate which pixel of the sprite your tank is on.
    Its very easy to do and you dont need colliders or other functions only math

    FOR EXAMPLE:

    if your sprites were 1 pixel per unit

    your tank on position x=16, y=16 would mean that you are on pixel (16,16) on the sprite

    then you would use texture2d.getpixel(16,16), and this would give you the color of the pixel on the sprite

    this means that if you have other pixels per unit, for example 100 pixels per unit on your sprite

    if your tank is on position 100x100 your pixel is (100/100, 100/100) which means you are on pixel (1,1)

    so the formula is texture2d.getpixel(worldposition.x/pixelsperunit , worldposition.y/pixelsperunit)

    For this logic to work you should make sure that your background sprite bottom left corner is on (0,0,0) world position

    though you could add offsets if you wanted other positions, this is just to show you the logic
     
    Last edited: Jun 17, 2020
    joxthebest314 likes this.
  9. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    Yup this could work fine for the background, but what do I do with the other layers of sprites, like the water or the plants (wich are separate sprites) ? This seems a little to complicated to calculate for every sprites, because there is dozen of them and this in only one of 20 maps hahaha.
     
  10. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    you can put them on a dictionary that you lookup based on your position.

    you can also use a collider to detect the sprite.

    then the formula is texture2d.getpixel( (tankworldpositon.x - spritebotomleftcornerworldposition.x)/pixelsperunit, (tankworldpositon.y - spritebotomleftcornerworldposition.y)/pixelsperunit)