Exemple #1
0
        // Helper for ConvertGltf{1,2}Material
        private UnityMaterial CreateNewPbrMaterial(
            Material baseMaterial, Gltf2Material.PbrMetallicRoughness pbr)
        {
            Material mat = UnityEngine.Object.Instantiate(baseMaterial);

            string matName = baseMaterial.name;

            if (matName.StartsWith("Base"))
            {
                matName = matName.Substring(4);
            }

            mat.SetColor("_BaseColorFactor", pbr.baseColorFactor);
            Texture tex = null;

            if (pbr.baseColorTexture != null)
            {
                tex = pbr.baseColorTexture.texture.unityTexture;
                mat.SetTexture("_BaseColorTex", tex);
            }
            if (tex != null)
            {
                matName = string.Format("{0}_{1}", matName, tex.name);
            }
            mat.SetFloat("_MetallicFactor", pbr.metallicFactor);
            mat.SetFloat("_RoughnessFactor", pbr.roughnessFactor);
            mat.name = matName;

            return(new UnityMaterial {
                material = mat, template = baseMaterial
            });
        }
Exemple #2
0
        // Helper for ConvertGltf{1,2}Material
        private UnityMaterial CreateNewPbrMaterial(
            Material baseMaterial, Gltf2Material.PbrMetallicRoughness pbr)
        {
            Material mat = UnityEngine.Object.Instantiate(baseMaterial);

            string matName = baseMaterial.name;

            if (matName.StartsWith("Base"))
            {
                matName = matName.Substring(4);
            }

            mat.SetColor("_BaseColorFactor", pbr.baseColorFactor);
            mat.SetColor("_Color", pbr.baseColorFactor); //dook hack to get other shaders to work with color (not sure why they arn't using _color for their shader anyway?)
            Texture tex = null;

            if (pbr.baseColorTexture != null)
            {
                tex = pbr.baseColorTexture.texture.unityTexture;
                mat.SetTexture("_BaseColorTex", tex);
                mat.SetTexture("_MainTex", tex);
            }
            if (tex != null)
            {
                matName = string.Format("{0}_{1}", matName, tex.name);
            }
            mat.SetFloat("_MetallicFactor", pbr.metallicFactor);
            mat.SetFloat("_RoughnessFactor", pbr.roughnessFactor);
            mat.name = matName;

            return(new UnityMaterial {
                material = mat, template = baseMaterial
            });
        }
Exemple #3
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)
        {
            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));
        }