Search Unity

[SOLVED] Using matrices in shaders

Discussion in 'Shaders' started by palex-nx, May 16, 2019.

  1. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I'm using float4x4 matrix in my UI shader to ajust image colors. When app starts, I create a bunch of materials and push matrices to them using Material.SetMatrix. When app is running, it assign those materials to canvas images where appopriate, like this:

    Code (CSharp):
    1. image.material = chooseMaterial(conditions);
    Inside the fragment function I do

    Code (CSharp):
    1. color = mul(_Matrix, color);
    But the frame debugger shows what all matrices in shaders are identity matrices. I can push my matrix only with

    Code (CSharp):
    1. graphic.materialForRendering.SetMatrix(myMatrix);
    When added this line, everything working as expected.

    It looks like CanvasRenderer using some kind of material copy to render itself instead of given instance. How can I force it to use my material instance or to save my _Matrix variable value in this materialForRendering?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    The solution is to use 4 Vectors in parameters block and replace 1 matrix x vector multiplication with 4 dot products like this:

    Code (CSharp):
    1. color.x = dot(color, _row1)