private void UpdateRaytrace()
    {
        // make sure the canvas is correct
        int newWidth = (int)(Screen.width / _downsampling);

        if (_canvasTex == null || _canvasTex.width != newWidth)
        {
            _canvasTex            = new Texture2D(newWidth, (int)(Screen.height / _downsampling));
            _canvasTex.filterMode = FilterMode.Point;
        }

        // get main camera
        Camera cam = Camera.main;

        // get width and height
        int width  = _canvasTex.width,
            height = _canvasTex.height;

        // get z pos
        float camZ  = cam.transform.position.z;
        Color black = Color.black;

        // calculate color for each pixel on the canvas
        for (float i = 0; i < width; ++i)
        {
            for (float j = 0; j < height; ++j)
            {
                // Get Ray from pixel and zpos
                Vector3 pos = new Vector3(i / width, j / height, camZ);
                Ray     ray = cam.ViewportPointToRay(pos);


                RaycastHit raycastHitInfo;
                bool       didHit = Physics.Raycast(ray, out raycastHitInfo);
                if (didHit)
                {
                    // get the color from the RayTraceRenderer
                    RayTraceRenderer comp = raycastHitInfo.collider.GetComponent <RayTraceRenderer>();
                    if (comp != null)
                    {
                        _canvasTex.SetPixel((int)i, (int)j, comp.CalculateColor(raycastHitInfo, _recursionDepth, _ambience, ray));
                    }
                    else
                    {
                        _canvasTex.SetPixel((int)i, (int)j, black);
                    }
                }
                else
                {
                    // otherwise just set the color to black
                    _canvasTex.SetPixel((int)i, (int)j, black);
                }
            }
        }

        _canvasTex.Apply();
    }
Exemplo n.º 2
0
    private Color Bounce(Ray ray, int depth)
    {
        RaycastHit raycastHitInfo;

        if (Physics.Raycast(ray, out raycastHitInfo))
        {
            RayTraceRenderer comp = raycastHitInfo.collider.GetComponent <RayTraceRenderer>();
            if (comp != null)
            {
                return(comp.CalculateColor(ray, raycastHitInfo, depth - 1));
            }
        }

        return(Color.clear);
    }
    protected Color Bounce(Ray inRay, int depth, float ambience)
    {
        if (depth > 0)
        {
            RaycastHit outHit;
            bool       hit = Physics.Raycast(inRay, out outHit);
            if (hit)
            {
                RayTraceRenderer renderer = outHit.transform.gameObject.GetComponent <RayTraceRenderer>();
                if (renderer)
                {
                    return(renderer.CalculateColor(outHit, --depth, ambience, inRay));
                }
            }
        }


        return(Color.clear);
    }