# Particles 3D

> The same particle pool, drawn as squares that face the camera, in world units.

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

The same pool and the same `.particles` format, drawn as squares that always face the camera, in world units. The square is spread in the vertex shader along the camera's right and up, so an orbit changes two directions rather than every particle.

They are hidden by the log in front of them but never by each other: they test depth and never write it.

## Code

`index.ts`:

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

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

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

`components/fire.ts`:

```ts
import { createMesh, createParticles3d, useCylinderGeometry, useLoadParticles } from 'nacatamalon';
import * as Settings from '../settings';

export const Fire = () => {
    createMesh({
        geometry: useCylinderGeometry({
            radius: 0.45,
            height: 0.2,
            segments: 16
        }),
        tint: '#3a2a20',
        transform: {
            y: -0.1
        },
    });

    // The effect is the file: the rate, the cone it pours from and the colour it cools through.
    const fire = useLoadParticles({
        src: Settings.EFFECT
    });

    createParticles3d({
        effect: fire,
        transform: {
            y: 0.1
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useSpawn, useUpdate } from 'nacatamalon';
import { Fire } from '../components/fire';
import { ORBIT } from '../settings';

export const Campfire = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 50,
        near: 0.1,
        far: 100
    });

    useSpawn(Fire)();
    useUpdate((_delta, time) => {
        const angle = time * ORBIT.speed;

        camera.transform.x = Math.sin(angle) * ORBIT.radius;
        camera.transform.z = Math.cos(angle) * ORBIT.radius;
        camera.transform.y = ORBIT.height;
        camera.transform.rotationY = angle;
        camera.transform.rotationX = ORBIT.pitch;
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const EFFECT = '/example-assets/effects/campfire3d.particles';
export const ORBIT = {
    speed: 0.4,
    radius: 4,
    height: 1.6,
    pitch: -0.32
};
```
