Answer the question
In order to leave comments, you need to log in
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;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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;
}
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 questionAsk a Question
731 491 924 answers to any question