Search Unity

Nvidia ShaderFx standart matrices equavelant in unity?

Discussion in 'Shaders' started by aubergine, Nov 6, 2010.

  1. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Can someone please show the equavalents of the following standart matrices.

    float4x4 WorldITXf : WorldInverseTranspose = ???
    float4x4 WvpXf : WorldViewProjection = ???
    float4x4 WorldXf : World = ???
    float4x4 ViewIXf : ViewInverse = ???
    float4x4 WorldIXf: WorldInverse = ???

    float4x4 CamPos: EyePosition = ???
    .
    .
    etc.


    Even better, can someone show how to convert a standart shaderfx phong effect into unity?

    I believe so many people want to know this.

    thanks
     
  2. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    I know that help page, but the problem is it doesnt say which one is WorldInverseTranspose forexample, so i need someone to translate.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Neither is it, that matrix isn't present unless you calculate it yourself from World
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Unity uses OpenGL nomenclature. WorldInverseTranspose would be UNITY_MATRIX_IT_MV.
     
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Well, for learning purposes, can anyone teach me and everybody else here how to convert a HLSL shader into unity shader thing?

    I dont know much of HLSL either but i do understand most of the maths and code written there.

    Here is a basic relief mapping shader, if anyone can show how to make it work in unity, ill appreciate.

    Code (csharp):
    1. //====================================================
    2. // Relief Mapping
    3. //====================================================
    4. // By EVOLVED
    5. // [url]www.evolved-software.com[/url]
    6. //====================================================
    7.  
    8. //--------------
    9. // un-tweaks
    10. //--------------
    11.    matrix WorldVP:WorldViewProjection;
    12.    matrix World:World;  
    13.    matrix ViewInv:ViewInverse;
    14.  
    15. //--------------
    16. // tweaks
    17. //--------------
    18.    float3 Ambient={0.3f,0.3f,0.3f};
    19.    float3 LightPosition={150.0f,150.0f,0.0f};    
    20.    float3 LightColor={1.0f,1.0f,1.0f};    
    21.    float LightRange=200.0f;    
    22.    float SpecularPow=32.0f;  
    23.    float depth=0.03;
    24.    float4 FogColor={0.8f,0.8f,0.8f,1.0f};
    25.    float FogRange=2500.0f;
    26.    float Alpha=1.0f;
    27.  
    28. //--------------
    29. // Textures
    30. //--------------
    31.    texture BaseTX <string Name="";>;   
    32.    sampler2D Base = sampler_state
    33.       {
    34.     texture = <BaseTX>;
    35.       };
    36.    texture NormalTX <string Name="";>; 
    37.    sampler2D Normal = sampler_state
    38.       {
    39.     texture = <NormalTX>;
    40.       };
    41.  
    42. //--------------
    43. // structs
    44. //--------------
    45.    struct input
    46.      {
    47.     float4 Pos:POSITION;
    48.     float2 UV:TEXCOORD;
    49.     float3 Normal:NORMAL;
    50.     float3 Tangent:TANGENT;
    51.     float3 Binormal:BINORMAL;
    52.      };
    53.    struct output
    54.      {
    55.     float4 OPos:POSITION;
    56.     float2 Tex:TEXCOORD0;
    57.     float3 LightVec:TEXCOORD1;
    58.     float3 Attenuation:TEXCOORD2;
    59.     float3 ViewVec:TEXCOORD3;
    60.     float3 ViewPos:TEXCOORD4;
    61.     float3 VNormal:TEXCOORD5;
    62.     float Fog:FOG;
    63.      };
    64.  
    65. //--------------
    66. // vertex shader
    67. //--------------
    68.    output VS(input IN)
    69.      {
    70.     output OUT;
    71.     OUT.OPos=mul(IN.Pos,WorldVP);
    72.     OUT.Tex=IN.UV;
    73.     float3x3 TBN={IN.Tangent,IN.Binormal,IN.Normal};
    74.     TBN=transpose(mul(TBN,World));
    75.     float3 WPos=mul(IN.Pos,World);
    76.     float3 LightPos=LightPosition-WPos;
    77.     float3 ViewPos=ViewInv[3].xyz-WPos;
    78.     OUT.LightVec=mul(LightPos,TBN);
    79.     OUT.Attenuation=-LightPos/LightRange;
    80.     OUT.ViewVec=mul(ViewPos,TBN);  
    81.     OUT.ViewPos=-ViewPos;
    82.     OUT.VNormal=mul(IN.Normal,World);
    83.     OUT.Fog=1-saturate(dot(ViewPos/FogRange,ViewPos/FogRange));
    84.     return OUT;
    85.      }
    86.  
    87. //--------------
    88. // pixel shader
    89. //--------------
    90.     float GetDepth(in float2 Uv,in float2 Displace)
    91.      {
    92.     float Height=0.0;
    93.     float depth=1.0;
    94.     float Inc=0.125f;
    95.     for( int i=0;i<8;i++ )
    96.     {
    97.      Height+=Inc;
    98.      float HeightTex=tex2D(Normal,Uv+Displace*Height).w;
    99.       if (depth>0.995) 
    100.       if (Height>=HeightTex) depth=Height; 
    101.     }
    102.     Height=depth;
    103.     for( int i=0;i<4;i++ )
    104.     {
    105.      Inc*=0.5;
    106.      float HeightTex=tex2D(Normal,Uv+Displace*Height).w;
    107.           if (Height>=HeightTex)
    108.           {
    109.            depth=Height;
    110.            Height-=2*Inc;
    111.           }
    112.          Height=Height+Inc;
    113.     }
    114.     return depth;
    115.      }
    116.     float4 PS(output IN)  : COLOR
    117.      {
    118.     float2 Uv=IN.Tex;  
    119.     float3 View=normalize(IN.ViewVec);
    120.     float ViewN=dot(IN.VNormal,normalize(IN.ViewPos));
    121.     float2 Displace=((View*depth)/ViewN).xy;
    122.     float  Ray=GetDepth(Uv,Displace);  
    123.     float2 NewUv=(Uv+Displace*Ray);
    124.     float4 Texture=tex2D(Base,NewUv);
    125.     float3 NormalMap=tex2D(Normal,NewUv)*2-1;  NormalMap.y=-NormalMap.y;
    126.     NormalMap=normalize(NormalMap);
    127.     float3 LightV=normalize(IN.LightVec);
    128.     float Normal=saturate(dot(reflect(-View,NormalMap),LightV));
    129.     Normal=pow(Normal,SpecularPow)+saturate(dot(NormalMap,LightV));
    130.     float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
    131.     float3 Light=PixelLight*LightColor;
    132.     return float4(Texture*((Normal*Light)+Ambient),Texture.w*Alpha);
    133.      }
    134.  
    135. //--------------
    136. // techniques  
    137. //--------------
    138.    technique Relief
    139.       {
    140.     pass p1
    141.       {    
    142.     vertexShader = compile vs_2_0 VS();
    143.     pixelShader  = compile ps_2_a PS();
    144.     FOGCOLOR=(FogColor);
    145.     FOGENABLE=TRUE;    
    146.       }
    147.       }
     
  7. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    I think a more basic example will ensure that someone helps you out ).
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Honestly, I don't think I've ever seen someone get "help converting a shader from X" in this forum. What people asking this question don't realize is that it's a slightly different version of "teach me how to write this shader". That's not going to happen on a web forum.

    To do this yourself, you need to know what the shader does (pretty much what the math on every line means), and you need to know how to write Shaderlab and Cg fluently. If you don't know these things, you'll have to learn them, which will take a lot of effort, or get someone who already knows them to do the translation. Getting someone else to do it usually costs money, but it's probably the cheaper option if your time is worth anything.
     
  9. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Nevermind.
     
  10. LumaPxxx

    LumaPxxx

    Joined:
    Oct 3, 2010
    Posts:
    339
    wait for more surfaceshader reference and example come,then you could do it.
    but now i think few people could do it in unity3.0,maybe some could do it in unity2.6.
     
  11. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
  12. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Thank you kind sir, that was what i needed indeed.