Search Unity

shader not working after build

Discussion in 'Shaders' started by Unity-Artcraft, Jan 24, 2020.

  1. Unity-Artcraft

    Unity-Artcraft

    Joined:
    Jul 28, 2018
    Posts:
    85
    The deadline for my college project is on my monday and I realize now, the camera fog shader I found isn't working at all after build. :( Why? It does work in the editor. I put the shader into a resource folder, also included in "Always include shaders"... even put it on a primitive, shader.find isn't used either, its a script on my camera, accessing the shader with public... i have no clues about shaders, how can I fix this? :(

    Edit: shader = Resources.Load<Shader>("Prefabs/Materials/Shaders/Fog"); seems or would work but it turns everything into a white mess :(
    Edit2: okay -.- because he or she didn't found the shader -.- used the wrong path but the shader still doesnt work after build :(
    Edit3: I think there is something wrong with the shader? :/

    Code (CSharp):
    1. Shader "Hidden/Fog"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         // No culling or depth
    10.         Cull Off ZWrite Off ZTest Always
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.  
    18.             #include "UnityCG.cginc"
    19.          
    20.             // vertex input: position, UV
    21.             struct appdata {
    22.                 float4 vertex : POSITION;
    23.                 float2 uv : TEXCOORD0;
    24.             };
    25.  
    26.             struct v2f {
    27.                 float4 pos : SV_POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.                 float3 viewVector : TEXCOORD1;
    30.             };
    31.          
    32.             v2f vert (appdata v) {
    33.                 v2f output;
    34.                 output.pos = UnityObjectToClipPos(v.vertex);
    35.                 output.uv = v.uv;
    36.                 // Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
    37.                 // (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
    38.                 float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv * 2 - 1, 0, -1));
    39.                 output.viewVector = mul(unity_CameraToWorld, float4(viewVector,0));
    40.                 return output;
    41.             }
    42.  
    43.             sampler2D _MainTex;
    44.             sampler2D _CameraDepthTexture;
    45.             float4 params;
    46.  
    47.             float4 colA;
    48.             float4 colB;
    49.             float4 colC;
    50.             float4 colD;
    51.  
    52.             float remap(float v, float minOld, float maxOld, float minNew, float maxNew) {
    53.                 return minNew + (v-minOld) * (maxNew - minNew) / (maxOld-minOld);
    54.             }
    55.  
    56.             fixed4 frag (v2f i) : SV_Target
    57.             {
    58.                  // Create ray
    59.                 float3 rayPos = _WorldSpaceCameraPos;
    60.                 float viewLength = length(i.viewVector);
    61.                 float3 rayDir = i.viewVector / viewLength;
    62.              
    63.                 // Depth and cloud container intersection info:
    64.                 float nonlin_depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
    65.                 float depth = LinearEyeDepth(nonlin_depth) * viewLength;
    66.                 float t = saturate(exp(-(depth + params.y) * params.x*.01));
    67.  
    68.                 //return t;
    69.                 float4 fogCol = lerp(colA, colB, rayDir.y * .5 + .5 - params.w);
    70.              
    71.                 fixed4 col = tex2D(_MainTex, i.uv);
    72.                 return col * t + fogCol * (1-t);
    73.             }
    74.             ENDCG
    75.         }
    76.     }
    77. }
    78.  
     
    Last edited: Jan 25, 2020