/*private void setupMeshBuffer()
     * {
     *  MeshRenderer[] allObjects = UnityEngine.Object.FindObjectsOfType<MeshRenderer>();
     *
     *
     *  foreach (var meshR in allObjects)
     *  {
     *      Mesh mesh = meshR.gameObject.GetComponent<MeshFilter>().mesh;
     *      List<Material> materials = new List<Material>();
     *      meshR.GetMaterials(materials);
     *
     *      List<Vector3> v = new List<Vector3>();
     *      List<Vector3> n = new List<Vector3>();
     *      List<int> t;
     *      List<Vector2> uv0 = new List<Vector2>();
     *
     *      mesh.GetVertices(v);
     *      mesh.GetNormals(n);
     *      mesh.GetUVs(0, uv0);
     *      t = new List<int>(mesh.triangles);
     *
     *
     *  }
     *
     *  //_vertexBuffer = new ComputeBuffer(blackHoles.Count, size);
     *  //_triangleBuffer = new ComputeBuffer(blackHoles.Count, size);
     *  //_normalsBuffer = new ComputeBuffer(blackHoles.Count, size);
     *  //_uvBuffer = new ComputeBuffer(blackHoles.Count, size);
     * }*/

    private void SetShaderParameters()
    {
        //RayTracingShader.SetTexture(0, "_SkyboxTexture", SkyboxTexture);
        RayTracingShader.SetMatrix("_CameraToWorld", _camera.cameraToWorldMatrix);
        RayTracingShader.SetMatrix("_CameraInverseProjection", _camera.projectionMatrix.inverse);
        RayTracingShader.SetVector("_PixelOffset", new Vector2(Random.value, Random.value));
        RayTracingShader.SetFloat("_Seed", Random.value);

        Vector3 l = DirectionalLight.transform.forward;

        RayTracingShader.SetVector("_DirectionalLight", new Vector4(l.x, l.y, l.z, DirectionalLight.intensity));

        if (_sphereBuffer != null)
        {
            RayTracingShader.SetBuffer(0, "_Spheres", _sphereBuffer);
        }
        if (_blackHoleBuffer != null)
        {
            RayTracingShader.SetBuffer(0, "_BlackHoles", _blackHoleBuffer);
        }

        RayTracingMeshRenderer.pushGameObjects(RayTracingShader);
    }
示例#2
0
    private void SetShaderParameters()
    {
        //RayTracingShader.SetTexture(0, "_SkyboxTexture", SkyboxTexture);
        RayTracingShader.SetMatrix("_CameraToWorld", _camera.cameraToWorldMatrix);
        RayTracingShader.SetMatrix("_CameraInverseProjection", _camera.projectionMatrix.inverse);
        RayTracingShader.SetVector("_PixelOffset", new Vector2(Random.value, Random.value));
        RayTracingShader.SetFloat("_Seed", Random.value);

        outBuffer = new ComputeBuffer(1, 4 * 3);

        RayTracingShader.SetBuffer(0, "DEBoutput", outBuffer);


        var objects = UnityEngine.Object.FindObjectsOfType <Mass>();

        RayTracingShader.SetInt("_bhCount", objects.Length);

        if (objects.Length > 0)
        {
            List <sBlackHole> blackHoles = new List <sBlackHole>();

            for (int i = 0; i < objects.Length; ++i)
            {
                var        comp = objects[i];
                sBlackHole bhx  = new sBlackHole();

                //var comp = b.GetComponent<BlackHole>();
                bhx.rs       = comp.rs;
                bhx.position = comp.transform.position;
                blackHoles.Add(bhx);
            }

            if (_blackHoleBuffer != null)
            {
                _blackHoleBuffer.Release();
            }
            if (blackHoles.Count > 0)
            {
                int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(sBlackHole));
                _blackHoleBuffer = new ComputeBuffer(blackHoles.Count, size);
                _blackHoleBuffer.SetData(blackHoles);
            }
            if (_blackHoleBuffer != null)
            {
                RayTracingShader.SetBuffer(0, "_BlackHoles", _blackHoleBuffer);
            }
        }
        else
        {
            if (_blackHoleBuffer != null)
            {
                _blackHoleBuffer.Release();
            }
            List <sBlackHole> blackHoles = new List <sBlackHole>();
            sBlackHole        bhx        = new sBlackHole();
            bhx.position = new Vector3(0, 0, 0);
            bhx.rs       = 0;
            blackHoles.Add(bhx);

            int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(sBlackHole));
            _blackHoleBuffer = new ComputeBuffer(blackHoles.Count, size);
            _blackHoleBuffer.SetData(blackHoles);
            RayTracingShader.SetBuffer(0, "_BlackHoles", _blackHoleBuffer);
        }



        RayTracingMeshRenderer.pushGameObjects(RayTracingShader);
    }