Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background color and glsl-based alpha #78

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/ArkPetsConfigDefault.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"background_color":"#00000000",
"behavior_ai_activation":8,
"behavior_allow_interact":true,
"behavior_allow_sit":true,
Expand Down
4 changes: 4 additions & 0 deletions assets/UI/SettingsModule.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<Label text="高亮描边"/>
<JFXComboBox fx:id="configRenderOutline" prefWidth="120.0"/>
</HBox>
<HBox>
<Label text="背景颜色"/>
<JFXComboBox fx:id="configBackgroundColor" prefWidth="120.0"/>
</HBox>
<Separator/>
<Label styleClass="config-group-title" text="高级设置"/>
<HBox>
Expand Down
8 changes: 6 additions & 2 deletions assets/shaders/OutlineFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

// Gap Seaming and Ouline Effect Fragment Shader for TwoColorPolygonBatch.

#version 130
#version 120

varying vec2 v_texCoords; // From VS
uniform sampler2D u_texture; // From TCPB
uniform vec3 u_outlineColor; // Required
uniform float u_outlineWidth; // Required
uniform ivec2 u_textureSize;
uniform float u_alpha;

const float c_alphaLv0 = 0.1;
const float c_alphaLv1 = 0.5;
Expand All @@ -31,7 +33,7 @@ vec4[8] getNeighbors(sampler2D tex, vec2 texCoords, vec2 offset) {

void main() {
vec4 texColor = texture2D(u_texture, v_texCoords);
ivec2 texSize = textureSize(u_texture, 0);
ivec2 texSize = u_textureSize;

if (texColor.a < c_alphaLv0) {
// Outline effect apply on transparent areas
Expand Down Expand Up @@ -76,4 +78,6 @@ void main() {
// No effect apply on other areas
gl_FragColor = texColor;
}
// Alpha
gl_FragColor.a -= (1 - u_alpha);
}
2 changes: 1 addition & 1 deletion assets/shaders/TCPBFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Common Fragment Shader for TwoColorPolygonBatch.

#version 130
#version 120

#ifdef GL_ES
#define LOWP lowp
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/TCPBVertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Common Vertex Shader for TwoColorPolygonBatch.

#version 130
#version 120

attribute vec4 a_position;
attribute vec4 a_light;
Expand Down
7 changes: 6 additions & 1 deletion core/src/cn/harryh/arkpets/ArkChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class ArkChar {
protected final AnimClipGroup animList;
protected final HashMap<AnimStage, Insert> stageInsertMap;

protected float alpha;

/** Initializes an ArkPets character.
* @param config The ArkPets Config instance which contains the asset's information and other essential settings.
* @param scale The scale of the skeleton.
Expand All @@ -62,6 +64,7 @@ public ArkChar(ArkConfig config, float scale) {
camera.setMinInsert(canvasReserveLength - canvasMaxSize);
batch = new TwoColorPolygonBatch();
renderer = new SkeletonRenderer();
Color backgroundColor = config.getBackgroundColor();
isHarryh marked this conversation as resolved.
Show resolved Hide resolved
/* Pre-multiplied alpha shouldn't be applied to models released in Arknights 2.1.41 or later,
otherwise you may get a corrupted rendering result. */
renderer.setPremultipliedAlpha(false);
Expand Down Expand Up @@ -120,7 +123,7 @@ protected void onApply(AnimData playing) {
}
};
// 6.Canvas setup
setCanvas(Color.CLEAR);
setCanvas(backgroundColor);
stageInsertMap = new HashMap<>();
for (AnimStage stage : animList.clusterByStage().keySet()) {
// Figure out the suitable canvas size
Expand Down Expand Up @@ -227,6 +230,8 @@ protected void renderToBatch() {
shader2.bind();
shader2.setUniformf("u_outlineColor", 1f, 1f, 0f);
shader2.setUniformf("u_outlineWidth", outlineWidth.now());
shader2.setUniformi("u_textureSize", passedTexture.getWidth(), passedTexture.getHeight());
shader2.setUniformf("u_alpha", alpha);
batch.setShader(shader2);
ScreenUtils.clear(0, 0, 0, 0, true);
batch.begin();
Expand Down
17 changes: 17 additions & 0 deletions core/src/cn/harryh/arkpets/ArkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.alibaba.fastjson.annotation.JSONField;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;

import java.io.*;
import java.net.URL;
Expand Down Expand Up @@ -38,6 +39,8 @@ public class ArkConfig implements Serializable {


// Config items and default values:
/** @since ArkPets 3.3 */ @JSONField(defaultValue = "#00000000")
public String background_color;
/** @since ArkPets 1.0 */ @JSONField(defaultValue = "8")
public int behavior_ai_activation;
/** @since ArkPets 1.0 */ @JSONField(defaultValue = "true")
Expand Down Expand Up @@ -111,6 +114,20 @@ public boolean isNewcomer() {
return isNewcomer;
}

/** Returns converted background color.
*/
@JSONField(serialize = false)
public Color getBackgroundColor() {
Color backgroundColor;
if (background_color.matches("^#[0-9a-fA-F]{8}$")) {
backgroundColor = Color.valueOf(background_color);
} else {
Logger.warn("System", "Invalid background color,using transparent");
backgroundColor = Color.CLEAR;
}
return backgroundColor;
}

/** Gets the custom ArkConfig object by reading the external config file.
* If the external config file does not exist, a default config file will be generated.
* @return An ArkConfig object. {@code null} if failed.
Expand Down
5 changes: 4 additions & 1 deletion core/src/cn/harryh/arkpets/ArkPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -61,6 +62,8 @@ public void create() {
config = Objects.requireNonNull(ArkConfig.getConfig(), "ArkConfig returns a null instance, please check the config file.");
Gdx.input.setInputProcessor(this);
Gdx.graphics.setForegroundFPS(config.display_fps);
Logger.info("System", "OpenGL Version " + Gdx.gl.glGetString(GL20.GL_VERSION));
isHarryh marked this conversation as resolved.
Show resolved Hide resolved
Logger.info("System", "OpenGL Vendor " + Gdx.gl.glGetString(GL20.GL_VENDOR));

// 2.Character setup
Logger.info("App", "Using model asset \"" + config.character_asset + "\"");
Expand Down Expand Up @@ -142,7 +145,7 @@ public void render() {
setWindowPos();
if (!windowAlpha.isEnded()) {
windowAlpha.addProgress(Gdx.graphics.getDeltaTime());
hWndMine.setWindowAlpha(windowAlpha.now());
cha.alpha = windowAlpha.now();
}
promiseToolwindowStyle(1);

Expand Down
2 changes: 1 addition & 1 deletion core/src/cn/harryh/arkpets/tray/MemberTrayImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void onExit() {
public void run() {
Gdx.app.exit();
}
}, (int)durationNormal.toSeconds());
}, (int)durationNormal.toMillis());
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions desktop/src/cn/harryh/arkpets/EmbeddedLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import cn.harryh.arkpets.utils.Logger;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.system.MemoryUtil;
Expand All @@ -29,8 +28,9 @@ public static void main (String[] args) {
ArgPending.argCache = args;
// Logger
Logger.initialize(LogConfig.logCorePath, LogConfig.logCoreMaxKeep);
ArkConfig appConfig = Objects.requireNonNull(ArkConfig.getConfig());
try {
Logger.setLevel(Objects.requireNonNull(ArkConfig.getConfig()).logging_level);
Logger.setLevel(appConfig.logging_level);
} catch (Exception ignored) {
}
new ArgPending(LogConfig.errorArg, args) {
Expand Down Expand Up @@ -74,7 +74,7 @@ protected void process(String command, String addition) {
// Configure window display
config.setInitialVisible(true);
config.setTransparentFramebuffer(true);
config.setInitialBackgroundColor(Color.CLEAR);
config.setInitialBackgroundColor(appConfig.getBackgroundColor());
// Handle GLFW error
GLFW.glfwSetErrorCallback(new GLFWErrorCallback() {
@Override
Expand Down
12 changes: 12 additions & 0 deletions desktop/src/cn/harryh/arkpets/controllers/SettingsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cn.harryh.arkpets.guitasks.GuiTask;
import cn.harryh.arkpets.utils.*;
import cn.harryh.arkpets.utils.GuiComponents.*;
import com.badlogic.gdx.graphics.Color;
import com.jfoenix.controls.*;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
Expand Down Expand Up @@ -40,6 +41,8 @@ public final class SettingsModule implements Controller<ArkHomeFX> {
@FXML
private JFXComboBox<NamedItem<Integer>> configRenderOutline;
@FXML
private JFXComboBox<NamedItem<Integer>> configBackgroundColor;
@FXML
private JFXCheckBox configWindowTopmost;
@FXML
private JFXComboBox<String> configLoggingLevel;
Expand Down Expand Up @@ -140,6 +143,15 @@ public String getContent() {
app.config.display_render_outline = newValue.value();
app.config.save();
});
new ComboBoxSetup<>(configBackgroundColor).setItems(new NamedItem<>("透明", 0x00000000),
new NamedItem<>("绿色", 0x00ff00ff),
new NamedItem<>("蓝色", 0x0000ffff),
new NamedItem<>("品红色", 0xff00ffff))
.selectValue(Color.rgba8888(app.config.getBackgroundColor()), app.config.background_color + "(自定义)")
.setOnNonNullValueUpdated((observable, oldValue, newValue) -> {
app.config.background_color = String.format("#%08X", newValue.value());
app.config.save();
});
configWindowTopmost.setSelected(app.config.window_style_topmost);
configWindowTopmost.setOnAction(e -> {
app.config.window_style_topmost = configWindowTopmost.isSelected();
Expand Down
Loading