void Painter() { if (State != 1) { State = 1; } Event e = Event.current; if (e.type == EventType.KeyDown && e.keyCode == KeyCode.T) { if (T4MSC.T4MActived != "Activated") { T4MSC.T4MActived = "Activated"; } else { T4MSC.T4MActived = "Deactivated"; } } if (T4MSC.T4MPreview && T4MSC.T4MActived == "Activated" && T4MSC.T4MPreview.enabled == false || T4MSC.T4MPreview.enabled == false) { if ( T4MSC.PaintPrev != T4MSC.PaintHandle.Follow_Normal_Circle && T4MSC.PaintPrev != T4MSC.PaintHandle.Follow_Normal_WireCircle && T4MSC.PaintPrev != T4MSC.PaintHandle.Hide_preview ) { T4MSC.T4MPreview.enabled = true; } } else if (T4MSC.T4MPreview && T4MSC.T4MActived == "Deactivated" && T4MSC.T4MPreview.enabled == true || T4MSC.T4MPreview.enabled == true) { if (T4MSC.PaintPrev != T4MSC.PaintHandle.Classic) { T4MSC.T4MPreview.enabled = false; } } if (T4MSC.T4MActived == "Activated") { HandleUtility.AddDefaultControl(0); RaycastHit raycastHit = new RaycastHit(); Ray terrain = HandleUtility.GUIPointToWorldRay(e.mousePosition); if (e.type == EventType.KeyDown && e.keyCode == KeyCode.KeypadPlus) { T4MSC.brushSize += 1; } else if (e.type == EventType.KeyDown && e.keyCode == KeyCode.KeypadMinus) { T4MSC.brushSize -= 1; } if (Physics.Raycast(terrain, out raycastHit, Mathf.Infinity, layerMask)) { if (T4MSC.CurrentSelect.gameObject.GetComponent <T4MObjSC>().ConvertType != "UT") { T4MSC.T4MPreview.transform.localEulerAngles = new Vector3(90, 180 + T4MSC.CurrentSelect.localEulerAngles.y, 0); } else { T4MSC.T4MPreview.transform.localEulerAngles = new Vector3(90, T4MSC.CurrentSelect.localEulerAngles.y, 0); } T4MSC.T4MPreview.transform.position = raycastHit.point; if (T4MSC.PaintPrev != T4MSC.PaintHandle.Classic && T4MSC.PaintPrev != T4MSC.PaintHandle.Hide_preview && T4MSC.PaintPrev != T4MSC.PaintHandle.Follow_Normal_WireCircle) { Handles.color = new Color(1f, 1f, 0f, 0.05f); Handles.DrawSolidDisc(raycastHit.point, raycastHit.normal, T4MSC.T4MPreview.orthographicSize * 0.9f); } else if (T4MSC.PaintPrev != T4MSC.PaintHandle.Classic && T4MSC.PaintPrev != T4MSC.PaintHandle.Hide_preview && T4MSC.PaintPrev != T4MSC.PaintHandle.Follow_Normal_Circle) { Handles.color = new Color(1f, 1f, 0f, 1f); Handles.DrawWireDisc(raycastHit.point, raycastHit.normal, T4MSC.T4MPreview.orthographicSize * 0.9f); } if ((e.type == EventType.mouseDrag && e.alt == false && e.shift == false && e.button == 0) || (e.shift == false && e.alt == false && e.button == 0 && ToggleF == false)) { Vector2 pixelUV = raycastHit.textureCoord * T4MSC.T4MMaskTexUVCoord; //0.14f; int PuX = Mathf.FloorToInt(pixelUV.x * T4MSC.T4MMaskTex.width); int PuY = Mathf.FloorToInt(pixelUV.y * T4MSC.T4MMaskTex.height); int x = Mathf.Clamp(PuX - T4MSC.T4MBrushSizeInPourcent / 2, 0, T4MSC.T4MMaskTex.width - 1); int y = Mathf.Clamp(PuY - T4MSC.T4MBrushSizeInPourcent / 2, 0, T4MSC.T4MMaskTex.height - 1); int width = Mathf.Clamp((PuX + T4MSC.T4MBrushSizeInPourcent / 2), 0, T4MSC.T4MMaskTex.width) - x; int height = Mathf.Clamp((PuY + T4MSC.T4MBrushSizeInPourcent / 2), 0, T4MSC.T4MMaskTex.height) - y; Color[] terrainBay = T4MSC.T4MMaskTex.GetPixels(x, y, width, height, 0); if (T4MSC.T4MMaskTex2) { terrainBay2 = T4MSC.T4MMaskTex2.GetPixels(x, y, width, height, 0); } for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int index = (i * width) + j; float Stronger = T4MSC.T4MBrushAlpha[Mathf.Clamp((y + i) - (PuY - T4MSC.T4MBrushSizeInPourcent / 2), 0, T4MSC.T4MBrushSizeInPourcent - 1) * T4MSC.T4MBrushSizeInPourcent + Mathf.Clamp((x + j) - (PuX - T4MSC.T4MBrushSizeInPourcent / 2), 0, T4MSC.T4MBrushSizeInPourcent - 1)] * T4MSC.T4MStronger; if (T4MSC.T4MselTexture < 3) { terrainBay[index] = Color.Lerp(terrainBay[index], T4MSC.T4MtargetColor, Stronger); } else { terrainBay[index] = Color.Lerp(terrainBay[index], T4MSC.T4MtargetColor, Stronger); //*0.3f); if (T4MSC.T4MMaskTex2) { terrainBay2[index] = Color.Lerp(terrainBay2[index], T4MSC.T4MtargetColor2, Stronger); ///0.3f); } } } } T4MSC.T4MMaskTex.SetPixels(x, y, width, height, terrainBay, 0); T4MSC.T4MMaskTex.Apply(); if (T4MSC.T4MMaskTex2) { T4MSC.T4MMaskTex2.SetPixels(x, y, width, height, terrainBay2, 0); T4MSC.T4MMaskTex2.Apply(); UndoObj = new Texture2D[2]; UndoObj[0] = T4MSC.T4MMaskTex; UndoObj[1] = T4MSC.T4MMaskTex2; } else { UndoObj = new Texture2D[1]; UndoObj[0] = T4MSC.T4MMaskTex; } //Undo.RecordObjects(UndoObj, "T4MMask"); //Unity don't work correctly with this for now ToggleF = true; } else if (e.type == EventType.mouseUp && e.alt == false && e.button == 0 && ToggleF == true) { T4MSC.SaveTexture(); ToggleF = false; } } } }
//绘制纹理 void Painter() { if (State != 1) { State = 1; } Event e = Event.current; if (e.type == EventType.KeyDown && e.keyCode == KeyCode.T) { if (T4MSC.T4MActived != "Activated") { T4MSC.T4MActived = "Activated"; } else { T4MSC.T4MActived = "Deactivated"; } } if (T4MSC.T4MPreview && T4MSC.T4MActived == "Activated" && T4MSC.T4MPreview.enabled == false || T4MSC.T4MPreview.enabled == false) { if ( T4MSC.PaintPrev != T4MSC.PaintHandle.Follow_Normal_Circle && T4MSC.PaintPrev != T4MSC.PaintHandle.Follow_Normal_WireCircle && T4MSC.PaintPrev != T4MSC.PaintHandle.Hide_preview ) { Debug.Log("Project true"); T4MSC.T4MPreview.enabled = true; } } else if (T4MSC.T4MPreview && T4MSC.T4MActived == "Deactivated" && T4MSC.T4MPreview.enabled == true || T4MSC.T4MPreview.enabled == true) { if (T4MSC.PaintPrev != T4MSC.PaintHandle.Classic) { Debug.Log("Project false"); T4MSC.T4MPreview.enabled = false; } } if (T4MSC.T4MActived == "Activated") { HandleUtility.AddDefaultControl(0); RaycastHit raycastHit = new RaycastHit(); Ray terrain = HandleUtility.GUIPointToWorldRay(e.mousePosition); if (e.type == EventType.KeyDown && e.keyCode == KeyCode.KeypadPlus) { T4MSC.brushSize += 1; } else if (e.type == EventType.KeyDown && e.keyCode == KeyCode.KeypadMinus) { T4MSC.brushSize -= 1; } string currentSelect = Selection.transforms[0].name; if (Physics.Raycast(terrain, out raycastHit, Mathf.Infinity, layerMask) && raycastHit.collider.name == currentSelect) { //如果类型是ut说明是从Terrain转化过来的 if (T4MSC.CurrentSelect.gameObject.GetComponent <T4MObjSC>().ConvertType != "UT") { T4MSC.T4MPreview.transform.localEulerAngles = new Vector3(90, 180 + T4MSC.CurrentSelect.localEulerAngles.y, 0); } else { T4MSC.T4MPreview.transform.localEulerAngles = new Vector3(90, T4MSC.CurrentSelect.localEulerAngles.y, 0); } T4MSC.T4MPreview.transform.position = raycastHit.point; //这里不稍微加一点在2019中无法在平面上显示出来 T4MSC.T4MPreview.transform.position += new Vector3(0, 1, 0); //这段是显示画笔用的 // 三种画笔形式 if (T4MSC.PaintPrev == T4MSC.PaintHandle.Follow_Normal_Circle) { Handles.color = new Color(1f, 1f, 0f, 0.05f); Handles.DrawSolidDisc(raycastHit.point, raycastHit.normal, T4MSC.T4MPreview.orthographicSize * 0.9f); //Debug.Log("Follow_Normal_Circle" + T4MSC.T4MPreview.transform.position + "and" + raycastHit.point); } else if (T4MSC.PaintPrev == T4MSC.PaintHandle.Follow_Normal_WireCircle) { Handles.color = new Color(1f, 1f, 0f, 1f); Handles.DrawWireDisc(raycastHit.point, raycastHit.normal, T4MSC.T4MPreview.orthographicSize * 0.9f); //Debug.Log("Follow_Normal_WireCircle" + T4MSC.T4MPreview.transform.position + "and" + raycastHit.point); } // Debug.Log(e.type); // if(e.type == EventType.MouseDown && UndoObj!=null&&!saveflag) // { // // var path = "Assets/testimg/tt.png"; // // var bytes = UndoObj[0].EncodeToPNG (); // saveflag = true; // Undo.RecordObjects(UndoObj, "T4MMask"); // // File.WriteAllBytes (path, bytes); // Debug.Log("!!"); // } if (e.type == EventType.MouseUp) { saveflag = false; } if ((e.type == EventType.MouseDrag && e.alt == false && e.shift == false && e.button == 0) || (e.type != EventType.MouseMove && e.shift == false && e.alt == false && e.isMouse && e.button == 0 && ToggleF == false)) { Vector2 pixelUV = raycastHit.textureCoord * T4MSC.T4MMaskTexUVCoord; //0.14f; //转成图片上的坐标 int PuX = Mathf.FloorToInt(pixelUV.x * T4MSC.T4MMaskTex.width); int PuY = Mathf.FloorToInt(pixelUV.y * T4MSC.T4MMaskTex.height); //T4MBrushSizeInPourcent是将笔刷大小转化成 1:(T4MMaskTex.width/100) 的大小 int x = Mathf.Clamp(PuX - T4MSC.T4MBrushSizeInPourcent / 2, 0, T4MSC.T4MMaskTex.width - 1); int y = Mathf.Clamp(PuY - T4MSC.T4MBrushSizeInPourcent / 2, 0, T4MSC.T4MMaskTex.height - 1); int width = Mathf.Clamp((PuX + T4MSC.T4MBrushSizeInPourcent / 2), 0, T4MSC.T4MMaskTex.width) - x; int height = Mathf.Clamp((PuY + T4MSC.T4MBrushSizeInPourcent / 2), 0, T4MSC.T4MMaskTex.height) - y; //读取选中区块的颜色 Color[] terrainBay = T4MSC.T4MMaskTex.GetPixels(x, y, width, height, 0); //如果有两张权重图 if (T4MSC.T4MMaskTex2) { terrainBay2 = T4MSC.T4MMaskTex2.GetPixels(x, y, width, height, 0); } for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int index = (i * width) + j; //获取笔刷的a通道值 float Stronger = T4MSC.T4MBrushAlpha[Mathf.Clamp((y + i) - (PuY - T4MSC.T4MBrushSizeInPourcent / 2), 0, T4MSC.T4MBrushSizeInPourcent - 1) * T4MSC.T4MBrushSizeInPourcent + Mathf.Clamp((x + j) - (PuX - T4MSC.T4MBrushSizeInPourcent / 2), 0, T4MSC.T4MBrushSizeInPourcent - 1)] * T4MSC.T4MStronger; terrainBay[index] = Color.Lerp(terrainBay[index], T4MSC.T4MtargetColor, Stronger); //*0.3f); if (T4MSC.T4MMaskTex2) { terrainBay2[index] = Color.Lerp(terrainBay2[index], T4MSC.T4MtargetColor2, Stronger); ///0.3f); } } } if (T4MSC.T4MMaskTex2) { T4MSC.T4MMaskTex2.SetPixels(x, y, width, height, terrainBay2, 0); T4MSC.T4MMaskTex2.Apply(); } //这里修改的是Mask贴图也就是权重图,画面上的效果是通过shader读取权重图来显示的 // if(UndoObj == null) // { // UndoObj = new Texture2D[2]; // if(T4MSC.T4MMaskTex2){ // UndoObj[0] = T4MSC.T4MMaskTex; // UndoObj[1] = T4MSC.T4MMaskTex2; // T4MSC.T4MMaskTex2.SetPixels(x, y, width,height, terrainBay2, 0); // T4MSC.T4MMaskTex2.Apply(); // }else{ // UndoObj[0] = T4MSC.T4MMaskTex; // } // } T4MSC.T4MMaskTex.SetPixels(x, y, width, height, terrainBay, 0); T4MSC.T4MMaskTex.Apply(); //Unity don't work correctly with this for now //Undo.RecordObjects(UndoObj, "T4MMask"); ToggleF = true; } else if (e.type == EventType.MouseUp && e.alt == false && e.button == 0 && ToggleF == true) { T4MSC.SaveTexture(); ToggleF = false; } } //如果不鼠标没有在范围内 else { DestroyImmediate(T4MSC.T4MPreview); } } }