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;
        chunk.StateCheck();

    }

    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); }
            }
        }

        lightspreadcount++;
        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 dev@nuzly.com. 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)
    {
        CheckDrop();
      
        dragObject.transform.position = startPos;
        sm.UpdateSlots();

        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].gameObject.name;

            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) 
        {
            sm.SetDragFrom(slot);
            sm.SetDragTo(dropSlot-1);

        }

        Debug.Log(dropSlot);
    }
}

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()
{
mat.SetColor("_TintColor",
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.