374 字
2 分钟
Extra Decal Pre Processor

Prerequisite#

  1. Cities: Skylines 2
  2. ExtraAssetsImporter
  3. PowerShell 7
WINDOWS EMBEDED POWERSHELL IS OUTDATED

The powershell that comes with windows is powershell 5, which doesn’t fully realize the functionality of 7!

Step#

  1. Setup Mod toolchain in CS2, basically check it in game.
  2. Follow the tutorial to create mods that depend on EAI, this article does not focus on these.
  3. Build decals.

Let’s assume a scenario where you have a folder for all your HD decals (.png) and you need to create uniform decals and thumbnails based on these decals.

So you have:

D:\lorem
├─blahblah
│      a.png
│      b1.png
│      b2.png
│      b3.png
│      b4.png
│      b1.png
│      b2.png

decal.json can be obtained from the EAI example.

Place build.ps1 and decal.json to this directory. Then:

D:\lorem
├─blahblah
│      build.ps1
│      decal.json
│      a.png
│      b1.png
│      b2.png
│      b3.png
│      b4.png
│      b1.png
│      b2.png

Content in build.ps1#

function GenerateIconAndMesh {
    param(
        $file
    )
    
    try {
        $originalImage = [System.Drawing.Image]::FromFile($file.FullName)
        $maxSize = [Math]::Max($originalImage.Width, $originalImage.Height)
        $aspectRatio = $originalImage.Width / $originalImage.Height
        $squareImage = New-Object System.Drawing.Bitmap $maxSize, $maxSize

        $graphics = [System.Drawing.Graphics]::FromImage($squareImage)
        $graphics.Clear([System.Drawing.Color]::Transparent)

        $x = ($maxSize - $originalImage.Width) / 2
        $y = ($maxSize - $originalImage.Height) / 2

        $graphics.DrawImage($originalImage, $x, $y, $originalImage.Width, $originalImage.Height)

        $resizedImage = New-Object System.Drawing.Bitmap 128, 128
        $resizeGraphics = [System.Drawing.Graphics]::FromImage($resizedImage)
        $resizeGraphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic

        $resizeGraphics.DrawImage($squareImage, 0, 0, 128, 128)
        $resizeGraphics.Dispose()

        $newFilePath = [IO.Path]::Combine($file.DirectoryName, "icon.png")
        $resizedImage.Save($newFilePath)

        Write-Host "Saved icon to $newFilePath"

        $jsonPath = Join-Path $file.DirectoryName "decal.json"

        if (Test-Path $jsonPath) {
            $json = Get-Content $jsonPath -Raw | ConvertFrom-Json
            
            $json.Vector.colossal_MeshSize.x = $aspectRatio * 1.5
            $json.Vector.colossal_MeshSize.z = 1.5

            $json | ConvertTo-Json -Depth 10 | Set-Content $jsonPath
            
            Write-Host "Aspect Ratio = $aspectRatio"
        }
        else {
            Write-Warning "decal.json not found in the directory of $($file.DirectoryName)"
        }
    }
    catch {
        Write-Warning "Error processing file $($file.FullName): $_"
    }
    finally {
        # dispose
        if ($null -ne $graphics) {
            $graphics.Dispose()
        }
        if ($null -ne $squareImage) {
            $squareImage.Dispose()
        }
        if ($null -ne $resizedImage) {
            $resizedImage.Dispose()
        }
        if ($null -ne $originalImage) {
            $resizedImage.Dispose()
        }
    }
}

Get-ChildItem | Foreach-Object {
    if ($_.Extension -eq ".png") {
        $folder = "./$($_.BaseName)"
        mkdir $folder -ErrorAction SilentlyContinue
        $base = "./$($_.BaseName)/_BaseColorMap.png"
        Copy-Item $_.FullName -Destination $base
        # $icon = "./$($_.BaseName)/icon.png"
        # Copy-Item $_.FullName -Destination $icon
        if (Test-Path ./decal.json) {
            Copy-Item ./decal.json -Destination $folder
        }
        else {
            Write-Warning "decal.json not found"
        }

        GenerateIconAndMesh(Get-Item $base)
    }
}

Run this script in a terminal. A quick way to open a terminal is to type pwsh in explorer’s bar.

./build.ps1

Now you’ve got everything done.

Extra Decal Pre Processor
https://nptr.cc/posts/cities2-extra-decal-preprocessor/
作者
Nullpinter
发布于
2024-06-08