Skip to content

Commit

Permalink
Further refinement including isolating rendering code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Waterpicker committed Sep 25, 2024
1 parent 2b4a0f9 commit b9555f0
Show file tree
Hide file tree
Showing 18 changed files with 306 additions and 353 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package gg.generations.rarecandy.pokeutils;

import gg.generations.rarecandy.renderer.animation.Animation;
import gg.generations.rarecandy.renderer.model.material.Material;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class ModelConfig {
public float scale = 1.0f;
public Map<String, MaterialReference> materials;

public Map<String, VariantDetails> defaultVariant;
public Map<String, VariantParent> variants;
public Map<String, HideDuringAnimation> hideDuringAnimation;
public Map<String, HideDuringAnimation> hideDuringAnimation = Collections.emptyMap();

public Map<String, Integer> animationFpsOverride;

Expand Down Expand Up @@ -42,6 +40,20 @@ public List<String> getMaterialsForAnimation(String trackName) {
return list;
}

public Map<String, Material> prepMaterials(Map<String, String> images) {
var map = new HashMap<String, Material>();

for (Map.Entry<String, MaterialReference> entry : materials.entrySet()) {
String k = entry.getKey();
MaterialReference v = entry.getValue();
var material = MaterialReference.process(k, materials, images);

map.put(k, material);
}

return map;
}

public record HideDuringAnimation(boolean blackList, List<String> animations) {
public static final HideDuringAnimation NONE = new HideDuringAnimation();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package gg.generations.rarecandy.renderer.animation;

import gg.generations.rarecandy.assimp.AIBone;
import gg.generations.rarecandy.assimp.AIMesh;
import gg.generations.rarecandy.pokeutils.ModelNode;
import gg.generations.rarecandy.renderer.rendering.Bone;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;

public class Skeleton {
public final Map<String, ModelNode> jointMap;
Expand All @@ -25,11 +27,13 @@ public Skeleton(Skeleton skeleton) {
}


public Skeleton(ModelNode rootNode, Set<String> meshNamesToExclude) {
public Skeleton(ModelNode rootNode, AIMesh[] meshes, boolean excludeMeshes) {
this.rootNode = rootNode;
this.boneIdMap = new HashMap<>();
var jointList = new ArrayList<ModelNode>();
populateJoints(rootNode, jointList, meshNamesToExclude);

var meshNames = excludeMeshes ? Stream.of(meshes).map(a -> a.mName().dataString()).collect(Collectors.toSet()) : Set.<String>of();
populateJoints(rootNode, jointList, meshNames);

var boneCount = jointList.size();
this.bones = new Bone[boneCount];
Expand All @@ -41,6 +45,27 @@ public Skeleton(ModelNode rootNode, Set<String> meshNamesToExclude) {
bones[i] = boneMap.get(name);
boneIdMap.put(name, i);
}

for (AIMesh aiMesh : meshes) {
processBones(aiMesh);
}
}

private void processBones(AIMesh mesh) {

if (mesh.mBones() != null) {
var aiBones = requireNonNull(mesh.mBones());

for (int i = 0; i < aiBones.capacity(); i++) {
configure(AIBone.create(aiBones.get(i)));
}
}
}

public void configure(AIBone bone) {
var b = boneMap.get(bone.mName().dataString());
ModelNode.from(b.inverseBindMatrix, bone.mOffsetMatrix());
b.restPose.set(b.inverseBindMatrix).invert();
}

private void populateJoints(ModelNode joint, ArrayList<ModelNode> jointList, Set<String> meshNamesToExclude) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import gg.generations.rarecandy.pokeutils.ModelConfig;
import gg.generations.rarecandy.renderer.animation.Animation;
import gg.generations.rarecandy.renderer.animation.Skeleton;
import gg.generations.rarecandy.renderer.model.GLModel;
import gg.generations.rarecandy.renderer.model.RenderModel;
import gg.generations.rarecandy.renderer.model.Variant;
import gg.generations.rarecandy.renderer.rendering.ObjectInstance;
import gg.generations.rarecandy.renderer.storage.AnimatedObjectInstance;
Expand All @@ -12,18 +11,16 @@

public class AnimatedMeshObject extends MeshObject {
public Map<String, Animation> animations;
public Skeleton skeleton;
private ModelConfig.HideDuringAnimation hideDuringAnimation;

public void setup(Map<String, Variant> variants, GLModel model, String name, Skeleton skeleton, Map<String, Animation> animations, ModelConfig.HideDuringAnimation hideDuringAnimation) {
public void setup(Map<String, Variant> variants, RenderModel model, String name, Map<String, Animation> animations, ModelConfig.HideDuringAnimation hideDuringAnimation) {
setup(variants, model, name);
this.hideDuringAnimation = hideDuringAnimation;
this.animations = animations;
this.skeleton = skeleton;
}

@Override
protected boolean shouldRender(ObjectInstance instance) {
public boolean shouldRender(ObjectInstance instance) {
return super.shouldRender(instance) ||
(instance instanceof AnimatedObjectInstance animationInstance && animationInstance.currentAnimation != null && hideDuringAnimation.check(animationInstance.currentAnimation.getAnimation()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
public class GuiObject extends RenderObject {

@Override
protected <T extends RenderObject> void render(List<ObjectInstance> instances, T object) {
public <T extends RenderObject> void render(List<ObjectInstance> instances) {

}

@Override
protected <T extends RenderObject> void render(ObjectInstance instance, T object) {
public <T extends RenderObject> void render(ObjectInstance instance) {

}
}
Original file line number Diff line number Diff line change
@@ -1,90 +1,32 @@
package gg.generations.rarecandy.renderer.components;

import gg.generations.rarecandy.pokeutils.BlendType;
import gg.generations.rarecandy.renderer.model.GLModel;
import gg.generations.rarecandy.renderer.model.RenderModel;
import gg.generations.rarecandy.renderer.model.Variant;
import gg.generations.rarecandy.renderer.model.material.Material;
import gg.generations.rarecandy.renderer.pipeline.Pipeline;
import gg.generations.rarecandy.renderer.rendering.ObjectInstance;
import gg.generations.rarecandy.renderer.rendering.RenderStage;

import java.io.IOException;
import java.util.*;
import java.util.function.Consumer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MeshObject extends RenderObject {
private Map<Material, List<Consumer<Pipeline>>> EMPTY = Collections.emptyMap();
public GLModel model;
public RenderModel model;
public String name;

private static void render(Material k, List<Consumer<Pipeline>> v) {
var pl = k.getPipeline();

if(pl == null) return;

pl.bind(k);
v.forEach(a -> a.accept(pl));
pl.unbind(k);
}

public void setup(Map<String, Variant> variants, GLModel model, String name) {
public void setup(Map<String, Variant> variants, RenderModel model, String name) {
this.name = name;
this.variants = variants;
this.model = model;
this.defaultVariant = new ArrayList<>(variants.keySet()).get(0);
this.ready = true;
}

public <T extends RenderObject> void render(List<ObjectInstance> instances, T object) {
Map<RenderStage, Map<Material, List<Consumer<Pipeline>>>> map = new HashMap<>();

for (var instance : instances) {
if (object.shouldRender(instance)) {
continue;
}

var material = object.getMaterial(instance.variant());

var stage = RenderStage.SOLID;

if(material.blendType() != BlendType.None) stage = RenderStage.TRANSPARENT;

var stages = map.computeIfAbsent(stage, s -> new HashMap<>());

stages.computeIfAbsent(material, a -> new ArrayList<>()).add(pipeline -> {
pipeline.updateOtherUniforms(instance, object);
pipeline.updateTexUniforms(instance, object);
model.runDrawCalls();
});
}



map.getOrDefault(RenderStage.SOLID, EMPTY).forEach(MeshObject::render);
map.getOrDefault(RenderStage.TRANSPARENT, EMPTY).forEach(MeshObject::render);
public <T extends RenderObject> void render(List<ObjectInstance> instances) {
model.render(instances, this);
}

public <T extends RenderObject> void render(ObjectInstance instance, T object) {
Map<Material, List<Consumer<Pipeline>>> solidMap = new HashMap<>();
Map<Material, List<Consumer<Pipeline>>> transparentMap = new HashMap<>();

if (object.shouldRender(instance)) return;

var material = object.getMaterial(instance.variant());

var stage = RenderStage.SOLID;

if(material.blendType() != BlendType.None) stage = RenderStage.TRANSPARENT;
var stages = stage == RenderStage.SOLID ? solidMap : transparentMap;

stages.computeIfAbsent(material, a -> new ArrayList<>()).add(pipeline -> {
pipeline.updateOtherUniforms(instance, object);
pipeline.updateTexUniforms(instance, object);
model.runDrawCalls();
});

solidMap.forEach(MeshObject::render);
transparentMap.forEach(MeshObject::render);
public <T extends RenderObject> void render(ObjectInstance instance) {
model.render(instance, this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,27 @@ public Material getMaterial(@Nullable String materialId) {
}

@Override
public <V extends RenderObject> void render(List<ObjectInstance> instances, V obj) {
public <V extends RenderObject> void render(List<ObjectInstance> instances) {
for (T object : this.objects) {
if (object != null && object.isReady()) {
object.render(instances, object);
object.render(instances);
}
}
}

@Override
public <V extends RenderObject> void render(ObjectInstance instance, V obj) {
public <V extends RenderObject> void render(ObjectInstance instance) {
for (T object : this.objects) {
if (object != null && object.isReady()) {
object.render(instance, object);
object.render(instance);
}
}
}

public void updateDimensions() {
for (RenderObject object : objects) {
if (object instanceof MeshObject mesh) {
dimensions.max(mesh.model.dimensions);
dimensions.max(mesh.model.getDimensions());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import gg.generations.rarecandy.renderer.model.material.Material;
import gg.generations.rarecandy.renderer.rendering.ObjectInstance;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -20,16 +19,6 @@ public abstract class RenderObject implements Closeable {
protected String defaultVariant = null;

protected boolean ready = false;
protected Matrix4f matrixOffset = new Matrix4f().identity();

public void render(List<ObjectInstance> instances) {
render(instances, this);
}

public void render(ObjectInstance instance) {
render(instance, this);
}


public void update() {
}
Expand All @@ -38,14 +27,6 @@ public boolean isReady() {
return ready;
}

public void setMatrixOffset(Matrix4f mat4f) {
matrixOffset.set(mat4f);
}

public void applyTransformOffset(Matrix4f currentTransform) {
currentTransform.mul(matrixOffset);
}

public Set<String> availableVariants() {
return variants.keySet();
}
Expand All @@ -61,11 +42,11 @@ public Transform getTransform(@Nullable String variantId) {
return variant != null && variant.offset() != null ? variant.offset() : AnimationController.NO_OFFSET;
}

protected abstract <T extends RenderObject> void render(List<ObjectInstance> instances, T object);
public abstract <T extends RenderObject> void render(List<ObjectInstance> instances);

protected abstract <T extends RenderObject> void render(ObjectInstance instance, T object);
public abstract <T extends RenderObject> void render(ObjectInstance instance);

protected boolean shouldRender(ObjectInstance instance) {
public boolean shouldRender(ObjectInstance instance) {
var variant = getVariant(instance);
return variant == null || variant.hide();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gg.generations.rarecandy.renderer.components;

public interface Renderer {

public static class InstancedRenderer {
public void render() {}
}
}
Loading

0 comments on commit b9555f0

Please sign in to comment.