Combine smoothness and metallic with frag Shader
Hi,
I'm trying to add some Smoothness and Metallic to my custom shader in order to have dark texture for day/night cycle.
For the moment, i have a procedurale planet and my shader draw different textures based on height (distance between center of planet and vertex at surface).
Until there, it works !
Now I really need to add Smoothness and Metallic to material but with my shader the textures of my procedurale planet is black and dont know why ? I have a pass for frag and then a surf function for smoothness and metallic. Why this doesn't work please ?
However when I look at the material icon and I change value it looks nice but procedurale planet is always black no matter of value i change and the color i set.
Can't solve this problem since too long time. Please need some help.
Thanks in advance.
Here is my shader :
Shader "Custom/Planet" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Tex0 ("Tex 0", 2D) = "white" {}
_Tex1 ("Tex 1", 2D) = "white" {}
_Tex2 ("Tex 2", 2D) = "white" {}
_Tex3 ("Tex 3", 2D) = "white" {}
_Tex4 ("Tex 4", 2D) = "white" {}
_Blend0to1and1to2 ("Blend between 0 and 1, 1 and 2", Vector) = (0,1,2,3)
_Blend2to3and3to4 ("Blend between 2 and 3, 3 and 4", Vector) = (0,1,2,3)
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
sampler2D _Tex0;
sampler2D _Tex1;
sampler2D _Tex2;
sampler2D _Tex3;
sampler2D _Tex4;
float4 _Blend0to1and1to2;
float4 _Blend2to3and3to4;
uniform float4 _Tex0_ST;
uniform float4 _Tex1_ST;
uniform float4 _Tex2_ST;
uniform float4 _Tex3_ST;
struct appData {
float4 vertex : POSITION;
float2 uv1 : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float2 uv3 : TEXCOORD2;
float2 uv4 : TEXCOORD3;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv1 : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float2 uv3 : TEXCOORD2;
float2 uv4 : TEXCOORD3;
float4 col : COLOR;
};
#define PI 3.141592653589793
inline float2 RadialCoords(float3 a_coords)
{
float3 a_coords_n = normalize(a_coords);
float lon = atan2(a_coords_n.z, a_coords_n.x);
float lat = acos(a_coords_n.y);
float2 sphereCoords = float2(lon, lat) * (1.0 / PI);
return float2(sphereCoords.x * 0.5 + 0.5, 1 - sphereCoords.y);
}
v2f vert (appData vInput) {
v2f output;
output.pos = UnityObjectToClipPos (vInput.vertex);
output.uv1 = TRANSFORM_TEX(vInput.uv1, _Tex0);
output.uv2 = TRANSFORM_TEX(vInput.uv2, _Tex1);
output.uv3 = TRANSFORM_TEX(vInput.uv3, _Tex2);
output.uv4 = TRANSFORM_TEX(vInput.uv4, _Tex3);
output.col = length(vInput.vertex);
return output;
}
half4 frag (v2f fInput) : COLOR {
half4 c0 = tex2D (_Tex0, fInput.uv1);
half4 c1 = tex2D (_Tex1, fInput.uv2);
half4 c2 = tex2D (_Tex2, fInput.uv3);
half4 c3 = tex2D (_Tex3, fInput.uv4);
half4 c4 = tex2D (_Tex4, fInput.uv1);
if (fInput.col.x < _Blend0to1and1to2.x) {
return c0;
}
if (fInput.col.x > _Blend0to1and1to2.x && fInput.col.x < _Blend0to1and1to2.y) {
return lerp(c0,c1,((fInput.col.x - _Blend0to1and1to2.x)/(_Blend0to1and1to2.y-_Blend0to1and1to2.x)));
}
if (fInput.col.x > _Blend0to1and1to2.y && fInput.col.x < _Blend0to1and1to2.z) {
return c1;
}
if (fInput.col.x > _Blend0to1and1to2.z && fInput.col.x < _Blend0to1and1to2.w) {
return lerp(c1,c2,((fInput.col.x - _Blend0to1and1to2.z)/(_Blend0to1and1to2.w-_Blend0to1and1to2.z)));
}
if (fInput.col.x > _Blend0to1and1to2.w && fInput.col.x < _Blend2to3and3to4.x) {
return c2;
}
if (fInput.col.x > _Blend2to3and3to4.x && fInput.col.x < _Blend2to3and3to4.y) {
return lerp(c2,c3,((fInput.col.x - _Blend2to3and3to4.x)/(_Blend2to3and3to4.y-_Blend2to3and3to4.x)));
}
if (fInput.col.x > _Blend2to3and3to4.y && fInput.col.x < _Blend2to3and3to4.z) {
return c3;
}
if (fInput.col.x > _Blend2to3and3to4.z && fInput.col.x < _Blend2to3and3to4.w) {
return lerp(c3,c4,((fInput.col.x - _Blend2to3and3to4.z)/(_Blend2to3and3to4.w-_Blend2to3and3to4.z)));
}
return c4;
}
ENDCG
}
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _Tex1;
struct Input {
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_Tex1, 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"
}
Debugging shaders is real hard work. Start by trying to get anything other than black ofc. Sometimes the problem is further back in the code than you think and sometimes its due to changes, which are listed in the Unity $$anonymous$$anual and Scripting API. I can't pretend I can do this for you without it taking me all day but I can point you in the direction of the CG in Unity book I read that showed me how to debug shaders. Its free and online. I dont know your level of shader writing ability, if you are a beginner then its a fantastic read, if you are a veteran then its always good to get back to basics.
Thanks for your comment and your link, first i'm beginner for ability to write the shaders. It's why I need some help. Then, do you see a potential error on this shader that can cause the black color of the textures ? If not, I will try to debug this shader but as you say i'ts a hard work.
Answer by Namnam · Feb 23, 2018 at 09:35 AM
Does anyone else can tell me what is wrong in this shader and why it result black color to all textures please ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612153213im_/https://answers.unity.com/themes/thub/images/avi.jpg)