Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [9.0.0-pre.2] - 2022-11-30
### Fixed
- Enabled the ability to select mipmap streaming for textures imported with the PSD Importer.
- Fixed an issue where the amount of alpha removed from layers would not be re-applied as final position offset of the layers.
- Fixed an issue where the editor would crash when importing .psd/.psb files with their layers outside of the document canvas. (Case DANB-300)
- Fixed an issue where the generated GameObjects would be laid out differently from how they appear in the DCC tool. (Case DANB-298)
  • Loading branch information
Unity Technologies committed Nov 30, 2022
1 parent 0ff59b2 commit a8904de
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 151 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [9.0.0-pre.2] - 2022-11-30
### Fixed
- Enabled the ability to select mipmap streaming for textures imported with the PSD Importer.
- Fixed an issue where the amount of alpha removed from layers would not be re-applied as final position offset of the layers.
- Fixed an issue where the editor would crash when importing .psd/.psb files with their layers outside of the document canvas. (Case DANB-300)
- Fixed an issue where the generated GameObjects would be laid out differently from how they appear in the DCC tool. (Case DANB-298)

## [9.0.0-pre.1] - 2022-09-21
### Changed
- Simplified the Sprite Meta Data storage. We now have 3 storages; Single Sprite, Multiple Sprites and Mosaiac (Atlased) Sprites.
Expand Down
2 changes: 1 addition & 1 deletion Documentation~/PSD-importer-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ To include and maintain the group and hierarchy structure as per the source file
![](images/as-per-source-22.2.png)<br/>The generated Prefab of the same source file with **Layer Group** set to **As Per Source File**.

###Character Rig
This section is only available if the **Texture Type** is set to **Multiple** and **Import Mode** is set to **Individual Sprites (Mosaic)**.
This section is only available if the **Texture Type** is set to **Multiple**, **Import Mode** is set to **Individual Sprites (Mosaic)** and the [2D Animation package](https://docs.unity3d.com/Packages/com.unity.2d.animation@latest) is installed.

![](images/character-rig-22.2.png)

Expand Down
23 changes: 14 additions & 9 deletions Editor/PSDImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace UnityEditor.U2D.PSD
/// ScriptedImporter to import Photoshop files
/// </summary>
// Version using unity release + 5 digit padding for future upgrade. Eg 2021.2 -> 21200000
[ScriptedImporter(23100001, new string[] {"psb"}, new[] {"psd"}, AllowCaching = true)]
[ScriptedImporter(23100002, new string[] {"psb"}, new[] {"psd"}, AllowCaching = true)]
[HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.psdimporter@latest")]
[MovedFrom("UnityEditor.Experimental.AssetImporters")]
public partial class PSDImporter : ScriptedImporter, ISpriteEditorDataProvider
Expand Down Expand Up @@ -52,7 +52,8 @@ internal enum ELayerMappingOption
readable = false,

#if ENABLE_TEXTURE_STREAMING
streamingMipmaps = true,
streamingMipmaps = false,
streamingMipmapsPriority = 0,
#endif

fadeOut = false,
Expand Down Expand Up @@ -512,7 +513,7 @@ TextureGenerationOutput ImportFlattenImage(Document doc, AssetImportContext ctx)
var outputImageBuffer = new NativeArray<Color32>(doc.width * doc.height, Allocator.Persistent);
try
{
FlattenImageTask.Execute(m_ExtractData, ref outputImageBuffer, m_ImportHiddenLayers, documentSize);
FlattenImageTask.Execute(m_ExtractData, ref outputImageBuffer, m_ImportHiddenLayers, canvasSize);

m_SingleSpriteImportData[0].name = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath) + "_1";
m_SingleSpriteImportData[0].alignment = (SpriteAlignment)m_TextureImporterSettings.spriteAlignment;
Expand Down Expand Up @@ -548,7 +549,7 @@ TextureGenerationOutput ImportFromLayers(AssetImportContext ctx)
List<PSDLayer> psdLayers = null;
try
{
ExtractLayerTask.Execute(in m_ExtractData, out psdLayers, m_ImportHiddenLayers, documentSize);
ExtractLayerTask.Execute(in m_ExtractData, out psdLayers, m_ImportHiddenLayers, canvasSize);

var mappingStrategy = GetLayerMappingStrategy();
var layerUnique = mappingStrategy.LayersUnique(psdLayers.ConvertAll(x => (IPSDLayerMappingStrategyComparable)x));
Expand Down Expand Up @@ -595,6 +596,10 @@ TextureGenerationOutput ImportFromLayers(AssetImportContext ctx)
}

ImagePacker.Pack(layerBuffers.ToArray(), layerWidth.ToArray(), layerHeight.ToArray(), m_Padding, m_SpriteSizeExpand, out outputImageBuffer, out int width, out int height, out RectInt[] spriteData, out Vector2Int[] uvTransform);

var packOffsets = new Vector2[spriteData.Length];
for (var i = 0; i < packOffsets.Length; ++i)
packOffsets[i] = new Vector2((uvTransform[i].x - spriteData[i].position.x) / -1f, (uvTransform[i].y - spriteData[i].position.y) / -1f);

var spriteImportData = GetSpriteImportData();
if (spriteImportData.Count <= 0 || shouldResliceFromLayer || hasNewLayer)
Expand Down Expand Up @@ -623,12 +628,11 @@ TextureGenerationOutput ImportFromLayers(AssetImportContext ctx)

psdLayer.spriteName = ImportUtilities.GetUniqueSpriteName(psdLayer.name, spriteNameHash, m_KeepDupilcateSpriteName);
spriteSheet.name = psdLayer.spriteName;
spriteSheet.spritePosition = psdLayer.layerPosition;
spriteSheet.spritePosition = psdLayer.layerPosition + packOffsets[i];

if(shouldResliceFromLayer)
spriteSheet.rect = new Rect(spriteData[i].x, spriteData[i].y, spriteData[i].width, spriteData[i].height);



spriteSheet.uvTransform = uvTransform[i];

psdLayer.spriteID = spriteSheet.spriteID;
Expand Down Expand Up @@ -697,8 +701,9 @@ TextureGenerationOutput ImportFromLayers(AssetImportContext ctx)
r.width = spriteData[k].width;
r.height = spriteData[k].height;
}

spriteSheet.rect = r;
spriteSheet.spritePosition = psdLayers[i].layerPosition;
spriteSheet.spritePosition = psdLayers[i].layerPosition + packOffsets[k];
}

if (spriteSheet != null)
Expand Down Expand Up @@ -1284,7 +1289,7 @@ internal void SetDocumentPivot(Vector2 pivot)
SpriteImportMode.None :
(SpriteImportMode)m_TextureImporterSettings.spriteMode;

internal Vector2Int documentSize => importData.documentSize;
internal Vector2Int canvasSize => importData.documentSize;

#if ENABLE_2D_ANIMATION
internal CharacterData characterData
Expand Down
27 changes: 27 additions & 0 deletions Editor/PSDImporterAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ public int mipmapFadeDistanceStart
}
}

/// <summary>
/// Enable mipmap streaming for the texture.
/// <br/><br/>Only load larger mipmaps as needed to render the current game cameras. Requires texture streaming to be enabled in quality settings.
/// </summary>
public bool streamingMipmaps
{
get => m_TextureImporterSettings.streamingMipmaps;
set
{
m_TextureImporterSettings.streamingMipmaps = value;
SetDirty();
}
}

/// <summary>
/// Mipmap streaming priority when there's contention for resources. Positive numbers represent higher priority. Valid range is -128 to 127.
/// </summary>
public int streamingMipmapsPriority
{
get => m_TextureImporterSettings.streamingMipmapsPriority;
set
{
m_TextureImporterSettings.streamingMipmapsPriority = Mathf.Clamp(value, -128, 127);
SetDirty();
}
}

/// <summary>
/// Mip level where texture is faded out completely.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Editor/PSDImporterDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public CharacterData GetCharacterData()

parts.Reverse();
cd.parts = parts.ToArray();
cd.dimension = dataProvider.documentSize;
cd.dimension = dataProvider.canvasSize;
cd.characterGroups = groups.ToArray();
return cd;
}
Expand Down
35 changes: 35 additions & 0 deletions Editor/PSDImporterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ struct InspectorGUI
SerializedProperty m_sRGBTexture;
SerializedProperty m_AlphaSource;
SerializedProperty m_Swizzle;
#if ENABLE_TEXTURE_STREAMING
SerializedProperty m_StreamingMipmaps;
SerializedProperty m_StreamingMipmapsPriority;
#endif
SerializedProperty m_MipMapMode;
SerializedProperty m_EnableMipMap;
SerializedProperty m_FadeOut;
Expand Down Expand Up @@ -150,6 +154,10 @@ public override void OnEnable()
m_IsReadable = textureImporterSettingsSP.FindPropertyRelative("m_IsReadable");
m_sRGBTexture = textureImporterSettingsSP.FindPropertyRelative("m_sRGBTexture");
m_AlphaSource = textureImporterSettingsSP.FindPropertyRelative("m_AlphaSource");
#if ENABLE_TEXTURE_STREAMING
m_StreamingMipmaps = textureImporterSettingsSP.FindPropertyRelative("m_StreamingMipmaps");
m_StreamingMipmapsPriority = textureImporterSettingsSP.FindPropertyRelative("m_StreamingMipmapsPriority");
#endif
m_MipMapMode = textureImporterSettingsSP.FindPropertyRelative("m_MipMapMode");
m_EnableMipMap = textureImporterSettingsSP.FindPropertyRelative("m_EnableMipMap");
m_Swizzle = textureImporterSettingsSP.FindPropertyRelative("m_Swizzle");
Expand Down Expand Up @@ -1093,6 +1101,22 @@ void MipMapGUI()
{
EditorGUI.indentLevel++;
ToggleFromInt(m_BorderMipMap, styles.borderMipMaps);

#if ENABLE_TEXTURE_STREAMING
ToggleFromInt(m_StreamingMipmaps, styles.streamingMipMaps);
if (m_StreamingMipmaps.boolValue && !m_StreamingMipmaps.hasMultipleDifferentValues)
{
EditorGUI.indentLevel++;
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_StreamingMipmapsPriority, styles.streamingMipmapsPriority);
if (EditorGUI.EndChangeCheck())
{
m_StreamingMipmapsPriority.intValue = Mathf.Clamp(m_StreamingMipmapsPriority.intValue, -128, 127);
}
EditorGUI.indentLevel--;
}
#endif

m_MipMapMode.intValue = EditorGUILayout.Popup(styles.mipMapFilter, m_MipMapMode.intValue, styles.mipMapFilterOptions);

ToggleFromInt(m_MipMapsPreserveCoverage, styles.mipMapsPreserveCoverage);
Expand Down Expand Up @@ -1336,6 +1360,13 @@ internal TextureImporterSettings GetSerializedPropertySettings(TextureImporterSe
if (!m_BorderMipMap.hasMultipleDifferentValues)
settings.borderMipmap = m_BorderMipMap.intValue > 0;

#if ENABLE_TEXTURE_STREAMING
if (!m_StreamingMipmaps.hasMultipleDifferentValues)
settings.streamingMipmaps = m_StreamingMipmaps.intValue > 0;
if (!m_StreamingMipmapsPriority.hasMultipleDifferentValues)
settings.streamingMipmapsPriority = m_StreamingMipmapsPriority.intValue;
#endif

if (!m_MipMapsPreserveCoverage.hasMultipleDifferentValues)
settings.mipMapsPreserveCoverage = m_MipMapsPreserveCoverage.intValue > 0;

Expand Down Expand Up @@ -1558,6 +1589,10 @@ internal class Styles
public readonly GUIContent generateMipMaps = new GUIContent("Generate Mip Maps");
public readonly GUIContent sRGBTexture = new GUIContent("sRGB (Color Texture)", "Texture content is stored in gamma space. Non-HDR color textures should enable this flag (except if used for IMGUI).");
public readonly GUIContent borderMipMaps = new GUIContent("Border Mip Maps");
#if ENABLE_TEXTURE_STREAMING
public readonly GUIContent streamingMipMaps = EditorGUIUtility.TrTextContent("Mip Streaming", "Only load larger mipmaps as needed to render the current game cameras. Requires texture streaming to be enabled in quality settings.");
public readonly GUIContent streamingMipmapsPriority = EditorGUIUtility.TrTextContent("Priority", "Mipmap streaming priority when there's contention for resources. Positive numbers represent higher priority. Valid range is -128 to 127.");
#endif
public readonly GUIContent mipMapsPreserveCoverage = new GUIContent("Mip Maps Preserve Coverage", "The alpha channel of generated Mip Maps will preserve coverage during the alpha test.");
public readonly GUIContent alphaTestReferenceValue = new GUIContent("Alpha Cutoff Value", "The reference value used during the alpha test. Controls Mip Map coverage.");
public readonly GUIContent mipMapFilter = new GUIContent("Mip Map Filtering");
Expand Down
19 changes: 19 additions & 0 deletions Editor/PSDLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ public PSDLayer(NativeArray<Color32> tex, int parent, bool group, string layerNa
m_SpriteID = new GUID().ToString();
}

public PSDLayer(PSDLayer layer)
{
m_Name = layer.m_Name;
m_SpriteName = layer.m_SpriteName;
m_IsGroup = layer.m_IsGroup;
m_ParentIndex = layer.m_ParentIndex;
m_SpriteID = layer.m_SpriteID;
m_LayerID = layer.m_LayerID;
m_MosaicPosition = layer.m_MosaicPosition;
m_Flatten = layer.m_Flatten;
m_IsImported = layer.m_IsImported;
m_IsVisible = layer.m_IsVisible;
m_LayerPosition = layer.m_LayerPosition;
m_GameObject = layer.m_GameObject;
width = layer.width;
height = layer.height;
texture = layer.texture;
}

public bool isVisible => m_IsVisible;
public int layerID { get { return m_LayerID; } private set { m_LayerID = value; } }

Expand Down
Loading

0 comments on commit a8904de

Please sign in to comment.