- Home /
How can I add a color, smoothness and metallic property to this shader?
I found a shader online that allows horizon curvature (like in many 3D infinite runner games). However, the shader doesn't have a colors property. So when I apply this shader to my game objects, they turn white and there's no way to change it. I can only change the texture, but that's not what I want to do. Here's the shader script:
Shader "Custom/CurvedWorld" {
Properties {
// Diffuse texture
_MainTex ("Base (RGB)", 2D) = "white" {}
// Degree of curvature
_Curvature ("Curvature", Float) = 0.001
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Surface shader function is called surf, and vertex preprocessor function is called vert
// addshadow used to add shadow collector and caster passes following vertex modification
#pragma surface surf Lambert vertex:vert addshadow
// Access the shaderlab properties
uniform sampler2D _MainTex;
uniform float _Curvature;
// Basic input structure to the shader function
// requires only a single set of UV texture mapping coordinates
struct Input {
float2 uv_MainTex;
};
// This is where the curvature is applied
void vert( inout appdata_full v)
{
// Transform the vertex coordinates from model space into world space
float4 vv = mul( _Object2World, v.vertex );
// Now adjust the coordinates to be relative to the camera position
vv.xyz -= _WorldSpaceCameraPos.xyz;
// Reduce the y coordinate (i.e. lower the "height") of each vertex based
// on the square of the distance from the camera in the z axis, multiplied
// by the chosen curvature factor
vv = float4( 0.0f, (vv.z * vv.z) * - _Curvature, 0.0f, 0.0f );
// Now apply the offset back to the vertices in model space
v.vertex += mul(_World2Object, vv);
}
// This is just a default surface shader
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
// FallBack "Diffuse"
}
I just started learning shader programming, so I can't really do everything yet. As of know, I'm just learning by trial-and-error. How can I add the basic Main Color, metallicness and smoothness properties (from the Standard shader) into this shader?
The Standard shader files are available here :) Just copy the relevant code bits and paste them into your own shader. Some adaption work will be neccessary of course.
Answer by TheSaviour · Jun 25, 2017 at 06:02 PM
Ok so after some trial-and-error, I finally figured it out. First I created a "Standard Surface Shader" by doing the following: Right click in Assets folder (or subfolder) > Create > Shader > Standard Surface Shader. The CG code for the Standard Surface Shader pretty much had everything that I needed for the basic texturing, color, smoothness and metallic properties. All I had to do then was to just copy the parts from the "CurvedWorld" shader script that correspond to curvature and paste it into the Standard Surface Shader, while keeping everything else intact. Here's the CG code for the "CurvedWorld" script now:
Shader "Custom/CurvedWorld" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_Curvature("Curvature", Float) = 0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard vertex:vert fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
uniform float _Curvature;
half _Glossiness;
half _Metallic;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
};
// This is where the curvature is applied
void vert( inout appdata_full v)
{
// Transform the vertex coordinates from model space into world space
float4 vv = mul( _Object2World, v.vertex );
// Now adjust the coordinates to be relative to the camera position
vv.xyz -= _WorldSpaceCameraPos.xyz;
// Reduce the y coordinate (i.e. lower the "height") of each vertex based
// on the square of the distance from the camera in the z axis, multiplied
// by the chosen curvature factor
vv = float4( 0.0f, (vv.z * vv.z) * - _Curvature, 0.0f, 0.0f );
// Now apply the offset back to the vertices in model space
v.vertex += mul(_World2Object, vv);
}
// 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_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_CBUFFER_END
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
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"
}
Everything works as expected. As of now, I haven't found any flaws. Now, I just have to figure out how to add emission property to this shader.
From this site (https://unity3d.com/get-unity/download/archive), you can download Unity's Built-In shaders. (I'm not linking to any directly because the site is updated with newer releases of Unity)
Among the files, you can see how a standard shader is constructed, so you can more effectively create your own variation on it.
I already checked the Standard.shader file in the Built-in shaders. I couldn't find any vert, frag or surf structures. All I found was a whole bunch of pragma statements.... Or who knows...maybe I was checking the wrong file. $$anonymous$$aybe there's another Standard.shader file that I'm not aware of.
Your answer
Follow this Question
Related Questions
See backside of a transparent emissive shader? 0 Answers
Material doesn't have a color property '_Color' 4 Answers
How to remove a color from texture using Shader? 0 Answers
How to extend a Material Shader so that it also maps the Normal Maps in WorldSpace? 1 Answer
Object is rendered white(saturated) 1 Answer