コード例 #1
0
        private bool AddTexture(Texture tex, TextureReferences reference)
        {
            var meta = EnsureTexture(tex);

            if (meta == null)
            {
                return(false);
            }
            return(meta.References.Add(reference));
        }
コード例 #2
0
ファイル: TextureExporter.cs プロジェクト: Y-way/Unity2Urho
        private void TransformDiffuse(AssetContext asset, Texture texture, TextureReferences reference)
        {
            var diffuse = texture as Texture2D;

            EnsureReadableTexture(diffuse);
            var specularGlossiness = reference.SmoothnessSource as Texture2D;

            EnsureReadableTexture(specularGlossiness);

            var metallicRoughMapName = GetTextureOutputName(asset.UrhoAssetName, reference);

            using (var fileStream = asset.DestinationFolder.Create(metallicRoughMapName))
            {
                if (fileStream != null)
                {
                    var tmpTexture     = CreateTargetTexture(diffuse, specularGlossiness);
                    var specularColors = new PixelSource(specularGlossiness, tmpTexture.width, tmpTexture.height,
                                                         Color.black);
                    var diffuseColors    = new PixelSource(diffuse, tmpTexture.width, tmpTexture.height, Color.white);
                    var smoothnessSource =
                        reference.SmoothnessTextureChannel == SmoothnessTextureChannel.MetallicOrSpecularAlpha
                            ? specularColors
                            : diffuseColors;
                    var pixels = new Color32[tmpTexture.width * tmpTexture.height];
                    var index  = 0;
                    for (var y = 0; y < tmpTexture.height; ++y)
                    {
                        for (var x = 0; x < tmpTexture.width; ++x)
                        {
                            var diffuseColor = diffuseColors.GetAt(x, y);
                            var mr           = PBRUtils.ConvertToMetallicRoughness(new PBRUtils.SpecularGlossiness
                            {
                                diffuse    = diffuseColor,
                                specular   = specularColors.GetAt(x, y),
                                glossiness = smoothnessSource.GetAt(x, y).a,
                                opacity    = diffuseColor.a
                            });
                            pixels[index] = new Color(mr.baseColor.r, mr.baseColor.g, mr.baseColor.b, mr.opacity);
                            ++index;
                        }
                    }

                    tmpTexture.SetPixels32(pixels, 0);
                    var bytes = tmpTexture.EncodeToPNG();
                    fileStream.Write(bytes, 0, bytes.Length);
                }
            }
        }
コード例 #3
0
ファイル: TextureExporter.cs プロジェクト: bmjoy/Unity2Urho
        public static string GetTextureOutputName(string baseAssetName, TextureReferences reference)
        {
            switch (reference.Semantic)
            {
            case TextureSemantic.PBRMetallicGlossiness:
                return(ReplaceExtension(baseAssetName, ".MetallicRoughness.png"));

            case TextureSemantic.PBRSpecularGlossiness:
                return(ReplaceExtension(baseAssetName, ".MetallicRoughness.png"));

            case TextureSemantic.PBRDiffuse:
                return(ReplaceExtension(baseAssetName, ".BaseColor.png"));

            default: return(baseAssetName);
            }
        }
コード例 #4
0
ファイル: TextureExporter.cs プロジェクト: bmjoy/Unity2Urho
        private void TransformDiffuse(AssetContext asset, Texture texture, TextureReferences reference)
        {
            var diffuse = texture as Texture2D;

            EnsureReadableTexture(diffuse);
            var specularGlossiness = reference.SmoothnessSource as Texture2D;

            EnsureReadableTexture(specularGlossiness);

            var metallicRoughMapName = GetTextureOutputName(asset.UrhoAssetName, reference);

            using (var fileStream = asset.DestinationFolder.Create(metallicRoughMapName))
            {
                if (fileStream != null)
                {
                    var specWidth        = specularGlossiness?.width ?? 1;
                    var specHeight       = specularGlossiness?.height ?? 1;
                    var smoothnessColors = specularGlossiness?.GetPixels32(0) ?? new[] { new Color32(0, 0, 0, 255) };
                    var width            = Math.Max(diffuse.width, specWidth);
                    var height           = Math.Max(diffuse.height, specHeight);
                    var tmpTexture       = new Texture2D(width, height, TextureFormat.RGBA32, false);
                    tmpTexture.hideFlags = HideFlags.HideAndDontSave;

                    var diffuseColors = diffuse.GetPixels32(0);
                    var pixels        = new Color32[width * height];
                    var index         = 0;
                    for (var y = 0; y < height; ++y)
                    {
                        for (var x = 0; x < width; ++x)
                        {
                            var specular     = Get(smoothnessColors, specWidth, specHeight, x, y, width, height);
                            var diffuseColor = Get(diffuseColors, diffuse.width, diffuse.height, x, y, width, height);
                            pixels[index] = new Color(diffuseColor.r + specular.r, diffuseColor.g + specular.g,
                                                      diffuseColor.b + specular.b, diffuseColor.a);
                            ++index;
                        }
                    }

                    tmpTexture.SetPixels32(pixels, 0);
                    var bytes = tmpTexture.EncodeToPNG();
                    fileStream.Write(bytes, 0, bytes.Length);
                }
            }
        }
コード例 #5
0
ファイル: TextureExporter.cs プロジェクト: bmjoy/Unity2Urho
        private void TransformSpecularGlossiness(AssetContext asset, Texture texture, TextureReferences reference)
        {
            var specularGloss = texture as Texture2D;

            EnsureReadableTexture(specularGloss);
            var diffuse = reference.SmoothnessSource as Texture2D;

            EnsureReadableTexture(diffuse);
            var smoothnessSource =
                reference.SmoothnessTextureChannel == SmoothnessTextureChannel.MetallicOrSpecularAlpha
                    ? specularGloss
                    : diffuse;

            var metallicRoughMapName = GetTextureOutputName(asset.UrhoAssetName, reference);

            using (var fileStream = asset.DestinationFolder.Create(metallicRoughMapName))
            {
                if (fileStream != null)
                {
                    var width      = Math.Max(specularGloss.width, smoothnessSource.width);
                    var height     = Math.Max(specularGloss.height, smoothnessSource.height);
                    var tmpTexture = new Texture2D(width, height, TextureFormat.RGBA32, false);
                    tmpTexture.hideFlags = HideFlags.HideAndDontSave;

                    var metallicColors   = specularGloss.GetPixels32(0);
                    var diffuseColors    = diffuse.GetPixels32(0);
                    var smoothnessColors = specularGloss == smoothnessSource
                        ? metallicColors
                        : smoothnessSource.GetPixels32(0);
                    var pixels = new Color32[width * height];
                    var index  = 0;
                    for (var y = 0; y < height; ++y)
                    {
                        for (var x = 0; x < width; ++x)
                        {
                            var r = 1.0f - Get(smoothnessColors, smoothnessSource.width, smoothnessSource.height, x, y,
                                               width, height).a;
                            var d = GetLuminance(Get(diffuseColors, diffuse.width, diffuse.height, x, y, width, height));
                            var s = GetLuminance(Get(metallicColors, specularGloss.width, specularGloss.height, x, y, width,
                                                     height));
                            var m = s / (d + s);
                            pixels[index] = new Color(r, m, 0, 1);
                            ++index;
                        }
                    }

                    tmpTexture.SetPixels32(pixels, 0);
                    var bytes = tmpTexture.EncodeToPNG();
                    fileStream.Write(bytes, 0, bytes.Length);
                }
            }
        }
コード例 #6
0
ファイル: MaterialExporter.cs プロジェクト: bmjoy/Unity2Urho
        private void ExportSpecularGlossiness(AssetContext asset, SpecularGlossinessShaderArguments arguments)
        {
            using (var writer = asset.DestinationFolder.CreateXml(asset.UrhoAssetName))
            {
                if (writer == null)
                {
                    return;
                }
                writer.WriteStartDocument();
                writer.WriteWhitespace(Environment.NewLine);
                writer.WriteStartElement("material");
                writer.WriteWhitespace(Environment.NewLine);

                if (arguments.Diffuse != null)
                {
                    // Albedo
                    if (arguments.PBRSpecular != null)
                    {
                        // Albedo, MetallicGloss
                        if (arguments.Bump != null)
                        {
                            // Albedo, MetallicGloss, Normal
                            if (arguments.Emission)
                            {
                                // Albedo, MetallicGloss, Normal, Emission
                                if (arguments.Transparent)
                                {
                                    WriteTechnique(writer,
                                                   "Techniques/PBR/PBRMetallicRoughDiffNormalSpecEmissiveAlpha.xml");
                                }
                                else
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffNormalSpecEmissive.xml");
                                }
                                WriteTexture(arguments.Emission, writer, "emissive");
                            }
                            else
                            {
                                // Albedo, MetallicGloss, Normal, No Emission
                                if (arguments.Transparent)
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffNormalSpecAlpha.xml");
                                }
                                else
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffNormalSpec.xml");
                                }
                            }

                            WriteTexture(arguments.Bump, writer, "normal");
                        }
                        else
                        {
                            // Albedo, MetallicGloss, No Normal
                            if (arguments.Transparent)
                            {
                                WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffSpecAlpha.xml");
                            }
                            else
                            {
                                WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffSpec.xml");
                            }
                        }

                        if (_assets.TryGetTexturePath(arguments.PBRSpecular, out var baseAssetName))
                        {
                            var textureReferences = new TextureReferences(TextureSemantic.PBRSpecularGlossiness, 1.0f,
                                                                          arguments.SmoothnessTextureChannel == SmoothnessTextureChannel.AlbedoAlpha
                                    ? arguments.Diffuse
                                    : arguments.PBRSpecular, arguments.SmoothnessTextureChannel);
                            var textureOutputName =
                                TextureExporter.GetTextureOutputName(baseAssetName, textureReferences);
                            WriteTexture(textureOutputName, writer, "specular");
                        }
                    }
                    else
                    {
                        // Albedo, No MetallicGloss
                        if (arguments.Bump != null)
                        {
                            // Albedo, No MetallicGloss, Normal
                            if (arguments.Emission != null)
                            {
                                // Albedo, No MetallicGloss, Normal, Emission
                                if (arguments.Transparent)
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRDiffNormalEmissiveAlpha.xml");
                                }
                                else
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRDiffNormalEmissive.xml");
                                }
                                WriteTexture(arguments.Emission, writer, "emissive");
                            }
                            else
                            {
                                // Albedo, No MetallicGloss, Normal, No Emission
                                if (arguments.Transparent)
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRDiffNormalAlpha.xml");
                                }
                                else
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRDiffNormal.xml");
                                }
                            }

                            WriteTexture(arguments.Bump, writer, "normal");
                        }
                        else
                        {
                            // Albedo, No MetallicGloss, No Normal
                            if (arguments.Transparent)
                            {
                                WriteTechnique(writer, "Techniques/PBR/PBRDiffAlpha.xml");
                            }
                            else
                            {
                                WriteTechnique(writer, "Techniques/PBR/PBRDiff.xml");
                            }
                        }
                    }

                    if (_assets.TryGetTexturePath(arguments.Diffuse, out var diffuseName))
                    {
                        var textureReferences = new TextureReferences(TextureSemantic.PBRDiffuse, 1.0f,
                                                                      arguments.PBRSpecular, arguments.SmoothnessTextureChannel);
                        var textureOutputName = TextureExporter.GetTextureOutputName(diffuseName, textureReferences);
                        WriteTexture(textureOutputName, writer, "diffuse");
                    }
                }
                else
                {
                    // No albedo
                    if (arguments.Transparent)
                    {
                        WriteTechnique(writer, "Techniques/PBR/PBRNoTextureAlpha.xml");
                    }
                    else
                    {
                        WriteTechnique(writer, "Techniques/PBR/PBRNoTexture.xml");
                    }
                }

                WriteParameter(writer, "MatDiffColor", BaseNodeExporter.Format(arguments.DiffuseColor));
                if (arguments.HasEmission)
                {
                    WriteParameter(writer, "MatEmissiveColor", BaseNodeExporter.FormatRGB(arguments.EmissiveColor));
                }
                WriteParameter(writer, "MatEnvMapColor", BaseNodeExporter.FormatRGB(Color.white));
                WriteParameter(writer, "MatSpecColor", BaseNodeExporter.Format(Vector4.zero));
                if (arguments.PBRSpecular != null)
                {
                    WriteParameter(writer, "Roughness", BaseNodeExporter.Format(0));
                    WriteParameter(writer, "Metallic", BaseNodeExporter.Format(0));
                }
                else
                {
                    ////TODO: Evaluate correct parameters:
                    WriteParameter(writer, "Roughness", BaseNodeExporter.Format(0.0f));
                    WriteParameter(writer, "Metallic", BaseNodeExporter.Format(0.0f));
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
コード例 #7
0
ファイル: TextureExporter.cs プロジェクト: Y-way/Unity2Urho
        private void TransformMetallicGlossiness(AssetContext asset, Texture texture, TextureReferences reference)
        {
            var metallicGloss = texture as Texture2D;

            EnsureReadableTexture(metallicGloss);
            var smoothnessSource = reference.SmoothnessSource as Texture2D;

            EnsureReadableTexture(smoothnessSource);

            var metallicRoughMapName = GetTextureOutputName(asset.UrhoAssetName, reference);

            using (var fileStream = asset.DestinationFolder.Create(metallicRoughMapName))
            {
                if (fileStream != null)
                {
                    var tmpTexture = CreateTargetTexture(metallicGloss, reference.SmoothnessSource as Texture2D);

                    var metallicColors = new PixelSource(metallicGloss, tmpTexture.width, tmpTexture.height,
                                                         new Color(0, 0, 0, 0));
                    var smoothnessColors = metallicGloss == smoothnessSource
                        ? metallicColors
                        : new PixelSource(smoothnessSource, tmpTexture.width, tmpTexture.height, new Color(0, 0, 0, 0));
                    var pixels = new Color32[tmpTexture.width * tmpTexture.height];
                    var index  = 0;
                    for (var y = 0; y < tmpTexture.height; ++y)
                    {
                        for (var x = 0; x < tmpTexture.width; ++x)
                        {
                            var r = 1.0f - smoothnessColors.GetAt(x, y).a;
                            var m = metallicColors.GetAt(x, y).r;
                            pixels[index] = new Color(r, m, 0, 1);
                            ++index;
                        }
                    }

                    tmpTexture.SetPixels32(pixels, 0);
                    var bytes = tmpTexture.EncodeToPNG();
                    fileStream.Write(bytes, 0, bytes.Length);
                }
            }
        }