/// Returns a BrushDescriptor given a gltf material, or null if not found. /// If the material is an instance of a template, the descriptor for that /// will be returned. public static BrushDescriptor LookupBrushDescriptor(GltfMaterialBase gltfMaterial) { Guid guid = ParseGuidFromMaterial(gltfMaterial); if (guid == Guid.Empty) { return(null); } else { BrushDescriptor desc; TiltBrushToolkit.TbtSettings.BrushManifest.BrushesByGuid.TryGetValue( guid, out desc); if (desc == null) { // Maybe it's templated from a pbr material; the template guid // can be found on the shader. Gltf1Material gltf1Material = gltfMaterial as Gltf1Material; if (gltf1Material == null) { Debug.LogErrorFormat("Unexpected: glTF2 Tilt Brush material"); return(null); } Guid templateGuid = ParseGuidFromShader((Gltf1Material)gltfMaterial); TiltBrushToolkit.TbtSettings.BrushManifest.BrushesByGuid.TryGetValue( templateGuid, out desc); } return(desc); } }
/// Returns a BrushDescriptor given a gltf material, or null if not found. /// If the material is an instance of a template, the descriptor for that /// will be returned. /// Note that gltf2 has pbr support, and Tilt Brush uses that instead of /// template "brushes". public static BrushDescriptor LookupBrushDescriptor(GltfMaterialBase gltfMaterial) { Guid guid = ParseGuidFromMaterial(gltfMaterial); if (guid == Guid.Empty) { return(null); } else { BrushDescriptor desc; TbtSettings.Instance.TryGetBrush( guid, out desc); if (desc == null) { // Maybe it's templated from a pbr material; the template guid // can be found on the shader. Gltf1Material gltf1Material = gltfMaterial as Gltf1Material; if (gltf1Material == null) { #if TILT_BRUSH Debug.LogErrorFormat("Unexpected: glTF2 Tilt Brush material"); #endif return(null); } Guid templateGuid = ParseGuidFromShader((Gltf1Material)gltfMaterial); TbtSettings.Instance.TryGetBrush( templateGuid, out desc); } return(desc); } }
/// <summary> /// Gets (or creates) the Unity material corresponding to the given glTF material. /// </summary> /// <param name="gltfMaterial">The glTF material.</param> /// <returns>The Unity material that correpsonds to the given GLTF2 material.</returns> public UnityMaterial?GetMaterial(GltfMaterialBase gltfMaterial) { if (m_getMaterialMemo.TryGetValue(gltfMaterial, out UnityMaterial memo)) { return(memo); } if (LookUpGlobalMaterial(gltfMaterial) is UnityMaterial global) { Debug.Assert(global.material == global.template); m_getMaterialMemo[gltfMaterial] = global; return(global); } if (ConvertGltfMaterial(gltfMaterial) is UnityMaterial created) { Debug.Assert(created.material != created.template); m_newMaterials.Add(created.material); m_getMaterialMemo[gltfMaterial] = created; return(created); } Debug.LogErrorFormat("Failed to convert material {0}", gltfMaterial.name); return(null); }
/// Returns a BrushDescriptor given a gltf material, or null if not found. /// If the material is an instance of a template, the descriptor for that /// will be returned. /// Note that gltf2 has pbr support, and Tilt Brush uses that instead of /// template "brushes". public static BrushDescriptor LookupBrushDescriptor(GltfMaterialBase gltfMaterial) { Guid guid = ParseGuidFromMaterial(gltfMaterial); if (guid == Guid.Empty) { return(null); } else { BrushDescriptor desc; TbtSettings.Instance.TryGetBrush( guid, out desc); if (desc == null) { // Maybe it's templated from a pbr material; the template guid // can be found on the shader. Gltf1Material gltf1Material = gltfMaterial as Gltf1Material; if (gltf1Material == null) { return(null); } Guid templateGuid = ParseGuidFromShader((Gltf1Material)gltfMaterial); TbtSettings.Instance.TryGetBrush( templateGuid, out desc); } return(desc); } }
private UnityMaterial?ConvertGltfMaterial(GltfMaterialBase gltfMat) { if (gltfMat is Gltf1Material) { return(ConvertGltf1Material((Gltf1Material)gltfMat)); } else if (gltfMat is Gltf2Material) { return(ConvertGltf2Material((Gltf2Material)gltfMat)); } else { Debug.LogErrorFormat("Unexpected type: {0}", gltfMat.GetType()); return(null); } }
/// <summary> /// Gets (or creates) the Unity material corresponding to the given GLTF 2 material. /// </summary> /// <param name="gltfMaterial">The GLTF material.</param> /// <returns>The Unity material that correpsonds to the given GLTF2 material.</returns> public UnityMaterial?GetMaterial(GltfMaterialBase gltfMaterial) { // Have we already converted this material? { UnityMaterial result; if (materials.TryGetValue(gltfMaterial, out result)) { return(result); } } // Try to look up a global material first. Material global; if (null != (global = LookUpGlobalMaterial(gltfMaterial))) { // Found it. var result = new UnityMaterial { material = global, template = global }; materials[gltfMaterial] = result; return(result); } // Ok, we will have to create a new material. UnityMaterial?created = ConvertGltfMaterial(gltfMaterial); if (created == null) { Debug.LogErrorFormat("Failed to look up material {0}", gltfMaterial.name); } else { var result = created.Value; materials[gltfMaterial] = result; Debug.Assert(result.material != result.template); if (result.material != result.template) { newMaterials.Add(result.material); } } return(created); }
// Returns the guid that represents this material. // The guid may refer to a pre-existing material (like Blocks Paper, or Tilt Brush Light). // It may also refer to a dynamically-generated material, in which case the base material // can be found by using ParseGuidFromShader. private static Guid ParseGuidFromMaterial(GltfMaterialBase gltfMaterial) { // Tilt Brush names its gltf materials like: // material_Light-2241cd32-8ba2-48a5-9ee7-2caef7e9ed62 // .net 3.5 doesn't have Guid.TryParse, and raising FormatException generates // tons of garbage for something that is done so often. if (!kTiltBrushMaterialRegex.IsMatch(gltfMaterial.name)) { return(Guid.Empty); } int start = Mathf.Max(0, gltfMaterial.name.Length - 36); if (start < 0) { return(Guid.Empty); } return(new Guid(gltfMaterial.name.Substring(start))); }
/// <summary> /// Looks up a built-in global material that corresponds to the given GLTF material. /// This will NOT create new materials; it will only look up global ones. /// </summary> /// <param name="gltfMaterial">The material to look up.</param> /// <param name="materialGuid">The guid parsed from the material name, or Guid.None</param> /// <returns>The global material that corresponds to the given GLTF material, /// if found. If not found, null.</returns> private static UnityMaterial?LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) { // Check if it's a Tilt Brush material. Guid guid = ParseGuidFromMaterial(gltfMaterial); if (guid != Guid.Empty) { // Tilt Brush global material. PBR materials will use unrecognized guids; // these will be handled by the caller. BrushDescriptor desc; if (TbtSettings.Instance.TryGetBrush(guid, out desc)) { return(new UnityMaterial { material = desc.Material, template = desc.Material }); } } return(null); }
/// <summary> /// Looks up a built-in global material that corresponds to the given GLTF material. /// This will NOT create new materials, it will only look up global ones. /// </summary> /// <param name="gltfMaterial">The material to look up.</param> /// <param name="materialGuid">The guid parsed from the material name, or Guid.None</param> /// <returns>The global material that corresponds to the given GLTF material, /// if found. If not found, null.</returns> private static Material LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) { // Is this a Blocks gvrss material? #if false if (gltfMaterial.TechniqueExtras != null) { string surfaceShader = null; gltfMaterial.TechniqueExtras.TryGetValue("gvrss", out surfaceShader); if (surfaceShader != null) { // Blocks material. Look up the mapping in PtSettings. Material material = PtSettings.Instance.LookupSurfaceShaderMaterial(surfaceShader); if (material != null) { return(material); } else { Debug.LogWarningFormat("Unknown gvrss surface shader {0}", surfaceShader); } } } #endif // Check if it's a Tilt Brush material. Guid guid = ParseGuidFromMaterial(gltfMaterial); if (guid != Guid.Empty) { // Tilt Brush global material. PBR materials will use unrecognized guids; // these will be handled by the caller. BrushDescriptor desc; if (TiltBrushToolkit.TbtSettings.BrushManifest.BrushesByGuid.TryGetValue(guid, out desc)) { return(desc.Material); } } return(null); }
/// <summary> /// Looks up a built-in global material that corresponds to the given GLTF material. /// This will NOT create new materials; it will only look up global ones. /// </summary> /// <param name="gltfMaterial">The material to look up.</param> /// <param name="materialGuid">The guid parsed from the material name, or Guid.None</param> /// <returns>The global material that corresponds to the given GLTF material, /// if found. If not found, null.</returns> private static UnityMaterial?LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) { #if TILT_BRUSH // Is this a Gltf1 blocks material? if (gltfMaterial.TechniqueExtras != null) { string surfaceShader = null; gltfMaterial.TechniqueExtras.TryGetValue("gvrss", out surfaceShader); if (surfaceShader != null) { // Blocks material. Look up the mapping in TbtSettings. if (TbtSettings.Instance.LookupSurfaceShaderMaterial(surfaceShader) is UnityMaterial um) { return(um); } else { Debug.LogWarningFormat("Unknown gvrss surface shader {0}", surfaceShader); } } } // This method of building Guid from a name is flimsy, and proven so by b/109698832. // As a patch fix, look specifically for Blocks material names. // Is this a Gltf2 Blocks material? Gltf2Material gltf2 = gltfMaterial as Gltf2Material; if (gltf2 != null) { if (gltfMaterial.name != null) { string url = "https://vr.google.com/shaders/w/gvrss/"; if (gltfMaterial.name.Equals("BlocksGem")) { return(TbtSettings.Instance.LookupSurfaceShaderMaterial(url + "gem.json")); } if (gltfMaterial.name.Equals("BlocksGlass")) { return(TbtSettings.Instance.LookupSurfaceShaderMaterial(url + "glass.json")); } if (gltfMaterial.name.Equals("BlocksPaper")) { return(TbtSettings.Instance.LookupSurfaceShaderMaterial(url + "paper.json")); } } } #endif // Check if it's a Tilt Brush material. Guid guid = ParseGuidFromMaterial(gltfMaterial); if (guid != Guid.Empty) { // Tilt Brush global material. PBR materials will use unrecognized guids; // these will be handled by the caller. BrushDescriptor desc; if (TbtSettings.Instance.TryGetBrush(guid, out desc)) { return(new UnityMaterial { material = desc.Material, #if TILT_BRUSH exportableMaterial = desc, #endif template = desc.Material }); } } return(null); }
/// <returns>true if there is a global material corresponding to the given glTF material, /// false if a material needs to be created for this material.</returns> private static bool IsGlobalMaterial(GltfMaterialBase gltfMaterial) { // Simple implementation for now return(LookUpGlobalMaterial(gltfMaterial) != null); }