fix: normalize exported texture filenames
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { getAssetFamily } from './asset-naming'
|
||||
|
||||
export type AssetCategory = 'color' | 'diffuse' | 'roughness' | 'normal' | 'metalness' | 'opacity' | 'assets'
|
||||
export type AssetCategory = 'color' | 'diffuse' | 'roughness' | 'normal' | 'metalness' | 'height' | 'opacity' | 'orm' | 'ao' | 'assets'
|
||||
|
||||
export function classifyAssetCategory(filename: string): AssetCategory {
|
||||
const name = filename.replace(/\.[^.]+$/, '')
|
||||
@@ -26,9 +26,21 @@ export function classifyAssetCategory(filename: string): AssetCategory {
|
||||
return 'metalness'
|
||||
}
|
||||
|
||||
if (family === 'height') {
|
||||
return 'height'
|
||||
}
|
||||
|
||||
if (family === 'opacity') {
|
||||
return 'opacity'
|
||||
}
|
||||
|
||||
if (family === 'orm') {
|
||||
return 'orm'
|
||||
}
|
||||
|
||||
if (family === 'ao') {
|
||||
return 'ao'
|
||||
}
|
||||
|
||||
return 'assets'
|
||||
}
|
||||
|
||||
+77
-1
@@ -6,6 +6,8 @@ export const ASSET_FAMILIES = [
|
||||
'metalness',
|
||||
'height',
|
||||
'opacity',
|
||||
'orm',
|
||||
'ao',
|
||||
] as const
|
||||
|
||||
export type AssetFamily = typeof ASSET_FAMILIES[number]
|
||||
@@ -21,6 +23,23 @@ const FORBIDDEN_ASSET_FAMILY_ALIASES: ReadonlyMap<string, AssetFamily> = new Map
|
||||
['occlusion_roughness_metallic', 'roughness'],
|
||||
])
|
||||
|
||||
const EXPORTED_SUFFIX_ALIASES: Array<{ suffix: string; family: AssetFamily }> = [
|
||||
{ suffix: 'occlusionroughnessmetallic', family: 'orm' },
|
||||
{ suffix: 'occlusion_roughness_metallic', family: 'orm' },
|
||||
{ suffix: 'normal_opengl', family: 'normal' },
|
||||
{ suffix: 'normalopengl', family: 'normal' },
|
||||
{ suffix: 'base_color', family: 'color' },
|
||||
{ suffix: 'basecolor', family: 'color' },
|
||||
{ suffix: 'mixed_ao', family: 'ao' },
|
||||
{ suffix: 'metallic', family: 'metalness' },
|
||||
{ suffix: 'roughness', family: 'roughness' },
|
||||
{ suffix: 'normal', family: 'normal' },
|
||||
{ suffix: 'height', family: 'height' },
|
||||
{ suffix: 'opacity', family: 'opacity' },
|
||||
{ suffix: 'diffuse', family: 'diffuse' },
|
||||
{ suffix: 'color', family: 'color' },
|
||||
]
|
||||
|
||||
export function getAssetFamily(value: string): AssetFamily | undefined {
|
||||
return ASSET_FAMILY_BY_KEY.get(value.toLowerCase())
|
||||
}
|
||||
@@ -33,11 +52,68 @@ function getFileStem(filename: string) {
|
||||
return filename.replace(/\.[^.]+$/, '')
|
||||
}
|
||||
|
||||
function getFileExtension(filename: string) {
|
||||
return filename.split('.').pop()?.toLowerCase() || ''
|
||||
}
|
||||
|
||||
function normalizeTargetName(target: string) {
|
||||
return target
|
||||
.trim()
|
||||
.replace(/[\s-]+/g, '_')
|
||||
.replace(/_+/g, '_')
|
||||
.replace(/^_+|_+$/g, '')
|
||||
}
|
||||
|
||||
function buildTextureFilename(family: AssetFamily, target: string, extension: string) {
|
||||
const normalizedTarget = normalizeTargetName(target)
|
||||
return `${family}${normalizedTarget ? `_${normalizedTarget}` : ''}.${extension}`
|
||||
}
|
||||
|
||||
function getExportedTextureAlias(stem: string) {
|
||||
const lowerStem = stem.toLowerCase()
|
||||
|
||||
for (const alias of EXPORTED_SUFFIX_ALIASES) {
|
||||
const lowerSuffix = alias.suffix.toLowerCase()
|
||||
if (lowerStem === lowerSuffix) {
|
||||
return { family: alias.family, target: '' }
|
||||
}
|
||||
|
||||
if (lowerStem.endsWith(`_${lowerSuffix}`)) {
|
||||
return {
|
||||
family: alias.family,
|
||||
target: stem.slice(0, -lowerSuffix.length - 1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function normalizeTextureFilename(filename: string) {
|
||||
const stem = getFileStem(filename)
|
||||
const extension = getFileExtension(filename)
|
||||
const [prefix, ...targetParts] = stem.split('_')
|
||||
const family = getAssetFamily(prefix)
|
||||
|
||||
if (family) {
|
||||
return buildTextureFilename(family, targetParts.join('_'), extension)
|
||||
}
|
||||
|
||||
const exportedAlias = getExportedTextureAlias(stem)
|
||||
if (exportedAlias) {
|
||||
return buildTextureFilename(exportedAlias.family, exportedAlias.target, extension)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function getTextureNamingError(filename: string) {
|
||||
const stem = getFileStem(filename)
|
||||
const [prefix, ...targetParts] = stem.split('_')
|
||||
const family = getAssetFamily(prefix)
|
||||
const extension = filename.split('.').pop()
|
||||
const extension = getFileExtension(filename)
|
||||
|
||||
if (normalizeTextureFilename(filename)) return null
|
||||
|
||||
if (family && targetParts.every(Boolean)) return null
|
||||
|
||||
|
||||
@@ -62,11 +62,14 @@ export function buildCommitMessage(
|
||||
roughness: '🪶 Textures (roughness)',
|
||||
normal: '🧭 Textures (normal)',
|
||||
metalness: '🔩 Textures (metalness)',
|
||||
height: '⛰ Textures (height)',
|
||||
opacity: '🪟 Textures (opacity)',
|
||||
orm: '🧱 Textures (orm)',
|
||||
ao: '🌑 Textures (ao)',
|
||||
assets: '🧩 Assets',
|
||||
}
|
||||
|
||||
for (const category of ['color', 'diffuse', 'roughness', 'normal', 'metalness', 'opacity', 'assets'] as const) {
|
||||
for (const category of ['color', 'diffuse', 'roughness', 'normal', 'metalness', 'height', 'opacity', 'orm', 'ao', 'assets'] as const) {
|
||||
const entries = grouped.get(category)
|
||||
if (!entries || entries.length === 0) continue
|
||||
lines.push('')
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { extname } from 'path'
|
||||
import { compressTextureBuffer } from '@/lib/texture-compression'
|
||||
import { classifyAssetCategory } from '@/lib/asset-classification'
|
||||
import { normalizeTextureFilename } from '@/lib/asset-naming'
|
||||
import { TEXTURE_EXTENSIONS } from '@/lib/constants'
|
||||
import { getModelAssetPath } from '@/lib/model-paths'
|
||||
import type { ParsedFile, PreparedAssetSummary, PushFile } from '@/lib/types'
|
||||
|
||||
@@ -16,6 +19,65 @@ interface PrepareGitAssetsResult {
|
||||
compressionError?: string
|
||||
}
|
||||
|
||||
function getTextureFilenameMap(parsedFiles: ParsedFile[]) {
|
||||
const filenameMap = new Map<string, string>()
|
||||
const normalizedOwners = new Map<string, string>()
|
||||
|
||||
for (const file of parsedFiles) {
|
||||
const ext = extname(file.filename).toLowerCase()
|
||||
if (!TEXTURE_EXTENSIONS.has(ext)) continue
|
||||
|
||||
const normalizedFilename = normalizeTextureFilename(file.filename)
|
||||
if (!normalizedFilename) continue
|
||||
|
||||
const normalizedKey = normalizedFilename.toLowerCase()
|
||||
const existingOwner = normalizedOwners.get(normalizedKey)
|
||||
|
||||
if (existingOwner && existingOwner.toLowerCase() !== file.filename.toLowerCase()) {
|
||||
throw new Error(`Textures en conflit apres normalisation : ${existingOwner} et ${file.filename} deviennent ${normalizedFilename}`)
|
||||
}
|
||||
|
||||
filenameMap.set(file.filename.toLowerCase(), normalizedFilename)
|
||||
normalizedOwners.set(normalizedKey, file.filename)
|
||||
}
|
||||
|
||||
return filenameMap
|
||||
}
|
||||
|
||||
function getReferencedFilename(uri: string) {
|
||||
const cleanUri = decodeURIComponent(uri.split(/[?#]/)[0] || '')
|
||||
return cleanUri.split(/[\\/]/).pop()?.toLowerCase()
|
||||
}
|
||||
|
||||
function rewriteGltfUris(value: unknown, filenameMap: Map<string, string>): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((entry) => rewriteGltfUris(entry, filenameMap))
|
||||
}
|
||||
|
||||
if (!value || typeof value !== 'object') return value
|
||||
|
||||
const rewritten: Record<string, unknown> = {}
|
||||
|
||||
for (const [key, entry] of Object.entries(value)) {
|
||||
if (key === 'uri' && typeof entry === 'string') {
|
||||
const filename = getReferencedFilename(entry)
|
||||
rewritten[key] = filename ? filenameMap.get(filename) || entry : entry
|
||||
continue
|
||||
}
|
||||
|
||||
rewritten[key] = rewriteGltfUris(entry, filenameMap)
|
||||
}
|
||||
|
||||
return rewritten
|
||||
}
|
||||
|
||||
function prepareModelBuffer(buffer: Buffer, filenameMap: Map<string, string>) {
|
||||
if (filenameMap.size === 0) return buffer
|
||||
|
||||
const parsed: unknown = JSON.parse(buffer.toString('utf-8'))
|
||||
return Buffer.from(JSON.stringify(rewriteGltfUris(parsed, filenameMap), null, 2), 'utf-8')
|
||||
}
|
||||
|
||||
export async function prepareGitAssets({
|
||||
folderName,
|
||||
parsedFiles,
|
||||
@@ -25,22 +87,26 @@ export async function prepareGitAssets({
|
||||
let modelFilename = ''
|
||||
let compressed = false
|
||||
let compressionError: string | undefined
|
||||
const textureFilenameMap = getTextureFilenameMap(parsedFiles)
|
||||
|
||||
for (const pf of parsedFiles) {
|
||||
let content = pf.buffer
|
||||
let filename = pf.filename
|
||||
|
||||
if (pf.isModel) {
|
||||
content = prepareModelBuffer(pf.buffer, textureFilenameMap)
|
||||
modelFilename = pf.filename
|
||||
|
||||
assetSummaries.push({
|
||||
filename: pf.filename,
|
||||
filename,
|
||||
kind: 'model',
|
||||
compressed: false,
|
||||
})
|
||||
} else {
|
||||
const category = classifyAssetCategory(pf.filename)
|
||||
filename = textureFilenameMap.get(pf.filename.toLowerCase()) || pf.filename
|
||||
const category = classifyAssetCategory(filename)
|
||||
|
||||
const textureResult = await compressTextureBuffer(pf.filename, pf.buffer)
|
||||
const textureResult = await compressTextureBuffer(filename, pf.buffer)
|
||||
content = textureResult.buffer
|
||||
compressed ||= textureResult.compressed
|
||||
|
||||
@@ -49,7 +115,7 @@ export async function prepareGitAssets({
|
||||
}
|
||||
|
||||
assetSummaries.push({
|
||||
filename: pf.filename,
|
||||
filename,
|
||||
kind: category === 'assets' ? 'asset' : 'texture',
|
||||
category,
|
||||
compressed: textureResult.compressed,
|
||||
@@ -57,7 +123,7 @@ export async function prepareGitAssets({
|
||||
}
|
||||
|
||||
filesToPush.push({
|
||||
path: getModelAssetPath(folderName, pf.filename),
|
||||
path: getModelAssetPath(folderName, filename),
|
||||
contentBase64: content.toString('base64'),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -139,9 +139,10 @@ async function readOriginalParsedFiles(stagingId: string, manifest: StagingManif
|
||||
|
||||
async function buildPreparedPushFiles(stagingId: string, manifest: StagingManifest): Promise<PushFile[]> {
|
||||
const preparedDir = getPreparedDir(stagingId)
|
||||
const preparedFiles = manifest.prepared?.assetSummaries || []
|
||||
|
||||
return Promise.all(
|
||||
manifest.originals.map(async (file) => {
|
||||
preparedFiles.map(async (file) => {
|
||||
const buffer = await readFile(join(preparedDir, file.filename))
|
||||
return {
|
||||
path: getModelAssetPath(manifest.folderName, file.filename),
|
||||
|
||||
Reference in New Issue
Block a user