Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Whystler · Aug 09, 2011 at 01:36 AM · mesh

Script attached to mesh affects all meshes?

I applied a script to a mesh that allows me to click on the mesh and deform the vertices. It works, but even though it is applied to only one mesh, the deformation is possible on any other mesh in the scene - including the terrain. How do I make the script only apply to the mesh that it is attached to?

Thank you!

-Whystler

Comment
Add comment · Show 2
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 Whystler · Aug 09, 2011 at 03:19 AM 0
Share

Updating this question with more information in the event that it is needed. The script in question is the Vertex Paint script provided with the Procedural Unity Examples. It is placed on an imported mesh asset, but even so, it's function extends to the all other meshes added to the scene.

avatar image ina · Oct 29, 2011 at 10:57 AM 0
Share

Apparently .mesh is deprecated to .shared$$anonymous$$esh ... so this may be an issue for individual mesh edits in future.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FTheCloud · Aug 09, 2011 at 02:00 AM

put this in your update and then use the var "mesh".

 var mesh : Mesh = GetComponent(MeshFilter).mesh;

This gets the mesh of the gameObject its attached to.

Comment
Add comment · Show 5 · 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 Whystler · Aug 09, 2011 at 03:03 AM 0
Share

I'm about be a pain, I know it :)

But I'm not sure what to do with this. I tried putting it into the javascript in different ways and always get an error. So I assume this is not what you intended me to do.

Would you be able to tell me more specifically exactly what to do with this bit of code?

Thanks!

-Whystler

avatar image FTheCloud · Aug 09, 2011 at 03:32 AM 0
Share

You're not supposed to copy and paste it your supposed to implement it into your script. I can't tell you what to do and were to put it since I have no idea what your script looks like and how you might have it set up.

avatar image Whystler · Aug 09, 2011 at 03:41 AM 0
Share

I have updated the initial question post above with a comment about the script. For convenience, here is the script:

(PART 1)

var radius = 1.0; var pull = 10.0; private var unapplied$$anonymous$$esh : $$anonymous$$eshFilter;

enum FallOff { Gauss, Linear, Needle } var fallOff = FallOff.Gauss;

static function LinearFalloff (distance : float , inRadius : float) { return $$anonymous$$athf.Clamp01(1.0 - distance / inRadius); }

static function GaussFalloff (distance : float , inRadius : float) { return $$anonymous$$athf.Clamp01 ($$anonymous$$athf.Pow (360.0, -$$anonymous$$athf.Pow (distance / inRadius, 2.5) - 0.01)); }

function NeedleFalloff (dist : float, inRadius : float) { return -(dist*dist) / (inRadius * inRadius) + 1.0; }

function Deform$$anonymous$$esh (mesh : $$anonymous$$esh, position : Vector3, power : float, inRadius : float) { var vertices = mesh.vertices; var normals = mesh.normals; var sqrRadius = inRadius * inRadius;

avatar image Whystler · Aug 09, 2011 at 03:41 AM 0
Share

(PART 2)

 // Calculate averaged normal of all surrounding vertices    
 var averageNormal = Vector3.zero;
 for (var i=0;i<vertices.length;i++)
 {
     var sqr$$anonymous$$agnitude = (vertices[i] - position).sqr$$anonymous$$agnitude;
     // Early out if too far away
     if (sqr$$anonymous$$agnitude > sqrRadius)
         continue;

     var distance = $$anonymous$$athf.Sqrt(sqr$$anonymous$$agnitude);
     var falloff = LinearFalloff(distance, inRadius);
     averageNormal += falloff * normals[i];
 }
 averageNormal = averageNormal.normalized;
 
 // Deform vertices along averaged normal
 for (i=0;i<vertices.length;i++)
 {
     sqr$$anonymous$$agnitude = (vertices[i] - position).sqr$$anonymous$$agnitude;
     // Early out if too far away
     if (sqr$$anonymous$$agnitude > sqrRadius)
         continue;

     distance = $$anonymous$$athf.Sqrt(sqr$$anonymous$$agnitude);
     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;
 }
 
 mesh.vertices = vertices;
 mesh.RecalculateNormals();
 mesh.RecalculateBounds();

}

function Update () {

 // When no button is pressed we update the mesh collider
 if (!Input.Get$$anonymous$$ouseButton (0))
 {
     // Apply collision mesh when we let go of button
     Apply$$anonymous$$eshCollider();
     return;
 }
     
     
 // Did we hit the surface?
 var hit : RaycastHit;
 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 if (Physics.Raycast (ray, hit))
 {
     var filter : $$anonymous$$eshFilter = hit.collider.GetComponent($$anonymous$$eshFilter);
     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 != unapplied$$anonymous$$esh)
         {
             Apply$$anonymous$$eshCollider();
             unapplied$$anonymous$$esh = filter;
         }
         
         // Deform mesh
         var relativePoint = filter.transform.InverseTransformPoint(hit.point);
         Deform$$anonymous$$esh(filter.mesh, relativePoint, pull * Time.deltaTime, radius);
     }
 }

}

function Apply$$anonymous$$eshCollider () { if (unapplied$$anonymous$$esh && unapplied$$anonymous$$esh.GetComponent($$anonymous$$eshCollider)) { unapplied$$anonymous$$esh.GetComponent($$anonymous$$eshCollider).mesh = unapplied$$anonymous$$esh.mesh; } unapplied$$anonymous$$esh = null; }

avatar image Whystler · Aug 09, 2011 at 03:42 AM 0
Share

I hope that came out ok ...

-Whystler

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Camera Meshrenderer in a multiplayer game 2 Answers

How should I describe to access child mesh in Java 1 Answer

Cutting a Mesh Into Smaller Pieces 5 Answers

Mesh deformation simulating a fire 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