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

added reinit particle shader #74

Merged
merged 1 commit into from
May 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
package dev.lyzev.schizoid.injection.mixins.minecraft.client;

import dev.lyzev.api.events.EventItemUse;
import dev.lyzev.api.events.EventReload;
import dev.lyzev.api.events.EventReloadShader;
import dev.lyzev.api.events.EventStartup;
import dev.lyzev.api.events.EventWindowResize;
import dev.lyzev.schizoid.Schizoid;
import net.minecraft.client.MinecraftClient;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -79,7 +78,7 @@ private void onItemUse(CallbackInfo ci) {

@Inject(method = "reloadResources()Ljava/util/concurrent/CompletableFuture;", at = @At(value = "HEAD"))
private void onReloadResources(CallbackInfoReturnable<CompletableFuture<Void>> cir) {
EventReload.INSTANCE.fire();
EventReloadShader.INSTANCE.fire();
}

}
2 changes: 1 addition & 1 deletion src/main/kotlin/dev/lyzev/api/events/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class EventAttackEntity(val player: PlayerEntity, val entity: Entity) : Event

class EventOSThemeUpdate(val theme: OSTheme.Theme) : Event

object EventReload : Event
object EventReloadShader : Event

class EventGetFOV(var fov: Double) : Event

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package dev.lyzev.api.imgui.render.renderable

import dev.lyzev.api.events.EventReload
import dev.lyzev.api.events.EventReloadShader
import dev.lyzev.api.imgui.font.ImGuiFonts.OPEN_SANS_BOLD
import dev.lyzev.api.imgui.font.ImGuiFonts.OPEN_SANS_REGULAR
import dev.lyzev.api.imgui.render.ImGuiRenderable
Expand All @@ -22,7 +22,7 @@ class ImGuiRenderableDeveloperTool : ImGuiRenderable {
OPEN_SANS_BOLD.end()
OPEN_SANS_REGULAR.begin()
if (button("Reload Shaders", getColumnWidth(), OPEN_SANS_REGULAR.size + getStyle().framePaddingY * 2)) {
EventReload.fire()
EventReloadShader.fire()
}
OPEN_SANS_REGULAR.end()
}
Expand Down
52 changes: 36 additions & 16 deletions src/main/kotlin/dev/lyzev/api/opengl/shader/Shader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package dev.lyzev.api.opengl.shader

import com.mojang.blaze3d.systems.RenderSystem
import dev.lyzev.api.events.EventListener
import dev.lyzev.api.events.EventReload
import dev.lyzev.api.events.EventReloadShader
import dev.lyzev.api.events.EventWindowResize
import dev.lyzev.api.events.on
import dev.lyzev.schizoid.Schizoid
Expand All @@ -29,7 +29,7 @@ import java.io.FileNotFoundException
*/
abstract class Shader(val shader: String) : EventListener {

var program = glCreateProgram()
var program = 0
private val uniforms = HashMap<String, Int>()

/**
Expand Down Expand Up @@ -180,10 +180,19 @@ abstract class Shader(val shader: String) : EventListener {
}

open fun init() {
glLinkProgram(program)
if (program == 0)
program = glCreateProgram()
}

val isLinked = glGetProgrami(program, GL_LINK_STATUS)
open fun delete() {
if (program != 0)
glDeleteProgram(program)
program = 0
}

fun link() {
glLinkProgram(program)
val isLinked = glGetProgrami(program, GL_LINK_STATUS)
if (isLinked == 0)
Schizoid.logger.error(
glGetProgramInfoLog(
Expand All @@ -194,15 +203,14 @@ abstract class Shader(val shader: String) : EventListener {
}

fun reload() {
glDeleteProgram(program)
program = glCreateProgram()
delete()
init()
}

override val shouldHandleEvents = true

init {
on<EventReload> {
on<EventReloadShader> {
if (!Schizoid.DEVELOPER_MODE) return@on
reload()
}
Expand Down Expand Up @@ -235,6 +243,10 @@ abstract class ShaderVertexFragment(
) : Shader(shader) {

final override fun init() {
val tmp = program
super.init()
if (tmp == program)
return
val vSource = ClassLoader.getSystemResourceAsStream("$PATH/core/$shader/${shader}_VP.glsl")?.use {
it.readAllBytes().decodeToString()
} ?: "null"
Expand All @@ -249,7 +261,8 @@ abstract class ShaderVertexFragment(

glDeleteShader(vShader)
glDeleteShader(fShader)
super.init()

link()
}

init {
Expand Down Expand Up @@ -309,6 +322,10 @@ abstract class ShaderCompute(
}

override fun init() {
val tmp = program
super.init()
if (tmp == program)
return
ClassLoader.getSystemResourceAsStream("$PATH/core/$shader/${shader}_CP.glsl")
?.use {
val computeShader = compile(
Expand All @@ -319,19 +336,22 @@ abstract class ShaderCompute(
glDeleteShader(computeShader)
}

if (texture == 0) {
link()

if (texture == 0)
genTexture()
}
super.init()
}

final override fun delete() {
super.delete()
if (texture != 0)
glDeleteTextures(texture)
texture = 0
}

init {
on<EventWindowResize> {
// delete old texture and create a new one
if (texture != 0) {
glDeleteTextures(texture)
}
genTexture()
reload()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlin.reflect.KProperty
* @param unit The unit of the slider setting.
* @param allowOutOfBounds A flag to determine whether the slider setting allows out of bounds values.
* @param hide A lambda function that determines whether this setting is hidden or not.
* @param change A lambda function that will be called when the value of the setting changes.
* @param onChange A lambda function that will be called when the value of the setting changes.
*/
@Suppress("UNCHECKED_CAST")
class SettingClientSlider<T : Number>(
Expand All @@ -39,29 +39,31 @@ class SettingClientSlider<T : Number>(
private val decimalPlaces: Int,
private val unit: String?,
private val allowOutOfBounds: Boolean,
private val onlyUpdateOnRelease: Boolean,
hide: () -> Boolean,
change: (T) -> Unit
) : SettingClient<T>(container, name, desc, value, hide, change) {

/**
* The class of the value type.
*/
private val valueType: KClass<out Number> = value::class
onChange: (T) -> Unit
) : SettingClient<T>(container, name, desc, value, hide, onChange) {

override fun render() {
text(name)
if (desc != null && isItemHovered()) setTooltip(desc)
if (value is Int) {
i[0] = value as Int
setNextItemWidth(getColumnWidth())
if (sliderInt("", i, minValue as Int, maxValue as Int, "%d" + if (unit != null) " $unit" else "")) {
if (!onlyUpdateOnRelease) {
value = if (allowOutOfBounds)
i[0] as T
else
MathHelper.clamp(i[0], minValue.toInt(), maxValue.toInt()) as T
}
}
if (isItemDeactivatedAfterEdit() && onlyUpdateOnRelease) {
value = if (allowOutOfBounds)
i[0] as T
else
MathHelper.clamp(i[0], minValue.toInt(), maxValue.toInt()) as T
}
} else if (value is Float) {
f[0] = value as Float
setNextItemWidth(getColumnWidth())
if (sliderFloat(
"",
Expand All @@ -71,6 +73,14 @@ class SettingClientSlider<T : Number>(
"%.${decimalPlaces}f" + if (unit != null) " $unit" else ""
)
) {
if (!onlyUpdateOnRelease) {
value = if (allowOutOfBounds)
f[0] as T
else
MathHelper.clamp(f[0], minValue.toFloat(), maxValue.toFloat()) as T
}
}
if (isItemDeactivatedAfterEdit() && onlyUpdateOnRelease) {
value = if (allowOutOfBounds)
f[0] as T
else
Expand All @@ -82,33 +92,35 @@ class SettingClientSlider<T : Number>(
override fun setValue(ref: Any, prop: KProperty<*>, value: T) {
if (allowOutOfBounds)
super.setValue(ref, prop, value)
else if (valueType == Int::class)
else if (this.value is Int)
super.setValue(ref, prop, MathHelper.clamp(value.toInt(), minValue.toInt(), maxValue.toInt()) as T)
else if (valueType == Float::class)
else if (this.value is Float)
super.setValue(ref, prop, MathHelper.clamp(value.toFloat(), minValue.toFloat(), maxValue.toFloat()) as T)
if (this.value is Int)
i[0] = this.value as Int
else if (this.value is Float)
f[0] = this.value as Float
}

override fun load(value: JsonElement) {
this.value = when (valueType) {
Int::class -> value.jsonPrimitive.int.let { if (allowOutOfBounds) it else it.coerceIn(minValue.toInt(), maxValue.toInt()) } as T
Float::class -> value.jsonPrimitive.float.let { if (allowOutOfBounds) it else it.coerceIn(minValue.toFloat(), maxValue.toFloat()) } as T
this.value = when (this.value) {
is Int -> (value.jsonPrimitive.int.let { if (allowOutOfBounds) it else it.coerceIn(minValue.toInt(), maxValue.toInt()) } as T).apply { i[0] = this as Int }
is Float -> (value.jsonPrimitive.float.let { if (allowOutOfBounds) it else it.coerceIn(minValue.toFloat(), maxValue.toFloat()) } as T).apply { f[0] = this as Float }
else -> return
}
}

override fun save(): JsonElement = JsonPrimitive(value)

companion object {
/**
* An array to store the integer value of the slider.
*/
private val i = intArrayOf(0)
/**
* An array to store the integer value of the slider.
*/
private val i = intArrayOf(0)

/**
* An array to store the float value of the slider.
*/
private val f = floatArrayOf(0f)
}
/**
* An array to store the float value of the slider.
*/
private val f = floatArrayOf(0f)
}

/**
Expand All @@ -121,8 +133,9 @@ class SettingClientSlider<T : Number>(
* @param maxValue The maximum value of the slider setting.
* @param unit The unit of the slider setting.
* @param allowOutOfBounds A flag to determine whether the slider setting allows out of bounds values.
* @param onlyUpdateOnRelease A flag to determine whether the slider setting only updates on release.
* @param hide A lambda function that determines whether this setting is hidden or not.
* @param change A lambda function that will be called when the value of the setting changes.
* @param onChange A lambda function that will be called when the value of the setting changes.
*
* @return The created integer slider setting.
*/
Expand All @@ -134,9 +147,10 @@ fun IFeature.slider(
maxValue: Int,
unit: String? = null,
allowOutOfBounds: Boolean = false,
onlyUpdateOnRelease: Boolean = false,
hide: () -> Boolean = { false },
change: (Int) -> Unit = {}
) = SettingClientSlider(this::class, name, desc, value, minValue, maxValue, 0, unit, allowOutOfBounds, hide, change)
onChange: (Int) -> Unit = {}
) = SettingClientSlider(this::class, name, desc, value, minValue, maxValue, 0, unit, allowOutOfBounds, onlyUpdateOnRelease, hide, onChange)

/**
* Creates a new float slider setting.
Expand All @@ -149,8 +163,9 @@ fun IFeature.slider(
* @param decimalPlaces The number of decimal places for the slider setting.
* @param unit The unit of the slider setting.
* @param allowOutOfBounds A flag to determine whether the slider setting allows out of bounds values.
* @param onlyUpdateOnRelease A flag to determine whether the slider setting only updates on release.
* @param hide A lambda function that determines whether this setting is hidden or not.
* @param change A lambda function that will be called when the value of the setting changes.
* @param onChange A lambda function that will be called when the value of the setting changes.
*
* @return The created float slider setting.
*/
Expand All @@ -163,8 +178,9 @@ fun IFeature.slider(
decimalPlaces: Int = 1,
unit: String? = null,
allowOutOfBounds: Boolean = false,
onlyUpdateOnRelease: Boolean = false,
hide: () -> Boolean = { false },
change: (Float) -> Unit = {}
onChange: (Float) -> Unit = {}
) = SettingClientSlider(
this::class,
name,
Expand All @@ -175,6 +191,7 @@ fun IFeature.slider(
decimalPlaces,
unit,
allowOutOfBounds,
onlyUpdateOnRelease,
hide,
change
onChange
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ object ImGuiScreenFeature : ImGuiScreen("Feature Screen"), EventListener {
}

val particles: Boolean by switch("Particles", "Enables the particle shader.\nToggle to reload.", false) {
if (!it) return@switch
ShaderParticle.amount = particleAmount * 1000
ShaderParticle.reload()
if (it) ShaderParticle.init()
else ShaderParticle.delete()
}
val particleAmount by slider(
"Particle Amount",
"The amount of particles.",
100,
1,
1000,
999,
"k",
onlyUpdateOnRelease = true,
hide = ::particles neq true
)
) {
ShaderParticle.amount = it * 1_000
ShaderParticle.reload()
}

private val texturesMario = Array(3) {
Identifier(Schizoid.MOD_ID, "textures/mario_$it.png")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object ModuleToggleableWaterMark :
val waterMark by text("Water Mark", "The water mark to display.", "SCHIZOID", ::waterMarkMath eq true)
val showFPS by switch("Show FPS", "Shows the current FPS.", true)
val fpsBoost by switch("FPS Boost", "Boosts the FPS.", false)
val boost by slider("Boost", "The amount to boost the FPS by.", 1000, 0, 1000, "fps", true, ::fpsBoost neq true)
val boost by slider("Boost", "The amount to boost the FPS by.", 1000, 0, 1000, "fps", true, hide = ::fpsBoost neq true)

private val functions = Math::class.members.map { it.name }.toMutableList()
private var time = System.currentTimeMillis()
Expand Down
Loading