# Objects

What you see in a scene are three.js objects: meshes, groups, cameras, lights. Framescript does not wrap them. You build an object, put it on the timeline with `add`, and change its properties with [steps](/docs/framework/animation/values.md).

## Plain three.js objects

Anything that is a three.js object can be added, whether you made it with `new Mesh(...)` or with one of Framescript's builders such as [`panel`](/docs/framework/objects/shapes.md) or [`text`](/docs/framework/objects/text.md), which return three.js objects too. `add` gives back the object it was handed, with its real type, so everything three.js can do with it still works.

```ts
import { add, camera, timeline, tween, video, wait } from "framescript"
import { text } from "framescript/text"
import {
  BoxGeometry,
  Color,
  Mesh,
  MeshBasicMaterial,
  PerspectiveCamera,
  type Scene,
} from "three"

export default video(
  { width: 1920, height: 1080, fps: 60 },
  timeline(function* ({ stage }) {
    ;(stage.scene as Scene).background = new Color("#0e0e0e")
    const lens = yield* add(new PerspectiveCamera(32, 16 / 9, 0.1, 100), {
      position: [0, 0, 14],
    })
    yield* camera(lens)

    const box = yield* add(
      new Mesh(
        new BoxGeometry(2, 2, 2),
        new MeshBasicMaterial({ color: "#ff6467" })
      ),
      { position: [0, 0.5, 0], rotation: [0.4, 0.6, 0] }
    )
    yield* add(
      text("a box", { fontSize: 0.4, color: "#ededed", anchorX: "center" }),
      { position: [0, -1.6, 0] }
    )

    yield* tween(box.rotation, { y: 2.2 }, 2)
    yield* wait(0.5)
  })
)
```

`stage.scene` is the root three.js scene. Set its background, fog and environment on it directly. A scene needs a camera: add one and make it the active one with [`camera(cam)`](/docs/framework/scene/camera.md).

### three.js addons

A scene imports any of three.js's addons, the modules under `examples/jsm` of the three.js Framescript runs, by either name: `three/addons/geometries/TextGeometry.js` and `three/examples/jsm/geometries/TextGeometry.js` are the same module. `three/tsl` and `three/webgpu` import too, though a scene is drawn with WebGL. Two addons, `loaders/TTFLoader.js` and `libs/demuxer_mp4.js`, load code from the network, which a scene cannot: they import, and fail when used.

```ts
import { add } from "framescript"
import { Mesh, MeshBasicMaterial } from "three"
import { RoundedBoxGeometry } from "three/addons/geometries/RoundedBoxGeometry.js"

yield* add(
  new Mesh(
    new RoundedBoxGeometry(2, 2, 2, 4, 0.3),
    new MeshBasicMaterial({ color: "#ff6467" })
  )
)
```

A loader addon reads its file from an address that does not say which video it belongs to, so a scene loads files through [`load`](/docs/framework/media/assets.md) instead. `DRACOLoader` and `KTX2Loader` find their decoders without a path, and [compressed models and textures](/docs/framework/media/assets.md#compressed-models-and-textures) load through `load.gltf` and `load.texture`.

## `add`

[`add(object, props?, options?)`](/docs/framework/reference/framescript.md#add) puts an object into the world from this moment until the enclosing [clip](/docs/framework/animation/timeline.md#clips) ends. On the main timeline that is the end of the scene.

**`props`** are initial values, written like the props of a [tween](/docs/framework/animation/values.md#what-can-be-animated): `{ position: [0, 1, 0], scale: [2, 2, 2], opacity: 0 }`. Values set by your own code before `add` work as well. The difference is that the studio can edit the props of an `add`, because they are literals at a place it knows.

**`options.parent`** attaches the object under another added object instead of the scene:

```ts
import { add, tween } from "framescript"
import { panel } from "framescript/shapes"
import { Group } from "three"

const group = yield* add(new Group(), { position: [-2, 0, 0] })
const card = yield* add(
  panel({ width: 2, height: 1, color: "#ff6467" }),
  { position: [0, 1, 0] },
  { parent: group }
)

// Moves the card with it.
yield* tween(group.position, { x: 2 }, 1)
yield* tween(card.rotation, { z: 0.3 }, 0.5)
```

**`options.flatten`** draws the object as one picture, see [flattened groups](#flattened-groups-and-blur).

`add` is a step like any other: it does nothing until it is run with `yield*`. To add several objects from a list, pass the steps to `all`, which gives back the objects:

```ts
import { add, all } from "framescript"
import { panel } from "framescript/shapes"

const cards = yield* all(
  ...[-2, 0, 2].map((x) =>
    add(panel({ width: 1.5, height: 1, color: "#ff6467" }), {
      position: [x, 0, 0],
    })
  )
)
```

## Where state may be changed

Before an object is passed to `add`, any three.js code may set it up: build a group, set positions, attach children with three's own `group.add(child)`.

After `add`, change what is seen through steps: `set`, `tween` and the others. A step is recorded with its time, so every frame knows the value. A direct assignment such as `box.position.x = 3` is not recorded: it has no moment on the timeline, so after a tween of the property it is lost on every frame, and without one it shows from the first frame on. Check reports it as `mutation-outside-step`, an error, at the step after it: between two steps, the run compares where each added object stands, how it is turned and scaled, whether it is visible and how opaque it is with what the steps left. Setting an object up before its `add`, and what a `custom` step writes, is no mutation; a `custom` step that moves, shows or fades an added object names it in `writes` (see the [rules of the check](/docs/framework/agents/rules.md)).

Things that are not properties of an object, such as its name, are yours to set at any time. Naming objects helps: the studio shows the name.

## `remove`, `show` and `hide`

| Step                                                          | Does                                                       |
| ------------------------------------------------------------- | ---------------------------------------------------------- |
| [`remove(object)`](/docs/framework/reference/framescript.md#remove)        | Ends the object before its clip does. It is gone for good. |
| [`hide(object, exit?)`](/docs/framework/reference/framescript.md#hide)     | Stops drawing the object. It stays where and as it was.    |
| [`show(object, entrance?)`](/docs/framework/reference/framescript.md#show) | Draws it again.                                            |

`hide` and `show` take a step as their second argument. `hide` plays it first and hides the object when it has ended; `show` shows the object and then plays it. Each lasts as long as that step, or no time without one.

```ts
import { add, hide, remove, show, tween, wait } from "framescript"
import { panel } from "framescript/shapes"

const card = yield* add(panel({ width: 3, height: 2, color: "#ff6467" }))
const note = yield* add(panel({ width: 1, height: 1, color: "#ededed" }), {
  position: [3, 0, 0],
})

yield* hide(card, tween(card, { opacity: 0 }, 0.4))
yield* remove(note)
yield* wait(1)
yield* show(card, tween(card, { opacity: 1 }, 0.4))
```

Whether an object is visible is a value on the timeline like any other. `hide(card)` is the same as `set(card, { visible: false })`. A hidden object cannot be picked in the studio's viewport. Given `undefined` or `null` in place of the object, `show` and `hide` play only the step.

## `opacity`

three.js has opacity on materials only. Framescript gives every object in the scene an `opacity` property that you can set in the props of `add` and animate:

```ts
import { add, tween } from "framescript"
import { panel } from "framescript/shapes"
import { Group } from "three"

const group = new Group()
group.add(
  panel({ width: 3, height: 2, color: "#262626" }),
  panel({ width: 1, height: 1, color: "#ff6467" })
)
yield* add(group, { opacity: 0 })

yield* tween(group, { opacity: 1 }, 0.6)
```

It fades the object and everything under it: the opacity of each material is multiplied by the `opacity` of its object and of every object above it. It is a property for steps and for the props of `add`; `group.opacity` is not something to read or assign.

## Flattened groups and blur

Because `opacity` goes into every material, each part of a group fades by itself. Where two parts overlap, the one behind shows through the one in front while the group is faint.

Added with `{ flatten: true }`, an object and everything under it is drawn into a picture of its own, and that picture is laid into the frame as one image. Its `opacity` then fades the picture, so the parts keep hiding each other:

```ts
import { add, blur, tween } from "framescript"
import { panel } from "framescript/shapes"
import { Group } from "three"

const group = new Group()
const front = panel({ width: 1.5, height: 1.5, color: "#ff6467" })
front.position.set(0.5, 0.3, 0.1)
group.add(panel({ width: 3, height: 2, color: "#262626" }), front)

const card = yield* add(group, { position: [0, 0, 0] }, { flatten: true })
yield* tween(card, { opacity: 0.4 }, 0.8)

const soft = yield* add(blur(card, { radius: 0 }))
yield* tween(soft, { radius: 0.03 }, 0.8)
```

[`blur(object, { radius })`](/docs/framework/reference/effects.md#blur) softens the picture of one flattened object and leaves the rest of the frame sharp. It is an effect: you put it on the timeline with `add`, and its `radius`, a share of the frame's height, is a number you tween. A blur of an object that was not added with `flatten` is reported as a warning. See [effects](/docs/framework/scene/effects.md).

What to know about a flattened object:

- The picture stands at the depth of the object's origin. Something solid that is nearer the camera than that covers it, and it is drawn over everything that is further away.
- `flatten` is for an object in the scene's space. On a [layer on the screen](/docs/framework/scene/screen.md) or an effect it is a warning; flatten a group under the layer instead.
- Each flattened object costs one more drawing of that object per frame: one pass more. A `blur` of it costs three more.

## Draw order

Solid things hide each other by depth. See-through things are drawn from far to near, which is right until two of them lie in one plane. Then you say which is in front:

- [`inFront(object, of?)`](/docs/framework/reference/framescript.md#infront) draws an object, and everything under it, in front of another.
- [`behind(object, of?)`](/docs/framework/reference/framescript.md#behind) draws it behind another.

Without the second argument, the object is drawn in front of, or behind, everything that was not told otherwise. Both are plain functions, not steps, and give back the object. Call them when you build the objects, before `add`:

```ts
import { add, inFront } from "framescript"
import { panel } from "framescript/shapes"
import { MeshBasicMaterial } from "three"

const square = (color: string) =>
  panel({
    width: 1.5,
    height: 1.5,
    material: new MeshBasicMaterial({ color, transparent: true }),
  })

// Both lie at z = 0.
const grey = square("#5e5e5e")
const red = inFront(square("#ff6467"), grey)
yield* add(grey, { position: [-0.3, 0.2, 0] })
yield* add(red, { position: [0.3, -0.2, 0] })
```

You do not write a render order by number. What belongs to the frame and not to the world, such as a title or a logo in a corner, goes on a [layer on the screen](/docs/framework/scene/screen.md), which needs no order against the scene.

## Finding parts

A component is a plain function that returns an object. To animate a part of it, return the parts from the function, or give the part a name and find it with three's own `getObjectByName`:

```ts
import { add, need, tween } from "framescript"
import { panel } from "framescript/shapes"
import { Group } from "three"

const badge = () => {
  const group = new Group()
  const dot = panel({
    width: 0.4,
    height: 0.4,
    radius: 0.2,
    color: "#ff6467",
  })
  dot.name = "dot"
  dot.position.set(1, 0.5, 0.1)
  group.add(panel({ width: 3, height: 2, color: "#262626" }), dot)
  return group
}

const card = yield* add(badge())
const dot = need(card.getObjectByName("dot"), "the dot of the badge")
yield* tween(dot.scale, { x: 2, y: 2 }, 0.5)
```

[`need(value, what?)`](/docs/framework/reference/framescript.md#need) is for a value your code knows is there, such as the result of a lookup or an entry of a list. It gives the value back with the "may be missing" taken off its type, and throws `need() was given nothing where the dot of the badge was expected.` when the code was wrong. For an object that may really be missing, see [steps that may be missing](/docs/framework/animation/time-remap.md#steps-that-may-be-missing).

## See also

- [Values](/docs/framework/animation/values.md): changing the properties of objects
- [The timeline](/docs/framework/animation/timeline.md): clips and the lifetime of what they add
- [Shapes](/docs/framework/objects/shapes.md), [text](/docs/framework/objects/text.md), [images](/docs/framework/objects/images.md): objects Framescript builds for you
- [Camera](/docs/framework/scene/camera.md), [light](/docs/framework/scene/light.md), [effects](/docs/framework/scene/effects.md), [a layer on the screen](/docs/framework/scene/screen.md)
- [Reference: the main entry](/docs/framework/reference/framescript.md)
- Examples: basics/show-hide-order, layers/flatten, layers/blur
