Render the computer cursor as emissive

- Split the front face of the computer model into two layers - one for
   the main texture, and one for the cursor. This is actually a
   simplification of what we had before, which is nice.

 - Make the cursor layer render as an emissive quad, meaning it glows in
   the dark. This is very easy on Forge (just some model JSON) and very
   hard on Fabric (requires a custom model loader).
This commit is contained in:
Jonathan Coates 2023-06-17 18:01:42 +01:00
parent 0f866836a0
commit ccfed0059b
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
31 changed files with 276 additions and 44 deletions

View File

@ -104,6 +104,7 @@ tasks.withType(JavaCompile::class.java).configureEach {
tasks.processResources {
exclude("**/*.license")
exclude(".cache")
}
tasks.withType(AbstractArchiveTask::class.java).configureEach {

View File

@ -37,6 +37,14 @@
import static net.minecraft.data.models.model.TextureMapping.getBlockTexture;
class BlockModelProvider {
private static final TextureSlot CURSOR = TextureSlot.create("cursor");
private static final ModelTemplate COMPUTER_ON = new ModelTemplate(
Optional.of(new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/computer_on")),
Optional.empty(),
TextureSlot.FRONT, TextureSlot.SIDE, TextureSlot.TOP, CURSOR
);
private static final ModelTemplate MONITOR_BASE = new ModelTemplate(
Optional.of(new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/monitor_base")),
Optional.empty(),
@ -142,11 +150,18 @@ private static void registerPrinter(BlockModelGenerators generators) {
private static void registerComputer(BlockModelGenerators generators, ComputerBlock<?> block) {
generators.blockStateOutput.accept(MultiVariantGenerator.multiVariant(block)
.with(createHorizontalFacingDispatch())
.with(createModelDispatch(ComputerBlock.STATE, state -> ModelTemplates.CUBE_ORIENTABLE.createWithSuffix(
block, "_" + state.getSerializedName(),
TextureMapping.orientableCube(block).put(TextureSlot.FRONT, getBlockTexture(block, "_front" + state.getTexture())),
generators.modelOutput
)))
.with(createModelDispatch(ComputerBlock.STATE, state -> switch (state) {
case OFF -> ModelTemplates.CUBE_ORIENTABLE.createWithSuffix(
block, "_" + state.getSerializedName(),
TextureMapping.orientableCube(block),
generators.modelOutput
);
case ON, BLINKING -> COMPUTER_ON.createWithSuffix(
block, "_" + state.getSerializedName(),
TextureMapping.orientableCube(block).put(CURSOR, new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/computer" + state.getTexture())),
generators.modelOutput
);
}))
);
generators.delegateItemModel(block, getModelLocation(block, "_blinking"));
}

View File

@ -0,0 +1,39 @@
{
"parent": "minecraft:block/block",
"render_type": "cutout",
"textures": {
"particle": "#front"
},
"display": {
"firstperson_righthand": {
"rotation": [ 0, 135, 0 ],
"translation": [ 0, 0, 0 ],
"scale": [ 0.40, 0.40, 0.40 ]
}
},
"elements": [
{
"from": [ 0, 0, 0 ],
"to": [ 16, 16, 16 ],
"faces": {
"down": { "texture": "#top", "cullface": "down" },
"up": { "texture": "#top", "cullface": "up" },
"north": { "texture": "#front", "cullface": "north" },
"south": { "texture": "#side", "cullface": "south" },
"west": { "texture": "#side", "cullface": "west" },
"east": { "texture": "#side", "cullface": "east" }
}
},
{
"from": [ 0, 0, 0 ],
"to": [ 16, 16, 16 ],
"faces": {
"north": {
"texture": "#cursor",
"cullface": "north",
"forge_data": {"block_light": 15, "sky_light": 15}
}
}
}
]
}

View File

@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
SPDX-License-Identifier: MPL-2.0

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 B

View File

@ -1,6 +0,0 @@
{
"animation": {
"frametime": 8,
"frames": [ 0, 1 ]
}
}

View File

@ -1,6 +0,0 @@
{
"animation": {
"frametime": 8,
"frames": [ 0, 1 ]
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 B

View File

@ -165,7 +165,6 @@ tasks.processResources {
filesMatching("fabric.mod.json") {
expand(mapOf("version" to modVersion))
}
exclude(".cache")
}
tasks.jar {

View File

@ -4,6 +4,7 @@
package dan200.computercraft.client;
import dan200.computercraft.client.model.EmissiveComputerModel;
import dan200.computercraft.client.model.turtle.TurtleModelLoader;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.network.client.ClientNetworkContext;
@ -35,9 +36,12 @@ public static void init() {
ClientRegistry.registerItemColours(ColorProviderRegistry.ITEM::register);
ClientRegistry.registerMainThread();
ModelLoadingRegistry.INSTANCE.registerModelProvider((manager, out) -> ClientRegistry.registerExtraModels(out));
ModelLoadingRegistry.INSTANCE.registerResourceProvider(loader -> (path, ctx) -> TurtleModelLoader.load(loader, path));
ModelLoadingRegistry.INSTANCE.registerResourceProvider(loader -> (path, ctx) -> EmissiveComputerModel.load(loader, path));
BlockRenderLayerMap.INSTANCE.putBlock(ModRegistry.Blocks.COMPUTER_NORMAL.get(), RenderType.cutout());
BlockRenderLayerMap.INSTANCE.putBlock(ModRegistry.Blocks.COMPUTER_COMMAND.get(), RenderType.cutout());
BlockRenderLayerMap.INSTANCE.putBlock(ModRegistry.Blocks.COMPUTER_ADVANCED.get(), RenderType.cutout());
BlockRenderLayerMap.INSTANCE.putBlock(ModRegistry.Blocks.MONITOR_NORMAL.get(), RenderType.cutout());
BlockRenderLayerMap.INSTANCE.putBlock(ModRegistry.Blocks.MONITOR_ADVANCED.get(), RenderType.cutout());

View File

@ -0,0 +1,172 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.client.model;
import com.google.gson.JsonObject;
import com.mojang.datafixers.util.Either;
import dan200.computercraft.api.ComputerCraftAPI;
import net.fabricmc.fabric.api.client.model.ModelProviderException;
import net.fabricmc.fabric.api.client.model.ModelResourceProvider;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel;
import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.*;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.GsonHelper;
import net.minecraft.util.RandomSource;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Wraps a computer's {@link BlockModel}/{@link BakedModel} to render the computer's cursor as an emissive quad.
* <p>
* While Fabric has a quite advanced rendering extension API (including support for custom materials), but unlike Forge
* it doesn't expose this in the model JSON (though externals mods like <a href="https://github.com/vram-guild/json-model-extensions/">JMX</a>
* do handle this).
* <p>
* Instead, we support emissive quads by injecting a custom {@linkplain ModelResourceProvider model loader/provider}
* which targets a hard-coded list of computer models, and wraps the returned model in a custom
* {@linkplain FabricBakedModel} implementation which renders specific quads as emissive.
* <p>
* See also the <code>assets/computercraft/models/block/computer_on.json</code> model, which is the base for all
* emissive computer models.
*/
public final class EmissiveComputerModel {
private static final Set<String> MODELS = Set.of(
"item/computer_advanced",
"block/computer_advanced_on",
"block/computer_advanced_blinking",
"item/computer_command",
"block/computer_command_on",
"block/computer_command_blinking",
"item/computer_normal",
"block/computer_normal_on",
"block/computer_normal_blinking"
);
private EmissiveComputerModel() {
}
public static @Nullable UnbakedModel load(ResourceManager resources, ResourceLocation path) throws ModelProviderException {
if (!path.getNamespace().equals(ComputerCraftAPI.MOD_ID) || !MODELS.contains(path.getPath())) return null;
JsonObject json;
try (var reader = resources.openAsReader(new ResourceLocation(path.getNamespace(), "models/" + path.getPath() + ".json"))) {
json = GsonHelper.parse(reader).getAsJsonObject();
} catch (IOException e) {
throw new ModelProviderException("Failed loading model " + path, e);
}
// Parse a subset of the model JSON
var parent = new ResourceLocation(GsonHelper.getAsString(json, "parent"));
Map<String, Either<Material, String>> textures = new HashMap<>();
if (json.has("textures")) {
var jsonObject = GsonHelper.getAsJsonObject(json, "textures");
for (var entry : jsonObject.entrySet()) {
var texture = entry.getValue().getAsString();
textures.put(entry.getKey(), texture.startsWith("#")
? Either.right(texture.substring(1))
: Either.left(new Material(InventoryMenu.BLOCK_ATLAS, new ResourceLocation(texture)))
);
}
}
return new Unbaked(parent, textures);
}
/**
* An {@link UnbakedModel} which wraps the returned model using {@link Baked}.
* <p>
* This subclasses {@link BlockModel} to allow using these models as a parent of other models.
*/
private static final class Unbaked extends BlockModel {
Unbaked(ResourceLocation parent, Map<String, Either<Material, String>> materials) {
super(parent, List.of(), materials, null, null, ItemTransforms.NO_TRANSFORMS, List.of());
}
@Override
public BakedModel bake(ModelBaker baker, Function<Material, TextureAtlasSprite> spriteGetter, ModelState state, ResourceLocation location) {
var baked = super.bake(baker, spriteGetter, state, location);
if (!hasTexture("cursor")) return baked;
var render = RendererAccess.INSTANCE.getRenderer();
if (render == null) return baked;
return new Baked(
baked,
spriteGetter.apply(getMaterial("cursor")),
render.materialFinder().find(),
render.materialFinder().emissive(0, true).find()
);
}
}
/**
* A {@link FabricBakedModel} which renders quads using the {@code "cursor"} texture as emissive.
*/
private static final class Baked extends ForwardingBakedModel {
private final TextureAtlasSprite cursor;
private final RenderMaterial defaultMaterial;
private final RenderMaterial emissiveMaterial;
Baked(BakedModel wrapped, TextureAtlasSprite cursor, RenderMaterial defaultMaterial, RenderMaterial emissiveMaterial) {
this.wrapped = wrapped;
this.cursor = cursor;
this.defaultMaterial = defaultMaterial;
this.emissiveMaterial = emissiveMaterial;
}
@Override
public boolean isVanillaAdapter() {
return false;
}
@Override
public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier<RandomSource> randomSupplier, RenderContext context) {
emitQuads(context, state, randomSupplier.get());
}
@Override
public void emitItemQuads(ItemStack stack, Supplier<RandomSource> randomSupplier, RenderContext context) {
emitQuads(context, null, randomSupplier.get());
}
private void emitQuads(RenderContext context, @Nullable BlockState state, RandomSource random) {
var emitter = context.getEmitter();
for (var faceIdx = 0; faceIdx <= ModelHelper.NULL_FACE_ID; faceIdx++) {
var cullFace = ModelHelper.faceFromIndex(faceIdx);
var quads = wrapped.getQuads(state, cullFace, random);
var count = quads.size();
for (var i = 0; i < count; i++) {
final var q = quads.get(i);
emitter.fromVanilla(q, q.getSprite() == cursor ? emissiveMaterial : defaultMaterial, cullFace);
emitter.emit();
}
}
}
}
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_advanced_front_blink",
"cursor": "computercraft:block/computer_blink",
"front": "computercraft:block/computer_advanced_front",
"side": "computercraft:block/computer_advanced_side",
"top": "computercraft:block/computer_advanced_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_advanced_front_on",
"cursor": "computercraft:block/computer_on",
"front": "computercraft:block/computer_advanced_front",
"side": "computercraft:block/computer_advanced_side",
"top": "computercraft:block/computer_advanced_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_command_front_blink",
"cursor": "computercraft:block/computer_blink",
"front": "computercraft:block/computer_command_front",
"side": "computercraft:block/computer_command_side",
"top": "computercraft:block/computer_command_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_command_front_on",
"cursor": "computercraft:block/computer_on",
"front": "computercraft:block/computer_command_front",
"side": "computercraft:block/computer_command_side",
"top": "computercraft:block/computer_command_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_normal_front_blink",
"cursor": "computercraft:block/computer_blink",
"front": "computercraft:block/computer_normal_front",
"side": "computercraft:block/computer_normal_side",
"top": "computercraft:block/computer_normal_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_normal_front_on",
"cursor": "computercraft:block/computer_on",
"front": "computercraft:block/computer_normal_front",
"side": "computercraft:block/computer_normal_side",
"top": "computercraft:block/computer_normal_top"
}

View File

@ -206,7 +206,6 @@ tasks.processResources {
filesMatching("META-INF/mods.toml") {
expand(mapOf("forgeVersion" to libs.versions.forge.get(), "file" to mapOf("jarVersion" to modVersion)))
}
exclude(".cache")
}
tasks.jar {

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_advanced_front_blink",
"cursor": "computercraft:block/computer_blink",
"front": "computercraft:block/computer_advanced_front",
"side": "computercraft:block/computer_advanced_side",
"top": "computercraft:block/computer_advanced_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_advanced_front_on",
"cursor": "computercraft:block/computer_on",
"front": "computercraft:block/computer_advanced_front",
"side": "computercraft:block/computer_advanced_side",
"top": "computercraft:block/computer_advanced_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_command_front_blink",
"cursor": "computercraft:block/computer_blink",
"front": "computercraft:block/computer_command_front",
"side": "computercraft:block/computer_command_side",
"top": "computercraft:block/computer_command_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_command_front_on",
"cursor": "computercraft:block/computer_on",
"front": "computercraft:block/computer_command_front",
"side": "computercraft:block/computer_command_side",
"top": "computercraft:block/computer_command_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_normal_front_blink",
"cursor": "computercraft:block/computer_blink",
"front": "computercraft:block/computer_normal_front",
"side": "computercraft:block/computer_normal_side",
"top": "computercraft:block/computer_normal_top"
}

View File

@ -1,7 +1,8 @@
{
"parent": "minecraft:block/orientable",
"parent": "computercraft:block/computer_on",
"textures": {
"front": "computercraft:block/computer_normal_front_on",
"cursor": "computercraft:block/computer_on",
"front": "computercraft:block/computer_normal_front",
"side": "computercraft:block/computer_normal_side",
"top": "computercraft:block/computer_normal_top"
}