@npcify/web 0.1.4 → 0.1.5

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.
Files changed (2) hide show
  1. package/dist/index.js +86 -10
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -3477,10 +3477,15 @@ var AVATAR_COLOR_SETTINGS = {
3477
3477
  toneMapping: NoToneMapping,
3478
3478
  outputColorSpace: SRGBColorSpace
3479
3479
  };
3480
+ var AVATAR_LIGHTS = {
3481
+ ambientIntensity: 0.7 * Math.PI / 1.9,
3482
+ directionalIntensity: 1.2 * Math.PI / 1.9,
3483
+ directionalPosition: [1, 1.5, 1]
3484
+ };
3480
3485
  var addAvatarLights = function(scene, {
3481
- ambientIntensity = 0.7,
3482
- directionalIntensity = 1.2,
3483
- directionalPosition = [1, 1.5, 1]
3486
+ ambientIntensity = AVATAR_LIGHTS.ambientIntensity,
3487
+ directionalIntensity = AVATAR_LIGHTS.directionalIntensity,
3488
+ directionalPosition = [...AVATAR_LIGHTS.directionalPosition]
3484
3489
  } = {}) {
3485
3490
  scene.add(new AmbientLight(16777215, ambientIntensity));
3486
3491
  const key = new DirectionalLight(16777215, directionalIntensity);
@@ -3829,6 +3834,12 @@ import {
3829
3834
  Texture as Texture2
3830
3835
  } from "three";
3831
3836
 
3837
+ // ../vrm-renderer/src/generated-character.ts
3838
+ import {
3839
+ MToonMaterial as MToonMaterial2,
3840
+ VRMExpressionMaterialColorBind
3841
+ } from "@pixiv/three-vrm";
3842
+
3832
3843
  // ../vrm-renderer/src/texture-expressions.ts
3833
3844
  import {
3834
3845
  MToonMaterial,
@@ -3957,13 +3968,9 @@ var findAtlasBindings = function(vrm) {
3957
3968
  var hasTextureMouthExpressions = function(manager) {
3958
3969
  return !!manager && installed.has(manager);
3959
3970
  };
3960
- var enableTextureExpressionPlayback = function(vrm) {
3971
+ var installTextureMouthPlayback = function(vrm, bindings) {
3961
3972
  const manager = vrm.expressionManager;
3962
- if (!manager || installed.has(manager)) {
3963
- return;
3964
- }
3965
- const bindings = findAtlasBindings(vrm);
3966
- if (!bindings.length) {
3973
+ if (!manager || installed.has(manager) || !bindings.length) {
3967
3974
  return;
3968
3975
  }
3969
3976
  installed.add(manager);
@@ -4009,6 +4016,75 @@ var enableTextureExpressionPlayback = function(vrm) {
4009
4016
  }
4010
4017
  };
4011
4018
 
4019
+ // ../vrm-renderer/src/generated-character.ts
4020
+ var TOLERANCE = 1e-4;
4021
+ var prepared = /* @__PURE__ */ new WeakMap();
4022
+ var sameColour = function(a, b) {
4023
+ return Math.abs(a.r - b.r) < TOLERANCE && Math.abs(a.g - b.g) < TOLERANCE && Math.abs(a.b - b.b) < TOLERANCE;
4024
+ };
4025
+ var sceneMToonMaterials = function(scene) {
4026
+ const materials = /* @__PURE__ */ new Set();
4027
+ scene.traverse((object) => {
4028
+ const material = object.material;
4029
+ for (const entry of Array.isArray(material) ? material : [material]) {
4030
+ if (entry instanceof MToonMaterial2) {
4031
+ materials.add(entry);
4032
+ }
4033
+ }
4034
+ });
4035
+ return materials;
4036
+ };
4037
+ var sharesFaceRecipe = function(material, faces) {
4038
+ return !!material.map && material.shadeMultiplyTexture?.image === material.map.image && faces.some(
4039
+ (face) => sameColour(material.color, face.color) && sameColour(material.shadeColorFactor, face.shadeColorFactor)
4040
+ );
4041
+ };
4042
+ var detectGeneratedCharacter = function(vrm) {
4043
+ const known = prepared.get(vrm);
4044
+ if (known) {
4045
+ return known;
4046
+ }
4047
+ const mouths = findAtlasBindings(vrm);
4048
+ if (!mouths.length) {
4049
+ return null;
4050
+ }
4051
+ const faces = [
4052
+ ...new Set(mouths.map(({ bind }) => bind.material))
4053
+ ];
4054
+ const coloured = /* @__PURE__ */ new Set();
4055
+ for (const expression of vrm.expressionManager?.expressions ?? []) {
4056
+ for (const bind of expression.binds) {
4057
+ if (bind instanceof VRMExpressionMaterialColorBind) {
4058
+ coloured.add(bind.material);
4059
+ }
4060
+ }
4061
+ }
4062
+ const materials = [...sceneMToonMaterials(vrm.scene)].filter(
4063
+ (material) => !coloured.has(material) && (faces.includes(material) || sharesFaceRecipe(material, faces))
4064
+ );
4065
+ return { faces, mouths, materials };
4066
+ };
4067
+ var normaliseLitFactor = function(material) {
4068
+ const { r, g, b } = material.color;
4069
+ if (!(r > 0) || r >= 1 - TOLERANCE || Math.abs(r - g) >= TOLERANCE || Math.abs(r - b) >= TOLERANCE) {
4070
+ return;
4071
+ }
4072
+ material.color.multiplyScalar(1 / r);
4073
+ material.shadeColorFactor.multiplyScalar(1 / r);
4074
+ };
4075
+ var prepareGeneratedCharacter = function(vrm) {
4076
+ const character = detectGeneratedCharacter(vrm);
4077
+ if (!character || prepared.has(vrm)) {
4078
+ return character;
4079
+ }
4080
+ for (const material of character.materials) {
4081
+ normaliseLitFactor(material);
4082
+ }
4083
+ installTextureMouthPlayback(vrm, character.mouths);
4084
+ prepared.set(vrm, character);
4085
+ return character;
4086
+ };
4087
+
4012
4088
  // ../vrm-renderer/src/session.ts
4013
4089
  import { VRMUtils as VRMUtils2 } from "@pixiv/three-vrm";
4014
4090
  import { AnimationMixer, LoopOnce, LoopRepeat } from "three";
@@ -4333,7 +4409,7 @@ var loadVrm = async ({
4333
4409
  });
4334
4410
  VRMUtils4.removeUnnecessaryVertices(gltf.scene);
4335
4411
  VRMUtils4.combineSkeletons(gltf.scene);
4336
- enableTextureExpressionPlayback(gltf.userData.vrm);
4412
+ prepareGeneratedCharacter(gltf.userData.vrm);
4337
4413
  return gltf.userData.vrm;
4338
4414
  };
4339
4415
  var loadVrma = async ({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npcify/web",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Framework-independent Npcify characters, live conversations and baked performances for the web",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -35,8 +35,8 @@
35
35
  "tsup": "^8.5.0",
36
36
  "typescript": "^5.9.3",
37
37
  "@npcify/shared": "^0.1.0",
38
- "@npcify/web-client": "^0.0.0",
39
- "@npcify/vrm-renderer": "^0.1.0"
38
+ "@npcify/vrm-renderer": "^0.1.0",
39
+ "@npcify/web-client": "^0.0.0"
40
40
  },
41
41
  "repository": {
42
42
  "type": "git",