donderdag 7 april 2016

Volumetric light cones

I want to have a volumetric light beam on my spotlights so you see an actual light beam in game. This is a very important element for a project I’m working on for school. The atmosphere of the game I’m developing is about the dark and the spotlights.

Unity does not have built in volumetric light effects except for Sun Shafts. The Sun Shaft effect looks good but only supports 1 light source.

The alternatives I found are:

Volumetric Light Beam Kit

This is a $40 Unity package that does create the desired effect with a cone mesh and a shader. However this package has been deprecated and cannot be purchased anymore
https://www.assetstore.unity3d.com/en/#!/content/3559 


Sunshine!

This must be the best looking lighting effect that you can buy at the asset store, however it comes with a price tag of $95. This is way too expensive for a student project. https://www.assetstore.unity3d.com/en/#!/content/10153

Light shafeds

This was the best solution that I could find. It’s a public domain package that creates the exact effect I was looking for.
https://github.com/robertcupisz/LightShafts

The only problem is that this effect stacks with multiple beams so it still is not the volumetric effect that I’m looking for.

Light shafeds effect stacking.

Unity announced a volumetric light effect that will release in June 2016 with the Unity 5.4 release. I think I will wait for that since it’s a really complex effect and I don't feel experienced enough to build my own volumetric light effect.

Interacting with games through a website

For a small art project I was asked to create a website that is able to interact with a game. The assignment was to develop a web form that users can fill to submit a message to the game. The information that has been sent to the game becomes part of the game.

The game, is a Tamagotchi, fed by people sending food through a website. You also can  add a message when sending the food and the Tamagotchi will react to it.

I’ve created a simple PHP based service that adds “food” to a queue in a database and another service to request the content of this queue. This service returns a Json object on request that is de-serialized in Unity by using LitJson. I was not able to use the build in Json serializer of Unity, it didn’t accept an array of objects for some reason.


GetFood.cs


using UnityEngine;
using System.Collections;
using LitJson;

public class LucyGetsFood : MonoBehaviour {
    private string _URL = "url to a website/service.php";

 public void GetFood()
 {
  StartCoroutine(GetFood());
 }

 IEnumerator GetFood() {
  WWW www = new WWW(_URL);
  yield return www;
        string jsonString = System.Text.ASCIIEncoding.ASCII.GetString(www.bytes).TrimEnd('\0');
        JsonData inputList = JsonMapper.ToObject(jsonString);

        if(inputList["inputList"].Count > 0)
        {
            print(inputList["inputList"][0]["name"].ToString());
            for(int i = 0; inputList["inputList"].Count > i; i++)
            {
                string name = inputList["inputList"][i]["name"].ToString();
                string color = inputList["inputList"][i]["color"].ToString();
                string message = inputList["inputList"][i]["message"].ToString();

                SpwanFood(name, color, message);
            }
        }
 }
}

I have worked with Json before so I thought that is would be done in no time. But it turned out to be different. I did not expect the difficulties I encountered with the build in Unity Json serializer. It was interesting to work with Json for Unity and I think it is valuable knowledge for the future. I would like to learn more about web-database based user information for things like users save games and high scores.
This project was a good experience to be better prepared to properly perform other tasks related to games interacting with web databases.

zondag 13 maart 2016

Directional Color Shader


I wanted to replicate an effect that a friend made with directional light in Unity. The goal was to make a shader that colors an object based on its normal direction in world space. 

DirectionalColor.shader

Shader "Custom/DirectionalColor" {
    Properties {
        _ColorX ("ColorX", Color) = (1,0,0,1)
        _ColorY ("ColorY", Color) = (0,1,0,1)
        _ColorZ ("ColorZ", Color) = (0,0,1,1)
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define UNITY_PASS_FORWARDBASE
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2
            #pragma target 3.0

            uniform float4 _ColorX;
            uniform float4 _ColorY;
            uniform float4 _ColorZ;

            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct VertexOutput {
                float4 pos : SV_POSITION;
                float3 normalDir : TEXCOORD0;
            };

            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.normalDir = UnityObjectToWorldNormal(v.normal);
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
                return o;
            }

            float4 frag(VertexOutput i) : COLOR {
                i.normalDir = normalize(i.normalDir);
                float3 normalDirection = i.normalDir;
                float3 squared = (i.normalDir*i.normalDir);
                float3 finalColor = (squared.r*_ColorX.rgb + squared.g*_ColorY.rgb + squared.b*_ColorZ.rgb);
                return fixed4(finalColor,1);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}