# 2D inside 3D

> createSpriteTexture draws a whole component into a picture every frame and a model shows it: the screen of a handheld.

Example `sprite-texture`, running at https://nacatamalon.com/en/examples/sprite-texture. The whole catalogue: https://nacatamalon.com/llms/examples.md.

`createSpriteTexture` draws a component into a picture every frame instead of the screen, and a model shows it: the handheld's screen is a creature and a bar living in their own 128×128 pixels.

With `sees: '3d'` the picture also shows the 3D world around it from a camera of its own: the monitor on the wall is a security camera watching the handheld.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { Level } from './scenes/demo';

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

// Declare Scenes
game({
    Level
});
```

`components/bar.ts`:

```ts
import { createSprite, useUpdate } from 'nacatamalon';

export const Bar = () => {
    const bar = createSprite({
        width: 104,
        height: 10,
        tint: '#306230',
        anchor: {
            x: 0,
            y: 0.5
        },
        transform: {
            x: 12,
            y: 112
        }
    });

    useUpdate((_delta, t) => {
        bar.width = 30 + 74 * (0.5 + 0.5 * Math.sin(t * 0.7));
    });
};
```

`components/screen.ts`:

```ts
import { createSprite, useSpawn, useUpdate } from 'nacatamalon';

import { Bar } from './bar';

export const Screen = () => {
    const body = createSprite({
        width: 40,
        height: 40,
        tint: '#0f380f',
        transform: {
            x: 64,
            y: 60
        }
    });

    const eyeL = createSprite({
        width: 9,
        height: 12,
        tint: '#9bbc0f',
        transform: {
            x: 55,
            y: 56
        },
        zIndex: 1
    });

    const eyeR = createSprite({
        width: 9,
        height: 12,
        tint: '#9bbc0f',
        transform: {
            x: 73,
            y: 56
        },
        zIndex: 1
    });

    useSpawn(Bar)();

    useUpdate((_delta, t) => {
        const hop = Math.abs(Math.sin(t * 2.2)) * 12;
        const sway = Math.sin(t) * 16;

        body.transform.x = 64 + sway;
        eyeL.transform.x = 55 + sway;
        eyeR.transform.x = 73 + sway;
        body.transform.y = 60 - hop;
        eyeL.transform.y = 56 - hop;
        eyeR.transform.y = 56 - hop;
    });
};
```

`scenes/demo.ts`:

```ts
import { createMesh, createScene, createSpriteTexture, useCamera3d, useCubeGeometry, useDirectionalLight, usePlaneGeometry, useUpdate } from 'nacatamalon';

import { Screen } from '../components/screen';
import { SecurityCamera } from '../views/securitycamera';

export const Level = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        near: 0.1,
        far: 100,
        transform: {
            x: 0.8,
            y: 0.6,
            z: 4.2,
            rotationY: 0.18,
            rotationX: -0.1
        }
    });
    useDirectionalLight({
        intensity: 1.1,
        ambient: 0.5
    });

    const cube = useCubeGeometry();

    createMesh({
        geometry: usePlaneGeometry({
            width: 8,
            depth: 8
        }),
        tint: '#2e3440',
        transform: {
            y: -0.8
        }
    });

    const screen = createSpriteTexture({
        width: 128,
        height: 128,
        background: '#9bbc0f'
    }, Screen);

    const handheld = createMesh({
        geometry: cube,
        texture: screen
    });

    const feed = createSpriteTexture({
        width: 160,
        height: 120,
        background: '#10131a',
        sees: '3d'
    }, SecurityCamera);

    createMesh({
        geometry: cube,
        texture: feed,
        transform: {
            x: -1.9,
            y: 0.5,
            z: -1.2,
            rotationY: 0.5,
            scaleX: 1.6,
            scaleY: 1.2,
            scaleZ: 0.08
        }
    });

    useUpdate((delta, t) => {
        handheld.transform.rotationY += delta * 0.6;
        handheld.transform.rotationX = Math.sin(t * 0.4) * 0.3;
    });

    return createScene();
};
```

`settings.ts`:

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

`views/securitycamera.ts`:

```ts
import { createSprite, useCamera3d, useUpdate } from 'nacatamalon';

export const SecurityCamera = () => {
    // The picture's own camera: up in a corner, looking down at the handheld.
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        near: 0.1,
        far: 50,
        transform: {
            x: 2.6,
            y: 2.4,
            z: 2.6,
            rotationY: 0.785,
            rotationX: -0.55
        }
    });

    const rec = createSprite({
        width: 8,
        height: 8,
        tint: '#e03030',
        transform: {
            x: 10,
            y: 10
        }
    });

    useUpdate((_delta, t) => {
        rec.visible = t % 1 < 0.6;
    });
};
```
