Code Monkey home page Code Monkey logo

schematicwebviewer's Introduction

SchematicWebViewer

An NPM package to facilitate importing and viewing of modern Minecraft schematics.

Originally by cpdt and was available here.

Usage

This library requires a complete Minecraft resource pack in order to function. This means a resource pack that include all models, blockstates, and textures. As most resource packs only include what they have changed, they do not fit this criteria. Luckily, the Minecraft client jar file is formatted in the same way as a resource pack.

To use this on your site, create a canvas element in your HTML that is able to be queried in the JavaScript.

<canvas id="schematicRenderer" , width="500," height="500"></canvas>
renderSchematic(document.querySelector('#schematicRenderer'), SCHEMATIC_FILE, {
    size: 500,
    renderArrow: false,
    renderBars: false,
    corsBypassUrl: 'https://url-to-cors-anywhere/',
});

The renderSchematic function takes a few options.

The first argument is the canvas element to render to.

The second argument is a schematic file encoded in Base64. The schematic format must be supported by SchematicJS.

The final argument is an options object that allows configuring various settings about how the schematic is rendered. The following properties are on the object,

interface SchematicRenderOptions {
    /**
     * Usage as number is deprecated and will be removed
     */
    size?: number | { width: number; height: number };
    /**
     * A url of a cors-anywhere instance to allow access to MC server jars. Required by the default `getClientJarUrl` function
     */
    corsBypassUrl?: string;
    /**
     * A function that returns the url of the client jar to use. Defaults to using the EngineHub Cassette Deck service
     */
    getClientJarUrl?: (props: GetClientJarUrlProps) => Promise<string>;
    /**
     * A list of resource pack URLs in priority order
     */
    resourcePacks?: string[];
    /**
     * Whether a grid should be rendered
     */
    renderBars?: boolean;
    /**
     * Whether an arrow to show direction should be rendered
     */
    renderArrow?: boolean;
    /**
     * Whether the view should automatically rotate when not being dragged by the user
     */
    orbit?: boolean;
    /**
     * The speed at which the view should orbit (default: 0.02)
     */
    orbitSpeed?: number;
    /**
     * Whether antialiasing should be enabled
     */
    antialias?: boolean;
    /**
     * Background color of the canvas (default: 0xffffff), or if it should be transparent
     */
    backgroundColor?: number | 'transparent';
    /**
     * Whether to enable further debug information
     */
    debug?: boolean;
    /**
     * Only update the view when {@link SchematicHandles#render} is called. This is useful if you want to control the rendering yourself
     */
    disableAutoRender?: boolean;
}

Notes on Cors-Anywhere

Due to the way this works, it must have access to a Minecraft jar file. As redistribution of this file would be a breach of the license, and Mojang uses CORS on their download site, a cors-anywhere instance must be used to allow access to these jar files.

schematicwebviewer's People

Contributors

mcjeffr avatar me4502 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

schematicwebviewer's Issues

Properly handle different dataVersions

Due to Mojang renaming blocks every so often, schematics are fairly tied to specific jar files. The viewer should properly handle using the right data given the data version of the schematic.

This is semi-complex, as there does not appear to be an online dataVersion -> version registry.

Buffer is not defined

Hi, I have this error when I try to load my schem file :
Uncaught (in promise) ReferenceError: Buffer is not defined at parseNbt (utils.js:34:1) at Module.<anonymous> (renderer.js:103:1) at Generator.next (<anonymous>) at renderer.js:29:1 at new Promise (<anonymous>) at __awaiter (renderer.js:8:1) at Module.renderSchematic (renderer.js:59:1) at index.js:64:1 at commitHookEffectListMount (react-dom.development.js:19607:1) at commitPassiveHookEffects (react-dom.development.js:19644:1)

image

Rendered in Reverse

Versions

SchematicWebViewer - 4.2.0
Minecraft - 1.18.1
WorldEdit-Bukkit - 7.2.8

Issue

Schematics renedered with SchematicWebViewer are reversed.

How to Reproduce

Create Structure, Save as Schematic, Render with SchematicWebViewer.

The Code I Used

Schematic (Base64)
H4sIAAAAAAAA/5VT32vUQBCeZu9Xkl6rKIjok+CDcC++CvdgtWChxaLVWo8S9jaTy3JJNuzOeVf/d9HZ+9HcFYW6kIed78v3zczORBB+VjmWkrQSEJ3LAonwTC4A4DCC7jog4EmpK1R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VUJCaX6wi2KaFROVr7VnaMlhmwKe/0m3mcu5Q9QF3O49l472sP3tCniy1XudlicPBLwsS0yjuIlRsHN0FhZrjGx8Q2OVzl2LKlC/xHxMCVR9aF4hyUiSFODdnH+KY4t0uzzXm/XdL46t9XenDkzzEZ1zqAlofWCKjUbrTQO8M+fTpRk7gKOKqNbqjabnt8Rx+6SzidPObvx33eVX14P2gv88J0UKre8sV6D5XuvJtcqQPYAYaLtoE5dgGYIhZ3xHLSEe1C4IM7soNaFyEW3fTau7rALtNem1Jsmj7UPrM4aqQPvVYuQaUbtRkWtnC+rLDq1kXbzj5zH8Bi8t0e9rBQAA
JS Code
  renderSchematic(document.querySelector("#schem"), data, {
      size: 500,
      orbit: false,
      renderArrow: false,
      renderBars: false,
      corsBypassUrl: CORS_ANYWHERE,
      backgroundColor: "#000000"
  });

Pictures

In Game

In Game 1
In Game 2
In Game 3
In Game 4
Additional Blocks
In Game 5

SchematicWebViewer

SchematicWebViewer 1
SchematicWebViewer 2
SchematicWebViewer 3
Additional Blocks
SchematicWebViewer 4

Allow for enabling transparent canvas

Right now, the background color of the canvas is white. This is acceptable in most use-cases, though in some others it might be useful to set the background color to something that is not white. Use cases for this could be a dark-mode of a website that makes use of this library, or a shade of gray when on a card of some sort.

In order to deal with all possible cases, either one of the following options could work:

  • Allowing users to set the background color to something else than white
  • Allowing users to remove the background altogether, relying on the background of the component the canvas is rendered in
  • A combination of both

Since removing the background might affect performance, allowing the user to set their own background might be the best solution for performance. But if performance is not so important for the user, removing the background color might be a better solution.

I will see about creating a PR for this later this week.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.