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 loihb · May 28, 2012 at 07:08 PM · javascriptraycastmaterialhitname

Optimize script or Other way get material name (js)

Hi all.

I wrote this script, so this script can get material name hiting by ray but need something easiest or optimize this.

What can optimize here?

Here is script :

 //Thanks to Bunny83 for help
 
 var transf : Transform;
 
 class hitmat1 {
     var enabled : boolean = true;
     var texturehit : String;
     @HideInInspector
     var tranglind : int;
     @HideInInspector
     var Layer : LayerMask = 1;
 }
 
 var hitmat1 : hitmat1 = hitmat1();
 
 function Update() {
     var hit : RaycastHit;
     if (Physics.Raycast(transf.position, transf.TransformDirection(Vector3.forward), hit, 2, hitmat1.Layer)) {
         ind = hit.triangleIndex;
         if (hitmat1.tranglind != ind && hitmat1.enabled) {
             hitmat1.tranglind = ind;
             var go1 = hit.collider.gameObject;
             var mee = go1.GetComponent(MeshFilter).mesh;
             var mre = go1.GetComponent(MeshRenderer);
             if (mee && mre) {
                 if (go1.renderer.materials.Length > 1) {
                     for (var submesh = 0; submesh < mee.subMeshCount; submesh++) {
                         var indices = mee.GetTriangles(submesh);
                         for (var i = 0; i < indices.Length; i += 3) {
                             if (mee.vertices[indices[i]] == mee.vertices[mee.triangles[ind * 3]] && mee.vertices[indices[i + 1]] == mee.vertices[mee.triangles[ind * 3 + 1]] && mee.vertices[indices[i + 2]] == mee.vertices[mee.triangles[ind * 3 + 2]]) {
                                 hitmat1.texturehit = mre.materials[submesh].name.ToLower();
                                 i = indices.Length;
                                 submesh = mee.subMeshCount;
                                 }else{
                                     hitmat1.texturehit = "Null";
                             }
                         }
                     }
                     }else{
                         mtexx = go1.renderer.material.mainTexture;
                         if (mtexx)
                             hitmat1.texturehit = mtexx.name.ToLower();
                             else
                             hitmat1.texturehit = "Null";
                 }
             }
         }
     }
 }
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
1

Answer by Paulius-Liekis · May 29, 2012 at 08:34 AM

Generic advice

Things like mee.vertices and mee.triangles will perform a copy of vertices and triangles every time you call them, so move them out of the loop:

 // before the loop
 var triangles = mee.triangles;

and then use triangles instead of mee.triangles.

Special tricks

Unity internally doesn't keep a list triangles for each submesh, it only keeps a startIndex+endIndex which are indices into Mesh.triangles array. Unfortunately Unity doesn't give you startIndex+endIndex, but in theory you could precalculate them yourself. Then once you have finding the material would be piece of cake, something like this:

 if (hit.triangleIndex >= startIndex && hit.triangleIndex <= endIndex)
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 Paulius-Liekis · May 29, 2012 at 01:14 PM 0
Share

I'm not sure what you're saying. $$anonymous$$aybe you're mixing up indices-of-vertices with index-of-triangle?

avatar image
0

Answer by loihb · May 30, 2012 at 12:53 PM

Now working faster :

Here is script :

 var transf : Transform;
 
 class hitmat1 {
  var enabled : boolean = true;
  var texturehit : String;
  @HideInInspector
  var tranglind : int;
  @HideInInspector
  var Layer : LayerMask = 1;
 }
 
 var hitmat1 : hitmat1 = hitmat1();
 
 function Update() {
  var hit : RaycastHit;
  if (Physics.Raycast(transf.position, transf.TransformDirection(Vector3.forward), hit, 2, hitmat1.Layer)) {
  ind = hit.triangleIndex;
  if (hitmat1.tranglind != ind && hitmat1.enabled) {
  hitmat1.tranglind = ind;
  var go1 = hit.collider.gameObject;
  var mee = go1.GetComponent(MeshFilter).mesh;
  var mre = go1.GetComponent(MeshRenderer);
  if (mee && mre) {
  if (go1.renderer.materials.Length > 1) {
  var triangles = mee.triangles;
  for (var submesh = 0; submesh < mee.subMeshCount; submesh++) {
  var indices = mee.GetTriangles(submesh);
  for (var i = 0; i < indices.Length; i += 3) {
  if (indices[i] == triangles[ind * 3] && indices[i + 1] == triangles[ind * 3 + 1] && indices[i + 2] == triangles[ind * 3 + 2]) {
  hitmat1.texturehit = mre.materials[submesh].mainTexture.name.ToLower();
  i = indices.Length;
  submesh = mee.subMeshCount;
  }else{
  hitmat1.texturehit = "Null";
  }
  }
  }
  }else{
  mtexx = go1.renderer.material.mainTexture;
  if (mtexx)
  hitmat1.texturehit = mtexx.name.ToLower();
  else
  hitmat1.texturehit = "Null";
  }
  }
  }
  }
 }
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 Paulius-Liekis · May 30, 2012 at 09:21 PM 0
Share

You can also move "hitmat1.texturehit = "Null";" of the loop and add "break" to your loop.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

RayCast to send message to the object it hits? 1 Answer

Detect Object that are hit by a ray 3 Answers

Hit distance Change light range 1 Answer

Raycasting in script suddenly stopped working 1 Answer

How to hit two objects with only one shot (raycast) 0 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