Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheSaviour · Jun 24, 2017 at 04:04 PM · shadercolormaterialsshader programming

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jun 25, 2017 at 01:33 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eno-Khaon · Jun 25, 2017 at 06:53 PM 0
Share

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.

avatar image TheSaviour Eno-Khaon · Jun 25, 2017 at 08:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges