示例#1
0
    protected virtual void BuildMeshThreadProc()
    {
        meshBuilt = false;
        //Debug.Log("GeoSurface.BuildMesh thread start");
        Utils.StopWatch buildMeshStopWatch = new Utils.StopWatch("BuildMeshThreadProc");

        // Calculating normals
        BuildNormals();

        // Building mesh containers
        meshContainers.Clear();
        for (int triTreeNode = 0; triTreeNode < triTreeRootSize; triTreeNode++)
        {
            meshContainers.Add(BuildTriTreeNodeMesh(triTreeNode));

            if (threadsShouldStop)
            {
                Debug.Log("GeoSurface.BuildMesh thread abort !!!");
                return;
            }
        }

        //Debug.Log("GeoSurface.BuildMesh thread finish <<");
        buildMeshStopWatch.Stop();
        meshBuilt = true;
    }
示例#2
0
    protected virtual void ApplyNoiseThreadProc()
    {
        noiseApplied = false;
        //Debug.Log("GeoSurface.ApplyNoise thread start");
        Utils.StopWatch applyNoiseStopWatch = new Utils.StopWatch("ApplyNoiseThreadProc");

        // Applying generated noise
        for (int i = 0; i < nodes.Count; i++)
        {
            if (!nodes[i].transformed)
            {
                nodes[i].elevation   = noiseOutput[i] * NoiseScale;
                nodes[i].position    = ElevatePoint(nodes[i].position, nodes[i].elevation);
                nodes[i].transformed = true;
            }

            if (threadsShouldStop)
            {
                Debug.Log("GeoSurface.ApplyNoise thread abort !!!");
                return;
            }
        }

        noiseInput.Clear();
        noiseOutput = new float[0] {
        };

        //Debug.Log("GeoSurface.ApplyNoise thread finish <<");
        applyNoiseStopWatch.Stop();
        noiseApplied = true;
    }
示例#3
0
    protected virtual void SubdivideThreadProc()
    {
        subdivided = false;
        //Debug.Log("GeoSurface.Subdivide thread start");
        Utils.StopWatch subdivideStopWatch = new Utils.StopWatch("SubdivideThreadProc");

        // Subdividing
        for (int triTreeNode = 0; triTreeNode < triTreeRootSize; triTreeNode++)
        {
            Subdivide(triTreeNode, TargetSubdivisionLevel);

            if (threadsShouldStop)
            {
                Debug.Log("GeoSurface.Subdivide thread abort !!!");
                return;
            }
        }

        if (BuildAtmosphere)
        {
            BuildAtmosphereProc();
        }

        // Collecting points
        noiseInput = new List <Vector3>();
        foreach (GeoNode node in nodes)
        {
            if (!node.transformed)
            {
                noiseInput.Add(node.position * NoiseSpaceScale);
            }

            if (threadsShouldStop)
            {
                Debug.Log("GeoSurface.Subdivide thread abort !!!");
                return;
            }
        }

        //Debug.Log("GeoSurface.Subdivide thread finish <<");
        subdivideStopWatch.Stop();
        subdivided = true;
    }
示例#4
0
    protected virtual void NormalMapsColorizeThreadProc()
    {
        //Debug.Log("GeoSurface.NormalMapsColorize thread start normalMapGeneratorOutput: " + normalMapGeneratorOutput.Length + " nmap: " + (TextureSize*TextureSize) );
        Utils.StopWatch normalMapsColorizeStopWatch = new Utils.StopWatch("NormalMapsColorizeThreadProc");

        Color[] nmap = new Color[TextureSize * TextureSize];
        for (int i = 0; i < nmap.Length; i++)
        {
            //Vector3 n = new Vector3(normalMapGeneratorOutput[i], normalMapGeneratorOutput[i*3+1], normalMapGeneratorOutput[i*3+2]);
            nmap[i] = ColorizeNormal(normalMapGeneratorOutput[i]);
            //float c = hmtest[i];
            //nmap[i] = Colorize(new float[1] { c });
            //nmap[i] *= 0.5f;
            if (threadsShouldStop)
            {
                Debug.Log("GeoSurface.NormalMapsColorize thread abort !!!");
                return;
            }
        }
        normalMaps.Add(nmap);
        normalMapsColorized++;
        //Debug.Log("GeoSurface.NormalMapsColorize thread finish << normalMapsColorized = " + normalMapsColorized);
        normalMapsColorizeStopWatch.Stop();
    }
示例#5
0
    public virtual bool Update()
    {
        if (subdivided)
        {
            // start surface noise
            if (!Noise.Started)
            {
                Noise.Start(noiseInput.ToArray(), NoiseGenFrames);
                Debug.Log("noiseInput.count = " + noiseInput.Count);
            }

            // proceed surface noise
            if (Noise.Update() && !applyNoiseThread.IsAlive && !noiseApplied)
            {
                // start applying noise to the geometry
                noiseOutput = Noise.Output();
                applyNoiseThread.Start();
            }

            // Textures generating //
            if (TexNoises.Length > 0 && texColorized < (trianglePairs.Length - 1))
            {
                // create input points for FBMGPU generators
                if (texGeneratorLoaded <= texColorized && texGeneratorLoaded < (trianglePairs.Length - 1) && !texLoadGeneratorThread.IsAlive)
                {
                    texLoadGeneratorThread = new Thread(this.TexLoadGeneratorThreadProc);
                    texLoadGeneratorThread.Start();
                }

                // start FBMGPUs if loaded
                if (texGeneratorLoaded > texColorized && texGeneratorLoaded > texGeneratorDone)
                {
                    foreach (ISpatialNoiseGenerator texNoise in TexNoises)
                    {
                        if (!texNoise.Started)
                        {
                            texNoise.Start(texNoiseInput, TexNoiseGenFrames);
                        }
                    }
                }

                // update FBMGPUs
                bool allTexNoiseDone = true;
                foreach (ISpatialNoiseGenerator texNoise in TexNoises)
                {
                    if (!texNoise.Update())
                    {
                        allTexNoiseDone = false;
                    }
                }

                // take FBMGPUs output if they're done and previous texture has been colorized
                if (allTexNoiseDone && !texColorizeThread.IsAlive)
                {
                    texNoiseOutput = new float[TexNoises.Length][];
                    for (int i = 0; i < TexNoises.Length; i++)
                    {
                        texNoiseOutput[i] = TexNoises[i].Output();
                        TexNoises[i].Reset();
                    }

                    texGeneratorDone++;
                    texColorizeThread = new Thread(this.TexColorizeThreadProc);
                    texColorizeThread.Start();
                }
            }

            if (texColorized == (trianglePairs.Length - 1))
            {
                texturesGenerated = true;
            }

            // Normal maps generating //
            if (NormalMapGenerator != null && normalMapsColorized < texColorized)
            {
                // Start normal map generator
                if (normalMapsGenerated < texColorized && !NormalMapGenerator.IsStarted)
                {
                    NormalMapGenerator.SetInput(heightMapsCache[normalMapsGenerated + 1], NormalGenChunkSize);
                    NormalMapGenerator.Start();
                }

                // Update normal map generator
                if (NormalMapGenerator.Update())
                {
                    // Start colorizing normal map if NormalMapGenerator is done
                    if (!normalMapColorizeThread.IsAlive)
                    {
                        normalMapsGenerated++;
                        normalMapGeneratorOutput = NormalMapGenerator.GetOutput();
                        //hmtest = hmtestcache[normalMapsGenerated];
                        normalMapColorizeThread = new Thread(this.NormalMapsColorizeThreadProc);
                        normalMapColorizeThread.Start();
                        NormalMapGenerator.Reset();
                    }
                }
            }

            if (normalMapsColorized == (trianglePairs.Length - 1) || NormalMapGenerator == null)
            {
                allNormalMapsDone = true;
            }
        }

        if (noiseApplied)
        {
            if (!buildMeshThread.IsAlive && !meshBuilt)
            {
                buildMeshThread.Start();
            }
        }

        if (buildingStopWatch != null && Done)
        {
            buildingStopWatch.Stop();
            buildingStopWatch = null;
        }

        return(Done);
    }
示例#6
0
    protected virtual void TexColorizeThreadProc()
    {
        //Debug.Log("GeoSurface.TexColorize thread start");
        Utils.StopWatch texColorizeStopWatch = new Utils.StopWatch("TexColorizeThreadProc");

        if (TexNoises.Length > 0)
        {
            Color[] tex = new Color[TextureSize * TextureSize];

            int       widthWithOverlap = TextureSize + 2 * TexFilterOverlap;
            int       sizeWithOverlap  = widthWithOverlap * widthWithOverlap;
            Vector3[] heightMap        = new Vector3[sizeWithOverlap];
            float[]   hmtest           = new float[TextureSize * TextureSize];

            /*for (int i = 0; i < sizeWithOverlap; i++)
             * {
             *      float h;
             *      float[] values = new float[texNoiseOutput.Length];
             *      for (int j = 0; j < texNoiseOutput.Length; j++)
             *              values[j] = texNoiseOutput[j][i];
             *
             *      if (i < tex.Length)
             *              tex[i] = new Color(1f, 0f, 1f);
             *
             *      int x = i % widthWithOverlap;
             *      int y = i / widthWithOverlap;
             *
             *      if (x >= TexFilterOverlap && x < TextureSize+TexFilterOverlap &&
             *              y >= TexFilterOverlap && y < TextureSize+TexFilterOverlap)
             *      {
             *              tex[ (x-TexFilterOverlap) + (y-TexFilterOverlap)*TextureSize ] = Colorize(values);
             *              //hmtest[ (x-TexFilterOverlap) + (y-TexFilterOverlap)*TextureSize ] = HeightMap(values);
             *              h = HeightMap(values);
             *      }
             *      else
             *              h = 0f;
             *
             *      h = HeightMap(values);
             *      heightMap[i] = texNoiseInputCache[texColorized+1][i].normalized * (1f + h*NoiseScale);
             *
             *      if (threadsShouldStop)
             *      {
             *              Debug.Log("GeoSurface.TexColorize thread abort !!!");
             *              return;
             *      }
             * }*/

            int      subthreadsFinished = 0;
            int      subthreadsCount    = 8;
            Thread[] subthreads         = new Thread[subthreadsCount];

            for (int ti = 0; ti < subthreads.Length; ti++)
            {
                subthreads[ti] = new Thread(
                    delegate(object subthreadIndex)
                {
                    for (int i = 0; i < sizeWithOverlap / subthreadsCount; i++)
                    {
                        if (threadsShouldStop)
                        {
                            break;
                        }
                        int pos = i * subthreadsCount + (int)subthreadIndex;

                        float h;
                        float[] values = new float[texNoiseOutput.Length];
                        for (int j = 0; j < texNoiseOutput.Length; j++)
                        {
                            values[j] = texNoiseOutput[j][pos];
                        }

                        //if (pos < tex.Length)
                        //	tex[pos] = new Color(1f, 0f, 1f);

                        int x = pos % widthWithOverlap;
                        int y = pos / widthWithOverlap;

                        if (x >= TexFilterOverlap && x < TextureSize + TexFilterOverlap &&
                            y >= TexFilterOverlap && y < TextureSize + TexFilterOverlap)
                        {
                            tex[(x - TexFilterOverlap) + (y - TexFilterOverlap) * TextureSize] = Colorize(values);
                            //hmtest[ (x-TexFilterOverlap) + (y-TexFilterOverlap)*TextureSize ] = HeightMap(values);
                            h = HeightMap(values);
                        }
                        else
                        {
                            h = 0f;
                        }

                        h = HeightMap(values);
                        heightMap[pos] = texNoiseInputCache[texColorized + 1][pos].normalized * (1f + h * NoiseScale);
                    }
                    subthreadsFinished++;
                }
                    );
                subthreads[ti].Start(ti);
            }

            while (subthreadsFinished < subthreadsCount)
            {
                if (threadsShouldStop)
                {
                    Debug.Log("GeoSurface.TexColorize thread abort !!!");
                    return;
                }
            }

            heightMapsCache.Add(heightMap);
            //hmtestcache.Add(hmtest);
            textures.Add(tex);
            texColorized++;
            //Debug.Log("texColorized = " + texColorized);
        }
        //Debug.Log("GeoSurface.TexColorize thread finish <<");
        texColorizeStopWatch.Stop();
    }
示例#7
0
    protected virtual void TexLoadGeneratorThreadProc()
    {
        //Debug.Log("GeoSurface.TexLoadGenerator thread start");
        Utils.StopWatch texLoadGeneratorStopWatch = new Utils.StopWatch("TexLoadGeneratorThreadProc");

        if (TexNoises.Length > 0)
        {
            int widthWithOverlap = TextureSize + 2 * TexFilterOverlap;
            int sizeWithOverlap  = widthWithOverlap * widthWithOverlap;

            texNoiseInput = new Vector3[sizeWithOverlap];

            /*int[] tripair =  trianglePairs[texGeneratorLoaded+1];
             * int tri_iA;
             * int tri_iB;
             * int tri_iC1;
             * int tri_iC2;
             * GetQuadVertices(tripair[0], tripair[1], out tri_iA, out tri_iB, out tri_iC1, out tri_iC2);*/

            int      subthreadsFinished = 0;
            int      subthreadsCount    = 8;
            Thread[] subthreads         = new Thread[subthreadsCount];

            for (int ti = 0; ti < subthreads.Length; ti++)
            {
                subthreads[ti] = new Thread(
                    delegate(object subthreadIndex)
                {
                    for (int i = 0; i < sizeWithOverlap / subthreadsCount; i++)
                    {
                        if (threadsShouldStop)
                        {
                            break;
                        }
                        int pos            = i * subthreadsCount + (int)subthreadIndex;
                        int x              = pos % widthWithOverlap;
                        int y              = pos / widthWithOverlap;
                        texNoiseInput[pos] =                                 //new Vector3(0f, 0f, 0f);
                                             TexNoiseSpaceScale * GetVertexPositionFromUV(new Vector2((float)(x - TexFilterOverlap) / (float)TextureSize, (float)(y - TexFilterOverlap) / (float)TextureSize), texGeneratorLoaded + 1);
                    }
                    subthreadsFinished++;
                }
                    );
                subthreads[ti].Start(ti);
            }

            while (subthreadsFinished < subthreadsCount)
            {
                if (threadsShouldStop)
                {
                    Debug.Log("GeoSurface.TexLoadGenerator thread abort !!!");
                    return;
                }
            }

            texNoiseInputCache.Add(texNoiseInput);
            texGeneratorLoaded++;
        }
        //Debug.Log("GeoSurface.TexLoadGenerator thread finish <<");
        texLoadGeneratorStopWatch.Stop();
    }