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 tmpTexture = CreateTargetTexture(specularGloss, diffuse); var specularColors = new PixelSource(specularGloss, tmpTexture.width, tmpTexture.height, Color.black); var diffuseColors = new PixelSource(diffuse, tmpTexture.width, tmpTexture.height, Color.white); var smoothnessColors = specularGloss == smoothnessSource ? 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 mr = PBRUtils.ConvertToMetallicRoughness(new PBRUtils.SpecularGlossiness { diffuse = diffuseColors.GetAt(x, y), specular = specularColors.GetAt(x, y), glossiness = smoothnessColors.GetAt(x, y).a, opacity = 1.0f }); pixels[index] = new Color(mr.roughness, mr.metallic, 0, 1); ++index; } } tmpTexture.SetPixels32(pixels, 0); var bytes = tmpTexture.EncodeToPNG(); fileStream.Write(bytes, 0, bytes.Length); } } }
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); } } }