Skip to content

Commit

Permalink
Make blending data-driven
Browse files Browse the repository at this point in the history
  • Loading branch information
Alluysl committed Jun 5, 2021
1 parent 1c390b9 commit 19b7bc7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.3

# Mod Properties
mod_version = 1.2.0-alpha
mod_version = 1.2.0-beta
maven_group = alluysl
archives_base_name = alluyslorigins

Expand Down
1 change: 1 addition & 0 deletions src/main/java/alluysl/alluyslorigins/AlluyslOrigins.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class AlluyslOrigins implements ModInitializer {

public static final String MODID = "alluyslorigins";
public static final int NO_BLEND = 0; // the value shall not conflict with any of the OpenGL blend mode macros

@Override
public void onInitialize() {
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/alluysl/alluyslorigins/mixin/GameRendererMixin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package alluysl.alluyslorigins.mixin;

import alluysl.alluyslorigins.AlluyslOrigins;
import alluysl.alluyslorigins.power.OverlayPower;
import alluysl.alluyslorigins.util.OverlayInfo;
import com.mojang.blaze3d.platform.GlStateManager;
Expand All @@ -25,14 +26,13 @@
@Mixin(GameRenderer.class)
public abstract class GameRendererMixin {

private final int NO_BLEND = 0;
private final int defaultBlendEquation = GL_FUNC_ADD;

private float r = 0, g = 0, b = 0, a = 1.0F;
private final double[][] vertices = new double[4][2];
private Identifier texture = null;
private int blendEquation = defaultBlendEquation;
private int srcFactor, dstFactor, srcAlpha, dstAlpha;
private int srcFactor = GL_ONE, dstFactor = GL_ONE, srcAlpha = GL_ONE, dstAlpha = GL_ONE;

private void resetTexture(){ texture = null; }
private void setTexture(Identifier id){ texture = id; }
Expand All @@ -52,7 +52,7 @@ private void setBlendFunc() { // edited blendFuncSeparate
private void drawTexture(){ // edited method_31136 (second part)
RenderSystem.disableDepthTest();
RenderSystem.depthMask(false);
if (blendEquation != NO_BLEND){
if (blendEquation != AlluyslOrigins.NO_BLEND){
RenderSystem.enableBlend();
if (blendEquation != GL_FUNC_ADD) // default
RenderSystem.blendEquation(blendEquation);
Expand All @@ -72,7 +72,7 @@ private void drawTexture(){ // edited method_31136 (second part)
tessellator.draw();

RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
if (blendEquation != NO_BLEND){
if (blendEquation != AlluyslOrigins.NO_BLEND){
RenderSystem.defaultBlendFunc();
if (blendEquation != GL_FUNC_ADD)
RenderSystem.blendEquation(GL_FUNC_ADD);
Expand Down Expand Up @@ -103,13 +103,15 @@ private void setTextureCentered(double scale) {

private void drawOverlay(OverlayPower power, float ratio){
setTextureCentered(MathHelper.lerp(ratio, power.startScale, power.endScale));
r = ratio * power.r;
g = ratio * power.g;
b = ratio * power.b;
a = 1.0F;
float colorRatio = power.ratioDrivesColor ? ratio : 1;
r = colorRatio * power.r;
g = colorRatio * power.g;
b = colorRatio * power.b;
a = (power.ratioDrivesAlpha ? ratio : 1) * power.a;
setTexture(power.texture);
blendEquation = defaultBlendEquation;
srcFactor = dstFactor = srcAlpha = dstAlpha = GL_ONE;
blendEquation = power.blendEquation;
srcFactor = power.srcFactor; dstFactor = power.dstFactor;
srcAlpha = power.srcAlpha; dstAlpha = power.dstAlpha;
drawTexture();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ public static void register(){
.add("texture", SerializableDataType.IDENTIFIER, new Identifier("textures/misc/nausea.png"))
.add("style", SerializableDataType.STRING, "classic")
.add("scale", SerializableDataType.FLOAT, 1.0F)
.addFunctionedDefault("start_scale", SerializableDataType.FLOAT, data -> data.getFloat("scale")),
.addFunctionedDefault("start_scale", SerializableDataType.FLOAT, data -> data.getFloat("scale"))
.add("blend_equation", SerializableDataType.STRING, "")
.add("blend_mode", SerializableDataType.STRING, "")
.add("source_factor", SerializableDataType.STRING, "")
.add("destination_factor", SerializableDataType.STRING, "")
.add("source_alpha_factor", SerializableDataType.STRING, "")
.add("destination_alpha_factor", SerializableDataType.STRING, "")
.addFunctionedDefault("ratio_drives_color", SerializableDataType.BOOLEAN, data -> data.getString("style").equals("classic"))
.addFunctionedDefault("ratio_drives_alpha", SerializableDataType.BOOLEAN, data -> data.getString("style").equals("classic_alpha")),
data -> (type, player) -> new OverlayPower(
type, player,
data.hashCode(), // thankfully, the hash is consistent, and powers with identical JSON files even get a different hash!
Expand All @@ -84,7 +92,15 @@ public static void register(){
data.getId("texture"),
data.getString("style"),
data.getFloat("start_scale"),
data.getFloat("scale"))
data.getFloat("scale"),
data.getString("blend_equation").equals("") ?
data.getString("blend_mode") : data.getString("blend_equation"),
data.getString("source_factor"),
data.getString("destination_factor"),
data.getString("source_alpha_factor"),
data.getString("destination_alpha_factor"),
data.getBoolean("ratio_drives_color"),
data.getBoolean("ratio_drives_alpha"))
).allowCondition());

System.out.println("[Alluysl's Origins] Powers registered.");
Expand Down
62 changes: 59 additions & 3 deletions src/main/java/alluysl/alluyslorigins/power/OverlayPower.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package alluysl.alluyslorigins.power;

import alluysl.alluyslorigins.AlluyslOrigins;
import io.github.apace100.origins.power.Power;
import io.github.apace100.origins.power.PowerType;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;

import static org.lwjgl.opengl.GL14.*;

public class OverlayPower extends Power {

private final int id;
public float r, g, b, a;
public int
upTicks, /** amount of ticks for the overlay to fully appear */
downTicks; /** amount of ticks for the overlay to fully disappear */
public float startScale, /** scale at ratio 0 */
endScale; /** scale at ratio 1 */
public Identifier texture;
public String style; /** the type of overlay positioning */
public int blendEquation, srcFactor, dstFactor, srcAlpha, dstAlpha; /** OpenGL blending parameters */
public boolean ratioDrivesColor, ratioDrivesAlpha; /** whether the progress ratio of the overlay should influence the color/alpha values */
public float startScale, /** scale at ratio 0 */
endScale; /** scale at ratio 1 */

public OverlayPower(PowerType<?> type, PlayerEntity player, int id, float r, float g, float b, float a, int upTicks, int downTicks, Identifier texture, String style, float startScale, float endScale) {
public OverlayPower(PowerType<?> type, PlayerEntity player, int id,
float r, float g, float b, float a,
int upTicks, int downTicks, Identifier texture, String style,
float startScale, float endScale, String blendEquation,
String srcFactor, String dstFactor, String srcAlpha, String dstAlpha,
boolean ratioDrivesColor, boolean ratioDrivesAlpha) {
super(type, player);
this.id = id;
this.r = MathHelper.clamp(r, 0.0F, 1.0F);
Expand All @@ -31,6 +42,51 @@ public OverlayPower(PowerType<?> type, PlayerEntity player, int id, float r, flo
this.style = style;
this.startScale = startScale;
this.endScale = endScale;
this.blendEquation = getBlendEquation(blendEquation, style);
this.srcFactor = getFactor(srcFactor, false);
this.dstFactor = getFactor(dstFactor, true);
this.srcAlpha = srcAlpha.equals("") ? this.srcFactor : getFactor(srcFactor, false);
this.dstAlpha = dstAlpha.equals("") ? this.dstFactor : getFactor(dstFactor, true);
this.ratioDrivesColor = ratioDrivesColor;
this.ratioDrivesAlpha = ratioDrivesAlpha;
}

private int getBlendEquation(String name, String style){
switch (name){
case "add": case "additive": return GL_FUNC_ADD;
case "sub": case "subtract": case "subtractive": return GL_FUNC_SUBTRACT;
case "rsub": case "reverse_subtract": case "reverse_subtractive": return GL_FUNC_REVERSE_SUBTRACT;
case "min": case "minimum": return GL_MIN;
case "max": case "maximum": return GL_MAX;
case "none": case "no_blend": case "no_blending": return AlluyslOrigins.NO_BLEND;
default: return style.equals("classic") || style.equals("classic_alpha") ? GL_FUNC_ADD : AlluyslOrigins.NO_BLEND;
}
}
private int getFactor(String name, boolean destination){
switch (name){
case "zero": case "GL_ZERO": return GL_ZERO;
case "one": case "GL_ONE": return GL_ONE;
case "source_color": case "GL_SRC_COLOR": return GL_SRC_COLOR;
case "one_minus_source_color": case "GL_ONE_MINUS_SRC_COLOR": return GL_ONE_MINUS_SRC_COLOR;
case "destination_color": case "GL_DST_COLOR": return GL_DST_COLOR;
case "one_minus_destination_color": case "GL_ONE_MINUS_DST_COLOR": return GL_ONE_MINUS_DST_COLOR;
case "source_alpha": case "GL_SRC_ALPHA": return GL_SRC_ALPHA;
case "one_minus_source_alpha": case "GL_ONE_MINUS_SRC_ALPHA": return GL_ONE_MINUS_SRC_ALPHA;
case "destination_alpha": case "GL_DST_ALPHA": return GL_DST_ALPHA;
case "one_minus_destination_alpha": case "GL_ONE_MINUS_DST_ALPHA": return GL_ONE_MINUS_DST_ALPHA;
case "constant_color": case "GL_CONSTANT_COLOR": return GL_CONSTANT_COLOR;
case "one_minus_constant_color": case "GL_ONE_MINUS_CONSTANT_COLOR": return GL_ONE_MINUS_CONSTANT_COLOR;
case "constant_alpha": case "GL_CONSTANT_ALPHA": return GL_CONSTANT_ALPHA;
case "one_minus_constant_alpha": case "GL_ONE_MINUS_CONSTANT_ALPHA": return GL_ONE_MINUS_CONSTANT_ALPHA;
case "source_alpha_saturate": case "GL_SRC_ALPHA_SATURATE": return GL_SRC_ALPHA_SATURATE;
// The four following modes aren't useful to my knowledge since afaik there is only one source buffer (constants are from the GL15 class)
// case "second_source_color": case "GL_SRC1_COLOR": return GL_SRC1_COLOR;
// case "one_minus_second_source_color": case "GL_ONE_MINUS_SRC1_COLOR": return GL_ONE_MINUS_SRC1_COLOR;
// case "second_source_alpha": case "GL_SRC1_ALPHA": return GL_SRC1_ALPHA;
// case "one_minus_second_source_alpha": case "GL_ONE_MINUS_SRC1_ALPHA": return GL_ONE_MINUS_SRC1_ALPHA;
default: return style.equals("classic") ? GL_ONE :
destination ? GL_ONE_MINUS_SRC_ALPHA : GL_SRC_ALPHA;
}
}

public int getId(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"g": 0.1,
"b": 0.05,
"up_ticks": 10,
"style": "classic",
"down_ticks": 10,
"start_scale": 2.0,
"end_scale": 1.0,
"scale": 1.0,
"ratio_drives_color": true,
"ratio_drives_alpha": false,
"condition": {
"type": "origins:resource",
"resource": "alluyslorigins:burrow_on",
Expand Down

0 comments on commit 19b7bc7

Please sign in to comment.