private static void HandleNodeActionEnd(EditorInputInfo inputInfo) { CombinMeshEditor cme = inputInfo.editorState.editor as CombinMeshEditor; if (inputInfo.editorState.action == EditorState.Action.None && inputInfo.inputEvent.button == 0) { for (int i = 0; i < cme.nodeList.Count; ++i) { CombinNode cn = cme.nodeList[i]; if (cn.drawRect.Contains(inputInfo.inputPos)) { cme.focusedNode = cn; break; } } } if (inputInfo.editorState.action == EditorState.Action.Move && cme.focusedNode != null) { CombinNode cn = cme.focusedNode; cn.rect.x += inputInfo.editorState.dragOffset.x * inputInfo.editorState.zoom; cn.rect.y += inputInfo.editorState.dragOffset.y * inputInfo.editorState.zoom; cn.offset = Vector2.zero; inputInfo.editorState.dragOffset = Vector2.zero; } if (inputInfo.editorState.action == EditorState.Action.Scale && cme.focusedNode != null) { CombinNode cn = cme.focusedNode; Rect rect = cn.drawRect; float zoom = inputInfo.editorState.zoom; if (cme.powerOfTwo) { cn.rect.width = EditorUtil.ToNearPowerOfTwo((int)(rect.width * zoom)); cn.rect.height = EditorUtil.ToNearPowerOfTwo((int)(rect.height * zoom)); } else { cn.rect.width = rect.width * zoom; cn.rect.height = rect.height * zoom; } cn.rect.x = rect.x * zoom - inputInfo.editorState.panOffset.x; cn.rect.y = rect.y * zoom - inputInfo.editorState.panOffset.y; cn.scale = Vector2.zero; inputInfo.editorState.dragOffset = Vector2.zero; } inputInfo.editorState.action = EditorState.Action.None; }
private static void LeftRotNode(EditorInputInfo inputInfo) { CombinMeshEditor cme = inputInfo.editorState.editor as CombinMeshEditor; if (cme.focusedNode != null) { cme.focusedNode.rotation -= 90; inputInfo.inputEvent.Use(); } }
public static void CombinTexture(CombinMeshEditor cme, getTexture d, string textureName) { using (new GameViewSize()) { for (int i = 0; i < cme.nodeList.Count; ++i) { Material mat = cme.nodeList[i].bakeMat; mat.shader = Shader.Find("Unlit/Texture"); mat.mainTexture = d(i); } int size = 2048; RenderTexture rt = RenderTexture.GetTemporary(size, size, 32, RenderTextureFormat.ARGB32); rt.filterMode = FilterMode.Point; camera.targetTexture = rt; camera.Render(); int width = cme.combinTextureWidth;; int height = cme.combinTextureHeight; Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false); RenderTexture.active = rt; texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0); for (int i = 0; i < cme.nodeList.Count; ++i) { Material mat = cme.nodeList[i].bakedObject.GetComponent <Renderer>().sharedMaterial; mat.shader = Shader.Find("Unlit/BakeAlpha"); } camera.Render(); Texture2D textureAlpha = new Texture2D(width, height, TextureFormat.ARGB32, false); textureAlpha.ReadPixels(new Rect(0, 0, width, height), 0, 0); textureAlpha.Apply(); for (int w = 0; w < width; ++w) { for (int h = 0; h < height; ++h) { Color color = texture2D.GetPixel(w, h); Color alphaColor = textureAlpha.GetPixel(w, h); color.a = alphaColor.r; texture2D.SetPixel(w, h, color); } } texture2D.Apply(); EditorUtil.ExportMesh(cme); RenderTexture.active = null; Texture firstTexture = cme.nodeList[0].texture; string fileName = cme.nodeList[0].path + "/" + textureName + ".png"; File.WriteAllBytes(fileName, texture2D.EncodeToPNG()); } }
private static void HandleNodeAction(EditorInputInfo inputInfo) { CombinMeshEditor cme = inputInfo.editorState.editor as CombinMeshEditor; if (inputInfo.editorState.action == EditorState.Action.None || cme.focusedNode == null) { return; } inputInfo.editorState.dragOffset = inputInfo.inputPos - inputInfo.editorState.dragMouseStart; if (inputInfo.editorState.action == EditorState.Action.Move) { CombinNode cn = cme.focusedNode; cn.offset = inputInfo.editorState.dragOffset; } if (inputInfo.editorState.action == EditorState.Action.Scale) { CombinNode cn = cme.focusedNode; Vector2 offset = inputInfo.editorState.dragOffset; if (inputInfo.inputEvent.shift) { switch (inputInfo.editorState.pivot) { case EditorState.Pivot.LeftTop: { float max = Mathf.Max(-offset.x, -offset.y); offset = new Vector2(-max, -max); }; break; case EditorState.Pivot.LeftBottom: { float max = Mathf.Max(-offset.x, offset.y); offset = new Vector2(-max, max); }; break; case EditorState.Pivot.RightTop: { float max = Mathf.Max(offset.x, -offset.y); offset = new Vector2(max, -max); }; break; case EditorState.Pivot.RightBottom: { float max = Mathf.Max(offset.x, offset.y); offset = new Vector2(max, max); }; break; } } cn.scale = offset; } }
// 处理UV并导出模型 public static void ExportMesh(CombinMeshEditor cme) { int w = cme.combinTextureWidth; int h = cme.combinTextureHeight; for (int i = 0; i < cme.nodeList.Count; ++i) { CombinNode ti = cme.nodeList[i]; Matrix2x2 matrix = Matrix2x2.identity.Clone(); matrix.Rotate(-ti.rotation * Mathf.PI / 180); for (int j = 0; j < ti.objects.Count; ++j) { Mesh mesh = ti.meshs[j]; string fileDir = Path.GetDirectoryName(AssetDatabase.GetAssetPath(mesh)); string fbxName = Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(mesh)) + "_combin.FBX"; Vector2[] uvBak = new Vector2[mesh.uv.Length]; Array.Copy(mesh.uv, uvBak, mesh.uv.Length); Vector2[] uv = new Vector2[mesh.uv.Length]; for (int mi = 0; mi < mesh.uv.Length; ++mi) { Vector2 u = Vector2.zero; Vector2 v = mesh.uv[mi]; v.x -= 0.5f; v.y -= 0.5f; v = matrix * v; v.x += 0.5f; v.y += 0.5f; u.x = v.x * ti.rect.width / w + ti.rect.x / w; u.y = v.y * ti.rect.height / h + (h - ti.rect.y - ti.rect.height) / h; uv[mi] = u; } mesh.uv = uv; var options = new ExportModelSettingsSerialize(); options.exportFormat = ExportSettings.ExportFormat.Binary; options.include = ExportSettings.Include.Model; options.objectPosition = ExportSettings.ObjectPosition.LocalCentered; options.exportUnrendered = true; ModelExporter.ExportObject(fileDir + "/" + fbxName, ti.objects[j], options); mesh.uv = uvBak; } } }
private static void HandleNodeActionStart(EditorInputInfo inputInfo) { CombinMeshEditor cme = inputInfo.editorState.editor as CombinMeshEditor; if (cme.focusedNode == null || inputInfo.inputEvent.button != 0) { return; } EditorState state = inputInfo.editorState; Vector2 pos = inputInfo.inputEvent.mousePosition; state.action = EditorState.Action.None; int pivotIndex = -1; for (int i = 0; i < 4; ++i) { if (cme.currentPivotRect[i].Contains(pos)) { pivotIndex = i; state.action = EditorState.Action.Scale; break; } } if (pivotIndex == -1) { if (cme.focusedNode.drawRect.Contains(pos)) { state.action = EditorState.Action.Move; } } if (state.action != EditorState.Action.None) { state.pivot = (EditorState.Pivot)(pivotIndex + 1); state.dragMouseStart = pos; state.dragOffset = Vector2.zero; } }
private static void HandleContextClicks(EditorInputInfo inputInfo) { CombinMeshEditor cme = inputInfo.editorState.editor as CombinMeshEditor; if (cme.focusedNode == null) { return; } if (Event.current.button == 1 && cme.focusedNode.drawRect.Contains(inputInfo.inputPos)) { GenericMenu contextMenu = new GenericMenu(); if (cme.focusedNode != null) { EditorInputSystem.FillContextMenu(inputInfo, contextMenu); } contextMenu.ShowAsContext(); Event.current.Use(); } }
private static void HandleKey(EditorInputInfo inputInfo) { CombinMeshEditor cme = inputInfo.editorState.editor as CombinMeshEditor; if (GUIUtility.keyboardControl > 0) { return; } EditorState state = inputInfo.editorState; if (cme.focusedNode != null) { Vector2 pos = Vector2.zero; if (inputInfo.inputEvent.keyCode == KeyCode.RightArrow) { pos += new Vector2(1, 0); } if (inputInfo.inputEvent.keyCode == KeyCode.LeftArrow) { pos += new Vector2(-1, 0); } if (inputInfo.inputEvent.keyCode == KeyCode.DownArrow) { pos += new Vector2(0, 1); } if (inputInfo.inputEvent.keyCode == KeyCode.UpArrow) { pos += new Vector2(0, -1); } cme.focusedNode.rect.x += pos.x * state.zoom; cme.focusedNode.rect.y += pos.y * state.zoom; inputInfo.inputEvent.Use(); } }
// 合并贴图 public static void BakeTexture(CombinMeshEditor cme) { if (cme.nodeList.Count == 0) { return; } CreateCameraThings(); GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad); quad.transform.parent = camera.transform; quad.transform.localPosition = new Vector3(0, 0, 4); quad.transform.localScale = Vector3.one * 2; float offset = 0.1f; for (int i = 0; i < cme.nodeList.Count; ++i) { offset += 0.1f; CreateMesh(camera.transform, cme.nodeList[i], offset); } CombinTexture(cme, (i) => { return(cme.nodeList[i].texture); }, "CombinTexture"); if (cme.hadNormal) { CombinTexture(cme, (i) => { return(cme.nodeList[i].textureNormal); }, "CombinNormalTexture"); } if (cme.hadSpecular) { CombinTexture(cme, (i) => { return(cme.nodeList[i].textureNormal); }, "CombinSpecularTexture"); } DestroyCameraThings(); EditorUtility.DisplayDialog("提示", "模型合并贴图处理完毕!", "确定"); AssetDatabase.Refresh(); }
public void Draw(CombinMeshEditor cme) { EditorState state = cme.editorState; float zoom = state.zoom; drawRect = new Rect(rect); drawRect.width /= zoom; drawRect.height /= zoom; drawRect.x = (drawRect.x + state.panOffset.x) / zoom + offset.x; drawRect.y = (drawRect.y + state.panOffset.y) / zoom + offset.y; if (cme.focusedNode == this && state.action == EditorState.Action.Scale) { if (cme.powerOfTwo) { if (state.pivot == EditorState.Pivot.LeftTop || state.pivot == EditorState.Pivot.LeftBottom) { scale.x = -1 * (EditorUtil.ToNearPowerOfTwo((int)((drawRect.width - scale.x) * zoom)) / zoom - drawRect.width); } else { scale.x = EditorUtil.ToNearPowerOfTwo((int)((drawRect.width + scale.x) * zoom)) / zoom - drawRect.width; } if (state.pivot == EditorState.Pivot.LeftTop || state.pivot == EditorState.Pivot.RightTop) { scale.y = -1 * (EditorUtil.ToNearPowerOfTwo((int)((drawRect.height - scale.y) * zoom)) / zoom - drawRect.height); } else { scale.y = EditorUtil.ToNearPowerOfTwo((int)((drawRect.height + scale.y) * zoom)) / zoom - drawRect.height; } lastScale = Vector2.Lerp(lastScale, scale, 0.1f); } else { lastScale = scale; } switch (state.pivot) { case EditorState.Pivot.LeftTop: { drawRect.width -= lastScale.x; drawRect.height -= lastScale.y; drawRect.x += lastScale.x; drawRect.y += lastScale.y; }; break; case EditorState.Pivot.LeftBottom: { drawRect.x += lastScale.x; drawRect.width -= lastScale.x; drawRect.height += lastScale.y; }; break; case EditorState.Pivot.RightTop: { drawRect.y += lastScale.y; drawRect.width += lastScale.x; drawRect.height -= lastScale.y; }; break; case EditorState.Pivot.RightBottom: { drawRect.width += lastScale.x; drawRect.height += lastScale.y; }; break; } } GUI.DrawTexture(drawRect, texture); }