Search Unity

Resolved Passing light direction to a shader to render on a texture.

Discussion in 'Scripting' started by neoshaman, Jun 28, 2022.

  1. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    Hello
    I have a problem I don't understand, yet another thing that use to works and suddenly stopped after refactoring...

    So I'm rendering using low level call to a texture, which I update everyframe. However it seems that passing the light direction no longer update for some reason.

    Code (CSharp):
    1.  
    2.  
    3. //the update function
    4.  void Update(){
    5.         foreach (var scene in scenes){
    6.            glight.updateLight();
    7.            scene.updateLight();//TODO: only when light change, refresh
    8.         }
    9.  
    10.  
    11. //...
    12.  
    13.     public void updateLight(){
    14.         GI.updateDirectLight(geometry);//TODO:only when change happen, this render and cache direct lighting on a texture
    15.         RenderSurface.show(root, GI.returnDisplay());
    16.     }
    17.  
    18.  
    19. //function that don't update
    20. public  void updateDirectLight (Mesh[] mesh){
    21.         directPass.SetVector("_MainLight",globalLights.directionalLight);
    22.         RenderSurface.applyShader(mesh,directlight,directPass);
    23.     }
    24.  
    25. // what render surface does:
    26. public static void applyShader(Mesh[] mesh, RenderTexture canvas, Material painter){
    27.         RenderSurface.initRender(canvas, painter);
    28.         RenderSurface.clear();
    29.         RenderSurface.render(mesh);
    30.         RenderSurface.close();
    31.     }
    32.  
    33. //low level graphic rendering utils
    34.     public  static void initRender(RenderTexture RT, Material mat){
    35.         Graphics.SetRenderTarget(RT);
    36.         // GL.Flush();
    37.         GL.PushMatrix();
    38.         GL.LoadOrtho();
    39.         mat.SetPass(0);
    40.     }
    41.     public  static void clear(){
    42.         GL.Clear(true, true, Color.black);
    43.     }
    44.     public  static void render(Mesh[] mesh){    
    45.         //cycle//spin//run//shift//jolt//period//turn//swing//generation//phase//index
    46.         //pass//sweep//loop//order//move//lap//walk//trace//tour//epoch//time//session
    47.         for (int pass = 0; pass < mesh.Length; pass++)
    48.         {
    49.             Graphics.DrawMeshNow(mesh[pass],  Matrix4x4.identity);
    50.         }
    51.     }
    52.     public  static void close(){
    53.         GL.PopMatrix();
    54.         Graphics.SetRenderTarget(null);
    55.  
    56.     public  static void show(GameObject frame, RenderTexture canvas){
    57.         Material CanvasFrame = frame.GetComponent<MeshRenderer>().sharedMaterial;
    58.         CanvasFrame.SetTexture("_MainTex", canvas);
    59.     }
    60.     }
    I mean the code works because:
    - it properly show the rendered texture, it just don't update it when I move the light and return the light direction data.
    - I verified that the light is properly passed because outside of play, I move the light, then use play and again and the color has changed accordingly, so it's the update that fails. A debug.log show the light data varying. Before and after the function call.
    - It used to work in the prototype spaghetti code, the code was mostly copy pasted.
    - I checked Typo in case of, i'm out of options.
     
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    I'm trying to look at the profiler, but as far as I can tell...
    upload_2022-6-28_19-46-53.png

    I emited a log from the rendering function and it seems to be called and work fine ...

    This doesn't make any sense.
     
  3. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    This time I tried to preemptively not being stuck month before asking help, well I figure out that one.