Search Unity

Shader does not work in the compiled version

Discussion in 'Shaders' started by Marcos-Schultz, Aug 8, 2016.

  1. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    Hello, I created a simple clouds system where I use a script, a shader and a texture.

    Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class SistemaDeNuvens : MonoBehaviour {
    4.     public Shader ShaderDoObjeto;
    5.     public Texture TexturaShader;
    6.     void Awake(){
    7.         GameObject bola = new GameObject("EsferaNuvens");
    8.         bola.AddComponent (typeof(MeshFilter));
    9.         bola.AddComponent (typeof(MeshRenderer));
    10.         GameObject bolaTemp = GameObject.CreatePrimitive (PrimitiveType.Sphere);
    11.         bola.GetComponent<MeshFilter> ().mesh = bolaTemp.GetComponent<MeshFilter> ().mesh;
    12.         Destroy (bolaTemp);
    13.         bola.transform.localScale = new Vector3 (20*2000.0f, 2000.0f, 20*2000.0f);
    14.         bola.GetComponent<Renderer> ().material.shader = ShaderDoObjeto;
    15.         bola.GetComponent<Renderer> ().material.SetTexture ("_MainTex", TexturaShader);
    16.  
    17.     }
    18.     void Start(){
    19.         Camera.main.farClipPlane = 51 * 2000.0f;
    20.     }
    21.     void Update () {
    22.         Shader.SetGlobalFloat("_velocidade", Time.time*5.0f);
    23.         Shader.SetGlobalFloat("_altura", 17.0f);
    24.         Shader.SetGlobalFloat("_quantidade", 0.2f);
    25.         Shader.SetGlobalFloat("_claridade", 9.0f);
    26.         Shader.SetGlobalFloat("_tamanhoNuvem", (1/1.4f));
    27.         Shader.SetGlobalFloat("_intensidade", 30.0f);
    28.         Shader.SetGlobalFloat("_opacidadeHorizonte", 50.0f);
    29.     }
    30. }
    Shader script:
    Code (CSharp):
    1. Shader "Nuvens" {
    2.     Properties {
    3.          _MainTex ("Textura", 2D) = "white" {}
    4.          _Cutoff ("Alpha cutoff", Range(0,1)) = 1
    5.     }
    6.     SubShader {
    7.         Tags {"Queue"="Transparent+100" "IgnoreProjector"="True" "RenderType"="Transparent"}
    8.         LOD 400 //level of detail
    9.         Lighting On
    10.         Cull Off
    11.         ZWrite Off
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.  
    14.         Pass{ //dentro da pass, mapeia todo o objeto
    15.             Tags { "LightMode" = "ForwardBase" }
    16.        
    17.              CGPROGRAM
    18.            
    19.              #pragma target 3.0
    20.              #pragma fragmentoption ARB_precision_hint_fastest
    21.            
    22.              #pragma vertex vert
    23.              #pragma fragment frag
    24.              #pragma multi_compile_fwdbase
    25.                        
    26.              #include "UnityCG.cginc"
    27.              #include "AutoLight.cginc"
    28.              sampler2D _MainTex;
    29.              float _velocidade;
    30.              float _altura;
    31.              float _quantidade;
    32.              float _claridade;
    33.              float _tamanhoNuvem;
    34.              float _opacidadeHorizonte;
    35.              float _intensidade;
    36.              float getCloud(float2 uv, float scale, float disp) {
    37.                     float y = 0.0f;
    38.  
    39.                     int NN = 5;
    40.                     for(int i=0;i < NN; i++) {
    41.                         float k = scale*i  + 0.11934;
    42.                         y+= tex2D( _MainTex, k*uv + float2(0.1234*i*_velocidade*0.015 - 0.04234*i*i*_velocidade*0.015 + 0.9123559 + 0.23411*i , 0.31342  + 0.5923*i + disp) ).x;
    43.                     }
    44.  
    45.                     y /= 0.5f*NN;
    46.                     return clamp( pow(_quantidade/y, _tamanhoNuvem),0,1.0);
    47.              }
    48.              float getNormal(float2 uv, float scale, float dst, out float3 n, float nscale, float disp) {
    49.                     float height = getCloud(uv, scale, disp);
    50.                     int N =5;
    51.                     for (int i=0;i<N;i++) {
    52.                  
    53.                         float2 du1 = float2(dst*cos((i)*2*3.14159 / (N)), dst*sin(i*2*3.14159/(N)));
    54.                         float2 du2 = float2(dst*cos((i+1)*2*3.14159 / (N)), dst*sin((i+1)*2*3.14159/(N)));
    55.                      
    56.                         float hx = getCloud(uv + du1, scale, disp);
    57.                         float hy = getCloud(uv + du2, scale, disp);
    58.                  
    59.                         float3 d2 = float3(0,height*nscale,0) - float3(du1.x,hx*nscale,du1.y);
    60.                         float3 d1 = float3(0,height*nscale,0) - float3(du2.x,hy*nscale,du2.y);
    61.                  
    62.                         n = n + normalize(cross(d1,d2));
    63.                     }
    64.                     n = normalize(n);
    65.                     return height;
    66.                  
    67.             }
    68.             struct v2f{
    69.  
    70.                  float4 pos : POSITION;
    71.                  float4 texcoord : TEXCOORD0;
    72.                  float3 normal : TEXCOORD1;
    73.                  float4 uv : TEXCOORD2;
    74.                  float3 worldPosition: TEXCOORD3;
    75.                  LIGHTING_COORDS(3,4)
    76.              };
    77.            
    78.              v2f vert (appdata_base v){
    79.  
    80.                  v2f o;
    81.                  o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    82.                  o.uv = v.texcoord;
    83.                  o.normal = normalize(v.normal).xyz;
    84.                  o.texcoord = v.texcoord;
    85.                  o.worldPosition = mul (unity_ObjectToWorld, v.vertex).xyz;
    86.                  TRANSFER_VERTEX_TO_FRAGMENT(o);
    87.                
    88.                  return o;
    89.              }
    90.            
    91.             fixed4 frag(v2f IN) : COLOR {
    92.             float3 worldSpacePosition = IN.worldPosition;
    93.             float3 N;
    94.             float3 lightDir = _WorldSpaceLightPos0;
    95.                  
    96.             float3 viewDirection = normalize(_WorldSpaceCameraPos - worldSpacePosition);
    97.             float dist = clamp(1.0/pow(length(0.5 + 0.0001*_opacidadeHorizonte*(_WorldSpaceCameraPos - worldSpacePosition)),1.0),0,1);
    98.             float2 newPos = worldSpacePosition.xz*0.00005;
    99.                  
    100.             float x = getNormal(newPos, 1.73252*_altura*0.1, 0.00375f, N, 0.05*0.75f, worldSpacePosition.y/1381.1234f + _velocidade*0.0002);
    101.             float albedoColor = (0.5f*x, 0.9f*x, 0.9f*x);
    102.             float3 norm=float3(0,1,0);
    103.             float globalLight = saturate(dot(norm, lightDir));
    104.             if (IN.normal.y<0) discard;
    105.             float spec = pow(max(0.0, dot(
    106.                   reflect(-lightDir, N),
    107.                   viewDirection)), 2);
    108.          
    109.             float  NL = 0.3*_claridade*(1 + spec + saturate((pow((dot(-N, lightDir)),1))));
    110.             albedoColor*=NL*globalLight;
    111.          
    112.             float4 c = float4(0,0,0,0);
    113.             c.a = _intensidade*pow(x,2)*dist;
    114.             c.rgb = albedoColor;
    115.             return c;
    116.              }
    117.              ENDCG
    118.          }
    119.      }
    120. Fallback "Transparent/Cutout/Diffuse"
    121. }
    Script image:


    The system works perfectly in the engine, but when the game is compiled, nothing happens, no cloud is generated.

    I think the problem is in the shader, but could not identify what type of error may be happening.


    UNITYPACKAGE: http://www.mediafire.com/download/ge91le8rxins116/Nuvens.unitypackage
     
    Last edited: Aug 9, 2016
  2. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    I removed the fallback shader, and it made the object is pink in the compiled version of the game.

    This indicates that the computer could not play the shader in the compiled version. But why?

    Why the engine works and compiled version does not? What poss do to solve?
     
  3. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    Which device are you testing?
     
  4. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    windows - PC

    8 GB ram
    GTX 650 TI
     
  5. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  6. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  7. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  8. Deleted User

    Deleted User

    Guest

    I'd guess you need to go to EDIT -> PROJECT SETTINGS -> GRAPHICS and increase the number of "included shaders" so that there is a new empty slot, and add your shader to the list... assuming you didn't already add it. Any shader that is selected via a script like yours is... the compiler doesn't know that it needs to be included in the compiled version. But adding it to that list will probably fix your problem.
     
    Marcos-Schultz likes this.
  9. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    I've done it, it was the first thing I tried to solve the problem but not solved.
     
  10. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  11. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  12. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  13. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  14. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  15. Deleted User

    Deleted User

    Guest

    I donno what else could do it, except if your shader or ANY of its dependencies (materials, renderers, etc) are being set within blocks of code that are inside of classes that derive from ":Editor", or are surrounded by #If UNITY_EDITOR statements, or anything similar. Those lines of code won't work in the compiled version, and if lines of code within those areas are initializing the shader, renderer or materials in any way, then that might explain the pink in the compiled version.

    A test might be to create a new scene, drop an object with no scripts on it into the scene, and put that particular shader onto that object... no scripts at all. Try to play that scene in the compiled version and see if it is pink. If so, then you know its something in the build settings, project settings, or the shader itself. If it isn't pink, then try to back track the problem by adding in your scripts bit by bit until it turns pink, and you'll find the problem.
     
    Marcos-Schultz likes this.
  16. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
  17. Deleted User

    Deleted User

    Guest

    It listed quite a few errors with your shader. I fixed the errors, and left some comments on the lines I changed. I'm attaching the shader with my fixes.
     

    Attached Files:

    Marcos-Schultz likes this.
  18. Deleted User

    Deleted User

    Guest

    In the future, you can click the shader file in the project view, and then in the inspector window, click the "compile and show code" button. If it has any errors, those errors will be in the Console window, so you can see what the errors are. Even though it played in the editor, it wouldn't play in the builded version because it had errors.
     
    Marcos-Schultz likes this.
  19. Deleted User

    Deleted User

    Guest

    PS... in case I don't come back here... I did run it in a build (exe) version and the clouds were working after I made those edits. So hope it works for you!
     
    Marcos-Schultz likes this.
  20. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    Man, thank you, thank!

    I've been more than a month trying to solve this problem ...

    Thank you very much!