void PrepareMirror()
        {
            if (m_settings.mirrorMode == MirrorMode.None)
            {
                return;
            }

            bool needsSetup = false;

            if (m_mirrorRelation == null || m_mirrorRelation.Count != m_points.Count)
            {
                m_mirrorRelation = new PinnedList <int>(m_points.Count);
                needsSetup       = true;
            }
            if (m_prevMirrorMode != m_settings.mirrorMode)
            {
                m_prevMirrorMode = m_settings.mirrorMode;
                needsSetup       = true;
            }

            Vector3 planeNormal = GetMirrorPlane(m_settings.mirrorMode);

            if (needsSetup)
            {
                npMeshData tmp = m_npModelData;
                tmp.vertices = m_pointsBasePredeformed;
                tmp.normals  = m_normalsBasePredeformed;
                if (npBuildMirroringRelation(ref tmp, planeNormal, m_settings.mirrorEpsilon, m_mirrorRelation) == 0)
                {
                    Debug.LogWarning("This mesh seems not symmetric");
                    m_mirrorRelation      = null;
                    m_settings.mirrorMode = MirrorMode.None;
                }
            }
        }
        public bool Extract(Mesh mesh)
        {
            if (!mesh || !mesh.isReadable)
            {
                return(false);
            }

            vertexCount = mesh.vertexCount;
            mesh.GetVertices(vertices.List);
            mesh.GetNormals(normals.List);
            mesh.GetUVs(0, uv.List);
            mesh.GetTangents(tangents.List);
            indices = new PinnedList <int>(mesh.triangles);
            return(true);
        }
Пример #3
0
        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 <VertexTweakerSettings>(AssetDatabase.GUIDToAssetPath("9b3ba32ae87b3a94e9cf70157c96f58a"));
                if (ds != null)
                {
                    m_settings = Instantiate(ds);
                }
                if (m_settings == null)
                {
                    m_settings = ScriptableObject.CreateInstance <VertexTweakerSettings>();
                }
            }

            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("5786f144ee220ce4ea056f3f5ef4af19")));
            }
            if (m_matOverlay == null)
            {
                m_matOverlay = new Material(AssetDatabase.LoadAssetAtPath <Shader>(AssetDatabase.GUIDToAssetPath("dada189530d1c844588658636810ae94")));
            }

            if (m_meshTarget == null ||
                m_meshTarget != tmesh ||
                (m_points != null && m_meshTarget.vertexCount != m_points.Count))
            {
                m_meshTarget     = tmesh;
                m_points         = m_pointsBase = m_pointsPredeformed = m_pointsBasePredeformed = null;
                m_normals        = m_normalsBase = m_normalsPredeformed = m_normalsBasePredeformed = null;
                m_tangents       = m_tangentsBase = m_tangentsPredeformed = m_tangentsBasePredeformed = null;
                m_indices        = null;
                m_mirrorRelation = null;
                m_selection      = null;

                ReleaseComputeBuffers();
            }

            if (m_meshTarget != null)
            {
                m_points = m_pointsPredeformed = new PinnedList <Vector3>(m_meshTarget.vertices);

                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_normals = new PinnedList <Vector3>(m_meshTarget.normals);
                }
                m_normalsPredeformed = m_normals;

                m_tangents = new PinnedList <Vector4>(m_meshTarget.tangents);
                if (m_tangents.Count == 0)
                {
                    m_meshTarget.RecalculateTangents();
                    m_tangents = new PinnedList <Vector4>(m_meshTarget.tangents);
                }
                m_tangentsPredeformed = m_tangents;

                if (m_pointsBase == null)
                {
                    m_pointsBase   = m_pointsBasePredeformed = m_points.Clone();
                    m_normalsBase  = m_normalsBasePredeformed = m_normals.Clone();
                    m_tangentsBase = m_tangentsBasePredeformed = m_tangents.Clone();
                }

                m_indices   = new PinnedList <int>(m_meshTarget.triangles);
                m_selection = new PinnedList <float>(m_points.Count);

                m_cbPointsDirty    = true;
                m_cbNormalsDirty   = true;
                m_cbTangentsDirty  = true;
                m_cbSelectionDirty = true;

                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_pointsBasePredeformed   = m_pointsBase.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;
                }
            }

            m_settings.InitializeBrushData();

            m_forceDisableRecalculation = true;
            UpdateTransform();
            UpdateVertices();
            PushUndo();
            m_forceDisableRecalculation = false;

            Tools.current = Tool.None;
            m_editing     = true;
        }