Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
4
Question by velv · Jul 07, 2012 at 01:19 PM · meshvisiblefaceback

How make visible the back face of a Mesh

Hi, i want to make the back face of a mesh visible. How can i make this?

Thnx

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

4 Replies

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

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!
}
Comment
Add comment · Show 7 · 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 velv · Jul 08, 2012 at 12:15 PM 0
Share

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!

avatar image aldonaletto · Jul 08, 2012 at 03:58 PM 0
Share

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.

avatar image velv · Jul 08, 2012 at 06:41 PM 0
Share

Ok, i 'm aware of UV and normals, i 'll fix that. Thanks again

avatar image Branxord velv · Oct 04, 2018 at 03:27 PM 0
Share

How...did you even learn all of this? i'm baffled

avatar image psantoki · Jan 05, 2013 at 12:42 AM 0
Share

@Aldo, most clever :-)

avatar image fnunnari · Oct 02, 2018 at 02:28 PM 0
Share

Looks like it doesn't work if the mesh to duplicate comes from a Skinned$$anonymous$$eshRenderer. Any hint?

avatar image aldonaletto fnunnari · Oct 03, 2018 at 03:51 AM 0
Share

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!

avatar image
5

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!
 }
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 viashino999 · Jul 07, 2014 at 04:19 PM 0
Share

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

avatar image fnunnari · Oct 02, 2018 at 02:27 PM 0
Share

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?

avatar image
1

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.

Comment
Add comment · Show 4 · 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 velv · Jul 07, 2012 at 05:08 PM 0
Share

@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)?

avatar image Eric5h5 · Jul 07, 2012 at 05:59 PM 0
Share

You should really just create both sides in the mesh.

avatar image velv · Jul 07, 2012 at 06:19 PM 0
Share

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.

avatar image Eric5h5 · Jul 07, 2012 at 07:18 PM 0
Share

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.

avatar image
0
Wiki

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.

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 velv · Jul 07, 2012 at 03:55 PM 0
Share

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!

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

16 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

Related Questions

[Solved]Simple shader - light only faces. 1 Answer

By Code generated Mesh (problems with visibility) 4 Answers

Get face normal and get its local Euler Angles? 3 Answers

How would I rotate an object without moving one of its faces (locking it) 0 Answers

Why are some of the faces on my mesh not being textured? 2 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