# Surface

`com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface`

**Since:** 1.6.5

A [Draw2D](../Draw2D.md) panel rendered in the world. Its element coordinates are surface
pixels measured from the top left, with y increasing downwards; surface size is in
blocks, and [getWidth()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getWidth-) and [getHeight()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getHeight-) report pixel dimensions.  

This is a 2D overlay living in three dimensions. It is a [Draw2D](../Draw2D.md) in its own
right, so everything a 2D overlay can hold, `addText`, `addRect`,  
`addLine`, `addImage`, `addItem` and the rest, works on it
unchanged, and the same builders do too. The difference is where the result appears:  
a surface is placed at a block position in the world, rotated to face a direction,  
and drawn as a flat surface there rather than flat on the screen. The coordinates a
script gives its elements are the surface's own pixels, measured from its top left
with y growing downwards, and how big a pixel is in blocks is decided by the size
and the subdivision count rather than by the element.  

A surface is reached through `Draw3D`, either with `addDraw2D` for the
quick form or through [Draw3D.surfaceBuilder()](../Draw3D.md#com.jsmacrosce.jsmacros.client.api.classes.render.Draw3D_surfaceBuilder-) for the full one. A surface on
its own draws nothing; it is the 3D overlay holding it that has to be registered.  

Three settings decide whether it can be seen. `cull` makes it depth tested, so
terrain draws over it; leaving it off draws the surface over the world instead.  
`renderBack` decides whether the back is drawn at all, and with it off the
surface disappears when the camera is behind it. And `rotateToPlayer` turns
the surface to face the camera every frame, which overwrites whatever rotations
were set.  

Lighting is a fourth. By default everything on a surface is drawn at full
brightness, ignoring the world. [setWorldLight()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setWorldLight-) samples the world instead,  
once per frame at the surface's own position, and [setLight(int, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setLight-int-int-) fixes
a level. None of them model a cast shadow.  

The size and the subdivision count work together rather than independently. The
size is in blocks and the subdivision count says how many of those the smaller side
is divided into, and one subdivision is the unit the elements are measured in. So a
surface `1` block on a side with a subdivision count of `200` is
`200` by `200` surface pixels, and the same surface at
`minSubdivisions` of `1` is a single pixel. [getWidth()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getWidth-) and
[getHeight()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getHeight-) report those subdivision counts, not the block size.  

The two ways of making a surface do not start from the same place.  
[Draw3D.surfaceBuilder()](../Draw3D.md#com.jsmacrosce.jsmacros.client.api.classes.render.Draw3D_surfaceBuilder-) leaves the count at `1`, so a builder with
nothing set on it gives a single pixel across its smaller side, while the short
`Draw3D.addDraw2D` forms that take no subdivision count pass `200`.  
```js
const draw = Hud.createDraw3D();  
// a one block panel with room for a label, drawn over the world
const surface = draw.surfaceBuilder()
   .pos(0.5, 64, 0.5)
   .size(1, 1)
   .minSubdivisions(200)
   .cull(false)
   .renderBack(true)
   .buildAndAdd();  
surface.addText("hello", 10, 10, Color.rgb(0xFF, 0xFF, 0xFF), true);  
draw.register();  
```

Available in scripts as `RenderElements.Surface`.

## Nested types

- [Builder](Surface.Builder.md)

## Constructors

### new Surface {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_constructor-Pos3D-Pos3D-Pos2D-int-boolean-boolean-}

```js
new Surface(Pos3D pos, Pos3D rotations, Pos2D sizes, int minSubdivisions, boolean renderBack, boolean cull)
```

Initializes a surface with world position, rotations, block dimensions, and rendering options.  
The position and rotation are in world coordinates; the position is the panel's top-left corner,  
and rotation components are degrees. The three position objects are retained by reference and
must be non-null. Subdivisions are clamped to at least `1`; initialization computes the
pixel scale and clears/runs the inherited initialization behavior, but does not add this surface
to a [Draw3D](../Draw3D.md) overlay.

**Parameters:**
- `pos`: panel's top-left world position, in blocks
- `rotations`: rotations about x, y, and z, in degrees
- `sizes`: physical width and height, in blocks
- `minSubdivisions`: subdivisions across the smaller side, clamped to at least `1`
- `renderBack`: whether to draw the surface when viewed from its back side
- `cull`: whether depth testing against the world is enabled

## Fields

### boundEntity {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_boundEntity}

```js
boundEntity: EntityHelper<?>
```

Entity followed by this surface, or `null` if unbound. While alive, the
surface uses the entity's interpolated position plus [boundOffset](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_boundOffset); if
the entity is no longer alive, rendering uses [pos](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_pos) instead.

**Since:** 1.6.5

### boundOffset {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_boundOffset}

```js
boundOffset: Pos3D
```

Offset in blocks added to the bound entity's interpolated position at render
time. It has no effect when the surface is unbound.

**Since:** 1.6.5

### cull {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_cull}

```js
cull: boolean
```

Whether the surface is depth tested against the world. When false, it is drawn
over the world instead.

**Since:** 1.6.5

### pos {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_pos}

```js
pos: Pos3D
```

World position in blocks used when this surface is not following a live entity.  
It is the panel corner; [rotateCenter](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotateCenter) changes the rotation pivot, not
the position's meaning.

**Since:** 1.6.5

### renderBack {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_renderBack}

```js
renderBack: boolean
```

Whether the surface is drawn when the camera is on its back side. When false,  
rendering is skipped from that side.

**Since:** 1.6.5

### rotateCenter {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotateCenter}

```js
rotateCenter: boolean
```

Whether surface rotations pivot around the center rather than the corner.  
This also selects the pivot for camera-facing rotation. The field defaults to
`false`; the builder defaults to `true`.

**Since:** 1.6.5

### rotateToPlayer {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotateToPlayer}

```js
rotateToPlayer: boolean
```

Whether the surface faces the camera during rendering. When enabled, the
camera-derived angles overwrite [rotations](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotations) each frame; the pivot is
controlled by [rotateCenter](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotateCenter).

**Since:** 1.6.5

### rotations {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotations}

```js
rotations: Pos3D
```

Surface rotations in degrees, applied in y, x, then z order. When
[rotateToPlayer](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotateToPlayer) is enabled, rendering overwrites these values each frame.

**Since:** 1.6.5

### zIndexScale {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_zIndexScale}

```js
zIndexScale: double
```

Scales each element's z-index into a rendering offset in blocks. Defaults to
`0.001` blocks per z-index unit.

**Since:** 1.6.5

## Methods

### bindToEntity {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_bindToEntity-EntityHelper-}

```js
bindToEntity(EntityHelper<?> boundEntity): Surface
```

Binds this surface to an entity; while that entity is alive, its interpolated
position plus [boundOffset](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_boundOffset) determines the render position. Passing
`null` unbinds it.

**Parameters:**
- `boundEntity`: the entity to bind the surface to

**Returns:** self for chaining.

**Since:** 1.8.4

### compareToSame {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_compareToSame-Surface-}

```js
compareToSame(Surface other): int
```

Compares surfaces by position, then rotations, then size. Surfaces matching on
all three compare equal; this compares those values, not spatial distance.

**Parameters:**
- `other`: another surface to order this one against

**Returns:** a negative number, zero, or a positive number as this surface sorts before,  
        equal to, or after `other`

**Since:** 1.6.5

### doesRotateToPlayer {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_doesRotateToPlayer-}

```js
doesRotateToPlayer(): boolean
```

**Returns:** `true` if the surface is configured to face the camera

**Since:** 1.8.4

### equals {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_equals-Object-}

```js
equals(Object o): boolean
```

Tests equality using the exact runtime class and the current position, rotations, and size only.  
These mutable values determine the result; subdivision and rendering settings are ignored.

**Parameters:**
- `o`: object to compare

**Returns:** `true` if `o` is this surface or has the same runtime class and equal position, rotations, and size; `false` for `null` or a different class

### getBoundEntity {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getBoundEntity-}

```js
getBoundEntity(): EntityHelper<?>
```

Returns the entity this surface follows, if any.

**Returns:** the bound entity, or `null` if none is set

**Since:** 1.8.4

### getBoundOffset {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getBoundOffset-}

```js
getBoundOffset(): Pos3D
```

Returns the offset added to the bound entity's interpolated position, in blocks.  
The returned object is the surface's live offset; mutating it changes the offset.

**Returns:** the bound-entity offset

**Since:** 1.8.4

### getHeight {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getHeight-}

```js
getHeight(): int
```

Returns the surface height in pixels. The smaller side has
[getMinSubdivisions()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getMinSubdivisions-) pixels; the other side scales with the surface's
aspect ratio. The result is truncated to an integer.

**Returns:** the height in surface pixels

**Since:** 1.6.5

### getMinSubdivisions {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getMinSubdivisions-}

```js
getMinSubdivisions(): int
```

Returns the subdivision count across this surface's smaller side.  

This is the number of surface pixels across the smaller side, so it is what the
element coordinates are measured in on that axis. It is never below `1`.

**Returns:** the subdivision count across the smaller side

**Since:** 1.6.5

### getSizes {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getSizes-}

```js
getSizes(): Pos2D
```

Returns this surface's physical width and height in blocks.  

A copy rather than the surface's own size object, so writing to what this
returns does not resize the surface. To change the size, use
[setSizes(double, double)](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setSizes-double-double-).  

These are blocks. The number of surface pixels the elements are measured in is
[getWidth()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getWidth-) and [getHeight()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getHeight-), which are subdivisions of this.

**Returns:** the size of this surface, in blocks

**Since:** 1.6.5

### getWidth {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getWidth-}

```js
getWidth(): int
```

Returns the surface width in pixels. The smaller side has
[getMinSubdivisions()](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getMinSubdivisions-) pixels; the other side scales with the surface's
aspect ratio. The result is truncated to an integer.

**Returns:** the width in surface pixels

**Since:** 1.6.5

### getZIndex {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_getZIndex-}

```js
getZIndex(): int
```

Surfaces do not use a z-index for ordering; this method always returns `0`.

**Returns:** always `0`

**Since:** 1.6.5

### hashCode {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_hashCode-}

```js
hashCode(): int
```

Computes a hash from the surface's current position, rotations, and size; changes to those mutable values can change the result.

**Returns:** hash code derived from the current position, rotations, and size

### init {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_init-}

```js
init(): void
```

Recomputes the pixel scale, clears the elements, and runs the init function if set.

**Since:** 1.6.5

### isRotatingCenter {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_isRotatingCenter-}

```js
isRotatingCenter(): boolean
```

**Returns:** whether rotations use the surface center as their pivot

**Since:** 1.8.4

### render {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_render-GuiGraphicsExtractor-int-int-float-}

```js
render(GuiGraphicsExtractor drawContext, int mouseX, int mouseY, float delta): void
```

This override does nothing; it does not render the surface or use its arguments.

**Parameters:**
- `drawContext`: graphics context, unused
- `mouseX`: mouse x-coordinate, unused
- `mouseY`: mouse y-coordinate, unused
- `delta`: frame delta, unused

### setBoundOffset {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setBoundOffset-Pos3D-}

```js
setBoundOffset(Pos3D boundOffset): Surface
```

Sets the entity-following offset by reference, in blocks.

**Parameters:**
- `boundOffset`: offset from the entity's interpolated position

**Returns:** this surface for chaining

**Since:** 1.8.4

### setBoundOffset {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setBoundOffset-double-double-double-}

```js
setBoundOffset(double x, double y, double z): Surface
```

Sets the offset added to the bound entity's interpolated position, in blocks.  
Has no effect while the surface is unbound. Replaces the offset object.

**Parameters:**
- `x`: the x offset in blocks
- `y`: the y offset in blocks
- `z`: the z offset in blocks

**Returns:** this surface

**Since:** 1.8.4

### setFullBrightLight {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setFullBrightLight-}

```js
setFullBrightLight(): Surface
```

Makes all elements on this surface render at full brightness, ignoring world lighting.

**Returns:** self for chaining.

**Since:** 2.1.0

### setLight {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setLight-int-int-}

```js
setLight(int blockLight, int skyLight): Surface
```

Sets fixed block and sky light levels for all elements on this surface.

**Parameters:**
- `blockLight`: block light level, 0-15 (e.g. 15 next to a torch)
- `skyLight`: sky light level, 0-15 (e.g. 15 outdoors in daylight)

**Returns:** self for chaining.

**Since:** 2.1.0

### setMinSubdivisions {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setMinSubdivisions-int-}

```js
setMinSubdivisions(int minSubdivisions): void
```

Sets the number of subdivisions across the smaller side of this surface.  

The count is across the *smaller* side; the larger side gets however many
that works out to. A value of `0` or below is raised to `1`, so a
surface can be made to have a single pixel but never none.  
The pixel scale is recomputed after the count is set.

**Parameters:**
- `minSubdivisions`: how many subdivisions the smaller side is divided into

**Since:** 1.6.5

### setPos {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setPos-Pos3D-}

```js
setPos(Pos3D pos): Surface
```

Updates this surface's position coordinates from `pos`.

**Parameters:**
- `pos`: position in world blocks

**Returns:** this surface for chaining

**Since:** 1.8.4

### setPos {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setPos-BlockPosHelper-}

```js
setPos(BlockPosHelper pos): Surface
```

Updates this surface's position coordinates from `pos`.

**Parameters:**
- `pos`: block position in the world

**Returns:** this surface for chaining

**Since:** 1.8.4

### setPos {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setPos-double-double-double-}

```js
setPos(double x, double y, double z): Surface
```

moves this surface to a point in the world.  

The coordinates are in blocks and are the panel's top left corner, not its
middle, unless [rotateCenter](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotateCenter) is on. The position object is written into
rather than replaced, so a reference to [pos](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_pos) keeps following the surface.  

This is ignored for rendering while the surface is bound to a live entity, since
then the entity's own position is used instead.  
```js
const draw = Hud.createDraw3D();  
const surface = draw.surfaceBuilder().pos(0, 64, 0).buildAndAdd();  
surface.setPos(10, 70, -4);  
draw.register();  
```

**Parameters:**
- `x`: the x coordinate of the surface's corner
- `y`: the y coordinate of the surface's corner
- `z`: the z coordinate of the surface's corner

**Returns:** self for chaining.

**Since:** 1.6.5

### setRotateCenter {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setRotateCenter-boolean-}

```js
setRotateCenter(boolean rotateCenter): Surface
```

**Parameters:**
- `rotateCenter`: whether rotations use the surface center rather than its corner as pivot

**Returns:** self for chaining.

**Since:** 1.8.4

### setRotateToPlayer {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setRotateToPlayer-boolean-}

```js
setRotateToPlayer(boolean rotateToPlayer): Surface
```

Sets whether the surface is rotated to face the camera during rendering.  
When enabled, this overwrites the configured rotations each frame.

**Parameters:**
- `rotateToPlayer`: whether to face the camera

**Returns:** this surface

**Since:** 1.8.4

### setRotations {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setRotations-double-double-double-}

```js
setRotations(double x, double y, double z): void
```

Sets the surface's rotations in degrees.  

The rotations are applied in the order y, then x, then z, so the y rotation is
the one that swings the panel around to face a compass direction and the other
two tilt it. They turn about the panel's corner unless
[rotateCenter](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotateCenter) is on, in which case they turn about its middle.  

Angles are not range-clamped. These rotations are overwritten during rendering
while [rotateToPlayer](#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_rotateToPlayer) is enabled.

**Parameters:**
- `x`: the rotation about the x axis, in degrees
- `y`: the rotation about the y axis, in degrees
- `z`: the rotation about the z axis, in degrees

**Since:** 1.6.5

### setSizes {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setSizes-double-double-}

```js
setSizes(double x, double y): void
```

Sets the surface's physical width and height in blocks.  

This is the physical size of the panel in the world, not how much room its
elements have: the pixels they are measured in are subdivisions of this, so
making the panel bigger with the subdivision count fixed gives each pixel more
physical space.  
The size object is updated in place, and the pixel-to-block scale is recomputed.

**Parameters:**
- `x`: the width of the surface, in blocks
- `y`: the height of the surface, in blocks

**Since:** 1.6.5

### setWorldLight {#com.jsmacrosce.jsmacros.client.api.classes.render.components3d.Surface_setWorldLight-}

```js
setWorldLight(): Surface
```

Makes elements use the block and sky light at the surface's render position, sampled
each frame. Sky light is reduced by the world's sky darkening; cast shadows are not
modelled. If no world is loaded, this uses full-bright lighting.

**Returns:** self for chaining.

**Since:** 2.1.0
