I don’t wannt to start to thread therefore I ask here my two questions about different themes.
When I hit a object it deform his mesh.
var mesh : Mesh = hit.transform.GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = mesh.vertices;
var triangles : int[] = mesh.triangles;
// Extract local space vertices that were hit
vertices[triangles[hit.triangleIndex * 3 + 0]] += (hit.transform.position-transform.position)/Vector3.Distance(hit.transform.position,transform.position)*multiplyer*Time.deltaTime;
vertices[triangles[hit.triangleIndex * 3 + 1]] += (hit.transform.position-transform.position)/Vector3.Distance(hit.transform.position,transform.position)*multiplyer*Time.deltaTime;
vertices[triangles[hit.triangleIndex * 3 + 2]] += (hit.transform.position-transform.position)/Vector3.Distance(hit.transform.position,transform.position)*multiplyer*Time.deltaTime;
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = vertices;
mesh.RecalculateBounds();
hit.transform.GetComponent(MeshCollider).sharedMesh = mesh;
The problem is at some time it gives my a thing like that:
Why? And how is it to fix?
In my multiplayer fps game I change the weapons.
I do this trough SetActiveRecursively to false and true. (over a RPC call) But If I’m shooting and changing the weapons at the same time my last weapon won’t work again.
I think this is because my shooting function is not in the update function and it fire all 0.2 seconds. (So it isn’t relative to the frame rate) The function will be interrupted.
How can I fix that?
I have a gameobject and the weapons are children of it. On each weapon is a shooting script that activates and deactivates the muzzle flash and the shooting light over a RPC call.
What he means is that your script gets a triangle, then deforms the triangle in a direction. We want to know if the object your deforming has more than one vertex per point. 99% of the time, this is true, so you will need to cycle through all of the vertices and find the ones affected by the deformation, then deform only those vertices. There are no triangles in this case.
My guess is that the vertices are not welded together, if you’re forcing the triangle to move then all neighboring triangles must deform since they are connected by the vertices of the attacked triangle.
Since it looks like it’s moving the triangle back but leaving the neighbor then I’m guessing there are duplicated vertices, otherwise what is the neighbor triangle composed of? The program should not be creating new vertices to ensure the neighbor triangle is still shaped properly.
*edit
like MrB mentions, deforming vertices better.
Instead of finding one triangle, and getting the vertices from that triangle, search through all the vertices, and find the ones that are closest to the hit.point in local space.
Ok, I wrote a code for subdividing in Unity but it seems that it doesn’t work as I want. I calculate a vertex in the middle of each triangle. And with that I have 3 new triangles per 1 old triangle.
It has a litte error.
Maybe somebody find it.
code:
var material : Material;
function Awake() {
///////////////////////////////////////////////////////////
//// initialize
///////////////////////////////////////////////////////////
if(GetComponent(MeshFilter) == null)
gameObject.AddComponent(MeshFilter);
if(GetComponent(MeshRenderer) == null)
gameObject.AddComponent(MeshRenderer);
if(GetComponent(MeshCollider) == null)
gameObject.AddComponent(MeshCollider);
var mesh : Mesh = new Mesh();
GetComponent(MeshFilter).mesh = mesh;
GetComponent(MeshRenderer).material = material;
///////////////////////////////////////////////////////////
//// build mesh
///////////////////////////////////////////////////////////
//// vertices
var verts : Vector3[] =
[
Vector3(0,0,0),Vector3(0,1,0),Vector3(1,0,0)
];
//// uvs
var uvs : Vector2[] =
[
Vector2(0,0),Vector2(0,1),Vector2(1,0)
];
//// triangles
var trigs : int[] =
[
0,1,2
];
Debug.Log(verts.length);
///////////////////////////////////////////////////////////
//// subdivide mesh
///////////////////////////////////////////////////////////
//// verticles
var zv : Vector3[] = new Vector3[trigs.length];
for(var m : int = 0;m<trigs.length/4;m++) {
var middlevert = (verts[m]+verts[m++]+verts[m+2])/3;
zv[m] = middlevert;
}
var nv : Vector3[] = new Vector3[verts.length+zv.length];
for(var zev : int = 0;zev<zv.length;zev++) {
nv[zev] = zv[zev];
}
for(var zevb : int = 0;zevb<verts.length;zevb++) {
nv[zevb+zv.length] = verts[zevb];
}
verts = new Vector3[nv.length];
for(var ev : int = 0;ev<nv.length;ev++) {
verts[ev] = nv[ev];
}
//// uvs
uvs = new Vector2[verts.length];
for(var uvsi : int = 0;uvsi<verts.length;uvsi++)
uvs[uvsi] = Vector2(verts[uvsi].x,verts[uvsi].y);
//// triangles
trigs = new int[verts.length];
for(var trigsi : int = 0;trigsi<verts.length;trigsi++)
trigs[trigsi] = trigsi;
///////////////////////////////////////////////////////////
//// apply mesh
///////////////////////////////////////////////////////////
mesh.vertices = verts;
mesh.normals = verts;
mesh.uv = uvs;
mesh.triangles = trigs;
GetComponent(MeshFilter).mesh = mesh;
GetComponent(MeshCollider).sharedMesh = mesh;
Debug.Log(verts.length);
mesh.RecalculateBounds();
}
There is a flaw in the concept. Subdivision needs to be based on triangular insets, not vertex insets. This means you will get 6 points from 3, and a total of 6 triangles from one.
You can develop it based off the triangular shape, meaning you can pull bulge from the opposing angle or leave the triangle flat. In either event, you will have to develop a new geometric shape independent of the current geometric shape.
The whole thing is called Tessellation in 3ds Max.