Search Unity

Pixel color test

Discussion in 'Shaders' started by ScottJ88, May 22, 2017.

  1. ScottJ88

    ScottJ88

    Joined:
    Aug 28, 2012
    Posts:
    47
    I have an idea with regards to how a shader would work, I am unsure if it's possible to do, hopefully someone can advise and point me in the correct direction.

    Part A - the reason
    the whole point of this shader is to replace physics triggers - my thought is then it would be less cpu intensive and be relatively inexpensive overall. - if it works the way I think (hope) it would

    Part B - the idea
    The plan is to have 2 textures, 1 large texture and 1 small and multiply them together, the large would be the areas that are triggered on the level's map and the small would be of the character moving updating per frame to represent its location on the map, then this is the part I'm unsure if its possible or not.
    Do a per pixel test - of the small texture - to determine if any pixels are white - if so then put an output in the inspector, or maybe something like the first pixel on the large texture to indicate the player has entered an area and then can trigger an event.

    This would effectively act like a 2d collision trigger - all done on the gpu, using 1 bit channel (from my research this would be only possible by using an alpha channel of other textures) and potentially be quicker with lots of small areas being tested in one simple shader appose to separate ones on the cpu - even with collision trees- etc

    I am not overly confident in my abilities for shader code - I know how to multiply stuff and also update location by using the X,Y offset values.

    the complicated part is running a test on the pixels (would only need to test a set range for the small texture as there would never be anywhere else that turns white with the multiply) and then outputting this to either the inspector - to test per frame - be it just an int or some other means - or to add a white pixel in a specific place to then use texture2d.GetPixel.

    Is this just some wishfull thinking, would it work and would it be faster? - this would potentially remove the limit of the axis constraint for the 2d physics too.