예제 #1
0
        private bool BuildQueue(bool usingOvercast = false)
        {
            if (_isWorking)
            {
                Debug.LogWarning("BuildQueue found job already in progress - stopping");
                return(false);
            }

            this._persistance = UnityEngine.Random.Range(.5f, .6f);
            CloudParams cp = new CloudParams(SkyMan.cloudSeed, SkyMan.cloudQuality, NUMOFOCTAVES, _persistance, usingOvercast);

            if (usingOvercast)
            {
                cp.gradient = PresetContainer.Instance.cloudNoiseOver;
            }
            else
            {
                cp.gradient = PresetContainer.Instance.cloudNoiseBase;
            }
            _isWorking = true;
            //executes on thread pool; if no threads avaliable will wait for one
            if (!ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), cp))
            {
                Debug.LogError("Failed to queue worker");
                return(false);
            }
            else
            {
                return(true);
            }
        }
        private bool BuildQueue(bool usingOvercast = false)
        {
            if (_isWorking)
            {
                Debug.LogWarning("BuildQueue found job already in progress - stopping");
                return false;

            }

            this._persistance = UnityEngine.Random.Range(.5f, .6f);
            CloudParams cp = new CloudParams(SkyMan.cloudSeed, SkyMan.cloudQuality, NUMOFOCTAVES, _persistance, usingOvercast);

            if (usingOvercast)
                cp.gradient = PresetContainer.Instance.cloudNoiseOver;
            else
                cp.gradient = PresetContainer.Instance.cloudNoiseBase;
            _isWorking = true;
            //executes on thread pool; if no threads avaliable will wait for one
            if (!ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), cp))
            {
                Debug.LogError("Failed to queue worker");
                return false;
            }
            else
                return true;
        }
예제 #3
0
        /// <summary>
        /// Generates color array used to build cloud texture
        /// </summary>
        /// <param name="e"></param>
        private void DoWork(object e)
        {
            CloudParams cp = (CloudParams)e;

            if (cp == null)
            {
                DaggerfallUnity.LogMessage("Error - cp == null", true);
                return;
            }
            if (cp.gradient == null)
            {
                DaggerfallUnity.LogMessage("Error - invalid gradient supplied", true);
                return;
            }

            int numOfOctaves = cp.numOfoctaves;

            if (cp.seed < 0)        //if seed negative, get a random seed
            {
                cp.seed = System.Environment.TickCount;
            }

            System.Random rand = new System.Random(cp.seed);

            Noise[] octaves = new Noise[numOfOctaves];
            float[] freq    = new float[numOfOctaves];
            float[] amp     = new float[numOfOctaves];

            for (int i = 0; i < numOfOctaves; i++)
            {
                octaves[i] = new Noise();
                octaves[i].Seed(rand.Next());
                freq[i] = (float)Math.Pow(2, i);
                amp[i]  = (float)Math.Pow(cp.persistance, octaves.Length - i);
            }

            _colors = new Color[cp.xDimension * cp.yDimension];
            for (int i = 0; i < cp.xDimension; i++)
            {
                //if isWorking set to false from main thread, stop
                if (_abortJob)
                {
                    _abortJob = false;
                    _colors   = null;
                    return;
                }

                for (int j = 0; j < cp.yDimension; j++)
                {
                    float result = 0;
                    for (int z = 0; z < numOfOctaves; z++)
                    {
                        result += octaves[z].Generate((i / freq[z]) % cp.xDimension, (j / freq[z]) % cp.yDimension) * amp[z];
                    }

                    //DaggerfallUnity.LogMessage(string.Format("res: {0} i: {1} j: {2}", result, i, j), true);
                    _colors[i + (j * cp.xDimension)] = cp.gradient.Evaluate(0.5f * (1 + result));
                }
            }

            //DaggerfallUnity.LogMessage("Finished generating cloud noise...seed: " + cp.seed, true);
            if (cp.overCast)
            {
                _overcastCloudBuffer.Enqueue(_colors);
            }
            else
            {
                _normalCloudBuffer.Enqueue(_colors);
            }
            //alert WaitForJobFinish() to create texture
            _isWorking = false;
        }