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 awplays49 · Dec 13, 2015 at 05:37 AM · shadershaderscg

Broken specular shader code (CG)

Recently I've been making shaders, and I put this on a capsule

 Shader "Example/Specular" {
 
     Properties
     {    
         _Color ("Color", Color) = (1, 1, 1, 1)
         _Shininess ("Shininess", Range (0, 1)) = 0.5
     }
 
     SubShader
     {
         Pass
         {
             Tags
             {
                 "LightMode" = "ForwardBase"
             }
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             float4 _Color;
             float4 _LightColor;
             float4 _Shininess;
 
             struct vertexInput {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
             };
 
             struct vertexOutput {
                 float4 pos : POSITION;
                 float4 col : COLOR;
             };
             
             vertexOutput vert (vertexInput input) {
                 vertexOutput output;
                 float3 normalDirection = normalize (mul (float4 (input.normal, 0), _World2Object).xyz);
                 float3 lightDirection = normalize (_WorldSpaceLightPos0.xyz);
                 float3 viewDirection = float4 (_WorldSpaceCameraPos.xyz, 1) - mul (UNITY_MATRIX_MVP, input.vertex);
                 float atten = 1;
                  float3 diffuseReflection = atten * _LightColor.xyz * _Color.rgb * max (0, dot (normalDirection, lightDirection));
                  float3 specularReflection = max (0, dot (normalDirection, lightDirection)) * pow (max (0, dot (reflect (-lightDirection, normalDirection), viewDirection)), _Shininess);
                  float3 lightFinal = diffuseReflection + specularReflection + UNITY_LIGHTMODEL_AMBIENT;
                 output.col = float4 (lightFinal, 1);
                 output.pos = mul (UNITY_MATRIX_MVP, input.vertex);
                 return output;
             }
 
             float4 frag (vertexOutput output) : COLOR {
                 return output.col;
             }
 
             ENDCG
         }
     }
     Fallback "Diffuse"
 }

and it made a bunch of vertices facing the light white, and the rest midnight black. Im so overwhelmed with learning this new language, its like when i was trying to learn C#, it was like no one could help me. Im afraid there really is no one to teach me this language though.

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 awplays49 · Dec 13, 2015 at 05:38 AM -1
Share

I dont understand any of the code im using either, and im trying to avoid scary code. I know from my past C# experience that avoiding code is not the answer if i want to learn, so can someone please help me? ill look back in the morning, but its in the A$$anonymous$$ hours now. goodnight.

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.

Follow this Question

Answers Answers and Comments

35 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

Related Questions

Using custom keywords in cg shaders 1 Answer

Shader works differently on Android (OpenGL) and Editor (DX11) 0 Answers

_LightMatrix0 and Directional Light 1 Answer

Shader time related questions 1 Answer

Shader - UNITY_SAMPLE_DEPTH function 2 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