Skip to content

Commit

Permalink
Update to compatible with MCglTF-1.2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLee9024 committed Aug 7, 2022
1 parent 6af1067 commit cd45332
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 265 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0#gltf-20-sampl

In order to adapt into BSL Shaders' SEUS/Old PBR format, some change were made:
- All normal textures had been converted from OpenGL format (Y+) to DirectX format (Y-) by flipping green channel.
- `Occlusion(R)Roughness(G)Metallic(B)` textures and `Emissive color(RGB)` textures had been edited and combined into `Glossiness(R) Metallic(G)Emissive strength(B)` textures for specular map.
- `Occlusion(R)Roughness(G)Metallic(B)` textures and `Emissive color(RGB)` textures had been edited and combined into `Glossiness(R)Metallic(G)Emissive strength(B)` textures for specular map.
## Additonal Note About Setup This Project
1. Build MCglTF with "gradlew jar" to create a [deobuscated version](https://forums.minecraftforge.net/topic/81617-1152-eclipse-and-gradle-how-to-use-jar-from-another-project-and-import-solved) of MCglTF.
2. Create a folder named `libs` in the same dir level as `src`.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.16.5-Forge-1.1.0.1'
version = '1.16.5-Forge-1.2.0.0'
group = 'com.timlee9024.mcgltf.example' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'MCglTF-Example'

Expand Down

This file was deleted.

138 changes: 14 additions & 124 deletions src/main/java/com/timlee9024/mcgltf/example/Example.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package com.timlee9024.mcgltf.example;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.timlee9024.mcgltf.MCglTF;

import de.javagl.jgltf.model.animation.Animation;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.model.ItemCameraTransforms.TransformType;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.client.renderer.tileentity.ItemStackTileEntityRenderer;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.EntityType;
Expand Down Expand Up @@ -68,7 +60,7 @@ public static void onTileEntityTypeRegistryEvent(final RegistryEvent.Register<Ti
EXAMPLE_TILE_ENTITY_TYPE.setRegistryName(new ResourceLocation("mcgltf", "example_tileentity"));
event.getRegistry().register(EXAMPLE_TILE_ENTITY_TYPE);
}

@SubscribeEvent
public static void onEntityTypeRegistryEvent(final RegistryEvent.Register<EntityType<?>> event) {
EXAMPLE_ENTITY_TYPE = EntityType.Builder.of(ExampleEntity::new, EntityClassification.MISC)
Expand All @@ -86,7 +78,7 @@ public static void onItemRegistryEvent(final RegistryEvent.Register<Item> event)
event.getRegistry().register(item);

BlockItem blockItem = new BlockItem(EXAMPLE_BLOCK, new Item.Properties().tab(ItemGroup.TAB_MISC));
item.setRegistryName(EXAMPLE_BLOCK.getRegistryName());
blockItem.setRegistryName(EXAMPLE_BLOCK.getRegistryName());
event.getRegistry().register(blockItem);

ForgeSpawnEggItem spawnEggItem = new ForgeSpawnEggItem(() -> EXAMPLE_ENTITY_TYPE, 12422002, 5651507, new Item.Properties().tab(ItemGroup.TAB_MISC));
Expand All @@ -105,7 +97,7 @@ public static class Client {

private static Item item;
private static BlockItem blockItem;

@SubscribeEvent
public static void onBlockRegistryEvent(final RegistryEvent.Register<Block> event) {
EXAMPLE_BLOCK = new ExampleBlock(AbstractBlock.Properties.of(Material.STONE).strength(0.3F)
Expand All @@ -125,7 +117,7 @@ public static void onTileEntityTypeRegistryEvent(final RegistryEvent.Register<Ti
EXAMPLE_TILE_ENTITY_TYPE.setRegistryName(new ResourceLocation("mcgltf", "example_tileentity"));
event.getRegistry().register(EXAMPLE_TILE_ENTITY_TYPE);
}

@SubscribeEvent
public static void onEntityTypeRegistryEvent(final RegistryEvent.Register<EntityType<?>> event) {
EXAMPLE_ENTITY_TYPE = EntityType.Builder.of(ExampleEntity::new, EntityClassification.MISC)
Expand All @@ -138,139 +130,37 @@ public static void onEntityTypeRegistryEvent(final RegistryEvent.Register<Entity

@SubscribeEvent
public static void onItemRegistryEvent(final RegistryEvent.Register<Item> event) {
AbstractItemGltfModelReceiver itemModelReceiver = new AbstractItemGltfModelReceiver() {
ExampleItemRenderer itemRenderer = new ExampleItemRenderer() {

@Override
public ResourceLocation getModelLocation() {
return new ResourceLocation("mcgltf", "models/item/water_bottle.gltf");
}
};
MCglTF.getInstance().addGltfModelReceiver(itemModelReceiver);
MCglTF.getInstance().addGltfModelReceiver(itemRenderer);

AbstractItemGltfModelReceiver blockItemModelReceiver = new AbstractItemGltfModelReceiver() {
ExampleItemRenderer blockItemRenderer = new ExampleItemRenderer() {

@Override
public ResourceLocation getModelLocation() {
return new ResourceLocation("mcgltf", "models/block/boom_box.gltf");
}
};
MCglTF.getInstance().addGltfModelReceiver(blockItemModelReceiver);
MCglTF.getInstance().addGltfModelReceiver(blockItemRenderer);

//According to Forge Doc "Each mod should only have one instance of a custom TEISR/ISTER/BEWLR.", due to creating an instance will also initiate unused fields inside the class which waste a lots of memory.
ItemStackTileEntityRenderer ister = new ItemStackTileEntityRenderer() {

@Override
public void renderByItem(ItemStack p_239207_1_, TransformType p_239207_2_, MatrixStack p_239207_3_, IRenderTypeBuffer p_239207_4_, int p_239207_5_, int p_239207_6_) {
GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);

GL11.glEnable(GL11.GL_LIGHTING);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glEnable(GL11.GL_COLOR_MATERIAL);

RenderSystem.multMatrix(p_239207_3_.last().pose());

switch(p_239207_2_) {
case THIRD_PERSON_LEFT_HAND:
case THIRD_PERSON_RIGHT_HAND:
case HEAD:
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

GL13.glMultiTexCoord2s(GL13.GL_TEXTURE1, (short)(p_239207_6_ & '\uffff'), (short)(p_239207_6_ >> 16 & '\uffff'));
GL13.glMultiTexCoord2s(GL13.GL_TEXTURE2, (short)(p_239207_5_ & '\uffff'), (short)(p_239207_5_ >> 16 & '\uffff'));

MCglTF.CURRENT_PROGRAM = GL11.glGetInteger(GL20.GL_CURRENT_PROGRAM);
if(MCglTF.CURRENT_PROGRAM == 0) {
GL13.glActiveTexture(GL13.GL_TEXTURE1);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL13.glActiveTexture(GL13.GL_TEXTURE2);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL13.glActiveTexture(GL13.GL_TEXTURE0);

renderWithVanillaCommands(p_239207_1_);
}
else {
renderWithShaderModCommands(p_239207_1_);
}
break;
case FIRST_PERSON_LEFT_HAND:
case FIRST_PERSON_RIGHT_HAND:
case GROUND:
case FIXED:
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

GL13.glMultiTexCoord2s(GL13.GL_TEXTURE2, (short)(p_239207_5_ & '\uffff'), (short)(p_239207_5_ >> 16 & '\uffff'));

MCglTF.CURRENT_PROGRAM = GL11.glGetInteger(GL20.GL_CURRENT_PROGRAM);
if(MCglTF.CURRENT_PROGRAM == 0) {
GL13.glActiveTexture(GL13.GL_TEXTURE2);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL13.glActiveTexture(GL13.GL_TEXTURE0);

renderWithVanillaCommands(p_239207_1_);
}
else {
renderWithShaderModCommands(p_239207_1_);
}
break;
default:
MCglTF.CURRENT_PROGRAM = GL11.glGetInteger(GL20.GL_CURRENT_PROGRAM);
if(MCglTF.CURRENT_PROGRAM == 0) {
renderWithVanillaCommands(p_239207_1_);
}
else {
renderWithShaderModCommands(p_239207_1_);
}
break;
}

GL11.glPopAttrib();
GL11.glPopMatrix();
}

private void renderWithVanillaCommands(ItemStack itemStack) {
Item currentItem = itemStack.getItem();
if(currentItem == item) {
Minecraft mc = Minecraft.getInstance();
//Play every animation clips simultaneously
for(Animation animation : itemModelReceiver.animations) {
animation.update(net.minecraftforge.client.model.animation.Animation.getWorldTime(mc.level, net.minecraftforge.client.model.animation.Animation.getPartialTickTime()) % animation.getEndTimeS());
}
itemModelReceiver.vanillaCommands.forEach(Runnable::run);
}
else if(currentItem == blockItem) {
Minecraft mc = Minecraft.getInstance();
for(Animation animation : blockItemModelReceiver.animations) {
animation.update(net.minecraftforge.client.model.animation.Animation.getWorldTime(mc.level, net.minecraftforge.client.model.animation.Animation.getPartialTickTime()) % animation.getEndTimeS());
}
blockItemModelReceiver.vanillaCommands.forEach(Runnable::run);
}
}

private void renderWithShaderModCommands(ItemStack itemStack) {
Item currentItem = itemStack.getItem();
public void renderByItem(ItemStack p_239207_1_, ItemCameraTransforms.TransformType p_239207_2_, MatrixStack p_239207_3_, IRenderTypeBuffer p_239207_4_, int p_239207_5_, int p_239207_6_) {
Item currentItem = p_239207_1_.getItem();
if(currentItem == item) {
Minecraft mc = Minecraft.getInstance();
for(Animation animation : itemModelReceiver.animations) {
animation.update(net.minecraftforge.client.model.animation.Animation.getWorldTime(mc.level, net.minecraftforge.client.model.animation.Animation.getPartialTickTime()) % animation.getEndTimeS());
}
itemModelReceiver.shaderModCommands.forEach(Runnable::run);
itemRenderer.render(p_239207_2_, p_239207_3_, p_239207_4_, p_239207_5_, p_239207_6_);
}
else if(currentItem == blockItem) {
Minecraft mc = Minecraft.getInstance();
for(Animation animation : blockItemModelReceiver.animations) {
animation.update(net.minecraftforge.client.model.animation.Animation.getWorldTime(mc.level, net.minecraftforge.client.model.animation.Animation.getPartialTickTime()) % animation.getEndTimeS());
}
blockItemModelReceiver.shaderModCommands.forEach(Runnable::run);
blockItemRenderer.render(p_239207_2_, p_239207_3_, p_239207_4_, p_239207_5_, p_239207_6_);
}
}

};

item = new Item(new Item.Properties().tab(ItemGroup.TAB_MISC).setISTER(() -> () -> ister));
Expand All @@ -285,7 +175,7 @@ else if(currentItem == blockItem) {
spawnEggItem.setRegistryName(new ResourceLocation("mcgltf", "example_entity_spawn_egg"));
event.getRegistry().register(spawnEggItem);
}

@SubscribeEvent
public static void onEvent(final EntityAttributeCreationEvent event) {
event.put(EXAMPLE_ENTITY_TYPE, ExampleEntity.createAttributes().build());
Expand Down
Loading

0 comments on commit cd45332

Please sign in to comment.