Skip to content

armgit / viz3d

Public Imported from GitHub
0

An AI agent skill that turns any subject into a 3D film or app

Add the pixel diorama look and fuller looks.md descriptions

Nguyen Xuan Anc1ebd031e769
diff --git a/plugins/visualize-in-3d/skills/visualize-in-3d/references/looks.md b/plugins/visualize-in-3d/skills/visualize-in-3d/references/looks.md
index f81e496..82267bd 100644
--- a/plugins/visualize-in-3d/skills/visualize-in-3d/references/looks.md
+++ b/plugins/visualize-in-3d/skills/visualize-in-3d/references/looks.md
@@ -2,7 +2,7 @@
Each topic file names one look. The look sets the world's rendering, the UI skin and what the film shows; the data
model, the dashboard parts and the film's click-driven shape stay the same. Reading map: Toy isometric (default),
-Point-cloud survey, Photoreal terrain analysis, Dark documentary, Choosing.
+Point-cloud survey, Photoreal terrain analysis, Dark documentary, Pixel diorama, Choosing.
Reference stills or clips for a look are optional. If you have some, save them under `{{WORK_DIR}}/references/<slug>/`
and point the briefs at them; they are for study only and never ship in the app or the film. Without them, the
@@ -89,10 +89,54 @@ frames, music or text). Fits a case file or a piece of history: a crime, a disap
- Motion and sound: slow pushes and dissolves, nothing snappy; a low ambient drone with soft hits on reveals.
- Respect: victims are named with dates and treated as people; the film follows the investigation, not the gore.
+## Pixel diorama
+
+Reference: the "small floating world" pixel style seen in short battle and port clips. Reuse
+`{{SKILL_DIR}}/scripts/pixel-pass.js` (copy to `src/pixel-pass.js`; it only imports `three`). Fits any place seen as
+a small floating world with many tiny units: ports, yards, farms, camps, fairs, battles. Needs no Blender: boxes,
+extrusions and a few icospheres read well at this resolution.
+
+- Rendering: a real 3D scene drawn at about 360 px across (use `pixelScale` 5 at 1920x1080, so 384x216) and
+ upscaled with nearest-neighbour; one directional sun with real cast shadows; flat toon shading, 3 to 4 tones per
+ material (`createToonMaterial`); dark 1 px outlines around objects from depth edges, light creases from normal
+ edges; a limited natural palette (greens, soil browns, concrete, teal water, muted colours); a plain
+ paper-coloured background; the world is a floating block of 0.5 u cells (a cap cell over a soil column) with
+ ragged edges (cells near the border drop out by noise) and a soil cliff below.
+- Camera: orthographic, azimuth -135 and elevation 30 for the iso views. `snapOrthographicCamera` snaps the camera to
+ whole low-res pixels along its right and up axes and returns the sub-pixel residual that `PixelPass.render` applies
+ while upscaling, so the camera drifts smoothly and nothing shimmers. Ease the focus, zoom, azimuth and elevation
+ yourself every frame; keep zoom changes slow (they re-sample every texel).
+- Smoke, dust and fade-ins: clusters of 4 or 5 icospheres sharing one material with `addDitherDissolve`, an ordered
+ Bayer discard in low-res pixels. Mark the group `userData.skipNormalPass = true`. Units fade in at the edge of the
+ world with the same helper, so nothing appears out of thin air.
+- Water: a flat toon slab, deeper-blue lane stripes, pale arcs (`RingGeometry` arcs that blink on for the middle of
+ their life) and sparkles (tiny cubes blinking in steps), plus arcs spawned behind moving boats.
+- Traps: pixel scale too high (units under 3 px vanish; keep boxes at least 4 px), too many shadow-casting instanced
+ meshes (cast shadows only from objects, not ground cells), a half-float render target needs
+ `EXT_color_buffer_half_float` (all WebGL2 desktop), clipping planes need `renderer.localClippingEnabled = true`.
+- Views, each chosen for what it shows best: a wide iso overview, a close-up of the busiest spot, a map-style
+ top-down (azimuth 0, elevation 76) for flows, a low side view for arrivals, and a side cutaway of one unit. A
+ cutaway clips the camera-side half of that unit with a clipping plane (`material.clippingPlanes`,
+ `side = DoubleSide`) and hides anything in front, so its inside shows while work goes on. Build the interior
+ inside the unit from the start.
+- UI: flat paper panels with a 3 px ink outline and a hard offset shadow, a pixel font (Pixelify Sans, OFL,
+ bundled), segmented progress bars, 8x8 bitmap status icons with a different shape per state, quiet text tabs.
+- The floating block itself needs detail, or its corners and underside look sharp and plain:
+ - corners rounded in pixel steps, with an eroded edge
+ - an underside of rock strata that tapers to hanging spires of varied length, with roots
+ - hanging vines that sway over the edges
+ - a waterfall where water meets the edge, and a lava stream where lava meets it
+ - loose rocks, grass tufts and moss along the top edge, instanced
+- Characters, creatures or vehicles acting in this look (a fight, a chase): `action.md`.
+- Film: real clicks on a virtual clock (`film.md`, Live app films): overview, close-up, click a unit (card and
+ follow), cutaway, top-down, low view, overview. Check frames at the real size; pixels must be square and crisp.
+
## Choosing
Toy isometric is the default for every topic. The point-cloud look can read as dull on a busy site, so use it when
it is asked for. Otherwise pick the look the place itself suggests: built places full of moving vehicles go toy
isometric; land and water seen from above go point-cloud; an event spreading across real land over time goes
-photoreal terrain; a case or a piece of history told in order goes dark documentary. Write the look's name in the
-topic file. Add a new look only after saving its reference and writing a section here.
+photoreal terrain; a case or a piece of history told in order goes dark documentary; a small floating world with
+many tiny units, or a battle between creatures, goes pixel diorama. A look the user shows from a reference video is
+studied frame by frame (`scripts/contact-sheet.sh`) and written up as a new section here. Write the look's name in
+the topic file. Add a new look only after saving its reference and writing a section here.
diff --git a/plugins/visualize-in-3d/skills/visualize-in-3d/scripts/pixel-pass.js b/plugins/visualize-in-3d/skills/visualize-in-3d/scripts/pixel-pass.js
new file mode 100644
index 0000000..3c5a8b8
--- /dev/null
+++ b/plugins/visualize-in-3d/skills/visualize-in-3d/scripts/pixel-pass.js
@@ -0,0 +1,353 @@
+// Pixel pass for three.js: a real 3D scene drawn at a low internal resolution and upscaled with nearest-neighbour
+// sampling, with outlines from depth and normal edges, optional palette quantising, a dither-dissolve material
+// helper and a pixel-snapped orthographic camera. Used by the "pixel diorama" look (references/looks.md).
+//
+// Copy this file into an app (src/pixel-pass.js); it only imports "three" (tested with 0.186).
+//
+// const pass = new PixelPass(renderer, { pixelScale: 5, outlineColor: 0x1b1722 });
+// pass.setSize(window.innerWidth, window.innerHeight); // canvas pixels, pixel ratio 1
+// const residual = snapOrthographicCamera(camera, { focus, azimuthDegrees: 45, elevationDegrees: 30,
+// distance: 120, viewHeight: 40, lowResWidth: pass.lowResWidth, lowResHeight: pass.lowResHeight });
+// pass.render(scene, camera, residual); // every frame instead of renderer.render
+
+import * as THREE from 'three';
+
+const MAXIMUM_PALETTE_COLORS = 32;
+
+const BAYER_FUNCTION_GLSL = /* glsl */ `
+float bayer4(vec2 pixelCoordinate) {
+ vec2 cell = mod(floor(pixelCoordinate), 4.0);
+ float index = cell.x + cell.y * 4.0;
+ // 4x4 Bayer matrix, values 0..15, laid out row by row.
+ float values[16] = float[16](0.0, 8.0, 2.0, 10.0, 12.0, 4.0, 14.0, 6.0, 3.0, 11.0, 1.0, 9.0, 15.0, 7.0, 13.0, 5.0);
+ return (values[int(index)] + 0.5) / 16.0;
+}
+`;
+
+const FULLSCREEN_VERTEX_SHADER = /* glsl */ `
+void main() {
+ gl_Position = vec4(position.xy, 0.0, 1.0);
+}
+`;
+
+const COMPOSITE_FRAGMENT_SHADER = /* glsl */ `
+precision highp float;
+uniform sampler2D colorTexture;
+uniform sampler2D depthTexture;
+uniform sampler2D normalTexture;
+uniform vec2 lowResSize; // size of the render target in texels (includes the 1 px margin)
+uniform vec2 residual; // sub-pixel camera offset in low-res pixels
+uniform float pixelScale; // canvas pixels per low-res pixel
+uniform float cameraNear;
+uniform float cameraFar;
+uniform vec3 outlineColor;
+uniform float outlineTint; // 0 = flat outline colour, 1 = outline takes the darkened colour of the near object
+uniform float depthEdgeThreshold; // world units
+uniform float normalEdgeThreshold; // 0..2, difference of unit normals
+uniform float creaseDarkness; // 0 = no inner lines
+uniform int paletteSize;
+uniform vec3 palette[${MAXIMUM_PALETTE_COLORS}];
+uniform float paletteDither;
+
+${BAYER_FUNCTION_GLSL}
+
+float worldDepth(vec2 texel) {
+ float raw = texture2D(depthTexture, (texel + 0.5) / lowResSize).x;
+ return cameraNear + raw * (cameraFar - cameraNear);
+}
+
+vec3 normalAt(vec2 texel) {
+ return texture2D(normalTexture, (texel + 0.5) / lowResSize).xyz * 2.0 - 1.0;
+}
+
+vec3 colorAt(vec2 texel) {
+ return texture2D(colorTexture, (texel + 0.5) / lowResSize).rgb;
+}
+
+vec3 quantise(vec3 color, vec2 lowCoordinate) {
+ if (paletteSize == 0) return color;
+ float bias = (bayer4(lowCoordinate) - 0.5) * paletteDither;
+ vec3 biased = color + bias;
+ float bestDistance = 1e9;
+ vec3 best = palette[0];
+ for (int index = 0; index < ${MAXIMUM_PALETTE_COLORS}; index++) {
+ if (index >= paletteSize) break;
+ vec3 difference = biased - palette[index];
+ float distanceSquared = dot(difference, difference);
+ if (distanceSquared < bestDistance) {
+ bestDistance = distanceSquared;
+ best = palette[index];
+ }
+ }
+ return best;
+}
+
+void main() {
+ // Canvas pixel -> low-res texel. The +1 skips the margin, the residual slides the image by the camera's
+ // sub-pixel offset so the camera moves smoothly while the pixels themselves never shimmer.
+ vec2 lowCoordinate = gl_FragCoord.xy / pixelScale + residual + 1.0;
+ vec2 texel = floor(lowCoordinate);
+
+ vec3 color = colorAt(texel);
+ float centerDepth = worldDepth(texel);
+ vec3 centerNormal = normalAt(texel);
+
+ vec2 offsets[4] = vec2[4](vec2(1.0, 0.0), vec2(-1.0, 0.0), vec2(0.0, 1.0), vec2(0.0, -1.0));
+ float nearestNeighbourDepth = centerDepth;
+ vec3 nearestNeighbourColor = color;
+ float creaseAmount = 0.0;
+ for (int index = 0; index < 4; index++) {
+ vec2 neighbour = texel + offsets[index];
+ float neighbourDepth = worldDepth(neighbour);
+ if (neighbourDepth < nearestNeighbourDepth) {
+ nearestNeighbourDepth = neighbourDepth;
+ nearestNeighbourColor = colorAt(neighbour);
+ }
+ if (abs(neighbourDepth - centerDepth) < depthEdgeThreshold) {
+ float normalDifference = length(normalAt(neighbour) - centerNormal);
+ creaseAmount = max(creaseAmount, step(normalEdgeThreshold, normalDifference));
+ }
+ }
+
+ // Outline: this pixel lies behind a clearly nearer neighbour, so the line is drawn just outside the silhouette.
+ float isOutline = step(depthEdgeThreshold, centerDepth - nearestNeighbourDepth);
+ vec3 lineColor = mix(outlineColor, nearestNeighbourColor * 0.35, outlineTint);
+ color = mix(color, color * (1.0 - creaseDarkness), creaseAmount);
+ color = quantise(color, lowCoordinate);
+ color = mix(color, lineColor, isOutline);
+
+ gl_FragColor = vec4(color, 1.0);
+ #include <colorspace_fragment>
+}
+`;
+
+// A stepped gradient map for MeshToonMaterial: "tones" flat bands from dark to light, no smoothing.
+export function createToonGradient(tones = 3, darkest = 0.45) {
+ const data = new Uint8Array(tones * 4);
+ for (let tone = 0; tone < tones; tone++) {
+ const level = tones === 1 ? 1 : darkest + (1 - darkest) * (tone / (tones - 1));
+ const byte = Math.round(level * 255);
+ data.set([byte, byte, byte, 255], tone * 4);
+ }
+ const texture = new THREE.DataTexture(data, tones, 1, THREE.RGBAFormat);
+ texture.minFilter = THREE.NearestFilter;
+ texture.magFilter = THREE.NearestFilter;
+ texture.generateMipmaps = false;
+ texture.needsUpdate = true;
+ return texture;
+}
+
+const gradientCache = new Map();
+
+// Flat toon material with 3 to 4 tones. Casts and receives real shadows through the scene's directional light.
+export function createToonMaterial(color, options = {}) {
+ const tones = options.tones ?? 3;
+ const darkest = options.darkest ?? 0.5;
+ const cacheKey = `${tones}:${darkest}`;
+ if (!gradientCache.has(cacheKey)) gradientCache.set(cacheKey, createToonGradient(tones, darkest));
+ return new THREE.MeshToonMaterial({ color, gradientMap: gradientCache.get(cacheKey), ...options.materialOptions });
+}
+
+// Adds an ordered-dither dissolve to any material. The returned object holds `amount` (0 = solid, 1 = gone);
+// materials that share one controller dissolve together. The Bayer pattern is read in low-res pixels because
+// the scene is drawn at the low resolution, so the dither cells are real pixels.
+export function addDitherDissolve(material, controller = { amount: 0 }) {
+ const previousHook = material.onBeforeCompile;
+ material.onBeforeCompile = (shader, renderer) => {
+ if (previousHook) previousHook(shader, renderer);
+ shader.uniforms.ditherAmount = controller.uniform ?? (controller.uniform = { value: controller.amount });
+ shader.fragmentShader = shader.fragmentShader
+ .replace('void main() {', `uniform float ditherAmount;\n${BAYER_FUNCTION_GLSL}\nvoid main() {`)
+ .replace('#include <clipping_planes_fragment>', `#include <clipping_planes_fragment>\n if (bayer4(gl_FragCoord.xy) < ditherAmount) discard;`);
+ };
+ material.customProgramCacheKey = () => 'dither-dissolve';
+ material.needsUpdate = true;
+ controller.uniform = controller.uniform ?? { value: controller.amount };
+ controller.set = (amount) => {
+ controller.amount = amount;
+ controller.uniform.value = amount;
+ };
+ return controller;
+}
+
+// Places an orthographic camera on the low-res pixel grid and returns the sub-pixel residual (in low-res pixels)
+// for PixelPass.render. The camera looks at `focus` from the given compass azimuth and elevation. Snapping the
+// camera to whole low-res pixels along its own right and up axes keeps every texel of the world on a texel of
+// the image while the focus moves continuously, which removes shimmer; the residual is applied when upscaling.
+export function snapOrthographicCamera(camera, settings) {
+ const { focus, azimuthDegrees, elevationDegrees, distance, viewHeight, lowResWidth, lowResHeight } = settings;
+ const azimuth = THREE.MathUtils.degToRad(azimuthDegrees);
+ const elevation = THREE.MathUtils.degToRad(elevationDegrees);
+ const toCamera = new THREE.Vector3(
+ Math.sin(azimuth) * Math.cos(elevation),
+ Math.sin(elevation),
+ Math.cos(azimuth) * Math.cos(elevation),
+ );
+ const worldUp = new THREE.Vector3(0, 1, 0);
+ const right = new THREE.Vector3().crossVectors(worldUp, toCamera).normalize();
+ const up = new THREE.Vector3().crossVectors(toCamera, right).normalize();
+
+ const unitsPerPixel = viewHeight / lowResHeight;
+ const rightCoordinate = focus.dot(right);
+ const upCoordinate = focus.dot(up);
+ const snappedRight = Math.round(rightCoordinate / unitsPerPixel) * unitsPerPixel;
+ const snappedUp = Math.round(upCoordinate / unitsPerPixel) * unitsPerPixel;
+ const snappedFocus = focus
+ .clone()
+ .addScaledVector(right, snappedRight - rightCoordinate)
+ .addScaledVector(up, snappedUp - upCoordinate);
+
+ // The render target has a one pixel margin on every side, so the frustum covers two extra pixels.
+ const halfHeight = ((lowResHeight + 2) * unitsPerPixel) / 2;
+ const halfWidth = ((lowResWidth + 2) * unitsPerPixel) / 2;
+ camera.left = -halfWidth;
+ camera.right = halfWidth;
+ camera.top = halfHeight;
+ camera.bottom = -halfHeight;
+ camera.near = 0.5;
+ camera.far = distance * 2.2;
+ camera.position.copy(snappedFocus).addScaledVector(toCamera, distance);
+ camera.up.set(0, 1, 0);
+ camera.lookAt(snappedFocus);
+ camera.updateProjectionMatrix();
+ camera.updateMatrixWorld(true);
+
+ return new THREE.Vector2(
+ (rightCoordinate - snappedRight) / unitsPerPixel,
+ (upCoordinate - snappedUp) / unitsPerPixel,
+ );
+}
+
+export class PixelPass {
+ constructor(renderer, options = {}) {
+ this.renderer = renderer;
+ this.pixelScale = options.pixelScale ?? 5;
+ this.backgroundColor = new THREE.Color(options.backgroundColor ?? 0xf4f1e8);
+ this.lowResWidth = 1;
+ this.lowResHeight = 1;
+
+ this.colorTarget = null;
+ this.normalTarget = null;
+ this.normalMaterial = new THREE.MeshNormalMaterial();
+
+ this.compositeMaterial = new THREE.ShaderMaterial({
+ vertexShader: FULLSCREEN_VERTEX_SHADER,
+ fragmentShader: COMPOSITE_FRAGMENT_SHADER,
+ depthTest: false,
+ depthWrite: false,
+ uniforms: {
+ colorTexture: { value: null },
+ depthTexture: { value: null },
+ normalTexture: { value: null },
+ lowResSize: { value: new THREE.Vector2(1, 1) },
+ residual: { value: new THREE.Vector2() },
+ pixelScale: { value: this.pixelScale },
+ cameraNear: { value: 0.5 },
+ cameraFar: { value: 100 },
+ outlineColor: { value: new THREE.Color(options.outlineColor ?? 0x1b1722) },
+ outlineTint: { value: options.outlineTint ?? 0.35 },
+ depthEdgeThreshold: { value: options.depthEdgeThreshold ?? 0.55 },
+ normalEdgeThreshold: { value: options.normalEdgeThreshold ?? 0.9 },
+ creaseDarkness: { value: options.creaseDarkness ?? 0.22 },
+ paletteSize: { value: 0 },
+ palette: { value: Array.from({ length: MAXIMUM_PALETTE_COLORS }, () => new THREE.Color()) },
+ paletteDither: { value: options.paletteDither ?? 0.0 },
+ },
+ });
+ this.quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), this.compositeMaterial);
+ this.quad.frustumCulled = false;
+ this.quadScene = new THREE.Scene();
+ this.quadScene.add(this.quad);
+ this.quadCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
+
+ if (options.palette) this.setPalette(options.palette);
+ }
+
+ // Hex colours (sRGB). Pass null to switch quantising off. At most 32 colours.
+ setPalette(hexColors, ditherStrength = this.compositeMaterial.uniforms.paletteDither.value) {
+ const uniforms = this.compositeMaterial.uniforms;
+ if (!hexColors || hexColors.length === 0) {
+ uniforms.paletteSize.value = 0;
+ return;
+ }
+ const count = Math.min(hexColors.length, MAXIMUM_PALETTE_COLORS);
+ for (let index = 0; index < count; index++) uniforms.palette.value[index].set(hexColors[index]);
+ uniforms.paletteSize.value = count;
+ uniforms.paletteDither.value = ditherStrength;
+ }
+
+ setBackground(color) {
+ this.backgroundColor.set(color);
+ }
+
+ // Canvas size in pixels. Keep the renderer's pixel ratio at 1 so the pixel scale is exact.
+ setSize(canvasWidth, canvasHeight) {
+ this.renderer.setPixelRatio(1);
+ this.renderer.setSize(canvasWidth, canvasHeight, false);
+ this.lowResWidth = Math.ceil(canvasWidth / this.pixelScale);
+ this.lowResHeight = Math.ceil(canvasHeight / this.pixelScale);
+ this.colorTarget?.dispose();
+ this.normalTarget?.dispose();
+ const width = this.lowResWidth + 2;
+ const height = this.lowResHeight + 2;
+ this.colorTarget = new THREE.WebGLRenderTarget(width, height, {
+ type: THREE.HalfFloatType,
+ minFilter: THREE.NearestFilter,
+ magFilter: THREE.NearestFilter,
+ depthBuffer: true,
+ samples: 0,
+ });
+ this.colorTarget.depthTexture = new THREE.DepthTexture(width, height);
+ this.colorTarget.depthTexture.minFilter = THREE.NearestFilter;
+ this.colorTarget.depthTexture.magFilter = THREE.NearestFilter;
+ this.normalTarget = new THREE.WebGLRenderTarget(width, height, {
+ type: THREE.UnsignedByteType,
+ minFilter: THREE.NearestFilter,
+ magFilter: THREE.NearestFilter,
+ depthBuffer: true,
+ });
+ this.compositeMaterial.uniforms.lowResSize.value.set(width, height);
+ this.compositeMaterial.uniforms.pixelScale.value = this.pixelScale;
+ }
+
+ // Draws the scene to the canvas. `residual` comes from snapOrthographicCamera.
+ render(scene, camera, residual = new THREE.Vector2()) {
+ const renderer = this.renderer;
+ const previousBackground = scene.background;
+ const previousAutoUpdate = renderer.shadowMap.autoUpdate;
+
+ scene.background = this.backgroundColor;
+ renderer.setRenderTarget(this.colorTarget);
+ renderer.clear();
+ renderer.render(scene, camera);
+
+ // Normal pass for crease lines. Objects flagged userData.skipNormalPass (dissolving smoke) are hidden in it.
+ const hidden = [];
+ scene.traverse((object) => {
+ if (object.visible && object.userData.skipNormalPass) {
+ object.visible = false;
+ hidden.push(object);
+ }
+ });
+ scene.background = new THREE.Color(0x8080ff);
+ scene.overrideMaterial = this.normalMaterial;
+ renderer.shadowMap.autoUpdate = false;
+ renderer.setRenderTarget(this.normalTarget);
+ renderer.clear();
+ renderer.render(scene, camera);
+ renderer.shadowMap.autoUpdate = previousAutoUpdate;
+ scene.overrideMaterial = null;
+ scene.background = previousBackground;
+ for (const object of hidden) object.visible = true;
+
+ const uniforms = this.compositeMaterial.uniforms;
+ uniforms.colorTexture.value = this.colorTarget.texture;
+ uniforms.depthTexture.value = this.colorTarget.depthTexture;
+ uniforms.normalTexture.value = this.normalTarget.texture;
+ uniforms.residual.value.copy(residual);
+ uniforms.cameraNear.value = camera.near;
+ uniforms.cameraFar.value = camera.far;
+ renderer.setRenderTarget(null);
+ renderer.render(this.quadScene, this.quadCamera);
+ }
+}