Search Unity

Shader turns black when I build on the phone

Discussion in 'Shaders' started by onurduha, Dec 17, 2020.

  1. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    I don't know anything about shaders, so I found one snow track shader online. It works fine, but when I get a build on the phone, the platform looks black. I couldn't figure out why. When I do the fallback Mobile/Diffuse, it looks normal, but it doesn't draw the tracks. How can I solve this problem?

    Code (CSharp):
    1. Shader "SnowTracks"
    2. {
    3.     Properties
    4.     {
    5.         _Tess("Tessellation", Range(1,32)) = 4
    6.         _SnowColor ("Snow Color", Color) = (1,1,1,1)
    7.         _SnowTex ("Snow (RGB)", 2D) = "white" {}
    8.         _GroundColor("Ground Color", Color) = (1,1,1,1)
    9.         _GroundTex("Ground (RGB)", 2D) = "white" {}
    10.         _Splat("SplatMap", 2D) = "black" {}
    11.         _Displacement("Displacement", Range(0, 1.0)) = 0.3
    12.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    13.         _Metallic ("Metallic", Range(0,1)) = 0.0
    14.     }
    15.     SubShader
    16.     {
    17.         Tags { "RenderType"="Opaque" }
    18.         LOD 200
    19.  
    20.         CGPROGRAM
    21.         // Physically based Standard lighting model, and enable shadows on all light types
    22.         #pragma surface surf Standard fullforwardshadows vertex:disp tessellate:tessDistance
    23.  
    24.         #pragma target 4.6
    25.  
    26.         #include "Tessellation.cginc"
    27.  
    28.             struct appdata {
    29.                 float4 vertex : POSITION;
    30.                 float4 tangent : TANGENT;
    31.                 float3 normal : NORMAL;
    32.                 float2 texcoord : TEXCOORD0;
    33.             };
    34.  
    35.             float _Tess;
    36.  
    37.             float4 tessDistance(appdata v0, appdata v1, appdata v2) {
    38.                 float minDist = 10.0;
    39.                 float maxDist = 25.0;
    40.                 return UnityDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, minDist, maxDist, _Tess);
    41.             }
    42.  
    43.             sampler2D _Splat;
    44.             float _Displacement;
    45.  
    46.             void disp(inout appdata v)
    47.             {
    48.                 float d = tex2Dlod(_Splat, float4(v.texcoord.xy,0,0)).r * _Displacement;
    49.                 v.vertex.xyz -= v.normal * d;
    50.                 v.vertex.xyz += v.normal * _Displacement;
    51.             }
    52.  
    53.         sampler2D _GroundTex;
    54.         fixed4 _GroundColor;
    55.         sampler2D _SnowTex;
    56.         fixed4 _SnowColor;
    57.  
    58.         struct Input
    59.         {
    60.             float2 uv_GroundTex;
    61.             float2 uv_SnowTex;
    62.             float2 uv_Splat;
    63.         };
    64.  
    65.         half _Glossiness;
    66.         half _Metallic;
    67.  
    68.  
    69.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    70.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    71.         // #pragma instancing_options assumeuniformscaling
    72.         UNITY_INSTANCING_BUFFER_START(Props)
    73.             // put more per-instance properties here
    74.         UNITY_INSTANCING_BUFFER_END(Props)
    75.  
    76.         void surf (Input IN, inout SurfaceOutputStandard o)
    77.         {
    78.             // Albedo comes from a texture tinted by color
    79.             half amount = tex2Dlod(_Splat, float4(IN.uv_Splat, 0, 0)).r;
    80.             fixed4 c = lerp(tex2D(_SnowTex, IN.uv_SnowTex) * _SnowColor, tex2D(_GroundTex, IN.uv_GroundTex) * _GroundColor, amount);
    81.  
    82.             //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    83.             o.Albedo = c.rgb;
    84.             // Metallic and smoothness come from slider variables
    85.             o.Metallic = _Metallic;
    86.             o.Smoothness = _Glossiness;
    87.             o.Alpha = c.a;
    88.         }
    89.         ENDCG
    90.     }
    91.     FallBack "Diffuse"
    92. }
    93.  
    Code (CSharp):
    1. Shader "Unlit/DrawTracks"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Coordinate("Coordinate", Vector) = (0,0,0,-1)
    7.         _Color ("Draw Color", Color) = (1,0,0,0)
    8.         _Size("Size",Range(1,500)) = 1
    9.         _Strength("Strength",Range(0,1)) = 1
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType"="Opaque" }
    14.         LOD 100
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float2 uv : TEXCOORD0;
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             float4 _MainTex_ST;
    38.             fixed4 _Coordinate, _Color;
    39.             half _Size, _Strength;
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    46.                 return o;
    47.             }
    48.  
    49.             fixed4 frag (v2f i) : SV_Target
    50.             {
    51.                 // sample the texture
    52.                 fixed4 col = tex2D(_MainTex, i.uv);
    53.                 float draw = pow(saturate(1 - distance(i.uv, _Coordinate.xy)), 500 / _Size);
    54.                 fixed4 drawcol = _Color * (draw * _Strength);
    55.                 return saturate(col + drawcol);
    56.             }
    57.             ENDCG
    58.         }
    59.     }
    60. }
    61.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SnowBallTrack : MonoBehaviour
    6. {
    7.     public Shader drawShader;
    8.  
    9.     private RenderTexture splatMap;
    10.  
    11.     private Material drawMaterial;
    12.     private Material myMaterial;
    13.  
    14.     public GameObject _terrain;
    15.  
    16.     public Transform _SnowBall;
    17.  
    18.     [Range(0, 4)]
    19.     public float _brushSize;
    20.     [Range(0, 1)]
    21.     public float _brushStrength;
    22.  
    23.     RaycastHit groundHit;
    24.  
    25.     int _layerMask;
    26.  
    27.     void Start()
    28.     {
    29.         _layerMask = LayerMask.GetMask("Ground");
    30.         drawMaterial = new Material(drawShader);
    31.         myMaterial = _terrain.GetComponent<MeshRenderer>().material;
    32.         myMaterial.SetTexture("_Splat", splatMap = new RenderTexture(1024, 1024, 0, RenderTextureFormat.ARGBFloat));
    33.     }
    34.  
    35.  
    36.     void Update()
    37.     {
    38.         if (Physics.Raycast(_SnowBall.position, Vector3.down,out groundHit,10f,_layerMask))
    39.         {
    40.             drawMaterial.SetVector("_Coordinate", new Vector4(groundHit.textureCoord.x, groundHit.textureCoord.y, 0, 0));
    41.             drawMaterial.SetFloat("_Strength", _brushStrength);
    42.             drawMaterial.SetFloat("_Size", _brushSize);
    43.             RenderTexture temp = RenderTexture.GetTemporary(splatMap.width, splatMap.height, 0, RenderTextureFormat.ARGBFloat);
    44.             Graphics.Blit(splatMap, temp);
    45.             Graphics.Blit(temp, splatMap, drawMaterial);
    46.             RenderTexture.ReleaseTemporary(temp);
    47.         }
    48.         Debug.DrawRay(_SnowBall.position, Vector3.down, Color.red,10);
    49.  
    50.     }
    51. }
    52.  
     
  2. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    Does your phone support that?

    #pragma target 4.6 (or gl4.1)
    OpenGL 4.1 capabilities (DX11 SM5.0 on D3D platforms, just without compute shaders). This is basically the highest OpenGL level supported by Macs.
    Not supported on DX11 before SM5.0, OpenGL before 4.1, OpenGL ES 2.0/3.0/3.1, Metal.
    Supported on DX11+ SM5.0, OpenGL 4.1+, OpenGL ES 3.1+AEP, Vulkan, Metal (without geometry), PS4/XB1 consoles.
     
  3. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    It does not support it, but my goal is to work on all devices, so I changed pragma to 3.0 and I made some changes. It works now. It does not work on the device for some reason, but works on the emulator, I guess it does not work on some devices. How do I make it work on devices that don't work

    Code (CSharp):
    1. Shader "SnowTracks" {
    2.     Properties{
    3.         _Tess("Tessellation", Range(1,32)) = 4
    4.         _SnowColor("Snow Color", Color) = (1,1,1,1)
    5.         _SnowTex("Snow (RGB)", 2D) = "white" {}
    6.         _GroundColor("Ground Color", Color) = (1,1,1,1)
    7.         _GroundTex("Ground (RGB)", 2D) = "white" {}
    8.         _Splat("Splat Map", 2D) = "black" {}
    9.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    10.         _Displacement("Displacement", Range(0, 1.0)) = 0.3
    11.         _Glossiness("Smoothness", Range(0,1)) = 0.5
    12.         _Metallic("Metallic", Range(0,1)) = 0.0
    13.     }
    14.         SubShader{
    15.             Tags { "RenderType" = "Opaque" }
    16.             LOD 200
    17.  
    18.             CGPROGRAM
    19.             // Physically based Standard lighting model, and enable shadows on all light types
    20.             #pragma surface surf Standard fullforwardshadows
    21.  
    22.             #pragma target 3.0
    23.  
    24.  
    25.                 struct appdata {
    26.                     float4 vertex : POSITION;
    27.                     float4 tangent : TANGENT;
    28.                     float3 normal : NORMAL;
    29.                     float2 texcoord : TEXCOORD0;
    30.                 };
    31.  
    32.                 float _Tess;
    33.  
    34.  
    35.                 sampler2D _Splat;
    36.                 float _Displacement;
    37.  
    38.                 void disp(inout appdata v)
    39.                 {
    40.                     float d = tex2Dlod(_Splat, float4(v.texcoord.xy,0,0)).r * _Displacement;
    41.                     v.vertex.xyz -= v.normal * d;  // invert + to - to make it go down
    42.                     v.vertex.xyz += v.normal * _Displacement; // will push the other vertices upwards so we have a good collider with ground not the snow
    43.                 }
    44.  
    45.  
    46.             sampler2D _GroundTex;
    47.             fixed4 _GroundColor;
    48.             sampler2D _SnowTex;
    49.             fixed4 _SnowColor;
    50.  
    51.             struct Input {
    52.                 float2 uv_GroundTex;
    53.                 float2 uv_SnowTex;
    54.                 float2 uv_Splat;
    55.             };
    56.  
    57.             half _Glossiness;
    58.             half _Metallic;
    59.  
    60.  
    61.             // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    62.             // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    63.             // #pragma instancing_options assumeuniformscaling
    64.             UNITY_INSTANCING_BUFFER_START(Props)
    65.                 // put more per-instance properties here
    66.             UNITY_INSTANCING_BUFFER_END(Props)
    67.  
    68.             void surf(Input IN, inout SurfaceOutputStandard o) {
    69.                 // Albedo comes from a texture tinted by color
    70.                 half amount = tex2Dlod(_Splat, float4(IN.uv_Splat,0,0)).r; // take amount from splat image to lerp between color between snow and grouund
    71.                 fixed4 c = lerp(tex2D(_SnowTex, IN.uv_SnowTex) * _SnowColor, tex2D(_GroundTex, IN.uv_GroundTex) * _GroundColor, amount);
    72.  
    73.                 //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    74.                 o.Albedo = c.rgb;
    75.                 // Metallic and smoothness come from slider variables
    76.                 o.Metallic = _Metallic;
    77.                 o.Smoothness = _Glossiness;
    78.                 o.Alpha = c.a;
    79.             }
    80.             ENDCG
    81.         }
    82.             FallBack "Diffuse"
    83. }