Game Development Reference
In-Depth Information
052 if (!Physics.Raycast( Camera.main.ScreenPointToRay (
Input.mousePosition), out hit))
053 return;
054
055 //Get hit collider
056 Renderer renderer = hit.collider.renderer;
057 MeshCollider Collide = hit.collider as MeshCollider;
058 if (renderer == null || renderer.sharedMaterial == null
|| renderer.sharedMaterial.mainTexture == null || Collide ==
null)
059 return;
060
061 //Get UV Coords of hit surface
062 Vector2 pixelUV = hit.textureCoord;
063 pixelUV.x *= renderer.material.mainTexture.width;
064 pixelUV.y *= renderer.material.mainTexture.height;
065
066 //Update coords to texture middle (align brush texture
center to cursor)
067 pixelUV.x -= BrushTexture.width/2;
068 pixelUV.y -= BrushTexture.height/2;
069
070 //Clamp pixel values between 0 and width
071 pixelUV.x = Mathf.Clamp(pixelUV.x, 0,
renderer.material.mainTexture.width);
072 pixelUV.y = Mathf.Clamp(pixelUV.y, 0,
renderer.material.mainTexture.height);
073
074 //Paint onto destination texture
075 PaintSourceToDestTexture (BrushTexture,
renderer.material.mainTexture as Texture2D, (int)pixelUV.x,
(int)pixelUV.y);
076 }
077 }
078 //--------------------------------------------------
079 //Paint source text to destination
080 //Will paint a brush texture onto a destination texture
081 public static void PaintSourceToDestTexture(Texture2D
Source, Texture2D Dest, int Left, int Top)
082 {
083 //Get source pixels
084 Color[] SourcePixels = Source.GetPixels();
085
 
Search WWH ::




Custom Search