Skip to content
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 @@ -4,6 +4,7 @@
import foundry.veil.api.flare.modifier.PropertyModifier;
import gg.moonflower.molangcompiler.api.MolangExpression;
import net.minecraft.client.renderer.ShaderInstance;
import org.jetbrains.annotations.Contract;

import java.util.List;
import java.util.Optional;
Expand All @@ -21,16 +22,24 @@ public abstract class InvertibleProperty<T> extends Property<T> {

public InvertibleProperty(PropertyRegistry.PropertyType<T, ?> type, T value) {
super(type, value);
this.inverseValue = this.calculateInverse(this.cloneValue(value));
this.overrideInverseValue = this.cloneValue(value);
this.inverseValue = this.invertAndMutate(this.cloneValue(value));
this.overrideInverseValue = this.cloneValue(inverseValue);
}

protected abstract T calculateInverse(T value);
@SuppressWarnings("UnstableApiUsage")
@Contract(mutates = "param")
protected abstract T invertAndMutate(T value);

@Override
public final void modify(T value, PropertyModifier.PropertyModifierMode mode, Optional<List<MolangExpression>> optionalMolang) {
this.modifyPreInvert(value, mode, optionalMolang);
this.overrideInverseValue = this.calculateInverse(value);

T originalOverrideValue = this.overrideValue;
this.overrideValue = overrideInverseValue;
this.modifyPreInvert(originalOverrideValue, PropertyModifier.PropertyModifierMode.REPLACE, Optional.empty());
this.overrideValue = originalOverrideValue;

this.invertAndMutate(this.overrideInverseValue);
}

public abstract void modifyPreInvert(T value, PropertyModifier.PropertyModifierMode mode, Optional<List<MolangExpression>> optionalMolang);
Expand All @@ -44,7 +53,10 @@ public void applyInverseValue(String name, ShaderInstance shader) {

@Override
public void resetOverrideValue() {
super.resetOverrideValue();
this.overrideInverseValue = this.inverseValue;
this.modifyPreInvert(this.value, PropertyModifier.PropertyModifierMode.REPLACE, Optional.empty());
T originalOverrideValue = this.overrideValue;
this.overrideValue = overrideInverseValue;
this.modifyPreInvert(this.inverseValue, PropertyModifier.PropertyModifierMode.REPLACE, Optional.empty());
this.overrideValue = originalOverrideValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void setQueries(MolangRuntime.Builder builder) {
protected abstract T cloneValue(T value);

public void resetOverrideValue() {
this.overrideValue = this.cloneValue(this.value);
this.modify(this.value, PropertyModifier.PropertyModifierMode.REPLACE, Optional.empty());
}

public PropertyRegistry.PropertyType<T, ? extends Property<T>> getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
@InapplicableProperty
public class RotationModelProperty extends Vec3ModelProperty {
private final Quaternionfc rotation;
private final Quaternionf overrideRotation = new Quaternionf();
private final Quaternionf overrideRotation;

public RotationModelProperty(Vector3f value) {
super(value);
rotation = new Quaternionf().rotationXYZ(value.x() * Mth.DEG_TO_RAD, value.y() * Mth.DEG_TO_RAD, value.z() * Mth.DEG_TO_RAD);
this.rotation = new Quaternionf().rotationXYZ(value.x() * Mth.DEG_TO_RAD, value.y() * Mth.DEG_TO_RAD, value.z() * Mth.DEG_TO_RAD);
this.overrideRotation = new Quaternionf(this.rotation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gg.moonflower.molangcompiler.api.MolangExpression;
import gg.moonflower.molangcompiler.api.MolangRuntime;
import net.minecraft.client.renderer.ShaderInstance;
import org.jetbrains.annotations.Contract;
import org.joml.Matrix3f;
import org.joml.Matrix3fc;

Expand Down Expand Up @@ -46,9 +47,11 @@ public void modifyPreInvert(Matrix3f value, PropertyModifier.PropertyModifierMod
protected Matrix3f cloneValue(Matrix3f value) {
return new Matrix3f(value);
}


@SuppressWarnings("UnstableApiUsage")
@Contract(mutates = "param")
@Override
protected Matrix3f calculateInverse(Matrix3f value) {
protected Matrix3f invertAndMutate(Matrix3f value) {
return value.invert();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gg.moonflower.molangcompiler.api.MolangExpression;
import gg.moonflower.molangcompiler.api.MolangRuntime;
import net.minecraft.client.renderer.ShaderInstance;
import org.jetbrains.annotations.Contract;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;

Expand Down Expand Up @@ -46,9 +47,11 @@ public void modifyPreInvert(Matrix4f value, PropertyModifier.PropertyModifierMod
protected Matrix4f cloneValue(Matrix4f value) {
return new Matrix4f(value);
}


@SuppressWarnings("UnstableApiUsage")
@Contract(mutates = "param")
@Override
protected Matrix4f calculateInverse(Matrix4f value) {
protected Matrix4f invertAndMutate(Matrix4f value) {
return value.invert();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public class Sampler2DProperty extends Property<AbstractTexture> {
public static final MapCodec<Sampler2DProperty> CODEC = ResourceLocation.CODEC.fieldOf("value").xmap(Sampler2DProperty::new, property -> property.source);

public Sampler2DProperty(ResourceLocation value) {
super(PropertyRegistry.SAMPLER2D.get(), Minecraft.getInstance().getTextureManager().getTexture(value));
super(PropertyRegistry.SAMPLER2D.get(), null);
this.source = value;
}

@Override
public void applyValue(String name, ShaderInstance shader) {
shader.setSampler(name, this.overrideValue.getId());
shader.setSampler(name, Minecraft.getInstance().getTextureManager().getTexture(source).getId());
// Shader must be re-applied for the sampler to take effect
shader.apply();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private TimeProperty() {
public void applyValue(String name, ShaderInstance shader) {
Uniform uniform = shader.getUniform(name);
if (uniform != null) {
float time = Minecraft.getInstance().getFrameTimeNs() * 1e-9f;
uniform.set(this.value.set(time / 20.0f, time, 2.0f * time, Mth.sin(time)));
float time = System.nanoTime() * 1e-9f;
uniform.set(this.value.set(time / 20.0f, time, 2.0f * time, Mth.sin(time % Mth.TWO_PI)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ private LayeredRenderType(RenderType defaultValue, List<RenderType> layers, Stri
public void draw(@NotNull MeshData meshData) {
super.draw(meshData);
if (BufferUploader.lastImmediateBuffer != null) {
ShaderInstance shader = RenderSystem.getShader();
if (shader == null) {
return;
}

Matrix4f modelViewMatrix = RenderSystem.getModelViewMatrix();
Matrix4f projectionMatrix = RenderSystem.getProjectionMatrix();
for (RenderType layer : this.layers) {
layer.setupRenderState();
ShaderInstance shader = RenderSystem.getShader();
if (shader == null) {
return;
}
BufferUploader.lastImmediateBuffer.drawWithShader(modelViewMatrix, projectionMatrix, shader);
layer.clearRenderState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public void render(EffectHost host, MatrixStack matrixStack, float partialTick,
}

ControllerManager controllerManager = FlareEffectManager.getInstance().getControllerManager();
for (PropertyModifier<?> modifier : this.originalModifiers) {

List<PropertyModifier<?>> size = this.originalModifiers;
for (int i = 0, propertyModifiersSize = size.size(); i < propertyModifiersSize; i++) {
PropertyModifier<?> modifier = size.get(i);
controllerManager.getOrCreateController(modifier.inputControllerName(), host).update(partialTick);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public FlareEffectTemplate(List<FlareEffectLayer> effectLayers) {
}

public void render(EffectHost host, MatrixStack matrixStack, float partialTick, @Nullable Map<ResourceLocation, BakedShell> shellOverrides) {
for (FlareEffectLayer effectLayer : this.effectLayers) {
List<FlareEffectLayer> layers = this.effectLayers;
for (int i = 0, size = layers.size(); i < size; i++) {
FlareEffectLayer effectLayer = layers.get(i);
effectLayer.render(host, matrixStack, partialTick, shellOverrides);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import foundry.veil.api.flare.modifier.PropertyModifier;
import foundry.veil.api.util.CodecUtil;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -40,7 +41,8 @@ public final class FlareMaterial {
private final String clazz;
private final ResourceLocation renderTypeLocation;
private final boolean randomizeSeed;
private final Map<String, Property<?>> properties;
private final Object2ObjectArrayMap<String, Property<?>> properties;
private final List<Object2ObjectMap.Entry<String, Property<?>>> propertyEntries;


public FlareMaterial(String clazz, ResourceLocation renderTypeLocation, boolean randomizeSeed, Map<String, Property<?>> properties) {
Expand All @@ -51,14 +53,17 @@ public FlareMaterial(String clazz, ResourceLocation renderTypeLocation, boolean
this.clazz = clazz;
this.renderTypeLocation = renderTypeLocation;
this.randomizeSeed = randomizeSeed;
this.properties = Map.copyOf(properties);
this.properties = new Object2ObjectArrayMap<>(properties);
this.propertyEntries = this.properties.object2ObjectEntrySet().stream().toList();
}

public void applyProperties(EffectHost host, @Nullable ShaderInstance shader, Map<String, List<PropertyModifier<?>>> modifiers) {
if (shader == null) {
return;
}
for (Map.Entry<String, Property<?>> entry : this.properties.entrySet()) {
List<Object2ObjectMap.Entry<String, Property<?>>> entries = this.propertyEntries;
for (int i = 0, size = entries.size(); i < size; i++) {
Map.Entry<String, Property<?>> entry = entries.get(i);
Property<?> property = entry.getValue();
Class<?> propertyClass = property.getClass();

Expand All @@ -82,7 +87,10 @@ public void resetProperties(EffectHost host, @Nullable ShaderInstance shader) {
if (shader == null) {
return;
}
for (Property<?> property : this.properties.values()) {
List<Object2ObjectMap.Entry<String, Property<?>>> entries = this.propertyEntries;
for (int i = 0, size = entries.size(); i < size; i++) {
Object2ObjectMap.Entry<String, Property<?>> entry = entries.get(i);
Property<?> property = entry.getValue();
if (property.getClass().getAnnotation(ModelProperty.class) != null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Matrix4fStack;
import org.joml.Vector3f;
import org.joml.Vector3fc;
import org.joml.*;

import java.util.List;
import java.util.Map;
Expand All @@ -41,8 +38,9 @@ public class FlareModel {
CodecUtil.VECTOR3FC_CODEC.fieldOf("scaleOffset").forGetter(FlareModel::getScaleOffset),
CodecUtil.singleOrList(FlareMaterial.CODEC).fieldOf("materials").forGetter(FlareModel::getMaterials)
).apply(instance, FlareModel::new));

public static final Matrix4f dummyMatrix = new Matrix4f();

public static final Matrix4f MODEL_TO_LOCAL_MATRIX = new Matrix4f();
public static final Matrix4f MATRIX4F = new Matrix4f();

public static final String POSITION_PROPERTY_NAME = "model::position";
public static final String ROTATION_PROPERTY_NAME = "model::rotation";
Expand All @@ -66,43 +64,47 @@ public FlareModel(ResourceLocation shell, Vector3fc position, Vector3fc rotation

public void render(EffectHost host, MatrixStack matrixStack, Map<String, List<PropertyModifier<?>>> modifiers, @Nullable Map<ResourceLocation, BakedShell> shellOverrides) {
Vector3fc positionOffset = this.positionOffset.getValue();
Quaternionfc rotationOffset = this.rotationOffset.getRotation();
Vector3fc scaleOffset = this.scaleOffset.getValue();

matrixStack.matrixPush();
matrixStack.translate(positionOffset.x(), positionOffset.y(), positionOffset.z());
matrixStack.rotate(this.rotationOffset.getRotation());
matrixStack.applyScale(scaleOffset.x(), scaleOffset.y(), scaleOffset.z());

MODEL_TO_LOCAL_MATRIX.set(matrixStack.position());
MODEL_TO_LOCAL_MATRIX.translate(positionOffset.x(), positionOffset.y(), positionOffset.z());
MODEL_TO_LOCAL_MATRIX.rotate(rotationOffset);
MODEL_TO_LOCAL_MATRIX.scale(scaleOffset.x(), scaleOffset.y(), scaleOffset.z());

BakedShell bakedShell = (shellOverrides != null && shellOverrides.containsKey(this.shell)) ?
shellOverrides.get(this.shell) :
FlareEffectManager.getInstance().getBakedShell(this.shell);

Vec3 cameraPos = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();

this.modelToWorld.modify(
matrixStack.position().translateLocal((float) cameraPos.x, (float) cameraPos.y, (float) cameraPos.z, dummyMatrix),
MODEL_TO_LOCAL_MATRIX.translate((float) cameraPos.x, (float) cameraPos.y, (float) cameraPos.z, MATRIX4F),
PropertyModifier.PropertyModifierMode.REPLACE,
Optional.empty()
);

BakedShell bakedShell = (shellOverrides != null && shellOverrides.containsKey(this.shell)) ?
shellOverrides.get(this.shell) :
FlareEffectManager.getInstance().getBakedShell(this.shell);

Matrix4fStack stack = RenderSystem.getModelViewStack();
stack.pushMatrix();
stack.mul(matrixStack.position());
Matrix4fStack modelViewStack = RenderSystem.getModelViewStack();
MATRIX4F.set(modelViewStack);
modelViewStack.mul(MODEL_TO_LOCAL_MATRIX);
RenderSystem.applyModelViewMatrix();

VertexArray vertexArray = bakedShell.getVertexArray();
vertexArray.bind();
for (FlareMaterial material : this.materials) {

List<FlareMaterial> flareMaterials = this.materials;
for (int i = 0, size = flareMaterials.size(); i < size; i++) {
FlareMaterial material = flareMaterials.get(i);
RenderType renderType = VeilRenderType.get(material.renderTypeLocation());
if (renderType == null) {
continue;
}

this.draw(renderType, vertexArray, host, material, modifiers);
}
VertexArray.unbind();
matrixStack.matrixPop();

stack.popMatrix();

modelViewStack.set(MATRIX4F);
RenderSystem.applyModelViewMatrix();
}

Expand All @@ -123,7 +125,9 @@ private void draw(RenderType renderType, VertexArray vertexArray, EffectHost hos
vertexArray.clear(renderType);

if (renderType instanceof VeilRenderType.LayeredRenderType layeredRenderType) {
for (RenderType layer : layeredRenderType.getLayers()) {
List<RenderType> layers = layeredRenderType.getLayers();
for (int i = 0, size = layers.size(); i < size; i++) {
RenderType layer = layers.get(i);
vertexArray.setup(layer);
currentShader = RenderSystem.getShader();
material.applyProperties(host, currentShader, modifiers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public void render(EffectHost host, MatrixStack matrixStack, float partialTick)
}

public void render(EffectHost host, MatrixStack matrixStack, float partialTick, @Nullable Map<ResourceLocation, BakedShell> shellOverrides) {
for (ResourceLocation templateLocation : this.templates) {
List<ResourceLocation> resourceLocations = this.templates;
for (int i = 0, size = resourceLocations.size(); i < size; i++) {
ResourceLocation templateLocation = resourceLocations.get(i);
FlareEffectTemplate template = FlareEffectManager.getTemplate(templateLocation);
if (template == null) {
LOGGER.error("Template {} could not be found!", templateLocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static FlareBakedQuad bakeQuad(
}

private static float[] makeVertices(ShellFaceUV uvs, Vector3f normal, Direction direction, float[] posDiv16, @Nullable ShellElementRotation rotation) {
float[] vertexData = new float[20];
float[] vertexData = new float[32];
Matrix4f rotationMatrix = null;
if (rotation != null) {
float angle = rotation.angle() * Mth.DEG_TO_RAD;
Expand Down Expand Up @@ -103,12 +103,12 @@ private static float[] setupShape(Vector3fc min, Vector3fc max) {
}

private static void fillVertex(float[] vertexData, int vertexIndex, Vector3f pos, Vector3f normal, ShellFaceUV shellFaceUV) {
int i = vertexIndex * 5;
int i = vertexIndex * 8;
vertexData[i] = pos.x();
vertexData[i + 1] = pos.y();
vertexData[i + 2] = pos.z();
vertexData[i + 3] = normal.z();
vertexData[i + 4] = normal.z();
vertexData[i + 3] = normal.x();
vertexData[i + 4] = normal.y();
vertexData[i + 5] = normal.z();
vertexData[i + 6] = shellFaceUV.getU(vertexIndex) / 16.0F;
vertexData[i + 7] = shellFaceUV.getV(vertexIndex) / 16.0F;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static void modifyProperty(EffectHost host, @Nullable String clazz, Prope
return;
}
for (PropertyModifier<?> modifier : modifiers) {
if (clazz != null && !Objects.equals(clazz, modifier.clazz)) {
if (clazz != null && modifier.clazz != null && !modifier.clazz.equals(clazz)) {
continue;
}
modifier.apply(host, property);
Expand Down
Loading