Beispiel #1
0
        public void ApplyProjection(Mesh target, Matrix4x4 ttrans, bool baseNormalsAsRayDirection, bool pushUndo)
        {
            if (!IsValidMesh(target))
            {
                return;
            }

            var tpoints  = new PinnedList <Vector3>(target.vertices);
            var tnormals = new PinnedList <Vector3>(target.normals);
            var tindices = new PinnedList <int>(target.triangles);

            var tdata = new npModelData();

            tdata.transform     = ttrans;
            tdata.num_vertices  = tpoints.Length;
            tdata.num_triangles = tindices.Length / 3;
            tdata.vertices      = tpoints;
            tdata.normals       = tnormals;
            tdata.indices       = tindices;

            var  rayDirections = baseNormalsAsRayDirection ? m_normalsBase : m_normals;
            bool mask          = m_numSelected > 0;

            npProjectNormals(ref m_npModelData, ref tdata, rayDirections, mask);

            UpdateNormals();
            if (pushUndo)
            {
                PushUndo();
            }
        }
Beispiel #2
0
        bool ApplyMirroringInternal()
        {
            if (m_settings.mirrorMode == MirrorMode.None)
            {
                return(false);
            }

            bool needsSetup = false;

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

            Vector3 planeNormal = GetMirrorPlane(m_settings.mirrorMode);

            if (needsSetup)
            {
                npModelData tmp = m_npModelData;
                tmp.vertices = m_pointsPredeformed;
                tmp.normals  = m_normalsBasePredeformed;
                if (npBuildMirroringRelation(ref tmp, planeNormal, 0.0001f, m_mirrorRelation) == 0)
                {
                    Debug.LogWarning("NormalEditor: this mesh seems not symmetric");
                    m_mirrorRelation      = null;
                    m_settings.mirrorMode = MirrorMode.None;
                    return(false);
                }
            }

            npApplyMirroring(m_normals.Length, m_mirrorRelation, planeNormal, m_normalsPredeformed);
            return(true);
        }
Beispiel #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 <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;
        }