public static void AmbientOcclusionRecalc(Mesh mesh)
        {
            var squares = new float[mesh.Verteces.Count];

            for (int i = 0; i < mesh.Verteces.Count; i += 3)
            {
                var v0 = mesh.Verteces[i];
                var v1 = mesh.Verteces[i + 1];
                var v2 = mesh.Verteces[i + 2];

                var sq = SstlHelper.TriangleSquare(v0, v1, v2);
                squares[i] = squares[i + 1] = squares[i + 2] = sq;
            }

            AorResultTotal = mesh.Verteces.Count;
            AorResult      = 0;
            int processorCount = Environment.ProcessorCount;

            IAsyncResult[] results = new IAsyncResult[processorCount];
            var            t       = SstlHelper.NearestMod((int)(mesh.Verteces.Count / (float)processorCount), 3);

            for (int i = 0; i < processorCount; i++)
            {
                Action <Mesh, float[], int, int> aorT = AorThread;

                results[i] = aorT.BeginInvoke(mesh, squares, t * i, t, null, null);
            }

            while (true)
            {
                bool all = true;
                for (int i = 0; i < results.Length; i++)
                {
                    if (!results[i].IsCompleted)
                    {
                        all = false;
                    }
                }
                if (all)
                {
                    break;
                }
                Thread.Sleep(300);
            }
        }
Example #2
0
        public static Mesh SmartTesselate(Mesh mesh)
        {
            Mesh m = new Mesh();

            float size = 0;

            for (int i = 0; i < mesh.Indeces.Count; i += 3)
            {
                size += SstlHelper.TriangleSquare(mesh.Verteces[(int)mesh.Indeces[i]],
                                                  mesh.Verteces[(int)mesh.Indeces[i + 1]],
                                                  mesh.Verteces[(int)mesh.Indeces[i + 2]]);
            }
            size /= mesh.Indeces.Count;

            int off = 0;

            for (int i = 0; i < mesh.Indeces.Count; i += 3)
            {
                VertexPositionNormalTexture t;

                var sq = SstlHelper.TriangleSquare(mesh.Verteces[(int)mesh.Indeces[i]],
                                                   mesh.Verteces[(int)mesh.Indeces[i + 1]],
                                                   mesh.Verteces[(int)mesh.Indeces[i + 2]]);
                if (sq <= size * 6)
                {
                    m.Verteces.Add(mesh.Verteces[(int)mesh.Indeces[i]]);
                    m.Verteces.Add(mesh.Verteces[(int)mesh.Indeces[i + 1]]);
                    m.Verteces.Add(mesh.Verteces[(int)mesh.Indeces[i + 2]]);
                    m.Indeces.Add((uint)off + 0);
                    m.Indeces.Add((uint)off + 1);
                    m.Indeces.Add((uint)off + 2);
                    off += 3;
                    continue;
                }

                m.Verteces.Add(mesh.Verteces[(int)mesh.Indeces[i]]);
                m.Verteces.Add(mesh.Verteces[(int)mesh.Indeces[i + 1]]);
                m.Verteces.Add(mesh.Verteces[(int)mesh.Indeces[i + 2]]);

                t = (mesh.Verteces[(int)mesh.Indeces[i]] + mesh.Verteces[(int)mesh.Indeces[i + 1]]) / 2;
                m.Verteces.Add(t);

                t = (mesh.Verteces[(int)mesh.Indeces[i]] + mesh.Verteces[(int)mesh.Indeces[i + 2]]) / 2;
                m.Verteces.Add(t);

                t = (mesh.Verteces[(int)mesh.Indeces[i + 1]] + mesh.Verteces[(int)mesh.Indeces[i + 2]]) / 2;
                m.Verteces.Add(t);

                m.Indeces.Add((uint)off + 0);
                m.Indeces.Add((uint)off + 3);
                m.Indeces.Add((uint)off + 4);

                m.Indeces.Add((uint)off + 5);
                m.Indeces.Add((uint)off + 3);
                m.Indeces.Add((uint)off + 1);


                m.Indeces.Add((uint)off + 5);
                m.Indeces.Add((uint)off + 2);
                m.Indeces.Add((uint)off + 4);

                m.Indeces.Add((uint)off + 3);
                m.Indeces.Add((uint)off + 5);
                m.Indeces.Add((uint)off + 4);

                off += 6;
            }
            return(m);
        }