B
B
BadCats2018-02-13 13:53:00
C++ / C#
BadCats, 2018-02-13 13:53:00

Unity3d GetPixels not changing texture?

There is a following situation: in Unity - I have a certain material containing a certain texture. There is the following task: when scrolling with the mouse over a game object with this material, draw lines on the object - that is, as in Paint - but in realTime and on any object on the stage. To do this, I added a script containing the Draw method to the object:

public void Draw()

{

    Vector2 MouseVector = new Vector2(OnMouseUp(),OnMouseDown());
   Vector2 ImageVector = new Vector2( MainSHDtexture.width,MainSHDtexture.height);
    for (int i = 0; i < ImageVector.magnitude; i++)
    {
        if (MouseVector.x<=ImageVector.x && MouseVector.y <= ImageVector.x) 
        {
            int K = System.Convert.ToInt32(MouseVector.y);
            int L = System.Convert.ToInt32(MouseVector.x);
            for (K++; i < ImageVector.x; i++)
            {
                for (L++; i < ImageVector.y; i++)
                {

                    Texture2D tx = (Texture2D)MainSHDtexture;
                    tx.SetPixel(K , L , Color.white);
                    MainSHDtexture = tx;
                }

            }

        }
        else if (MouseVector.y >= ImageVector.x ) 
        {
            int K = System.Convert.ToInt32(MouseVector.y);
            for (K++; i < ImageVector.y; i++)
            {
                Texture2D tx = (Texture2D)MainSHDtexture;
                tx.SetPixel(K , System.Convert.ToInt32( ImageVector.x), Color.white);
                MainSHDtexture = tx;
            }
        }

        else if (MouseVector.x <= ImageVector.y)
        {
            int K = System.Convert.ToInt32(MouseVector.x);
            for (K++; i < ImageVector.y; i++)
            {
                Texture2D tx = (Texture2D)MainSHDtexture;
                tx.SetPixel(K , System.Convert.ToInt32(ImageVector.y) , Color.white);
                MainSHDtexture = tx;
            }
        }
    }

}

The idea of ​​the script in the following is somewhat similar to IntervalTree - let the mouse movements can be set as a vector - where it starts with a button press, and its end is, respectively, the case when we release the mouse button.
Accordingly, in OnMouseUp() and OnMouseDown() - I return the mouse coordinates X and Y
In the same way, you can set the texture as a vector - starting from the width - ending with the height - you get one segment of the total length.
That is, the task itself is to replace the pixels from the ImageVector vector with all pixels of the MouseVector length. I'm assuming that these vectors do not intersect, but are parallel, and MouseVector - lies "above" ImageVector - and the projection of MouseVector is the same number of pixels needed for replacement.
Next, I consider three cases - the full projection of the MouseVector into the ImageVector , the projection of only the head of the MouseVector into the beginning of the ImageVector , and the projection of the tail of the MouseVector into the ImageVector .
I also want to note that K ++ is made from hopelessness - because only increment, decrement and creation of a new object are allowed.
Studio and Unity do not give errors, but the script does not perform the task - that is, it does not set the necessary pixels of the texture to the desired color.
I also looked under Debug using breakpoints - when executed, the script enters the second if block (the one that is else if).
Writing / reading in the properties of the texture - when importing - allowed.
What is the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2018-02-13
@BadCats

This is how you distort the texture. The easiest way is to shove it into the shader. You select the width of the strip, and pass it to the shader in which place it will be placed. Type shader with this logic:

sampler2D _MainTex;
half _LinePos;
half _LineHeight;

fixed4 frag (v2f i) : SV_Target
{
  fixed4 col = tex2D(_MainTex, i.uv);
  float b = i.uv.x < _LinePos || i.uv.x > _LinePos + _LineHeight;

  col.rgb = col.rgb * b;
  return col;
}

D
Daniil Basmanov, 2018-02-13
@BasmanovDaniil

Poorly read the documentation , after SetPixel you need to call Texture2D.Apply .
In general, it is better to draw with decals or particles, drawing by texture works very slowly, see this tutorial as an example .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question