JavaScript 3D ray casting: Chrome is the most efficient browser
Tags: javascript, raycasting, chrome, webdev
Published: 2024-05-31 23:16:06

### Links:
- [Play the Game](https://raycastingjs.web.app/)
- [GitHub Repository](https://github.com/coddicat/ts3d)

>
![Game scene](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a4eyxhtla1wnadftv17.png)![Game scene](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7plxb1suqju2m6xslgue.png)![Game scene](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvqyt3iqnsjf6bv5zlp2.png)

## Introduction
As a full-stack developer, I constantly seek out tasks and projects to keep my skills sharp and my curiosity satisfied. My latest adventure took me back to the 90s, to the world of 3D ray casting. Inspired by classics like Wolfenstein 3D, I set out to implement this technique in pure JavaScript, without relying on the 3D functions of the canvas context. This project turned into a fascinating journey through the intricacies of game development and browser performance.

![raycasting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlavc9r78ta3ywrf00q1.jpg)

## The Challenge
Ray casting in the style of early 90s games involves casting rays from the player’s perspective, one for each pixel horizontally across the screen. The rays move in intervals of one tile, creating a 3D effect. Despite having no background in game development, I was eager to tackle challenges such as sprite rendering, player movement, and interaction with the environment and moving objects.
>
![raycasting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6ho4caq35cdotogo3c2.jpg)![raycasting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ohro1f8vpp2l2ro2bwk.gif)

## Why JavaScript?
I chose to implement the project using pure JavaScript to see if modern browsers and computers could handle the intensive processing required for rendering game scenes. This approach meant manually processing and rendering each pixel, without relying on built-in 3D rendering functions. The question at the heart of the project was: Can modern web technology deliver acceptable performance for such a demanding task?

```ts
    const ctx = canvas.getContext('2d', {
      alpha: true,
      willReadFrequently: true
    });
```

## The Journey
What began as a simple curiosity-driven project quickly grew into a full-fledged endeavor, consuming six months of my time. The challenge of optimizing performance was particularly engaging. I frequently revisited my code, eliminating redundant operations and optimizing calculations, especially those involving trigonometric functions. For instance, I used bitwise shifts instead of multiplying or dividing by 2, and bitwise |0 instead of rounding or using Math.floor(), to speed up computations.

## Pixel-Level Control
For manipulating pixels, I used Uint32Array, which allowed me to write pixel states directly by index. This approach was effective because three bits represent color, and the fourth bit is alpha, enabling adjustments in pixel brightness and marking screen areas as occupied. This level of control was crucial for achieving the desired visual effects and performance.

```ts
const buf = new ArrayBuffer(height * width * 4);
const buf8 = new Uint8ClampedArray(buf);
const data = new Uint32Array(buf);

...

const alphaMask = 0x00ffffff | (light << 24);
const pixel = textureImageData[textureIndex];
data[dataIndex] = pixel & alphaMask;
```

## Overcoming Mathematical Hurdles
One of the most daunting aspects was finding solutions and formulas for rendering horizontal surfaces such as floors, ceilings, and various beams. Without a background in mathematics or game development, this part of the project was particularly time-consuming. However, the result was a visually interesting and acceptable image that met my performance goals.

```ts
//// This's just an example. The actual code has been improved to reduce the number of calculations.
public getTileSpriteDataIndexBySideX_positive(
    ray: Ray,
    offset: number,
    textureData: TextureData
  ): number {
    const { width, height } = textureData;
    const fixSinAbs = Math.abs(Math.sin(ray.angle)) / ray.fixDistance;
    const factY = textureData.height * rayAngle.fixSinAbs
    const diff = Math.abs(ray.fixedDistance - ray.distance);
    const fixedCos = Math.cos(ray.angle) / ray.fixDistance;
    const fixedCosDiff = fixedCos * diff;
    const offsetX = offset - fixedCosDiff + (fixedCosDiff | 0) + 1;
    const spriteOffsetX = ((offsetX - (offsetX | 0)) * width) | 0;
    const spriteOffsetY = (diff * factY) | 0;
    const fixedX = height - mod(spriteOffsetY, height) - 1;
    return Math.imul(fixedX, width) + spriteOffsetX;
  }
```

## Not Just JavaScript
Although I initially aimed to use pure JavaScript, I eventually incorporated TypeScript to manage complex code structures more effectively. Additionally, I used the Vue framework to bundle everything together, ensuring a smoother development process and more maintainable codebase.

```ts
export type Tile = {
  bottom: number;
  texture?: Texture;
  name?: string;
};

export type Wall = {
  public top: number;
  public bottom: number;
  public texture?: Texture;
  public name?: string;
};

export type MapItem = {
  walls: Wall[];
  tiles: Tile[];  
  mirror?: boolean;
  stopRay: boolean;
}
```

## Performance Insights
The project also served as an insightful browser performance test. On my laptop with an i7–10510U processor, Chrome emerged as the most efficient browser, followed by Edge, Firefox, and Safari. These findings highlighted the varying capabilities of different browsers when handling intensive JavaScript computations.
> FPS. 1920x1080. Chrome
![Chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1emn0u1omhat8uv3mjlc.png)

> FPS. 1920x1080. Edge
![Edge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufb9twy1r9shiflyk9x5.png)

> FPS. 1920x1080. Firefox
![Firefox](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkm8vprwanw5rg12elt4.png)

## Future Prospects
While the current version of the game is a proof of concept, I have plans to enhance its playability. Future updates may include adding player goals, sound effects, and even multiplayer capabilities. The project has provided a solid foundation for exploring these possibilities.

## Conclusion
This journey into 90s 3D ray casting has been both challenging and rewarding. It started as a pet project driven by curiosity and evolved into a deep dive into classic game programming techniques and modern web performance. I invite you to check out the game and explore its source code. Your feedback and contributions are welcome as I continue to refine and expand this project.

Links:
- [Play the Game](https://raycastingjs.web.app/)
- [GitHub Repository](https://github.com/coddicat/ts3d)

Thank you for reading, and happy coding!

{% embed https://www.youtube.com/embed/4Bktj-XoUHs?si=PrOu6SteMhEHfRK1 %}