Model changes

- Buffered models directly consume IModels
 - Document IModel more
 - Move contraption world render spoofing to flywheel
 - Miscellaneous new RenderMaths
 - Added WorldModel, renders many blocks given a world instance
 - Fix broken transparency on contraptions when using Flywheel
This commit is contained in:
Jozufozu 2021-07-29 01:37:48 -07:00
parent 7c202ed491
commit e1248e598a
5 changed files with 15 additions and 123 deletions

View file

@ -16,7 +16,7 @@ cursegradle_version = 1.4.0
# dependency versions # dependency versions
registrate_version = 1.0.4 registrate_version = 1.0.4
flywheel_version = 1.16-0.2.0.32 flywheel_version = 1.16-0.2.0.33
jei_version = 7.7.1.110 jei_version = 7.7.1.110
# curseforge information # curseforge information

View file

@ -164,11 +164,5 @@ public class GlueInstance extends EntityInstance<SuperGlueEntity> implements ITi
public VertexFormat format() { public VertexFormat format() {
return Formats.UNLIT_MODEL; return Formats.UNLIT_MODEL;
} }
@Override
public ElementBuffer createEBO() {
return QuadConverter.getInstance()
.quads2Tris(2);
}
} }
} }

View file

@ -1,12 +1,11 @@
package com.simibubi.create.content.contraptions.components.structureMovement.render; package com.simibubi.create.content.contraptions.components.structureMovement.render;
import static org.lwjgl.opengl.GL11.GL_QUADS; import java.util.Collection;
import java.util.Random;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.core.model.ModelUtil;
import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.event.GatherContextEvent; import com.jozufozu.flywheel.event.GatherContextEvent;
import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent;
@ -24,36 +23,24 @@ import com.simibubi.create.foundation.render.SuperByteBuffer;
import com.simibubi.create.foundation.render.TileEntityRenderHelper; import com.simibubi.create.foundation.render.TileEntityRenderHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockModelRenderer;
import net.minecraft.client.renderer.BlockModelShapes;
import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.LightType; import net.minecraft.world.LightType;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.gen.feature.template.Template; import net.minecraft.world.gen.feature.template.Template;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.model.data.EmptyModelData;
import net.minecraftforge.common.util.Lazy;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
@Mod.EventBusSubscriber(Dist.CLIENT) @Mod.EventBusSubscriber(Dist.CLIENT)
public class ContraptionRenderDispatcher { public class ContraptionRenderDispatcher {
private static final Lazy<BlockModelRenderer> MODEL_RENDERER = Lazy.of(() -> new BlockModelRenderer(Minecraft.getInstance().getBlockColors()));
private static final Lazy<BlockModelShapes> BLOCK_MODELS = Lazy.of(() -> Minecraft.getInstance().getModelManager().getBlockModelShaper());
private static WorldAttached<ContraptionRenderManager<?>> WORLDS = new WorldAttached<>(SBBContraptionManager::new); private static WorldAttached<ContraptionRenderManager<?>> WORLDS = new WorldAttached<>(SBBContraptionManager::new);
@ -147,42 +134,12 @@ public class ContraptionRenderDispatcher {
} }
public static SuperByteBuffer buildStructureBuffer(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) { public static SuperByteBuffer buildStructureBuffer(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
BufferBuilder builder = buildStructure(renderWorld, c, layer); Collection<Template.BlockInfo> values = c.getBlocks()
.values();
BufferBuilder builder = ModelUtil.getBufferBuilderFromTemplate(renderWorld, layer, values);
return new SuperByteBuffer(builder); return new SuperByteBuffer(builder);
} }
public static BufferBuilder buildStructure(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
MatrixStack ms = new MatrixStack();
Random random = new Random();
BufferBuilder builder = new BufferBuilder(DefaultVertexFormats.BLOCK.getIntegerSize());
builder.begin(GL_QUADS, DefaultVertexFormats.BLOCK);
ForgeHooksClient.setRenderLayer(layer);
BlockModelRenderer.enableCaching();
for (Template.BlockInfo info : c.getBlocks()
.values()) {
BlockState state = info.state;
if (state.getRenderShape() != BlockRenderType.MODEL)
continue;
if (!RenderTypeLookup.canRenderInLayer(state, layer))
continue;
BlockPos pos = info.pos;
ms.pushPose();
ms.translate(pos.getX(), pos.getY(), pos.getZ());
MODEL_RENDERER.get().renderModel(renderWorld, BLOCK_MODELS.get().getBlockModel(state), state, pos, ms, builder, true,
random, 42, OverlayTexture.NO_OVERLAY, EmptyModelData.INSTANCE);
ms.popPose();
}
BlockModelRenderer.clearCache();
ForgeHooksClient.setRenderLayer(null);
builder.end();
return builder;
}
public static int getLight(World world, float lx, float ly, float lz) { public static int getLight(World world, float lx, float ly, float lz) {
BlockPos.Mutable pos = new BlockPos.Mutable(); BlockPos.Mutable pos = new BlockPos.Mutable();
float block = 0, sky = 0; float block = 0, sky = 0;

View file

@ -1,26 +1,20 @@
package com.simibubi.create.content.contraptions.components.structureMovement.render; package com.simibubi.create.content.contraptions.components.structureMovement.render;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.attrib.CommonAttributes;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry;
import com.jozufozu.flywheel.backend.material.MaterialManager; import com.jozufozu.flywheel.backend.material.MaterialManager;
import com.jozufozu.flywheel.backend.model.ArrayModelRenderer; import com.jozufozu.flywheel.backend.model.ArrayModelRenderer;
import com.jozufozu.flywheel.backend.model.BufferedModel;
import com.jozufozu.flywheel.backend.model.IndexedModel;
import com.jozufozu.flywheel.backend.model.ModelRenderer; import com.jozufozu.flywheel.backend.model.ModelRenderer;
import com.jozufozu.flywheel.core.model.IModel;
import com.jozufozu.flywheel.core.model.WorldModel;
import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.light.GridAlignedBB; import com.jozufozu.flywheel.light.GridAlignedBB;
import com.jozufozu.flywheel.util.BufferBuilderReader;
import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity;
import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption;
import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionLighter; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionLighter;
@ -28,7 +22,6 @@ import com.simibubi.create.foundation.render.CreateContexts;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
@ -39,13 +32,6 @@ import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World; import net.minecraft.world.World;
public class RenderedContraption extends ContraptionRenderInfo { public class RenderedContraption extends ContraptionRenderInfo {
public static final VertexFormat FORMAT = VertexFormat.builder()
.addAttributes(CommonAttributes.VEC3,
CommonAttributes.NORMAL,
CommonAttributes.UV,
CommonAttributes.RGBA,
CommonAttributes.LIGHT)
.build();
private final ContraptionLighter<?> lighter; private final ContraptionLighter<?> lighter;
@ -137,14 +123,12 @@ public class RenderedContraption extends ContraptionRenderInfo {
List<RenderType> blockLayers = RenderType.chunkBufferLayers(); List<RenderType> blockLayers = RenderType.chunkBufferLayers();
for (RenderType layer : blockLayers) { for (RenderType layer : blockLayers) {
BufferedModel layerModel = buildStructureModel(renderWorld, contraption, layer); Supplier<IModel> layerModel = () -> new WorldModel(renderWorld, layer, contraption.getBlocks().values());
if (layerModel != null) { if (Backend.getInstance().compat.vertexArrayObjectsSupported())
if (Backend.getInstance().compat.vertexArrayObjectsSupported()) renderLayers.put(layer, new ArrayModelRenderer(layerModel));
renderLayers.put(layer, new ArrayModelRenderer(layerModel)); else
else renderLayers.put(layer, new ModelRenderer(layerModel));
renderLayers.put(layer, new ModelRenderer(layerModel));
}
} }
} }
@ -167,47 +151,4 @@ public class RenderedContraption extends ContraptionRenderInfo {
private void buildActors() { private void buildActors() {
contraption.getActors().forEach(kinetics::createActor); contraption.getActors().forEach(kinetics::createActor);
} }
@Nullable
private static BufferedModel buildStructureModel(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
BufferBuilderReader reader = new BufferBuilderReader(ContraptionRenderDispatcher.buildStructure(renderWorld, c, layer));
int vertexCount = reader.getVertexCount();
if (vertexCount == 0) return null;
VertexFormat format = FORMAT;
ByteBuffer vertices = ByteBuffer.allocate(format.getStride() * vertexCount);
vertices.order(ByteOrder.nativeOrder());
for (int i = 0; i < vertexCount; i++) {
vertices.putFloat(reader.getX(i));
vertices.putFloat(reader.getY(i));
vertices.putFloat(reader.getZ(i));
vertices.put(reader.getNX(i));
vertices.put(reader.getNY(i));
vertices.put(reader.getNZ(i));
vertices.putFloat(reader.getU(i));
vertices.putFloat(reader.getV(i));
vertices.put(reader.getR(i));
vertices.put(reader.getG(i));
vertices.put(reader.getB(i));
vertices.put(reader.getA(i));
int light = reader.getLight(i);
byte block = (byte) (LightTexture.block(light) << 4);
byte sky = (byte) (LightTexture.sky(light) << 4);
vertices.put(block);
vertices.put(sky);
}
vertices.rewind();
return IndexedModel.fromSequentialQuads(format, vertices, vertexCount);
}
} }

View file

@ -30,7 +30,7 @@ BlockFrag FLWMain(Vertex v) {
#if defined(DEBUG_NORMAL) #if defined(DEBUG_NORMAL)
b.color = vec4(norm, 1.); b.color = vec4(norm, 1.);
#else #else
b.color = v.color / diffuse(v.normal); b.color = vec4(v.color.rgb / diffuse(v.normal), v.color.a);
#endif #endif
return b; return b;