Beispiel #1
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));
        }