Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Polak149 · Nov 12, 2014 at 05:43 PM · lightingnormalsprocedural meshtangent

Procedural mesh generation - problem with normals.

I'm trying to make mesh figure generated procedurally but some triangles clearly have broken normals. What is weird, everything is okay during play in editor: alt text

But after build game and run it from exe file,has become a strange light on some triangles. alt text

On pictures we have exactly same place, but we can see, that some triangles on lava mesh ignore light.

My mesh needs to have normal maps. It of course require tangents so to achieve that, I'm using script avaible at http://www.terathon.com/code/tangent.html and converted to javascript by nootz (http://forum.unity3d.com/threads/how-to-calculate-mesh-tangents.38984/):

 /*
 Derived from
 Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. Terathon Software 3D Graphics Library, 2001.
 http://www.terathon.com/code/tangent.html
 */
 
 
 static function TangentSolver(theMesh : Mesh)
 {
     vertexCount = theMesh.vertexCount;
     vertices = theMesh.vertices;
     normals = theMesh.normals;
     texcoords = theMesh.uv;
     triangles = theMesh.triangles;
     triangleCount = triangles.length/3;
     tangents = new Vector4[vertexCount];
     tan1 = new Vector3[vertexCount];
     tan2 = new Vector3[vertexCount];
     tri = 0;
     for ( i = 0; i < (triangleCount); i++)
     {
         i1 = triangles[tri];
         i2 = triangles[tri+1];
         i3 = triangles[tri+2];
        
         v1 = vertices[i1];
         v2 = vertices[i2];
         v3 = vertices[i3];
        
         w1 = texcoords[i1];
         w2 = texcoords[i2];
         w3 = texcoords[i3];
        
         x1 = v2.x - v1.x;
         x2 = v3.x - v1.x;
         y1 = v2.y - v1.y;
         y2 = v3.y - v1.y;
         z1 = v2.z - v1.z;
         z2 = v3.z - v1.z;
        
         s1 = w2.x - w1.x;
         s2 = w3.x - w1.x;
         t1 = w2.y - w1.y;
         t2 = w3.y - w1.y;
        
         r = 1.0 / (s1 * t2 - s2 * t1);
         sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
         tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
        
         tan1[i1] += sdir;
         tan1[i2] += sdir;
         tan1[i3] += sdir;
        
         tan2[i1] += tdir;
         tan2[i2] += tdir;
         tan2[i3] += tdir;
        
         tri += 3;
     }
    
     for (i = 0; i < (vertexCount); i++)
     {
         n = normals[i];
         t = tan1[i];
        
         // Gram-Schmidt orthogonalize
         Vector3.OrthoNormalize( n, t );
        
         tangents[i].x  = t.x;
         tangents[i].y  = t.y;
         tangents[i].z  = t.z;
    
         // Calculate handedness
         tangents[i].w = ( Vector3.Dot(Vector3.Cross(n, t), tan2[i]) < 0.0 ) ? -1.0 : 1.0;
     }      
     theMesh.tangents = tangents;
     return theMesh;
 }

Turning off that script fix the problem with broken normals. So there is my question: where lies the problem in attached script and is there other possibility to calculate mesh tangents?

normal normals.jpg (369.5 kB)
broken normals.jpg (410.4 kB)
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

2 Replies

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

Answer by Polak149 · Nov 13, 2014 at 02:40 PM

I found the answer. I'am not sure if it is correct, but I assignet to every mesh tangents Vector4(0, 1, 0, 1).

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
avatar image
0

Answer by Cherno · Nov 13, 2014 at 02:52 PM

Not sure if it helps, but here is a script that calculates tangents:

 using UnityEngine;
 using System.Collections;
 
 public class CalculateTangents : MonoBehaviour
 {
     
     public static void CalculateMeshTangents(Mesh mesh)
     {
         //speed up math by copying the mesh arrays
         int[] triangles = mesh.triangles;
         Vector3[] vertices = mesh.vertices;
         Vector2[] uv = mesh.uv;
         Vector3[] normals = mesh.normals;
         
         //variable definitions
         int triangleCount = triangles.Length;
         int vertexCount = vertices.Length;
         
         Vector3[] tan1 = new Vector3[vertexCount];
         Vector3[] tan2 = new Vector3[vertexCount];
         
         Vector4[] tangents = new Vector4[vertexCount];
         
         for (long a = 0; a < triangleCount; a += 3)
         {
             long i1 = triangles[a + 0];
             long i2 = triangles[a + 1];
             long i3 = triangles[a + 2];
             
             Vector3 v1 = vertices[i1];
             Vector3 v2 = vertices[i2];
             Vector3 v3 = vertices[i3];
             
             Vector2 w1 = uv[i1];
             Vector2 w2 = uv[i2];
             Vector2 w3 = uv[i3];
             
             float x1 = v2.x - v1.x;
             float x2 = v3.x - v1.x;
             float y1 = v2.y - v1.y;
             float y2 = v3.y - v1.y;
             float z1 = v2.z - v1.z;
             float z2 = v3.z - v1.z;
             
             float s1 = w2.x - w1.x;
             float s2 = w3.x - w1.x;
             float t1 = w2.y - w1.y;
             float t2 = w3.y - w1.y;
             
             //float r = 1.0f / (s1 * t2 - s2 * t1);
             float div = s1 * t2 - s2 * t1;
             float r = div == 0.0f ? 0.0f : 1.0f / div;
             
             Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
             Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
             
             tan1[i1] += sdir;
             tan1[i2] += sdir;
             tan1[i3] += sdir;
             
             tan2[i1] += tdir;
             tan2[i2] += tdir;
             tan2[i3] += tdir;
         }
         
         
         for (long a = 0; a < vertexCount; ++a)
         {
             Vector3 n = normals[a];
             Vector3 t = tan1[a];
             
             //Vector3 tmp = (t - n * Vector3.Dot(n, t)).normalized;
             //tangents[a] = new Vector4(tmp.x, tmp.y, tmp.z);
             Vector3.OrthoNormalize(ref n, ref t);
             tangents[a].x = t.x;
             tangents[a].y = t.y;
             tangents[a].z = t.z;
             
             tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
         }
         
         mesh.tangents = tangents;
     }
 }
 
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 Bunny83 · Nov 13, 2014 at 03:01 PM 0
Share

Uhm, he's already using that tangent solver. He has it in the question (the JS version). The problem most likely comes from that script as in some cases it can't calculate the tangents. For a flat mesh it's the easiest and most reliable solution to assign fix tangents as they would be the same anyways for each vertex.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Procedural-mesh normals reversed, but only when lit from the left 1 Answer

normal maps aren't showing up in the webplayer 1 Answer

Correct normals on model- odd light map. Help! 1 Answer

Lighting induced seams 0 Answers

Finding distance of player to a path in 3D 1 Answer


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