Home Technical Talk
The BRAWL² Tournament Challenge has been announced!

It starts May 12, and ends Sept 12. Let's see what you got!

https://polycount.com/discussion/237047/the-brawl²-tournament

Problem with HLSL

KikiAlex
polycounter lvl 18
Offline / Send Message
KikiAlex polycounter lvl 18
I am trying to make a Fresnel shader, the problem is that if I move the object from the [0,0,0] position , it looks broken.
I am using 3d Max 2008.
  1. string ParamID = "0x003"; // this string tells 3ds Max to use the DXSAS compiler
  2.  
  3. float4x4 wvp : WorldViewProjection < string UIWidget = "None"; >;
  4. float4x4 ViewInverse : ViewInverse < string UIWidget = "None"; >;
  5. float4x4 World : WORLD < string UIWidget = "None"; >;
  6.  
  7. struct app2vertex
  8. {
  9. float4 position : POSITION;
  10. float4 normal : NORMAL;
  11. };
  12.  
  13. struct vertex2pixel
  14. {
  15. float4 position : POSITION;
  16. float3 eyeVec : TEXCOORD0;
  17. float4 normal : TEXCOORD1;
  18. };
  19.  
  20.  
  21. vertex2pixel vertex(app2vertex In)
  22. {
  23. vertex2pixel Out = (vertex2pixel)0;
  24. Out.position = mul(In.position, wvp);
  25. Out.normal = mul(In.normal, World);
  26. //--------------------------------------
  27. float3 worldP = mul(In.position, World);
  28. Out.eyeVec = (ViewInverse[3].xyz-worldP);
  29. return Out;
  30. }
  31.  
  32.  
  33. float4 pixel(vertex2pixel In) : COLOR
  34. {
  35. float3 P = normalize(In.eyeVec);
  36. float3 N = normalize(In.normal);
  37. float col = 1-dot(P,N);
  38. return float4(col,col,col,1);
  39. }
  40.  
  41. technique Complete
  42. {
  43. pass simple
  44. {
  45. VertexShader = compile vs_1_1 vertex();
  46. ZEnable = true;
  47. ZWriteEnable = true;
  48. CullMode = cw;
  49. AlphaBlendEnable = false;
  50. PixelShader = compile ps_2_0 pixel();
  51. }
  52. }

Thanks.

Replies

  • CrazyButcher
    Offline / Send Message
    CrazyButcher polycounter lvl 20
    1. Out.normal = mul(In.normal, World);

    you are multiplying a float4 with a float4x4 matrix, which means rotation/scale AND translation is applied. But: "Thou shalt not translate your model's normals"

    feed your normals as float3 (thats what they are!) and maybe you need to cast the float4x4 to float3x3 in that mul.
  • KikiAlex
    Offline / Send Message
    KikiAlex polycounter lvl 18
    Thanks :) it works !
    1. string ParamID = "0x003"; // this string tells 3ds Max to use the DXSAS compiler
    2.  
    3.  
    4. float3 colorB
    5. <
    6. string UIName = "Color 1";
    7. string UIWidget = "color";
    8. > = {0.9f, 0.7f, 0.4f};
    9.  
    10.  
    11.  
    12. float3 colorA
    13. <
    14. string UIName = "Color 2";
    15. string UIWidget = "color";
    16. > = {0.7f, 0.5f, 0.5f};
    17.  
    18.  
    19. float4x4 wvp : WorldViewProjection < string UIWidget = "None"; >;
    20. float4x4 ViewInverse : ViewInverse < string UIWidget = "None"; >;
    21. float3x3 World : WORLD < string UIWidget = "None"; >;
    22. struct app2vertex
    23. {
    24. float4 position : POSITION;
    25. float4 normal : NORMAL;
    26. };
    27.  
    28. struct vertex2pixel
    29. {
    30. float4 position : POSITION;
    31. float3 eyeVec : TEXCOORD0;
    32. float3 normal : TEXCOORD1;
    33. };
    34.  
    35.  
    36. vertex2pixel vertex(app2vertex In)
    37. {
    38. vertex2pixel Out = (vertex2pixel)0;
    39. Out.position = mul(In.position, wvp);
    40. Out.normal = mul(In.normal,World);
    41. //--------------------------------------
    42. float3 worldP = mul(In.position,World);
    43. Out.eyeVec = (ViewInverse[3].xyz - mul(In.position,World));
    44. return Out;
    45. }
    46.  
    47.  
    48. float4 pixel(vertex2pixel In) : COLOR
    49. {
    50. float3 P = normalize(In.eyeVec);
    51. float3 N = normalize(In.normal);
    52. float col = pow((dot(P,N)),5);
    53. float3 col2 = lerp(colorA,colorB,col);
    54. return float4(col2,col);
    55. }
    56.  
    57. technique Complete
    58. {
    59. pass simple
    60. {
    61. VertexShader = compile vs_1_1 vertex();
    62. ZEnable = true;
    63. ZWriteEnable = true;
    64. CullMode = cw;
    65. AlphaBlendEnable = true;
    66. PixelShader = compile ps_2_0 pixel();
    67. }
    68. }

    You are the Man CrazyButcher :)
    Thanks!!
  • CrazyButcher
    Offline / Send Message
    CrazyButcher polycounter lvl 20
    I'd still recommend passing normals from app to vertex as float3. because its very "unsafe" what the 4th coordinate might be. Most applications feed normals as float3, and the w coordinate is filled by driver (many cases 1.0, but it can be "undefined" as well).
    1. struct app2vertex
    2. {
    3. float4 position : POSITION;
    4. float4 normal : NORMAL; // change this to float3
    5. };
Sign In or Register to comment.