/// 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; PtSettings.Instance.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); PtSettings.Instance.brushManifest.BrushesByGuid.TryGetValue( 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 Material LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) { // Is this a Blocks gvrss material? 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); } } } // 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 (PtSettings.Instance.brushManifest.BrushesByGuid.TryGetValue(guid, out desc)) { return(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); }