untitled-pixi-live2d-engine 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cubism.es.js CHANGED
@@ -1,4 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
2
4
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
4
6
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -15,6 +17,7 @@ var __spreadValues = (a, b) => {
15
17
  }
16
18
  return a;
17
19
  };
20
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
18
21
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
19
22
  var __async = (__this, __arguments, generator) => {
20
23
  return new Promise((resolve, reject) => {
@@ -36,7 +39,7 @@ var __async = (__this, __arguments, generator) => {
36
39
  step((generator = generator.apply(__this, __arguments)).next());
37
40
  });
38
41
  };
39
- import { EventEmitter, Matrix, loadTextures, Assets, Ticker, Transform, Point, Bounds, Container, ObservablePoint, WebGLRenderer } from "pixi.js";
42
+ import { EventEmitter, Matrix, loadTextures, Assets, Ticker, Transform, Point, Bounds, ExtensionType, Container, ObservablePoint, WebGLRenderer, DOMAdapter, Texture, CanvasSource } from "pixi.js";
40
43
  import { sound, Sound, webaudio } from "@pixi/sound";
41
44
  const _csmVector = class _csmVector {
42
45
  /**
@@ -2757,7 +2760,7 @@ const config = {
2757
2760
  preserveExpressionOnMotion: true,
2758
2761
  cubism: CubismFrameworkConfig
2759
2762
  };
2760
- const VERSION = "1.0.2";
2763
+ const VERSION = "1.2.0";
2761
2764
  const logger = {
2762
2765
  log(tag, ...messages) {
2763
2766
  if (config.logLevel <= config.LOG_LEVEL_VERBOSE) {
@@ -2775,7 +2778,7 @@ const logger = {
2775
2778
  }
2776
2779
  }
2777
2780
  };
2778
- function clamp(num, lower, upper) {
2781
+ function clamp$1(num, lower, upper) {
2779
2782
  return num < lower ? lower : num > upper ? upper : num;
2780
2783
  }
2781
2784
  function rand(min, max) {
@@ -3050,8 +3053,8 @@ class FocusController {
3050
3053
  * @param instant - Should the focus position be instantly applied.
3051
3054
  */
3052
3055
  focus(x, y, instant = false) {
3053
- this.targetX = clamp(x, -1, 1);
3054
- this.targetY = clamp(y, -1, 1);
3056
+ this.targetX = clamp$1(x, -1, 1);
3057
+ this.targetY = clamp$1(y, -1, 1);
3055
3058
  if (instant) {
3056
3059
  this.x = this.targetX;
3057
3060
  this.y = this.targetY;
@@ -4568,6 +4571,68 @@ class CubismLegacyModelSettings extends ModelSettings {
4568
4571
  }
4569
4572
  }
4570
4573
  }
4574
+ const DEFAULT_LOD_SCALE_THRESHOLD = 0.5;
4575
+ const DEFAULT_LOD_TEXTURE_SIZE_THRESHOLD = 4096;
4576
+ function getTextureLODPlan({
4577
+ lod,
4578
+ effectiveScale,
4579
+ textureWidth,
4580
+ textureHeight,
4581
+ lodScaleThreshold,
4582
+ lodTextureSizeThreshold,
4583
+ lodMaxLevel
4584
+ }) {
4585
+ if (getTextureLODMode(lod) !== "single-auto") {
4586
+ return void 0;
4587
+ }
4588
+ if (!Number.isFinite(effectiveScale) || effectiveScale <= 0) {
4589
+ return void 0;
4590
+ }
4591
+ const width = Math.floor(textureWidth);
4592
+ const height = Math.floor(textureHeight);
4593
+ if (width <= 1 || height <= 1) {
4594
+ return void 0;
4595
+ }
4596
+ const scaleThreshold = validPositive(lodScaleThreshold, DEFAULT_LOD_SCALE_THRESHOLD);
4597
+ if (effectiveScale >= scaleThreshold) {
4598
+ return void 0;
4599
+ }
4600
+ const textureSizeThreshold = validPositive(
4601
+ lodTextureSizeThreshold,
4602
+ DEFAULT_LOD_TEXTURE_SIZE_THRESHOLD
4603
+ );
4604
+ if (Math.max(width, height) < textureSizeThreshold) {
4605
+ return void 0;
4606
+ }
4607
+ const rawLevel = Math.round(Math.log2(1 / effectiveScale));
4608
+ const maxDimensionLevel = Math.max(1, Math.floor(Math.log2(Math.max(width, height))));
4609
+ const maxLevel = Math.min(maxDimensionLevel, validMaxLevel(lodMaxLevel, maxDimensionLevel));
4610
+ const level = clamp(Math.max(1, rawLevel), 1, maxLevel);
4611
+ const factor = __pow(2, level);
4612
+ return {
4613
+ level,
4614
+ width: Math.max(1, Math.round(width / factor)),
4615
+ height: Math.max(1, Math.round(height / factor))
4616
+ };
4617
+ }
4618
+ function getTextureLODMode(lod) {
4619
+ if (lod === false || lod === "single-auto") {
4620
+ return lod;
4621
+ }
4622
+ return "full";
4623
+ }
4624
+ function getTextureLODFilter(filter) {
4625
+ return filter === "nearest" ? "nearest" : "linear";
4626
+ }
4627
+ function validPositive(value, fallback) {
4628
+ return value !== void 0 && Number.isFinite(value) && value > 0 ? value : fallback;
4629
+ }
4630
+ function validMaxLevel(value, fallback) {
4631
+ return value !== void 0 && Number.isFinite(value) && value >= 1 ? Math.floor(value) : fallback;
4632
+ }
4633
+ function clamp(value, min, max) {
4634
+ return Math.min(max, Math.max(min, value));
4635
+ }
4571
4636
  function createTexture(url, options = {}) {
4572
4637
  const config2 = loadTextures.config;
4573
4638
  const previousCrossOrigin = config2 == null ? void 0 : config2.crossOrigin;
@@ -4580,7 +4645,12 @@ function createTexture(url, options = {}) {
4580
4645
  config2.preferCreateImageBitmap = false;
4581
4646
  config2.preferWorkers = false;
4582
4647
  }
4583
- return Assets.load(url).catch((error) => {
4648
+ return Assets.load({
4649
+ src: url,
4650
+ data: {
4651
+ autoGenerateMipmaps: getTextureLODMode(options.lod) === "full"
4652
+ }
4653
+ }).catch((error) => {
4584
4654
  if (error instanceof Error) {
4585
4655
  throw error;
4586
4656
  }
@@ -4713,9 +4783,9 @@ const setupOptionals = (context, next) => __async(null, null, function* () {
4713
4783
  const setupEssentials = (context, next) => __async(null, null, function* () {
4714
4784
  if (context.settings) {
4715
4785
  const live2DModel = context.live2dModel;
4716
- const textureOptions = {
4786
+ const textureOptions = __spreadValues({
4717
4787
  crossOrigin: context.options.crossOrigin
4718
- };
4788
+ }, context.options.textureOptions);
4719
4789
  if (context.settings instanceof CubismLegacyModelSettings) {
4720
4790
  textureOptions.preferCreateImageBitmap = false;
4721
4791
  }
@@ -4761,114 +4831,6 @@ const createInternalModel = (context, next) => __async(null, null, function* ()
4761
4831
  }
4762
4832
  throw new TypeError("Missing settings.");
4763
4833
  });
4764
- const _ZipLoader = class _ZipLoader {
4765
- static unzip(reader, settings) {
4766
- return __async(this, null, function* () {
4767
- const filePaths = yield _ZipLoader.getFilePaths(reader);
4768
- const requiredFilePaths = [];
4769
- for (const definedFile of settings.getDefinedFiles()) {
4770
- const actualPath = decodeURI(resolveURL(settings.url, definedFile));
4771
- if (filePaths.includes(actualPath)) {
4772
- requiredFilePaths.push(actualPath);
4773
- }
4774
- }
4775
- const files = yield _ZipLoader.getFiles(reader, requiredFilePaths);
4776
- for (let i = 0; i < files.length; i++) {
4777
- const path = requiredFilePaths[i];
4778
- const file = files[i];
4779
- Object.defineProperty(file, "webkitRelativePath", {
4780
- value: path
4781
- });
4782
- }
4783
- return files;
4784
- });
4785
- }
4786
- static createSettings(reader) {
4787
- return __async(this, null, function* () {
4788
- const filePaths = yield _ZipLoader.getFilePaths(reader);
4789
- const settingsFilePath = filePaths.find(
4790
- (path) => path.endsWith("model.json") || path.endsWith("model3.json")
4791
- );
4792
- if (!settingsFilePath) {
4793
- throw new Error("Settings file not found");
4794
- }
4795
- const settingsText = yield _ZipLoader.readText(reader, settingsFilePath);
4796
- if (!settingsText) {
4797
- throw new Error("Empty settings file: " + settingsFilePath);
4798
- }
4799
- const settingsJSON = JSON.parse(settingsText);
4800
- settingsJSON.url = settingsFilePath;
4801
- const runtime = _ZipLoader.live2dFactory.findRuntime(settingsJSON);
4802
- if (!runtime) {
4803
- throw new Error("Unknown settings JSON");
4804
- }
4805
- return runtime.createModelSettings(settingsJSON);
4806
- });
4807
- }
4808
- static zipReader(data, url) {
4809
- return Promise.reject(new Error("Not implemented"));
4810
- }
4811
- static getFilePaths(reader) {
4812
- return Promise.reject(new Error("Not implemented"));
4813
- }
4814
- static getFiles(reader, paths) {
4815
- return Promise.reject(new Error("Not implemented"));
4816
- }
4817
- static readText(reader, path) {
4818
- return Promise.reject(new Error("Not implemented"));
4819
- }
4820
- static releaseReader(reader) {
4821
- }
4822
- };
4823
- // will be set by Live2DFactory
4824
- __publicField(_ZipLoader, "live2dFactory");
4825
- __publicField(_ZipLoader, "ZIP_PROTOCOL", "zip://");
4826
- __publicField(_ZipLoader, "uid", 0);
4827
- __publicField(_ZipLoader, "factory", (context, next) => __async(null, null, function* () {
4828
- const source = context.source;
4829
- let sourceURL;
4830
- let zipBlob;
4831
- let settings;
4832
- if (typeof source === "string" && (source.endsWith(".zip") || source.startsWith(_ZipLoader.ZIP_PROTOCOL))) {
4833
- if (source.startsWith(_ZipLoader.ZIP_PROTOCOL)) {
4834
- sourceURL = source.slice(_ZipLoader.ZIP_PROTOCOL.length);
4835
- } else {
4836
- sourceURL = source;
4837
- }
4838
- zipBlob = yield Live2DLoader.load({
4839
- url: sourceURL,
4840
- type: "blob",
4841
- target: context.live2dModel
4842
- });
4843
- } else if (Array.isArray(source) && source.length === 1 && source[0] instanceof File && source[0].name.endsWith(".zip")) {
4844
- zipBlob = source[0];
4845
- sourceURL = URL.createObjectURL(zipBlob);
4846
- settings = source.settings;
4847
- }
4848
- if (zipBlob) {
4849
- if (!zipBlob.size) {
4850
- throw new Error("Empty zip file");
4851
- }
4852
- const reader = yield _ZipLoader.zipReader(zipBlob, sourceURL);
4853
- if (!settings) {
4854
- settings = yield _ZipLoader.createSettings(reader);
4855
- }
4856
- settings._objectURL = _ZipLoader.ZIP_PROTOCOL + _ZipLoader.uid + "/" + settings.url;
4857
- const files = yield _ZipLoader.unzip(reader, settings);
4858
- files.settings = settings;
4859
- context.source = files;
4860
- if (sourceURL.startsWith("blob:")) {
4861
- context.live2dModel.once("modelLoaded", (internalModel) => {
4862
- internalModel.once("destroy", function() {
4863
- URL.revokeObjectURL(sourceURL);
4864
- });
4865
- });
4866
- }
4867
- _ZipLoader.releaseReader(reader);
4868
- }
4869
- return next();
4870
- }));
4871
- let ZipLoader = _ZipLoader;
4872
4834
  const _FileLoader = class _FileLoader {
4873
4835
  /**
4874
4836
  * Resolves the path of a resource file to the object URL.
@@ -5149,7 +5111,6 @@ __publicField(_Live2DFactory, "createInternalModel", createInternalModel);
5149
5111
  * Middlewares to run through when setting up a Live2DModel.
5150
5112
  */
5151
5113
  __publicField(_Live2DFactory, "live2DModelMiddlewares", [
5152
- ZipLoader.factory,
5153
5114
  FileLoader.factory,
5154
5115
  urlToJSON,
5155
5116
  jsonToSettings,
@@ -5175,7 +5136,6 @@ ExpressionManager.prototype["_loadExpression"] = function(index) {
5175
5136
  return Live2DFactory.loadExpression(this, index);
5176
5137
  };
5177
5138
  FileLoader["live2dFactory"] = Live2DFactory;
5178
- ZipLoader["live2dFactory"] = Live2DFactory;
5179
5139
  const _Automator = class _Automator {
5180
5140
  constructor(model, {
5181
5141
  autoUpdate = true,
@@ -5329,10 +5289,17 @@ const tempMatrix$1 = new Matrix();
5329
5289
  const tempWorldMatrix = new Matrix();
5330
5290
  const tempDrawableBounds = { x: 0, y: 0, width: 0, height: 0 };
5331
5291
  const tempModelBounds = new Bounds();
5292
+ let live2DPluginFallbackWarned = false;
5332
5293
  class Live2DPipe {
5333
5294
  constructor(renderer) {
5334
5295
  this.renderer = renderer;
5335
5296
  }
5297
+ addRenderable(model, instructionSet) {
5298
+ var _a, _b;
5299
+ const renderPipes = this.renderer.renderPipes;
5300
+ (_b = (_a = renderPipes.batch) == null ? void 0 : _a.break) == null ? void 0 : _b.call(_a, instructionSet);
5301
+ instructionSet.add(model);
5302
+ }
5336
5303
  execute(model) {
5337
5304
  if (!model.visible || model.alpha <= 0) {
5338
5305
  return;
@@ -5341,15 +5308,86 @@ class Live2DPipe {
5341
5308
  }
5342
5309
  updateRenderable() {
5343
5310
  }
5311
+ destroyRenderable() {
5312
+ }
5313
+ validateRenderable() {
5314
+ return false;
5315
+ }
5344
5316
  destroy() {
5345
5317
  }
5346
5318
  }
5319
+ __publicField(Live2DPipe, "extension", {
5320
+ type: [ExtensionType.WebGLPipes],
5321
+ name: "live2d"
5322
+ });
5323
+ const Live2DPlugin = Live2DPipe;
5347
5324
  function ensureLive2DPipe(renderer) {
5348
5325
  const r = renderer;
5349
5326
  if (!r.renderPipes.live2d) {
5327
+ if (!live2DPluginFallbackWarned) {
5328
+ live2DPluginFallbackWarned = true;
5329
+ console.warn(
5330
+ "[untitled-pixi-live2d-engine] Lazy Live2D render pipe registration is deprecated. Register Live2DPlugin explicitly with `extensions.add(Live2DPlugin)` before creating the Pixi renderer."
5331
+ );
5332
+ }
5350
5333
  r.renderPipes.live2d = new Live2DPipe(renderer);
5351
5334
  }
5352
5335
  }
5336
+ function getMatrixMaxScale(matrix) {
5337
+ return Math.max(Math.hypot(matrix.a, matrix.b), Math.hypot(matrix.c, matrix.d));
5338
+ }
5339
+ function isDrawableTextureResource(resource) {
5340
+ if (!resource || typeof resource !== "object") {
5341
+ return false;
5342
+ }
5343
+ const { width, height } = resource;
5344
+ return typeof width === "number" && width > 0 && typeof height === "number" && height > 0;
5345
+ }
5346
+ function createTextureLODTexture(sourceTexture, level, width, height, filter) {
5347
+ var _a, _b;
5348
+ const resource = sourceTexture.source.resource;
5349
+ if (!isDrawableTextureResource(resource)) {
5350
+ return void 0;
5351
+ }
5352
+ const canvas = DOMAdapter.get().createCanvas(width, height);
5353
+ const context = canvas.getContext("2d");
5354
+ if (!context) {
5355
+ return void 0;
5356
+ }
5357
+ try {
5358
+ context.clearRect(0, 0, width, height);
5359
+ context.imageSmoothingEnabled = filter !== "nearest";
5360
+ if ("imageSmoothingQuality" in context && filter !== "nearest") {
5361
+ context.imageSmoothingQuality = "high";
5362
+ }
5363
+ context.drawImage(
5364
+ resource,
5365
+ 0,
5366
+ 0,
5367
+ sourceTexture.source.pixelWidth,
5368
+ sourceTexture.source.pixelHeight,
5369
+ 0,
5370
+ 0,
5371
+ width,
5372
+ height
5373
+ );
5374
+ } catch (e) {
5375
+ return void 0;
5376
+ }
5377
+ const label = `${(_b = (_a = sourceTexture.label) != null ? _a : sourceTexture.source.label) != null ? _b : "Live2DTexture"} LOD ${level}`;
5378
+ return new Texture({
5379
+ label,
5380
+ source: new CanvasSource({
5381
+ resource: canvas,
5382
+ width,
5383
+ height,
5384
+ resolution: 1,
5385
+ scaleMode: getTextureLODFilter(filter),
5386
+ autoGenerateMipmaps: false,
5387
+ label
5388
+ })
5389
+ });
5390
+ }
5353
5391
  class Live2DModel extends Container {
5354
5392
  /**
5355
5393
  * Creates a new Live2DModel instance.
@@ -5373,6 +5411,9 @@ class Live2DModel extends Container {
5373
5411
  * @type {Texture[]}
5374
5412
  */
5375
5413
  __publicField(this, "textures", []);
5414
+ __publicField(this, "textureOptions");
5415
+ __publicField(this, "textureLODStates", []);
5416
+ __publicField(this, "textureLODFailures", []);
5376
5417
  /** @override
5377
5418
  * The Live2DTransform instance for this model.
5378
5419
  */
@@ -5429,15 +5470,25 @@ class Live2DModel extends Container {
5429
5470
  this.internalModel.updateWebGLContext(renderer.gl, this.generateUID());
5430
5471
  shouldUpdateTexture = true;
5431
5472
  }
5473
+ const { projectionMatrix, offset, worldTransformMatrix } = renderer.globalUniforms.globalUniformData;
5474
+ const adjustedWorld = tempWorldMatrix.copyFrom(worldTransformMatrix).append(this.groupTransform);
5475
+ const effectiveScale = getMatrixMaxScale(adjustedWorld) * renderer.resolution;
5432
5476
  for (let i = 0; i < this.textures.length; i++) {
5433
- const texture = this.textures[i];
5434
- const hasGlSource = Boolean(texture.source._gpuData[renderer.uid]);
5435
- if (shouldUpdateTexture || !hasGlSource) {
5436
- renderer.gl.pixelStorei(
5437
- WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL,
5438
- this.internalModel.textureFlipY
5439
- );
5440
- renderer.texture.bind(texture, 0);
5477
+ const originalTexture = this.textures[i];
5478
+ let texture = this.resolveTextureForRender(i, originalTexture, effectiveScale);
5479
+ try {
5480
+ this.uploadTextureForRender(renderer, texture, shouldUpdateTexture);
5481
+ } catch (e) {
5482
+ if (texture === originalTexture) {
5483
+ throw new Error("Failed to upload Live2D texture.");
5484
+ }
5485
+ const state = this.textureLODStates[i];
5486
+ if ((state == null ? void 0 : state.texture) === texture) {
5487
+ this.markTextureLODFailure(i, originalTexture, state.level);
5488
+ this.destroyTextureLODState(i);
5489
+ }
5490
+ texture = originalTexture;
5491
+ this.uploadTextureForRender(renderer, texture, shouldUpdateTexture);
5441
5492
  }
5442
5493
  this.internalModel.bindTexture(i, renderer.texture.getGlSource(texture.source).texture);
5443
5494
  }
@@ -5454,8 +5505,6 @@ class Live2DModel extends Container {
5454
5505
  if (frameDelta) {
5455
5506
  this.internalModel.update(frameDelta, this.elapsedTime);
5456
5507
  }
5457
- const { projectionMatrix, offset } = renderer.globalUniforms.globalUniformData;
5458
- const adjustedWorld = tempWorldMatrix.copyFrom(this.worldTransform);
5459
5508
  adjustedWorld.tx -= (_a = offset == null ? void 0 : offset.x) != null ? _a : 0;
5460
5509
  adjustedWorld.ty -= (_b = offset == null ? void 0 : offset.y) != null ? _b : 0;
5461
5510
  const internalTransform = tempMatrix$1.copyFrom(projectionMatrix).append(adjustedWorld);
@@ -5478,6 +5527,7 @@ class Live2DModel extends Container {
5478
5527
  }
5479
5528
  }
5480
5529
  });
5530
+ this.textureOptions = options == null ? void 0 : options.textureOptions;
5481
5531
  this.anchor = new ObservablePoint(
5482
5532
  {
5483
5533
  _onUpdate: () => this.onAnchorChange()
@@ -5629,9 +5679,7 @@ class Live2DModel extends Container {
5629
5679
  ensureLive2DPipe(renderer);
5630
5680
  const renderPipes = renderer.renderPipes;
5631
5681
  const addRenderable = () => {
5632
- var _a2, _b2;
5633
- (_b2 = (_a2 = renderPipes.batch) == null ? void 0 : _a2.break) == null ? void 0 : _b2.call(_a2, instructionSet);
5634
- instructionSet.add(this);
5682
+ renderPipes.live2d.addRenderable(this, instructionSet);
5635
5683
  };
5636
5684
  const collectChildren = () => {
5637
5685
  const children = this.children;
@@ -5922,6 +5970,75 @@ class Live2DModel extends Container {
5922
5970
  this.deltaTime += dt;
5923
5971
  this.elapsedTime += dt;
5924
5972
  }
5973
+ resolveTextureForRender(index, texture, effectiveScale) {
5974
+ var _a;
5975
+ const plan = getTextureLODPlan(__spreadProps(__spreadValues({}, this.textureOptions), {
5976
+ effectiveScale,
5977
+ textureWidth: texture.source.pixelWidth,
5978
+ textureHeight: texture.source.pixelHeight
5979
+ }));
5980
+ if (!plan) {
5981
+ return texture;
5982
+ }
5983
+ const failure = this.textureLODFailures[index];
5984
+ if ((failure == null ? void 0 : failure.source) === texture && failure.levels.has(plan.level)) {
5985
+ return texture;
5986
+ }
5987
+ const existing = this.textureLODStates[index];
5988
+ if ((existing == null ? void 0 : existing.source) === texture && existing.level === plan.level && !existing.texture.destroyed) {
5989
+ return existing.texture;
5990
+ }
5991
+ this.destroyTextureLODState(index);
5992
+ const lodTexture = createTextureLODTexture(
5993
+ texture,
5994
+ plan.level,
5995
+ plan.width,
5996
+ plan.height,
5997
+ (_a = this.textureOptions) == null ? void 0 : _a.lodFilter
5998
+ );
5999
+ if (!lodTexture) {
6000
+ this.markTextureLODFailure(index, texture, plan.level);
6001
+ return texture;
6002
+ }
6003
+ this.textureLODStates[index] = {
6004
+ source: texture,
6005
+ level: plan.level,
6006
+ texture: lodTexture
6007
+ };
6008
+ return lodTexture;
6009
+ }
6010
+ markTextureLODFailure(index, texture, level) {
6011
+ let failure = this.textureLODFailures[index];
6012
+ if ((failure == null ? void 0 : failure.source) !== texture) {
6013
+ failure = { source: texture, levels: /* @__PURE__ */ new Set() };
6014
+ this.textureLODFailures[index] = failure;
6015
+ }
6016
+ failure.levels.add(level);
6017
+ }
6018
+ destroyTextureLODState(index) {
6019
+ const state = this.textureLODStates[index];
6020
+ if (!state) {
6021
+ return;
6022
+ }
6023
+ state.texture.destroy(true);
6024
+ this.textureLODStates[index] = void 0;
6025
+ }
6026
+ destroyTextureLODStates() {
6027
+ for (let i = 0; i < this.textureLODStates.length; i++) {
6028
+ this.destroyTextureLODState(i);
6029
+ }
6030
+ this.textureLODFailures.length = 0;
6031
+ }
6032
+ uploadTextureForRender(renderer, texture, shouldUpdateTexture) {
6033
+ const hasGlSource = Boolean(texture.source._gpuData[renderer.uid]);
6034
+ if (shouldUpdateTexture || !hasGlSource) {
6035
+ renderer.gl.pixelStorei(
6036
+ WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL,
6037
+ this.internalModel.textureFlipY
6038
+ );
6039
+ renderer.texture.bind(texture, 0);
6040
+ }
6041
+ }
5925
6042
  /**
5926
6043
  * Destroys the model and all related resources. This takes the same options and also
5927
6044
  * behaves the same as `PIXI.Container#destroy`.
@@ -5936,6 +6053,7 @@ class Live2DModel extends Container {
5936
6053
  */
5937
6054
  destroy(options) {
5938
6055
  this.emit("destroy");
6056
+ this.destroyTextureLODStates();
5939
6057
  if (options == null ? void 0 : options.texture) {
5940
6058
  this.textures.forEach((texture) => texture.destroy(options.baseTexture));
5941
6059
  }
@@ -11233,6 +11351,12 @@ var Live2DCubismFramework$8;
11233
11351
  const tempMatrix = new CubismMatrix44();
11234
11352
  const DEFAULT_MASKS_PER_RENDER_TEXTURE = 36;
11235
11353
  const MASKS_PER_RENDER_TEXTURE = 32;
11354
+ const HIGH_PRECISION_MASK_UNIQUE_MASK_SET_THRESHOLD = 16;
11355
+ const HIGH_PRECISION_MASK_MASKED_DRAWABLE_THRESHOLD = 64;
11356
+ const HIGH_PRECISION_MASK_MAX_MASKS_PER_DRAWABLE_THRESHOLD = 3;
11357
+ const HIGH_PRECISION_MASK_MASKED_VERTEX_THRESHOLD = 4096;
11358
+ const HIGH_PRECISION_MASK_VERTEX_RATIO_THRESHOLD = 0.5;
11359
+ const HIGH_PRECISION_MASK_VERTEX_RATIO_DRAWABLE_THRESHOLD = 24;
11236
11360
  function getRequiredMaskRenderTextureCount(model) {
11237
11361
  var _a;
11238
11362
  if (!model.isUsingMasking()) {
@@ -11255,6 +11379,51 @@ function getRequiredMaskRenderTextureCount(model) {
11255
11379
  }
11256
11380
  return Math.ceil(clippingContextCount / MASKS_PER_RENDER_TEXTURE);
11257
11381
  }
11382
+ function getMaskProfile(model) {
11383
+ var _a, _b;
11384
+ const maskCounts = model.getDrawableMaskCounts();
11385
+ const masks = model.getDrawableMasks();
11386
+ const uniqueMaskSets = /* @__PURE__ */ new Set();
11387
+ const drawableCount = model.getDrawableCount();
11388
+ let maskedDrawableCount = 0;
11389
+ let maxMasksPerDrawable = 0;
11390
+ let maskedVertexCount = 0;
11391
+ let totalVertexCount = 0;
11392
+ for (let i = 0; i < drawableCount; i++) {
11393
+ const vertexCount = model.getDrawableVertexCount(i);
11394
+ const maskCount = (_a = maskCounts[i]) != null ? _a : 0;
11395
+ totalVertexCount += vertexCount;
11396
+ if (maskCount <= 0) {
11397
+ continue;
11398
+ }
11399
+ maskedDrawableCount++;
11400
+ maskedVertexCount += vertexCount;
11401
+ maxMasksPerDrawable = Math.max(maxMasksPerDrawable, maskCount);
11402
+ const maskSet = Array.from((_b = masks[i]) != null ? _b : []).slice(0, maskCount).sort((a, b) => a - b).join(",");
11403
+ uniqueMaskSets.add(maskSet);
11404
+ }
11405
+ return {
11406
+ maskedDrawableCount,
11407
+ uniqueMaskSetCount: uniqueMaskSets.size,
11408
+ maxMasksPerDrawable,
11409
+ maskedVertexCount,
11410
+ totalVertexCount
11411
+ };
11412
+ }
11413
+ function shouldUseHighPrecisionMask(model) {
11414
+ if (!model.isUsingMasking()) {
11415
+ return false;
11416
+ }
11417
+ const profile = getMaskProfile(model);
11418
+ const maskedVertexRatio = profile.totalVertexCount > 0 ? profile.maskedVertexCount / profile.totalVertexCount : 0;
11419
+ return profile.uniqueMaskSetCount > HIGH_PRECISION_MASK_UNIQUE_MASK_SET_THRESHOLD || profile.maskedDrawableCount > HIGH_PRECISION_MASK_MASKED_DRAWABLE_THRESHOLD || profile.maxMasksPerDrawable >= HIGH_PRECISION_MASK_MAX_MASKS_PER_DRAWABLE_THRESHOLD || profile.maskedVertexCount > HIGH_PRECISION_MASK_MASKED_VERTEX_THRESHOLD || maskedVertexRatio >= HIGH_PRECISION_MASK_VERTEX_RATIO_THRESHOLD && profile.maskedDrawableCount > HIGH_PRECISION_MASK_VERTEX_RATIO_DRAWABLE_THRESHOLD;
11420
+ }
11421
+ function resolveHighPrecisionMaskOption(model, option) {
11422
+ if (typeof option === "boolean") {
11423
+ return option;
11424
+ }
11425
+ return shouldUseHighPrecisionMask(model);
11426
+ }
11258
11427
  class CubismInternalModel extends InternalModel {
11259
11428
  constructor(coreModel, settings, options) {
11260
11429
  super();
@@ -11355,6 +11524,9 @@ class CubismInternalModel extends InternalModel {
11355
11524
  breathParams.pushBack(new BreathParameterData(this.idParamBreath, 0, 0.5, 3.2345, 0.5));
11356
11525
  this.breath.setParameters(breathParams);
11357
11526
  this.renderer.initialize(this.coreModel, getRequiredMaskRenderTextureCount(this.coreModel));
11527
+ this.renderer.useHighPrecisionMask(
11528
+ resolveHighPrecisionMaskOption(this.coreModel, this.options.useHighPrecisionMask)
11529
+ );
11358
11530
  this.renderer.setIsPremultipliedAlpha(true);
11359
11531
  }
11360
11532
  getIdSafe(id) {
@@ -11453,7 +11625,7 @@ class CubismInternalModel extends InternalModel {
11453
11625
  value = Math.pow(value, 1.15);
11454
11626
  const min_ = value > 0 ? 0.1 : 0;
11455
11627
  const max_ = 1;
11456
- value = clamp(value, min_, max_);
11628
+ value = clamp$1(value, min_, max_);
11457
11629
  this.motionManager.lipSyncIds.forEach((lipSyncId) => {
11458
11630
  model.addParameterValueById(this.getIdSafe(lipSyncId), value, this.options.lipSyncWeight);
11459
11631
  });
@@ -15709,6 +15881,7 @@ export {
15709
15881
  Live2DFactory,
15710
15882
  Live2DLoader,
15711
15883
  Live2DModel,
15884
+ Live2DPlugin,
15712
15885
  Live2DTransform,
15713
15886
  LogLevel,
15714
15887
  MBToByte,
@@ -15722,9 +15895,8 @@ export {
15722
15895
  VERSION,
15723
15896
  VOLUME,
15724
15897
  XHRLoader,
15725
- ZipLoader,
15726
15898
  applyMixins,
15727
- clamp,
15899
+ clamp$1 as clamp,
15728
15900
  config,
15729
15901
  configureCubismSDK,
15730
15902
  copyArray,