Null3d Port Threejs
ASecurityPort three.js projects, scenes, examples, components and shaders to the null3D engine (also written null3d, Null3D or null 3d), keeping the same look and gaining speed. Use this skill whenever the user wants to convert, migrate, move or rewrite three.js code to null3D, including code with imports from three or three/addons, THREE calls, GLTFLoader, OrbitControls, InstancedMesh, EffectComposer, ShaderMaterial, onBeforeCompile or TSL, and React Three Fiber or drei scenes. Also use it to answer ...
- 3 stars
- 0 votes
- 0 copies
- 1 view
- Added November 25, 2026
Works with
Security analysis
100/100Pro scans all 15 files and shows the line behind each finding
npx -y skills add null3d-engine/null3d --skill null3d-port-threejs --agent claude-codeAre you the author of Null3d Port Threejs?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/null3d-engine-null3d-port-threejs)---
name: null3d-port-threejs
description: Port three.js projects, scenes, examples, components and shaders to the null3D engine (also written null3d, Null3D or null 3d), keeping the same look and gaining speed. Use this skill whenever the user wants to convert, migrate, move or rewrite three.js code to null3D, including code with imports from three or three/addons, THREE calls, GLTFLoader, OrbitControls, InstancedMesh, EffectComposer, ShaderMaterial, onBeforeCompile or TSL, and React Three Fiber or drei scenes. Also use it to answer what the null3D equivalent of a three.js API is, to estimate how hard a port will be, and to compare a three.js app with its null3D port. Use it even for a single class, snippet or shader. For new null3D work that is not a port, use null3d-develop.
compatibility: The scanner script needs Node.js 18 or newer. Porting needs a null3D project as the target; the null3d-develop skill covers null3D itself.
metadata:
skill-version: 0.1.0
engine-versions: "0.1"
---
# Porting three.js to null3D
A good port looks like the original, runs faster, and reads like null3D code. Translating line by line reaches the first goal at best. Some three.js habits keep the original's speed problems. Among them are objects changed every frame, per-object update methods, allocations in the render loop, and DOM access next to scene code. Many of them do not work at all, because null3D sketch code runs in a worker. So port by intent: work out what each part of the original does, then write that the null3D way.
## A port is a rewrite
No layer runs three.js code on null3D. `@null3d/three-compat` holds three.js's Reinhard and Cineon tone curves and `OutlinePass`'s soft outline, and nothing more. It takes no three.js scene, object, material or shader (`api/three-compat`). So a request such as "port my site" means a rewrite in null3D's own API. Say so to the user before you start:
- The scene code moves into a sketch in a worker. Objects are handles with setters, and many similar objects are rows of a batch that typed arrays fill.
- GLSL in a `ShaderMaterial` or an `onBeforeCompile` patch becomes a WGSL surface function, unlit function or vertex offset (`references/shaders.md`).
- An `EffectComposer` chain becomes `post.set` settings, with `post.addEffect` for each custom pass (`references/post-processing.md`).
Custom shaders meet these limits. Find them in the inventory and agree each workaround with the user (`porting/threejs-overview`, "Limits that a port meets"):
- A batch row carries four values and a color. More per-instance data is packed, or read from a data texture at `input.instance`.
- A custom material's uniforms fit in 32 numbers, less one for each texture, and it samples up to 6 textures.
- One environment lights the whole scene, so a port drops per-material and blended environment maps.
- No `BackSide`: flip the winding, or use `doubleSided: true`.
## 1. Before you start
1. If the null3d-develop skill is available, read its sections 1, 2 and 4: the docs system, the thread model and the performance rules. They apply to every port. Without it, read the engine docs pages `concepts/architecture` and `guides/performance`.
2. Find the engine version in the target project, and read docs pages by ID: `node_modules/@null3d/engine/docs/<id>.md`, or `docs/<id>.md` inside the null3D repository. Use only the APIs that the installed engine's docs and type declarations show. A name that is absent there does not exist, so do not invent it. A page also names the limits of its feature. When a limit affects the port, tell the user and use the workaround that the page gives.
3. Look up three.js APIs in `references/api-mapping.md`. For each three.js API that it lists, it gives the null3D equivalent, a status and a doc ID. The statuses:
- `direct`: same concept, new name.
- `changed`: supported with a different API or pattern; follow the note.
- `manual`: rewrite by hand (shaders, render hooks, some material maps).
- `unsupported`: null3D has no equivalent; use the workaround in the note.
## 2. Workflow
### Phase 1: Inventory
```sh
node <this-skill-dir>/scripts/analyze-threejs.mjs <three-project-dir> --md PORTING-INVENTORY.md --json porting-inventory.json
```
The scanner lists every three.js feature it finds, grouped by status, with file and line references and the null3D equivalent. It also gives warnings (per-frame allocations, DOM access, GLSL, React Three Fiber) and a rough effort size. A mapping note also names what the null3D equivalent does not do, such as options with no equivalent. It matches text patterns, so read the code to confirm each row.
Then tell the user, before writing any code:
- which features need a hand rewrite, which are `unsupported`, and the workaround you propose for each;
- the rough effort.
These decide the scope. Settling them first avoids a half-done port that fails on a feature nobody discussed.
### Phase 2: Baseline
Capture reference images and timings from the running three.js app at fixed camera views, at a fixed canvas size and pixel ratio (`references/verification.md`). Without a baseline, "looks the same" cannot be checked. If the app cannot run, say so, and ask for screenshots or accept a port without parity checks.
### Phase 3: Split the architecture
In null3D, a 3D scene is called a sketch, and it lives in `sketch.ts`. Decide what stays on the page: DOM, HTML UI, GUI panels, the start of the audio, video elements and storage. The rest moves to `sketch.ts`: the scene, the loop, input handling, controls, and state such as scores or selections. Design the few messages between them. Read `references/architecture-and-loop.md`; for React Three Fiber apps, `references/react-three-fiber.md`.
For a large app, a two-step route lowers risk. First port with `createEngine({ sketchThread: 'main' })`, so sketch code runs on the main thread, where it can still reach the DOM. Then move it to the worker once parity holds.
### Phase 4: Port in this order
Check parity images after each step. Each step needs the earlier ones to be visible, and a difference is easier to trace when little has changed.
1. Renderer and loop: `createEngine` on the page, `defineSketch` in `sketch.ts`, the loop body in `onUpdate`.
2. Camera, camera controls and input.
3. Models and textures. The engine loads glTF only: convert FBX, OBJ, STL and PLY files with `bunx @null3d/cli assets convert <in> <out.glb>`. It keeps FBX skins, blend shapes and clips, and turns Draco compression into meshopt. Then optimize them with `bunx @null3d/cli assets optimize <in> <out>`, which writes integer vertices and KTX2 textures. Draco glTF models load as they are, so delete the `DRACOLoader` setup and its decoder files, then convert the models to meshopt with the optimize command.
4. Materials and texture settings (`references/materials.md`).
5. Lights, shadows, environment, fog, background.
6. Geometry, instancing and batching.
7. Interaction: picking, pointer events, labels.
8. Animation.
9. Post-processing (`references/post-processing.md`).
10. Custom shaders (`references/shaders.md`).
11. UI panels, audio, physics and other libraries.
### Phase 5: Make it idiomatic
- Many similar objects become instance batches driven by typed arrays.
- Per-object `update()` methods become loops over arrays.
- Per-frame allocations go away: scratch arrays are created once.
- Objects that move every frame get `dynamic: true`; everything else stays static.
- Layer masks limit picking and special passes.
- Short-lived objects come from pools.
This phase is where most of the speed comes from. Measure before and after it.
### Phase 6: Verify
Compare parity images for every camera view on WebGPU and on WebGL2 (`?gpu=webgl2`). Compare performance with the baseline on the same device and browser. Test on phones if the original targets them. Details: `references/verification.md`.
### Phase 7: Report
Write `PORTING-REPORT.md` with the template in `references/verification.md`. It says what was ported, the parity results per view, and performance before and after. It also says what changed visually and why, and lists every feature left out with its workaround.
## 3. Differences that break ports
| three.js habit | null3D way | Why |
| --- | --- | --- |
| `renderer.render(scene, camera)` inside `requestAnimationFrame` | Logic in `onUpdate(dt)`; the engine renders by itself | Frames run on the render worker's own clock |
| `mesh.position.x += 1` | `mesh.setPosition(x, y, z)`, or typed arrays for many objects | Engine objects are handles; they have no position properties |
| `scene.add(mesh)` | Objects exist as soon as they are created; `setParent` builds hierarchy | Creation is a batched command |
| `scene.add` and `scene.remove` during play, or `object.visible` toggles | Create during setup; hide with `setVisible`; pool short-lived objects in a batch with `setActiveCount` | Creating, destroying and re-parenting rebuild the draw tables; visibility and active counts upload only what changed (`guides/performance`) |
| `new THREE.Vector3()` in the loop | Scratch arrays created once, with array math | Allocations cause garbage-collection stutter |
| Game rules that use three.js math and a server replays, such as a deterministic simulation | The rules import the exact helpers from `@null3d/engine/math/vec3`, `/quat`, `/mat4` and `/math`, and build `setAxisAngle`, `fromEuler`, `rotateX`, `rotateY`, `rotateZ`, `slerp`, `vec3.angle` and `math.damp` with `createMath` on an exact math library. The page passes `input: false` to `createEngine` when the game records its own input | `Math.sin` and the other functions that three.js's math calls differ between JavaScript engines, and `@null3d/engine` brings engine code that a replaying engine refuses (`guides/deterministic-simulation`) |
| `document`, `window` and DOM events next to scene code | The page owns the DOM; input arrives in `ctx.input`; messages carry data | Sketch code runs in a worker without a DOM |
| `obj.userData`, subclasses of `Mesh` | Your own maps or typed arrays keyed by handle or row | Engine objects have no `userData`, and their types refuse new properties |
| `onBeforeRender`, per-draw callbacks | `onUpdate` or `onLateUpdate`, or a declared pass | No sketch code runs in the render worker |
| `material.needsUpdate = true` to switch features at run time | Create both material variants while loading. Swap with `setMaterial` for a rare change; for a frequent one, keep two objects and swap their visibility | A shader change needs a new pipeline, whose objects draw nothing until it is built, and `setMaterial` rebuilds the draw tables |
| `InstancedMesh.setMatrixAt` with a dummy `Object3D` | Write `positions`, `rotations` and `scales` arrays | No matrix composition in JavaScript: in the S1 benchmark it cost three.js about 0.5 ms per frame for 100,000 instances. The loop's own motion math costs the same in both engines, so keep it tight |
| `object.traverse` every frame | Collect the handles you need at setup | Traversal costs work every frame |
| `geometry.attributes.position.needsUpdate = true` after changing vertices | Keep the array, change it, and call `mesh.updateVertices('positions', positions)` | It allocates nothing and uploads only the vertices that changed; the mesh keeps its first bounding sphere, so add `setBounds` when vertices move outside it |
| `TorusKnotGeometry`, `ExtrudeGeometry`, `Shape` and the other extra geometry classes | `geometry.fromArrays(torusKnot({ ... }))` and the same classes from `@null3d/geometry`, with points as arrays | The functions return three.js's vertices for `fromArrays`; `ExtrudeGeometry`'s caps and sides become one mesh |
| `mergeGeometries` to cut draw calls | Separate objects that share a mesh and material | They already share one draw (`guides/performance`) |
| `matrixAutoUpdate = false` on still objects | Nothing | Objects are static by default and cost nothing until a setter changes them |
| `renderer.compile` or `compileAsync` after loading | `await scene.warmUp()` in the sketch, and wait for `engine.firstFrame` on the page | The first frame waits for its pipelines; warm up a later loading stage before you show it |
| Resize handlers and `setSize` | Nothing | The engine follows the canvas size |
| `camera.setViewOffset` to put the subject beside the page's text, or a camera turned to move it off center | `camera.setLensShift(x, y)`, in fractions of the view's width and height, with y up (`api/cameras`) | A shift keeps the perspective, and culling, shadows, picking and labels follow it. A turned camera makes upright lines lean |
| `EffectComposer` pass chains | `post.set` settings, and `post.addEffect` for each custom `ShaderPass` | The chain is built in and merged into few passes. Each custom effect is a full-screen pass of its own |
| `localStorage` in scene code | Keep it on the page, or use IndexedDB, which workers have | Workers have no `localStorage` |
| `object.static = true` on still meshes (`WebGPURenderer`) | Nothing | Objects are static by default. In three.js r186 and r187 the flag never applies to a node material: its refresh check returns before it reads the flag |
| Frame counters on `requestAnimationFrame`, `renderer.waitForGPU()` or `onSubmittedWorkDone` | `engine.measure(seconds)`: `completedFps`, `presentedFps` and `gpuLatencyMs` | The engine tracks every frame to the GPU's finish. three.js r186 removed `waitForGPU()` |
| Many `InstancedMesh` objects merged into fewer, to cut shader builds | Batches and objects split as the scene needs | Up to three.js r186, each instanced mesh's id is in its shader cache key, so each one builds its own shaders in every pass. r187 shares them between instanced meshes of the same kind. null3D builds a pipeline per shading model and vertex format, never per object |
| A worker renderer of the app's own, through `transferControlToOffscreen` | Delete it, and pass the canvas to `createEngine` | The engine draws from a worker already. A canvas that has a context cannot move to a worker, so draw nothing on it first |
| A scheduler that yields with `setTimeout(0)` or a `MessageChannel` while the scene builds | The setup builds the first view; later heavy work runs in a task on the job workers (`runTask`), or in slices per frame | The setup runs in the sketch worker, so the page stays responsive (section 11 of the null3d-develop skill's performance reference) |
## 4. Settings that change the look
When parity images differ, check these first.
- Color management. three.js r152 and later, like null3D, read hex colors as sRGB and light in linear space. Older projects (with `outputEncoding`, or `ColorManagement.enabled = false`) look different by design; decide with the user which look to keep.
- Texture color spaces. Color and emissive maps are sRGB; normal, roughness, metalness and AO maps are linear. If the original forgot the sRGB flag, its look is wrong in a way users may like; ask before "fixing" it.
- Tone mapping. three.js defaults to none, and null3D to AgX with a punchy look (`'agx-punchy'`). A port that set `AgXToneMapping` sets `'agx'`, one that set `ACESFilmicToneMapping` sets `'aces'`, and one that set `ReinhardToneMapping` or `CineonToneMapping` sets the `reinhard` or `cineon` curve of `@null3d/three-compat` (`references/post-processing.md`). Set `post.set({ toneMapping: 'none' })` to match an original without tone mapping, and copy `toneMappingExposure` to `exposure` unchanged. null3D multiplies the exposure into each light instead of the finished picture; the image is the same, and bloom's threshold keeps its meaning.
- Light units. null3D uses physical units, as three.js r155 and later do: candela for point and spot lights, lux for the others. Scenes tuned with legacy lights need new intensities. `light.power = lumens` becomes `intensity: lumens, intensityUnit: 'lumen'`, which divides as `power` does. A scene in real units, such as a 100,000-lux sun, sets `post.set({ ev100 })`; `exposure` then acts as exposure compensation (`api/post`).
- Point, spot and area light range. three.js `distance: 0` means infinite range, and `RectAreaLight` has no range at all; null3D needs a finite `range` on each. Pick the distance where the light no longer matters; the edge of the light may differ slightly. `RectAreaLight` maps to `scene.createRectAreaLight` with the same width, height, color and intensity; drop `RectAreaLightUniformsLib.init()`.
- Shadows. three.js shadow cameras are hand-fitted; null3D cascades fit the view. Tune `mapSize`, `cascades` and bias rather than copying `shadow.camera`. The `shadowFilter` quality setting replaces `shadowMap.type` and `shadow.radius`. three.js picks shadow casters by the camera's layers, and null3D by the light's. Give each light that casts shadows the layers of every object that must cast (`concepts/render-layers`). For one subject in close-up, `sun.setShadow({ hero: object })` gives it a tightly fitted shadow map of its own; three.js has no equivalent, and a hand-fitted second light can go.
- Pixel ratio. Many three.js apps render at the full device pixel ratio (3 on many phones); null3D's presets cap it (`concepts/quality-presets`). For parity tests, fix the pixel ratio to 1 in both.
- Material approximations. Lambert, Phong and Toon materials become standard materials or surface functions; small differences are expected (`references/materials.md`).
- Post effects. Bloom is a chain of mip levels, so map `UnrealBloomPass`'s, `bloom()`'s or pmndrs `BloomEffect`'s settings onto it with the table or `scripts/map-bloom.mjs` (`references/post-processing.md`). The match holds at one canvas size; on a larger screen null3D's glow looks wider. Color grading and the vignette take `LUTPass`'s and `VignetteShader`'s numbers as they are. `@null3d/three-compat`'s `createOutlinePass` keeps `OutlinePass`'s soft edge, glow and pulse. Its `strength`, `thickness`, `glow` and `pulsePeriod` take the values of `edgeStrength`, `edgeThickness`, `edgeGlow` and `pulsePeriod` (`references/post-processing.md`). The core's `outline` draws a cheaper crisp line instead: map `OutlinePass`'s colors to `color` and `hiddenColor`, and `edgeThickness` to a `width` of about twice its value. Ambient occlusion runs `GTAOPass`'s steps, so copy its settings. It darkens only the ambient light. Match other occlusion passes by tuning, one effect at a time.
## 5. When there is no equivalent
For `unsupported` rows:
1. Tell the user before porting the rest, with the workaround from the mapping table and what it costs visually or in behavior.
2. If the feature is central, for example an XR experience, recommend waiting rather than porting.
3. Never drop a feature silently. List every omission and workaround in the report.
4. Do not layer a three.js canvas over the null3D canvas to keep one effect. Two GPU contexts double memory and break the frame pacing; use it only as a stopgap the user explicitly accepts.
## 6. A small example
Before, in three.js:
```js
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(devicePixelRatio);
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 1.5, 4);
const controls = new OrbitControls(camera, renderer.domElement);
scene.add(new THREE.AmbientLight(0xffffff, 0.4));
const sun = new THREE.DirectionalLight(0xffffff, 3);
sun.position.set(1, 2, 1);
scene.add(sun);
const cube = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshStandardMaterial({ color: 0x4a8cff }));
scene.add(cube);
addEventListener('resize', () => { camera.aspect = innerWidth / innerHeight; camera.updateProjectionMatrix(); renderer.setSize(innerWidth, innerHeight); });
renderer.setAnimationLoop((t) => { cube.rotation.y = t / 1000; controls.update(); renderer.render(scene, camera); });
```
After, in null3D:
```ts
// page.ts
import { createEngine } from '@null3d/engine';
await createEngine({ canvas: document.querySelector('canvas')!, sketch: new URL('./sketch.ts', import.meta.url) });
```
```ts
// sketch.ts
import { defineSketch } from '@null3d/engine';
import { createOrbitControls } from '@null3d/controls';
export default defineSketch(async (ctx) => {
const { scene, geometry, materials, post, time } = ctx;
post.set({ toneMapping: 'none' }); // three.js default, for parity
const camera = scene.createPerspectiveCamera({ fov: 60, near: 0.1, far: 100, position: [0, 1.5, 4] });
scene.setActiveCamera(camera);
const controls = createOrbitControls(ctx, camera);
scene.createAmbientLight({ intensity: 0.4 });
scene.createDirectionalLight({ direction: [-1, -2, -1], intensity: 3 }); // from the light's position to its target
const cube = scene.createMesh({
mesh: geometry.box(),
material: materials.standard({ color: 0x4a8cff }),
dynamic: true,
});
return {
onUpdate(dt) {
cube.setRotationEuler(0, time.now, 0);
controls.update(dt);
},
};
});
```
The resize handler, the pixel-ratio call, `scene.add` and the render call disappear. A directional light takes the direction its light travels, where three.js takes a position and a target. The canvas lives in `index.html`, and the engine sizes it from CSS.
## 7. References in this skill
- `references/api-mapping.md`: the full mapping table, grouped by area. Search it for a class or property name.
- `references/threejs-mapping.json`: the same data, used by the scanner.
- `scripts/analyze-threejs.mjs`: the scanner (phase 1).
- `scripts/map-bloom.mjs`: maps `UnrealBloomPass`, `bloom()` and pmndrs `BloomEffect` settings onto null3D's bloom for a canvas size (`references/post-processing.md`, section 5).
- `references/architecture-and-loop.md`: what goes on the page and what goes in the sketch; loops, input, messages, per-object classes.
- `references/materials.md`: every material and texture parameter, approximations, and toon, matcap and clipping recipes.
- `references/shaders.md`: GLSL to WGSL, three.js built-ins, `onBeforeCompile` patterns, TSL, worked examples and pitfalls.
- `references/post-processing.md`: composer passes, pmndrs effects and three.js TSL post nodes, mapped to `post.set` and `post.addEffect`, with the bloom mapping tables.
- `references/react-three-fiber.md`: R3F and drei to null3D, with a React wrapper component.
- `references/verification.md`: baseline capture, parity tests, performance comparison and the report template.
## 8. Before you finish
- The inventory was confirmed against the code, and the user agreed on how to handle every `manual` and `unsupported` item.
- Every camera view passes its parity test on WebGPU and WebGL2, or its difference is explained and accepted.
- Performance was measured against the baseline on the same device and browser, with a production build of each app (`references/verification.md`, section 5).
- Per-frame code allocates nothing, and many similar objects use instance batches.
- DOM and storage code sits in `page.ts`. three.js audio becomes `@null3d/sound`: sounds made in the sketch, started on the page.
- `PORTING-REPORT.md` lists results, visual changes and omissions.
Files in this skill
- SKILL.md
- evals/evals.json
- evals/files/Viewer.jsx
- evals/files/threejs-app/package.json
- evals/files/threejs-app/src/main.js
- evals/files/wave-shader.js
- evals/trigger-queries.json
- references/architecture-and-loop.md
- references/materials.md
- references/post-processing.md
- references/react-three-fiber.md
- references/shaders.md
- references/verification.md
- scripts/analyze-threejs.mjs
- scripts/map-bloom.mjs
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.