npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aframe-tilemap

v0.2.1

Published

Tilemap implementations in A-Frame using object cloning, merging, and instancing

Downloads

4

Readme

aframe-tilemap

A set of A-Frame components that render a static tilemap from a specially coded image source and a set of reference entities. It includes three components with different underlying implementations: tilemap-cloned, tilemap-merged, and tilemap-instanced.

Usage

<a-scene>
  <a-assets>
    <a-asset-item id="milkTruck" src="assets/CesiumMilkTruck.glb"></a-asset-item>
    <img id="tilemap-image" src="maps/tilemap-128px.png">
  </a-assets>
  <a-entity tilemap-instanced="src: #tilemap-image" position="0 0 -20">
    <a-entity tile="id: 1" geometry="primitive: box" visible="false" scale="1 1 3"></a-entity>
    <a-entity tile="id: 2; readyEvent: model-loaded" gltf-model="#milkTruck" visible="false" scale="0.5 0.5 0.5"></a-entity>
    <a-entity tile="id: 3" geometry="primitive: sphere" visible="false"></a-entity>
  </a-entity>
  <a-sky color="#333"></a-sky>
</a-scene>

API

Tilemaps are specified using a combination of tilemap-* and tile components. An entity with the tilemap-* component represents the tilemap itself, and should be transformed and attached to the desired part of the scene graph.

Entities that are direct children of the tilemap-* can include the tile component to indicate that they represent one of the tile IDs that can be specified in the tilemap. Their transformation relative to the origin of the tilemap is replicated in each of their instances.

Usually, these tile entities should also have the component visible="false".

Tilemap Properties

| Property | Type | Description | Default Value | | ---------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- | | src | asset | An image asset from which to derive the tile values | | | tileWidth | number | Width of each tile in scene units | 1 | | tileHeight | number | Height of each tile in scene units | 1 | | origin | vec2 | Origin of the tilemap in normalized image coordinates. A value of (0,0) will render a tilemap whose entity center is the top-left corner, while a value of (1,1) will center the entity at the bottom right | {x:0.5,y:0.5} | | debug | boolean | Enables additional debug printing to console | false |

Tilemap Events

| Property | Description | | ------------ | ---------------------------------------------------------------------------------------------- | | model-loaded | Tilemap completed loading.This emulates the behavior of other model loaders in A-Frame. |

Tile Properties

| Property | Type | Description | | ---------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | id | int | The ID value in the tilemap that this entity represents | | readyEvent | string | If specified, the tilemap will wait for this entity to emit this event before loading. This is needed for tiles that load assets (e.g. GLTF models).For most A-Frame loaders, listen for the event model-loaded. |

Tilemap Image Format

| Example: Square 128px Randomized-ID Tilemap | | -------------------------------------------------------------------------------------------------------- | | tilemap | | IDs: random, 0 to 3 | | Rotation: 0 to 360 deg from left to right |

The image tilemap that determines where each tile is rendered contains only two components: ID and Rotation. Images are expected to contain one byte per channel of either RGB or RGBA information.

| Channel | Size | Description | | ------------- | -------- | ---------------------------------------------- | | R (Red) | 1 byte | HIGH byte of the tile ID | | G (Green) | 1 byte | LOW byte of the tile ID | | B (Blue) | 1 byte | Normalized rotation (0 = 0°, 255 = 358.5°) | | A (Alpha) | 1 byte | Unused |

The equations for determining the tile offets from the pixel value of the image are:

TILE_ID    = 256 * pixel.R + pixel.G
TILE_THETA = PI / 128.0 * pixel.B

Each pixel of the image represents one tile, with each column spaced at tileWidth intervals and each row spaced at tileHeight intervals.

Implementations

Cloned Tilemaps: tilemap-cloned

In the cloned tilemap, each tile location in the map is rendered by cloning the THREE.Object3D represented in the reference tile with the matching ID.

This approach exactly reuses the THREE.Geometry and THREE.Material associated with the reference tile, so it will not interfere with shaders or other complex models. However, it is the least performant, as each tile is handled as a completely different node in the scene graph.

Merged Tilemaps: tilemap-merged

In the merged tilemap, each reference tile is decomposed into a list of reference meshes. The geometry of each of these meshes is then duplicated at each matching tile location, creating a single merged mesh that contains geometry for all instances of the reference tile.

This method is much more performant than cloning, because each reference tile mesh simply pushes one large geometry buffer to the GPU. However, it is very memory intensive, because it must maintain the full geometric representation of every tile location in memory.

Instanced Tilemaps: tilemap-instanced

In the instanced tilemap, each reference tile is pushed to the GPU, where a vertex shader is applied to each child mesh that instead renders it at each tile location.

This is the most performant of the three methods, as it only pushes each reference tile to the GPU once, and it also only needs a small amount of memory to maintain a buffer of tile locations to which each tile will be rendered.

One downside of this approach is that it requires the direct insertion of a vertex shader into the material that is used to render the object. This limits the material options for these objects. The implementation tries to replicate the functionality of standard THREE source materials as best it can, but some effects will not work properly, and all vertex shader effects are removed.

Another downside is that the rendered instances are not available outside the GPU for other applications, such as raycasts or other physical checks. For applications that require those, cloned or merged tilemaps may be a better choice.

License

This library is free software and is distributed under an MIT License.