// Returns the guid found on this material's vert or frag shader, or Empty on failure. // This Guid represents the template from which a pbr material was created. // For example, BasePbrOpaqueDoubleSided. private static Guid ParseGuidFromShader(Gltf1Material material) { var technique = material.techniquePtr; if (technique == null) { return(Guid.Empty); } var program = technique.programPtr; if (program == null) { return(Guid.Empty); } var shader = program.vertexShaderPtr ?? program.fragmentShaderPtr; if (shader == null) { return(Guid.Empty); } var match = kTiltBrushShaderRegex.Match(shader.uri); if (match.Success) { return(new Guid(match.Groups[1].Value)); } else { return(Guid.Empty); } }
/// 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); } }
/// <summary> /// Converts the given glTF1 material to a new Unity material. /// This is only possible if the passed material is a Tilt Brush "PBR" material /// squeezed into glTF1. /// </summary> /// <param name="gltfMat">The glTF1 material to convert.</param> /// <returns>The result of the conversion, or null on failure.</returns> private UnityMaterial?ConvertGltf1Material(Gltf1Material gltfMat) { Guid instanceGuid = ParseGuidFromMaterial(gltfMat); Guid templateGuid = ParseGuidFromShader(gltfMat); BrushDescriptor desc; if (!PtSettings.Instance.brushManifest.BrushesByGuid.TryGetValue(templateGuid, out desc)) { // If they are the same, there is no template/instance relationship. if (instanceGuid != templateGuid) { Debug.LogErrorFormat("Unexpected: cannot find template material {0} for {1}", templateGuid, instanceGuid); } return(null); } TiltBrushGltf1PbrValues tbPbr = gltfMat.values; // The default values here are reasonable fallbacks if there is no tbPbr Gltf2Material.PbrMetallicRoughness pbr = new Gltf2Material.PbrMetallicRoughness(); if (tbPbr != null) { if (tbPbr.BaseColorFactor != null) { pbr.baseColorFactor = tbPbr.BaseColorFactor.Value; } if (tbPbr.MetallicFactor != null) { pbr.metallicFactor = tbPbr.MetallicFactor.Value; } if (tbPbr.RoughnessFactor != null) { pbr.roughnessFactor = tbPbr.RoughnessFactor.Value; } if (tbPbr.BaseColorTexPtr != null) { pbr.baseColorTexture = new Gltf2Material.TextureInfo { index = -1, texCoord = 0, texture = tbPbr.BaseColorTexPtr }; } // Tilt Brush doesn't support metallicRoughnessTexture (yet?) } return(CreateNewPbrMaterial(desc.Material, pbr)); }