• 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 /
  • Help Room /
avatar image
0
Question by MrQuetch · Oct 04, 2016 at 05:38 PM · shader programmingshader writing

How Do I Combine These Two Shaders Efficiently? Surface + Vert / Frag

I'm certain that this question has been asked and answered several times before, but I still cannot combine these two shaders properly. One shader contains a color ramp and the other contains a color and texture. I'd like to figure out how to ( as so the title says ) be able to put these two scripts into one. I've tried looking for other answers already, and have not been able to find anyone in the same problem with a surface shader and a vertex / fragment shader. I've seen other threads in which people combined either one or the other into another, but not a surface and vert / frag in one.

I apologize for the inconvenience, but I've been learning and I know more now about shaders than I did before - and it still bothers me that I cannot settle this shader out. So, after lurking around Unity3D.com for awhile, I figured I'd finally ask for help.

Here's the code for the color ramp:

 Shader "16_Bit_Pallete"
 {
     Properties
     {
         _Color("Main Color", Color) = (0.5,0.5,0.5,1)
         _MainTex("Base (RGB)", 2D) = "white" {}
     _Ramp("Toon Ramp (RGB)", 2D) = "gray" {}
     }
         SubShader
     {
         Tags{ "RenderType" = "Opaque" }
         LOD 200
 
         CGPROGRAM
 #pragma surface surf ToonRamp
 
         sampler2D _Ramp;
 
     // custom lighting function that uses a texture ramp based
     // on angle between light direction and normal
 #pragma lighting ToonRamp exclude_path:prepass
     inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
     {
 #ifndef USING_DIRECTIONAL_LIGHT
         lightDir = normalize(lightDir);
 #endif
 
         half d = dot(s.Normal, lightDir)*0.5 + 0.5;
         //half d = dot(s.Normal, lightDir)*0.5 + 1;
         half3 ramp = tex2D(_Ramp, float2(d,d)).rgb;
         //half3 ramp = tex2D(_Ramp, float2(d*2, d*2)).rgb*2; //makes RGB brighter / darker
 
         half4 c;
         //c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
         c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 0.5); //makes lighter brighter / darker
         c.a = 0;
         return c;
     }
 
     sampler2D _MainTex;
 
     struct Input
     {
         float2 uv_MainTex : TEXCOORD0;
     };
 
     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"
 }

And here's the code for the vert / frag shader:

 Shader "psx/unlit_noambient" {
     Properties{
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Base (RGB)", 2D) = "white" {}
         _ColorRamp("Color Ramp", 2D) = "gray" {}
     }
         SubShader{
         Tags{ "RenderType" = "Opaque" }
         LOD 200
 
         Pass{
         Lighting On
         CGPROGRAM
 
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
     struct v2f
     {
         fixed4 pos : SV_POSITION;
         half4 color : COLOR0;
         half4 colorFog : COLOR1;
         float2 uv_MainTex : TEXCOORD0;
         half3 normal : TEXCOORD1;
     }; 
 
     float4 _Color;
     float4 _MainTex_ST;
     sampler2D _ColorRamp;
     uniform half4 unity_FogStart;
     uniform half4 unity_FogEnd;
 
     v2f vert(appdata_full v)
     {
         v2f o;
 
         //Vertex snapping
         float4 snapToPixel = mul(UNITY_MATRIX_MVP,v.vertex);
         float4 vertex = snapToPixel;
         vertex.xyz = snapToPixel.xyz / snapToPixel.w;
         vertex.x = floor(160 * vertex.x) / 160;
         vertex.y = floor(120 * vertex.y) / 120;
         vertex.xyz *= snapToPixel.w;
         o.pos = vertex;
 
         //Vertex lighting 
         o.color = v.color;
 
         float distance = length(mul(UNITY_MATRIX_MV,v.vertex));
 
         //Affine Texture Mapping
         float4 affinePos = vertex;//vertex;                
         o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
         o.uv_MainTex *= distance + (vertex.w*(UNITY_LIGHTMODEL_AMBIENT.a * 8)) / distance / 2;
         o.normal = distance + (vertex.w*(UNITY_LIGHTMODEL_AMBIENT.a * 8)) / distance / 2;
 
         //Fog
         float4 fogColor = unity_FogColor;
 
         float fogDensity = (unity_FogEnd - distance) / (unity_FogEnd - unity_FogStart);
         o.normal.g = fogDensity;
         o.normal.b = 1;
 
         o.colorFog = fogColor;
         o.colorFog.a = clamp(fogDensity,0,1);
 
         //Cut out polygons
         if (distance > unity_FogStart.z + unity_FogColor.a * 255)
         {
             o.pos.w = 0;
         }
 
 
         return o;
     }
 
     sampler2D _MainTex;
 
     float4 frag(v2f IN) : COLOR
     {
 
         half4 c = tex2D(_MainTex, IN.uv_MainTex / IN.normal.r)*IN.color*_Color;
         half4 color = c*(IN.colorFog.a);
         color.rgb += IN.colorFog.rgb*(1 - IN.colorFog.a);
         return color;
     }
         ENDCG
     }
     }
 }

I've already attempted to bring some of the code from the 16_Bit_Pallete shader to the other, but am having trouble. Unity seems to not like it when I add the #pragma surface surf ToonRamp when I take it from that shader and input it into the psx/unlit_nonambient.

Overall, the feel that I am shooting for is a PS1 pixelated cell shading look and feel by combining these two shaders into one. The shaders have already been posted elsewhere on the internet, and I cannot remember where. However, I have files for many other PS1 shaders if anyone is interested. I could link them here if wanted.

I apologize, yet again for the inconveniences - I hope you guys can help. Thank you so much.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

73 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

Related Questions

Mask Shader Problem in Unity 0 Answers

Shader error : unexpected $end, expecting TOK_SETTEXTURE or '}' at line 18 1 Answer

extend Mobile-Bumped and Mobile-BumpedSpec.shader with color 1 Answer

Reversed UV Light with 2D PointLight (shader graph) 0 Answers

Shader object parts discard by Shadow 0 Answers


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