TexTransTool

repository·master·Indexed 40 days ago

https://github.com/reinas-64892/textranstool

A Unity-focused utility for non-destructive texture application, VRAM optimization via atlasing, and direct PSD layer manipulation within the Unity editor. Key features include decal-based texture application, a PSD importer supporting 16-bit and 32-bit files with various blend modes, and components like MaterialModifier and IslandSelector for advanced mesh and material control.

Tokens
8.7K
Snippets
19
Records
37
Agent score
100%

What's inside TexTransTool

  1. Overview of TexTransTool

    master

    TexTransTool is a utility designed for non-destructive texture management and optimization in Unity. Its primary capabilities include:

    • Decal-based Texture Application: Intuitive, non-destructive application of textures using decals.
    • VRAM Optimization: Reducing VRAM usage through texture atlasing.
    • PSD Integration: Importing PSD files and enabling direct manipulation of PSD layers within the Unity editor.
  2. Handle Layer Folders using SelectionDivider

    master

    Layer folders are managed via SelectionDivider additional layer information, identified by the keys lsct or lsdk.

    lsdk is used in newer Photoshop versions for deep hierarchies (6+ levels) and may cause compatibility issues with some software like ClipStudioPaint.

    SelectionDividerType

    Determines the folder state or position:

    • 0: AnyOther (Unknown type)
    • 1: OpenFolder (Indicates the end of an lsct block where the folder was open)
    • 2: ClosedFolder (Indicates the end of an lsct block where the folder was closed)
    • 3: BoundingSectionDivider (Indicates the start of a layer folder)

    Data Layout

    If AdditionalLayerInformation.Length is $\ge 12$:

    • 4 bytes: Signature (8BIM)
    • 4 bytes: BlendModeKey (ASCII-String)

    If AdditionalLayerInformation.Length is $\ge 16$:

    • 4 bytes: SubType (0 = Normal, 1 = Scene Group/Animation timeline related)
    // SelectionDividerType Enum values
    // 0: AnyOther
    // 1: OpenFolder
    // 2: ClosedFolder
    // 3: BoundingSectionDivider
  3. Use IslandSelector for advanced culling and selection

    master

    Introduced in v0.7.0, IslandSelector provides a robust way to select or cull specific parts of a mesh (islands).

    • Types of Selectors:
      • Box(Sphere)IslandSelector: Selects islands based on spatial bounds. Includes an IsAll option.
      • SubMeshIslandSelector: Selects based on submesh boundaries.
      • IslandSelectorOR and IslandSelectorNOT: Logical operators for combining selection criteria.
      • RendererIslandSelector: Selects based on renderer properties.
    • Integration:
      • SimpleDecal now uses IslandSelector for its experimental culling functionality.
      • Migration Note: If you were previously using SimpleDecal.IslandCulling, you should migrate to using IslandSelector as IslandCulling was removed in v0.7.0.
  4. Understand the LayerAndMaskInformation section

    master

    The LayerAndMaskInformation section is the most critical part of a PSD file, containing layer information and pixel data. It consists of three potential components:

    1. LayerInfo: Contains the actual layer metadata and raster data.
    2. GlobalLayerMaskInfo: Contains global mask information.
    3. CanvasType-AdditionalLayerInformationArray: Additional information applied to the canvas.

    Important Implementation Note: This section may contain only LayerInfo. You must check the LayerAndMaskInformationLength to ensure you do not attempt to read GlobalLayerMaskInfo or AdditionalLayerInformationArray if the length has already been exhausted by the LayerInfo section.

    |Byte|Description|
    |---|---|
    |PSD 4(uint) or PSB 8(ulong)|LayerAndMaskInformationLength: 下記三つのの info すべての長さ。|
    |Variable = [後述参照](#layerinfo)|LayerInfo: レイヤー情報。 [後述参照](#layerinfo)|
    |Variable = [後述参照](#globallayermaskinfo)|GlobalLayerMaskInfo: [後述参照](#globallayermaskinfo)|
    |Variable = Unknown|CanvasType-AdditionalLayerInformationArray: レイヤー追加情報がキャンバスに対して付与されることがあるようです...は??? 恐らく LayerAndMaskInformationLength が尽きるまで並んでいる。[後述参照](#additionallayerinformation)|
  5. Understand the structure of AdditionalLayerInformation

    master

    Additional layer information in PSD files follows a specific block structure. This allows for extensible metadata attached to layer records.

    ByteDescription
    4 (ASCII-String)Signature: Either 8BIM or 8B64
    4 (ASCII-String)Key: A key code identifying the type of additional information
    4 (uint) or 8 (ulong)Length: The length of the data. Note: For PSB files, if the Key is one of the following, the Length is 8 bytes (ulong): LMsk, Lr16, Lr32, Layr, Mt16, Mt32, Mtrn, Alph, FMsk, lnk2, FEid, FXid, PxSD
    Variable = LengthData: The actual payload corresponding to the Key

    Note: For canvas-attached information, the Length is typically rounded up to the nearest multiple of 4.

  6. Handle RectTangle coordinates

    master

    The RectTangle defines a rectangular area using Top, Left, Bottom, and Right integers.

    Coordinate System: The PSD coordinate system uses the top-left as the origin (0,0) and increases towards the bottom-right.

    For a 2048 × 2048 canvas, a rectangle covering the entire canvas would be:

    • Top = 0
    • Left = 0
    • Bottom = 2048
    • Right = 2048
    |Byte|Description|
    |---|---|
    |4(int)|Top:|
    |4(int)|Left:|
    |4(int)|Bottom:|
    |4(int)|Right:|
  7. Parse ColorModeData

    master

    The ColorModeData section follows the FileHeader. Its existence depends on the ColorMode value.

    Structure:

    • 4 (uint): ColorModeData-Length (The total length of the following data).
    • Variable: ColorData (The actual color data).

    Behavior:

    • If ColorMode is 2 (Indexed) or 8 (Duotone), ColorData contains specific information.
    • For all other modes, ColorData is zero. In these cases, after reading the 4-byte ColorModeData-Length, you should immediately skip to the ImageResources section.
  8. Understand the PSD File Structure

    master

    A PSD file follows a specific sequential structure. When parsing, you must read these five components in order:

    1. FileHeader: Contains basic metadata like signature, version, and canvas dimensions.
    2. ColorModeData: Contains color-specific information (only present if ColorMode requires it).
    3. ImageResources: A collection of metadata blocks (e.g., software info).
    4. LayerAndMaskInformation: Contains layer-specific data.
    5. ImageData: The actual pixel data.

    Important Implementation Notes:

    • Most numerical values are stored in BigEndian format.
    • Unless otherwise specified, numbers should be treated as unsigned.
  9. Import PSD files with TTT PSD Importer

    master

    The TTT PSD Importer supports advanced Photoshop features:

    • Layer Support: Imports raster layers, including those without alpha (fixed in v0.7.1).
    • Blend Modes: Supports all PSD blend modes, including Additive (Luminosity) and Overlay (Luminosity) (added in v0.6.0).
    • Adjustment Layers: Imports LevelAdjustmentLayer and SelectiveColorAdjustment (added in v0.6.0).
    • Clip Studio Paint Compatibility: Automatically handles color composition for PSDs exported from Clip Studio Paint (added in v0.7.0).
    • Performance: Preview generation for imported PSDs is significantly optimized (as of v0.7.0).
  10. Adjust ActualDataSizeFollows for padding

    master

    The ActualDataSizeFollows field in an ImageResourceBlock is used to align data to a multiple of two. The value provided in the file might not be the true length of the ResourceData. To get the correct length, you must round the value up to the nearest even number:

    var trueLength = (ActualDataSizeFollows % 2 ) == 0  ? ActualDataSizeFollows : ActualDataSizeFollows + 1 ;
  11. Handle PascalString-ImageResourceBlockName

    master

    The ImageResourceBlockName is stored as a Pascal String.

    Encoding:

    • In Japanese locale PSDs, the character encoding is typically shift-jis. For other locales, the encoding is unspecified.

    Null Terminator Logic: When reading the string based on the PascalStringLength byte, be aware of how null terminators are handled. A null byte may or may not exist depending on the length:

    if (PascalString-Length == 0) { /* null byte exists. */ }
    else if ( ( PascalString-Length % 2 ) == 0) { /* null byte exists. */ }
    else { /* null byte does NOT exist. */ }
    /* Logic for determining null byte existence in Pascal Strings */
  12. Configure TTT PSD Importer ImportMode

    master

    The TTT PSD Importer allows you to override the detected source software of a PSD file using the ImportMode setting. This is useful for ensuring that PassThrough or Clipping behaviors are correctly adjusted based on the expected software output. Supported modes include SAI (for Paint Tool SAI) and ClipStudioPaint (for Clip Studio Paint).

    ImportMode: [ClipStudioPaint, SAI, ...]