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
        public void RecalculateTangents(TangentsPrecision precision)
        {
            if (precision == TangentsPrecision.Precise)
            {
                m_meshTarget.RecalculateTangents();
                m_tangentsPredeformed.LockList(l => {
                    m_meshTarget.GetTangents(l);
                });

                if (m_skinned)
                {
                    npApplySkinning(ref m_npSkinData,
                                    null, null, m_tangentsPredeformed,
                                    null, null, m_tangents);
                }
            }
            else
            {
                if (m_skinned)
                {
                    npModelData tmp = m_npModelData;
                    tmp.vertices = m_pointsPredeformed;
                    tmp.normals  = m_normalsPredeformed;
                    npGenerateTangents(ref tmp, m_tangentsPredeformed);
                    npApplySkinning(ref m_npSkinData,
                                    null, null, m_tangentsPredeformed,
                                    null, null, m_tangents);
                }
                else
                {
                    npGenerateTangents(ref m_npModelData, m_tangents);
                }
                m_meshTarget.SetTangents(m_tangentsPredeformed);
            }

            if (m_cbTangents != null)
            {
                m_cbTangents.SetData(m_tangents);
            }
        }
Beispiel #3
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 #4
0
 [DllImport("NormalPainterCore")] static extern int npMove(
     ref npModelData model, Vector3 amount);
Beispiel #5
0
 [DllImport("NormalPainterCore")] static extern int npAssign(
     ref npModelData model, Vector3 value);
Beispiel #6
0
 [DllImport("NormalPainterCore")] static extern void npProjectNormals(
     ref npModelData model, ref npModelData target, IntPtr ray_dir, bool mask);
Beispiel #7
0
 [DllImport("NormalPainterCore")] static extern int npWeld2(
     ref npModelData model, int num_targets, npModelData[] targets,
     int weldMode, float weldAngle, bool mask);
Beispiel #8
0
 [DllImport("NormalPainterCore")] static extern int npSmooth(
     ref npModelData model, float radius, float strength, bool mask);
Beispiel #9
0
 [DllImport("NormalPainterCore")] static extern int npSelectLasso(
     ref npModelData model, ref Matrix4x4 viewproj, Vector2[] lasso, int numLassoPoints, Vector3 campos, float strength, bool frontfaceOnly);
Beispiel #10
0
 [DllImport("NormalPainterCore")] static extern int npSelectRect(
     ref npModelData model, ref Matrix4x4 viewproj, Vector2 rmin, Vector2 rmax, Vector3 campos, float strength, bool frontfaceOnly);
Beispiel #11
0
 [DllImport("NormalPainterCore")] static extern int npSelectConnected(
     ref npModelData model, float strength, bool clear);
Beispiel #12
0
 [DllImport("NormalPainterCore")] static extern int npSelectHole(
     ref npModelData model, float strength, bool clear, bool mask);
Beispiel #13
0
 [DllImport("NormalPainterCore")] static extern int npSelectTriangle(
     ref npModelData model, Vector3 pos, Vector3 dir, float strength);
Beispiel #14
0
 [DllImport("NormalPainterCore")] static extern Vector3 npPickNormal(
     ref npModelData model, Vector3 pos, int ti);
Beispiel #15
0
 [DllImport("NormalPainterCore")] static extern int npRaycast(
     ref npModelData model, Vector3 pos, Vector3 dir, ref int tindex, ref float distance);
Beispiel #16
0
 [DllImport("NormalPainterCore")] static extern int npRotate(
     ref npModelData model, Quaternion amount, Quaternion pivotRot);
Beispiel #17
0
 [DllImport("NormalPainterCore")] static extern int npScale(
     ref npModelData model, Vector3 amount, Vector3 pivotPos, Quaternion pivotRot);
Beispiel #18
0
 [DllImport("NormalPainterCore")] static extern int npSelectBrush(
     ref npModelData model,
     Vector3 pos, float radius, float strength, int num_bsamples, float[] bsamples);
Beispiel #19
0
 [DllImport("NormalPainterCore")] static extern int npWeld(
     ref npModelData model, bool smoothing, float weldAngle, bool mask);
Beispiel #20
0
 [DllImport("NormalPainterCore")] static extern int npUpdateSelection(
     ref npModelData model,
     ref Vector3 selection_pos, ref Vector3 selection_normal);
Beispiel #21
0
 [DllImport("NormalPainterCore")] static extern int npBuildMirroringRelation(
     ref npModelData model, Vector3 plane_normal, float epsilon, int[] relation);
Beispiel #22
0
 [DllImport("NormalPainterCore")] static extern int npBrushReplace(
     ref npModelData model,
     Vector3 pos, float radius, float strength, int num_bsamples, float[] bsamples, Vector3 amount, bool mask);
Beispiel #23
0
 [DllImport("NormalPainterCore")] static extern int npGenerateTangents(
     ref npModelData model, Vector4[] dst);
Beispiel #24
0
 [DllImport("NormalPainterCore")] static extern int npBrushPaint(
     ref npModelData model,
     Vector3 pos, float radius, float strength, int num_bsamples, float[] bsamples, Vector3 baseNormal, int blend_mode, bool mask);
Beispiel #25
0
 [DllImport("NormalPainterCore")] static extern int npBrushLerp(
     ref npModelData model,
     Vector3 pos, float radius, float strength, int num_bsamples, float[] bsamples, Vector3[] baseNormals, Vector3[] normals, bool mask);
Beispiel #26
0
        public bool ApplyWelding2(GameObject[] targets, int weldMode, float weldAngle, bool pushUndo)
        {
            var data = new List <WeldData>();

            // build weld data
            foreach (var t in targets)
            {
                if (!t || t == this.gameObject)
                {
                    continue;
                }

                var smr = t.GetComponent <SkinnedMeshRenderer>();
                if (smr)
                {
                    var mesh = smr.sharedMesh;
                    if (!IsValidMesh(mesh))
                    {
                        continue;
                    }

                    var d = new WeldData();
                    d.skinned   = true;
                    d.mesh      = mesh;
                    d.transform = t.GetComponent <Transform>().localToWorldMatrix;
                    d.vertices  = new PinnedList <Vector3>(mesh.vertices);
                    d.normals   = new PinnedList <Vector3>(mesh.normals);

                    d.weights   = new PinnedList <BoneWeight>(mesh.boneWeights);
                    d.bindposes = new PinnedList <Matrix4x4>(mesh.bindposes);
                    d.bones     = new PinnedList <Matrix4x4>(d.bindposes.Length);

                    var bones = smr.bones;
                    int n     = System.Math.Min(d.bindposes.Length, smr.bones.Length);
                    for (int bi = 0; bi < n; ++bi)
                    {
                        d.bones[bi] = smr.bones[bi].localToWorldMatrix;
                    }

                    d.skinData.num_vertices = d.vertices.Length;
                    d.skinData.num_bones    = d.bindposes.Length;
                    d.skinData.weights      = d.weights;
                    d.skinData.bindposes    = d.bindposes;
                    d.skinData.bones        = d.bones;
                    d.skinData.root         = d.transform;

                    npApplySkinning(ref d.skinData,
                                    d.vertices, d.normals, null,
                                    d.vertices, d.normals, null);

                    data.Add(d);
                }

                var mr = t.GetComponent <MeshRenderer>();
                if (mr)
                {
                    var mesh = t.GetComponent <MeshFilter>().sharedMesh;
                    if (!IsValidMesh(mesh))
                    {
                        continue;
                    }

                    var d = new WeldData();
                    d.mesh      = mesh;
                    d.transform = t.GetComponent <Transform>().localToWorldMatrix;
                    d.vertices  = new PinnedList <Vector3>(mesh.vertices);
                    d.normals   = new PinnedList <Vector3>(mesh.normals);
                    data.Add(d);
                }
            }

            if (data.Count == 0)
            {
                Debug.LogWarning("Nothing to weld.");
                return(false);
            }

            // create data to pass to C++
            bool mask = m_numSelected > 0;

            npModelData[] tdata = new npModelData[data.Count];
            for (int i = 0; i < data.Count; ++i)
            {
                tdata[i].num_vertices = data[i].vertices.Length;
                tdata[i].vertices     = data[i].vertices;
                tdata[i].normals      = data[i].normals;
                tdata[i].transform    = data[i].transform;
            }

            Vector3[] normalsPreWeld = null;
            if (pushUndo)
            {
                normalsPreWeld = m_normals.Clone();
            }

            // do weld
            bool ret = false;

            if (npWeld2(ref m_npModelData, tdata.Length, tdata, weldMode, weldAngle, mask) > 0)
            {
                // data to undo
                History.Record[] records = null;

                if (pushUndo && (weldMode == 0 || weldMode == 2))
                {
                    records = new History.Record[data.Count];

                    for (int i = 0; i < data.Count; ++i)
                    {
                        records[i]         = new History.Record();
                        records[i].mesh    = data[i].mesh;
                        records[i].normals = data[i].mesh.normals;
                    }
                    PushUndo(normalsPreWeld, records);
                }

                // update normals
                if (weldMode == 1 || weldMode == 2)
                {
                    UpdateNormals();
                }
                if (weldMode == 0 || weldMode == 2)
                {
                    foreach (var d in data)
                    {
                        if (d.skinned)
                        {
                            npApplyReverseSkinning(ref d.skinData,
                                                   null, d.normals, null,
                                                   null, d.normals, null);
                        }
                        d.mesh.normals = d.normals;
                        d.mesh.UploadMeshData(false);
                    }

                    if (pushUndo)
                    {
                        for (int i = 0; i < data.Count; ++i)
                        {
                            records[i]         = new History.Record();
                            records[i].mesh    = data[i].mesh;
                            records[i].normals = data[i].normals;
                        }
                    }
                }

                if (pushUndo)
                {
                    PushUndo(m_normals, records);
                }

                ret = true;
            }

            return(ret);
        }