Graphics with WASM

WASM Helper Function

(func $toPixel (export "toPixel") (param $x i32) (param $y i32) (param $size i32) (result i32)
        (i32.mul
            (i32.add
                (i32.mul 
                    (get_local $y)
                    (get_local $size)
                )
                (get_local $x)
            )
            (i32.const 4)
        )
    )

    (func $setPixel (export "setPixel") (param $x i32) (param $y i32)(param $c i32) (param $size i32)
        (i32.store
            (call $toPixel (get_local $x) (get_local $y) (get_local $size))
            (get_local $c)
        )
    )

    (func $getPixel (export "getPixel") (param $x i32)(param $y i32) (param $size i32) (result i32)
        (i32.load
            (call $toPixel (get_local $x) (get_local $y)(get_local $size))
        )
    )

WASM i32 to RGBA


        (local $r i32)
        (local $g i32)
        (local $b i32)
        (local $a i32)
                (i32.and
                        (call $getPixel (get_local $x)(get_local $y)(get_local $size)) 
                        (i32.const 255)
                    )
                    set_local $r
                    (i32.shr_u 
                        (i32.and
                            (call $getPixel (get_local $x)(get_local $y)(get_local $size)) 
                            (i32.const 65280)
                        )
                        (i32.const 8)
                    )
                    f32.convert_s/i32
                    (i32.shr_u 
                        (i32.and
                            (call $getPixel (get_local $x)(get_local $y)(get_local $size)) 
                            (i32.const 16711680)
                        )
                        (i32.const 16)
                    )
                    set_local $b
                    (i32.shr_u 
                        (i32.and
                            (call $getPixel (get_local $x)(get_local $y)(get_local $size)) 
                            (i32.const 4278190080)
                        )
                        (i32.const 24)
                    )
                    set_local $a

Javascript (draw to canvas)

var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
fetch('out2.wasm').then(response =>
    response.arrayBuffer()
  ).then(bytes =>
    WebAssembly.instantiate(bytes, importObject)
  ).then(results => {
    results.instance.exports.main();
    var buffer = memory.buffer.slice(0, size*size*4);
    const imgData = new ImageData(new Uint8ClampedArray(buffer), size, size);
    ctx.putImageData(imgData, 0, 0);
  });

Simple Unity Dungeon Maker Script

Untitled.png

This script is placed on a plane and creates a 15 by 15 grid of prefabs based on a csv-like file. It can be used to create a dungeon like map.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


[System.Serializable]
public class MultiDimensionalInt
{
    public int[] intArray;
}

public class MapMaker : MonoBehaviour { 
    public bool hideWall = false;
    private bool hist = false;
    public Transform player;
    public float disappearThresh;
    public GameObject[] walls;
    public float[] yoffset;
    public float[] xoffset;
    public float[] zoffset;

    public TextAsset mapData;
    public string[][] map = new string[15][];
    
    void Start()
    {
        string[] text = mapData.text.Split('\n');
        for (int i = 0; i < map.Length; i++)
        {
            map[i] = text[i].Split(',');
        }

        if (!hideWall)
            loadWalls();
    }
    
    void Update()
    {
        if (hideWall != hist)
        {
            if (hideWall)
            {
                unloadWalls();
            } else
            {
                loadWalls();
            }
            hist = hideWall;
        }

        if (Vector3.Distance(transform.position, player.position) > disappearThresh)
        {
            hideWall = true;
        } else if (Vector3.Distance(transform.position, player.position) < disappearThresh)
        {
            hideWall = false;
        }
    }

    void loadWalls()
    {
        for (int i = -14; i <= 14; i += 2)
        {
            for (int j = -14; j <= 14; j += 2)
            {
                int res = Convert.ToInt32(map[(i + 14) / 2][ (j + 14) / 2]);
                if (res != 0)
                {
                    int index = res - 1;
                    var wall = Instantiate(walls[index], transform.position + (new Vector3(i + xoffset[index], yoffset[index], j + zoffset[index])), Quaternion.identity);
                    wall.transform.parent = transform;
                }
            }
        }
    }

    void unloadWalls()
    {
        foreach (Transform child in transform)
        {
            Destroy(child.gameObject);
        }
    }
}