| 1 | /************************************************************************************ |
| 2 | Filename : ONSPPropagationGeometry.cs |
| 3 | Content : Geometry Functions |
| 4 | Attach to a game object with meshes and material scripts to create geometry |
| 5 | NOTE: ensure that Oculus Spatialization is enabled for AudioSource components |
| 6 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 7 | |
| 8 | Licensed under the Oculus SDK Version 3.5 (the "License"); |
| 9 | you may not use the Oculus SDK except in compliance with the License, |
| 10 | which is provided at the time of installation or download, or which |
| 11 | otherwise accompanies this software in either electronic or hard copy form. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | |
| 15 | https://developer.oculus.com/licenses/sdk-3.5/ |
| 16 | |
| 17 | Unless required by applicable law or agreed to in writing, the Oculus SDK |
| 18 | distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | See the License for the specific language governing permissions and |
| 21 | limitations under the License. |
| 22 | ************************************************************************************/ |
| 23 | #define INCLUDE_TERRAIN_TREES |
| 24 | |
| 25 | using UnityEngine; |
| 26 | using System; |
| 27 | using System.Collections.Generic; |
| 28 | using Oculus.Spatializer.Propagation; |
| 29 | |
| 30 | public class ONSPPropagationGeometry : MonoBehaviour |
| 31 | { |
| 32 | public static string GeometryAssetDirectory = "AudioGeometry"; |
| 33 | public static string GeometryAssetPath { get { return Application.streamingAssetsPath + "/" + GeometryAssetDirectory; } } |
| 34 | //------- |
| 35 | // PUBLIC |
| 36 | |
| 37 | /// The path to the serialized mesh file that holds the preprocessed mesh geometry. |
| 38 | public string filePathRelative = ""; |
| 39 | public string filePath { get { return GeometryAssetPath + "/" + filePathRelative; } } |
| 40 | public bool fileEnabled = false; |
| 41 | |
| 42 | public bool includeChildMeshes = true; |
| 43 | |
| 44 | //------- |
| 45 | // PRIVATE |
| 46 | private IntPtr geometryHandle = IntPtr.Zero; |
| 47 | |
| 48 | //------- |
| 49 | // PUBLIC STATIC |
| 50 | public static int OSPSuccess = 0; |
| 51 | |
| 52 | public const string GEOMETRY_FILE_EXTENSION = "ovramesh"; |
| 53 | |
| 54 | private static string GetPath(Transform current) |
| 55 | { |
| 56 | if (current.parent == null) |
| 57 | return current.gameObject.scene.name + "/" + current.name; |
| 58 | return GetPath(current.parent) + "-" + current.name; |
| 59 | } |
| 60 | |
| 61 | /// <summary> |
| 62 | /// If script is attached to a gameobject, it will try to create geometry |
| 63 | /// </summary> |
| 64 | void Awake() |
| 65 | { |
| 66 | CreatePropagationGeometry(); |
| 67 | } |
| 68 | |
| 69 | /// <summary> |
| 70 | /// Call this function to create geometry handle |
| 71 | /// </summary> |
| 72 | void CreatePropagationGeometry() |
| 73 | { |
| 74 | // Create Geometry |
| 75 | if (ONSPPropagation.Interface.CreateAudioGeometry(out geometryHandle) != OSPSuccess) |
| 76 | { |
| 77 | throw new Exception("Unable to create geometry handle"); |
| 78 | } |
| 79 | |
| 80 | // Upload Mesh |
| 81 | if (filePath != null && filePath.Length != 0 && fileEnabled && Application.isPlaying) |
| 82 | { |
| 83 | if (!ReadFile()) |
| 84 | { |
| 85 | Debug.LogError("Failed to read file, attempting to regenerate audio geometry"); |
| 86 | |
| 87 | // We should not try to upload data dynamically if data already exists |
| 88 | UploadGeometry(); |
| 89 | } |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | UploadGeometry(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// <summary> |
| 98 | /// Update the world transform (TODO) |
| 99 | /// </summary> |
| 100 | private void Update() |
| 101 | { |
| 102 | if (geometryHandle == IntPtr.Zero) |
| 103 | return; |
| 104 | |
| 105 | Matrix4x4 m = transform.localToWorldMatrix; |
| 106 | // Note: flip Z to convert from left-handed (+Z forward) to right-handed (+Z backward) |
| 107 | float[] matrix = { m[0,0], m[1,0], -m[2,0], m[3,0], |
| 108 | m[0,1], m[1,1], -m[2,1], m[3,1], |
| 109 | m[0,2], m[1,2], -m[2,2], m[3,2], |
| 110 | m[0,3], m[1,3], -m[2,3], m[3,3] }; |
| 111 | |
| 112 | ONSPPropagation.Interface.AudioGeometrySetTransform(geometryHandle, matrix); |
| 113 | } |
| 114 | |
| 115 | /// <summary> |
| 116 | /// Call when destroyed |
| 117 | /// </summary> |
| 118 | private void OnDestroy() |
| 119 | { |
| 120 | // DESTROY GEOMETRY |
| 121 | if (geometryHandle != IntPtr.Zero && ONSPPropagation.Interface.DestroyAudioGeometry(geometryHandle) != OSPSuccess) |
| 122 | { |
| 123 | throw new Exception("Unable to destroy geometry"); |
| 124 | } |
| 125 | |
| 126 | geometryHandle = IntPtr.Zero; |
| 127 | } |
| 128 | |
| 129 | // |
| 130 | // FUNCTIONS FOR UPLOADING MESHES VIA GAME OBJECT |
| 131 | // |
| 132 | |
| 133 | static int terrainDecimation = 4; |
| 134 | |
| 135 | private struct MeshMaterial |
| 136 | { |
| 137 | public MeshFilter meshFilter; |
| 138 | public ONSPPropagationMaterial[] materials; |
| 139 | } |
| 140 | |
| 141 | private struct TerrainMaterial |
| 142 | { |
| 143 | public Terrain terrain; |
| 144 | public ONSPPropagationMaterial[] materials; |
| 145 | public Mesh[] treePrototypeMeshes; |
| 146 | } |
| 147 | |
| 148 | private static void traverseMeshHierarchy(GameObject obj, ONSPPropagationMaterial[] currentMaterials, bool includeChildren, |
| 149 | List<MeshMaterial> meshMaterials, List<TerrainMaterial> terrainMaterials, bool ignoreStatic, ref int ignoredMeshCount) |
| 150 | { |
| 151 | if (!obj.activeInHierarchy) |
| 152 | return; |
| 153 | |
| 154 | MeshFilter[] meshes = obj.GetComponents<MeshFilter>(); |
| 155 | Terrain[] terrains = obj.GetComponents<Terrain>(); |
| 156 | ONSPPropagationMaterial[] materials = obj.GetComponents<ONSPPropagationMaterial>(); |
| 157 | |
| 158 | // Initialize the current material array to a new array if there are any new materials. |
| 159 | if (materials != null && materials.Length > 0) |
| 160 | { |
| 161 | // Determine the length of the material array. |
| 162 | int maxLength = materials.Length; |
| 163 | if (currentMaterials != null) |
| 164 | maxLength = Math.Max(maxLength, currentMaterials.Length); |
| 165 | |
| 166 | ONSPPropagationMaterial[] newMaterials = new ONSPPropagationMaterial[maxLength]; |
| 167 | |
| 168 | // Copy the previous materials into the new array. |
| 169 | if (currentMaterials != null) |
| 170 | { |
| 171 | for (int i = materials.Length; i < maxLength; i++) |
| 172 | newMaterials[i] = currentMaterials[i]; |
| 173 | } |
| 174 | currentMaterials = newMaterials; |
| 175 | |
| 176 | // Copy the current materials. |
| 177 | for (int i = 0; i < materials.Length; i++) |
| 178 | currentMaterials[i] = materials[i]; |
| 179 | } |
| 180 | |
| 181 | // Gather the meshes. |
| 182 | foreach (MeshFilter meshFilter in meshes) |
| 183 | { |
| 184 | Mesh mesh = meshFilter.sharedMesh; |
| 185 | if (mesh == null) |
| 186 | continue; |
| 187 | |
| 188 | if (ignoreStatic && !mesh.isReadable) |
| 189 | { |
| 190 | Debug.LogWarning("Mesh: " + meshFilter.gameObject.name + " not readable, cannot be static.", meshFilter.gameObject); |
| 191 | ++ignoredMeshCount; |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | MeshMaterial m = new MeshMaterial(); |
| 196 | m.meshFilter = meshFilter; |
| 197 | m.materials = currentMaterials; |
| 198 | meshMaterials.Add(m); |
| 199 | } |
| 200 | |
| 201 | // Gather the terrains. |
| 202 | foreach (Terrain terrain in terrains) |
| 203 | { |
| 204 | TerrainMaterial m = new TerrainMaterial(); |
| 205 | m.terrain = terrain; |
| 206 | m.materials = currentMaterials; |
| 207 | terrainMaterials.Add(m); |
| 208 | } |
| 209 | |
| 210 | // Traverse to the child objects. |
| 211 | if (includeChildren) |
| 212 | { |
| 213 | foreach (Transform child in obj.transform) |
| 214 | { |
| 215 | if (child.GetComponent<ONSPPropagationGeometry>() == null) // skip children which have their own component |
| 216 | traverseMeshHierarchy(child.gameObject, currentMaterials, includeChildren, meshMaterials, terrainMaterials, ignoreStatic, ref ignoredMeshCount); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // |
| 222 | // CALL THIS ON GAME OBJECT THAT HAS GEOMETRY ATTACHED TO IT |
| 223 | // |
| 224 | private int uploadMesh(IntPtr geometryHandle, GameObject meshObject, Matrix4x4 worldToLocal) |
| 225 | { |
| 226 | int unused = 0; |
| 227 | return uploadMesh(geometryHandle, meshObject, worldToLocal, false, ref unused); |
| 228 | } |
| 229 | |
| 230 | private int uploadMesh(IntPtr geometryHandle, GameObject meshObject, Matrix4x4 worldToLocal, bool ignoreStatic, ref int ignoredMeshCount) |
| 231 | { |
| 232 | // Get the child mesh objects. |
| 233 | List<MeshMaterial> meshes = new List<MeshMaterial>(); |
| 234 | List<TerrainMaterial> terrains = new List<TerrainMaterial>(); |
| 235 | traverseMeshHierarchy(meshObject, null, includeChildMeshes, meshes, terrains, ignoreStatic, ref ignoredMeshCount); |
| 236 | |
| 237 | #if INCLUDE_TERRAIN_TREES |
| 238 | // TODO: expose tree material |
| 239 | ONSPPropagationMaterial[] treeMaterials = new ONSPPropagationMaterial[1]; |
| 240 | treeMaterials[0] = gameObject.AddComponent<ONSPPropagationMaterial>(); |
| 241 | |
| 242 | #if true |
| 243 | treeMaterials[0].SetPreset(ONSPPropagationMaterial.Preset.Foliage); |
| 244 | #else |
| 245 | // Custom material that is highly transmissive |
| 246 | treeMaterials[0].absorption.points = new List<ONSPPropagationMaterial.Point>{ |
| 247 | new ONSPPropagationMaterial.Point(125f, .03f), |
| 248 | new ONSPPropagationMaterial.Point(250f, .06f), |
| 249 | new ONSPPropagationMaterial.Point(500f, .11f), |
| 250 | new ONSPPropagationMaterial.Point(1000f, .17f), |
| 251 | new ONSPPropagationMaterial.Point(2000f, .27f), |
| 252 | new ONSPPropagationMaterial.Point(4000f, .31f) }; |
| 253 | |
| 254 | treeMaterials[0].scattering.points = new List<ONSPPropagationMaterial.Point>{ |
| 255 | new ONSPPropagationMaterial.Point(125f, .20f), |
| 256 | new ONSPPropagationMaterial.Point(250f, .3f), |
| 257 | new ONSPPropagationMaterial.Point(500f, .4f), |
| 258 | new ONSPPropagationMaterial.Point(1000f, .5f), |
| 259 | new ONSPPropagationMaterial.Point(2000f, .7f), |
| 260 | new ONSPPropagationMaterial.Point(4000f, .8f) }; |
| 261 | |
| 262 | treeMaterials[0].transmission.points = new List<ONSPPropagationMaterial.Point>(){ |
| 263 | new ONSPPropagationMaterial.Point(125f, .95f), |
| 264 | new ONSPPropagationMaterial.Point(250f, .92f), |
| 265 | new ONSPPropagationMaterial.Point(500f, .87f), |
| 266 | new ONSPPropagationMaterial.Point(1000f, .81f), |
| 267 | new ONSPPropagationMaterial.Point(2000f, .71f), |
| 268 | new ONSPPropagationMaterial.Point(4000f, .67f) }; |
| 269 | #endif |
| 270 | |
| 271 | #endif |
| 272 | |
| 273 | //*********************************************************************** |
| 274 | // Count the number of vertices and indices. |
| 275 | |
| 276 | int totalVertexCount = 0; |
| 277 | uint totalIndexCount = 0; |
| 278 | int totalFaceCount = 0; |
| 279 | int totalMaterialCount = 0; |
| 280 | |
| 281 | foreach (MeshMaterial m in meshes) |
| 282 | { |
| 283 | updateCountsForMesh(ref totalVertexCount, ref totalIndexCount, ref totalFaceCount, ref totalMaterialCount, m.meshFilter.sharedMesh); |
| 284 | } |
| 285 | |
| 286 | for (int i = 0; i < terrains.Count; ++i) |
| 287 | { |
| 288 | TerrainMaterial t = terrains[i]; |
| 289 | TerrainData terrain = t.terrain.terrainData; |
| 290 | |
| 291 | int w = terrain.heightmapWidth; |
| 292 | int h = terrain.heightmapHeight; |
| 293 | int wRes = (w - 1) / terrainDecimation + 1; |
| 294 | int hRes = (h - 1) / terrainDecimation + 1; |
| 295 | int vertexCount = wRes * hRes; |
| 296 | int indexCount = (wRes - 1) * (hRes - 1) * 6; |
| 297 | |
| 298 | totalMaterialCount++; |
| 299 | totalVertexCount += vertexCount; |
| 300 | totalIndexCount += (uint)indexCount; |
| 301 | totalFaceCount += indexCount / 3; |
| 302 | |
| 303 | #if INCLUDE_TERRAIN_TREES |
| 304 | TreePrototype[] treePrototypes = terrain.treePrototypes; |
| 305 | t.treePrototypeMeshes = new Mesh[treePrototypes.Length]; |
| 306 | |
| 307 | // assume the sharedMesh with the lowest vertex is the lowest LOD |
| 308 | for (int j = 0; j < treePrototypes.Length; ++j) |
| 309 | { |
| 310 | GameObject prefab = treePrototypes[j].prefab; |
| 311 | MeshFilter[] meshFilters = prefab.GetComponentsInChildren<MeshFilter>(); |
| 312 | int minVertexCount = int.MaxValue; |
| 313 | int index = -1; |
| 314 | for (int k = 0; k < meshFilters.Length; ++k) |
| 315 | { |
| 316 | int count = meshFilters[k].sharedMesh.vertexCount; |
| 317 | if (count < minVertexCount) |
| 318 | { |
| 319 | minVertexCount = count; |
| 320 | index = k; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | t.treePrototypeMeshes[j] = meshFilters[index].sharedMesh; |
| 325 | } |
| 326 | |
| 327 | TreeInstance[] trees = terrain.treeInstances; |
| 328 | foreach (TreeInstance tree in trees) |
| 329 | { |
| 330 | updateCountsForMesh(ref totalVertexCount, ref totalIndexCount, ref totalFaceCount, ref totalMaterialCount, t.treePrototypeMeshes[tree.prototypeIndex]); |
| 331 | } |
| 332 | |
| 333 | terrains[i] = t; |
| 334 | #endif |
| 335 | } |
| 336 | |
| 337 | //*********************************************************************** |
| 338 | // Copy the mesh data. |
| 339 | |
| 340 | List<Vector3> tempVertices = new List<Vector3>(); |
| 341 | List<int> tempIndices = new List<int>(); |
| 342 | |
| 343 | MeshGroup[] groups = new MeshGroup[totalMaterialCount]; |
| 344 | float[] vertices = new float[totalVertexCount * 3]; |
| 345 | int[] indices = new int[totalIndexCount]; |
| 346 | |
| 347 | int vertexOffset = 0; |
| 348 | int indexOffset = 0; |
| 349 | int groupOffset = 0; |
| 350 | |
| 351 | foreach (MeshMaterial m in meshes) |
| 352 | { |
| 353 | MeshFilter meshFilter = m.meshFilter; |
| 354 | |
| 355 | // Compute the combined transform to go from mesh-local to geometry-local space. |
| 356 | Matrix4x4 matrix = worldToLocal * meshFilter.gameObject.transform.localToWorldMatrix; |
| 357 | |
| 358 | uploadMeshFilter(tempVertices, tempIndices, groups, vertices, indices, ref vertexOffset, ref indexOffset, ref groupOffset, meshFilter.sharedMesh, m.materials, matrix); |
| 359 | } |
| 360 | |
| 361 | foreach (TerrainMaterial t in terrains) |
| 362 | { |
| 363 | TerrainData terrain = t.terrain.terrainData; |
| 364 | |
| 365 | // Compute the combined transform to go from mesh-local to geometry-local space. |
| 366 | Matrix4x4 matrix = worldToLocal * t.terrain.gameObject.transform.localToWorldMatrix; |
| 367 | |
| 368 | int w = terrain.heightmapWidth; |
| 369 | int h = terrain.heightmapHeight; |
| 370 | float[,] tData = terrain.GetHeights(0, 0, w, h); |
| 371 | |
| 372 | Vector3 meshScale = terrain.size; |
| 373 | meshScale = new Vector3(meshScale.x / (w - 1) * terrainDecimation, meshScale.y, meshScale.z / (h - 1) * terrainDecimation); |
| 374 | int wRes = (w - 1) / terrainDecimation + 1; |
| 375 | int hRes = (h - 1) / terrainDecimation + 1; |
| 376 | int vertexCount = wRes * hRes; |
| 377 | int triangleCount = (wRes - 1) * (hRes - 1) * 2; |
| 378 | |
| 379 | // Initialize the group. |
| 380 | groups[groupOffset].faceType = FaceType.TRIANGLES; |
| 381 | groups[groupOffset].faceCount = (UIntPtr)triangleCount; |
| 382 | groups[groupOffset].indexOffset = (UIntPtr)indexOffset; |
| 383 | |
| 384 | if (t.materials != null && 0 < t.materials.Length) |
| 385 | { |
| 386 | t.materials[0].StartInternal(); |
| 387 | groups[groupOffset].material = t.materials[0].materialHandle; |
| 388 | } |
| 389 | else |
| 390 | groups[groupOffset].material = IntPtr.Zero; |
| 391 | |
| 392 | // Build vertices and UVs |
| 393 | for (int y = 0; y < hRes; y++) |
| 394 | { |
| 395 | for (int x = 0; x < wRes; x++) |
| 396 | { |
| 397 | int offset = (vertexOffset + y * wRes + x) * 3; |
| 398 | Vector3 v = matrix.MultiplyPoint3x4(Vector3.Scale(meshScale, new Vector3(y, tData[x * terrainDecimation, y * terrainDecimation], x))); |
| 399 | vertices[offset + 0] = v.x; |
| 400 | vertices[offset + 1] = v.y; |
| 401 | vertices[offset + 2] = v.z; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Build triangle indices: 3 indices into vertex array for each triangle |
| 406 | for (int y = 0; y < hRes - 1; y++) |
| 407 | { |
| 408 | for (int x = 0; x < wRes - 1; x++) |
| 409 | { |
| 410 | // For each grid cell output two triangles |
| 411 | indices[indexOffset + 0] = (vertexOffset + (y * wRes) + x); |
| 412 | indices[indexOffset + 1] = (vertexOffset + ((y + 1) * wRes) + x); |
| 413 | indices[indexOffset + 2] = (vertexOffset + (y * wRes) + x + 1); |
| 414 | |
| 415 | indices[indexOffset + 3] = (vertexOffset + ((y + 1) * wRes) + x); |
| 416 | indices[indexOffset + 4] = (vertexOffset + ((y + 1) * wRes) + x + 1); |
| 417 | indices[indexOffset + 5] = (vertexOffset + (y * wRes) + x + 1); |
| 418 | indexOffset += 6; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | vertexOffset += vertexCount; |
| 423 | groupOffset++; |
| 424 | |
| 425 | #if INCLUDE_TERRAIN_TREES |
| 426 | TreeInstance[] trees = terrain.treeInstances; |
| 427 | foreach (TreeInstance tree in trees) |
| 428 | { |
| 429 | Vector3 pos = Vector3.Scale(tree.position, terrain.size); |
| 430 | Matrix4x4 treeLocalToWorldMatrix = t.terrain.gameObject.transform.localToWorldMatrix; |
| 431 | treeLocalToWorldMatrix.SetColumn(3, treeLocalToWorldMatrix.GetColumn(3) + new Vector4(pos.x, pos.y, pos.z, 0.0f)); |
| 432 | // TODO: tree rotation |
| 433 | Matrix4x4 treeMatrix = worldToLocal * treeLocalToWorldMatrix; |
| 434 | uploadMeshFilter(tempVertices, tempIndices, groups, vertices, indices, ref vertexOffset, ref indexOffset, ref groupOffset, t.treePrototypeMeshes[tree.prototypeIndex], treeMaterials, treeMatrix); |
| 435 | } |
| 436 | #endif |
| 437 | } |
| 438 | |
| 439 | // Upload mesh data |
| 440 | return ONSPPropagation.Interface.AudioGeometryUploadMeshArrays(geometryHandle, |
| 441 | vertices, totalVertexCount, |
| 442 | indices, indices.Length, |
| 443 | groups, groups.Length); |
| 444 | } |
| 445 | |
| 446 | private static void uploadMeshFilter(List<Vector3> tempVertices, List<int> tempIndices, MeshGroup[] groups, float[] vertices, int[] indices, |
| 447 | ref int vertexOffset, ref int indexOffset, ref int groupOffset, Mesh mesh, ONSPPropagationMaterial[] materials, Matrix4x4 matrix) |
| 448 | { |
| 449 | // Get the mesh vertices. |
| 450 | tempVertices.Clear(); |
| 451 | mesh.GetVertices(tempVertices); |
| 452 | |
| 453 | // Copy the Vector3 vertices into a packed array of floats for the API. |
| 454 | int meshVertexCount = tempVertices.Count; |
| 455 | for (int i = 0; i < meshVertexCount; i++) |
| 456 | { |
| 457 | // Transform into the parent space. |
| 458 | Vector3 v = matrix.MultiplyPoint3x4(tempVertices[i]); |
| 459 | int offset = (vertexOffset + i) * 3; |
| 460 | vertices[offset + 0] = v.x; |
| 461 | vertices[offset + 1] = v.y; |
| 462 | vertices[offset + 2] = v.z; |
| 463 | } |
| 464 | |
| 465 | // Copy the data for each submesh. |
| 466 | for (int i = 0; i < mesh.subMeshCount; i++) |
| 467 | { |
| 468 | MeshTopology topology = mesh.GetTopology(i); |
| 469 | |
| 470 | if (topology == MeshTopology.Triangles || topology == MeshTopology.Quads) |
| 471 | { |
| 472 | // Get the submesh indices. |
| 473 | tempIndices.Clear(); |
| 474 | mesh.GetIndices(tempIndices, i); |
| 475 | int subMeshIndexCount = tempIndices.Count; |
| 476 | |
| 477 | // Copy and adjust the indices. |
| 478 | for (int j = 0; j < subMeshIndexCount; j++) |
| 479 | indices[indexOffset + j] = tempIndices[j] + vertexOffset; |
| 480 | |
| 481 | // Initialize the group. |
| 482 | if (topology == MeshTopology.Triangles) |
| 483 | { |
| 484 | groups[groupOffset + i].faceType = FaceType.TRIANGLES; |
| 485 | groups[groupOffset + i].faceCount = (UIntPtr)(subMeshIndexCount / 3); |
| 486 | } |
| 487 | else if (topology == MeshTopology.Quads) |
| 488 | { |
| 489 | groups[groupOffset + i].faceType = FaceType.QUADS; |
| 490 | groups[groupOffset + i].faceCount = (UIntPtr)(subMeshIndexCount / 4); |
| 491 | } |
| 492 | |
| 493 | groups[groupOffset + i].indexOffset = (UIntPtr)indexOffset; |
| 494 | |
| 495 | if (materials != null && materials.Length != 0) |
| 496 | { |
| 497 | int matIndex = i; |
| 498 | if (matIndex >= materials.Length) |
| 499 | matIndex = materials.Length - 1; |
| 500 | materials[matIndex].StartInternal(); |
| 501 | groups[groupOffset + i].material = materials[matIndex].materialHandle; |
| 502 | } |
| 503 | else |
| 504 | groups[groupOffset + i].material = IntPtr.Zero; |
| 505 | |
| 506 | indexOffset += subMeshIndexCount; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | vertexOffset += meshVertexCount; |
| 511 | groupOffset += mesh.subMeshCount; |
| 512 | } |
| 513 | |
| 514 | private static void updateCountsForMesh(ref int totalVertexCount, ref uint totalIndexCount, ref int totalFaceCount, ref int totalMaterialCount, Mesh mesh) |
| 515 | { |
| 516 | totalMaterialCount += mesh.subMeshCount; |
| 517 | totalVertexCount += mesh.vertexCount; |
| 518 | |
| 519 | for (int i = 0; i < mesh.subMeshCount; i++) |
| 520 | { |
| 521 | MeshTopology topology = mesh.GetTopology(i); |
| 522 | if (topology == MeshTopology.Triangles || topology == MeshTopology.Quads) |
| 523 | { |
| 524 | uint meshIndexCount = mesh.GetIndexCount(i); |
| 525 | totalIndexCount += meshIndexCount; |
| 526 | |
| 527 | if (topology == MeshTopology.Triangles) |
| 528 | totalFaceCount += (int)meshIndexCount / 3; |
| 529 | else if (topology == MeshTopology.Quads) |
| 530 | totalFaceCount += (int)meshIndexCount / 4; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | //*********************************************************************** |
| 536 | // UploadGeometry |
| 537 | |
| 538 | public void UploadGeometry() |
| 539 | { |
| 540 | int ignoredMeshCount = 0; |
| 541 | if (uploadMesh(geometryHandle, gameObject, gameObject.transform.worldToLocalMatrix, true, ref ignoredMeshCount) != OSPSuccess) |
| 542 | throw new Exception("Unable to upload audio mesh geometry"); |
| 543 | |
| 544 | if (ignoredMeshCount != 0) |
| 545 | { |
| 546 | Debug.LogError("Failed to upload meshes, " + ignoredMeshCount + " static meshes ignored. Turn on \"File Enabled\" to process static meshes offline", gameObject); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | #if UNITY_EDITOR |
| 551 | //*********************************************************************** |
| 552 | // WriteFile - Write the serialized mesh file. |
| 553 | |
| 554 | public bool WriteFile() |
| 555 | { |
| 556 | if (filePathRelative == "") |
| 557 | { |
| 558 | filePathRelative = GetPath(transform); |
| 559 | string modifier = ""; |
| 560 | int counter = 0; |
| 561 | while (System.IO.File.Exists(filePath + modifier)) |
| 562 | { |
| 563 | modifier = "-" + counter; |
| 564 | ++counter; |
| 565 | |
| 566 | if (counter > 10000) |
| 567 | { |
| 568 | // sanity check to prevent hang |
| 569 | throw new Exception("Unable to find sutiable file name"); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | filePathRelative = filePathRelative + modifier; |
| 574 | Debug.Log("No file path specified, autogenerated: " + filePathRelative); |
| 575 | } |
| 576 | |
| 577 | // Create the directory |
| 578 | string directoryName = filePathRelative.Substring(0, filePathRelative.LastIndexOf('/')); |
| 579 | System.IO.Directory.CreateDirectory(GeometryAssetPath + "/" + directoryName); |
| 580 | |
| 581 | // Create a temporary geometry. |
| 582 | IntPtr tempGeometryHandle = IntPtr.Zero; |
| 583 | if (ONSPPropagation.Interface.CreateAudioGeometry(out tempGeometryHandle) != OSPSuccess) |
| 584 | { |
| 585 | throw new Exception("Failed to create temp geometry handle"); |
| 586 | } |
| 587 | |
| 588 | // Upload the mesh geometry. |
| 589 | if (uploadMesh(tempGeometryHandle, gameObject, gameObject.transform.worldToLocalMatrix) != OSPSuccess) |
| 590 | { |
| 591 | Debug.LogError("Error uploading mesh " + gameObject.name); |
| 592 | return false; |
| 593 | } |
| 594 | |
| 595 | // Write the mesh to a file. |
| 596 | if (ONSPPropagation.Interface.AudioGeometryWriteMeshFile(tempGeometryHandle, filePath) != OSPSuccess) |
| 597 | { |
| 598 | Debug.LogError("Error writing mesh file " + filePath); |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | // Destroy the geometry. |
| 603 | if (ONSPPropagation.Interface.DestroyAudioGeometry(tempGeometryHandle) != OSPSuccess) |
| 604 | { |
| 605 | throw new Exception("Failed to destroy temp geometry handle"); |
| 606 | } |
| 607 | |
| 608 | return true; |
| 609 | } |
| 610 | #endif |
| 611 | |
| 612 | //*********************************************************************** |
| 613 | // ReadFile - Read the serialized mesh file. |
| 614 | |
| 615 | public bool ReadFile() |
| 616 | { |
| 617 | if (filePath == null || filePath.Length == 0) |
| 618 | { |
| 619 | Debug.LogError("Invalid mesh file path"); |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | if (ONSPPropagation.Interface.AudioGeometryReadMeshFile(geometryHandle, filePath) != OSPSuccess) |
| 624 | { |
| 625 | Debug.LogError("Error reading mesh file " + filePath); |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | return true; |
| 630 | } |
| 631 | |
| 632 | public bool WriteToObj() |
| 633 | { |
| 634 | // Create a temporary geometry. |
| 635 | IntPtr tempGeometryHandle = IntPtr.Zero; |
| 636 | if (ONSPPropagation.Interface.CreateAudioGeometry(out tempGeometryHandle) != OSPSuccess) |
| 637 | { |
| 638 | throw new Exception("Failed to create temp geometry handle"); |
| 639 | } |
| 640 | |
| 641 | // Upload the mesh geometry. |
| 642 | if (uploadMesh(tempGeometryHandle, gameObject, gameObject.transform.worldToLocalMatrix) != OSPSuccess) |
| 643 | { |
| 644 | Debug.LogError("Error uploading mesh " + gameObject.name); |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | // Write the mesh to a .obj file. |
| 649 | if (ONSPPropagation.Interface.AudioGeometryWriteMeshFileObj(tempGeometryHandle, filePath + ".obj") != OSPSuccess) |
| 650 | { |
| 651 | Debug.LogError("Error writing .obj file " + filePath + ".obj"); |
| 652 | return false; |
| 653 | } |
| 654 | |
| 655 | // Destroy the geometry. |
| 656 | if (ONSPPropagation.Interface.DestroyAudioGeometry(tempGeometryHandle) != OSPSuccess) |
| 657 | { |
| 658 | throw new Exception("Failed to destroy temp geometry handle"); |
| 659 | } |
| 660 | |
| 661 | return true; |
| 662 | } |
| 663 | } |
| 664 | |