@npcify/web 0.1.7 → 0.2.1

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.
@@ -0,0 +1,107 @@
1
+ // three-game.js — Import an Npcify AI character directly into a Three.js game
2
+ import * as THREE from 'three'
3
+ import { loadCharacter } from '@npcify/web'
4
+
5
+ const canvas = document.querySelector('#npc')
6
+ const start = document.querySelector('#start')
7
+ const replies = document.querySelector('#replies')
8
+ const status = document.querySelector('#status')
9
+
10
+ // 1. Your existing Three.js game scene, camera, renderer, and clock
11
+ const scene = new THREE.Scene()
12
+ const camera = new THREE.PerspectiveCamera(
13
+ 35,
14
+ canvas.clientWidth / canvas.clientHeight,
15
+ 0.1,
16
+ 100,
17
+ )
18
+ camera.position.set(0, 1.4, 2.5)
19
+
20
+ const renderer = new THREE.WebGLRenderer({ canvas, antialias: true })
21
+ renderer.setSize(canvas.clientWidth, canvas.clientHeight, false)
22
+ renderer.shadowMap.enabled = true
23
+
24
+ scene.add(new THREE.AmbientLight(0xffffff, 1.2))
25
+ const sun = new THREE.DirectionalLight(0xffffff, 1.8)
26
+ sun.position.set(2, 4, 3)
27
+ sun.castShadow = true
28
+ scene.add(sun)
29
+
30
+ // 2. Load the AI character and attach it to your Three.js scene
31
+ const character = await loadCharacter({
32
+ baseUrl: 'https://api.npcify.ai',
33
+ characterId: 'YOUR_CHARACTER_ID',
34
+ apiKey: 'YOUR_SCOPED_API_KEY',
35
+ parent: scene,
36
+ gazeTarget: camera,
37
+ shadows: true,
38
+ position: [0, 0, 0],
39
+ onError: (error) => {
40
+ status.textContent = error.message
41
+ },
42
+ })
43
+
44
+ // 3. Optional: route character voice into Three.js 3D spatial audio
45
+ const listener = new THREE.AudioListener()
46
+ camera.add(listener)
47
+ const positionalAudio = new THREE.PositionalAudio(listener)
48
+ const audioSource = character.audio.createMediaElementSource(listener.context)
49
+ positionalAudio.setNodeSource(audioSource)
50
+ character.getBone('head')?.add(positionalAudio)
51
+
52
+ // 4. Handle dialogue options and user interaction
53
+ character.onOptionsReady(({ options }) => {
54
+ replies.replaceChildren()
55
+ for (const option of options) {
56
+ const button = document.createElement('button')
57
+ button.textContent = option.label
58
+ button.onclick = async () => {
59
+ replies.replaceChildren()
60
+ try {
61
+ await character.sendMessage(option.label)
62
+ } catch (error) {
63
+ status.textContent = error.message
64
+ }
65
+ }
66
+ replies.append(button)
67
+ }
68
+ })
69
+
70
+ start.disabled = false
71
+ start.onclick = async () => {
72
+ start.disabled = true
73
+ try {
74
+ await character.audio.unlock()
75
+ await character.createConversation()
76
+ } catch (error) {
77
+ status.textContent = error.message
78
+ start.disabled = false
79
+ }
80
+ }
81
+
82
+ // 5. Advance the character inside your game loop and read real-time state
83
+ const clock = new THREE.Clock()
84
+ const animate = function () {
85
+ requestAnimationFrame(animate)
86
+ const delta = Math.min(clock.getDelta(), 0.1)
87
+ character.update(delta)
88
+
89
+ // Real-time access to mesh, audio, expressions, and subtitle state:
90
+ // character.mesh, character.skinnedMeshes, character.audio.playing,
91
+ // character.expressions.mouthOpenness, character.revealedText
92
+ if (character.speaking) {
93
+ status.textContent = character.revealedText
94
+ }
95
+
96
+ renderer.render(scene, camera)
97
+ }
98
+ animate()
99
+
100
+ window.addEventListener(
101
+ 'pagehide',
102
+ () => {
103
+ void character.dispose()
104
+ renderer.dispose()
105
+ },
106
+ { once: true },
107
+ )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npcify/web",
3
- "version": "0.1.7",
3
+ "version": "0.2.1",
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",