untitled-pixi-live2d-engine 1.0.2 → 1.1.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.
@@ -2,8 +2,25 @@
2
2
  typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("pixi.js"), require("@pixi/sound")) : typeof define === "function" && define.amd ? define(["exports", "pixi.js", "@pixi/sound"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory((global.PIXI = global.PIXI || {}, global.PIXI.live2d = global.PIXI.live2d || {}), global.PIXI, global.PIXI));
3
3
  })(this, (function(exports2, pixi_js, sound) {
4
4
  "use strict";var __defProp = Object.defineProperty;
5
+ var __defProps = Object.defineProperties;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
10
  var __pow = Math.pow;
6
11
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
7
24
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
25
  var __async = (__this, __arguments, generator) => {
9
26
  return new Promise((resolve, reject) => {
@@ -91,7 +108,7 @@ var __async = (__this, __arguments, generator) => {
91
108
  preserveExpressionOnMotion: true,
92
109
  cubism: CubismFrameworkConfig
93
110
  };
94
- const VERSION = "1.0.2";
111
+ const VERSION = "1.1.0";
95
112
  const logger = {
96
113
  log(tag, ...messages) {
97
114
  if (config.logLevel <= config.LOG_LEVEL_VERBOSE) {
@@ -109,7 +126,7 @@ var __async = (__this, __arguments, generator) => {
109
126
  }
110
127
  }
111
128
  };
112
- function clamp(num, lower, upper) {
129
+ function clamp$1(num, lower, upper) {
113
130
  return num < lower ? lower : num > upper ? upper : num;
114
131
  }
115
132
  function rand(min, max) {
@@ -384,8 +401,8 @@ var __async = (__this, __arguments, generator) => {
384
401
  * @param instant - Should the focus position be instantly applied.
385
402
  */
386
403
  focus(x, y, instant = false) {
387
- this.targetX = clamp(x, -1, 1);
388
- this.targetY = clamp(y, -1, 1);
404
+ this.targetX = clamp$1(x, -1, 1);
405
+ this.targetY = clamp$1(y, -1, 1);
389
406
  if (instant) {
390
407
  this.x = this.targetX;
391
408
  this.y = this.targetY;
@@ -1902,6 +1919,68 @@ var __async = (__this, __arguments, generator) => {
1902
1919
  }
1903
1920
  }
1904
1921
  }
1922
+ const DEFAULT_LOD_SCALE_THRESHOLD = 0.5;
1923
+ const DEFAULT_LOD_TEXTURE_SIZE_THRESHOLD = 4096;
1924
+ function getTextureLODPlan({
1925
+ lod,
1926
+ effectiveScale,
1927
+ textureWidth,
1928
+ textureHeight,
1929
+ lodScaleThreshold,
1930
+ lodTextureSizeThreshold,
1931
+ lodMaxLevel
1932
+ }) {
1933
+ if (getTextureLODMode(lod) !== "single-auto") {
1934
+ return void 0;
1935
+ }
1936
+ if (!Number.isFinite(effectiveScale) || effectiveScale <= 0) {
1937
+ return void 0;
1938
+ }
1939
+ const width = Math.floor(textureWidth);
1940
+ const height = Math.floor(textureHeight);
1941
+ if (width <= 1 || height <= 1) {
1942
+ return void 0;
1943
+ }
1944
+ const scaleThreshold = validPositive(lodScaleThreshold, DEFAULT_LOD_SCALE_THRESHOLD);
1945
+ if (effectiveScale >= scaleThreshold) {
1946
+ return void 0;
1947
+ }
1948
+ const textureSizeThreshold = validPositive(
1949
+ lodTextureSizeThreshold,
1950
+ DEFAULT_LOD_TEXTURE_SIZE_THRESHOLD
1951
+ );
1952
+ if (Math.max(width, height) < textureSizeThreshold) {
1953
+ return void 0;
1954
+ }
1955
+ const rawLevel = Math.round(Math.log2(1 / effectiveScale));
1956
+ const maxDimensionLevel = Math.max(1, Math.floor(Math.log2(Math.max(width, height))));
1957
+ const maxLevel = Math.min(maxDimensionLevel, validMaxLevel(lodMaxLevel, maxDimensionLevel));
1958
+ const level = clamp(Math.max(1, rawLevel), 1, maxLevel);
1959
+ const factor = __pow(2, level);
1960
+ return {
1961
+ level,
1962
+ width: Math.max(1, Math.round(width / factor)),
1963
+ height: Math.max(1, Math.round(height / factor))
1964
+ };
1965
+ }
1966
+ function getTextureLODMode(lod) {
1967
+ if (lod === false || lod === "single-auto") {
1968
+ return lod;
1969
+ }
1970
+ return "full";
1971
+ }
1972
+ function getTextureLODFilter(filter) {
1973
+ return filter === "nearest" ? "nearest" : "linear";
1974
+ }
1975
+ function validPositive(value, fallback) {
1976
+ return value !== void 0 && Number.isFinite(value) && value > 0 ? value : fallback;
1977
+ }
1978
+ function validMaxLevel(value, fallback) {
1979
+ return value !== void 0 && Number.isFinite(value) && value >= 1 ? Math.floor(value) : fallback;
1980
+ }
1981
+ function clamp(value, min, max) {
1982
+ return Math.min(max, Math.max(min, value));
1983
+ }
1905
1984
  function createTexture(url, options = {}) {
1906
1985
  const config2 = pixi_js.loadTextures.config;
1907
1986
  const previousCrossOrigin = config2 == null ? void 0 : config2.crossOrigin;
@@ -1914,7 +1993,12 @@ var __async = (__this, __arguments, generator) => {
1914
1993
  config2.preferCreateImageBitmap = false;
1915
1994
  config2.preferWorkers = false;
1916
1995
  }
1917
- return pixi_js.Assets.load(url).catch((error) => {
1996
+ return pixi_js.Assets.load({
1997
+ src: url,
1998
+ data: {
1999
+ autoGenerateMipmaps: getTextureLODMode(options.lod) === "full"
2000
+ }
2001
+ }).catch((error) => {
1918
2002
  if (error instanceof Error) {
1919
2003
  throw error;
1920
2004
  }
@@ -2047,9 +2131,9 @@ var __async = (__this, __arguments, generator) => {
2047
2131
  const setupEssentials = (context, next) => __async(null, null, function* () {
2048
2132
  if (context.settings) {
2049
2133
  const live2DModel = context.live2dModel;
2050
- const textureOptions = {
2134
+ const textureOptions = __spreadValues({
2051
2135
  crossOrigin: context.options.crossOrigin
2052
- };
2136
+ }, context.options.textureOptions);
2053
2137
  if (context.settings instanceof CubismLegacyModelSettings) {
2054
2138
  textureOptions.preferCreateImageBitmap = false;
2055
2139
  }
@@ -2684,6 +2768,61 @@ var __async = (__this, __arguments, generator) => {
2684
2768
  r.renderPipes.live2d = new Live2DPipe(renderer);
2685
2769
  }
2686
2770
  }
2771
+ function getMatrixMaxScale(matrix) {
2772
+ return Math.max(Math.hypot(matrix.a, matrix.b), Math.hypot(matrix.c, matrix.d));
2773
+ }
2774
+ function isDrawableTextureResource(resource) {
2775
+ if (!resource || typeof resource !== "object") {
2776
+ return false;
2777
+ }
2778
+ const { width, height } = resource;
2779
+ return typeof width === "number" && width > 0 && typeof height === "number" && height > 0;
2780
+ }
2781
+ function createTextureLODTexture(sourceTexture, level, width, height, filter) {
2782
+ var _a, _b;
2783
+ const resource = sourceTexture.source.resource;
2784
+ if (!isDrawableTextureResource(resource)) {
2785
+ return void 0;
2786
+ }
2787
+ const canvas = pixi_js.DOMAdapter.get().createCanvas(width, height);
2788
+ const context = canvas.getContext("2d");
2789
+ if (!context) {
2790
+ return void 0;
2791
+ }
2792
+ try {
2793
+ context.clearRect(0, 0, width, height);
2794
+ context.imageSmoothingEnabled = filter !== "nearest";
2795
+ if ("imageSmoothingQuality" in context && filter !== "nearest") {
2796
+ context.imageSmoothingQuality = "high";
2797
+ }
2798
+ context.drawImage(
2799
+ resource,
2800
+ 0,
2801
+ 0,
2802
+ sourceTexture.source.pixelWidth,
2803
+ sourceTexture.source.pixelHeight,
2804
+ 0,
2805
+ 0,
2806
+ width,
2807
+ height
2808
+ );
2809
+ } catch (e) {
2810
+ return void 0;
2811
+ }
2812
+ const label = `${(_b = (_a = sourceTexture.label) != null ? _a : sourceTexture.source.label) != null ? _b : "Live2DTexture"} LOD ${level}`;
2813
+ return new pixi_js.Texture({
2814
+ label,
2815
+ source: new pixi_js.CanvasSource({
2816
+ resource: canvas,
2817
+ width,
2818
+ height,
2819
+ resolution: 1,
2820
+ scaleMode: getTextureLODFilter(filter),
2821
+ autoGenerateMipmaps: false,
2822
+ label
2823
+ })
2824
+ });
2825
+ }
2687
2826
  class Live2DModel extends pixi_js.Container {
2688
2827
  /**
2689
2828
  * Creates a new Live2DModel instance.
@@ -2707,6 +2846,9 @@ var __async = (__this, __arguments, generator) => {
2707
2846
  * @type {Texture[]}
2708
2847
  */
2709
2848
  __publicField(this, "textures", []);
2849
+ __publicField(this, "textureOptions");
2850
+ __publicField(this, "textureLODStates", []);
2851
+ __publicField(this, "textureLODFailures", []);
2710
2852
  /** @override
2711
2853
  * The Live2DTransform instance for this model.
2712
2854
  */
@@ -2763,15 +2905,25 @@ var __async = (__this, __arguments, generator) => {
2763
2905
  this.internalModel.updateWebGLContext(renderer.gl, this.generateUID());
2764
2906
  shouldUpdateTexture = true;
2765
2907
  }
2908
+ const { projectionMatrix, offset, worldTransformMatrix } = renderer.globalUniforms.globalUniformData;
2909
+ const adjustedWorld = tempWorldMatrix.copyFrom(worldTransformMatrix).append(this.groupTransform);
2910
+ const effectiveScale = getMatrixMaxScale(adjustedWorld) * renderer.resolution;
2766
2911
  for (let i = 0; i < this.textures.length; i++) {
2767
- const texture = this.textures[i];
2768
- const hasGlSource = Boolean(texture.source._gpuData[renderer.uid]);
2769
- if (shouldUpdateTexture || !hasGlSource) {
2770
- renderer.gl.pixelStorei(
2771
- WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL,
2772
- this.internalModel.textureFlipY
2773
- );
2774
- renderer.texture.bind(texture, 0);
2912
+ const originalTexture = this.textures[i];
2913
+ let texture = this.resolveTextureForRender(i, originalTexture, effectiveScale);
2914
+ try {
2915
+ this.uploadTextureForRender(renderer, texture, shouldUpdateTexture);
2916
+ } catch (e) {
2917
+ if (texture === originalTexture) {
2918
+ throw new Error("Failed to upload Live2D texture.");
2919
+ }
2920
+ const state = this.textureLODStates[i];
2921
+ if ((state == null ? void 0 : state.texture) === texture) {
2922
+ this.markTextureLODFailure(i, originalTexture, state.level);
2923
+ this.destroyTextureLODState(i);
2924
+ }
2925
+ texture = originalTexture;
2926
+ this.uploadTextureForRender(renderer, texture, shouldUpdateTexture);
2775
2927
  }
2776
2928
  this.internalModel.bindTexture(i, renderer.texture.getGlSource(texture.source).texture);
2777
2929
  }
@@ -2788,8 +2940,6 @@ var __async = (__this, __arguments, generator) => {
2788
2940
  if (frameDelta) {
2789
2941
  this.internalModel.update(frameDelta, this.elapsedTime);
2790
2942
  }
2791
- const { projectionMatrix, offset } = renderer.globalUniforms.globalUniformData;
2792
- const adjustedWorld = tempWorldMatrix.copyFrom(this.worldTransform);
2793
2943
  adjustedWorld.tx -= (_a = offset == null ? void 0 : offset.x) != null ? _a : 0;
2794
2944
  adjustedWorld.ty -= (_b = offset == null ? void 0 : offset.y) != null ? _b : 0;
2795
2945
  const internalTransform = tempMatrix.copyFrom(projectionMatrix).append(adjustedWorld);
@@ -2812,6 +2962,7 @@ var __async = (__this, __arguments, generator) => {
2812
2962
  }
2813
2963
  }
2814
2964
  });
2965
+ this.textureOptions = options == null ? void 0 : options.textureOptions;
2815
2966
  this.anchor = new pixi_js.ObservablePoint(
2816
2967
  {
2817
2968
  _onUpdate: () => this.onAnchorChange()
@@ -3256,6 +3407,75 @@ var __async = (__this, __arguments, generator) => {
3256
3407
  this.deltaTime += dt;
3257
3408
  this.elapsedTime += dt;
3258
3409
  }
3410
+ resolveTextureForRender(index, texture, effectiveScale) {
3411
+ var _a;
3412
+ const plan = getTextureLODPlan(__spreadProps(__spreadValues({}, this.textureOptions), {
3413
+ effectiveScale,
3414
+ textureWidth: texture.source.pixelWidth,
3415
+ textureHeight: texture.source.pixelHeight
3416
+ }));
3417
+ if (!plan) {
3418
+ return texture;
3419
+ }
3420
+ const failure = this.textureLODFailures[index];
3421
+ if ((failure == null ? void 0 : failure.source) === texture && failure.levels.has(plan.level)) {
3422
+ return texture;
3423
+ }
3424
+ const existing = this.textureLODStates[index];
3425
+ if ((existing == null ? void 0 : existing.source) === texture && existing.level === plan.level && !existing.texture.destroyed) {
3426
+ return existing.texture;
3427
+ }
3428
+ this.destroyTextureLODState(index);
3429
+ const lodTexture = createTextureLODTexture(
3430
+ texture,
3431
+ plan.level,
3432
+ plan.width,
3433
+ plan.height,
3434
+ (_a = this.textureOptions) == null ? void 0 : _a.lodFilter
3435
+ );
3436
+ if (!lodTexture) {
3437
+ this.markTextureLODFailure(index, texture, plan.level);
3438
+ return texture;
3439
+ }
3440
+ this.textureLODStates[index] = {
3441
+ source: texture,
3442
+ level: plan.level,
3443
+ texture: lodTexture
3444
+ };
3445
+ return lodTexture;
3446
+ }
3447
+ markTextureLODFailure(index, texture, level) {
3448
+ let failure = this.textureLODFailures[index];
3449
+ if ((failure == null ? void 0 : failure.source) !== texture) {
3450
+ failure = { source: texture, levels: /* @__PURE__ */ new Set() };
3451
+ this.textureLODFailures[index] = failure;
3452
+ }
3453
+ failure.levels.add(level);
3454
+ }
3455
+ destroyTextureLODState(index) {
3456
+ const state = this.textureLODStates[index];
3457
+ if (!state) {
3458
+ return;
3459
+ }
3460
+ state.texture.destroy(true);
3461
+ this.textureLODStates[index] = void 0;
3462
+ }
3463
+ destroyTextureLODStates() {
3464
+ for (let i = 0; i < this.textureLODStates.length; i++) {
3465
+ this.destroyTextureLODState(i);
3466
+ }
3467
+ this.textureLODFailures.length = 0;
3468
+ }
3469
+ uploadTextureForRender(renderer, texture, shouldUpdateTexture) {
3470
+ const hasGlSource = Boolean(texture.source._gpuData[renderer.uid]);
3471
+ if (shouldUpdateTexture || !hasGlSource) {
3472
+ renderer.gl.pixelStorei(
3473
+ WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL,
3474
+ this.internalModel.textureFlipY
3475
+ );
3476
+ renderer.texture.bind(texture, 0);
3477
+ }
3478
+ }
3259
3479
  /**
3260
3480
  * Destroys the model and all related resources. This takes the same options and also
3261
3481
  * behaves the same as `PIXI.Container#destroy`.
@@ -3270,6 +3490,7 @@ var __async = (__this, __arguments, generator) => {
3270
3490
  */
3271
3491
  destroy(options) {
3272
3492
  this.emit("destroy");
3493
+ this.destroyTextureLODStates();
3273
3494
  if (options == null ? void 0 : options.texture) {
3274
3495
  this.textures.forEach((texture) => texture.destroy(options.baseTexture));
3275
3496
  }
@@ -3635,7 +3856,7 @@ var __async = (__this, __arguments, generator) => {
3635
3856
  this.rightParam = coreModel.getParamIndex("PARAM_EYE_R_OPEN");
3636
3857
  }
3637
3858
  setEyeParams(value) {
3638
- this.eyeParamValue = clamp(value, 0, 1);
3859
+ this.eyeParamValue = clamp$1(value, 0, 1);
3639
3860
  this.coreModel.setParamFloat(this.leftParam, this.eyeParamValue);
3640
3861
  this.coreModel.setParamFloat(this.rightParam, this.eyeParamValue);
3641
3862
  }
@@ -3725,6 +3946,12 @@ var __async = (__this, __arguments, generator) => {
3725
3946
  { breathDepth: 1, lipSyncGain: 1.5, lipSyncWeight: 0.4 },
3726
3947
  options
3727
3948
  );
3949
+ if ((options == null ? void 0 : options.useHighPrecisionMask) !== void 0 && options.useHighPrecisionMask !== false) {
3950
+ logger.warn(
3951
+ "CubismLegacyInternalModel",
3952
+ "`useHighPrecisionMask` is only supported by Cubism 3/4/5 models and will be ignored for Cubism 2 models."
3953
+ );
3954
+ }
3728
3955
  this.motionManager = new CubismLegacyMotionManager(this);
3729
3956
  this.parallelMotionManager = [];
3730
3957
  this.eyeBlink = new Live2DEyeBlink(coreModel);
@@ -3862,7 +4089,7 @@ var __async = (__this, __arguments, generator) => {
3862
4089
  const max_ = 1;
3863
4090
  const min_ = value > 0 ? 0.1 : 0;
3864
4091
  value = Math.pow(value, 1.15);
3865
- value = clamp(value, min_, max_);
4092
+ value = clamp$1(value, min_, max_);
3866
4093
  for (let i = 0; i < this.motionManager.lipSyncIds.length; ++i) {
3867
4094
  this.coreModel.addToParamFloat(
3868
4095
  this.coreModel.getParamIndex(this.motionManager.lipSyncIds[i]),
@@ -4102,7 +4329,7 @@ var __async = (__this, __arguments, generator) => {
4102
4329
  );
4103
4330
  if (visibleIndex >= 0) {
4104
4331
  const originalOpacity = model.getPartsOpacity(partsGroup[visibleIndex].partsIndex);
4105
- visibleOpacity = clamp(originalOpacity + dt / this.opacityAnimDuration, 0, 1);
4332
+ visibleOpacity = clamp$1(originalOpacity + dt / this.opacityAnimDuration, 0, 1);
4106
4333
  } else {
4107
4334
  visibleIndex = 0;
4108
4335
  visibleOpacity = 1;
@@ -4222,7 +4449,7 @@ var __async = (__this, __arguments, generator) => {
4222
4449
  exports2.XHRLoader = XHRLoader;
4223
4450
  exports2.ZipLoader = ZipLoader;
4224
4451
  exports2.applyMixins = applyMixins;
4225
- exports2.clamp = clamp;
4452
+ exports2.clamp = clamp$1;
4226
4453
  exports2.config = config;
4227
4454
  exports2.copyArray = copyArray;
4228
4455
  exports2.copyProperty = copyProperty;