Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 opri · Apr 17, 2014 at 03:16 PM · shadowscustom shader

How to receive shadows on a simple custom shader

I've been trying to add shadows to a very simple vertex color+texture shader but unfortunately to no avail. The aim is to have a shader that renders a texture which it then blends with custom vertex colors (working) and to receive shadows from other objects in the scene. I've tried several approaches but none of them seems to do the trick.

Please find the shader code below (originated from: http://wiki.unity3d.com/index.php/VertexColor):

 Shader " Vertex Colored" {
 Properties {
     _Color ("Main Color", Color) = (1,1,1,1)
     _MainTex ("Base (RGB)", 2D) = "white" {}
 }
 
 SubShader {
     Pass {
         Material {
             Diffuse(1,1,1,1)
             Ambient(1,1,1,1)
         }
         ColorMaterial AmbientAndDiffuse
         Lighting On
         SetTexture [_MainTex] {
             Combine texture * primary, texture * primary
         }
         SetTexture [_MainTex] {
             constantColor [_Color]
             Combine previous * constant DOUBLE, previous * constant
         } 
       }         
 }    
 Fallback "VertexLit"    
 }

 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by opri · Sep 23, 2014 at 07:15 PM

CG to the rescue, hopefully helps someone. A shader that blends vertex colors with texture colors and receives shadows.

 Shader " Vertex Colored" {
  Properties {
     _Color ("Main Color", Color) = (1,1,1,1)
     _MainTex ("Base (RGB)", 2D) = "white" {}
 }
 SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 200
 
 CGPROGRAM
 #pragma surface surf Lambert
 
 sampler2D _MainTex;
 fixed4 _Color;
 
 struct Input {
     float2 uv_MainTex;
     float4 color : COLOR;
 };
 
 void surf (Input IN, inout SurfaceOutput o) {
     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
     o.Albedo = c.rgb;
     o.Albedo *= IN.color.rgb;
     o.Alpha = c.a;
 }
 ENDCG
 }
 Fallback "VertexLit"
 }
 
Comment
Add comment · Show 1 · 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 umirza47 · Mar 02, 2016 at 06:13 AM 2
Share

Can you translate this into vert/frag shader? I have done this but my results are very strange, I am getting some strange lines in shadow areas, I have maximized my settings of shadows from quality settings but still its not working fine.

alt text

Code is below:

 Shader "Umirza47/NonSurf/Lambert"
 {
     Properties
     {
         _Color ("Color", Color) = (1.0,1.0,1.0,1.0)
     }
     
     CGINCLUDE
     #include "UnityCG.cginc"
     #include "AutoLight.cginc"
     #include "Lighting.cginc"
     ENDCG
 
  SubShader
  {
      LOD 200
      Tags { "RenderType"="Opaque" }
      Pass { 
             Lighting On
             Tags {"Light$$anonymous$$ode" = "ForwardBase"}
             CGPROGRA$$anonymous$$
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_fwdbase
 
             uniform half4        _Color;
 
             struct vertexInput
             {
                 half4     vertex        :    POSITION;
                 half3     normal        :    NOR$$anonymous$$AL;
             };
             
             struct vertexOutput
             {
                 half4     pos                :    SV_POSITION;
                 fixed4     lightDirection    :    TEXCOORD1;
                 fixed3     viewDirection    :    TEXCOORD2;
                 fixed3     normalWorld        :    TEXCOORD3;
                 LIGHTING_COORDS(4,6)
             };
 
              vertexOutput vert (vertexInput v)
             {
                 vertexOutput o;
                 
                 half4 posWorld = mul( _Object2World, v.vertex );
                 o.normalWorld = normalize( mul(half4(v.normal, 0.0), _World2Object).xyz );
                 o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
                 o.viewDirection = normalize(_WorldSpaceCameraPos.xyz - posWorld.xyz);
             
                 TRANSFER_VERTEX_TO_FRAG$$anonymous$$ENT(o);
                 
                 return o;
             }
             
             half4 frag (vertexOutput i) : COLOR
             {
                 fixed NdotL = dot(i.normalWorld, i.lightDirection);
                 half atten = LIGHT_ATTENUATION(i);
                 fixed3 diffuseReflection = _LightColor0.rgb * atten * _Color.rgb;
                 fixed3 finalColor = UNITY_LIGHT$$anonymous$$O$$anonymous$$_A$$anonymous$$BIENT.xyz + diffuseReflection;
                 
                 return float4(finalColor, 1.0);
             }
             
              ENDCG
          }
      }
      FallBack "Diffuse"
  }

capture.png (55.9 kB)

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

22 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

Related Questions

Is it possible to create a light only casts shadows? 5 Answers

How to achieve shadows in custom Vertex/Frag shaders? 0 Answers

realtime light breaks baked GI 1 Answer

Shadow Strength = 0 vs Shadow type = No Shadows 2 Answers

影がきれいに落ちない。The shadows do not fall clean. 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