void DrawOnTexture(RaycastHit _hit) { int ID = 0; Renderer currentRend; ID = _hit.transform.gameObject.GetInstanceID(); currentRend = _hit.transform.GetComponent <Renderer>(); //Create a new Paint Texture if it doesn't exist if (!paintObjects.ContainsKey(ID)) { Paint paint = new Paint((int)mappingResolution, (int)mappingResolution, initColors); paintObjects.Add(ID, paint); } if (currentRend && paintObjects[ID] != null) { float size = activeBrush ? brushSize : eraserSize; if (oldSize != size) { oldSize = size; size *= ratio; //Set Temporary Texture to rescale TextureScale.CopyTexture(activeBrush ? brushTexture : eraserTexture, ref tempBrush); TextureScale.Bilinear(tempBrush, (int)(brushTexture.width * size), (int)(brushTexture.height * size)); halfWidth = (int)((brushTexture.width * 0.5f) * size); halfHeight = (int)((brushTexture.height * 0.5f) * size); } // local position on the lightmap texture int XLightMap = (int)((_hit.lightmapCoord.x) * paintObjects[ID].TmpPaint.width) - halfWidth; int YLightMap = (int)((_hit.lightmapCoord.y) * paintObjects[ID].TmpPaint.height) - halfHeight; for (int i = 0; i < tempBrush.width; i++) { for (int j = 0; j < tempBrush.height; j++) { float alpha = tempBrush.GetPixel(i, j).a; if (alpha > 0.0f) { int XPixel = i + XLightMap; int YPixel = j + YLightMap; //out of bounds texture if (XPixel >= 0 && XPixel < paintObjects[ID].TmpPaint.width - 1 && YPixel >= 0 && YPixel < paintObjects[ID].TmpPaint.height - 1) { //paint on the mask(white or black) brushColor.a = alpha; eraserColor.a = alpha; paintObjects[ID].TmpPaint.SetPixel(XPixel, YPixel, activeBrush ? brushColor : eraserColor); } } } } //Apply to the corresponding material MaterialPropertyBlock mbp = new MaterialPropertyBlock(); paintObjects[ID].TmpPaint.Apply(); mbp.SetTexture("_PaintTex", paintObjects[ID].TmpPaint); currentRend.SetPropertyBlock(mbp); } }