User Tools

Site Tools


mtr_addon:nte:js:resources

Resource Loading

NTE provides several methods for controlling the loading and retrieval of resources within resource packs in JavaScript scripts.

Code written in the top-level space outside of functions will be executed when a resource pack is loaded and can be used to initialize resources such as models, textures, etc. Resources that should not be different for each train (such as models and such) are recommended to be stored in global variables to avoid excessive memory usage when loading a copy of the same content for each train.

ResourceLocation

Minecraft uses a ResourceLocation to identify files in a resource pack. Many functions only accept ResourceLocation paths rather than strings. To create a ResourceLocation, you can use the following functions:

  • static Resources.id(idStr: String): ResourceLocation
    • Converts the string to the appropriate ResourceLocation, e.g. Resources.id(“mtr:path/absolute.js”).
  • static Resources.idr(relPath: String): ResourceLocation
    • Converts the string to ResourceLocation relative to the directory that the currently executed script are located in, e.g. Resources.idr(“relative.js”).

Model Loading

The way to handle the model in NTE is that the model file should first be loaded as RawModel, then you may do some processing with it, then you have to upload it to get the ModelCluster, and finally you have to hand the ModelCluster to NTE to display when rendering.

  • static ModelManager.loadRawModel(Resources.manager(), path: ResourceLocation, null): RawModel
    • Loads a model as a whole as a RawModel.
  • static ModelManager.loadPartedRawModel(Resources.manager(), path: ResourceLocation, null): Map<String, RawModel>
    • Loads each group of a model into individual RawModels, returning a Java Map.
  • static ModelManager.uploadVertArrays(rawModel: RawModel): ModelCluster
    • Uploads the RawModel to the GPU's video memory (VRAM), returning the uploaded ModelCluster.

Loading AWT Resources

These functions load the resources used to draw dynamic textures via the Java AWT.

  • static Resources.getSystemFont(name: String): Font
    • Get a system or MTR built-in font.
Font Name Description
Noto Serif MTR's built-in serif font (similar to Song). Identical on all systems.
Noto Sans NTE Built-in sans serif font (similar to Helvetica). Identical on all systems.
Serif A serif font selected by AWT that is installed on this computer. May be different on different devices.
SansSerif A sans serif font selected by AWT that is installed on this computer. May be different on different devices.
Monospaced A monospaced font selected by AWT that is installed on this computer. May be different on different devices.
  • static Resources.readBufferedImage(path: ResourceLocation): BufferedImage
    • Loads an image file as an AWT BufferedImage.
  • static Resources.readFont(path: ResourceLocation): Font
    • Load a custom TTF or OTF font file as an AWT Font.
    • Note: Due to the way Java and Minecraft Resource Pack works, new temporary font files are generated on your disk every time you call this function. If you find yourself running out of disk space during the development, you can free up disk space by deleting files starting with +~JF on your system temp directory, or restart your Minecraft/Computer.
  • static Resources.getFontRenderContext(): FontRenderContext
    • Get an AWT FontRenderContext.

Reading resource files

  • static Resources.readString(location: ResourceLocation): String
    • Reads the contents of a resource file as a string. Returns null if reading fails.

Misc

  • static Resources.parseNbtString(nbtStr: String): CompoundTag
    • Returns Minecraft's CompoundTag (type for NBT), syntax is similar to the one used in Command Blocks.

RawModel

Functions Description
RawModel.append(other: RawModel): void Merges other RawModel into this RawModel.
RawModel.append(other: RawMesh): void Merges other RawMesh into this RawModel.
RawModel.applyMatrix(transform: Matrix4f): void Applies the transformation matrix on the vertices of the model.
RawModel.applyTranslation(x: float, y: float, z: float): void Moves the vertices of the model according to the specified parameters.
RawModel.applyRotation(direction: Vector3f, angle: float): void Rotates the model vertices around the specified axis centered at the coordinate origin. The angles are specified in degrees.
RawModel.applyScale(x: float, y: float, z: float): void Changes the size of the model.
RawModel.applyMirror(vx: boolean, vy: boolean, vz: boolean, nx: boolean, ny: boolean, nz: boolean): void Mirrors the model. Six boolean values, the first three whether to mirror vertices or not, the last three whether to mirror normals or not.
RawModel.applyUVMirror(u: boolean, v: boolean): void Mirrors the UV directions. As a result, it is necessary that the positive direction of V is directed downwards, so when importing models from Blockbench or Blender you should use rawModel.applyUVMirror(false, true).
RawModel.replaceTexture(oldFileName: String, path: ResourceLocation): void Replaces all textures referencing oldFileName with the texture specified by path ResourceLocation.
RawModel.replaceAllTexture(path: ResourceLocation): void Replaces all textures with the texture specified by path ResourceLocation.
RawModel.copy(): RawModel Copies the textures and vertices of this model into a new model.
RawModel.copyForMaterialChanges(): RawModel Copies the textures of this model into the new model, while keeping the vertices shared.

If the same model needs to be modified in different ways, it may be necessary to copy the model. For example, a is a RawModel, and when b = a, both variables will point to the same RawModel: modifying b will affect a, in which case the previous state will be lost. To prevent this, you should use b = a.copy() to create a copy so that the two variables do not affect each other.

If you don't need to change the geometry, but only need to change the material or replace the texture, you can use b = a.copyForMaterialChanges(), which copies only the information associated with the material, obviating the need to copy vertices in this case.

ModelCluster

Once the model is uploaded to the GPU video memory, the vertices can no longer be changed, however the texture may still be changed. Therefore, if you need to replace textures several times, you can upload the model as a ModelCluster first and then replace the textures, thus avoiding the unnecessary uploading of the model (Where you upload each model with a different textures each time).

Functions Description
ModelCluster.replaceTexture(oldFileName: String, path: ResourceLocation): void Replaces all textures referencing oldFileName in the model with the texture specified by path ResourceLocation.
ModelCluster.replaceAllTexture(path: ResourceLocation): void Replaces all textures with the texture specified by path.
ModelCluster.copyForMaterialChanges(): ModelCluster Copies the textures of this model into the new model, while keeping the vertices shared.

Source

mtr_addon/nte/js/resources.txt · Last modified: 2024/02/08 05:35 by lx862