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 DanceWreck · Apr 11, 2012 at 11:11 PM · javascriptupdatemeshcolliderdeform

problem deforming mesh colliders

I've basically just taken the paintverticles.js script from the file this page, and implemented it into a more fps-like scene.

The script works nicely the first time I deform each mesh, pushing the verticals up or down when clicked (depending on wether or not I'm holding down left shift), and updating the collision mesh so the character reacts to the terrain nicely.

However, when I release the mouse button after the first deform, and click to deform the same mesh again, the mesh filter is deformed, but the mesh collider is not. (i.e., the block is bent, but other colliders ignore the newly bent section)

Here's my modified version of PaintVerticles.js I'm using.

(this should be simple to figure this out, but I'm new to programming, so I'm stumped easily.)

 var radius = 1.0;
 var pull = 10.0;
 private var unappliedMesh : MeshFilter;
 //var digSound = AudioClip;
 
 enum FallOff { Gauss, Linear, Needle }
 var fallOff = FallOff.Gauss;
 
 static function LinearFalloff (distance : float , inRadius : float) {
     return Mathf.Clamp01(1.0 - distance / inRadius);
 }
 
 static function GaussFalloff (distance : float , inRadius : float) {
     return Mathf.Clamp01 (Mathf.Pow (360.0, -Mathf.Pow (distance / inRadius, 2.5) - 0.01));
 }
 
 function NeedleFalloff (dist : float, inRadius : float)
 {
     return -(dist*dist) / (inRadius * inRadius) + 1.0;
 }
 
 //function dig () {
 //    if (digSound)
 //    {
 //        AudioSource.PlayClipAtPoint(digSound, transform.position);
 //    }
 //}
 
 function DeformMesh (mesh : Mesh, position : Vector3, power : float, inRadius : float, deformDirection : int) {
     var vertices = mesh.vertices;
     var normals = mesh.normals;
     var sqrRadius = inRadius * inRadius;
     
     // Calculate averaged normal of all surrounding vertices    
     var averageNormal = Vector3.zero;
     for (var i=0;i<vertices.length;i++)
     {
         var sqrMagnitude = (vertices[i] - position).sqrMagnitude;
         // Early out if too far away
         if (sqrMagnitude > sqrRadius)
             continue;
 
         var distance = Mathf.Sqrt(sqrMagnitude);
         var falloff = LinearFalloff(distance, inRadius);
         averageNormal += falloff * normals[i];
     }
     averageNormal = averageNormal.normalized;
     
     // Deform vertices along averaged normal
     for (i=0;i<vertices.length;i++)
     {
         sqrMagnitude = (vertices[i] - position).sqrMagnitude;
         // Early out if too far away
         if (sqrMagnitude > sqrRadius)
             continue;
 
         distance = Mathf.Sqrt(sqrMagnitude);
         switch (fallOff)
         {
             case FallOff.Gauss:
                 falloff = GaussFalloff(distance, inRadius);
                 break;
             case FallOff.Needle:
                 falloff = NeedleFalloff(distance, inRadius);
                 break;
             default:
                 falloff = LinearFalloff(distance, inRadius);
                 break;
         }
         
         vertices[i] += averageNormal * falloff * power * deformDirection;
     }
     
     mesh.vertices = vertices;
     mesh.RecalculateNormals();
     mesh.RecalculateBounds();
 }
 
 
 function Update () {
     // When no mouse button is pressed we update the mesh collider
     
     if (!Input.GetMouseButton (0))
     {
         // Apply collision mesh when we let go of button
         ApplyMeshCollider();
         return;
     }
     var deformDirection = 1;
     var inversion = -1;
     if (Input.GetKey("left shift"))
     {
         deformDirection*=inversion;
     }
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     // Did we hit the surface?
     if (Physics.Raycast (ray, hit))
     {
         var filter : MeshFilter = hit.collider.GetComponent(MeshFilter);
         if (filter)
         {
             // Don't update mesh collider every frame since physX
             // does some heavy processing to optimize the collision mesh.
             // So this is not fast enough for real time updating every frame
             if (filter != unappliedMesh)
             {
                 ApplyMeshCollider();
                 unappliedMesh = filter;
             }
             
             // Deform mesh
             var relativePoint = filter.transform.InverseTransformPoint(hit.point);
             DeformMesh(filter.mesh, relativePoint, pull * Time.deltaTime, radius, -deformDirection);
         }
     }
 }
 
 function ApplyMeshCollider () {
     if (unappliedMesh && unappliedMesh.GetComponent(MeshCollider)) 
     {
         unappliedMesh.GetComponent(MeshCollider).mesh = unappliedMesh.mesh;
     }
     
     unappliedMesh = null;
 }

Comment
Add comment · Show 1
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 AlucardJay · Jul 04, 2012 at 01:51 PM 1
Share

Where is this script originally from? I first saw this in @nighthawx349 's question : http://answers.unity3d.com/questions/274269/mesh-deformation.html

I'm about to start on my own mesh deformations, so shall keep watch and post any findings =]

EDIT : never$$anonymous$$d, I found it. Didn't know this package existed =]

http://unity3d.com/support/resources/example-projects/procedural-examples about to d'load and look at : Crumple mesh modifier - A mesh deformed by Perlin Noise.

But to answer your question (and without reading the script), at some point the mesh should be assigned to the collider :

 this.transform.GetComponent($$anonymous$$eshCollider).shared$$anonymous$$esh = my$$anonymous$$esh;

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$eshCollider-shared$$anonymous$$esh.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by perplex · Dec 06, 2013 at 02:14 AM

I stumbeled over the same problem and found the answer somewhere here... Add the following before line 122 when assigning the new mesh.

 unappliedMesh.GetComponent(MeshCollider).mesh = null;
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with restarting the scene (js) 2 Answers

how turn off the mesh collider? 5 Answers

Javascript Class Update 2 Answers

Script not updating ? 0 Answers

How to code input. 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