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
1
Question by Varaquilex · Oct 18, 2015 at 01:09 AM · shaderlightingwebglphong

Custom shader behaves differently on WebGL deployment (correct) and editor view (incorrect)

I started working on shaders and lighting on Unity3D 5.2.1f1. I set up a scene where the sun object rotates the directional light (the only light in the scene) based on its position to simulate a light object orbiting around the model object while rotating the directional light accordingly.

screenshot1

Here, I have implemented Phong reflection model by writing the custom shader below.

 Shader ".Tutorial/Phong"
 {
     Properties
     {
         _Diffuse("Diffuse Color", Color) = (1, 1, 1, 1)
         _Specular("Specular Color", Color) = (1,1,1,1)
         _Ka("Ambient Light", Vector) = (0.3, 0.3, 0.3)
         _Shininess("Shininess", Float) = 1000
     }
 
     SubShader
     {
         Pass
         {
         CGPROGRAM
         #pragma vertex vert    
         #pragma fragment frag
 
         struct appdata
         {
             float4 vertex : POSITION;
             float3 normal : NORMAL;
             float2 texcoord : TEXCOORD0;
         };
 
         struct v2f
         {
             float4 pos : SV_POSITION;
             float3 normal : NORMAL;
             float2 texcoord: TEXCOORD0;
         };
 
         fixed4 _Diffuse;
         fixed4 _Specular;
         float4 _LightColor0;
         float4 _Ka;
         float _Shininess;
 
         v2f vert(appdata IN)
         {
             v2f OUT;
             OUT.pos = mul(UNITY_MATRIX_MVP, IN.vertex);
             OUT.texcoord = IN.texcoord;
             OUT.normal = mul(float4(IN.normal, 0.0), _Object2World).xyz; 
             return OUT;
         }
 
         fixed4 frag(v2f IN) : COLOR
         {
             float3 N = normalize(IN.normal);
             float3 L = normalize(_WorldSpaceLightPos0);
             float3 V = normalize(_WorldSpaceCameraPos - IN.pos.xyz);
             float3 R = 2 * max(0.0, dot(N, L)) * N - L;
 
             float4 Kd = _Diffuse * max(0.0, dot(N, L));
             float4 Ks = _Specular * pow( max(0.0, dot(R, V)) , _Shininess);
 
             float4 Illumination = Kd + Ks + _Ka;
 
             return _LightColor0*Illumination;
         }
 
         ENDCG
         }
     }
 }


Here's the issue: This shader works correct on the WebGL deployment but looks like this in the editor scene and play views.

screenshot2

You can see the specular component is far off in the scene/play view in the Editor, but is right on the WebGL deployment.

If you want to give yourselves a try here are the links to

  • The repository

  • The WebGL deployment


What causes this? How can I begin to debug such an issue? Anyone experienced this before?

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
1

Answer by Bunny83 · Oct 18, 2015 at 03:04 AM

To me this line doesn't make sense:

 float3 L = normalize(_WorldSpaceLightPos0);

"_WorldSpaceLightPos0" is a position relative to the world origin. normalizing it gives you the direction from the origin to the light source.

Further more this line:

 float3 V = normalize(_WorldSpaceCameraPos - IN.pos.xyz);

also seems to be wrong. At least since you use pos here. "pos" is already in viewport coordinates since it got multiplied by the MVP matrix. However you would need the worldspace position of the fragment as well. So you have to pass another "pos" to the fragment shader which should contain the world position of the fragment.

The reason why it seemingly works in the WebGL build "could be" that the shader is somehow not supported and Unity uses a fallback shader which does display correctly. Have you tried putting a hardcoded constant color into the shader and see if you actually see that color in the webGL build?

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

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

Assigning vertex normal in Surface program to o.Normal brakes lighting? 0 Answers

Shader: Changing a Ramp Size 1 Answer

Custom ambient lighting color in a Cg shader 2 Answers

Can this explosion effect be created in Unity 3.5? 1 Answer

2D Sprite Shader Acting like Light 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