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 /
avatar image
0
Question by MrOmen · Jul 01, 2021 at 02:14 AM · cameramesh

Front Culling

"I need a hero to save me now I need a hero (save me now) I need a hero to save my life A hero'll save me (just in time) I need a hero to save my life I need a hero just in time Save me just in time Save me just in time"


So I am trying to mesh a cylinder of linearly varying radius from some finite number all the way down to 0. However, when I view this object from outside the front side doesn't render while showing me only the backside, I guess the terminology for that is front culling. When viewed from inside it seems fine however (see attached photos). I am new to meshes so I have no idea what's going on, I have been dealing with this bug for hours but to no avail...its driving me insane, please save me.

I will also attach my source code below.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 [RequireComponent(typeof(MeshFilter))]
 public class Branch : MonoBehaviour
 {
     public Mesh mesh;
     int[] triangles;
     public float[] weights;
     public float radius = 10;
     public float height = 10;
     public int thetaNo = 9;
     public int zNo = 5;
     public float offset = 0.5f;
 
     
     void Start()
     {
         mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
 
         mesh.Clear();
         triangles = new int[thetaNo * zNo * 6];
 
         weights = new float[zNo * thetaNo];
         
         for (int j = 0; j < zNo - 1; j++)
         {
             for (int i = 0; i < thetaNo; i++)
             {
 
                 if (i == thetaNo - 1)
                 {
                     triangles[j * (thetaNo * 6) + i * 6 + 2] = j * thetaNo + 0 + thetaNo;
                     triangles[j * (thetaNo * 6) + i * 6 + 0] = j * thetaNo + i + 0;
                     triangles[j * (thetaNo * 6) + i * 6 + 1] = j * thetaNo + i + 0 + thetaNo;
                     triangles[j * (thetaNo * 6) + i * 6 + 5] = j * thetaNo + 0;
                     triangles[j * (thetaNo * 6) + i * 6 + 3] = j * thetaNo + i + 0;
                     triangles[j * (thetaNo * 6) + i * 6 + 4] = j * thetaNo + 0 + thetaNo;
                 }
                 else
                 {
                     triangles[j * (thetaNo * 6) + i * 6 + 2] = j * thetaNo + i + 1 + thetaNo;
                     triangles[j * (thetaNo * 6) + i * 6 + 0] = j * thetaNo + i + 0;
                     triangles[j * (thetaNo * 6) + i * 6 + 1] = j * thetaNo + i + 0 + thetaNo;
                     triangles[j * (thetaNo * 6) + i * 6 + 5] = j * thetaNo + i + 1;
                     triangles[j * (thetaNo * 6) + i * 6 + 3] = j * thetaNo + i + 0;
                     triangles[j * (thetaNo * 6) + i * 6 + 4] = j * thetaNo + i + 1 + thetaNo;
                 }
 
             }
         }
 
         mesh.vertices = createShape(radius, height, thetaNo, zNo, offset);
         mesh.triangles = triangles;
         for(int i = 0; i < mesh.normals.Length; i++)
         {
             mesh.normals[i] = -mesh.normals[i];
         }
     }
 
     private void Update()
     {
         mesh.RecalculateNormals();
     }
 
     Vector3[] createShape(float radius, float height, int thetaNo, int zNo, float offset)
     {
         int zStep = (int) (height / zNo);
         int thetaStep = 360 / thetaNo;
 
         Vector3[] array = new Vector3[zNo * thetaNo];
 
         for (int i = 0, z = 0; z < height; z += zStep)
         {
             float newRadius = (-radius / (height - zStep)) * z + radius;
             float xOffset = Random.Range(-newRadius * offset, newRadius * offset);
             float yOffset = Random.Range(-newRadius * offset, newRadius * offset);
             for (int theta = 0; theta < 360f; theta += thetaStep)
             {
                 weights[i] = 1 - newRadius/radius;
                 array[i] = new Vector3(newRadius * Mathf.Cos(Mathf.Deg2Rad * theta) + xOffset, newRadius * Mathf.Sin(Mathf.Deg2Rad * theta) + yOffset, z);
                 i++;
             }
         }
 
         return array;
     }
 
 }

alt text alt text

inner-view.jpg (25.1 kB)
outside-view.jpg (27.9 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

1 Reply

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

Answer by Eno-Khaon · Jul 01, 2021 at 02:57 AM

Well, simply put, it's probably safe to assume the shader is culling back faces. The problem is that you're building the faces inside-out.

Whether you build the faces clockwise or counter-clockwise makes a difference, so you simply need to reverse that. For a quick-fix sort of approach, it should be as simple as:

 // Swap triangle order here           v
 triangles[j * (thetaNo * 6) + i * 6 + 1] = j * thetaNo + 0 + thetaNo;
 triangles[j * (thetaNo * 6) + i * 6 + 0] = j * thetaNo + i + 0;
 triangles[j * (thetaNo * 6) + i * 6 + 2] = j * thetaNo + i + 0 + thetaNo;
 triangles[j * (thetaNo * 6) + i * 6 + 4] = j * thetaNo + 0;
 triangles[j * (thetaNo * 6) + i * 6 + 3] = j * thetaNo + i + 0;
 triangles[j * (thetaNo * 6) + i * 6 + 5] = j * thetaNo + 0 + thetaNo;
 // Swap triangle order here           ^


In this example, I swapped 1<->2 and 4<->5. That reverses the order of vertices in each triangle and, therefore, inverts the faces' fronts and backs.

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 MrOmen · Jul 01, 2021 at 12:02 PM 0
Share

Dude you are awesome! Thank you! <3

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

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

Related Questions

Create a hole in a mesh using a shader 0 Answers

Procedurally generated mesh disappears from certain camera angles 1 Answer

Passing Underneath Section of a Mesh 0 Answers

Camera distance from mesh not center 1 Answer

Mesh Goes Invisible At Particular Angles 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