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 d11ee95 commit 1942ffa
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 146 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. Create a folder named `libs` in the same dir level as `src`.
2. Download MCglTF jar from curseforge and then put into the `libs` folder.
Expand Down
27 changes: 1 addition & 26 deletions 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.12.2-Forge-1.1.0.1'
version = '1.12.2-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 Expand Up @@ -82,31 +82,6 @@ minecraft {
}
}
}

data {
workingDirectory project.file('run')

// Recommended logging data for a userdev environment
// The markers can be changed as needed.
// "SCAN": For mods scan.
// "REGISTRIES": For firing of registry events.
// "REGISTRYDUMP": For getting the contents of all registries.
property 'forge.logging.markers', 'REGISTRIES'

// Recommended logging level for the console
// You can set various levels here.
// Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
property 'forge.logging.console.level', 'debug'

// Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources.
args '--mod', 'example_mcgltf_usage', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')

mods {
example_mcgltf_usage {
source sourceSets.main
}
}
}
}
}

Expand Down

This file was deleted.

37 changes: 6 additions & 31 deletions src/main/java/com/timlee9024/mcgltf/example/ClientProxy.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.timlee9024.mcgltf.example;

import org.lwjgl.opengl.GL11;

import com.timlee9024.mcgltf.ItemCameraTransformsHelper;
import com.timlee9024.mcgltf.MCglTF;

import de.javagl.jgltf.model.animation.Animation;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
Expand Down Expand Up @@ -39,57 +35,36 @@ public Render<? super EntityExample> createRenderFor(RenderManager manager) {

@Override
public void preInit(FMLPreInitializationEvent event) {
AbstractItemGltfModelReceiver itemModelReceiver = new AbstractItemGltfModelReceiver() {
ItemRendererExample itemRenderer = new ItemRendererExample() {

@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() {
ItemRendererExample blockItemRenderer = new ItemRendererExample() {

@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.
TileEntityItemStackRenderer teisr = new TileEntityItemStackRenderer() {

@Override
public void renderByItem(ItemStack itemStackIn) {
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glShadeModel(GL11.GL_SMOOTH);

Item currentItem = itemStackIn.getItem();
if(currentItem == item) {
//Require ItemCameraTransformsHelper$registerDummyModelToAccessCurrentTransformTypeForTEISR(yourItem) during ModelRegistryEvent to make ItemCameraTransformsHelper$getCurrentTransformType() work
switch(ItemCameraTransformsHelper.getCurrentTransformType()) {
case GUI:
GL11.glEnable(GL11.GL_LIGHTING);
break;
default:
break;
}

//Play every animation clips simultaneously
for(Animation animation : itemModelReceiver.animations) {
animation.update(net.minecraftforge.client.model.animation.Animation.getWorldTime(Minecraft.getMinecraft().world, net.minecraftforge.client.model.animation.Animation.getPartialTickTime()) % animation.getEndTimeS());
}
itemModelReceiver.commands.forEach(Runnable::run);
itemRenderer.renderWithItemCameraTransformsHelper();
}
else if(currentItem == itemBlock) {
for(Animation animation : blockItemModelReceiver.animations) {
animation.update(net.minecraftforge.client.model.animation.Animation.getWorldTime(Minecraft.getMinecraft().world, net.minecraftforge.client.model.animation.Animation.getPartialTickTime()) % animation.getEndTimeS());
}
blockItemModelReceiver.commands.forEach(Runnable::run);
blockItemRenderer.render();
}

GL11.glPopAttrib();
}

};
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/timlee9024/mcgltf/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

@Mod(modid = "example_mcgltf_usage", useMetadata = true)
@Mod(modid = "example_mcgltf_usage", dependencies = "required-after-client:mcgltf;", useMetadata = true)
public class Example {

@SidedProxy(clientSide = "com.timlee9024.mcgltf.example.ClientProxy", serverSide = "com.timlee9024.mcgltf.example.ServerProxy")
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/com/timlee9024/mcgltf/example/ItemRendererExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.timlee9024.mcgltf.example;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30;

import com.timlee9024.mcgltf.IGltfModelReceiver;
import com.timlee9024.mcgltf.ItemCameraTransformsHelper;
import com.timlee9024.mcgltf.MCglTF;
import com.timlee9024.mcgltf.RenderedGltfModel;
import com.timlee9024.mcgltf.RenderedGltfScene;
import com.timlee9024.mcgltf.animation.GltfAnimationCreator;
import com.timlee9024.mcgltf.animation.InterpolatedChannel;

import de.javagl.jgltf.model.AnimationModel;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.model.animation.Animation;

public abstract class ItemRendererExample implements IGltfModelReceiver {

protected RenderedGltfScene renderedScene;

protected List<List<InterpolatedChannel>> animations;

@Override
public void onReceiveSharedModel(RenderedGltfModel renderedModel) {
renderedScene = renderedModel.renderedGltfScenes.get(0);
List<AnimationModel> animationModels = renderedModel.gltfModel.getAnimationModels();
animations = new ArrayList<List<InterpolatedChannel>>(animationModels.size());
for(AnimationModel animationModel : animationModels) {
animations.add(GltfAnimationCreator.createGltfAnimation(animationModel));
}
}

public void render() {
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glShadeModel(GL11.GL_SMOOTH);

float time = Animation.getWorldTime(Minecraft.getMinecraft().world, Animation.getPartialTickTime());
for(List<InterpolatedChannel> animation : animations) {
animation.parallelStream().forEach((channel) -> {
float[] keys = channel.getKeys();
channel.update(time % keys[keys.length - 1]);
});
}

if(MCglTF.getInstance().isShaderModActive()) {
renderedScene.renderForShaderMod();
}
else {
renderedScene.renderForVanilla();
}

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
RenderedGltfModel.nodeGlobalTransformLookup.clear();

GL11.glPopAttrib();
}

/**
* Require {@link ItemCameraTransformsHelper#registerDummyModelToAccessCurrentTransformTypeForTEISR(net.minecraft.item.Item) ItemCameraTransformsHelper#registerDummyModelToAccessCurrentTransformTypeForTEISR(yourItem)} during
* {@link net.minecraftforge.client.event.ModelRegistryEvent ModelRegistryEvent} to make {@link ItemCameraTransformsHelper#getCurrentTransformType()} work
*/
public void renderWithItemCameraTransformsHelper() {
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glShadeModel(GL11.GL_SMOOTH);

float time = Animation.getWorldTime(Minecraft.getMinecraft().world, Animation.getPartialTickTime());
//Play every animation clips simultaneously
for(List<InterpolatedChannel> animation : animations) {
animation.parallelStream().forEach((channel) -> {
float[] keys = channel.getKeys();
channel.update(time % keys[keys.length - 1]);
});
}

switch(ItemCameraTransformsHelper.getCurrentTransformType()) {
case GUI:
GL11.glEnable(GL11.GL_LIGHTING);
renderedScene.renderForVanilla();
break;
default:
if(MCglTF.getInstance().isShaderModActive()) {
renderedScene.renderForShaderMod();
}
else {
renderedScene.renderForVanilla();
}
break;
}

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
RenderedGltfModel.nodeGlobalTransformLookup.clear();

GL11.glPopAttrib();
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package com.timlee9024.mcgltf.example;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30;

import com.timlee9024.mcgltf.IGltfModelReceiver;
import com.timlee9024.mcgltf.MCglTF;
import com.timlee9024.mcgltf.RenderedGltfModel;
import com.timlee9024.mcgltf.RenderedGltfScene;
import com.timlee9024.mcgltf.animation.GltfAnimationCreator;
import com.timlee9024.mcgltf.animation.InterpolatedChannel;

import de.javagl.jgltf.model.GltfAnimations;
import de.javagl.jgltf.model.animation.Animation;
import de.javagl.jgltf.model.AnimationModel;
import net.minecraft.client.renderer.culling.ICamera;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.animation.Animation;

public class RenderEntityExample extends Render<EntityExample> implements IGltfModelReceiver {

protected List<Runnable> commands;
protected RenderedGltfScene renderedScene;

protected List<Animation> animations;
protected List<List<InterpolatedChannel>> animations;

public RenderEntityExample(RenderManager renderManager) {
super(renderManager);
Expand All @@ -33,8 +40,12 @@ public ResourceLocation getModelLocation() {

@Override
public void onReceiveSharedModel(RenderedGltfModel renderedModel) {
commands = renderedModel.sceneCommands.get(0);
animations = GltfAnimations.createModelAnimations(renderedModel.gltfModel.getAnimationModels());
renderedScene = renderedModel.renderedGltfScenes.get(0);
List<AnimationModel> animationModels = renderedModel.gltfModel.getAnimationModels();
animations = new ArrayList<List<InterpolatedChannel>>(animationModels.size());
for(AnimationModel animationModel : animationModels) {
animations.add(GltfAnimationCreator.createGltfAnimation(animationModel));
}
}

@Override
Expand All @@ -61,21 +72,37 @@ else if (livingEntity.getLeashed() && livingEntity.getLeashHolder() != null)

@Override
public void doRender(EntityExample entity, double x, double y, double z, float entityYaw, float partialTicks) {
if(commands != null) {
GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
GL11.glEnable(GL11.GL_BLEND);
GL11.glTranslated(x, y, z);
GL11.glRotatef(interpolateRotation(entity.prevRenderYawOffset, entity.renderYawOffset, partialTicks), 0.0F, 1.0F, 0.0F);
for(Animation animation : animations) {
animation.update(net.minecraftforge.client.model.animation.Animation.getWorldTime(entity.world, partialTicks) % animation.getEndTimeS());
}
commands.forEach(Runnable::run);
GL11.glPopAttrib();
GL11.glPopMatrix();
GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
GL11.glEnable(GL11.GL_BLEND);
GL11.glTranslated(x, y, z);
GL11.glRotatef(interpolateRotation(entity.prevRenderYawOffset, entity.renderYawOffset, partialTicks), 0.0F, 1.0F, 0.0F);

float time = Animation.getWorldTime(entity.world, partialTicks);
//Play every animation clips simultaneously
for(List<InterpolatedChannel> animation : animations) {
animation.parallelStream().forEach((channel) -> {
float[] keys = channel.getKeys();
channel.update(time % keys[keys.length - 1]);
});
}

if(MCglTF.getInstance().isShaderModActive()) {
renderedScene.renderForShaderMod();
}
else {
renderedScene.renderForVanilla();
}

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
RenderedGltfModel.nodeGlobalTransformLookup.clear();

GL11.glPopAttrib();
GL11.glPopMatrix();
super.doRender(entity, x, y, z, entityYaw, partialTicks);
}

Expand Down
Loading

0 comments on commit 1942ffa

Please sign in to comment.