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?
Shader "SnowTracks"
{
Properties
{
_Tess("Tessellation", Range(1,32)) = 4
_SnowColor ("Snow Color", Color) = (1,1,1,1)
_SnowTex ("Snow (RGB)", 2D) = "white" {}
_GroundColor("Ground Color", Color) = (1,1,1,1)
_GroundTex("Ground (RGB)", 2D) = "white" {}
_Splat("SplatMap", 2D) = "black" {}
_Displacement("Displacement", Range(0, 1.0)) = 0.3
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:disp tessellate:tessDistance
#pragma target 4.6
#include "Tessellation.cginc"
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
float _Tess;
float4 tessDistance(appdata v0, appdata v1, appdata v2) {
float minDist = 10.0;
float maxDist = 25.0;
return UnityDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, minDist, maxDist, _Tess);
}
sampler2D _Splat;
float _Displacement;
void disp(inout appdata v)
{
float d = tex2Dlod(_Splat, float4(v.texcoord.xy,0,0)).r * _Displacement;
v.vertex.xyz -= v.normal * d;
v.vertex.xyz += v.normal * _Displacement;
}
sampler2D _GroundTex;
fixed4 _GroundColor;
sampler2D _SnowTex;
fixed4 _SnowColor;
struct Input
{
float2 uv_GroundTex;
float2 uv_SnowTex;
float2 uv_Splat;
};
half _Glossiness;
half _Metallic;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
half amount = tex2Dlod(_Splat, float4(IN.uv_Splat, 0, 0)).r;
fixed4 c = lerp(tex2D(_SnowTex, IN.uv_SnowTex) * _SnowColor, tex2D(_GroundTex, IN.uv_GroundTex) * _GroundColor, amount);
//fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Shader "Unlit/DrawTracks"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Coordinate("Coordinate", Vector) = (0,0,0,-1)
_Color ("Draw Color", Color) = (1,0,0,0)
_Size("Size",Range(1,500)) = 1
_Strength("Strength",Range(0,1)) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Coordinate, _Color;
half _Size, _Strength;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
float draw = pow(saturate(1 - distance(i.uv, _Coordinate.xy)), 500 / _Size);
fixed4 drawcol = _Color * (draw * _Strength);
return saturate(col + drawcol);
}
ENDCG
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnowBallTrack : MonoBehaviour
{
public Shader drawShader;
private RenderTexture splatMap;
private Material drawMaterial;
private Material myMaterial;
public GameObject _terrain;
public Transform _SnowBall;
[Range(0, 4)]
public float _brushSize;
[Range(0, 1)]
public float _brushStrength;
RaycastHit groundHit;
int _layerMask;
void Start()
{
_layerMask = LayerMask.GetMask("Ground");
drawMaterial = new Material(drawShader);
myMaterial = _terrain.GetComponent<MeshRenderer>().material;
myMaterial.SetTexture("_Splat", splatMap = new RenderTexture(1024, 1024, 0, RenderTextureFormat.ARGBFloat));
}
void Update()
{
if (Physics.Raycast(_SnowBall.position, Vector3.down,out groundHit,10f,_layerMask))
{
drawMaterial.SetVector("_Coordinate", new Vector4(groundHit.textureCoord.x, groundHit.textureCoord.y, 0, 0));
drawMaterial.SetFloat("_Strength", _brushStrength);
drawMaterial.SetFloat("_Size", _brushSize);
RenderTexture temp = RenderTexture.GetTemporary(splatMap.width, splatMap.height, 0, RenderTextureFormat.ARGBFloat);
Graphics.Blit(splatMap, temp);
Graphics.Blit(temp, splatMap, drawMaterial);
RenderTexture.ReleaseTemporary(temp);
}
Debug.DrawRay(_SnowBall.position, Vector3.down, Color.red,10);
}
}