private ExportResults ExportHelper( SceneStatePayload payload, string outputFile, bool binary, bool doExtras, int gltfVersion, bool allowHttpUri) { // TODO: Ownership of this temp directory is sloppy. // Payload and export share the same dir and we assume that the exporter: // 1. will not write files whose names conflict with payload's // 2. will clean up the entire directory when done // This works, as long as the payload isn't used for more than one export (it currently isn't) using (var exporter = new GlTF_ScriptableExporter(payload.temporaryDirectory, gltfVersion)) { exporter.AllowHttpUri = allowHttpUri; try { m_exporter = exporter; exporter.G.binary = binary; exporter.BeginExport(outputFile); exporter.SetMetadata(payload.generator, copyright: null); if (doExtras) { SetExtras(exporter, payload); } if (payload.env.skyCubemap != null) { // Add the skybox texture to the export. string texturePath = ExportUtils.GetTexturePath(payload.env.skyCubemap); string textureFilename = Path.GetFileName(texturePath); exporter.G.extras["TB_EnvironmentSkybox"] = ExportFileReference.CreateLocal(texturePath, textureFilename); } WriteObjectsAndConnections(exporter, payload); string[] exportedFiles = exporter.EndExport(); return(new ExportResults { success = true, exportedFiles = exportedFiles, numTris = exporter.NumTris }); } catch (InvalidOperationException e) { OutputWindowScript.Error("glTF export failed", e.Message); // TODO: anti-pattern. Let the exception bubble up so caller can log it properly // Actually, InvalidOperationException is now somewhat expected in experimental, since // the gltf exporter does not check IExportableMaterial.SupportsDetailedMaterialInfo. // But we still want the logging for standalone builds. Debug.LogException(e); return(new ExportResults { success = false }); } catch (IOException e) { OutputWindowScript.Error("glTF export failed", e.Message); return(new ExportResults { success = false }); } finally { payload.Destroy(); // The lifetime of ExportGlTF, GlTF_ScriptableExporter, and GlTF_Globals instances // is identical. This is solely to be pedantic. m_exporter = null; } } }
// Get a material for the given mesh. If a _MainTex texture exists, reference to it from // UnityEditor.AssetBase. static IExportableMaterial GetMaterialForMeshData(MeshData data) { // Create parameter dictionaries. Dictionary <string, string> textureUris = new Dictionary <string, string>(); Dictionary <string, Vector2> textureSizes = new Dictionary <string, Vector2>(); Dictionary <string, Vector3> vectorParams = new Dictionary <string, Vector3>(); Dictionary <string, float> floatParams = new Dictionary <string, float>(); Dictionary <string, Color> colorParams = new Dictionary <string, Color>(); // Get material for the mesh. Material mat = data.renderer.sharedMaterial; Texture2D mainTex = (Texture2D)mat.GetTexture("_MainTex"); string uriBase = ""; if (mainTex) { string texturePath = ExportUtils.GetTexturePath(mainTex); textureUris.Add("BaseColorTex", ExportUtils.kTempPrefix + texturePath); Vector2 uvScale = mat.GetTextureScale("_MainTex"); Vector2 uvOffset = mat.GetTextureOffset("_MainTex"); colorParams.Add("UvAdjust", new Color(uvScale.x, uvScale.y, uvOffset.x, uvOffset.y)); } else { textureUris.Add("BaseColorTex", ExportUtils.kBuiltInPrefix + "whiteTextureMap.png"); colorParams.Add("UvAdjust", new Color(1.0f, 1.0f, 0.0f, 0.0f)); } // Check for secondary light map texture. bool hasLightMap = false; Texture2D lightMapTex = null; if (mat.HasProperty("_DetailAlbedoMap")) { lightMapTex = (Texture2D)mat.GetTexture("_DetailAlbedoMap"); } if (lightMapTex == null && mat.HasProperty("_LightMap")) { lightMapTex = (Texture2D)mat.GetTexture("_LightMap"); } if (lightMapTex) { hasLightMap = true; string texturePath = ExportUtils.GetTexturePath(lightMapTex); textureUris.Add("LightMapTex", ExportUtils.kTempPrefix + texturePath); } else { textureUris.Add("LightMapTex", ExportUtils.kBuiltInPrefix + "whiteTextureMap.png"); } // As much as possible, this should mimic the way that GltfMaterialConverter.ConvertMaterial() // converts materials from gltf2 to UnityMaterial methods. if (mat.HasProperty("_Color")) { colorParams.Add("BaseColorFactor", mat.color); } else { colorParams.Add("BaseColorFactor", Color.black); } // Create the exportable material. BrushDescriptor exportDescriptor = hasLightMap ? Resources.Load <BrushDescriptor>("Brushes/Poly/Environments/EnvironmentDiffuseLightMap") : Resources.Load <BrushDescriptor>("Brushes/Poly/Environments/EnvironmentDiffuse"); return(new DynamicExportableMaterial( // Things that come from the template blendMode: exportDescriptor.BlendMode, vertexLayout: exportDescriptor.VertexLayout, vertShaderUri: exportDescriptor.VertShaderUri, fragShaderUri: exportDescriptor.FragShaderUri, enableCull: exportDescriptor.EnableCull, // Things that vary durableName: data.renderer.gameObject.GetInstanceID().ToString(), emissiveFactor: 0.0f, // TODO: Should the export texture be the main pbr texture? hasExportTexture: false, exportTextureFilename: null, uriBase: uriBase, textureUris: textureUris, textureSizes: textureSizes, floatParams: floatParams, vectorParams: vectorParams, colorParams: colorParams)); }