关于“Shader wants tangents, but the mesh doesn't have them”的解决
来源:互联网 发布:曼哈顿音乐学院知乎 编辑:程序博客网 时间:2024/06/05 23:46
在我用UV代码绘制的物体替换shader时,报出“Shader wants tangents, but the mesh doesn't have them”的警告。
顾名思义,材质需要切线,但是网格中并不存在。
在国外论坛找到如下解决的方法:
private static void TangentSolver(Mesh theMesh){int vertexCount = theMesh.vertexCount;Vector3[] vertices = theMesh.vertices;Vector3[] normals = theMesh.normals;Vector2[] texcoords = theMesh.uv;int[] triangles = theMesh.triangles;int triangleCount = triangles.Length / 3;Vector4[] tangents = new Vector4[vertexCount];Vector3[] tan1 = new Vector3[vertexCount];Vector3[] tan2 = new Vector3[vertexCount];int tri = 0;for (int i = 0; i < (triangleCount); i++){int i1 = triangles[tri];int i2 = triangles[tri + 1];int i3 = triangles[tri + 2];Vector3 v1 = vertices[i1];Vector3 v2 = vertices[i2];Vector3 v3 = vertices[i3];Vector2 w1 = texcoords[i1];Vector2 w2 = texcoords[i2];Vector2 w3 = texcoords[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);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;tri += 3;}for (int i = 0; i < (vertexCount); i++){Vector3 n = normals[i];Vector3 t = tan1[i];// Gram-Schmidt orthogonalizeVector3.OrthoNormalize(ref n, ref t);tangents[i].x = t.x;tangents[i].y = t.y;tangents[i].z = t.z;// Calculate handednesstangents[i].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[i]) < 0.0) ? -1.0f : 1.0f;}theMesh.tangents = tangents;}
目前还没有好好分析这段代码,但是效果很好。
0 0
- 关于“Shader wants tangents, but the mesh doesn't have them”的解决
- 关于Shader wants normals, but the mesh doesn't have them的问题
- 关于Shader wants normals, but the mesh doesn't have them的问题
- 关于Shader wants normals, but the mesh New Game Object doesn't have them的问题
- 关于Shader wants normals, but the mesh doesn't have them的问题
- Unity的NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them
- NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them
- Mesh doesn’t have albedo UVs
- unity3d 解决doesn't have a clipped shader version for SoftClip 和 (AlphaClip)
- Unity3d NGUI doesn't have a clipped shader version for SoftClip 和 AlphaClip错误的解决方法
- 解决"Sorry, but you don’t have the administrative privileges needed to do this."的方法 wordpress, pagelines, eco
- 解决Field 'ssl_cipher' doesn't have a default value的问题(mysql创建用户名和密码)
- 解决mysql添加用户Field 'ssl_cipher' doesn't have a default value的错误
- Field 'userID' doesn't have a default value问题的解决
- Ubuntu:Target filesystem doesn't have /sbin/init (Slax 解决)
- Field 'id' doesn't have a default value解决
- django提交表单提示"You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SL
- django提交表单报错"You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SL
- 现在win7的系统有没有好一点介绍下。急需。
- Activity 回调函数
- Linux学习21_每天一个linux命令(30): chown命令
- a+++++b的问题【转载】
- WinDBG加载符号表的一点心得体会
- 关于“Shader wants tangents, but the mesh doesn't have them”的解决
- CSS块级元素和行内元素
- Java开发中的23种设计模式详解(转)
- HttpURLConnection.getHeaderField
- 秒聚 轻轻一秒 聚你相聚
- 1038. Recover the Smallest Number
- web技术概论第四周作业
- mac java8 uninstall
- Virtualbox WDDM之DxgkDdiPresent