- Home /
Problem creating a circle mesh
Hello,
I'm trying to create a circle mesh, however I get a warning error : Shader wants tangents, but the mesh doesn't have them.
What are theses tangents ? How can I fix this ?
My circle in editor should look like this :
And edges should be at each degrees (I simplified the image), here is my actual code :
var l : int = 50;
var x : float;
var y : float;
function Start() {
var verts: Vector3[] = new Vector3[361];
var normals: Vector3[] = new Vector3[361];
var uv: Vector2[] = new Vector2[361];
var tri: int[] = new int[1080];
var mf: MeshFilter = GetComponent(MeshFilter);
verts[0] = new Vector3(0, 0, 0);
uv[0] = new Vector3(0, 0);
for (var i : int = 1;i<361;i++) { // For each degrees
x = Mathf.Cos(i) * l; // I calc the position
y = Mathf.Sin(i) * l;
verts[i] = new Vector3(x, 0, y); // and assign vertices, uv, normals, etc..
uv[i] = new Vector3(x, y);
tri[(i-1)*3] = 0; // I don't know if triangles should work like this,
tri[(i-1)*3+1] = i; // here I think that triangle is the index of a vertice
if ((i+1 > 360)) {
tri[(i-1)*3 + 2] = 1;
} else {
tri[(i-1)*3 + 2] = i+1;
}
}
for (i = 0; i < normals.Length; i++) {
normals[i] = Vector3.up;
}
var mesh: Mesh = new Mesh();
mesh.vertices = verts;
mesh.triangles = tri;
mesh.uv = uv;
mesh.normals = normals;
mf.mesh = mesh;
}
Thanks you for your help and sorry for the lack of informations.
edit : I'm also sorry for the big image :/
@alucardj : I have already seen this link, but it didn't worked :(
Answer by AlucardJay · Mar 01, 2014 at 04:21 PM
from the comments : http://answers.unity3d.com/questions/597849/shader-wants-tangents-but-the-mesh-doesnt-have-the-1.html#answer-597899
have already seen this link, but it didn't worked :(*
Possibly because you are coding in uJS and the script is in C#. If you havn't put the TangentSolver in a special folder as per compilation order to be recognized.
Here is your script with a line added to use the class (tested and working) :
#pragma strict
var l : int = 50;
var x : float;
var y : float;
function Start()
{
var verts: Vector3[] = new Vector3[361];
var normals: Vector3[] = new Vector3[361];
var uv: Vector2[] = new Vector2[361];
var tri: int[] = new int[1080];
var mf: MeshFilter = GetComponent(MeshFilter);
verts[0] = new Vector3(0, 0, 0);
uv[0] = new Vector3(0, 0);
for (var i : int = 1;i<361;i++) { // For each degrees
x = Mathf.Cos(i) * l; // I calc the position
y = Mathf.Sin(i) * l;
verts[i] = new Vector3(x, 0, y); // and assign vertices, uv, normals, etc..
uv[i] = new Vector3(x, y);
tri[(i-1)*3] = 0; // I don't know if triangles should work like this,
tri[(i-1)*3+1] = i; // here I think that triangle is the index of a vertice
if ((i+1 > 360)) {
tri[(i-1)*3 + 2] = 1;
} else {
tri[(i-1)*3 + 2] = i+1;
}
}
for (i = 0; i < normals.Length; i++) {
normals[i] = Vector3.up;
}
var mesh: Mesh = new Mesh();
mesh.vertices = verts;
mesh.triangles = tri;
mesh.uv = uv;
mesh.normals = normals;
// Calculate Tangents
TangentSolver(mesh);
mf.mesh = mesh;
}
There is something not right about the way you are calculating the triangles. You can have a look at my answer here for another way to create a circle/ring mesh : http://answers.unity3d.com/questions/375604/expanding-ring-velocity-ellipsoid-particle-emitter.html
As you are working in uJS, to avoid compilation problems, here is my uJS version of the TangentSolver :
/*
Derived from
Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. Terathon Software 3D Graphics Library, 2001.
[url]http://www.terathon.com/code/tangent.html[/url]
*/
class TangentSolver
{
function TangentSolver(theMesh : Mesh)
{
var vertexCount = theMesh.vertexCount;
var vertices = theMesh.vertices;
var normals = theMesh.normals;
var texcoords = theMesh.uv;
var triangles = theMesh.triangles;
var triangleCount = triangles.length/3;
var tangents = new Vector4[vertexCount];
var tan1 = new Vector3[vertexCount];
var tan2 = new Vector3[vertexCount];
var tri = 0;
for (var i:int = 0; i < (triangleCount); i++)
{
var i1 = triangles[tri];
var i2 = triangles[tri+1];
var i3 = triangles[tri+2];
var v1 = vertices[i1];
var v2 = vertices[i2];
var v3 = vertices[i3];
var w1 = texcoords[i1];
var w2 = texcoords[i2];
var w3 = texcoords[i3];
var x1 = v2.x - v1.x;
var x2 = v3.x - v1.x;
var y1 = v2.y - v1.y;
var y2 = v3.y - v1.y;
var z1 = v2.z - v1.z;
var z2 = v3.z - v1.z;
var s1 = w2.x - w1.x;
var s2 = w3.x - w1.x;
var t1 = w2.y - w1.y;
var t2 = w3.y - w1.y;
var r = 1.0 / (s1 * t2 - s2 * t1);
var sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
var 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++)
{
var n = normals[i];
var 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;
}
}
Your answer
Follow this Question
Related Questions
How do I create a specific object in Unity? I need a planet with huge mountains and oceans. 1 Answer
Creating a smooth flat circle? 3 Answers
Any tips for creating grass on mesh(creating gardens) ? 0 Answers
Generating a cube mesh with two given points 1 Answer
How to connect walls corners 3 Answers