Skip to content

Example: Glowing Ore

Alex Habich edited this page Jul 18, 2020 · 5 revisions

Introduction

This article provides example code for a Redstone Ore block model with glowing bits of ore. It include optional shaders that activate when the Canvas renderer (or another, future FREX renderer) is active.

Textures

The non-shader model relies on double-layer cutout textures. They can be created using the technique described in How To: Preparing Double-Layer Cutout Textures

For the shader-based model, a single texture is used with the ore parts made translucent as a signal for the renderer to make them emissive and animate them.

The textures used for this example can be found here: https://github.com/grondag/json-model-extensions/tree/master/testpack/assets/jmx/textures/block

Model

Only a single model file is required. It inherits from the 2/jmx_cube_all model provided with JMX. The parent model is like the vanilla Minecraft model cube_all except it contains tags for JMX extensions. The 2 indicates that the model supports a sprite depth of 2.

This model, plus the textures, are the only assets necessary to create an enhanced model that works with Indigo.

assets/minecraft/models/block/redstone_ore.json

{
    "parent": "jmx:block/2/jmx_cube_all",
    "textures": {
        "all": "minecraft:block/redstone_ore"
    },
    "jmx": {
        "textures": {
            "jmx_tex_particle": "minecraft:block/redstone_ore",
            "layered_textures": [
                { "jmx_tex_all": "jmx:block/stone_ore_mask" },
                { "jmx_tex_all": "jmx:block/redstone_ore_bits" }
            ]
        },
        "materials": {
            "jmx_mat_all": {
                "layers": [
                    { "layer": "cutout_mipped" },
                    { "layer": "cutout_mipped", "emissive": true, "ambient_occlusion": false, "diffuse": false }
                ]
            }
        }
    },
    "frex": {
        "textures": {
            "jmx_tex_particle": "minecraft:block/redstone_ore",
            "layered_textures": [
                { "jmx_tex_all": "jmx:block/redstone_ore_shader" },
                { "jmx_tex_all": null }
            ]
        },
        "materials": {
            "jmx_mat_all": { "preset": "jmx:redstone" }
        }
    }
}

Canvas Shaders

IMPORTANT: These examples will break in the near future due to upcoming improvements to the Canvas GLSL library. They are provided to demonstrate the technique but it is recommended that content authors wait for those changes to be added to Canvas 0.7 before creating custom shaders.

assets/jmx/shaders/redstone.vert

#version 120

varying vec2 v_noise_uv;

void main()
{
    v_noise_uv = uv(gl_Vertex.xyz, in_normal_ao.xyz);
    setupVertex();
}

assets/jmx/shaders/redstone.frag

#version 120

varying vec2 v_noise_uv;

void main()
{
    vec4 color = texture2D(u_textures, v_texcoord_0);
    float noise = tnoise(v_noise_uv * 64, u_time);
    if(color.a == 0.0) {
        color = vec4(color.r * (0.5 + 0.5 * noise), 0.0, 0.0, 1.0);
    } else {
        color = diffuseColor();
        if(noise > 0.95) {
            color = vec4(min(1.0, color.r + (noise - 0.95) * 10.0), color.gba);
        }
    }
    gl_FragColor = fog(color);
}

FREX Material

This example relies on the FREX material loader to create a preset material with our shaders.

assets/jmx/materials/redstone.json

{
	"vertexSource": "jmx:shaders/redstone.vert",
	"fragmentSource": "jmx:shaders/redstone.frag",
	"layers": [
		{
			"disableAo": false,
			"disableColorIndex": false,
			"disableDiffuse": false,
			"blendMode": "solid",
			"emissive": false
		}
	]
}