68 using System.Collections.Generic;
74 private static Vector3
CENTER =
new Vector3(0f,0f,0f);
80 private static string TAG;
84 private static Vector3
FUDGE_FACTOR =
new Vector3(S_FUDGE_FACTOR, S_FUDGE_FACTOR, S_FUDGE_FACTOR);
101 List<List<Vector3>> normal_buffer =
new List<List<Vector3>>();
104 Vector3[] myNormals =
new Vector3[vCount];
107 for(
int i=0; i<vCount; i++) {
108 normal_buffer.Add(
new List<Vector3>());
109 myNormals[i] =
new Vector3(0f,0f,0f);
113 int index = triangles.Length;
116 for(
int i=0; i<index; i+=3) {
117 Vector3 p1 = vertices[triangles[i+0]];
118 Vector3 p2 = vertices[triangles[i+1]];
119 Vector3 p3 = vertices[triangles[i+2]];
121 Vector3 edge1 = p2 - p1;
122 Vector3 edge2 = p3 - p1;
123 Vector3 normal = Vector3.Cross(edge1, edge2);
128 normal_buffer[triangles[i+0]].Add(normal);
129 normal_buffer[triangles[i+1]].Add(normal);
130 normal_buffer[triangles[i+2]].Add(normal);
134 for(
int i=0; i<vCount; i++) {
135 int normalNumber = normal_buffer[i].Count;
138 for(
int j=0; j<normalNumber; j++)
139 myNormals[i] += normal_buffer[i][j];
142 myNormals[i].Normalize();
148 Vector3[] vertices = mesh.vertices;
149 Vector2[] uvs =
new Vector2[vertices.Length];
151 while (i < uvs.Length) {
152 uvs[i] =
new Vector2(vertices[i].x, vertices[i].z);
161 int[] triangles = mesh.triangles;
162 Vector3[] vertices = mesh.vertices;
163 Vector2[] uv = mesh.uv;
164 Vector3[] normals = mesh.normals;
167 int triangleCount = triangles.Length;
168 int vertexCount = vertices.Length;
170 Vector3[] tan1 =
new Vector3[vertexCount];
171 Vector3[] tan2 =
new Vector3[vertexCount];
173 Vector4[] tangents =
new Vector4[vertexCount];
175 for (
long a = 0; a < triangleCount; a += 3) {
176 long i1 = triangles[a + 0];
177 long i2 = triangles[a + 1];
178 long i3 = triangles[a + 2];
180 Vector3 v1 = vertices[i1];
181 Vector3 v2 = vertices[i2];
182 Vector3 v3 = vertices[i3];
188 float x1 = v2.x - v1.x;
189 float x2 = v3.x - v1.x;
190 float y1 = v2.y - v1.y;
191 float y2 = v3.y - v1.y;
192 float z1 = v2.z - v1.z;
193 float z2 = v3.z - v1.z;
195 float s1 = w2.x - w1.x;
196 float s2 = w3.x - w1.x;
197 float t1 = w2.y - w1.y;
198 float t2 = w3.y - w1.y;
200 float r = 1.0f / (s1 * t2 - s2 * t1);
202 Vector3 sdir =
new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
203 Vector3 tdir =
new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
214 for (
long a = 0; a < vertexCount; ++a) {
215 Vector3 n = normals[a];
220 Vector3.OrthoNormalize(ref n, ref t);
224 tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
227 mesh.tangents = tangents;
233 int[] triangles =
new int[mData.
triangles.Length];
235 for(
int i=0; i<mData.
triangles.Length; i+=3) {
245 Vector3[] normals = mData.
normals;
246 for(
int i=0; i<normals.Length; i++)
247 normals[i] = -normals[i];
254 Vector3[] vertices = mData.
vertices;
256 Vector3 invDelta =
new Vector3(1f/DELTA.x, 1f/DELTA.y, 1f/DELTA.z);
257 for(
int i=0; i<vertices.Length; i++) {
259 vertices[i] = Vector3.Scale(DELTA, vertices[i]) +
OFFSET_ORIGIN;
261 vertices[i] = Vector3.Scale(invDelta, (vertices[i] - FUDGE_FACTOR)) +
OFFSET_ORIGIN;
300 foreach(Mesh mesh
in meshes) {
301 GameObject surface =
new GameObject(
"SurfaceOBJ");
310 surface.transform.parent =
DADDY;
311 surface.transform.localPosition =
CENTER;
313 mesh.RecalculateBounds();
314 surface.AddComponent<MeshFilter>();
315 surface.AddComponent<MeshRenderer>();
316 surface.GetComponent<MeshFilter>().mesh = mesh;
320 surface.GetComponent<Renderer>().material =
new Material(Shader.Find(
"Diffuse"));
324 surface.GetComponent<Renderer>().material =
new Material(Shader.Find(
"Mat Cap Cut"));
325 surface.GetComponent<Renderer>().material.SetTexture(
"_MatCap",(Texture)Resources.Load(
"lit_spheres/divers/daphz1"));
327 Debug.Log(surface.GetComponent<Renderer>().material.shader.name);
332 XDIM = VOXELS.GetLength(0);
333 YDIM = VOXELS.GetLength(1);
334 ZDIM = VOXELS.GetLength(2);
338 Debug.Log(SLICE_WIDTH.ToString());
339 Debug.Log(
"GenerateMesh::CreateSurfaceObjects > Voxel grid dimensions (x,y,z):");
340 Debug.Log(XDIM.ToString());
341 Debug.Log(YDIM.ToString());
342 Debug.Log(ZDIM.ToString());
345 private static void InitGenMesh(
float[,,] vox,
float thresh, Vector3 d, Vector3 o) {
383 Color[] colors,
int isPos,
bool electro =
false) {
385 Debug.Log(ELECTRO.ToString());
395 float bMCTime = Time.realtimeSinceStartup;
397 Debug.Log(
"Entire surface contains " + mData.
vertices.Length.ToString() +
" vertices.");
398 float elapsed = 10f * (Time.realtimeSinceStartup - bMCTime);
399 Debug.Log(
"GenerateMesh::MarchingCubes time: " + elapsed.ToString());
402 float bSmooth = Time.realtimeSinceStartup;
406 elapsed = Time.realtimeSinceStartup - bSmooth;
407 Debug.Log(
"Smoothing time: " + elapsed.ToString());
412 Debug.Log(threshold.ToString());
417 List<Mesh> meshes = splitting.
Split(mData);
422 public void GM (Vector3[] Mvertices,
int[] Mtriangles, Vector3 center,
int surfaceNb, Color[] Colors) {
423 GameObject GOSurface =
new GameObject(
"SurfaceOBJ");
426 Mesh mesh =
new Mesh();
427 GOSurface.AddComponent<MeshFilter>();
428 GOSurface.AddComponent<MeshRenderer>();
429 GOSurface.GetComponent<MeshFilter>().mesh = mesh;
434 GOSurface.GetComponent<Renderer>().material =
new Material(Shader.Find(
"Mat Cap Cut"));
435 GOSurface.GetComponent<Renderer>().material.SetTexture(
"_MatCap",(Texture)Resources.Load(
"lit_spheres/divers/daphz1"));
442 GOSurface.transform.localPosition = center;
458 mesh.vertices = Mvertices;
460 Debug.Log(
"Exiting :: vertices filled");
461 mesh.triangles = Mtriangles;
462 mesh.colors = Colors;
463 mesh.RecalculateBounds();
465 mesh.RecalculateNormals();
static void CreateSurfaceObjects(List< Mesh > meshes, int isPos)
static void OffsetVertices(MeshData mData)
static void SetModeToCubes()
static Mesh CalculateMeshTangents(Mesh mesh)
static float S_FUDGE_FACTOR
void AddAllTriangles(int[] triangles)
static void FlipTriangles(MeshData mData)
static Vector3[] vertices
void addSurfaceNeg(GameObject surface)
List< Mesh > Split(MeshData mData)
static Vector3 FUDGE_FACTOR
static Mesh GenerateTextureCoordinates(Mesh mesh)
static void AdjSetsSmoother(MeshData mData, AdjacencySets adjacencySets)
static MeshData CreateMesh(float[,,] voxels, int xStart, int xEnd, int yStart, int yEnd, int zStart, int zEnd)
static void FlipNormals(MeshData mData)
void GM(Vector3[] Mvertices, int[] Mtriangles, Vector3 center, int surfaceNb, Color[] Colors)
static void ProperlyCalculateNormals(MeshData mData)
This computes the normal vectors from a MeshData object, and adds them to it.
static void SetTarget(float tar)
static void InitGenMesh(float[,,] vox, float thresh, Vector3 d, Vector3 o)
GameObject getParentGameObject()
static void CreateSurfaceObjects(float[,,] voxels, float threshold, Vector3 delta, Vector3 origin, Color[] colors, int isPos, bool electro=false)
Creates the surface objects.
static SurfaceManager getSurfaceManager()
void addSurfacePos(GameObject surface)
static Vector3 OFFSET_ORIGIN
void addSurface(GameObject surface)