Class MutableQuad

java.lang.Object
net.neoforged.neoforge.client.model.quad.MutableQuad

public class MutableQuad extends Object
A mutable representation of a BakedQuad.

This class can be used for constructing quads from scratch, or for loading and modifying existing quads.

It provides several utility methods that go beyond simply manipulating the attributes of the quad:

  • Field Details

    • positions

      private final Vector3f[] positions
    • uvs

      private final long[] uvs
    • normals

      private final int[] normals
    • colors

      private final int[] colors
    • tintIndex

      private int tintIndex
    • direction

      private Direction direction
    • spriteInfo

      private @Nullable BakedQuad.SpriteInfo spriteInfo
    • shade

      private boolean shade
    • lightEmission

      private int lightEmission
    • hasAmbientOcclusion

      private boolean hasAmbientOcclusion
    • lastSourceQuad

      private @Nullable BakedQuad lastSourceQuad
      This is only used to reuse position vectors when possible.
  • Constructor Details

    • MutableQuad

      public MutableQuad()
  • Method Details

    • x

      @Contract(pure=true) public float x(int vertexIndex)
      Returns the x-component of a vertex's position.
      Returns:
      the x-component of a vertex's position
    • y

      @Contract(pure=true) public float y(int vertexIndex)
      Returns the y-component of a vertex's position.
      Returns:
      the y-component of a vertex's position
    • z

      @Contract(pure=true) public float z(int vertexIndex)
      Returns the z-component of a vertex's position.
      Returns:
      the z-component of a vertex's position
    • positionComponent

      @Contract(pure=true) public float positionComponent(int vertexIndex, int componentIndex)
      Returns a component of a vertex's position.
      Returns:
      a component of a vertex's position
      See Also:
    • copyPosition

      @Contract(pure=true) public Vector3f copyPosition(int vertexIndex)
      Copies a vertex's position into a new vector and returns it.
    • copyPosition

      public Vector3f copyPosition(int vertexIndex, Vector3f dest)
      Copies a vertex's position into the given vector and returns it.
    • setX

      public MutableQuad setX(int vertexIndex, float x)
      Sets the x-component of a vertex's position.
    • setY

      public MutableQuad setY(int vertexIndex, float y)
      Sets the y-component of a vertex's position.
    • setZ

      public MutableQuad setZ(int vertexIndex, float z)
      Sets the x-component of a vertex's position.
    • setPositionComponent

      public MutableQuad setPositionComponent(int vertexIndex, int componentIndex, float value)
      Sets a component of a vertex's position.
      See Also:
    • setPosition

      public MutableQuad setPosition(int vertexIndex, float x, float y, float z)
      Sets a vertex's position.
    • setPosition

      public MutableQuad setPosition(int vertexIndex, Vector3fc position)
      Sets a vertex's position.
    • setCubeFaceFromSpriteCoords

      public MutableQuad setCubeFaceFromSpriteCoords(Direction side, float left, float bottom, float right, float top, float depth)
      Sets the positions of this quad to form a rectangle on the given block side using a coordinate-system matching the default orientation of sprites in Vanilla block-models.

      Inspired by the Fabric Renderer API method square.

      The left, bottom, right and top parameters correspond to the default sprite orientation in Vanilla block models. For Direction.UP the "up" direction is facing Direction.NORTH, while for Direction.DOWN, it faces Direction.SOUTH.

      All coordinates use a normalized [0,1] range.

      Passing left=0, bottom=0, right=1, top=1, depth=0 will produce a face on the blocks side.

    • setFullCubeFace

      public MutableQuad setFullCubeFace(Direction side)
    • setCubeFace

      public MutableQuad setCubeFace(Direction side, Vector3fc from, Vector3fc to)
      Same as setCubeFace(Direction, float, float, float, float, float, float), but takes the from and to positions from vectors.
    • setCubeFace

      public MutableQuad setCubeFace(Direction side, float fromX, float fromY, float fromZ, float toX, float toY, float toZ)
      Sets the positions of this quad to the face of a cube as it would be defined in a Vanilla block model.

      All coordinates use a normalized [0,1] range.

    • u

      @Contract(pure=true) public float u(int vertexIndex)
      Returns the horizontal texture coordinate in atlas-space for a vertex.
      Returns:
      the horizontal texture coordinate in atlas-space for a vertex
    • v

      @Contract(pure=true) public float v(int vertexIndex)
      Returns the vertical texture coordinate in atlas-space for a vertex.
      Returns:
      the vertical texture coordinate in atlas-space for a vertex
    • uvComponent

      @Contract(pure=true) public float uvComponent(int vertexIndex, int componentIndex)
      Returns the a texture coordinate in atlas-space for a vertex.
      Returns:
      the a texture coordinate in atlas-space for a vertex
    • packedUv

      @Contract(pure=true) public long packedUv(int vertexIndex)
      Returns the texture coordinates in atlas-space for a vertex in packed form.
      Returns:
      the texture coordinates in atlas-space for a vertex in packed form
      See Also:
    • copyUv

      @Contract(pure=true) public Vector2f copyUv(int vertexIndex)
      Same as copyUv(int, Vector2f), but constructs a destination vector automatically.
    • copyUv

      public Vector2f copyUv(int vertexIndex, Vector2f dest)
      Copies the texture coordinates of a vertex into a given vector and returns it.
    • setUv

      public MutableQuad setUv(int vertexIndex, float u, float v)
      Sets the texture coordinate of a vertex.

      Note that this method expects texture coordinates in the coordinate space of the atlas, not the sprite.

      See Also:
    • setUv

      public MutableQuad setUv(int vertexIndex, Vector2fc uv)
      Sets the texture coordinate of a vertex.

      Note that this method expects texture coordinates in the coordinate space of the atlas, not the sprite.

      See Also:
    • setUvComponent

      public MutableQuad setUvComponent(int vertexIndex, int componentIndex, float value)
      Sets a component of the texture coordinate of a vertex.

      Note that this method expects texture coordinates in the coordinate space of the atlas, not the sprite.

      See Also:
    • setPackedUv

      public MutableQuad setPackedUv(int vertexIndex, long packedUv)
      Sets the texture coordinate of a vertex from their packed representation.

      Note that this method expects texture coordinates in the coordinate space of the atlas, not the sprite.

      See Also:
    • setUvFromSprite

      public MutableQuad setUvFromSprite(int vertexIndex, float u, float v)
      Assigns UV coordinates to a vertex of the current quad based on its sprite and the given UV coordinates within that sprite.
    • setUvFromSprite

      public MutableQuad setUvFromSprite(int vertexIndex, Vector2fc uv)
      Assigns UV coordinates to a vertex of the current quad based on its sprite and the given UV coordinates within that sprite.
    • bakeUvsFromPosition

      public MutableQuad bakeUvsFromPosition()
      Projects each vertex onto the cube face the quad is sourcing its block lighting from, and derives the vertex UV that way.

      Requires spriteInfo() to be set.

    • bakeUvsFromPosition

      public MutableQuad bakeUvsFromPosition(UVTransform transform)
      Same as bakeUvsFromPosition(), but applies a transform to the generated UVs before baking.
    • tintIndex

      @Contract(pure=true) public int tintIndex()
    • setTintIndex

      public MutableQuad setTintIndex(int tintIndex)
    • direction

      @Contract(pure=true) public Direction direction()
    • setDirection

      public MutableQuad setDirection(Direction direction)
    • spriteInfo

      @Contract(pure=true) public @Nullable BakedQuad.SpriteInfo spriteInfo()
      Returns the sprite associated with the quad or null if no sprite has been set yet.

      Note that BakedQuad must have an associated sprite.

      Returns:
      the sprite associated with the quad or null if no sprite has been set yet
    • requiredSpriteInfo

      @Contract(pure=true) public BakedQuad.SpriteInfo requiredSpriteInfo()
      Same as spriteInfo(), but throws an exception if no sprite is set on the quad yet.
      Throws:
      IllegalStateException - If no sprite is set yet.
    • setSpriteInfo

      public MutableQuad setSpriteInfo(BakedQuad.SpriteInfo spriteInfo)
      Changes the texture atlas sprite used by this quad.

      Note that changing the sprite does not automatically translate the current UV coordinates within the atlas to be within this new sprite. Use setSpriteInfoAndMoveUv(BakedQuad.SpriteInfo) to change sprites and remap them, bakeUvsFromPosition() to generate texture coordinates from scratch, or set them manually.

    • setSpriteInfoAndMoveUv

      public MutableQuad setSpriteInfoAndMoveUv(BakedQuad.SpriteInfo spriteInfo)
      Changes the sprite and remaps the UV to the new sprites position in the texture atlas.
      Throws:
      IllegalStateException - If no sprite is currently set. There would be nothing to remap from.
    • shade

      @Contract(pure=true) public boolean shade()
    • setShade

      public MutableQuad setShade(boolean shade)
    • lightEmission

      @Contract(pure=true) public int lightEmission()
    • setLightEmission

      public MutableQuad setLightEmission(int lightEmission)
    • normalX

      @Contract(pure=true) public float normalX(int vertexIndex)
      Returns the x-component of a vertex's normal or NaN if the normal is undefined.
      Returns:
      the x-component of a vertex's normal or NaN if the normal is undefined
    • normalY

      @Contract(pure=true) public float normalY(int vertexIndex)
      Returns the y-component of a vertex's normal or NaN if the normal is undefined.
      Returns:
      the y-component of a vertex's normal or NaN if the normal is undefined
    • normalZ

      @Contract(pure=true) public float normalZ(int vertexIndex)
      Returns the z-component of a vertex's normal or NaN if the normal is undefined.
      Returns:
      the z-component of a vertex's normal or NaN if the normal is undefined
    • normalComponent

      @Contract(pure=true) public float normalComponent(int vertexIndex, int componentIndex)
      Returns a component of a vertex's normal or NaN if the normal is undefined.
      Returns:
      a component of a vertex's normal or NaN if the normal is undefined
      See Also:
    • packedNormal

      @Contract(pure=true) public int packedNormal(int vertexIndex)
      Returns a vertex normal in packed form.
      Returns:
      a vertex normal in packed form
      See Also:
    • copyNormal

      @Contract(pure=true) public Vector3f copyNormal(int vertexIndex)
      Same as copyNormal(int, Vector3f), but constructs a new destination vector automatically.
    • copyNormal

      public Vector3f copyNormal(int vertexIndex, Vector3f dest)
      Copies the normal vector of a vertex into a given vector and returns it.
    • setNormal

      public MutableQuad setNormal(int vertexIndex, float x, float y, float z)
      Sets the normal vector of a vertex.
    • setNormal

      public MutableQuad setNormal(int vertexIndex, Vector3fc normal)
      Sets the normal vector of a vertex.
    • setNormalComponent

      public MutableQuad setNormalComponent(int vertexIndex, int componentIndex, float value)
      Sets a component of a vertex's normal.

      If a normal was unspecified before this method was called, its other components will be set to 0.

      See Also:
    • setPackedNormal

      public MutableQuad setPackedNormal(int vertexIndex, int packedNormal)
      Sets the normal vector of a vertex from its packed representation.
      See Also:
    • setNormal

      public MutableQuad setNormal(BakedNormals bakedNormals)
      Sets the normal vector of all vertices to the normal vectors encoded in the given baked normals object.
    • recomputeNormals

      public MutableQuad recomputeNormals(boolean updateDirection)
      Recomputes the quad normal from the vertex positions.

      Assumes that the quad is planar.

    • color

      @Contract(pure=true) public int color(int vertexIndex)
      Returns the color of a given vertex in ARGB form.
      Returns:
      the color of a given vertex in ARGB form
      See Also:
    • setColor

      public MutableQuad setColor(int packedColor)
      Sets the color of all vertices to a packed ARGB color.
      See Also:
    • setColor

      public MutableQuad setColor(int vertexIndex, int packedColor)
      Sets the color of a vertex to a packed ARGB color.
      See Also:
    • setColor

      public MutableQuad setColor(int vertexIndex, int r, int g, int b, int a)
      Sets the color of a vertex from integer components (0-255).
      See Also:
    • setColor

      public MutableQuad setColor(BakedColors bakedColors)
      Sets the color of all vertices to a packed ARGB color.
    • hasAmbientOcclusion

      @Contract(pure=true) public boolean hasAmbientOcclusion()
    • setHasAmbientOcclusion

      public MutableQuad setHasAmbientOcclusion(boolean hasAmbientOcclusion)
    • setFrom

      public MutableQuad setFrom(BakedQuad quad)
    • transformUvsFromSpriteToAtlas

      private void transformUvsFromSpriteToAtlas()
      Assumes that the UV coordinates are in sprite-space and transforms them to atlas-space.
    • transformUvsFromAtlasToSprite

      private void transformUvsFromAtlasToSprite()
      Assumes that the UV coordinates are in atlas-space and transforms them to sprite-space.
    • toBakedQuad

      @Contract(pure=true) public BakedQuad toBakedQuad()
    • reuseVector

      private static Vector3fc reuseVector(BakedQuad quad, Vector3f position)
      Tries to reuse the position vectors of the last quad we sourced any data from. This avoids unnecessary allocations if the positions of the quad were not transformed, or if a rotation simply rotated the order of positions.
    • transform

      public MutableQuad transform(Matrix4f rotation)
      Applies the given matrix to this quads position and normals (if specified).

      Note that the direction() is not transformed.

    • recalculateWinding

      public MutableQuad recalculateWinding()
      Recalculates the order of vertices to conform to the order expected by the Vanilla AO algorithm.

      It uses the current direction and only works correctly for axis-aligned quads.

      See Also:
    • copy

      public MutableQuad copy()
      Returns a copy of this mutable quad.
      Returns:
      a copy of this mutable quad
    • copyInto

      public MutableQuad copyInto(MutableQuad dest)
      Copies the contents of this mutable quad into the provided mutable quad and returns it.
    • reset

      public MutableQuad reset()