- Home /
Answer by aldonaletto · Jul 07, 2012 at 09:09 PM
As @Eric5h5 said, removing backface culling works, but produces wrong lighting. You can double face the mesh by duplicating the triangles (the new ones must have inverted winding order) and the normals/vertices/uvs (you need reversed normals for the new triangles, what implies in duplicated vertices and uvs). Attach the script below to some object, and its mesh will become double faced:
function Start () { var mesh = GetComponent(MeshFilter).mesh; var vertices = mesh.vertices; var uv = mesh.uv; var normals = mesh.normals; var szV = vertices.length; var newVerts = new Vector3[szV*2]; var newUv = new Vector2[szV*2]; var newNorms = new Vector3[szV*2]; for (var j=0; j< szV; j++){ // duplicate vertices and uvs: newVerts[j] = newVerts[j+szV] = vertices[j]; newUv[j] = newUv[j+szV] = uv[j]; // copy the original normals... newNorms[j] = normals[j]; // and revert the new ones newNorms[j+szV] = -normals[j]; } var triangles = mesh.triangles; var szT = triangles.length; var newTris = new int[szT*2]; // double the triangles for (var i=0; i< szT; i+=3){ // copy the original triangle newTris[i] = triangles[i]; newTris[i+1] = triangles[i+1]; newTris[i+2] = triangles[i+2]; // save the new reversed triangle j = i+szT; newTris[j] = triangles[i]+szV; newTris[j+2] = triangles[i+1]+szV; newTris[j+1] = triangles[i+2]+szV; } mesh.vertices = newVerts; mesh.uv = newUv; mesh.normals = newNorms; mesh.triangles = newTris; // assign triangles last! }
Excellent, though i did not try the above one. U helped me discover something similar to it:
i made my 72 triangles (duplicating the first 36 triangles) and gave the same vertices by inverting the winding order. And voila!
The thing is that when i color my both meshes i see them beco$$anonymous$$g black to many points (like a shadow or something), and that's an issue...
@Eric5h5 , @Aldo Naletto Thank you guys so much!
Try the script above: it duplicates the triangles to create the inner faces, and also assign inverted normals to the new triangles, fixing the lighting problem.
NOTE: Normals aren't actually associated to the triangles - they are vertex attributes. Due to this, assigning the correct normals to the new triangles require also vertex duplication, as well as all other existing vertex attributes - like uv, the most frequent attribute.
Ok, i 'm aware of UV and normals, i 'll fix that. Thanks again
Looks like it doesn't work if the mesh to duplicate comes from a Skinned$$anonymous$$eshRenderer
. Any hint?
I didn't test this, but suppose that the attribute mesh.boneWeights should also be duplicated in the first for, just like vertices and uvs - don't forget to assign the duplicated array of weights to mesh.boneWeights after the second for!
Answer by roberto_sc · Jun 08, 2014 at 05:27 AM
C# version of @aldonaletto's script:
private void Start () {
var mesh = GetComponent<MeshFilter>().mesh;
var vertices = mesh.vertices;
var uv = mesh.uv;
var normals = mesh.normals;
var szV = vertices.Length;
var newVerts = new Vector3[szV*2];
var newUv = new Vector2[szV*2];
var newNorms = new Vector3[szV*2];
for (var j=0; j< szV; j++){
// duplicate vertices and uvs:
newVerts[j] = newVerts[j+szV] = vertices[j];
newUv[j] = newUv[j+szV] = uv[j];
// copy the original normals...
newNorms[j] = normals[j];
// and revert the new ones
newNorms[j+szV] = -normals[j];
}
var triangles = mesh.triangles;
var szT = triangles.Length;
var newTris = new int[szT*2]; // double the triangles
for (var i=0; i< szT; i+=3){
// copy the original triangle
newTris[i] = triangles[i];
newTris[i+1] = triangles[i+1];
newTris[i+2] = triangles[i+2];
// save the new reversed triangle
var j = i+szT;
newTris[j] = triangles[i]+szV;
newTris[j+2] = triangles[i+1]+szV;
newTris[j+1] = triangles[i+2]+szV;
}
mesh.vertices = newVerts;
mesh.uv = newUv;
mesh.normals = newNorms;
mesh.triangles = newTris; // assign triangles last!
}
Hi want to use this code in a fbx model but give and error like $$anonymous$$eshFilter can be find or something like that if someone can help me i will be very happy
Excellent piece of code, but it doesn't work for skinned meshes, where you get the reference through:
``` Skinned$$anonymous$$eshRenderer skinned_mesh_renderer = GetComponent(); $$anonymous$$esh original_mesh = skinned_mesh_renderer.shared$$anonymous$$esh; ```
The resulting mesh looks like the original.
Any hint?
Answer by Eric5h5 · Jul 07, 2012 at 04:10 PM
That's how it works with backface culling. You can either turn off backface culling in the shader (but the lighting on the backface will be wrong), or create the mesh as double-sided.
@Eric5h5 according to what u told me i guess i follow the philosophy of this link: http://docs.unity3d.com/Documentation/Components/SL-CullAndDepth.html
For what i understood, i have to create a separate file for a shader, right? But how can the shader file gain access to my class and verify the new shade options to my tmpobj(which is my mesh)?
u know my mesh has 36 triangles. If i do that i will end up having 72 triangles. And that means i'm gonna have too many code lines ins$$anonymous$$d of using a shader script which i think is a better choice don't u think? I'm just asking i don't know tbh.
72 triangles is nothing. A shader is usually not a better choice for this; it's harder to implement with proper lighting and doesn't save any computing time since you're turning off backface culling...it has to render both sides anyway. It's quite trivial to generate duplicate triangles for the back faces, all you have to do is invert the winding order, so it will be little extra code.
Answer by Jammer3000 · Jul 07, 2012 at 03:46 PM
Hi well the problem with this is that, it depends were you got the mesh and if you got it from the asset store or anywhere else in a unity run program, then click on the mesh in the project view and in the inspector make sure everything you want is selected, but if you got it from a 3d website like TurboSquid or if you imported it from a 3d modeling program, then most likely its the 3d modeling softwares settings, before you export a 3d model from somewhere make sure you check the export settings. And make sure everything you want to export is going to export.
No i did this procedurally by a script. I load the script and i see the mesh. i also used this sort of code line: tmpobj.renderer.material.color = Color.white; //tmpobj is a gameObject in which i attach my $$anonymous$$esh I can see my white colored mesh on the front but when i look at it from the back it becomes invisible!