Main Page

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Hearme (Talk); changed back to last version by NCarter)
(Deformation Smooth Filters for Meshes)
Line 1: Line 1:
[[Image:Splash.jpg]]
+
== Introduction ==
  
== Support UnifyWiki ==
+
This c# code demonstrates how to deformate a mesh at runtime using smoothing algorithms.
[http://www.dreamhost.com/donate.cgi?id=9231 Donate towards hosting of this wiki!]
+
  
== Welcome to the UnifyWiki ==
+
To solve this problem in a reasonable computational time a good algorithm is needed to
 +
solve the adjacent vertex problem.  Unity lacks mesh manipulation utilities and some manual
 +
effort is required to achieve this. 
  
The UnifyWiki is part of the [http://unifycommunity.com Unify Community] experience and is a place to find and share [http://unity3d.com/ Unity] knowledge. Special thanks to [[User:Aarku|Aarku]], [[User:Dho|Dho]], [[User:NCarter|NCarter]], [[User:KeliHlodversson|Freyr]], [[User:Outcast|Outcast]] and others.
+
(Note: I'm no unity expert so please don't shoot the piano player he is trying his best and improve anything.)
  
We're currently working on [[Special:Statistics|{{NUMBEROFARTICLES}}]] [[Special:Allpages|articles]] about Unity and related subjects.
+
== Basic Theory ==
  
Please see the [[:Talk:Main Page|discussion page]] of the main page for current goals and status'''Please read these [[:Help:Contents|general guidelines]] before you begin editing.'''
+
Given a Mesh/SkinnedMeshRenderer  Vector3[] v = mesh.vertices, and int[] t = mesh.triangles.
  
To create a new page, add a link to an existing page, then follow the link and edit the new page. It's a good idea to copy the layout from an existing page initially, to ensure that your contribution matches the conventions used in our existing articles.  If you just want to experiment, you can do so in the [[:Unify Community Wiki:Sandbox|sandbox]].
+
For each vertex i in the mesh, find the set of neighboring vertices.
  
== News ==
+
Computing the Laplacian Smooth Filter p[i] = ( 1 / number of adjacent vertices ) * summation of the adjacent vertices.
  
* Syntax highlighting has been improved.  Please see the [[:Talk:Main_Page|discussion page]] for more information.
+
Laplacian smoothing introduces shrinkage, so the HC-Algorithm is applied as an extension to reduce this issue. (More computationally expensive though !).
  
* Added cutting edge Captcha (now with angled lines!) to the Sign Up process. This should (hopefully) cut down on all those wonderful Wow Gold Mining opportunity posts we've been deleting.
+
== General Discussion ==
  
* The wiki has changed homes again. The new server should be faster, it is much larger, and UT is providing backup off the server, Thanks! But as always moves are not simple things. When you find broken things feel free to contact Jeff (guategeek), Neil (NCarter), Jon (Aarku) or David Helgason so we can try get them fixed ASAP. Thanks again for all the great stuff you guys are posting here.
+
Although the code demonstrates smoothing meshes, it provides a more generic strategy for manipulating meshes in Unity as many
 +
deformation algorithms could be applied. Have Fun!
  
* It is now possible to upload '''zip''' archives to the Unify wiki.  We encourage you to provide a zipped copy of your script or shader when adding a new article, as this will help to avoid the bug in Safari which inserts garbage characters into text which is copied and pasted from these pages.
 
  
* The maximum file size for any uploaded file is now '''256KB''', and this includes images as well as zip files.  Unfortunately, '''unityPackage''' files are not directly supported at present, as we have been unable to make MediaWiki recognise the unusually long filename extension.  If you wish to add a unityPackage to the wiki, please zip it first.
+
== TestSmoothFilter ==
 +
<javascript>
 +
using UnityEngine;
 +
using System.Collections;
  
== Call for Article Requests ==
+
/*
Please see the [[:Talk:Main Page|discussion page]] of the main page to add your requests for scripts, shaders, tips, or wizards.
+
Apply to any meshed gameobject for smoothing.
  
== Main Sections ==
+
        Works also by replacing MeshFilter with SkinnedMeshRenderer and use sharedMesh
  
* [[Extensions]] - Further extend the functionality of Unity with .NET plugins (suitable for any Unity licence) and native code plugins (for Pro users only).
+
At present tests Laplacian Smooth Filter and HC Reduced Shrinkage Variant Filter
 +
*/
 +
public class TestSmoothFilter : MonoBehaviour
 +
{
  
* [[Particle Library]] - Pre-configured particle systems for use in your own projects.
+
private Mesh sourceMesh;
 +
private Mesh workingMesh;
 +
 +
void Start ()
 +
{
 +
MeshFilter meshFilter = gameObject.GetComponentInChildren<MeshFilter>();
 +
 +
// Clone the cloth mesh to work on
 +
sourceMesh = new Mesh();
 +
// Get the sourceMesh
 +
sourceMesh = meshFilter.mesh;
 +
// Clone the sourceMesh
 +
workingMesh = CloneMesh(sourceMesh);
 +
// Reference workingMesh to see deformations
 +
skinnedmesh.mesh = workingMesh;
 +
 +
 +
// Apply Laplacian Smoothing Filter to Mesh
 +
int iterations = 1;
 +
for(int i=0; i<iterations; i++)
 +
workingMesh.vertices = SmoothFilter.laplacianFilter(workingMesh.vertices, workingMesh.triangles);
 +
//workingMesh.vertices = hcFilter(sourceMesh.vertices, workingMesh.vertices, workingMesh.triangles, 0.0f, 0.5f);
 +
}
 +
 +
// Clone a mesh
 +
    private static Mesh CloneMesh(Mesh mesh)
 +
    {
 +
        Mesh clone = new Mesh();
 +
        clone.vertices = mesh.vertices;
 +
        clone.normals = mesh.normals;
 +
        clone.tangents = mesh.tangents;
 +
        clone.triangles = mesh.triangles;
 +
        clone.uv = mesh.uv;
 +
        clone.uv1 = mesh.uv1;
 +
        clone.uv2 = mesh.uv2;
 +
        clone.bindposes = mesh.bindposes;
 +
        clone.boneWeights = mesh.boneWeights;
 +
        clone.bounds = mesh.bounds;
 +
        clone.colors = mesh.colors;
 +
        clone.name = mesh.name;
 +
        //TODO : Are we missing anything?
 +
        return clone;
 +
    }
 +
}</javascript>
  
* [[Programming]] - Unity programming resources
+
== SmoothFilter.cs ==
 +
<javascript>
 +
using UnityEngine;
 +
using System.Collections;
 +
using System.Collections.Generic;
  
* [[Scripts]] - Everything from general purpose effects to debugging.
+
/*
 +
    MeshSmoothTest
 +
 +
Laplacian Smooth Filter, HC-Smooth Filter
 +
 +
MarkGX, Jan 2011
 +
*/
 +
public class SmoothFilter : MonoBehaviour
 +
{
 +
/*
 +
Standard Laplacian Smooth Filter
 +
*/
 +
public static Vector3[] laplacianFilter(Vector3[] sv, int[] t)
 +
{
 +
Vector3[] wv = new Vector3[sv.Length];
 +
List<Vector3> adjacentVertices = new List<Vector3>();
 +
 +
float dx = 0.0f;
 +
float dy = 0.0f;
 +
float dz = 0.0f;
 +
 +
for (int vi=0; vi< sv.Length; vi++)
 +
{
 +
// Find the sv neighboring vertices
 +
adjacentVertices = MeshUtils.findAdjacentNeighbors (sv, t, sv[vi]);
 +
 +
if (adjacentVertices.Count != 0)
 +
{
 +
dx = 0.0f;
 +
dy = 0.0f;
 +
dz = 0.0f;
 +
 +
//Debug.Log("Vertex Index Length = "+vertexIndexes.Length);
 +
// Add the vertices and divide by the number of vertices
 +
for (int j=0; j<adjacentVertices.Count; j++)
 +
{
 +
dx += adjacentVertices[j].x;
 +
dy += adjacentVertices[j].y;
 +
dz += adjacentVertices[j].z;
 +
}
 +
 +
wv[vi].x = dx / adjacentVertices.Count;
 +
wv[vi].y = dy / adjacentVertices.Count;
 +
wv[vi].z = dz / adjacentVertices.Count;
 +
}
 +
}
 +
 +
return wv;
 +
}
 +
 +
/*
 +
HC (Humphrey’s Classes) Smooth Algorithm - Reduces Shrinkage of Laplacian Smoother
 +
 +
Where sv - original points
 +
pv - previous points,
 +
alpha [0..1] influences previous points pv, e.g. 0
 +
beta  [0..1] e.g. > 0.5
 +
*/
 +
public static Vector3[] hcFilter(Vector3[] sv, Vector3[] pv, int[] t, float alpha, float beta)
 +
{
 +
Vector3[] wv = new Vector3[sv.Length];
 +
Vector3[] bv = new Vector3[sv.Length];
 +
  
* [[Shaders]] - Specialised shaders to make your materials more refined.
+
 +
// Perform Laplacian Smooth
 +
wv = laplacianFilter(sv, t);
 +
 +
// Compute Differences
 +
for(int i=0; i<wv.Length; i++)
 +
{
 +
bv[i].x = wv[i].x - (alpha * sv[i].x + ( 1 - alpha ) * sv[i].x );
 +
bv[i].y = wv[i].y - (alpha * sv[i].y + ( 1 - alpha ) * sv[i].y );
 +
bv[i].z = wv[i].z - (alpha * sv[i].z + ( 1 - alpha ) * sv[i].z );
 +
}
 +
 +
List<int> adjacentIndexes = new List<int>();
 +
 +
float dx = 0.0f;
 +
float dy = 0.0f;
 +
float dz = 0.0f;
 +
 +
for(int j=0; j<bv.Length; j++)
 +
{
 +
adjacentIndexes.Clear();
 +
 +
// Find the bv neighboring vertices
 +
adjacentIndexes = MeshUtils.findAdjacentNeighborIndexes (sv, t, sv[j]);
 +
 +
dx = 0.0f;
 +
dy = 0.0f;
 +
dz = 0.0f;
 +
 +
for (int k=0; k<adjacentIndexes.Count; k++)
 +
{
 +
dx += bv[adjacentIndexes[k]].x;
 +
dy += bv[adjacentIndexes[k]].y;
 +
dz += bv[adjacentIndexes[k]].z;
 +
 +
}
 +
 +
wv[j].x -= beta * bv[j].x + ((1 - beta) / adjacentIndexes.Count) * dx;
 +
wv[j].y -= beta * bv[j].y + ((1 - beta) / adjacentIndexes.Count) * dy;
 +
wv[j].z -= beta * bv[j].z + ((1 - beta) / adjacentIndexes.Count) * dz;
 +
}
 +
 +
return wv;
 +
}
 +
}</javascript>
  
* [[Wizards]] - Scripts to extend the Unity editor.
+
== MeshUtils.cs ==
 +
<javascript>
 +
using UnityEngine;
 +
using System.Collections;
 +
using System.Collections.Generic;
  
== Extras ==
+
/*
 +
Useful mesh functions
 +
*/
 +
public class MeshUtils : MonoBehaviour
 +
{
 +
// Finds a set of adjacent vertices for a given vertex
 +
// Note the success of this routine expects only the set of neighboring faces to eacn contain one vertex corresponding
 +
// to the vertex in question
 +
public static List<Vector3> findAdjacentNeighbors ( Vector3[] v, int[] t, Vector3 vertex )
 +
{
 +
List<Vector3>adjacentV = new List<Vector3>();
 +
List<int>facemarker = new List<int>();
 +
int facecount = 0;
 +
 +
// Find matching vertices
 +
for (int i=0; i<v.Length; i++)
 +
if (Mathf.Approximately (vertex.x, v[i].x) &&
 +
Mathf.Approximately (vertex.y, v[i].y) &&
 +
Mathf.Approximately (vertex.z, v[i].z))
 +
{
 +
int v1 = 0;
 +
int v2 = 0;
 +
    bool marker = false;
 +
 +
// Find vertex indices from the triangle array
 +
for(int k=0; k<t.Length; k=k+3)
 +
if(facemarker.Contains(k) == false)
 +
{
 +
v1 = 0;
 +
v2 = 0;
 +
marker = false;
 +
 +
if(i == t[k])
 +
{
 +
v1 = t[k+1];
 +
v2 = t[k+2];
 +
marker = true;
 +
}
  
* [[Tips|Tips, Tools & Tricks]] - Tips and tricks for editing Unity content and supported tools & applications.
+
if(i == t[k+1])
 +
{
 +
v1 = t[k];
 +
v2 = t[k+2];
 +
marker = true;
 +
}
 +
 +
if(i == t[k+2])
 +
{
 +
v1 = t[k];
 +
v2 = t[k+1];
 +
marker = true;
 +
}
 +
 +
facecount++;
 +
if(marker)
 +
{
 +
// Once face has been used mark it so it does not get used again
 +
facemarker.Add(k);
  
* [[Tutorials]] - Tutorials for all things Unity. These are usually of larger scope than [[Tips|Tips and Tricks]].
+
// Add non duplicate vertices to the list
 +
if ( isVertexExist(adjacentV, v[v1]) == false )
 +
{
 +
adjacentV.Add(v[v1]);
 +
//Debug.Log("Adjacent vertex index = " + v1);
 +
}
 +
 +
if ( isVertexExist(adjacentV, v[v2]) == false )
 +
{
 +
adjacentV.Add(v[v2]);
 +
//Debug.Log("Adjacent vertex index = " + v2);
 +
}
 +
marker = false;
 +
}
 +
}
 +
}
 +
 +
//Debug.Log("Faces Found = " + facecount);
 +
 +
        return adjacentV;
 +
}
 +
 +
 +
// Finds a set of adjacent vertices for a given vertex
 +
// Note the success of this routine expects only the set of neighboring faces to eacn contain one vertex corresponding
 +
// to the vertex in question
 +
public static List<int> findAdjacentNeighborIndexes ( Vector3[] v, int[] t, Vector3 vertex )
 +
{
 +
List<int>adjacentIndexes = new List<int>();
 +
List<Vector3>adjacentV = new List<Vector3>();
 +
List<int>facemarker = new List<int>();
 +
int facecount = 0;
 +
 +
// Find matching vertices
 +
for (int i=0; i<v.Length; i++)
 +
if (Mathf.Approximately (vertex.x, v[i].x) &&
 +
Mathf.Approximately (vertex.y, v[i].y) &&
 +
Mathf.Approximately (vertex.z, v[i].z))
 +
{
 +
int v1 = 0;
 +
int v2 = 0;
 +
    bool marker = false;
 +
 +
// Find vertex indices from the triangle array
 +
for(int k=0; k<t.Length; k=k+3)
 +
if(facemarker.Contains(k) == false)
 +
{
 +
v1 = 0;
 +
v2 = 0;
 +
marker = false;
 +
 +
if(i == t[k])
 +
{
 +
v1 = t[k+1];
 +
v2 = t[k+2];
 +
marker = true;
 +
}
  
* [[Contests]] - User contests for fun and fame!
+
if(i == t[k+1])
 +
{
 +
v1 = t[k];
 +
v2 = t[k+2];
 +
marker = true;
 +
}
 +
 +
if(i == t[k+2])
 +
{
 +
v1 = t[k];
 +
v2 = t[k+1];
 +
marker = true;
 +
}
 +
 +
facecount++;
 +
if(marker)
 +
{
 +
// Once face has been used mark it so it does not get used again
 +
facemarker.Add(k);
  
* [[Unity Projects]] - Community development efforts to extend Unity.
+
// Add non duplicate vertices to the list
 
+
if ( isVertexExist(adjacentV, v[v1]) == false )
* [[IRC|IRC Chatroom]] - Your portal to the Unity community.
+
{
 
+
adjacentV.Add(v[v1]);
* [[Unify Community Wiki:Community Portal|Community Portal]] - Your portal to the Unity community.
+
adjacentIndexes.Add(v1);
 
+
//Debug.Log("Adjacent vertex index = " + v1);
----
+
}
 
+
Please see [http://meta.wikipedia.org/wiki/MediaWiki_i18n documentation on customizing the interface]
+
if ( isVertexExist(adjacentV, v[v2]) == false )
and the [http://meta.wikipedia.org/wiki/MediaWiki_User%27s_Guide MediaWiki User's Guide] for usage and configuration help.
+
{
 +
adjacentV.Add(v[v2]);
 +
adjacentIndexes.Add(v2);
 +
//Debug.Log("Adjacent vertex index = " + v2);
 +
}
 +
marker = false;
 +
}
 +
}
 +
}
 +
 +
//Debug.Log("Faces Found = " + facecount);
 +
 +
        return adjacentIndexes;
 +
}
 +
 +
// Does the vertex v exist in the list of vertices
 +
static bool isVertexExist(List<Vector3>adjacentV, Vector3 v)
 +
{
 +
bool marker = false;
 +
foreach (Vector3 vec in adjacentV)
 +
  if (Mathf.Approximately(vec.x,v.x) && Mathf.Approximately(vec.y,v.y) && Mathf.Approximately(vec.z,v.z))
 +
  {
 +
      marker = true;
 +
  break;
 +
  }
 +
 
 +
return marker;
 +
}
 +
}
 +
</javascript>

Revision as of 12:23, 10 January 2011

Contents

Introduction

This c# code demonstrates how to deformate a mesh at runtime using smoothing algorithms.

To solve this problem in a reasonable computational time a good algorithm is needed to solve the adjacent vertex problem. Unity lacks mesh manipulation utilities and some manual effort is required to achieve this.

(Note: I'm no unity expert so please don't shoot the piano player he is trying his best and improve anything.)

Basic Theory

Given a Mesh/SkinnedMeshRenderer Vector3[] v = mesh.vertices, and int[] t = mesh.triangles.

For each vertex i in the mesh, find the set of neighboring vertices.

Computing the Laplacian Smooth Filter p[i] = ( 1 / number of adjacent vertices ) * summation of the adjacent vertices.

Laplacian smoothing introduces shrinkage, so the HC-Algorithm is applied as an extension to reduce this issue. (More computationally expensive though !).

General Discussion

Although the code demonstrates smoothing meshes, it provides a more generic strategy for manipulating meshes in Unity as many deformation algorithms could be applied. Have Fun!


TestSmoothFilter

<javascript> using UnityEngine; using System.Collections;

/* Apply to any meshed gameobject for smoothing.

       Works also by replacing MeshFilter with SkinnedMeshRenderer and use sharedMesh

At present tests Laplacian Smooth Filter and HC Reduced Shrinkage Variant Filter

  • /

public class TestSmoothFilter : MonoBehaviour {

private Mesh sourceMesh; private Mesh workingMesh;

void Start () { MeshFilter meshFilter = gameObject.GetComponentInChildren<MeshFilter>();

// Clone the cloth mesh to work on sourceMesh = new Mesh(); // Get the sourceMesh sourceMesh = meshFilter.mesh; // Clone the sourceMesh workingMesh = CloneMesh(sourceMesh); // Reference workingMesh to see deformations skinnedmesh.mesh = workingMesh;


// Apply Laplacian Smoothing Filter to Mesh int iterations = 1; for(int i=0; i<iterations; i++) workingMesh.vertices = SmoothFilter.laplacianFilter(workingMesh.vertices, workingMesh.triangles); //workingMesh.vertices = hcFilter(sourceMesh.vertices, workingMesh.vertices, workingMesh.triangles, 0.0f, 0.5f); }

// Clone a mesh

   private static Mesh CloneMesh(Mesh mesh)
   {
       Mesh clone = new Mesh();
       clone.vertices = mesh.vertices;
       clone.normals = mesh.normals;
       clone.tangents = mesh.tangents;
       clone.triangles = mesh.triangles;
       clone.uv = mesh.uv;
       clone.uv1 = mesh.uv1;
       clone.uv2 = mesh.uv2;
       clone.bindposes = mesh.bindposes;
       clone.boneWeights = mesh.boneWeights;
       clone.bounds = mesh.bounds;
       clone.colors = mesh.colors;
       clone.name = mesh.name;
       //TODO : Are we missing anything?
       return clone;
   }

}</javascript>

SmoothFilter.cs

<javascript> using UnityEngine; using System.Collections; using System.Collections.Generic;

/*

   MeshSmoothTest

Laplacian Smooth Filter, HC-Smooth Filter

MarkGX, Jan 2011

  • /

public class SmoothFilter : MonoBehaviour { /* Standard Laplacian Smooth Filter */ public static Vector3[] laplacianFilter(Vector3[] sv, int[] t) { Vector3[] wv = new Vector3[sv.Length]; List<Vector3> adjacentVertices = new List<Vector3>();

float dx = 0.0f; float dy = 0.0f; float dz = 0.0f;

for (int vi=0; vi< sv.Length; vi++) { // Find the sv neighboring vertices adjacentVertices = MeshUtils.findAdjacentNeighbors (sv, t, sv[vi]);

if (adjacentVertices.Count != 0) { dx = 0.0f; dy = 0.0f; dz = 0.0f;

//Debug.Log("Vertex Index Length = "+vertexIndexes.Length); // Add the vertices and divide by the number of vertices for (int j=0; j<adjacentVertices.Count; j++) { dx += adjacentVertices[j].x; dy += adjacentVertices[j].y; dz += adjacentVertices[j].z; }

wv[vi].x = dx / adjacentVertices.Count; wv[vi].y = dy / adjacentVertices.Count; wv[vi].z = dz / adjacentVertices.Count; } }

return wv; }

/* HC (Humphrey’s Classes) Smooth Algorithm - Reduces Shrinkage of Laplacian Smoother

Where sv - original points pv - previous points, alpha [0..1] influences previous points pv, e.g. 0 beta [0..1] e.g. > 0.5 */ public static Vector3[] hcFilter(Vector3[] sv, Vector3[] pv, int[] t, float alpha, float beta) { Vector3[] wv = new Vector3[sv.Length]; Vector3[] bv = new Vector3[sv.Length];


// Perform Laplacian Smooth wv = laplacianFilter(sv, t);

// Compute Differences for(int i=0; i<wv.Length; i++) { bv[i].x = wv[i].x - (alpha * sv[i].x + ( 1 - alpha ) * sv[i].x ); bv[i].y = wv[i].y - (alpha * sv[i].y + ( 1 - alpha ) * sv[i].y ); bv[i].z = wv[i].z - (alpha * sv[i].z + ( 1 - alpha ) * sv[i].z ); }

List<int> adjacentIndexes = new List<int>();

float dx = 0.0f; float dy = 0.0f; float dz = 0.0f;

for(int j=0; j<bv.Length; j++) { adjacentIndexes.Clear();

// Find the bv neighboring vertices adjacentIndexes = MeshUtils.findAdjacentNeighborIndexes (sv, t, sv[j]);

dx = 0.0f; dy = 0.0f; dz = 0.0f;

for (int k=0; k<adjacentIndexes.Count; k++) { dx += bv[adjacentIndexes[k]].x; dy += bv[adjacentIndexes[k]].y; dz += bv[adjacentIndexes[k]].z;

}

wv[j].x -= beta * bv[j].x + ((1 - beta) / adjacentIndexes.Count) * dx; wv[j].y -= beta * bv[j].y + ((1 - beta) / adjacentIndexes.Count) * dy; wv[j].z -= beta * bv[j].z + ((1 - beta) / adjacentIndexes.Count) * dz; }

return wv; } }</javascript>

MeshUtils.cs

<javascript> using UnityEngine; using System.Collections; using System.Collections.Generic;

/* Useful mesh functions

  • /

public class MeshUtils : MonoBehaviour { // Finds a set of adjacent vertices for a given vertex // Note the success of this routine expects only the set of neighboring faces to eacn contain one vertex corresponding // to the vertex in question public static List<Vector3> findAdjacentNeighbors ( Vector3[] v, int[] t, Vector3 vertex ) { List<Vector3>adjacentV = new List<Vector3>(); List<int>facemarker = new List<int>(); int facecount = 0;

// Find matching vertices for (int i=0; i<v.Length; i++) if (Mathf.Approximately (vertex.x, v[i].x) && Mathf.Approximately (vertex.y, v[i].y) && Mathf.Approximately (vertex.z, v[i].z)) { int v1 = 0; int v2 = 0; bool marker = false;

// Find vertex indices from the triangle array for(int k=0; k<t.Length; k=k+3) if(facemarker.Contains(k) == false) { v1 = 0; v2 = 0; marker = false;

if(i == t[k]) { v1 = t[k+1]; v2 = t[k+2]; marker = true; }

if(i == t[k+1]) { v1 = t[k]; v2 = t[k+2]; marker = true; }

if(i == t[k+2]) { v1 = t[k]; v2 = t[k+1]; marker = true; }

facecount++; if(marker) { // Once face has been used mark it so it does not get used again facemarker.Add(k);

// Add non duplicate vertices to the list if ( isVertexExist(adjacentV, v[v1]) == false ) { adjacentV.Add(v[v1]); //Debug.Log("Adjacent vertex index = " + v1); }

if ( isVertexExist(adjacentV, v[v2]) == false ) { adjacentV.Add(v[v2]); //Debug.Log("Adjacent vertex index = " + v2); } marker = false; } } }

//Debug.Log("Faces Found = " + facecount);

       return adjacentV;

}


// Finds a set of adjacent vertices for a given vertex // Note the success of this routine expects only the set of neighboring faces to eacn contain one vertex corresponding // to the vertex in question public static List<int> findAdjacentNeighborIndexes ( Vector3[] v, int[] t, Vector3 vertex ) { List<int>adjacentIndexes = new List<int>(); List<Vector3>adjacentV = new List<Vector3>(); List<int>facemarker = new List<int>(); int facecount = 0;

// Find matching vertices for (int i=0; i<v.Length; i++) if (Mathf.Approximately (vertex.x, v[i].x) && Mathf.Approximately (vertex.y, v[i].y) && Mathf.Approximately (vertex.z, v[i].z)) { int v1 = 0; int v2 = 0; bool marker = false;

// Find vertex indices from the triangle array for(int k=0; k<t.Length; k=k+3) if(facemarker.Contains(k) == false) { v1 = 0; v2 = 0; marker = false;

if(i == t[k]) { v1 = t[k+1]; v2 = t[k+2]; marker = true; }

if(i == t[k+1]) { v1 = t[k]; v2 = t[k+2]; marker = true; }

if(i == t[k+2]) { v1 = t[k]; v2 = t[k+1]; marker = true; }

facecount++; if(marker) { // Once face has been used mark it so it does not get used again facemarker.Add(k);

// Add non duplicate vertices to the list if ( isVertexExist(adjacentV, v[v1]) == false ) { adjacentV.Add(v[v1]); adjacentIndexes.Add(v1); //Debug.Log("Adjacent vertex index = " + v1); }

if ( isVertexExist(adjacentV, v[v2]) == false ) { adjacentV.Add(v[v2]); adjacentIndexes.Add(v2); //Debug.Log("Adjacent vertex index = " + v2); } marker = false; } } }

//Debug.Log("Faces Found = " + facecount);

       return adjacentIndexes;

}

// Does the vertex v exist in the list of vertices static bool isVertexExist(List<Vector3>adjacentV, Vector3 v) { bool marker = false; foreach (Vector3 vec in adjacentV) if (Mathf.Approximately(vec.x,v.x) && Mathf.Approximately(vec.y,v.y) && Mathf.Approximately(vec.z,v.z)) { marker = true; break; }

return marker; } } </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox