Some old voxel lighting code

If you look at some of the previous posts you will see this code or a variant of it in use. It may be useful to some. It is very much a hack and i hope to revisit it perhaps in c++ or java.

 public static void SpreadChunkLight(Chunk chunk)

        int x, y, z; byte l;

        using (new Timer("Spread Light"))
            while (chunk.lightSources.Count > 0)
                LightUnit lu = chunk.lightSources.Dequeue();
                x = lu.pos.X; y = lu.pos.Y; z = lu.pos.Z;
                l = lu.val;
                if (IsolatedSunlight(lu.pos.X, lu.pos.Y, lu.pos.Z)) { continue; }

                Spread(x - 1, y, z, l, chunk);
                Spread(x + 1, y, z, l, chunk);
                Spread(x, y + 1, z, l, chunk);
                Spread(x, y - 1, z, l, chunk);
                Spread(x, y, z + 1, l, chunk);
                Spread(x, y, z - 1, l, chunk);

        chunk.State = ChunkState.SpreadLight; chunk.working = false;


    static void Spread(int x, int y, int z, byte l, Chunk pchunk)

        if (l <2) { return; }
        if (World.IsOpaque(x, y, z)) { return; }

        Chunk chunk = World.GetChunkFromHit(x, y, z);
        if (chunk == null) { return; }
        byte blocklight = GetBlockLightIndex(x, y, z);
        if (blocklight > (byte)(l - 2)) { return; }

        if (World.loadedChunks >= World.totalChunks)
            if (chunk.X != pchunk.X || chunk.Y != pchunk.Y || chunk.Z != pchunk.Z)
             //   Debug.Log("light into chunk");

              //  if (!Pool.toCheckState.Contains(chunk) && chunk.State == ChunkState.Render) { chunk.working = false; chunk.State = ChunkState.SpreadLight; Pool.toCheckState.Enqueue(chunk); }

        l = (byte)(l - 1);

        CoreLighting.SetBlockLight(x, y, z, l);

        Spread(x - 1, y, z, l, pchunk);
        Spread(x + 1, y, z, l, pchunk);
        Spread(x, y + 1, z, l, pchunk);
        Spread(x, y - 1, z, l, pchunk);
        Spread(x, y, z + 1, l, pchunk);
        Spread(x, y, z - 1, l, pchunk);


Lighting Test

Rewrote ambient occlusion and built a light spreading system.
This is a long way from NVIDIA’s new global illumination/maxwell ‘voxel’ based lighting i read about recently but I’m gradually improving the light system for this unity engine.

I still need to build a more efficient way to update chunks that have had light changed due to light spreading from another chunk.

I use a system of stacks (all chunks on vertical axis) and chunks. When a block is placed or removed the stack should have light recalculated and mesh rebuilt. If light values change in chunks not within this stack, then those chunks should be rebuilt. I’m probably still missing a few things but I’ll keep working on it.

Unity 5 Voxel Engine Update – Photon Multiplayer

more stuff here. more stuff to do. stuff..n stuff..

1. Implemented multiplayer chat with Photon from exit games. I’m not looking to implement full multiplayer yet but perhaps a town or village where people can meet in between battles. It will be easier to learn Photon this way and also give me a chance to clean up the code a bit.

2. Added a Shakespearean random name generator.

3. Rewrote mesh distortion, vastly simplified version. Still needs some adjustment.

TODO: debug everything, re-write lighting code, add new mobs, npcs

Unity Drag & Drop Slots-Inventory

Many of you seasoned unity pros out there are probably face-palming as you look at how I’ve approached this, but this is the method that is working for me at this point. I’m sure it will be revised in the future.

The script below is attached to all of my quickslot UI images. I use an extra “phantom” slot (dragImage & dragText) to play the part of the slot that is being dragged. so the actual slots or slot items don’t move, the slot data is just passed behind the scenes. If you have any q’s  you can contact me otherwise, i hope this is useful in some way.

Also, check out this tutorial for a good run down on a variation of the code below. It’s the tutorial that clued me into this method, also check the unity forums for some useful unity 4.6 > 5 info. (which is where i got the UI raycasting stuff.)

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragHandler : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler {

    public GameObject dragObject;
    public Image dragImage;
    public Sprite emptyImage;
    public Text dragText;
    public int slot;
    SlotManager sm;
    Vector3 startPos;

	void Start () {

        dragObject = GameObject.Find("Slot_Drag");
        dragImage = GameObject.Find("Slot_Drag_Image").GetComponent();
        dragText = GameObject.Find("Slot_Drag_Counter").GetComponent();

        sm = GameObject.Find("InventoryUIManager").GetComponent();

    public void OnBeginDrag(PointerEventData eventData)
        dragImage.overrideSprite = sm.GetSlotSprite(slot);
        dragText.text = sm.GetSlotCountText(slot);
        startPos = dragObject.transform.position;

        sm.slotImages[slot].GetComponent().overrideSprite = emptyImage;
        sm.slotCounters[slot].GetComponent().text = "";


    public void OnDrag(PointerEventData eventData)
        Vector3 m = Input.mousePosition;
        dragObject.transform.position = new Vector3(m.x, m.y, dragObject.transform.position.z);

    public void OnEndDrag(PointerEventData eventData)
        dragObject.transform.position = startPos;

        dragImage.overrideSprite = emptyImage;
        dragText.text = "";

    void CheckDrop() 
        int dropSlot=-1;
        PointerEventData pointer = new PointerEventData(EventSystem.current);
        pointer.position = dragObject.transform.position;
        List raycastResults = new List();
        EventSystem.current.RaycastAll(pointer, raycastResults);

        if (raycastResults.Count > 2)
            string slotname = raycastResults[2];

            if (slotname.IndexOf("Slot_01") > -1) { dropSlot = 1; }
            if (slotname.IndexOf("Slot_02") > -1) { dropSlot = 2; }
            if (slotname.IndexOf("Slot_03") > -1) { dropSlot = 3; }
            if (slotname.IndexOf("Slot_04") > -1) { dropSlot = 4; }
            if (slotname.IndexOf("Slot_05") > -1) { dropSlot = 5; }
            if (slotname.IndexOf("Slot_06") > -1) { dropSlot = 6; }
            if (slotname.IndexOf("Slot_07") > -1) { dropSlot = 7; }
            if (slotname.IndexOf("Slot_08") > -1) { dropSlot = 8; }
            if (slotname.IndexOf("Slot_09") > -1) { dropSlot = 9; }

        if (dropSlot != -1) 



Unity Custom Particle System – Test #1

Below are the functions I’m using to set scale and color. This is the first attempt. It seems to work OK but perhaps it can be improved on;

void SetColor()
new Color(Mathf.Lerp(startColor.r, endColor.r, lifeCounter / life), Mathf.Lerp(startColor.g, endColor.g, lifeCounter / life), Mathf.Lerp(startColor.b, endColor.b, lifeCounter / life), Mathf.Lerp(startColor.a, endColor.a, lifeCounter / life)));

void SetSize()
transform.localScale =
new Vector3(Mathf.Lerp(startSize.x, endSize.x, lifeCounter / life),
Mathf.Lerp(startSize.y, endSize.y, lifeCounter / life),
Mathf.Lerp(startSize.z, endSize.z, lifeCounter / life));

unity3d voxel game engine test #6 (Zombies Part.2)

I really need to keep a change log or something for this. Created a bunch of zombie textures and more models in blender. Added female and fat zombies with differing, xp, damage and move speeds. Recorded a bunch of sound effects and music. Fixed up some very very basic movement AI for mobs. They can now jump. Still no path finding. Added some more UI elements, power bar beside cross-hair, health bar for mobs, gold coins, exploding limbs, better decapitation and more. There is a smallish memory leak on loading new level. Otherwise happy so far.


unity3d voxel game engine test #5 (Zombies)

Added some more game elements to play with some ideas .. including headshot instakill on mobs with x2 xp. A lot more stuff to do yet. I’d made the zombie last year in blender so it’s the resident test mob for now. The zombie (boxy) model is compatible with minecraft skins so i should be able to use the existing skin editors to make a plethora of boxy type characters,npc etc though i may end up steering away from that style.

Blender Gems

I found a great tutorial for setting up blender to render gemstones. The image below took about 1.5hrs to render but I’m pretty happy with the result. I wanted to make my own gem icon so this came in very handy. It may be useful for some other game project related art as well.


If you’d like to try this yourself check out the tutorial by Addicted to CG here.


Thanks to the people that have expressed some interest in this project. I would like to clarify a few things for any future visitors.

This engine is not for sale. I am trying to create an engine to build my own game the way i want it. Most engines i have bought have had things missing or I’ve not understood the authors methodology so well to be able to modify it to my liking. This is not a fault of the various authors.

I’m happy to share some bits of advice and code. But i won’t hand over the entire project. There are a few great partially completed projects i did use to get my toes wet so please look for those on the unity forums, especially  mine package  This project got me started. If you want to understand blocky voxel engines, rip that project apart and see what’s going on. Then write your own from scratch, over and over again.

If you’re interested in joining this project as an artist, coder or musician. Send an email or leave a comment.