Search Unity

tri planar with standard shader

Discussion in 'Shaders' started by Elzean, Mar 13, 2020.

  1. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584
    Hey,

    i have this triplanar shader that keep working the scaling and rotation of the object but it is unlit and i was wondering if someone knew how to have the same with the standard shader to have some light / shadows as its hard to see the blockout shape with the unlit.

    I tried to copy some pieces of this shader in a new standard shader without success...


    Code (CSharp):
    1. Shader "Unlit/ObjectSpaceTriplanar"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" "DisableBatching"="True" }
    10.         LOD 100
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #pragma multi_compile_fog
    17.             #include "UnityCG.cginc"
    18.             struct appdata
    19.             {
    20.                 float4 vertex : POSITION;
    21.                 float3 normal : NORMAL;
    22.             };
    23.             struct v2f
    24.             {
    25.                 float4 pos : SV_POSITION;
    26.                 float3 uvPos : TEXCOORD0;
    27.                 float3 uvBlend : TEXCOORD1;
    28.                 UNITY_FOG_COORDS(2)
    29.             };
    30.             sampler2D _MainTex;
    31.             float4 _MainTex_ST;
    32.             v2f vert (appdata v)
    33.             {
    34.                 v2f o;
    35.                 o.pos = UnityObjectToClipPos(v.vertex);
    36.                 float3 scale = float3(
    37.                     length(unity_ObjectToWorld._m00_m10_m20),
    38.                     length(unity_ObjectToWorld._m01_m11_m21),
    39.                     length(unity_ObjectToWorld._m02_m12_m22)
    40.                     );
    41.                 o.uvPos = v.vertex.xyz * scale;
    42.                 o.uvBlend = v.normal.xyz / scale;
    43.                 o.uvBlend /= dot(abs(o.uvBlend), float3(1,1,1));
    44.                 UNITY_TRANSFER_FOG(o,o.pos);
    45.                 return o;
    46.             }
    47.             fixed4 frag (v2f i) : SV_Target
    48.             {
    49.                 float3 blend = abs(i.uvBlend);
    50.                 float2 uv;
    51.                 if (blend.x > max(blend.y, blend.z)) {
    52.                     uv = i.uvPos.yz;
    53.                 } else if (blend.z > blend.y) {
    54.                     uv = i.uvPos.xy;
    55.                 } else {
    56.                     uv = i.uvPos.xz;
    57.                 }
    58.                 fixed4 col = tex2D(_MainTex, TRANSFORM_TEX(uv, _MainTex));
    59.                 UNITY_APPLY_FOG(i.fogCoord, col);
    60.                 return col;
    61.             }
    62.             ENDCG
    63.         }
    64.     }
    65.     Fallback "VertexLit"
    66. }
    Thanks !
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Elzean likes this.