Skip to content

Commit

Permalink
"2d" texture plane model generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ttttdoy committed Jun 13, 2024
1 parent 9bd6382 commit 3f3d919
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/breadmod/ClientModEventBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ object ClientModEventBus {
event.register(modLocation( "${ModelProvider.ITEM_FOLDER}/$TOOL_GUN_DEF/item"))
event.register(modLocation("${ModelProvider.ITEM_FOLDER}/$TOOL_GUN_DEF/coil"))
event.register(modLocation("${ModelProvider.BLOCK_FOLDER}/generator_on"))
event.register(modLocation("${ModelProvider.ITEM_FOLDER}/textureplane_test"))
}

@SubscribeEvent
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/breadmod/CommonModEventBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import breadmod.datagen.tag.ModBlockTags
import breadmod.datagen.tag.ModFluidTags
import breadmod.datagen.tag.ModItemTags
import breadmod.datagen.tag.ModPaintingTags
import breadmod.datagen.texture_plane.ModTexturePlaneProvider
import breadmod.datagen.tool_gun.ModToolGunModeProvider
import breadmod.network.PacketHandler.NETWORK
import breadmod.registry.worldgen.dimensions.ModBiomes
Expand Down Expand Up @@ -42,6 +43,9 @@ object CommonModEventBus {

generator.addProvider(true, ModToolGunModeProvider(packOutput))

// experimental texture to 2d plane
generator.addProvider(true, ModTexturePlaneProvider(packOutput))

if(event.includeServer()) {
LOGGER.info("Server datagen")
generator.addProvider(true, ModLootTableProvider.create(packOutput))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package breadmod.datagen.texture_plane

import breadmod.ModMain
import net.minecraft.data.PackOutput

class ModTexturePlaneProvider(
packOutput: PackOutput
) : TexturePlaneProvider(packOutput, ModMain.ID) {
override fun addTextures() {
addTexture("textureplane_test", ModMain.modLocation("item", "tool_gun"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package breadmod.datagen.texture_plane

import breadmod.util.isSquareOf
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import net.minecraft.data.CachedOutput
import net.minecraft.data.DataProvider
import net.minecraft.data.PackOutput
import net.minecraft.resources.ResourceLocation
import java.awt.image.BufferedImage
import java.util.concurrent.CompletableFuture
import javax.imageio.ImageIO

abstract class TexturePlaneProvider(private val packOutput: PackOutput, private val modID: String): DataProvider {
private val addedModels: MutableMap<String, ResourceLocation> = mutableMapOf()
private val directions = listOf("north", "east", "south", "west", "down")

override fun run(pOutput: CachedOutput): CompletableFuture<*> {
addTextures()
val dataLocation = packOutput.getOutputFolder(PackOutput.Target.RESOURCE_PACK).resolve(modID).resolve(MODELS_LOC)
return CompletableFuture.allOf(
*buildList{
addedModels.forEach { (name, location) ->
add(DataProvider.saveStable(pOutput, JsonObject().also {
it.add(TEXTURE_SIZE, JsonArray().also { array ->
array.add(getTextureRes(location).first); array.add(getTextureRes(location).second) })
it.add(TEXTURES, JsonObject().also { key ->
key.addProperty(TEXTURE_IDENTIFIER, location.toString())
key.addProperty("particle", location.toString())
})
it.add(ELEMENTS, JsonArray().also { element ->
element.add(JsonObject().also { elementObject ->
elementObject.add("from", JsonArray().also { array -> array.add(0.0); array.add(0); array.add(0) })
elementObject.add("to", JsonArray().also { array -> array.add(16); array.add(0.025); array.add(16) })
elementObject.add("faces", JsonObject().also { faceObject ->
directions.forEach { direction ->
faceObject.add(direction, JsonObject().also { directions ->
directions.add("uv", JsonArray().also { array -> repeat(4) {array.add(0)} })
directions.addProperty("texture", TEXTURE_IDENTIFIER)
})
}
faceObject.add("up", JsonObject().also { upDirection ->
upDirection.add("uv", JsonArray().also { up -> up.add(16); up.add(16); up.add(0); up.add(0) })
upDirection.addProperty("texture", TEXTURE_IDENTIFIER)
})
})
})
})
}, dataLocation.resolve("$name.json")))
}
}.toTypedArray()
)
}

override fun getName(): String = "Texture Planes: $modID"

abstract fun addTextures()

fun addTexture(
name: String,
textureLocation: ResourceLocation
) {
if(addedModels.containsKey(name)) throw IllegalStateException("$name is already defined.")
println("texture width: ${getTextureRes(textureLocation).first}")
println("${isSquareOf(getTextureRes(textureLocation).first, 2)}")
println("texture height: ${getTextureRes(textureLocation).second}")
println(println("${isSquareOf(getTextureRes(textureLocation).second, 2)}"))
// if(!isSquareOf(getTextureRes(textureLocation).first, 2) && !isSquareOf(getTextureRes(textureLocation).second, 2)) throw IllegalStateException("Texture resolution is not power of 2.")
addedModels[name] = textureLocation
}

private fun getTextureRes(location: ResourceLocation): Pair<Int, Int> {
// println("INPUT FILE: /assets/${location.namespace}/${location.path}.png")
val image: BufferedImage = ImageIO.read(TexturePlaneProvider::class.java.getResourceAsStream("/assets/${location.namespace}/textures/${location.path}.png"))
val width = image.width
val height = image.height
val resolution = Pair(width, height)
return resolution
}

internal companion object {
const val MODELS_LOC = "models/item"

const val TEXTURE_SIZE = "texture_size"
const val TEXTURES = "textures"
const val TEXTURE_IDENTIFIER = "0"

const val ELEMENTS = "elements"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ToolGunItemRenderer : BlockEntityWithoutLevelRenderer(

private val mainModelLocation = modLocation("${ModelProvider.ITEM_FOLDER}/$TOOL_GUN_DEF/item")
private val coilModelLocation = modLocation("${ModelProvider.ITEM_FOLDER}/$TOOL_GUN_DEF/coil")
private val testModelLocation = modLocation("${ModelProvider.ITEM_FOLDER}/textureplane_test")

override fun renderByItem(
pStack: ItemStack,
Expand All @@ -50,11 +51,14 @@ class ToolGunItemRenderer : BlockEntityWithoutLevelRenderer(
val toolgunItem = pStack.item as ToolGunItem
val renderer = Minecraft.getInstance().itemRenderer
val fontRenderer = Minecraft.getInstance().font
val mainModel = Minecraft.getInstance().modelManager.getModel(mainModelLocation)
val coilModel = Minecraft.getInstance().modelManager.getModel(coilModelLocation)
val modelManager = Minecraft.getInstance().modelManager
val mainModel = modelManager.getModel(mainModelLocation)
val coilModel = modelManager.getModel(coilModelLocation)
val testModel = modelManager.getModel(testModelLocation)

pPoseStack.pushPose()
renderModel(mainModel, renderer, pStack, pPoseStack, pBuffer, pPackedOverlay, pPackedLight)
renderModel(testModel, renderer, pStack, pPoseStack, pBuffer, pPackedOverlay, pPackedLight)
// todo smooth rotation after firing toolgun, quickly tapering off
// todo recoil and increased coil spin when using tool gun

Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/breadmod/util/General.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ import java.nio.file.Files
import java.util.*
import kotlin.io.path.absolutePathString
import kotlin.io.path.writeBytes
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.ln
import kotlin.math.min
import kotlin.system.exitProcess

Expand Down Expand Up @@ -88,6 +91,10 @@ fun formatUnit(pFrom: Double, pTo: Double, pUnit: String, pFormatShort: Boolean,
fun formatUnit(pFrom: Int, pTo: Int, pUnit: String, pFormatShort: Boolean, pDecimals: Int, pUnitOffset: Int = 0, pUnitMax: Int = 1000): String =
formatUnit(pFrom.toDouble(), pTo.toDouble(), pUnit, pFormatShort, pDecimals, pUnitOffset, pUnitMax)

private val logs = mutableMapOf<Double, Double>()
fun isSquareOf(n: Double, p: Double) = logs.getOrPut(p) { ln(p) }.let { ((ceil(n) / it)) == floor(n / it) }
fun isSquareOf(n: Int, p: Int) = isSquareOf(n.toDouble(), p.toDouble())

fun GuiGraphics.renderFluid(
pX: Float, pY: Float, pWidth: Int, pHeight: Int,
pFluid: Fluid, pFlowing: Boolean, pDirection: Direction = Direction.NORTH,
Expand Down

0 comments on commit 3f3d919

Please sign in to comment.