Search Unity

Question RenderWithShader behaves incorrectly, different from manual assigning of material in the editor

Discussion in 'Shaders' started by Unique-Player, Jan 24, 2023.

  1. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    78
    I have a post effect shader which uses RenderTexture that is created on OnPreRender with RenderWithShader command, it uses UV2 and a texture that is assigned to the PropertyBlock of an object by script.
    When i assign Replacement Shader manually in the editor and start playing, PropertyBlock is read correctly and shader works as it is supposed to, but when i use Camera.RenderWithShader with the same shader and look at RenderTexture it is displaying object as solid white instead of texture from PropertyBlock.

    I am sure that Unity is doing something that is not mentioned anywhere, any help would appreciated.
     
  2. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    78
    Anybody? I have literally no idea where to dig.

    To demonstrate:
    When i assign "Replacement Shader" (Shader which is used when rendering to RenderTexture) to my model and hit play the following RenderTexture is generated:
    Correct.png
    You can see the lines, the result is correct. "Replacement Shader" is reading the texture from PropertyBlock of the model and is showing desired result.

    But, when i assign main material to my model, hit play and observe RenderTexture, the following is shown:
    Incorrect.png
    You can not see the lines, the result is incorrect. Even though the result should be identical since the "Replacement Shader" or PropertyBlock is not being modified in any way. The only thing changing is that i am not applying "Replacement Shader" to my model.

    I have tried applying the "Replacement Shader" to completely different object in the scene, same result. Moving textures, shaders and renderTextures to Resource folder thinking maybe some part of that is not being initialized, no success.

    I changed PropertyBlock texture to completely black one, and the Incorrect RenderTexture started showing black solid color, which makes me think that texture from PropertyBlock is read and applied, but probably something happens with UVs. I am not sure what is the problem though.