void BeginEdit() { var tmesh = GetTargetMesh(); if (tmesh == null) { Debug.LogWarning("Target mesh is null."); return; } else if (!tmesh.isReadable) { Debug.LogWarning("Target mesh is not readable."); return; } if (m_settings == null) { var ds = AssetDatabase.LoadAssetAtPath <NormalPainterSettings>(AssetDatabase.GUIDToAssetPath("f9fa1a75054c38b439daaed96bc5b424")); if (ds != null) { m_settings = Instantiate(ds); } if (m_settings == null) { m_settings = ScriptableObject.CreateInstance <NormalPainterSettings>(); } } if (m_meshPoint == null) { float l = 0.5f; var p = new Vector3[] { new Vector3(-l, -l, l), new Vector3(l, -l, l), new Vector3(l, -l, -l), new Vector3(-l, -l, -l), new Vector3(-l, l, l), new Vector3(l, l, l), new Vector3(l, l, -l), new Vector3(-l, l, -l), }; m_meshPoint = new Mesh(); m_meshPoint.vertices = new Vector3[] { p[0], p[1], p[2], p[3], p[7], p[4], p[0], p[3], p[4], p[5], p[1], p[0], p[6], p[7], p[3], p[2], p[5], p[6], p[2], p[1], p[7], p[6], p[5], p[4], }; m_meshPoint.SetIndices(new int[] { 3, 1, 0, 3, 2, 1, 7, 5, 4, 7, 6, 5, 11, 9, 8, 11, 10, 9, 15, 13, 12, 15, 14, 13, 19, 17, 16, 19, 18, 17, 23, 21, 20, 23, 22, 21, }, MeshTopology.Triangles, 0); m_meshPoint.UploadMeshData(false); } if (m_meshVector == null) { m_meshVector = new Mesh(); m_meshVector.vertices = new Vector3[2] { Vector3.zero, Vector3.zero }; m_meshVector.uv = new Vector2[2] { Vector2.zero, Vector2.one }; m_meshVector.SetIndices(new int[2] { 0, 1 }, MeshTopology.Lines, 0); m_meshVector.UploadMeshData(false); } if (m_meshLasso == null) { m_meshLasso = new Mesh(); } if (m_matVisualize == null) { m_matVisualize = new Material(AssetDatabase.LoadAssetAtPath <Shader>(AssetDatabase.GUIDToAssetPath("03871fa9be0375f4c91cb4842f15b890"))); } if (m_matOverlay == null) { m_matOverlay = new Material(AssetDatabase.LoadAssetAtPath <Shader>(AssetDatabase.GUIDToAssetPath("b531c1011d0464740aa59c2809bbcbb2"))); } if (m_matBake == null) { m_matBake = new Material(AssetDatabase.LoadAssetAtPath <Shader>(AssetDatabase.GUIDToAssetPath("4ddd0053dc720414b8afc76bf0a93f8e"))); } if (m_csBakeFromMap == null) { m_csBakeFromMap = AssetDatabase.LoadAssetAtPath <ComputeShader>(AssetDatabase.GUIDToAssetPath("f6687b99e1b6bfc4f854f46669e84e31")); } if (m_meshTarget == null || m_meshTarget != tmesh || (m_points != null && m_meshTarget.vertexCount != m_points.Count)) { m_meshTarget = tmesh; m_points = null; m_normals = null; m_normalsBase = null; m_tangents = null; m_indices = null; m_mirrorRelation = null; m_selection = null; ReleaseComputeBuffers(); } if (m_meshTarget != null) { m_points = new PinnedList <Vector3>(m_meshTarget.vertices); m_pointsPredeformed = m_points; m_uv = new PinnedList <Vector2>(m_meshTarget.uv); m_normals = new PinnedList <Vector3>(m_meshTarget.normals); if (m_normals.Count == 0) { m_meshTarget.RecalculateNormals(); m_normalsBase = m_normals = new PinnedList <Vector3>(m_meshTarget.normals); } else { m_meshTarget.RecalculateNormals(); m_normalsBase = new PinnedList <Vector3>(m_meshTarget.normals); m_meshTarget.normals = m_normals.Array; } m_normalsPredeformed = m_normals; m_normalsBasePredeformed = m_normalsBase; m_tangents = new PinnedList <Vector4>(m_meshTarget.tangents); if (m_tangents.Count == 0) { m_meshTarget.RecalculateTangents(); m_tangentsBase = m_tangents = new PinnedList <Vector4>(m_meshTarget.tangents); } else { m_meshTarget.RecalculateTangents(); m_tangentsBase = new PinnedList <Vector4>(m_meshTarget.tangents); m_meshTarget.tangents = m_tangents.Array; } m_tangentsPredeformed = m_tangents; m_tangentsBasePredeformed = m_tangentsBase; m_indices = new PinnedList <int>(m_meshTarget.triangles); m_selection = new PinnedList <float>(m_points.Count); m_npModelData.num_vertices = m_points.Count; m_npModelData.num_triangles = m_indices.Count / 3; m_npModelData.indices = m_indices; m_npModelData.vertices = m_points; m_npModelData.normals = m_normals; m_npModelData.tangents = m_tangents; m_npModelData.uv = m_uv; m_npModelData.selection = m_selection; var smr = GetComponent <SkinnedMeshRenderer>(); if (smr != null && smr.bones.Length > 0) { m_skinned = true; m_boneWeights = new PinnedList <BoneWeight>(m_meshTarget.boneWeights); m_bindposes = new PinnedList <Matrix4x4>(m_meshTarget.bindposes); m_boneMatrices = new PinnedList <Matrix4x4>(m_bindposes.Count); m_pointsPredeformed = m_points.Clone(); m_normalsPredeformed = m_normals.Clone(); m_normalsBasePredeformed = m_normalsBase.Clone(); m_tangentsPredeformed = m_tangents.Clone(); m_tangentsBasePredeformed = m_tangentsBase.Clone(); m_npSkinData.num_vertices = m_boneWeights.Count; m_npSkinData.num_bones = m_bindposes.Count; m_npSkinData.weights = m_boneWeights; m_npSkinData.bindposes = m_bindposes; m_npSkinData.bones = m_boneMatrices; } } if (m_cbPoints == null && m_points != null && m_points.Count > 0) { m_cbPoints = new ComputeBuffer(m_points.Count, 12); m_cbPoints.SetData(m_points.List); } if (m_cbNormals == null && m_normals != null && m_normals.Count > 0) { m_cbNormals = new ComputeBuffer(m_normals.Count, 12); m_cbNormals.SetData(m_normals.List); m_cbBaseNormals = new ComputeBuffer(m_normalsBase.Count, 12); m_cbBaseNormals.SetData(m_normalsBase.List); } if (m_cbTangents == null && m_tangents != null && m_tangents.Count > 0) { m_cbTangents = new ComputeBuffer(m_tangents.Count, 16); m_cbTangents.SetData(m_tangents.List); m_cbBaseTangents = new ComputeBuffer(m_tangentsBase.Count, 16); m_cbBaseTangents.SetData(m_tangentsBase.List); } if (m_cbSelection == null && m_selection != null && m_selection.Count > 0) { m_cbSelection = new ComputeBuffer(m_selection.Count, 4); m_cbSelection.SetData(m_selection.List); } if (m_cbArgPoints == null && m_points != null && m_points.Count > 0) { m_cbArgPoints = new ComputeBuffer(1, 5 * sizeof(uint), ComputeBufferType.IndirectArguments); m_cbArgPoints.SetData(new uint[5] { m_meshPoint.GetIndexCount(0), (uint)m_points.Count, 0, 0, 0 }); m_cbArgVectors = new ComputeBuffer(1, 5 * sizeof(uint), ComputeBufferType.IndirectArguments); m_cbArgVectors.SetData(new uint[5] { m_meshVector.GetIndexCount(0), (uint)m_points.Count, 0, 0, 0 }); } m_settings.InitializeBrushData(); UpdateTransform(); UpdateNormals(); PushUndo(); m_editing = true; }
void SetupResources() { if (m_settings == null) { var ds = AssetDatabase.LoadAssetAtPath <NormalPainterSettings>(AssetDatabase.GUIDToAssetPath("f9fa1a75054c38b439daaed96bc5b424")); if (ds != null) { m_settings = Instantiate(ds); } if (m_settings == null) { m_settings = ScriptableObject.CreateInstance <NormalPainterSettings>(); } } if (m_meshCube == null) { float l = 0.5f; var p = new Vector3[] { new Vector3(-l, -l, l), new Vector3(l, -l, l), new Vector3(l, -l, -l), new Vector3(-l, -l, -l), new Vector3(-l, l, l), new Vector3(l, l, l), new Vector3(l, l, -l), new Vector3(-l, l, -l), }; m_meshCube = new Mesh(); m_meshCube.vertices = new Vector3[] { p[0], p[1], p[2], p[3], p[7], p[4], p[0], p[3], p[4], p[5], p[1], p[0], p[6], p[7], p[3], p[2], p[5], p[6], p[2], p[1], p[7], p[6], p[5], p[4], }; m_meshCube.SetIndices(new int[] { 3, 1, 0, 3, 2, 1, 7, 5, 4, 7, 6, 5, 11, 9, 8, 11, 10, 9, 15, 13, 12, 15, 14, 13, 19, 17, 16, 19, 18, 17, 23, 21, 20, 23, 22, 21, }, MeshTopology.Triangles, 0); } if (m_meshLine == null) { m_meshLine = new Mesh(); m_meshLine.vertices = new Vector3[2] { Vector3.zero, Vector3.zero }; m_meshLine.uv = new Vector2[2] { Vector2.zero, Vector2.one }; m_meshLine.SetIndices(new int[2] { 0, 1 }, MeshTopology.Lines, 0); } if (m_meshLasso == null) { m_meshLasso = new Mesh(); } if (m_matVisualize == null) { m_matVisualize = new Material(AssetDatabase.LoadAssetAtPath <Shader>(AssetDatabase.GUIDToAssetPath("03871fa9be0375f4c91cb4842f15b890"))); } if (m_matBake == null) { m_matBake = new Material(AssetDatabase.LoadAssetAtPath <Shader>(AssetDatabase.GUIDToAssetPath("4ddd0053dc720414b8afc76bf0a93f8e"))); } if (m_csBakeFromMap == null) { m_csBakeFromMap = AssetDatabase.LoadAssetAtPath <ComputeShader>(AssetDatabase.GUIDToAssetPath("f6687b99e1b6bfc4f854f46669e84e31")); } var tmesh = GetTargetMesh(); if (tmesh == null) { return; } if (m_meshTarget == null || m_meshTarget.vertexCount != tmesh.vertexCount || (m_points != null && m_meshTarget.vertexCount != m_points.Length)) { m_meshTarget = tmesh; m_points = null; m_normals = null; m_normalsBase = null; m_tangents = null; m_triangles = null; m_mirrorRelation = null; m_selection = null; ReleaseComputeBuffers(); } bool initialized = false; if (m_points == null && m_meshTarget != null) { initialized = true; m_skinned = GetComponent <SkinnedMeshRenderer>() != null; m_points = m_meshTarget.vertices; m_normals = m_meshTarget.normals; if (m_normals.Length == 0) { m_meshTarget.RecalculateNormals(); m_normalsBase = m_normals = m_meshTarget.normals; } else { m_meshTarget.RecalculateNormals(); m_normalsBase = m_meshTarget.normals; m_meshTarget.normals = m_normals; } m_tangents = m_meshTarget.tangents; if (m_tangents.Length == 0) { m_meshTarget.RecalculateTangents(); m_tangentsBase = m_tangents = m_meshTarget.tangents; } else { m_meshTarget.RecalculateTangents(); m_tangentsBase = m_meshTarget.tangents; m_meshTarget.tangents = m_tangents; } m_triangles = m_meshTarget.triangles; m_selection = new float[m_points.Length]; } if (m_cbPoints == null && m_points != null && m_points.Length > 0) { m_cbPoints = new ComputeBuffer(m_points.Length, 12); m_cbPoints.SetData(m_points); } if (m_cbNormals == null && m_normals != null && m_normals.Length > 0) { m_cbNormals = new ComputeBuffer(m_normals.Length, 12); m_cbNormals.SetData(m_normals); m_cbBaseNormals = new ComputeBuffer(m_normalsBase.Length, 12); m_cbBaseNormals.SetData(m_normalsBase); } if (m_cbTangents == null && m_tangents != null && m_tangents.Length > 0) { m_cbTangents = new ComputeBuffer(m_tangents.Length, 16); m_cbTangents.SetData(m_tangents); m_cbBaseTangents = new ComputeBuffer(m_tangentsBase.Length, 16); m_cbBaseTangents.SetData(m_tangentsBase); } if (m_cbSelection == null && m_selection != null && m_selection.Length > 0) { m_cbSelection = new ComputeBuffer(m_selection.Length, 4); m_cbSelection.SetData(m_selection); } if (m_cbArg == null && m_points != null && m_points.Length > 0) { m_cbArg = new ComputeBuffer(1, 5 * sizeof(uint), ComputeBufferType.IndirectArguments); m_cbArg.SetData(new uint[5] { m_meshCube.GetIndexCount(0), (uint)m_points.Length, 0, 0, 0 }); } if (m_skinned) { if (m_boneMatrices == null || m_boneMatrices.Length != m_meshTarget.bindposes.Length) { m_boneMatrices = new Matrix4x4[m_meshTarget.bindposes.Length]; } if (m_normalsTmp == null || m_normalsTmp.Length != m_normals.Length) { m_normalsTmp = new Vector3[m_normals.Length]; m_normalsBasePredeformed = (Vector3[])m_normalsBase.Clone(); m_tangentsBasePredeformed = (Vector4[])m_tangentsBase.Clone(); } UpdateSkinning(); } m_settings.InitializeBrushData(); if (initialized) { UpdateNormals(); PushUndo(); } }