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
1
Question by ZDS Alpha · Dec 15, 2013 at 04:13 PM · collidermeshsavedetectskinned mesh

Detecting Mesh and Saving it

I am using skinned mesh pipes to create a pipe game.

I have tried there scripts to add mesh to mesh collider

 using UnityEngine;
 
 using System.Collections;
 
 
 
 public class SkinnedCollisionHelper : MonoBehaviour
     
 {
     
     // Public variables
     
     public bool forceUpdate;
     
     
     
     // Instance variables
     
     private CWeightList[]   nodeWeights;    // array of node weights (one per node)
     
     private Vector3[]       newVert;        // array for the regular update of the collision mesh
     
     
     
     private Mesh            mesh;       // the dynamically-updated collision mesh
     
     private MeshCollider    collide;    // quick pointer to the mesh collider that we're updating
     
     
     
     
     
     // Function:    Start
     
     //      This basically translates the information about the skinned mesh into
     
     // data that we can internally use to quickly update the collision mesh.
     
     void Start()
         
     {
         
         SkinnedMeshRenderer rend = GetComponent(typeof(SkinnedMeshRenderer)) as SkinnedMeshRenderer;
         
         collide = GetComponent(typeof(MeshCollider)) as MeshCollider;
         
         
         
         if (collide!=null && rend!=null)
             
         {
             
             Mesh baseMesh = rend.sharedMesh;
             
             mesh = new Mesh();
             
             mesh.vertices = baseMesh.vertices;
             
             mesh.uv = baseMesh.uv;
             
             mesh.triangles = baseMesh.triangles;
             
             newVert = new Vector3[baseMesh.vertices.Length];
             
             
             
             short i;
             
             // Make a CWeightList for each bone in the skinned mesh         
             
             nodeWeights = new CWeightList[rend.bones.Length];
             
             for ( i=0 ; i<rend.bones.Length ; i++ )
                 
             {
                 
                 nodeWeights[i] = new CWeightList();
                 
                 nodeWeights[i].transform = rend.bones[i];
                 
             }
             
             
             
             // Create a bone weight list for each bone, ready for quick calculation during an update...
             
             Vector3 localPt;
             
             for ( i=0 ; i<baseMesh.vertices.Length ; i++ )
                 
             {
                 
                 BoneWeight bw = baseMesh.boneWeights[i];
                 
                 if (bw.weight0!=0.0f)
                     
                 {
                     
                     localPt = baseMesh.bindposes[bw.boneIndex0].MultiplyPoint3x4( baseMesh.vertices[i] );
                     
                     nodeWeights[bw.boneIndex0].weights.Add( new CVertexWeight( i, localPt, bw.weight0 ) );
                     
                 }
                 
                 if (bw.weight1!=0.0f)
                     
                 {
                     
                     localPt = baseMesh.bindposes[bw.boneIndex1].MultiplyPoint3x4( baseMesh.vertices[i] );
                     
                     nodeWeights[bw.boneIndex1].weights.Add( new CVertexWeight( i, localPt, bw.weight1 ) );
                     
                 }
                 
                 if (bw.weight2!=0.0f)
                     
                 {
                     
                     localPt = baseMesh.bindposes[bw.boneIndex2].MultiplyPoint3x4( baseMesh.vertices[i] );
                     
                     nodeWeights[bw.boneIndex2].weights.Add( new CVertexWeight( i, localPt, bw.weight2 ) );
                     
                 }
                 
                 if (bw.weight3!=0.0f)
                     
                 {
                     
                     localPt = baseMesh.bindposes[bw.boneIndex3].MultiplyPoint3x4( baseMesh.vertices[i] );
                     
                     nodeWeights[bw.boneIndex3].weights.Add( new CVertexWeight( i, localPt, bw.weight3 ) );
                     
                 }
                 
             }
             
             
             
             UpdateCollisionMesh();
             
         }
         
         else
             
         {
             
             Debug.LogError(gameObject.name + ": SkinnedCollisionHelper: this object either has no SkinnedMeshRenderer or has no MeshCollider!");
             
         }
         
         
         
     }
     
     
     
     
     
     // Function:    UpdateCollisionMesh
     
     //  Manually recalculates the collision mesh of the skinned mesh on this
     
     // object.
     
     void UpdateCollisionMesh()
         
     {
         
         if (mesh!=null)
             
         {
             
             // Start by initializing all vertices to 'empty'
             
             for ( int i=0 ; i<newVert.Length ; i++ )
                 
             {
                 
                 newVert[i] = new Vector3(0,0,0);
                 
             }
             
             
             
             // Now get the local positions of all weighted indices...
             
             foreach ( CWeightList wList in nodeWeights )
                 
             {
                 
                 foreach ( CVertexWeight vw in wList.weights )
                     
                 {
                     
                     newVert[vw.index] += wList.transform.localToWorldMatrix.MultiplyPoint3x4( vw.localPosition ) * vw.weight;
                     
                 }
                 
             }
             
             
             
             // Now convert each point into local coordinates of this object.
             
             for ( int i=0 ; i<newVert.Length ; i++ )
                 
             {
                 
                 newVert[i] = transform.InverseTransformPoint( newVert[i] );
                 
             }
             
             
             
             // Update the mesh (& collider) with the updated vertices
             
             mesh.vertices = newVert;
             
             mesh.RecalculateBounds();
             
             collide.sharedMesh = mesh;
             
         }
         
     }
     
     
     
     
     
     // Function:    Update
     
     //  If the 'forceUpdate' flag is set, updates the collision mesh for the skinned mesh on this object 
     
     void Update()
         
     {
         
         if (forceUpdate)
             
         {
             
             forceUpdate = false;
             
             UpdateCollisionMesh();
             
         }
         
     }
     
 }
 
 class CVertexWeight
 {
     
     public int index;
     public Vector3 localPosition;
     public float weight;
     
     public CVertexWeight(int i, Vector3 p, float w)
     {
         
         index = i;
         localPosition = p;
         weight = w;
     }
     
 }
 
 class CWeightList
 {
     
     public Transform transform;
     public ArrayList weights;
     public CWeightList()
     {
         weights = new ArrayList(); 
     }
     
 }

I got it from http://forum.unity3d.com/threads/14378-Raycast-without-colliders.

It attaches mesh to mesh collider.But it takes too much time.

I want to save a detected mesh as mesh in my project.

please help me.

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
0
Best Answer

Answer by ZDS Alpha · Mar 09, 2014 at 10:10 AM

This problem is solved.

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

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

Pushing a mesh with mesh collider 1 Answer

Scaling the Mesh Collider 4 Answers

Why am I getting a null reference exception 1 Answer

How to remove internal triangle/faces when combining mesh 0 Answers

Mesh Collider Issue(?) - Raycast (ScreenPointToRay) Appears to Collide on Nothing 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