• 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 Alex_May · Jan 10, 2015 at 12:03 PM · proceduralvertex shader

What happens to normals and tangents between Unity and the shader?

I'm packing three floats into one as per http://forum.unity3d.com/threads/can-i-send-data-as-w-via-vertex-data.114111/#post-769621.

If I store the resulting float in normal.x or tangent.x, the data comes out screwy (I think there's some kind of bit shifting issue, since the result definitely contains something relating to my original data, but not sure exactly what is going on). But if I put it in uv.x or tangent.w, for example, it works great.

I already turned off auto-normalisation of normals.

Something must be happening to the values in normal and tangent on their way to the shader that's affecting the maths of unpacking the floats. Anyone know what it might be?

Here's my shader:

Shader "Starboretum/Flora2" {

     Properties {
     }
     SubShader {
         Tags {
             "Queue" = "Geometry"
             "RenderType" = "Opaque"
         }
         
     Cull Off
     CGPROGRAM
 
     #pragma surface surf NoLighting vertex:vert addshadow 
     #pragma glsl_no_auto_normalization
     #include "UnityCG.cginc"
 
     float _IterationProgress;
 
     struct appdata {
         float4 vertex : POSITION;
         float3 normal : NORMAL;
         float4 tangent : TANGENT;
         float4 texcoord : TEXCOORD0;
         float4 texcoord1 : TEXCOORD1;
         fixed4 color : COLOR;
     };
 
 
     struct Input 
     {
         float4 color : COLOR;
     };
     
     
     fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
     {
         fixed4 c;
         c.rgb = s.Albedo; 
         c.a = s.Alpha;
         return c;
     }
 
 
     void surf (Input IN, inout SurfaceOutput o) 
     {
         o.Albedo = (half3)IN.color.rgb;
     }
 
 
     float3 UnPackVector3(float src) 
     {     
         return (frac(float3(1.0f, 256.0f, 65536.0f) * src) * 2) - 1;     
     }
     
     
     void vert (inout appdata_full v)
     {
         // get the vertex position
         float4 position = mul(_Object2World, v.vertex);
         
         // obtain the direction of this segment
         float3 dir = UnPackVector3(v.texcoord1.x);
         
         float3 camDir = _WorldSpaceCameraPos - position.xyz;
 
         // widen the stem from prev width -> width
         float width = lerp(v.texcoord.y, v.texcoord1.y, _IterationProgress);
 
         // get a vector perpendicular to the direction to the camera
         float3 left = normalize(cross(dir, camDir));
         
         left *= v.texcoord.x; // reverse it if necessary
         
         //  expand the line segment by the width
         v.vertex.xyz += left * width;
             
         v.color = float4(lerp(v.color.rgb, v.tangent.xyz, _IterationProgress),1);
     }
     
     ENDCG
         
     } 
     
     FallBack "VertexLit"
 }

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 Alex_May · Jan 10, 2015 at 05:53 PM 0
Share

I tried creating a shader with vert + frag ins$$anonymous$$d of surface + vert, but same problem.

2 Replies

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

Answer by Alex_May · Jan 13, 2015 at 05:42 PM

Apparently this is due to dynamic batching, though the actual reason the data gets mangled remains unclear.

http://forum.unity3d.com/threads/passing-extra-vertex-data-to-a-shader.190448/

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 Alex_May · Jan 15, 2015 at 04:38 PM 0
Share

They're being normalised.

avatar image Alex_May · Jan 25, 2015 at 11:51 AM 0
Share

It seems like they're being normalised because of the Skinned $$anonymous$$esh Renderer

avatar image
0

Answer by Ferb · Oct 30, 2015 at 05:38 PM

I have found before that when things are dynamically batched, they all get multiplied by their individual model matrices before being sent to the shader, so that the shader can treat them all as a single model (with a model matrix just set to the identity matrix). That naturally messes with the position, normal and tangent coordinates, but any texture vertices remain unchanged, so you can either use those to send your data, or do the dynamic batching manually for anything affected by this shader (i.e. write a script to put all the affected meshes into a single mesh yourself).

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.

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

26 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

Related Questions

Drunkard Walk Algorithm C# Question 0 Answers

Substance - Procedural Materials - runtime edit - mobile 1 Answer

Using Unity to trigger MIDI sequences with Wwise 0 Answers

How do i assign perlin noise 2 Answers

Substance - How do you set the random seed of procedural material at runtime 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