- Home /
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
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.
Apparently .mesh is deprecated to .shared$$anonymous$$esh ... so this may be an issue for individual mesh edits in future.
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.
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
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.
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;
(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; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612173154im_/https://answers.unity.com/themes/thub/images/avi.jpg)