コード例 #1
0
    /// <summary>
    /// require a PRNG compute buffer for camera.
    /// </summary>
    /// <param name="width">the buffer width.</param>
    /// <param name="height">the buffer height.</param>
    /// <returns></returns>
    public ComputeBuffer RequirePRNGStates(Camera camera)
    {
        var id = camera.GetInstanceID();

        if (_PRNGStates.TryGetValue(id, out var buffer))
        {
            return(buffer);
        }

        buffer = new ComputeBuffer(camera.pixelWidth * camera.pixelHeight, 4 * 4, ComputeBufferType.Structured, ComputeBufferMode.Immutable);

        var _mt19937 = new MersenneTwister.MT.mt19937ar_cok_opt_t();

        _mt19937.init_genrand((uint)System.DateTime.Now.Ticks);

        var data = new uint[camera.pixelWidth * camera.pixelHeight * 4];

        for (var i = 0; i < camera.pixelWidth * camera.pixelHeight * 4; ++i)
        {
            data[i] = _mt19937.genrand_int32();
        }
        buffer.SetData(data);

        _PRNGStates.Add(id, buffer);
        return(buffer);
    }
コード例 #2
0
    //require a PRNG compute buffer for camera.
    public ComputeBuffer RequirePRNGStates(Camera vCamera)
    {
        var CameraId = vCamera.GetInstanceID();

        if (_PRNGStates.TryGetValue(CameraId, out var OutBuffer))
        {
            return(OutBuffer);
        }

        OutBuffer = new ComputeBuffer(vCamera.pixelWidth * vCamera.pixelHeight, 4 * 4, ComputeBufferType.Structured, ComputeBufferMode.Immutable);

        var Mt19937 = new MersenneTwister.MT.mt19937ar_cok_opt_t();

        Mt19937.init_genrand((uint)System.DateTime.Now.Ticks);

        var Data = new uint[vCamera.pixelWidth * vCamera.pixelHeight * 4];

        for (var i = 0; i < vCamera.pixelWidth * vCamera.pixelHeight * 4; ++i)
        {
            Data[i] = Mt19937.genrand_int32();
        }
        OutBuffer.SetData(Data);

        _PRNGStates.Add(CameraId, OutBuffer);
        return(OutBuffer);
    }
コード例 #3
0
    void CreatePRNGStates()
    {
        var mapSize = renderTextures[0].width;

        PRNGStates = new ComputeBuffer(mapSize * mapSize, 4 * 4, ComputeBufferType.Structured, ComputeBufferMode.Immutable);

        var _mt19937 = new MersenneTwister.MT.mt19937ar_cok_opt_t();

        _mt19937.init_genrand((uint)System.DateTime.Now.Ticks);

        var data = new uint[mapSize * mapSize * 4];

        for (var i = 0; i < mapSize * mapSize * 4; ++i)
        {
            data[i] = _mt19937.genrand_int32();
        }

        PRNGStates.SetData(data);
    }
コード例 #4
0
    public static void GenerateScene()
    {
        var _mt19937 = new MersenneTwister.MT.mt19937ar_cok_opt_t();

        _mt19937.init_genrand(95273);

        var        templateGo         = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Prefabs/Sphere.prefab");
        var        templateLambertian = AssetDatabase.LoadAssetAtPath <Material>("Assets/Materials/RandomLambertian.mat");
        var        templateMetal      = AssetDatabase.LoadAssetAtPath <Material>("Assets/Materials/RandomMetal.mat");
        var        templateDielectric = AssetDatabase.LoadAssetAtPath <Material>("Assets/Materials/RandomDielectric.mat");
        var        renderers          = new List <Renderer>();
        GameObject go;
        Renderer   renderer;
        Material   material;

        for (var a = -11; a < 11; ++a)
        {
            for (var b = -11; b < 11; ++b)
            {
                var chooseMat = _mt19937.genrand_real1();
                var center    = new Vector3(a + 0.9f * (float)_mt19937.genrand_real1(), 0.2f, b + 0.9f * (float)_mt19937.genrand_real1());
                if ((center - new Vector3(4.0f, 0.2f, 0.0f)).magnitude > 0.9f)
                {
                    if (chooseMat < 0.8)
                    {
                        go      = GameObject.Instantiate(templateGo);
                        go.name = $"Sphere_L_{a}_{b}";
                        go.transform.localPosition = center;
                        go.transform.localScale    = Vector3.one * 0.4f;
                        renderer = go.GetComponent <Renderer>();
                        material = new Material(templateLambertian);
                        material.SetColor(
                            "_Color",
                            new Color(
                                (float)(_mt19937.genrand_real1() * _mt19937.genrand_real1()),
                                (float)(_mt19937.genrand_real1() * _mt19937.genrand_real1()),
                                (float)(_mt19937.genrand_real1() * _mt19937.genrand_real1())));
                        renderer.material = material;
                        renderers.Add(renderer);
                    }
                    else if (chooseMat < 0.95)
                    {
                        go      = GameObject.Instantiate(templateGo);
                        go.name = $"Sphere_M_{a}_{b}";
                        go.transform.localPosition = center;
                        go.transform.localScale    = Vector3.one * 0.4f;
                        renderer = go.GetComponent <Renderer>();
                        material = new Material(templateMetal);
                        material.SetColor(
                            "_Color",
                            new Color(
                                (float)(0.5 * (1.0 + _mt19937.genrand_real1())),
                                (float)(0.5 * (1.0 + _mt19937.genrand_real1())),
                                (float)(0.5 * (1.0 + _mt19937.genrand_real1()))));
                        material.SetFloat("_Fuzz", (float)(0.5 * _mt19937.genrand_real1()));
                        renderer.material = material;
                        renderers.Add(renderer);
                    }
                    else
                    {
                        go      = GameObject.Instantiate(templateGo);
                        go.name = $"Sphere_D_{a}_{b}";
                        go.transform.localPosition = center;
                        go.transform.localScale    = Vector3.one * 0.4f;
                        renderer = go.GetComponent <Renderer>();
                        material = new Material(templateDielectric);
                        material.SetColor("_Color", Color.white);
                        material.SetFloat("_IOR", 1.5f);
                        renderer.material = material;
                        renderers.Add(renderer);
                    }
                }
            }
        }

        go      = GameObject.Instantiate(templateGo);
        go.name = "Sphere_D";
        go.transform.localPosition = new Vector3(0.0f, 1.0f, 0.0f);
        go.transform.localScale    = Vector3.one * 2.0f;
        renderer = go.GetComponent <Renderer>();
        material = new Material(templateDielectric);
        material.SetColor("_Color", Color.white);
        material.SetFloat("_IOR", 1.5f);
        renderer.material = material;
        renderers.Add(renderer);

        go      = GameObject.Instantiate(templateGo);
        go.name = "Sphere_L";
        go.transform.localPosition = new Vector3(-4.0f, 1.0f, 0.0f);
        go.transform.localScale    = Vector3.one * 2.0f;
        renderer = go.GetComponent <Renderer>();
        material = new Material(templateLambertian);
        material.SetColor("_Color", new Color(0.4f, 0.2f, 0.1f));
        renderer.material = material;
        renderers.Add(renderer);

        go      = GameObject.Instantiate(templateGo);
        go.name = "Sphere_M";
        go.transform.localPosition = new Vector3(4.0f, 1.0f, 0.0f);
        go.transform.localScale    = Vector3.one * 2.0f;
        renderer = go.GetComponent <Renderer>();
        material = new Material(templateMetal);
        material.SetColor("_Color", new Color(0.7f, 0.6f, 0.5f));
        material.SetFloat("_Fuzz", 0.0f);
        renderer.material = material;
        renderers.Add(renderer);

        SceneManager.Instance.renderers = renderers.ToArray();
    }