- Home /
Artifacts in custom texture painter
Hi all, I managed to create a texture paint script, but I encounterd a problem modifying the pixels of the texture with a AnimationCurve :
Its seems like there's a problem with depth precision, altough I think the error is related to this piece of code :
currHeight += brushHeight * speed;
where I modify the current pixel color adding brushHeight, which is just the evaluation of the AnimationCurve at a given distance from the hit pixel
PS : Note that I am using a 32 bit texture
Answer by Bunny83 · Sep 16, 2015 at 09:44 AM
If by artefacts you refer are those rings within each circle, it looks like you somehow round or trucate your "distance" value. This can happen when you store the distance in an int variable. You have to provide more information to get a more precise answer.
How does your animation curve look like? maybe a screenshot?
What does your exact code look like?
You said that you "modify the current pixel color". How do you modify the color? Do you work with SetPixel or with an array and SetPixels?
You talked about a "hit point", so i guess you use some sort of raycasting. Physics Raycast, Collider.Raycast or Plane.Raycast? Are you sure you do the coordinate conversion right? Again, code would help to understand.
Is this a runtime script or an editor script? If it's an editor script, do you use a custom EditorWindow or have you implemented it in the sceneview (like the terrain editor)?
edit
From the code in your comment below it seems you get / set each pixel seperately each step. The problem here is that each color component is stored internally in a byte, not a float.
So by reading, modifing and writing it again the actual value get clamped to the nearest byte value. When you use a temp "Color[]" which you only read once in the beginning the problem should go away.
@Bunny83 Thanks for the reply, I'm sorry for not being clear on the issue. However here're the answer to your points :
$$anonymous$$y animationCurve is one of the presets, except for the fact that it is decreasing. However I noticed that this problem persists even if I change the curve
I am working with setpixel right now, even if I plan to change the code to setpixels for better performance
I perform a raycast form my mouse coordinates on the screen, then I store all data about the hit, I get the texture coordinates from the uv coordinates of the hit renderer by multiplying it by my texture size (I am sure about this part)
It is a editor script
For all pixels near the hit point I eveluate the distance with Vector2.distance, then I get the brush height by using AnimationCurve.evaluate.
Here's a sample of the code
//Calculate distance from hit point and current point
dist = Vector2.Distance(new Vector2(pixelUV.x - i, pixelUV.y - j), pixelUV);
//Evaluate brushHeight
brushHeight = brushFalloff.Evaluate(dist / brushSize);
//If we press shift decrease, otherwise increase
if (Event.current.shift)
currHeight -= brushHeight * brushSpeed;
else
currHeight += brushHeight * brushSpeed;
//Set pixel in our texture
heightmap.SetPixel((int)pixelUV.x + i, (int)pixelUV.y + j, new Color(currHeight, currHeight, currHeight));
I noticed this problem occours when my brushSpeed is pretty low (about 0.01). According to me this seems to be caused by the summation of the small errors of the brush height float
Sorry for the late response, i was sick...
So are your variables "dist" and "brushSize" also float variables ?
What's the range of your for loop variables? It should be "-brushSize" to "brushSize".
I guess i know what the problem is. I'll edit my answer
Your answer
Follow this Question
Related Questions
Wearied Error 1 Answer
Assigning UV Map to model at runtime 0 Answers
How to see which material is using a texure as normal map? 1 Answer
Paint a texture dynamically? 0 Answers
Prefabs made in Unity are insanely slow, and don't show textures. 0 Answers