Search Unity

negative scale make wrong v.normal?

Discussion in 'Shaders' started by dreamerflyer, Jul 29, 2015.

  1. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    I use v.noraml to moving the vertex position,but when negative scale ,the moving is wrong,how to fixed this?
     

    Attached Files:

    • f1.jpg
      f1.jpg
      File size:
      48.6 KB
      Views:
      844
    • f-1.jpg
      f-1.jpg
      File size:
      41.7 KB
      Views:
      833
  2. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    it seems make the v.normal normalized when scale.x=-1,why?
     
  3. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    Show us the code.

    Generally you can't use model matrix to transform direction vectors when scale is nonuniform (as in your case, x = -1, y = z = 1), only position ones.

    For completely generic matrix you should use inverse transpose matrix (see http://docs.unity3d.com/ScriptReference/Matrix4x4-transpose.html and http://docs.unity3d.com/ScriptReference/Matrix4x4-inverse.html methods) to get corresponding transformation matrix for direction vectors.

    In your case I think there should be more simpler solution, but I am too lazy now to get the paper and calculate it.

    http://www.unknownroad.com/rtfm/graphics/rt_normals.html - explanation of why you can't transform normals with non-uniform scale matrix
     
  4. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Thanks your reply !my code is very simple.give difference vector for vetex normal .like this:mesh.normal=dirs;then in shader:float3 pos =v.vertex;pos=pos+time.y*v.normal;I don't use the matrix to scale,it is unity do it.
     
    Last edited: Jul 29, 2015
  5. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  6. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    i want to use unity_scale for control nonuniform scale and v.normal by this:

    v.normal=v.normal*unity_scale.w;it dosen't work...
     
  7. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Code (CSharp):
    1. c#
    2. Start():
    3. Matrix4x4 invertTraM=new Matrix4x4();
    4.                 invertTraM.SetTRS(Vector3.zero, Quaternion.identity, gameObject.transform.lossyScale);
    5.                 invertTraM=invertTraM.transpose;
    6.                 invertTraM=invertTraM.inverse;
    7.                 renderer.material.SetMatrix("_inverTraM",invertTraM);
    8.  
    9. shader:
    10.   half3 vNormal=mul((float3x3)_inverTraM,v.normal);
    hi mholub,I try use the matrix to fixed that,but my code not work,need your help,thanks advance!
     
  8. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  9. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    maybe this work?But new mesh in runtime how to enable this?
     
  10. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Any help?
     
  11. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    so after all any idea ?
     
  12. rohitvishwakarma1819

    rohitvishwakarma1819

    Joined:
    Feb 15, 2018
    Posts:
    14
    Hey @dreamerflyer did you find any solution ??
    please reply