Skip to content

Commit

Permalink
Displacement map notes
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Apr 4, 2024
1 parent b923f9d commit e3ce289
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/en/space-station-14/displacement-maps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Displacement Maps

People asked me for a quicky about how displacement maps work so uhhh...

Ok so in [PR #26709](https://github.com/space-wizards/space-station-14/pull/26709) I added displacement maps. Yippee. This allows you to deform a sprite based on another, specially-crafted sprite. How do we make these specially crafted sprites?

## Displacement map format

Displacement maps should work the same as [BYOND's](http://www.byond.com/docs/ref/index.html#/{notes}/filters/displace). I did not at all test BYOND's so there's a nonzero chance I misread their docs and they're like interpreted inversely or something. Whatever.

The basic idea is that each pixel of the displacement map specifies how much to "shift" the pixels at a position. It should be noted that, due to how GPUs work, this may not work as you may anticipate. Instead of specifying "Pixel X displaces to position Y", it actually specifies "Pixel X reads from position Y". i.e. if you specify a displacement of +1 pixel horizontally, the sprite will visually shift to the **left**, because they get their color from the texture pixel +1 to the right.

The red channel controls horizontal deviation, the green channels controls vertical deviation. A red and green value of 128 is "neutral", i.e. no difference. Greater red or green values cause displacements to be sampled to the bottom right, effectively meaning the sprite moves to the "top left".

The actual displacement value can be changed with a "size" parameter on the shader. Basically this size parameter is "how many pixels are shifted at the maximal pixel value". With a "size" of 4, a value of 255 would shift by 4 pixels. With a size of 127, each value on the channel is a one pixel shift (129 -> +1, 130 -> +2).

## Actual shader

The actual displacement map shader in SS14 is located at `/Textures/Shaders/displacement.swsl`. It has three parameters: `displacementSize` is the size described above, and `displacementMap` and `displacementUV` are intended to be filled out with the `CopyToShaderParameters` system of a sprite component. See the PR I linked above as example.

## Displacement RSI load parameters

Remember to set the following in your RSI's `meta.json` because otherwise sRGB will eat your face and none of the math will make sense:

```json
"load": {
"srgb": false
}
```

## Aseprite scripts

I wrote some Aseprite scripts to help with authoring displacement maps. You can find them in the `Tools/` folder of SS14's repo.
10 changes: 10 additions & 0 deletions src/en/specifications/robust-station-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Key | Meaning
`states` | A list of _states_ that store the actual meat of the RSI, see below.
`license` | Required. A valid [SPDX License Identifier](https://spdx.org/licenses/) applying to this work.
`copyright` | Required. Other arbitrary copyright info such as name, source, ...
`load` | Special loading parameters that will change how the sprites are interpreted by the engine.

### States

Expand Down Expand Up @@ -93,6 +94,15 @@ Note that in practice the JSON writer probably writes the most compact JSON poss
}
```

### Loading Parameters

The `load` key allows various load parameters that change how the engine loads the sprite. Keys are as such:

Key | Meaning
--- | -------
`srgb` | Boolean that indicates whether the sprite is interpreted as sRGB by shaders and such. Default `true`.


## Design Goals

* Editing an RSI must be possible without proper tooling. This means no binary metadata or metadata inside PNG files.
Expand Down

0 comments on commit e3ce289

Please sign in to comment.