# Particles in space

> A geyser and dust from their own files, one bent by the box it hangs from, and an orb whose aura travels with it while its trail is left behind.

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

A geyser pouring from a cone and dust lifting off the ground, each from its own `.particles` file. The second geyser hangs from a box that is turned and shrunk, and comes out sideways and small because of it.

The orb carries two effects that differ in one line: the trail is left behind, and the aura, with `"worldSpace": false`, travels with it. **Click** to throw sparks off the orb.

## Code

`index.ts`:

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

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

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

`components/ground.ts`:

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

export const Ground = () => {
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#292e38',
        transform: {
            y: -0.15,
            scaleX: 14,
            scaleY: 0.3,
            scaleZ: 14
        }
    });
    createMesh({
        geometry: useCylinderGeometry({
            radius: 1.1,
            height: 0.35,
            segments: 20
        }),
        tint: '#3b4252'
    });

    createParticles3d({
        effect: useLoadParticles({
            src: Settings.FILES.geyser
        }),
        transform: {
            y: 0.2
        }
    });
    createParticles3d({
        effect: useLoadParticles({
            src: Settings.FILES.dust
        }),
        transform: {
            y: 0.3
        }
    });
};
```

`components/nozzle.ts`:

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

export const Nozzle = () => {
    useTransform({
        x: -3,
        y: 0.8,
        rotation: -Math.PI / 2,
        scaleX: 0.5,
        scaleY: 0.5,
        scaleZ: 0.5
    });
    createMesh({
        geometry: useCylinderGeometry({
            radius: 0.5,
            height: 0.6,
            segments: 12
        }),
        tint: '#5e6b82'
    });
    createParticles3d({
        effect: useLoadParticles({
            src: Settings.FILES.geyser
        }),
        transform: {
            y: 0.3
        }
    });
};
```

`components/orb.ts`:

```ts
import { createMesh, createParticles3d, emitParticles, useLoadParticles, usePointer, useTransform, useUpdate, useSphereGeometry } from 'nacatamalon';
import * as Settings from '../settings';

export const Orb = () => {
    const place = useTransform({
        y: 1.4
    });

    createMesh({
        geometry: useSphereGeometry({
            radius: 0.18
        }),
        tint: '#dff6ff'
    });

    createParticles3d({
        effect: useLoadParticles({
            src: Settings.FILES.trail
        })
    });
    createParticles3d({
        effect: useLoadParticles({
            src: Settings.FILES.aura
        })
    });

    const sparks = createParticles3d({
        effect: useLoadParticles({
            src: Settings.FILES.burst
        }),
        autoplay: false
    });

    usePointer().onDown(() => emitParticles(sparks, Settings.BURST));

    useUpdate((_delta, time) => {
        const t = time * Settings.ORBIT.speed;

        place.x = Math.cos(t) * Settings.ORBIT.radius;
        place.z = Math.sin(t) * Settings.ORBIT.radius;
        place.y = Settings.ORBIT.height + Math.sin(t * 2) * Settings.ORBIT.bob;
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, useSpawn } from 'nacatamalon';
import { Ground } from '../components/ground';
import { Nozzle } from '../components/nozzle';
import { Orb } from '../components/orb';

export const Space = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        near: 0.1,
        far: 100,
        transform: {
            y: 3.2,
            z: 8,
            rotationX: -0.245
        }
    });
    useDirectionalLight({
        intensity: 0.9,
        ambient: 0.3
    });

    useSpawn(Ground)();
    useSpawn(Nozzle)();
    useSpawn(Orb)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const ORBIT = {
    speed: 0.8,
    radius: 2.4,
    height: 1.4,
    bob: 0.3
};
export const BURST = 60;
export const FILES = {
    geyser: '/example-assets/effects/geyser.particles',
    dust: '/example-assets/effects/dust.particles',
    trail: '/example-assets/effects/trail3d.particles',
    aura: '/example-assets/effects/aura3d.particles',
    burst: '/example-assets/effects/burst3d.particles'
};
```
