# Render styles

> A showroom in the CGI style with useStudio, switched to the PlayStation style while it runs: one call changes how every material is drawn, the effects over it and the resolution.

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

A showroom of chrome and plastic in the computer-graphics style of the era, lit by `useStudio` and reflecting a photo made round with `sphereFromPanorama`. A render style is how a whole scene is drawn: `useRenderStyle(RENDER.STYLES.cgi)` lights every material at every pixel and adds a soft bloom, at its own 640 × 480, smooth.

**Space** switches to `RENDER.STYLES.ps1` while it runs: pictures that swim, corners that shiver, colours dithered down, and 320 × 240 with no smoothing. One call changes how every material is drawn, the effects over the frame and the resolution; what the game itself sets afterwards still wins.

## Code

`index.ts`:

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

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    keep: 'height',
    fullscreenScaling: 'contain',
    background: '#141820',
    // Pictures read smoothly and edges softened, as a renderer drew them.
    smooth: true,
    msaa: 4,
    actions: ACTIONS
});

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

`components/exhibits.ts`:

```ts
import { chrome, createMaterial, createMesh, plastic, useCubeGeometry, usePlaneGeometry, useSphereGeometry, useTorusGeometry, useUpdate } from 'nacatamalon';
import type { TTexture } from 'nacatamalon';

// Three pieces on a floor. The floor and the red ball say nothing about how they are drawn, so they
// take the scene's style: at every pixel in CGI, swimming and shivering in PS1. The cube and the ring
// are recipes (plastic, chrome), which say perPixel themselves and keep it in either style.
export const Exhibits = (studio: TTexture, tiles: TTexture) => {
    createMesh({
        geometry: usePlaneGeometry({
            width: 10,
            depth: 10,
            // Cut into pieces, as the games of the era did, so the PlayStation's swimming stays small.
            widthSegments: 8,
            depthSegments: 8
        }),
        texture: tiles,
        tint: '#d8d0c0',
        specular: '#ffffff',
        shininess: 16
    });

    const ball = createMesh({
        geometry: useSphereGeometry({
            radius: 0.7,
            segments: 12,
            rings: 8
        }),
        tint: '#d8322a',
        specular: '#ffffff',
        shininess: 60,
        transform: {
            x: -1.8,
            y: 0.7
        }
    });

    const cube = createMesh({
        geometry: useCubeGeometry({
            width: 1,
            height: 1,
            depth: 1
        }),
        material: createMaterial(plastic({
            tint: '#2a7bd8',
            environment: studio
        })),
        transform: {
            y: 0.5
        }
    });

    const ring = createMesh({
        geometry: useTorusGeometry(),
        material: createMaterial(chrome({
            environment: studio
        })),
        transform: {
            x: 1.8,
            y: 0.8
        }
    });

    useUpdate((delta) => {
        ball.transform.rotationY += delta * 0.4;
        cube.transform.rotationY += delta * 0.5;
        ring.transform.rotationY += delta * 0.6;
        ring.transform.rotationX += delta * 0.3;
    });
};
```

`components/hud.ts`:

```ts
import { createText, RENDER, useAction, useGame, useLoadBitmapFont, useScreenSpace, useUpdate } from 'nacatamalon';
import type { TRenderStyleHandle } from 'nacatamalon';
import { STYLE_NAMES } from '../settings';

const STYLES = [RENDER.STYLES.cgi, RENDER.STYLES.ps1];

export const Hud = (style: TRenderStyleHandle) => {
    useScreenSpace();

    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
    });

    const label = createText({
        text: `STYLE ${STYLE_NAMES[0]}`,
        font,
        tint: '#ede6f5',
        transform: {
            x: 8,
            y: 8
        }
    });

    const hint = createText({
        text: 'SPACE CHANGE STYLE',
        font,
        tint: '#8c82a6',
        transform: {
            x: 8
        }
    });

    // The PS1 style brings its own 320 x 240: the hint is placed again at the bottom when it does.
    const game = useGame();
    const place = () => {
        hint.transform.y = game.getHeight() - 16;
    };
    place();
    game.onResized(place);

    const toggle = useAction('toggle');
    let current = 0;

    // The whole scene changes with one call: its materials on the next frame, its effects, its
    // resolution and its filtering with them.
    useUpdate(() => {
        if (toggle.justPressed()) {
            current = (current + 1) % STYLES.length;
            style.set(STYLES[current]!);
            label.text = `STYLE ${STYLE_NAMES[current]}`;
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { cameraLookAt, createScene, RENDER, useCamera3d, useLoadTexture, useRenderStyle, useSpawn, useStudio, useUpdate } from 'nacatamalon';
import { createPixelTexture, sphereFromPanorama, useLoadPixels } from 'nacatamalon/pixels';
import { Exhibits } from '../components/exhibits';
import { Hud } from '../components/hud';

export const Showroom = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 50,
        transform: {
            y: 2.4,
            z: 6
        }
    });

    // The scene's style: every material that does not say is lit at every pixel, with a soft bloom.
    const style = useRenderStyle(RENDER.STYLES.cgi);

    // What the chrome and the plastic reflect: a photo of a canal, made round. A photo covers about
    // 120 degrees; the rest of the way round is it mirrored.
    const canal = useLoadPixels({
        src: '/example-assets/assets/3d/textures/canal.png'
    });
    const surroundings = createPixelTexture(canal, (pixels) => sphereFromPanorama(pixels, {
        across: 120
    }));

    // A photographer's studio in one line: its lamps and its two-coloured ambient, reflecting the photo.
    const studio = useStudio({
        top: '#cfe0f4',
        ground: '#4a5a2a',
        environment: surroundings
    });

    const tiles = useLoadTexture({
        src: '/example-assets/assets/3d/textures/tiles.png'
    });

    useSpawn(Exhibits)(studio.environment, tiles);
    useSpawn(Hud)(style);

    // Slowly round, so the reflections and the shines move.
    useUpdate((_delta, time) => {
        const angle = Math.sin(time * 0.25) * 0.8;
        camera.transform.x = Math.sin(angle) * 6;
        camera.transform.z = Math.cos(angle) * 6;
        cameraLookAt(camera, { x: 0, y: 0.6, z: 0 });
    });

    return createScene();
};
```

`settings.ts`:

```ts
import type { TActionMap } from 'nacatamalon';

// A render's size rather than a console's: the computer graphics of the era were drawn at 640 x 480
// and up, smooth, where the games ran at 320 x 240.
export const WIDTH = 640;
export const HEIGHT = 480;

// The two styles the showroom switches between, in order.
export const STYLE_NAMES = ['CGI', 'PS1'] as const;

export const ACTIONS: TActionMap = [
    {
        name: 'toggle',
        bindings: [
            {
                type: 'key',
                key: ' '
            },
            {
                type: 'button',
                button: 'a'
            }
        ]
    }
];
```
