Esempio n. 1
0
    private void Generate()
    {
        if (!_initialized)
        {
            return;
        }

        _surfaceTool.Clear();
        _meshDataTool.Clear();
        _surfaceTool.CreateFrom(_meshInstance.Mesh, 0);
        _meshDataTool.CreateFromSurface(_surfaceTool.Commit(), 0);

        int numVertices = _meshDataTool.GetVertexCount();

        _data = new float[numVertices];

        for (int i = 0; i < numVertices; i++)
        {
            Vector3 vertex = _meshDataTool.GetVertex(i);
            _data[i] = Amplitude * _noise.GetNoise2d(vertex.x, vertex.z);
            _meshDataTool.SetVertex(i, new Vector3(vertex.x, _data[i], vertex.z));
            _meshDataTool.SetVertexNormal(i, Vector3.Up);
        }

        for (int i = 0; i < numVertices - _resX; i++)
        {
            Vector3 a = _meshDataTool.GetVertex(i);
            Vector3 b = _meshDataTool.GetVertex(i + 1);
            Vector3 c = _meshDataTool.GetVertex(i + _resX);

            Vector3 normal = (c - a).Cross(b - a);

            for (int j = i; j < i + 3; j++)
            {
                _meshDataTool.SetVertexNormal(j, _meshDataTool.GetVertexNormal(j) + normal);
            }
        }

        for (int i = 0; i < numVertices; i++)
        {
            _meshDataTool.SetVertexNormal(i, _meshDataTool.GetVertexNormal(i).Normalized());
        }

        ArrayMesh newMesh = new ArrayMesh();

        _meshDataTool.CommitToSurface(newMesh);
        _meshInstance.Mesh = newMesh;
        _meshInstance.RemoveChild(_meshInstance.GetChild(0));
        _meshInstance.CreateTrimeshCollision();
    }
        private Godot.Collections.Array createSurfaceByBones(ArrayMesh mesh, int surface, Skin newSkin, List <UMAReciepeBindPose> origBindPoses)
        {
            var mdt = new MeshDataTool();

            mdt.CreateFromSurface(mesh, surface);

            var st = new SurfaceTool();

            st.Begin(Mesh.PrimitiveType.Triangles);

            var newBindPoses = new List <UMAReciepeBindPose>();

            if (newSkin != null)
            {
                for (int i = 0; i < newSkin.GetBindCount(); i++)
                {
                    newBindPoses.Add(new UMAReciepeBindPose
                    {
                        boneName  = newSkin.GetBindName(i),
                        transform = newSkin.GetBindPose(i),
                        boneIndex = newSkin.GetBindBone(i)
                    });
                }
            }

            var boneAmount = 0;

            for (int i = 0; i < mdt.GetVertexCount(); i++)
            {
                var oldVer  = mdt.GetVertex(i);
                var oldNorm = mdt.GetVertexNormal(i);

                var newVer  = new Vector3();
                var newNorm = new Vector3();

                var indexes = mdt.GetVertexBones(i);

                //  st.AddTangent(mdt.GetVertexTangent(i));
                st.AddBones(mdt.GetVertexBones(i));
                st.AddWeights(mdt.GetVertexWeights(i));

                int boneId = 0;
                foreach (var weight in mdt.GetVertexWeights(i))
                {
                    if (newBindPoses.Count >= indexes[boneId] && origBindPoses.Count >= indexes[boneId])
                    {
                        var restBoneNew      = newBindPoses[indexes[boneId]];
                        var restBoneTemplate = origBindPoses[indexes[boneId]];

                        var dataup    = restBoneNew.transform.Xform(Vector3.Up);
                        var dataright = restBoneNew.transform.Xform(Vector3.Right);

                        var templateup    = restBoneTemplate.transform.Xform(Vector3.Up);
                        var templateright = restBoneTemplate.transform.Xform(Vector3.Right);

                        if (Mathf.Abs(dataup.AngleTo(templateup)) > 1 || Mathf.Abs(dataright.AngleTo(templateright)) > 1)
                        {
                            Transform convertMatrix = restBoneTemplate.transform.Inverse() * restBoneNew.transform;

                            newVer  += convertMatrix.Xform(oldVer) * weight;
                            newNorm += convertMatrix.basis.Xform(oldNorm) * weight;
                        }
                        else
                        {
                            newVer  += oldVer * weight;
                            newNorm += oldNorm * weight;
                        }
                    }
                    else
                    {
                        newVer  += oldVer * weight;
                        newNorm += oldNorm * weight;
                    }

                    boneId++;
                }


                st.AddUv(mdt.GetVertexUv(i));

                if (mdt.GetVertexColor(i) != null)
                {
                    st.AddColor(mdt.GetVertexColor(i));
                }

                if (mdt.GetVertexUv2(i) != null)
                {
                    st.AddUv2(mdt.GetVertexUv2(i));
                }

                st.AddNormal(newNorm);
                st.AddVertex(newVer);

                boneAmount += mdt.GetVertexBones(i).Length;
            }

            //creating indexes
            for (int face = 0; face < mdt.GetFaceCount(); face++)
            {
                for (int faceI = 0; faceI < 3; faceI++)
                {
                    var ind = mdt.GetFaceVertex(face, faceI);
                    st.AddIndex(ind);
                }
            }

            st.GenerateTangents();
            return(st.CommitToArrays());
        }
Esempio n. 3
0
    /// <summary>
    /// cyberality's technique.  needs some optimization for use with big meshes.
    /// </summary>
    /// <param name="sortedVerts"></param>
    /// <param name="colorChoices"></param>
    /// <param name="mdt"></param>
    private static void _CYBERREALITY(List <VertexInfo> sortedVerts, Color[] colorChoices, MeshDataTool mdt)
    {
        var done       = new Dictionary <int, bool>();  //vertidx/isDone
        var bary       = new Dictionary <int, Color>(); //vertidx/color
        var rand       = new Random(0);
        var rand_color = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble());
        var nors       = new Dictionary <int, (Vector3 normal, Color color)>();  //normalIdx/normal

        for (var j = 0; j < mdt.GetFaceCount(); j++)
        {
            var fid    = mdt.GetFaceVertex(j, 0);
            var nor    = mdt.GetVertexNormal(fid);
            var coords = new List <Color>()
            {
                Colors.Red, Colors.Green, Colors.Blue
            };
            foreach (var n in nors.Keys)
            {
                var dot = nor.Dot(nors[n].normal);
                if (dot == 1)
                {
                    rand_color = nors[n].color;
                }
                else
                {
                    rand_color = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble());
                }
            }
            nors[fid] = (normal : nor, color : rand_color);
            for (var k = 0; k < 3; k++)
            {
                var vid = mdt.GetFaceVertex(j, k);
                if (bary.ContainsKey(vid))
                {
                    coords.Remove(bary[vid]);
                }
            }
            for (var i = 0; i < 3; i++)
            {
                var vid = mdt.GetFaceVertex(j, i);
                if (!done.ContainsKey(vid) || done[vid] != true)
                {
                    done[vid] = true;
                    var removal = Colors.Black;

                    var vert_0 = mdt.GetFaceVertex(j, 0);
                    var vert_1 = mdt.GetFaceVertex(j, 1);
                    var vert_2 = mdt.GetFaceVertex(j, 2);
                    var edge_a = mdt.GetVertex(vert_2).DirectionTo(mdt.GetVertex(vert_0));
                    var edge_b = mdt.GetVertex(vert_0).DirectionTo(mdt.GetVertex(vert_1));
                    var edge_c = mdt.GetVertex(vert_1).DirectionTo(mdt.GetVertex(vert_2));

                    if ((edge_a > edge_b) && (edge_a > edge_c))
                    {
                        removal.g = 1;
                    }
                    else if ((edge_b > edge_c) && (edge_b > edge_a))
                    {
                        removal.r = 1;
                    }
                    else
                    {
                        removal.b = 1;
                    }

                    if (coords.Count > 0)
                    {
                        var next = coords[0]; coords.RemoveAt(0);
                        bary[vid] = next + removal;
                    }
                    else
                    {
                        var coords2 = new List <Color>()
                        {
                            Colors.Red, Colors.Green, Colors.Blue
                        };

                        for (var m = 0; m < 3; m++)
                        {
                            if (m == i)
                            {
                                continue;
                            }
                            var vid2 = mdt.GetFaceVertex(j, m);
                            if (bary.ContainsKey(vid2))
                            {
                                coords2.Remove(bary[vid2]);
                            }
                            bary[vid] = coords2[0] + removal;                             //BUG?  coords was  checked to not have any....  maybe means coords2
                            coords2.RemoveAt(0);
                        }
                    }
                    mdt.SetVertexColor(vid, bary[vid]);
                }
            }
        }
    }
Esempio n. 4
0
    /// <summary>
    /// my edited version of Cyberality's technique.
    /// faster performance, about as good coverage as "greedy" algo.
    /// </summary>
    /// <param name="sortedVerts"></param>
    /// <param name="_colorChoices"></param>
    /// <param name="mdt"></param>
    private static void _CYBERREALITY_EDIT(List <VertexInfo> sortedVerts, Color[] _colorChoices, MeshDataTool mdt)
    {
        var done              = new Dictionary <int, bool>();  //vertidx/isDone
        var vertColorStorage  = new Dictionary <int, Color>(); //vertidx/color
        var rand              = new Random(0);
        var rand_color        = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble());
        var faceVert0MetaInfo = new Dictionary <int, (Vector3 normal, Color color)>();        //normalIdx/normal

        for (var faceIdx = 0; faceIdx < mdt.GetFaceCount(); faceIdx++)
        {
            var faceVert0Idx  = mdt.GetFaceVertex(faceIdx, 0);
            var faceVert0Norm = mdt.GetVertexNormal(faceVert0Idx);
            var colorChoices  = new List <Color>()
            {
                Colors.Red, Colors.Green, Colors.Blue
            };
            //////JASON CLEANUP:  this code block isn't actually used in the algo...
            ////foreach (var n in faceVert0MetaInfo.Keys)
            ////{
            ////	var dot = faceVert0Norm.Dot(faceVert0MetaInfo[n].normal);
            ////	if (dot == 1)
            ////	{
            ////		rand_color = faceVert0MetaInfo[n].color;
            ////	}
            ////	else
            ////	{
            ////		rand_color = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble());
            ////	}
            ////}
            ////faceVert0MetaInfo[faceVert0Idx] = (normal: faceVert0Norm, color: rand_color);


            //loop through all verts for the face, and remove colors from our colorChoices if a vert is already using it
            for (var faceVertId = 0; faceVertId < 3; faceVertId++)
            {
                var vertIdx = mdt.GetFaceVertex(faceIdx, faceVertId);
                if (vertColorStorage.ContainsKey(vertIdx))
                {
                    colorChoices.Remove(vertColorStorage[vertIdx]);
                }
            }


            for (var faceVertId = 0; faceVertId < 3; faceVertId++)
            {
                var vertIdx = mdt.GetFaceVertex(faceIdx, faceVertId);
                if (!done.ContainsKey(vertIdx) || done[vertIdx] != true)
                {
                    done[vertIdx] = true;
                    var removal = Colors.Black;

                    var vert_0 = mdt.GetFaceVertex(faceIdx, 0);
                    var vert_1 = mdt.GetFaceVertex(faceIdx, 1);
                    var vert_2 = mdt.GetFaceVertex(faceIdx, 2);
                    var edge_a = mdt.GetVertex(vert_2).DirectionTo(mdt.GetVertex(vert_0));
                    var edge_b = mdt.GetVertex(vert_0).DirectionTo(mdt.GetVertex(vert_1));
                    var edge_c = mdt.GetVertex(vert_1).DirectionTo(mdt.GetVertex(vert_2));

                    if ((edge_a > edge_b) && (edge_a > edge_c))
                    {
                        removal.g = 1;
                    }
                    else if ((edge_b > edge_c) && (edge_b > edge_a))
                    {
                        removal.r = 1;
                    }
                    else
                    {
                        removal.b = 1;
                    }

                    if (colorChoices.Count > 0)
                    {
                        var next = colorChoices[0]; colorChoices.RemoveAt(0);
                        vertColorStorage[vertIdx] = next + removal;
                    }
                    //JASON CLEANUP:  this else will never trigger, as there are only 3 verticies
                    else
                    {
                        GD.Print("in else!");
                        var coords2 = new List <Color>()
                        {
                            Colors.Red, Colors.Green, Colors.Blue
                        };

                        for (var m = 0; m < 3; m++)
                        {
                            if (m == faceVertId)
                            {
                                continue;
                            }
                            var vid2 = mdt.GetFaceVertex(faceIdx, m);
                            if (vertColorStorage.ContainsKey(vid2))
                            {
                                coords2.Remove(vertColorStorage[vid2]);
                            }
                            vertColorStorage[vertIdx] = coords2[0] + removal;                             //BUG?  coords was  checked to not have any....  maybe means coords2
                            coords2.RemoveAt(0);
                        }
                    }
                    mdt.SetVertexColor(vertIdx, vertColorStorage[vertIdx]);
                }
            }
        }
    }