# Procedural 3D textures

> Checkerboard, brick and rock painted in code for models, and a noise picture a shader reads as a dissolve map.

Example `painted-textures-3d`, running at https://nacatamalon.com/en/examples/painted-textures-3d. The whole catalogue: https://nacatamalon.com/llms/examples.md.

Textures for models, painted in code and handed over like any loaded image: a checkerboard floor (`fillChecker`), bricks made from noise and `mapPixels`, and rock from `fillNoise` in four octaves. A different seed is a different rock, with no file to ship.

The torus is the one for **shaders**. Its material keeps a blue gradient as its picture and carries painted noise as one of its `maps`, which its effect, built with the shader composer, reads with `composerMapSample('noise', uv)` to dissolve it: where the noise is below a rising threshold the surface is cut away, with a burning edge just ahead of the cut. `fillNoise` tiles without a seam, so the same picture can also be repeated across a surface.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';

import * as Settings from './settings';
import { Game } from './scenes/demo';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#0b0d18',
});

// Declare Scenes
game({
    Game,
});
```

`art/textures.ts`:

```ts
import { getColor } from 'nacatamalon';
import { createPixels, fillChecker, fillGradient, fillNoise, mapPixels } from 'nacatamalon/pixels';
import type { TPixels } from 'nacatamalon/pixels';

// The pictures the models wear, painted here. A different seed is a different rock.

const c = (hex: string) => getColor(hex);

// The first texture every 3D scene is tried with.
export const paintChecker = (): TPixels => fillChecker(createPixels(128, 128), 16, c('#d8dce8'), c('#3a3f58'));

// Noise for the grain of the clay, then mapPixels draws the rows of bricks and the mortar over it.
export const paintBricks = (): TPixels => mapPixels(
    fillNoise(createPixels(64, 64), {
        seed: 4,
        scale: 8,
        octaves: 3,
        from: c('#7a2e1e'),
        to: c('#c4583a')
    }),
    (color, x, y) => (y % 8 === 7 || (x + (Math.floor(y / 8) % 2) * 8) % 16 === 15 ? c('#c9c1b0') : color),
);

// Four octaves of noise between two greys.
export const paintRock = (): TPixels =>
    fillNoise(createPixels(64, 64), {
        seed: 7,
        scale: 16,
        octaves: 4,
        from: c('#3b3540'),
        to: c('#a89cb0')
    });

// The torus's own picture, and the noise its shader reads to dissolve it.
export const paintBlue = (): TPixels => fillGradient(createPixels(4, 64), {
    from: c('#2a4ad0'),
    to: c('#9ad8ff')
});
export const paintDissolveMap = (): TPixels => fillNoise(createPixels(64, 64), {
    seed: 11,
    scale: 8,
    octaves: 3
});
```

`components/exhibit.ts`:

```ts
import { createMaterial, createMesh, useCubeGeometry, usePlaneGeometry, useTorusGeometry, useUpdate, useSphereGeometry } from 'nacatamalon';
import { createPixelTexture } from 'nacatamalon/pixels';

import { DISSOLVE } from '../shaders/materials';
import { paintBlue, paintBricks, paintChecker, paintDissolveMap, paintRock } from '../art/textures';

export const Exhibit = () => {
    createMesh({
        geometry: usePlaneGeometry({
            width: 10,
            depth: 10
        }),
        texture: createPixelTexture(paintChecker()),
        transform: {
            y: -0.8
        },
    });

    const spinning = [
        createMesh({
            geometry: useCubeGeometry({
                width: 1.2,
                height: 1.2,
                depth: 1.2
            }),
            texture: createPixelTexture(paintBricks()),
            transform: {
                x: -2.4
            },
        }),
        createMesh({
            geometry: useSphereGeometry({
                radius: 0.75
            }),
            texture: createPixelTexture(paintRock()),
        }),
        createMesh({
            geometry: useTorusGeometry({
                radius: 0.6,
                tube: 0.25
            }),
            material: createMaterial({
                ...DISSOLVE,
                texture: createPixelTexture(paintBlue()),
                maps: {
                    noise: createPixelTexture(paintDissolveMap())
                },
            }),
            transform: {
                x: 2.4
            },
        }),
    ];

    useUpdate((delta) => {
        for (const mesh of spinning) {
            mesh.transform.rotationY += delta * 0.6;
            mesh.transform.rotationX += delta * 0.2;
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { cameraLookAt, createScene, createText, useCamera3d, useDirectionalLight, useSpawn } from 'nacatamalon';

import { Exhibit } from '../components/exhibit';

export const Game = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 50,
        transform: {
            y: 2.2,
            z: 6.5
        }
    });

    cameraLookAt(camera, {
        x: 0,
        y: 0.3,
        z: 0
    });
    useDirectionalLight({
        color: '#fff0d8',
        intensity: 1.1,
        ambient: 0.3,
        transform: {
            rotationX: -0.7,
            rotationY: 0.5
        }
    });

    useSpawn(Exhibit)();
    createText({
        text: 'BRICK      ROCK      DISSOLVE',
        tint: '#8fb7ff',
        transform: {
            x: 74,
            y: 290
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
```

`shaders/materials.ts`:

```ts
import { compileShader, composerFract, composerLight, composerMapSample, composerMix, composerMul, composerSmoothstep, composerStep, composerSub, composerSurface, composerSwizzle, composerTime, composerUv, composerVec3, composerVec4, composerX } from 'nacatamalon/shader';

// The torus's colour effect, written with the shader composer: built once from typed nodes, and
// compileShader turns it into both shader languages.
//
// The noise map, from 0 to 1, is compared against a threshold that sweeps from below it to above it:
// below, the surface is cut away (alpha 0); just above, it burns orange; past that it keeps the
// picture's colour.

const threshold = composerSub(composerMul(composerFract(composerMul(composerTime(), 0.25)), 1.2), 0.1);
const left = composerSub(composerX(composerMapSample('noise', composerUv())), threshold);
const kept = composerStep(0, left);
const lit = composerMul(composerSwizzle(composerSurface(), 'rgb'), composerLight());
const burnt = composerMix(composerVec3(1, 0.55, 0.1), lit, composerSmoothstep(0, 0.08, left));

export const DISSOLVE = compileShader({
    shader: 'mesh3d',
    color: composerVec4(composerMul(burnt, kept), kept),
});
```
