-
Notifications
You must be signed in to change notification settings - Fork 0
SceneMap class
A scene map in TorchRay is sourced from and processed as an ImageData object. This makes level editing and creation very simple — you can just use your favorite graphics program! — and also makes the API intuitive for a JavaScript developer to interface with. Images should be in a lossless format that supports alpha transparency, such as PNG.
Each pixel in the image represents a wall cube in the game world, and is the basic unit of the world map. (The actual visible width and height of walls is determined by the renderer). If the alpha value for a pixel is 255 (fully opaque), that pixel is considered to be part of a wall cube. Otherwise, it represents empty space.
The red value of each pixel is used as a key for looking up which texture to render that wall as (the texture images are registered at runtime and are separate from the scene map). If this does not correspond to a texture, then the RGB values of the pixel are used as a solid color to render the wall.
let map = new SceneMap(ImageData imgData)
Returns a new SceneMap object with the given imgData. However, for convenience, the recommended method for constructing a SceneMap is through the fromImage static method.
map.isWall(Vector pos) -> Boolean
Returns true if the map position specified by the x and y properties of pos represents a wall (as opposed to empty space), and false otherwise. Indexing starts from zero. If pos is not within the boundaries of the scene map, false is returned.
map.colorAt(Vector pos) -> Color
Returns the color of the wall at the map position specified by the x and y properties of pos. This only has meaning if there is actually a wall at that position; see the isWall method. Also note that the actual appearance of the wall may be different based on renderer effects such as lighting, shading, textures, etc.
map.within(Vector pos) -> Boolean
Returns true if the position specified by the x and y properties of pos is within the rectangular boundaries of the map, and false otherwise.
SceneMap.fromImage(CanvasImageSource image) -> SceneMap
Constructs a new SceneMap from the image data of image and returns it. CanvasImageSource is not a real type; instead, it accepts any of HTMLImageElement, HTMLVideoElement, HTMLCanvasElement, or ImageBitmap.
Important: The resource behind the image must come from the same origin (or you must have access to that resource using CORS). If it is a canvas, the canvas must not have ever had any foreign-origin image drawn on it. These are security restrictions imposed by the browser.