Beispiel #1
0
        /// 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);
            }
        }
Beispiel #2
0
        // 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;
                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)
                    {
                        return(null);
                    }
                    Guid templateGuid = ParseGuidFromShader((Gltf1Material)gltfMaterial);
                    TbtSettings.Instance.TryGetBrush(
                        templateGuid, out desc);
                }
                return(desc);
            }
        }
Beispiel #5
0
        /// <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)
        {
            // We know this guid doesn't map to a brush; if it did, LookupGlobalMaterial would
            // have succeeded and we wouldn't be trying to create an new material.
            Guid instanceGuid = ParseGuidFromMaterial(gltfMat);
            Guid templateGuid = ParseGuidFromShader(gltfMat);

            BrushDescriptor desc;

            if (!TbtSettings.Instance.TryGetBrush(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?)
            }
            var pbrInfo = new TbtSettings.PbrMaterialInfo {
#if TILT_BRUSH
                descriptor = desc,
#endif
                material = desc.Material
            };

            return(CreateNewPbrMaterial(pbrInfo, gltfMat.name, pbr));
        }
        /// <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 (!TiltBrushToolkit.TbtSettings.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));
        }