Search Unity

Question Setting material property from main camera not working correctly in build

Discussion in 'Editor & General Support' started by rcrsnt, Sep 16, 2021.

  1. rcrsnt

    rcrsnt

    Joined:
    Sep 25, 2020
    Posts:
    2
    Hello, I encountered a weird behaviour that's breaking my build and I have no idea of where to even start looking so I thought of asking here.
    I'm working with Unity 2019.4.29f1 and URP 7.7.1, and I'm using the Blit renderer feature from https://github.com/Cyanilux/URP_BlitRenderFeature .

    In my project I created a blit pass, that has a material with its shader. From a script, attached to a game object in my scene, I basically have to get the main camera, store it in a field for later use, and set some camera properties to the material used for the blit. In particular, I set a float to be equal to Camera.fieldOfView, which works fine in the editor, but breaks for me in build.

    I was able to recreate a minimal example for my problem so I'm gonna show that.
    This is my blit, material and shader setup. I'm saving the material in the Resources folder.
    blitpass.png material.png shader.png
    The shader is made in Shadergraph, and for this example I'm just taking the fov property and using it to lerp between 0 and 120 to get a color value.
    The script attached to my game object looks like this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TesterScript : MonoBehaviour
    6. {
    7.     public Material material;
    8.     Camera mainCamera;
    9.  
    10.  
    11.     private void Start() {
    12.         mainCamera = Camera.main;
    13.  
    14.         Debug.Log("Start value: "+material.GetFloat("_verticalViewAngle"));
    15.         Debug.Log("Setting value to "+ mainCamera.fieldOfView);
    16.         material.SetFloat("_verticalViewAngle", mainCamera.fieldOfView);
    17.     }
    18.  
    19.     private void Update() {
    20.         Debug.Log("Material current value:" + material.GetFloat("_verticalViewAngle"));
    21.         Debug.Log("Camera fov current value:" + mainCamera.fieldOfView);
    22.         /* do other stuff with mainCamera */
    23.     }
    24. }
    Note that I initialize the property to 0 by hand, so in the editor before I hit play everything is just black. Then when I start the game my scene looks like this
    scene in editor.png

    However, when I build the project and run it everything just remains black
    scene in build.png
    although the log file says something different (I can't explain that start value:1)

    Start value: 1
    (Filename: C:\buildslave\unity\build\Runtime/Export/Debug/Debug.bindings.h Line: 39)
    Setting value to 60
    (Filename: C:\buildslave\unity\build\Runtime/Export/Debug/Debug.bindings.h Line: 39)
    Material current value:60
    (Filename: C:\buildslave\unity\build\Runtime/Export/Debug/Debug.bindings.h Line: 39)
    Camera fov current value:60
    (Filename: C:\buildslave\unity\build\Runtime/Export/Debug/Debug.bindings.h Line: 39)
    Material current value:60
    (Filename: C:\buildslave\unity\build\Runtime/Export/Debug/Debug.bindings.h Line: 39)
    Camera fov current value:60
    (Filename: C:\buildslave\unity\build\Runtime/Export/Debug/Debug.bindings.h Line: 39)


    Interestingly, when testing on my actual shader and script (instead of this toy example) in the log file I was getting a completely different number for the "Material current value", specifically 91.49284, which however still didn't match the actual result on my screen.

    As I mentioned, I have no idea of what might be causing this issue so any help is greatly appreciated!
     
  2. rcrsnt

    rcrsnt

    Joined:
    Sep 25, 2020
    Posts:
    2
    Ok after a couple more hours of testing different things, I have to update this. Apparently just changing the reference of the property in the shader fixed the issue.
    Am I missing something obvious here? I didn't find anywhere that "_verticalViewAngle" would be a reserved name, like a built-in shader variable or anything of the sort.