# Flat vs smooth shading

> How a model’s surface faces decides whether it reads as carved or moulded: flat on top, smooth below.

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

How a model's surface is made to face decides whether it reads as carved or as moulded. **The top row is `flat`**: every triangle with its own hard edge, the look of the era. **The bottom row is `smooth`**: worked out evenly across the surface.

The same three files, loaded twice each; nothing differs but the option. Left alone, the engine keeps what the artist baked into the file.

## 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: '#0e1118'
});

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

`components/modelrows.ts`:

```ts
import { createModel, getColor, useLoadModel, useUpdate } from 'nacatamalon';
import * as Settings from '../settings';
import type { TModel } from 'nacatamalon';

export const ModelRows = () => {
    const placed: TModel[] = [];

    for (const [src, scale, x, colour] of Settings.MODELS) {
        // Two of each, under names of their own: asking for the same file twice gives back the one
        // already read, so a different name is what makes these two different shapes.
        const flat = useLoadModel({
            src,
            key: src + '#flat',
            shading: 'flat',
            textures: false
        });

        const smooth = useLoadModel({
            src,
            key: src + '#smooth',
            shading: 'smooth',
            textures: false
        });

        const tint = getColor(colour);

        const size = {
            scaleX: scale,
            scaleY: scale,
            scaleZ: scale
        };

        placed.push(createModel({
            model: flat,
            tint,
            transform: {
                x,
                y: 2,
                ...size
            }
        }));
        placed.push(createModel({
            model: smooth,
            tint,
            transform: {
                x,
                y: -2,
                ...size
            }
        }));
    }

    useUpdate((delta) => {
        for (const model of placed) {
            model.transform.rotationY += delta * Settings.TURN_SPEED;
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, useSpawn } from 'nacatamalon';
import * as Settings from '../settings';

import { ModelRows } from '../components/modelrows';

export const Level = () => {
    useCamera3d(Settings.CAMERA);
    // A light faces along its own -Z, so turning it is how it is aimed: this one comes from above
    // and a little to the left, which is what shows the shape of a thing.
    useDirectionalLight({
        color: '#fff4e6',
        intensity: 1.15,
        ambient: 0.25,
        transform: {
            rotationX: -0.6,
            rotationY: 0.6
        }
    });

    useSpawn(ModelRows)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 560;
export const HEIGHT = 400;
import type { TCamera3dOptions } from 'nacatamalon';

export const CAMERA = {
    projection: 'perspective',
    fov: 45,
    transform: {
        z: 12
    }
} satisfies TCamera3dOptions;
export const MODELS = [['/example-assets/assets/3d/gltf/Suzanne/Suzanne.gltf', 0.9, -3.4, '#d9a066'], ['/example-assets/assets/3d/gltf/Avocado/Avocado.gltf', 19, 0, '#8fae52'], ['/example-assets/assets/3d/gltf/WaterBottle/WaterBottle.gltf', 9, 3.4, '#6f9bd1'],] satisfies [string, number, number, string][];

export const TURN_SPEED = 0.7;
```
