@needle-tools/engine 4.16.0-next.73c93c0 → 4.16.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/SKILL.md ADDED
@@ -0,0 +1,237 @@
1
+ ---
2
+ name: needle-engine
3
+ description: Automatically provides Needle Engine context when working in a Needle Engine web project. Use this skill when editing TypeScript components, Vite config, GLB assets, or anything related to @needle-tools/engine.
4
+ metadata:
5
+ reviewed-against: "@needle-tools/engine@4.15.0"
6
+ last-reviewed: "2026-03"
7
+ ---
8
+
9
+ # Needle Engine
10
+
11
+ You are an expert in Needle Engine — a web-first 3D engine built on Three.js with a component system and Unity/Blender-based workflow.
12
+
13
+ ## When to Use This Skill
14
+
15
+ **Use when the user is:**
16
+ - Editing TypeScript files that import from `@needle-tools/engine`
17
+ - Working on a project with `vite.config.ts` that uses `needlePlugins`
18
+ - Loading or debugging `.glb` files exported from Unity or Blender
19
+ - Using the Needle Engine Blender addon or Unity package
20
+ - Asking about component lifecycle, serialization, XR, networking, or deployment
21
+
22
+ **Do NOT use for:**
23
+ - Pure Three.js projects with no Needle Engine
24
+ - Non-web Unity/Blender work with no GLB export
25
+
26
+ ---
27
+
28
+ ## Quick Start
29
+
30
+ ```html
31
+ <needle-engine src="assets/scene.glb"></needle-engine>
32
+ <script type="module">
33
+ import "@needle-tools/engine";
34
+ </script>
35
+ ```
36
+
37
+ Minimal TypeScript component:
38
+ ```ts
39
+ import { Behaviour, serializable, registerType } from "@needle-tools/engine";
40
+
41
+ @registerType
42
+ export class HelloWorld extends Behaviour {
43
+ @serializable() message: string = "Hello!";
44
+
45
+ start() {
46
+ console.log(this.message);
47
+ }
48
+ }
49
+ ```
50
+
51
+ > **TypeScript config required:** `tsconfig.json` must have `"experimentalDecorators": true` and `"useDefineForClassFields": false` for decorators to work.
52
+
53
+ ---
54
+
55
+ ## Key Concepts
56
+
57
+ **Needle Engine** ships 3D scenes from Unity or Blender as GLB files and renders them in the browser using Three.js. TypeScript components attached to objects are serialized into the GLB and re-hydrated at runtime.
58
+
59
+ - **Unity workflow:** C# MonoBehaviours → auto-generated TypeScript stubs → GLB export on play/build
60
+ - **Blender workflow:** Components added via the Needle Engine Blender addon → GLB export with component data embedded
61
+ - **Embedding:** `<needle-engine src="assets/scene.glb">` web component creates and manages a 3D context
62
+ - **Context access:** use `onStart(ctx => { ... })` or `onInitialize(ctx => { ... })` lifecycle hooks (preferred); `document.querySelector("needle-engine").context` works but only from UI event handlers
63
+
64
+ ### `<needle-engine>` Attributes
65
+
66
+ Boolean attributes can be disabled with `="0"` (e.g. `camera-controls="0"`).
67
+
68
+ ```html
69
+ <needle-engine
70
+ src="assets/scene.glb"
71
+ camera-controls
72
+ auto-rotate
73
+ autoplay
74
+ background-color="#222"
75
+ environment-image="studio"
76
+ contactshadows
77
+ ></needle-engine>
78
+ ```
79
+
80
+ | Attribute | Description |
81
+ |---|---|
82
+ | `src` | GLB/glTF file path(s) — string, array, or comma-separated |
83
+ | `camera-controls` | Adds default OrbitControls with auto-fit if no `OrbitControls`/`ICameraController` exists in the root GLB. Disable with `="0"` for fully custom camera. To tweak defaults, get `OrbitControls` from the main camera in `onStart` |
84
+ | `auto-rotate` | Auto-rotate the camera (requires `camera-controls`) |
85
+ | `autoplay` | Auto-play animations in the loaded scene |
86
+ | `background-color` | Hex or RGB background color (e.g. `#ff0000`) |
87
+ | `background-image` | Skybox URL or preset: `studio`, `blurred-skybox`, `quicklook`, `quicklook-ar` |
88
+ | `background-blurriness` | Blur intensity for background (0–1) |
89
+ | `environment-image` | Environment lighting image URL or preset (same presets as `background-image`) |
90
+ | `contactshadows` | Enable contact shadows |
91
+ | `tone-mapping` | `none`, `linear`, `neutral`, `agx` |
92
+ | `poster` | Placeholder image URL shown while loading |
93
+ | `loadstart` / `progress` / `loadfinished` | Callback functions for loading lifecycle |
94
+
95
+ ---
96
+
97
+ ## Unity → Needle Cheat Sheet
98
+
99
+ | Unity (C#) | Needle Engine (TypeScript) |
100
+ |---|---|
101
+ | `MonoBehaviour` | `Behaviour` |
102
+ | `[SerializeField]` / public field | `@serializable()` (required for all serialized fields) |
103
+ | `Instantiate(prefab)` | `instantiate(obj)` |
104
+ | `Destroy(obj)` | `destroy(obj)` |
105
+ | `GetComponent<T>()` | `this.gameObject.getComponent(T)` |
106
+ | `AddComponent<T>()` | `this.gameObject.addComponent(T)` |
107
+ | `FindObjectOfType<T>()` | `findObjectOfType(T, ctx)` |
108
+ | `transform.position` | `this.gameObject.worldPosition` (world) / `this.gameObject.position` (local) |
109
+ | `transform.rotation` | `this.gameObject.worldQuaternion` (world) / `this.gameObject.quaternion` (local) |
110
+ | `transform.localScale` | `this.gameObject.worldScale` (world) / `this.gameObject.scale` (local) |
111
+ | `Resources.Load<T>()` | No direct equivalent — use `@serializable(AssetReference)` to assign refs in editor, then `.instantiate()` or `.asset` at runtime |
112
+ | `StartCoroutine()` | `this.startCoroutine()` (in a component; unlike Unity, coroutines stop when the component is disabled) |
113
+ | `Time.deltaTime` | `this.context.time.deltaTime` |
114
+ | `Camera.main` | `this.context.mainCamera` (THREE.Camera) / `this.context.mainCameraComponent` (Needle Camera component) |
115
+ | `Debug.Log()` | `console.log()` |
116
+ | `OnCollisionEnter()` | `onCollisionEnter(col: Collision)` |
117
+ | `OnTriggerEnter()` | `onTriggerEnter(col: Collision)` |
118
+
119
+ ---
120
+
121
+ ## Three.js → Needle Cheat Sheet
122
+
123
+ | Three.js | Needle Engine |
124
+ |---|---|
125
+ | `new Mesh(geo, mat)` | Created in Unity/Blender, exported as GLB; access via `Renderer.sharedMesh` / `Renderer.sharedMaterials` |
126
+ | `scene.add(obj)` | `this.gameObject.add(obj)` or `instantiate(prefab)` |
127
+ | `scene.remove(obj)` | `obj.removeFromParent()` (re-parent) or `destroy(obj)` (permanent) |
128
+ | `obj.position` | `obj.position` (local) / `obj.worldPosition` (world — Needle extension) |
129
+ | `obj.quaternion` | `obj.quaternion` (local) / `obj.worldQuaternion` (world — Needle extension) |
130
+ | `obj.scale` | `obj.scale` (local) / `obj.worldScale` (world — Needle extension) |
131
+ | `obj.getWorldPosition(v)` | `obj.worldPosition` (getter, no temp vec needed) |
132
+ | `obj.traverse(cb)` | `obj.traverse(cb)` (same — it's Three.js underneath) |
133
+ | `obj.children` | `obj.children` (same) |
134
+ | `obj.parent` | `obj.parent` (same) |
135
+ | `raycaster.intersectObjects()` | `this.context.physics.raycast()` (auto BVH, faster) |
136
+ | `renderer.setAnimationLoop(cb)` | `update() {}` in a component, or `onUpdate(cb)` hook |
137
+ | `clock.getDelta()` | `this.context.time.deltaTime` |
138
+ | `new GLTFLoader().load(url)` | `AssetReference.getOrCreate(base, url)` then `.instantiate()`, or `loadAsset(url)` |
139
+
140
+ Needle Engine extends `Object3D` with component methods (`getComponent`, `addComponent`, `worldPosition`, `worldQuaternion`, `worldScale`, `worldForward`, `worldRight`, `worldUp`, `contains`, etc.). `this.gameObject` is the `Object3D` a component is attached to. The underlying Three.js API still works directly.
141
+
142
+ ---
143
+
144
+ ## Creating a New Project
145
+
146
+ ```bash
147
+ npm create needle my-app # Vite (default)
148
+ npm create needle my-app -t react # React + Vite
149
+ npm create needle my-app -t vue # Vue + Vite
150
+ npm create needle my-app -t sveltekit # SvelteKit
151
+ npm create needle my-app -t nextjs # Next.js
152
+ npm create needle my-app -t react-three-fiber # R3F
153
+ ```
154
+
155
+ ---
156
+
157
+ ## Vite Plugin System
158
+
159
+ ```ts
160
+ import { defineConfig } from "vite";
161
+ import { needlePlugins } from "@needle-tools/engine/vite";
162
+
163
+ export default defineConfig(async ({ command }) => ({
164
+ plugins: [
165
+ ...(await needlePlugins(command, {}, {})),
166
+ ],
167
+ }));
168
+ ```
169
+
170
+ ---
171
+
172
+ ## Deployment
173
+
174
+ - **Needle Cloud** — `npx needle-cloud deploy`
175
+ - **Vercel / Netlify** — standard Vite web app
176
+ - **itch.io** — for games
177
+ - **Any static host / FTP** — `npm run build` (or `npm run build:production`) produces a standard dist folder
178
+
179
+ From Unity, built-in deployment components (e.g. `DeployToNetlify`) require a PRO license. Needle Cloud deployment works with the free tier.
180
+
181
+ ---
182
+
183
+ ## Progressive Loading (`@needle-tools/gltf-progressive`)
184
+
185
+ ```ts
186
+ import { useNeedleProgressive } from "@needle-tools/gltf-progressive";
187
+ useNeedleProgressive(gltfLoader, renderer);
188
+ gltfLoader.load(url, (gltf) => scene.add(gltf.scene));
189
+ ```
190
+
191
+ In Needle Engine projects this is built in — configure via **Compression & LOD Settings** in Unity.
192
+
193
+ ---
194
+
195
+ ## Searching the Documentation
196
+
197
+ Use the `needle_search` MCP tool to find relevant docs, forum posts, and community answers:
198
+
199
+ ```
200
+ needle_search("how to play animation clip from code")
201
+ needle_search("SyncedTransform multiplayer")
202
+ needle_search("deploy to Needle Cloud CI")
203
+ ```
204
+
205
+ Use this *before* guessing at API details — the docs are the source of truth.
206
+
207
+ ---
208
+
209
+ ## Common Gotchas
210
+
211
+ - `@registerType` is required or the component won't be instantiated from GLB (Unity/Blender export adds this automatically, but hand-written components need it)
212
+ - GLB assets go in `assets/`, static files (fonts, images) in `public/` (configurable via `needle.config.json`)
213
+ - `useDefineForClassFields: false` must be set in `tsconfig.json` — otherwise decorators silently break field initialization
214
+ - `@syncField()` only triggers on reassignment — mutating an array/object in place won't sync; do `this.arr = this.arr`
215
+ - Physics callbacks (`onCollisionEnter` etc.) require a Needle `Collider` component on the GameObject
216
+ - `removeComponent()` does NOT call `onDestroy` — use `destroy(obj)` for full cleanup
217
+ - Prefer `instantiate()` and `destroy()` functions over `GameObject.instantiate()` / `GameObject.destroy()`
218
+ - `loadAsset()` returns a model wrapper (not an Object3D) — use `.scene` to get the root Object3D
219
+ - `AssetReference.getOrCreateFromUrl()` caches by URL — loading the same URL twice returns the same Object3D. Use `.instantiate()` or `loadAsset()` with `{ context }` for multiple copies
220
+
221
+ ---
222
+
223
+ ## References
224
+
225
+ For detailed API usage, read these reference files:
226
+
227
+ - [Full API Reference](https://raw.githubusercontent.com/needle-tools/ai/refs/heads/main/providers/claude/plugin/skills/needle-engine/references/api.md) — lifecycle, decorators, context API, animation, networking, XR, physics
228
+ - [Framework Integration](https://raw.githubusercontent.com/needle-tools/ai/refs/heads/main/providers/claude/plugin/skills/needle-engine/references/integration.md) — React, Svelte, Vue, vanilla JS examples + SSR patterns
229
+ - [Troubleshooting](https://raw.githubusercontent.com/needle-tools/ai/refs/heads/main/providers/claude/plugin/skills/needle-engine/references/troubleshooting.md) — common errors and fixes
230
+ - [Component Template](https://raw.githubusercontent.com/needle-tools/ai/refs/heads/main/providers/claude/plugin/skills/needle-engine/templates/my-component.ts) — annotated starter component
231
+
232
+ ## Important URLs
233
+
234
+ - Docs: https://engine.needle.tools/docs/
235
+ - Samples: https://engine.needle.tools/samples/
236
+ - GitHub: https://github.com/needle-tools/needle-engine-support
237
+ - npm: https://www.npmjs.com/package/@needle-tools/engine