// engine/check/texture-budget.js - written by Muse Glimmer (Phase 32, part C, 27 Sep 2026; raw output in game/muse-authored/phase32/raw/), // reviewed by Grok Bot (coach) with game/muse-authored/phase32/harness/harness_p32c.mjs. Coach fixes are marked [coach fix P32-n], see FIXES-P32.md. (function () { var LIMITS = { uploadMax: 1024, largeWarn: 2048, memWarnMB: 256 }; function pot(x) { var v = Math.max(1, x); var p = Math.pow(2, Math.round(Math.log2(v))); return p; } function isPowerOfTwo(n) { return n > 0 && (n & (n - 1)) === 0; } function analyze(textures, opts) { opts = opts || {}; var uploadMax = typeof opts.uploadMax === 'number' && opts.uploadMax > 0 ? opts.uploadMax : LIMITS.uploadMax; var largeWarn = typeof opts.largeWarn === 'number' && opts.largeWarn > 0 ? opts.largeWarn : LIMITS.largeWarn; var memWarnMB = typeof opts.memWarnMB === 'number' && opts.memWarnMB > 0 ? opts.memWarnMB : LIMITS.memWarnMB; var kept = []; if (!Array.isArray(textures)) textures = []; // [coach fix P32-11] analyze(null) / analyze() threw; the engine may have no texture list for (var i = 0; i < textures.length; i++) { var t = textures[i]; if (typeof t !== 'object' || t === null) continue; var w = t.width, h = t.height; if (typeof w !== 'number' || typeof h !== 'number') continue; if (w <= 0 || h <= 0) continue; kept.push({ name: t.name, width: w, height: h }); } var count = kept.length; var totalDecoded = 0; var totalUpload = 0; var largest = null; var over = []; var npot = []; for (var j = 0; j < count; j++) { var tex = kept[j]; var w = tex.width, h = tex.height; var decodedMB = (w * h * 4) / 1048576; totalDecoded += decodedMB; var pw = Math.min(uploadMax, pot(w)); var ph = Math.min(uploadMax, pot(h)); var uploadMB = (pw * ph * 4 * 4) / 3 / 1048576; totalUpload += uploadMB; if (!largest || w * h > largest.width * largest.height) { largest = { name: tex.name, width: w, height: h }; } if (Math.max(w, h) > largeWarn) over.push(tex.name); if (!isPowerOfTwo(w) || !isPowerOfTwo(h)) npot.push(tex.name); } function round1(x) { return Math.round(x * 10) / 10; } var totalDecodedMB = round1(totalDecoded); var totalUploadMB = round1(totalUpload); var warnings = []; if (over.length > 0) { var L = null; for (var k = 0; k < kept.length; k++) { var tex = kept[k]; if (over.indexOf(tex.name) !== -1) { if (!L || tex.width * tex.height > L.width * L.height) L = tex; } } warnings.push({ code: 'LARGE_TEXTURES', level: 'warn', msg: over.length + ' texture(s) larger than ' + largeWarn + ' px (largest: ' + L.name + ' ' + L.width + 'x' + L.height + '): the engine shrinks every texture to ' + uploadMax + ' px, so the extra size only costs memory and loading time.', fix: 'Export 1024 or 2048 px maps (3ds Max: Render to Texture / resize the bitmaps; Maya: resize the file textures) before exporting.', }); } if (totalDecoded > memWarnMB) { warnings.push({ code: 'TEXTURE_MEMORY', level: 'warn', msg: 'Textures need about ' + Math.round(totalDecoded) + ' MB of memory while loading (' + count + ' images).', fix: 'Use fewer or smaller textures, or share one atlas between parts.', }); } if (npot.length > 0) { var first = null; for (var m = 0; m < kept.length; m++) { var tex = kept[m]; if (npot.indexOf(tex.name) !== -1) { first = tex; break; } } warnings.push({ code: 'NPOT_TEXTURES', level: 'info', msg: npot.length + ' texture(s) are not a power of two (e.g. ' + first.name + ' ' + first.width + 'x' + first.height + '): they are resized on upload and may blur slightly.', fix: 'Use power-of-two sizes such as 512, 1024 or 2048.', }); } return { count: count, totalDecodedMB: totalDecodedMB, totalUploadMB: totalUploadMB, largest: largest, over: over, npot: npot, warnings: warnings, }; } window.MuseTextureBudget = { analyze: analyze, LIMITS: LIMITS }; if (typeof module !== 'undefined') module.exports = window.MuseTextureBudget; })();