prismarine-viewer

repository·master·Indexed 18 days ago

https://github.com/prismarinejs/prismarine-viewer

A web-based visualization tool for Minecraft servers and bots (version 1.33.0). It allows developers to view worlds, bot movements, and environments in a browser. The library includes specialized modules for Mineflayer bot visualization, standalone world viewing, and headless rendering for recording video or streaming via RTMP and TCP. It features a Viewer class based on three.js for rendering, WorldView for camera and chunk management, and capabilities to draw geometric primitives like lines and boxes in the viewer.

Tokens
8.6K
Snippets
37
Records
45
Agent score
64%

What's inside prismarine-viewer

  1. Use the exporter to generate screenshots and 3D models

    master

    The exporter package provides examples for transforming world parts into screenshots and 3D models.

    To use these examples:

    1. Navigate to the examples/exporter directory.
    2. Install dependencies using npm install.
    3. Run the screenshot generator with node screenshot.js.
    4. Run the 3D model generator with node 3dmodel.js.

    Generated 3D files can be viewed using an external glTF viewer such as https://gltf.insimo.com/.

    npm install
    node screenshot.js
    node 3dmodel.js
  2. How the standalone viewer manages chunk updates

    master

    The standalone viewer uses a push-based model for chunk synchronization. When a client connects via Socket.io, the server immediately sends the current version, the initial position (based on the center config), and all chunks within the viewDistance radius around the center.

    To force the server to re-broadcast chunks to all currently connected clients (for example, if the world state has changed significantly), call the viewer.update() method. This method iterates through the chunk grid defined by the viewDistance and center and emits the loadChunk event for each chunk to all active sockets.

  3. Attach a viewer to a Mineflayer bot

    master

    You can attach a web-based viewer to a Mineflayer bot by calling the plugin entrypoint. This starts an Express server and a Socket.io instance that streams the bot's world and position to connected web clients.

    Options:

    • viewDistance (number, default: 6): The distance from the bot at which blocks are rendered.
    • firstPerson (boolean, default: false): If true, the viewer will include the bot's pitch in position updates.
    • port (number, default: 3000): The port on which the web server will run.
    • prefix (string, default: ''): A URL prefix for the socket.io path.
    const mineflayer = require('mineflayer')
    const viewer = require('prismarine-viewer')
    
    const bot = mineflayer.createBot({
      // ... bot config
    })
    
    // Attach the viewer
    viewer(bot, {
      viewDistance: 10,
      port: 8080,
      firstPerson: true
    })
  4. Visualize a Mineflayer bot

    master

    You can use the mineflayer module from prismarine-viewer to serve a webserver that allows you to visualize a Mineflayer bot in first or third person. This includes drawing capabilities to visualize paths or other data.

    const mineflayer = require('mineflayer')
    const mineflayerViewer = require('prismarine-viewer').mineflayer
    
    const bot = mineflayer.createBot({
      username: 'Bot'
    })
    
    bot.once('spawn', () => {
      // Start the viewing server on port 3000
      mineflayerViewer(bot, { port: 3000 })
    
      // Example: Draw the path followed by the bot
      const path = [bot.entity.position.clone()]
      bot.on('move', () => {
        if (path[path.length - 1].distanceTo(bot.entity.position) > 1) {
          path.push(bot.entity.position.clone())
          bot.viewer.drawLine('path', path)
        }
      })
    })
  5. Use WorldView to manage camera and chunk visibility

    master

    WorldView represents the world from a specific player or camera perspective. It manages which chunks are loaded based on a viewDistance and a central position.

    Constructor: WorldView(world, viewDistance, position = new Vec3(0, 0, 0), emitter = null)

    • world: A prismarine-world instance.
    • viewDistance: Number of chunks to consider.
    • position: The camera's position.
    • emitter: An event emitter to connect to (can be null).

    Key Methods:

    • listenToBot(bot): Automatically listens to events from a Mineflayer bot.
    • removeListenersFromBot(bot): Stops listening to the specified bot.
    • init(pos): Starts emitting chunks starting from the given position.
    • updatePosition(pos): Changes the camera position and triggers corresponding chunk load/unload events.
  6. Sync world updates using Viewer.listen()

    master

    The listen(emitter) method allows the Viewer to automatically react to world changes. The provided emitter must emit the following events:

    • entity(e): Updates an entity (expects a prismarine-entity).
    • primitive(p): Updates a Three.js primitive.
    • loadChunk({x, z, chunk}): Adds a chunk column (expects a prismarine-chunk).
    • unloadChunk({x, z}): Removes a chunk column.
    • blockUpdate({pos, stateId}): Updates a block at a specific position.

    The Viewer also listens for mouseClick({ origin, direction, button }) events from the emitter.

  7. Use the mineflayer module for bot visualization

    master

    The mineflayer entry point serves a webserver to visualize a bot's surroundings.

    Import:

    const { mineflayer } = require('prismarine-viewer')

    Options:

    OptionDescriptionDefault
    viewDistanceView radius in chunks6
    firstPersonWhether to use first-person viewfalse
    portPort for the webserver3000
  8. Use the standalone module to visualize a world

    master

    The standalone entry point serves a webserver to visualize a Minecraft world without requiring a bot.

    Import:

    const { standalone } = require('prismarine-viewer')

    Options:

    OptionDescriptionDefault
    versionMinecraft version to use1.13.2
    generatorA world generator function (x, y, z) => number(x, y, z) => 0
    centerA vec3 to center the view onnew Vec3(0, 0, 0)
    viewDistanceView radius in chunks6
    portPort for the webserver3000