Skip to content

Commit

Permalink
Merge branch 'graphics/simplified-setuniform-methods' into performanc…
Browse files Browse the repository at this point in the history
…e/quadtree
  • Loading branch information
ultraq committed Jun 28, 2024
2 parents e3da262 + 889ae4a commit db22c27
Show file tree
Hide file tree
Showing 22 changed files with 188 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class Map extends Node<Map> {
return scene
.requestCreateOrGet(new MeshRequest(MeshType.TRIANGLES,
new VertexBufferLayout(Attribute.POSITION, Attribute.COLOUR, Attribute.TEXTURE_UVS),
allVertices, Colour.WHITE, allTextureUVs, allIndices, false))
allVertices, Colour.WHITE, allTextureUVs, allIndices))
.thenAcceptAsync { newMesh ->
fullMesh = newMesh
}
Expand All @@ -376,6 +376,14 @@ class Map extends Node<Map> {
},
tileSetSpriteSheetFuture.thenApplyAsync { spriteSheet ->
material.texture = spriteSheet.texture
material.with {
texture = spriteSheet.texture
framesHorizontal = spriteSheet.framesHorizontal
framesVertical = spriteSheet.framesVertical
frameStepX = spriteSheet.frameStepX
frameStepY = spriteSheet.frameStepY
frame = 0
}
return spriteSheet
}
)
Expand Down Expand Up @@ -469,7 +477,7 @@ class Map extends Node<Map> {

var overlay = new PalettedSprite(tileFile)
overlay.name = "${tile.name} - Variant ${imageVariant}"
overlay.initialFrame = imageVariant
overlay.frame = imageVariant
overlay.position = new Vector2f(tilePos).asWorldCoords(1)
addChild(overlay)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package nz.net.ultraq.redhorizon.classic.nodes

import nz.net.ultraq.redhorizon.classic.Faction
import nz.net.ultraq.redhorizon.classic.shaders.Shaders
import nz.net.ultraq.redhorizon.engine.graphics.GraphicsRenderer
import nz.net.ultraq.redhorizon.engine.graphics.GraphicsRequests.ShaderRequest
import nz.net.ultraq.redhorizon.engine.graphics.GraphicsRequests.SpriteMeshRequest
import nz.net.ultraq.redhorizon.engine.scenegraph.Scene
Expand Down Expand Up @@ -92,8 +91,19 @@ class PalettedSprite extends Sprite implements FactionColours {
CompletableFuture<Void> onSceneAddedAsync(Scene scene) {

return CompletableFuture.allOf(
scene
.requestCreateOrGet(new SpriteMeshRequest(bounds, region))
spriteSheetGenerator.generate(scene)
.thenComposeAsync { newSpriteSheet ->
spriteSheet = newSpriteSheet
material.with {
texture = spriteSheet.texture
framesHorizontal = spriteSheet.framesHorizontal
framesVertical = spriteSheet.framesVertical
frameStepX = spriteSheet.frameStepX
frameStepY = spriteSheet.frameStepY
frame = this.frame
}
return scene.requestCreateOrGet(new SpriteMeshRequest(bounds, spriteSheet.textureRegion.scale(repeatX, repeatY)))
}
.thenAcceptAsync { newMesh ->
mesh = newMesh
},
Expand All @@ -102,37 +112,12 @@ class PalettedSprite extends Sprite implements FactionColours {
.thenAcceptAsync { requestedShader ->
shader = requestedShader
},
spriteSheetGenerator.generate(scene)
.thenApplyAsync { newSpriteSheet ->
spriteSheet = newSpriteSheet
material.texture = spriteSheet.texture

// TODO: Some uses are a repeating tile, others aren't. There should be a unified way of doing this 🤔
if (repeatX != 1f || repeatY != 1f) {
region.setMax(repeatX, repeatY)
}
else {
region.set(spriteSheet[initialFrame])
}
},
CompletableFuture.runAsync { ->
material.adjustmentMap = buildAdjustmentMap(faction)
}
)
}

@Override
void render(GraphicsRenderer renderer) {

if (material.adjustmentMap) {
if (factionChanged) {
material.adjustmentMap = buildAdjustmentMap(faction)
factionChanged = false
}
super.render(renderer)
}
}

/**
* Update the faction being applied to this sprite.
*/
Expand All @@ -142,4 +127,14 @@ class PalettedSprite extends Sprite implements FactionColours {
FactionColours.super.setFaction(faction)
factionChanged = true
}

@Override
void update() {

if (material.adjustmentMap && factionChanged) {
material.adjustmentMap = buildAdjustmentMap(faction)
factionChanged = false
}
super.update()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ layout (std140) uniform Camera {
mat4 view;
};
uniform mat4 model;
uniform int framesHorizontal;
uniform int framesVertical;
uniform float frameStepX;
uniform float frameStepY;
uniform int frame;

/**
* Vertex shader main function, mostly passes geometry information along to the
Expand All @@ -21,5 +26,9 @@ void main() {

gl_Position = projection * view * model * position;
v_vertexColour = colour;
v_textureUVs = textureUVs;

// Adjust textureUVs to the location of the selected frame in the spritesheet
float textureU = (frame % framesHorizontal) * frameStepX;
float textureV = floor(frame / framesHorizontal) * frameStepY;
v_textureUVs = textureUVs + vec2(textureU, textureV);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class PalettedSpriteShader extends ShaderConfig {
},
{ shader, material, window ->
shader.setUniform('adjustmentMap', material.adjustmentMap)
},
{ shader, material, window ->
shader.setUniform('frame', material.frame)
shader.setUniform('framesHorizontal', material.framesHorizontal)
shader.setUniform('framesVertical', material.framesVertical)
shader.setUniform('frameStepX', material.frameStepX)
shader.setUniform('frameStepY', material.frameStepY)
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class Unit extends Node<Unit> implements FactionColours, Rotatable, Temporal {
var closestHeading = Math.round(heading / degreesPerHeading)
var rotationFrame = closestHeading ? (headings - closestHeading) * frames as int : 0
var animationFrame = frames ? Math.floor((currentTimeMs - animationStartTime) / 1000 * FRAMERATE) % frames as int : 0
region.set(spriteSheet.getFrame(unitData.shpFile.getStateFramesOffset(currentState) + rotationFrame + animationFrame))
frame = unitData.shpFile.getStateFramesOffset(currentState) + rotationFrame + animationFrame
}

super.update()
Expand All @@ -233,7 +233,7 @@ class Unit extends Node<Unit> implements FactionColours, Rotatable, Temporal {
var turretHeadings = unitData.shpFile.parts.turret.headings
var closestTurretHeading = Math.round(heading / degreesPerHeading)
var turretRotationFrame = closestTurretHeading ? turretHeadings - closestTurretHeading as int : 0
region.set(spriteSheet.getFrame(unitData.shpFile.parts.body.headings + turretRotationFrame))
frame = unitData.shpFile.parts.body.headings + turretRotationFrame
}

super.update()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ interface GraphicsRenderer extends AutoCloseable, EventTarget {
*/
default Mesh createMesh(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour) {

return createMesh(type, layout, vertices, colour, null, null, false)
return createMesh(type, layout, vertices, colour, null, null)
}

/**
* Create a mesh with all of the mesh parts.
*/
Mesh createMesh(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, Vector2f[] textureUVs,
int[] indices, boolean dynamic)
int[] indices)

/**
* Create a new shader program from the given configuration, or return the
Expand All @@ -87,10 +87,7 @@ interface GraphicsRenderer extends AutoCloseable, EventTarget {
* Create a mesh to represent a surface onto which a texture will go. This is
* a convenience method for {@link #createMesh}.
*/
default Mesh createSpriteMesh(Rectanglef surface) {

return createSpriteMesh(surface, new Rectanglef(0, 0, 1, 1))
}
Mesh createSpriteMesh(Rectanglef surface)

/**
* Create a mesh to represent a surface onto which a texture will go. This is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ interface GraphicsRequests {

@ImmutableOptions(knownImmutables = ['layout', 'colour'])
static record MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour,
Vector2f[] textureUVs, int[] indices, boolean dynamic) implements Request<Mesh> {
Vector2f[] textureUVs, int[] indices) implements Request<Mesh> {

MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, int[] indices, boolean dynamic) {
this(type, layout, vertices, colour, null, indices, dynamic)
MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, int[] indices) {
this(type, layout, vertices, colour, null, indices)
}

MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour) {
this(type, layout, vertices, colour, null, null, false)
this(type, layout, vertices, colour, null, null)
}
}

@ImmutableOptions(knownImmutables = ['surface', 'textureUVs'])
static record SpriteMeshRequest(Rectanglef surface, Rectanglef textureUVs) implements Request<Mesh> {
SpriteMeshRequest(Rectanglef surface) {
this(surface, null)
this(surface, new Rectanglef(0, 0, 1, 1))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,8 @@ class GraphicsSystem extends EngineSystem implements GraphicsRequests {
var resource = switch (request) {
case ShaderRequest -> renderer.createShader(request.shaderConfig())
case MeshRequest -> renderer.createMesh(request.type(), request.layout(), request.vertices(), request.colour(),
request.textureUVs(), request.indices(), request.dynamic())
case SpriteMeshRequest -> {
if (request.textureUVs() != null) {
yield renderer.createSpriteMesh(request.surface(), request.textureUVs())
}
else {
yield renderer.createSpriteMesh(request.surface())
}
}
request.textureUVs(), request.indices())
case SpriteMeshRequest -> renderer.createSpriteMesh(request.surface(), request.textureUVs())
case TextureRequest -> renderer.createTexture(request.width(), request.height(), request.format(), request.data())
case SpriteSheetRequest -> renderer.createSpriteSheet(request.width(), request.height(), request.format(), request.data())
case UniformBufferRequest -> renderer.createUniformBuffer(request.name(), request.data())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,52 @@ package nz.net.ultraq.redhorizon.engine.graphics
*/
class Material {

private static final String KEY_FRAME = 'frame'
private static final String KEY_FRAMES_HORIZONTAL = 'framesHorizontal'
private static final String KEY_FRAMES_VERTICAL = 'framesVertical'
private static final String KEY_FRAME_STEP_X = 'frameStepX'
private static final String KEY_FRAME_STEP_Y = 'frameStepY'

final Map<String, Object> attributes = [:]
Texture texture

int getFrame() {
return attributes[KEY_FRAME]
}

float getFrameStepX() {
return attributes[KEY_FRAME_STEP_X]
}

float getFrameStepY() {
return attributes[KEY_FRAME_STEP_Y]
}

int getFramesHorizontal() {
return attributes[KEY_FRAMES_HORIZONTAL]
}

int getFramesVertical() {
return attributes[KEY_FRAMES_VERTICAL]
}

void setFrame(int frame) {
attributes[KEY_FRAME] = frame
}

void setFrameStepX(float frameStepX) {
attributes[KEY_FRAME_STEP_X] = frameStepX
}

void setFrameStepY(float frameStepY) {
attributes[KEY_FRAME_STEP_Y] = frameStepY
}

void setFramesHorizontal(int framesHorizontal) {
attributes[KEY_FRAMES_HORIZONTAL] = framesHorizontal
}

void setFramesVertical(int framesVertical) {
attributes[KEY_FRAMES_VERTICAL] = framesVertical
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,4 @@ abstract class Mesh implements GraphicsResource {
* Use this mesh in upcoming render operations.
*/
abstract void bind()

/**
* Update the textureUVs part of the mesh data. This is only allowed on
* meshes that have been configured to use dynamic buffer data.
*/
abstract void updateTextureUvs(Vector2f[] textureUVs)
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,13 @@ abstract class Shader implements GraphicsResource {
}

/**
* Apply a data uniform to the shader. The type of data is determined by the
* size of the data array.
*
* @param name
* @param data
*/
abstract void setUniform(String name, float[] data)

/**
* Apply a data uniform to the shader. The type of data is determined by the
* size of the data array.
*
* @param name
* @param data
* Apply a data uniform to the shader. If {@code data} is an array, it can be
* used to determine the shader type (eg: 2 floats = vec2).
*/
abstract void setUniform(String name, int[] data)

/**
* Apply a matrix uniform to the shader.
*
* @param name
* @param matrix
*/
abstract void setUniform(String name, Matrix4f matrix)
abstract void setUniform(String name, Object data)

/**
* Apply a texture uniform using the given texture ID.
*
* @param name
* @param textureUnit
* @param textureId
*/
abstract void setUniformTexture(String name, int textureUnit, Texture texture)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class SpriteSheet implements GraphicsResource {
/**
* Return coordinates on the sprite sheet that would locate the sprite with
* the corresponding index from the raw data.
* <p>
* Note that with texture adjustments calculated in the shader, there
* shouldn't be a need for this any more.
*/
Rectanglef getFrame(int index) {

Expand All @@ -65,4 +68,12 @@ class SpriteSheet implements GraphicsResource {
textureV + frameStepY as float
)
}

/**
* Return a texture region for use with this spritesheet.
*/
Rectanglef getTextureRegion() {

return getFrame(0)
}
}
Loading

0 comments on commit db22c27

Please sign in to comment.