Пример #1
0
    private void BuildTexure(List <Vector2> samples)
    {
        if (texturePixels == null || texturePixels.Length != IMAGE_LENGTH * IMAGE_LENGTH || lights == null || sampleWeights == null || sampleWeights.Length != IMAGE_LENGTH * IMAGE_LENGTH)
        {
            Initialize();
        }

        Vector2 center = new Vector2(.5f, .5f);

        int samplesAAexp = aaQuality;

        float pixelLength = 1f / IMAGE_LENGTH;

        foreach (Vector2 sample in samples)
        {
            int x = Mathf.Clamp((int)(sample.x * IMAGE_LENGTH), 0, IMAGE_LENGTH - 1);
            int y = Mathf.Clamp((int)(sample.y * IMAGE_LENGTH), 0, IMAGE_LENGTH - 1);;

            List <Vector2> aaSamples = Stochastic.GetStratifiedUniformGrid(samplesAAexp, samplesAAexp);
            Color          pixel     = Color.black;

            foreach (Vector2 aa in aaSamples)
            {
                Vector2 p = sample + aa * pixelLength;

                if ((p - center).magnitude < .5f)
                {
                    pixel += RenderPixel((p - center) / .5f) / aaSamples.Count;
                }
                else
                {
                    // Background pixel
                    Vector2 dirToPoint = p - center;
                    Vector2 edge       = dirToPoint.normalized;
                    //pixel += RenderPixel(edge *.999f) / aaSamples.Count;
                }
            }

            Color previousColor  = texturePixels[y * IMAGE_LENGTH + x];
            float previousWeight = sampleWeights[y * IMAGE_LENGTH + x];

            sampleWeights[y * IMAGE_LENGTH + x] += 1f;

            if (previousWeight > 0.0001f)
            {
                texturePixels[y * IMAGE_LENGTH + x] = ((previousColor * previousWeight) + pixel) / (previousWeight + 1f);
            }
            else
            {
                texturePixels[y * IMAGE_LENGTH + x] = pixel;
            }
        }
    }
Пример #2
0
    public void Update()
    {
        if (previewMode)
        {
            int previewSamples = (int)(IMAGE_LENGTH);

            BuildTexure(Stochastic.GetStratifiedUniformGrid(previewSamples, previewSamples));

            updateCounter++;

            if (updateCounter > 1)
            {
                sphereMap.SetPixels(texturePixels);
                sphereMap.Apply();
                updateCounter = 0;
            }
        }
    }