- Home /
make pencil just like the lab
i wanna make a marker just like they have in the steam game the lab can't find any tutorials online. on how to draw during runtime.
this is what i got right now but it doesn't draw smoothly.
public class ExampleClass : MonoBehaviour
{
private int Size = 2;
void Update()
{
Vector3 markerRange = transform.TransformDirection(Vector3.forward) * 0.1f;
RaycastHit hit;
if (Physics.Raycast(transform.position, markerRange ,out hit, 0.1f))
{
Renderer rend = hit.transform.GetComponent<Renderer>();
MeshCollider meshCollider = hit.collider as MeshCollider;
if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
return;
Texture2D tex = rend.material.mainTexture as Texture2D;
Vector2 pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
for (int i = 0; i < Size; i++)
{
int x = (int)pixelUV.x;
int y = (int)pixelUV.y;
//Increment the X and Y
x += i;
y += i;
//Apply
tex.SetPixel(x, y, Color.red);
//De-increment the X and Y
x = (int)pixelUV.x;
y = (int)pixelUV.y;
x -= i;
y -= i;
//Apply
tex.SetPixel(x, y, Color.red);
}
tex.Apply();
}
Debug.DrawRay(transform.position, markerRange, Color.blue);
}
}
any idea on how i could do it beter are welcome.
A reference to the steam game the "lab"
ezgifcom-optimize.gif
(265.2 kB)
Comment
Your answer
Follow this Question
Related Questions
drawing on texture during runtime 0 Answers
the "blocking object" is not work 0 Answers
Unity raycast only sometimes works 0 Answers
Change Transparency 1 Answer
Resources does load up in the editor but not in build 0 Answers