public Texture2D ToUnity(Utils.Progress progress = null) { if (unityTexture == null) { if (data != null) { unityTexture = new Texture2D(width, height, format, false); unityTexture.LoadRawTextureData(data); unityTexture.name = filename; unityTexture.Apply(); } else { unityTexture = new Texture2D(1, 1); unityTexture.name = "ERROR"; unityTexture.Apply(); } if (progress != null) { progress.Update(1); } } return(unityTexture); }
public UnityEngine.Material ToUnity(Scene scene, Utils.Progress progress = null) { if (unityMaterial == null) { // Get the material shader Shader unity_shader = GetUnityShader(shader); if (unity_shader != null) { // Create a new material with the selected shader unityMaterial = new UnityEngine.Material(unity_shader); // Set material options unityMaterial.name = name; unityMaterial.renderQueue = renderQueue; unityMaterial.hideFlags = hideFlags; unityMaterial.globalIlluminationFlags = globalIlluminationFlags; unityMaterial.shaderKeywords = keywords; // Activate required shader passes if (passes != null) { for (int i = 0; i < passes.Length; i++) { #if UNITY_5_6_OR_NEWER unityMaterial.SetShaderPassEnabled(unityMaterial.GetPassName(i), passes[i]); #endif } } // Set material properties ToUnityMaterialProperties <int, int>(unityMaterial, ints, (m, p, v) => m.SetInt(p, v)); ToUnityMaterialProperties <float, float>(unityMaterial, floats, (m, p, v) => m.SetFloat(p, v)); ToUnityMaterialProperties <Vector3, Vector3>(unityMaterial, vectors, (m, p, v) => m.SetVector(p, v)); ToUnityMaterialProperties <Color, Color>(unityMaterial, colors, (m, p, v) => m.SetColor(p, v)); ToUnityMaterialProperties <TextureParams, UnityEngine.Texture>(unityMaterial, textures, (m, p, v) => { m.SetTexture(p, scene.textures[v.index].ToUnity(progress)); m.SetTextureOffset(p, v.offset); m.SetTextureScale(p, v.scale); }); // Set materials missing properties / keywords if material is imported from assimp if ((keywords == null || keywords.Length <= 0) && shader == Constants.defaultAssimpShader) { CLARTE.Shaders.Standard.Utility.MaterialChanged(unityMaterial); } } if (progress != null) { progress.Update(1); } } return(unityMaterial); }
public UnityEngine.Mesh ToUnity(Utils.Progress progress = null) { if (unityMesh == null) { unityMesh = new UnityEngine.Mesh(); if (name != null) { unityMesh.name = name; } if (vertices != null) { unityMesh.vertices = vertices; } if (normals != null) { unityMesh.normals = normals; } if (tangents != null) { unityMesh.tangents = tangents; } if (uv1 != null) { unityMesh.uv = uv1; } if (uv2 != null) { unityMesh.uv2 = uv2; } if (colors != null) { unityMesh.colors = colors; } if (submeshes != null) { int nb_submeshes = submeshes.Length; unityMesh.subMeshCount = nb_submeshes; for (int i = 0; i < nb_submeshes; i++) { SubMesh submesh = submeshes[i]; unityMesh.SetIndices(submesh.triangles, submesh.topology, i); } } unityMesh.RecalculateBounds(); #if !UNITY_5_5_OR_NEWER unityMesh.Optimize(); #endif if (progress != null) { progress.Update(1); } } return(unityMesh); }
public static IEnumerator FromUnity(GameObject root, Action <Scene> callback, Module.ProgressCallback progress_callback = null) { if (root != null && callback != null) { Scene scene = new Scene(); scene.unityMapping = new Mapping(); scene.unityMeshes = new Dictionary <UnityEngine.Mesh, int>(); scene.unityMaterials = new Dictionary <UnityEngine.Material, int>(); scene.unityTextures = new Dictionary <Texture2D, int>(); scene.unityComponents = new Dictionary <Component, UnityComponent>(); scene.unityReferences = new HashSet <UnityReference>(); uint nb_nodes = CountNodes(root.transform); Utils.Progress progress = new Utils.Progress(); progress.Init(nb_nodes, progress_callback); // Parse the node hierarchy IEnumerator it = Node.FromUnity(scene, root.transform, n => scene.root_node = n, progress); while (it.MoveNext()) { progress.Display(); yield return(it.Current); } int meshes_count = scene.unityMeshes.Count; int materials_count = scene.unityMaterials.Count; int textures_count = scene.unityTextures.Count; progress.Init((uint)(nb_nodes + meshes_count + materials_count + textures_count), progress_callback); progress.Update(nb_nodes); // Convert the quick lookup meshes dictionary to the final destination array if (meshes_count > 0) { Mesh[] meshes = new Mesh[meshes_count]; foreach (KeyValuePair <UnityEngine.Mesh, int> mesh_pair in scene.unityMeshes) { meshes[mesh_pair.Value] = Mesh.FromUnity(mesh_pair.Key); progress.Update(1); progress.Display(); yield return(null); } scene.meshes = meshes; } // Convert the quick lookup materials dictionary to the final destination array if (materials_count > 0) { Material[] materials = new Material[materials_count]; foreach (KeyValuePair <UnityEngine.Material, int> mat_pair in scene.unityMaterials) { materials[mat_pair.Value] = Material.FromUnity(scene, mat_pair.Key); progress.Update(1); progress.Display(); yield return(null); } scene.materials = materials; } // Convert the quick lookup materials dictionary to the final destination array if (textures_count > 0) { Texture[] textures = new Texture[textures_count]; foreach (KeyValuePair <Texture2D, int> tex_pair in scene.unityTextures) { textures[tex_pair.Value] = Texture.FromUnity(tex_pair.Key); progress.Update(1); progress.Display(); yield return(null); } scene.textures = textures; } // Resolve references foreach (UnityReference reference in scene.unityReferences) { reference.ResolveReference(scene.unityMapping, scene.unityMeshes, scene.unityMaterials, scene.unityTextures); } // Clean up scene.unityMapping = null; scene.unityMeshes = null; scene.unityMaterials = null; scene.unityTextures = null; scene.unityComponents = null; scene.unityReferences = null; // Return the result callback(scene); } }