Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Any help appreciated: Ray "tracing" attempt isn't working

Discussion in 'Shaders' started by TheCuriousHobbyist, Oct 16, 2019.

  1. TheCuriousHobbyist

    TheCuriousHobbyist

    Joined:
    Nov 13, 2018
    Posts:
    12
    Screen Shot 2019-10-16 at 4.04.23 AM.png
    So the image above is my makeshift camera. What I am trying to do is to cast a bunch of rays from the position of the center, to an interpolation between 4 objects that are just there for the position. By the way, the lines are using a script on each of those points that sets a point of a line renderer to the position of the point each frame (Update) and I am using ExecuteInEditMode.

    I have an editor script that adds a render option onto the camera script. When you press this option, it updates the position of the lines based on the focal length and shift parameters. And it's supposed to cast a bunch of rays out from the camera. I am using a For loop for int Y, inside another For loop for int X, which increments them as long as they are under the width and height of a texture that I can drag into a slot. It raycasts from the center to an interpolation of another interpolation of the upper left and right points by x / Texture.width, and lower left/right points, by y / height.

    I also have a simple material class placed on the ball, it has a gradient that I am setting each pixel to a point on, by the angle between the hit normal and the ray direction / 90- this is to crush the 0-90 range to 0-1 so that if it hits an oblique angle it evaluates the gradient by 1, and gets the right color. (again, each time the for loop iterates.) I just want a simple fresnel. Nothing fancy looking.
    Unfortunately it isn't updating the texture at all. It is running the for loops, but I think it might not be running the raycasts. Here is the script.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class RayCamera : MonoBehaviour
    5. {
    6.     [Header("Camera Setup")]
    7.     public Transform uL;
    8.     public Transform uR;
    9.     public Transform lL;
    10.     public Transform lR;
    11.     public Transform plane;
    12.     public float focalLength;
    13.     public Vector2 shift;
    14.     public float Clip;
    15.     public bool trace;
    16.     [Space, Header("Texture2D")]
    17.     public Texture2D result;
    18.     Vector3 dir;
    19.     RaycastHit hit;
    20.     public int MaxDrawDistance;
    21.     public Vector2 OriginPoint;
    22.     public Color color;
    23.  
    24.     public void StartTrace()
    25.     {
    26.         plane.localPosition = new Vector3(shift.x, shift.y, focalLength);
    27.         if (trace)
    28.             Trace();
    29.     }
    30.     public void Trace ()
    31.     {
    32.         for (int x = 0; x < result.width; x++)
    33.         {
    34.             for (int y = 0; y < result.height; y++)
    35.             {
    36.                 dir = Vector3.Lerp(Vector3.Lerp(-transform.position + uL.position, -transform.position + uR.position, x / result.width), Vector3.Lerp(-transform.position + lL.position, -transform.position + lR.position, x / result.width), y / result.height);
    37.                 if (Physics.Raycast(transform.position, dir, out hit, Clip))
    38.                 {
    39.                     Debug.Log(hit.normal);
    40.                     result.SetPixel(x, y, hit.collider.GetComponent<MaterialInfo>().fresnel.Evaluate(Vector3.Angle(hit.normal, dir) / 90.0f));
    41.                 }
    42.                 else result.SetPixel(x, y, color);
    43.             }
    44.         }
    45.         result.Apply();
    46.     }
    47.  
    48.     public void DrawEllipse ()
    49.     {
    50.         for (int x = 0; x < result.width; x++)
    51.         {
    52.             for (int y = 0; y < result.height; y++)
    53.             {
    54.                 if (Vector2.Distance(new Vector2(x, y), OriginPoint) <= MaxDrawDistance)
    55.                     result.SetPixel(x, y, color);
    56.             }
    57.         }
    58.         result.Apply();
    59.     }
    60. }
    Also, I tested a distance function to draw an ellipse, and it does indeed work. Ugh.. Scripting is just fiddly and convoluted to me honestly. It just doesn't work for no apparent reason sometimes. Any help is appreciated.
     
    Last edited: Oct 22, 2019