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
6
Question by artie · Jul 28, 2012 at 01:16 AM · raycastmeshsubmesh

RaycastHit, triangleIndex, and submeshes - which submesh?

A raycast hit will give you the triangle index hit. What if the mesh has submeshes? There is no submesh index.

Is it ok to assume that the triangle list is sorted such that the submesh is the one modulo the number of vertices in preceding lists?

In other words, lets say submesh 0 has 10 verts, submesh 1 has 15 verts, and submesh 2 has 12 verts. And the raycast hit says 'triangle index 12'

Does that mean submesh 1? IOW submesh 0 is from 0-9, submesh 1 is 10-24, and submesh2 is 25-37?

How do I map a material to the submesh?

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

2 Replies

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

Answer by ScroodgeM · Jul 28, 2012 at 11:23 AM

  • main mesh contains all triangles of this mesh and also all that can be accessed via submeshes.
    • submeshes contains tris that can be accessed also via main mesh

    • submeshes are always exists in any mesh, at least one

    • sum of all submeshes tris is equal to main mesh tris

    • submeshes are used to apply different materials to tris

so finally, when you got from raycast hit to triangle index 50, you should search it in main mesh (it contains all tris including triangle 50), then get vertices for this triangle, and find triangle with those vertices in submeshes

bonus: MeshAnaliser.cs

using UnityEngine;
using System.Collections;
public class MeshAnaliser : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                Mesh m = GetMesh(gameObject);
                if (m)
                {
                    int[] hittedTriangle = new int[] 
                    {
                        m.triangles[hit.triangleIndex * 3], 
                        m.triangles[hit.triangleIndex * 3 + 1], 
                        m.triangles[hit.triangleIndex * 3 + 2] 
                    };
                    for (int i = 0; i < m.subMeshCount; i++)
                    {
                        int[] subMeshTris = m.GetTriangles(i);
                        for (int j = 0; j < subMeshTris.Length; j += 3)
                        {
                            if (subMeshTris[j] == hittedTriangle[0] &&
                                subMeshTris[j + 1] == hittedTriangle[1] &&
                                subMeshTris[j + 2] == hittedTriangle[2])
                            {
                                Debug.Log(string.Format("triangle index:{0} submesh index:{1} submesh triangle index:{2}", hit.triangleIndex, i, j / 3));
                            }
                        }
                    }
                }
            }
        }
    }
    static Mesh GetMesh(GameObject go)
    {
        if (go)
        {
            MeshFilter mf = go.GetComponent<MeshFilter>();
            if (mf)
            {
                Mesh m = mf.sharedMesh;
                if (!m) { m = mf.mesh; }
                if (m)
                {
                    return m;
                }
            }
        }
        return (Mesh)null;
    }
}
Comment
Add comment · Show 8 · 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 artie · Jul 31, 2012 at 11:51 PM 0
Share

Well that helps some. At least I could find which submesh tri list it's in. How does the submesh index correspond to the material? Is there some mapping somewhere? $$anonymous$$y own tests show that the first submesh is NOT necessarily the first material in the list

avatar image DaveA · Aug 01, 2012 at 12:13 AM 0
Share

This line:

$$anonymous$$eshFilter mf = go.GetComponent();

should probably work better like this:

$$anonymous$$eshFilter mf = go.GetComponent<$$anonymous$$eshFilter>();

Damn that Qato!

avatar image ScroodgeM · Aug 01, 2012 at 05:24 AM 0
Share

@DaveA, you are right 8) thanks. Qato thinks that it was a tag 8)

avatar image ScroodgeM · Aug 01, 2012 at 05:28 AM 0
Share

@art , how did you test material-to-submesh mapping? all materials with submeshes are mapped one-to-one. of course, if submeshes quantity and materials quantity are the same. if materials quantity is more then submeshes, all extra materials are drawn with last submesh. if materials quantity less then submeshes, submeshes are not drawn.

avatar image artie · Aug 01, 2012 at 06:58 AM 0
Share

I put a GUIText in and set the text of it to the triangle index (from the raycast hit, which comes from mouse position then screen-to-world) So mouse-over will tell me what triangle index I'm hitting. I also used Debug.DrawLine to hilight the triangle. I can see that if I'm on triangle 0, that it is not the first material in the list. I'll post screen shots tomorrow.

Show more comments
avatar image
1

Answer by bbupton · Feb 10, 2015 at 10:36 PM

I ran some tests using a procedurally-generated mesh. As artie suspected in his original post, the triangle lists for the submeshes seem to be stored in order in the global triangle list. The ordering is determined by the submesh index, not the order of creation of the submeshes.

So if you do something like:

 mesh.SetTriangles (submesh0, 0);
 mesh.SetTriangles (submesh1, 1);
 mesh.SetTriangles (submesh2, 2);
 

The full triangle list will consist of all the vertex indices for submesh0, followed by all the vertex indices for submesh1, followed by all the vertex indices for submesh2.

Comment
Add comment · 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

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

10 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

Related Questions

Getting the triangles (not setting) of statically batched meshes? 0 Answers

How to apply a texture to a submesh 1 Answer

Finding flat, horizontal regions for reliable object placement? 0 Answers

Getting bounds of submeshes 1 Answer

Does each submesh have it's own limits on vertices? 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