# A model and its parts

> This truck is five parts: a wheel on each side and a body that is paint, glass and trim. One placement moves them all.

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

A model file is one piece surprisingly rarely. This truck is **five**: a wheel on each side, and a body that is itself paint, glass and window trim. All of them are drawn, and **one placement moves the lot**.

`model.parts` fills in when the file arrives, and its parts are ordinary models, each with a material of its own. The name of each surface is on the loaded model (`truck.parts`), in the same order, and it is often the only thing telling two apart: here it makes the glass see-through without touching the paint.

To hide the whole truck there is no need to walk `parts`: `model.visible = false` hides every part at once, including the ones still on the way. Click or press **Space**.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT } from './settings';
import { Level } from './scenes/demo';

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

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

`components/truck.ts`:

```ts
import { createModel, createText, useKeyboard, useLoadModel, usePointer, useUpdate, useWatch } from 'nacatamalon';
import * as Settings from '../settings';

export const Truck = () => {
    const truck = useLoadModel({
        src: Settings.MODEL
    });

    const model = createModel({
        model: truck,
        transform: {
            y: -1
        }
    });

    // visible hides or shows the whole truck at once, every part, even before the file has arrived.
    createText({
        text: 'CLICK OR SPACE: HIDE / SHOW',
        tint: '#9fb0ff',
        transform: {
            x: 8,
            y: 8
        }
    });

    const keys = useKeyboard();

    const toggle = () => {
        model.visible = !model.visible;
    };

    usePointer().onDown(toggle);

    useUpdate((delta) => {
        if (keys.justPressed(' ')) {
            toggle();
        }

        model.transform.rotationY += delta * Settings.TURN_SPEED;
    });
    useWatch(() => {
        if (truck.status !== 'ready') {
            return;
        }

        // Once the file is here its parts are ordinary models, so this makes the windows see-through
        // without touching the paint beside them, which is the same piece of the file.
        // The file names each piece's material, and the model's parts come in the file's order.
        const index = truck.parts.findIndex((part) => part.material === 'glass');
        const glass = index >= 0 ? model.parts[index] : undefined;

        if (glass !== undefined) {
            glass.material.alpha = 0.35;
        }
    }, [truck]);
};
```

`scenes/demo.ts`:

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

import { Truck } from '../components/truck';

export const Level = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 45,
        transform: {
            y: 1.2,
            z: 7,
            rotationX: -0.12
        }
    });
    useDirectionalLight({
        intensity: 1.1,
        ambient: 0.35,
        transform: {
            rotationX: -0.5,
            rotationY: 0.7
        }
    });

    useSpawn(Truck)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 520;
export const HEIGHT = 340;
export const MODEL = '/example-assets/assets/3d/gltf/CesiumMilkTruck/CesiumMilkTruck.gltf';
export const TURN_SPEED = 0.6;
```
