Saturday 27 February 2016

AI Koi fish behaviour

I was watching 3 Koi fish in aquarium for quite long time and found out that fish behavior is not simple.I was thinking before that fish have a smallest amount of neurons, so their behavior is pretty random. But thats not true with Koi. This behavior can be used in video games or simulations. Maybe some day i will made it with unity or you can use it for any underwater creatures.

So what parameters fish have:

sex - male and female
activity - passive, active
long time memory
mood
hungry

What they can do:

- Swim slowly ( maybe random I don't figured it out )
- Swim fast
- Sleep at bottom
- Eat
- Find food
- Find food on bottom
- Male follow female with high speed

How parameters affect actions:
male follow female periodically
active male follow female, passive not
passive swim slowly most
they remember the most often place of feeding and at time of big hungry swim to that place
if they have bad mood ( was damaged ) they swim slowly most
if they are not hungry they not looking for food
Sleep when its night time
Active are much bigger

This actions can be triggered by neural network or just randomly in simplified AI model.

Thursday 25 February 2016

Interesting procedural generation

Procedural generation is common thing in computer industry. Procedural terrains, procedural quests, procedural weapon. But one thing that have all procedural things - they are boring. Boring terrains, boring quests, boring weapon. And it obvious, because any procedural generation have Gaussian distribution law that boring too. They only way to make procedural things interesting apply some rules like generated fractals, cool terrains with erosion. But anyway they boring as boring rules. But why this world, this life is not so boring ( commonly ) because rules applied many times and they are very balanced, that not destroying whole system just modify it. I found it when make some Neural Network Animation. This rules in real life - Physics, they applied at any very small part of the world and affect big huge connected structures. Creating such rules is not easy stuff they should be balanced, so creating random things like weapon or room in dungeon crawler is a same work as premade prefabs. Balance :)

Monday 22 February 2016

Re-Thinking 3D

When I started 3D ( modeling, texturing ) etc. I thought that mesh makes the most visual impact. I was trying hard in ZBrush etc., when making rocks and stuff like this. Then make high detailded 3d terrains. But they was not so good. I leaved 3D later.

During making of Skyship Aurora we make sattelite based terrains and I found out that not detailed shape of heightmap is not a big problem. Seriousle the most important thing is texture because of lack of real lighting in the game we can use just use high detailed prebaked textures. I turned back to the laser scanned images and found out that time a lot of models now available at Sketchfab. I found this and understood that even grass can be look realistic withour actual grass just a texture:

Sunday 21 February 2016

Skyship Aurora Make it huge




One of the special thing about Skyship Aurora - huge size. I mean ship really big and terrain is huge, you fly at 7km altitude . Of course Unity is mostly madden for middle size games like FPS, racer games, 2d platformers. We don't want to scale down whole game like most people do when they making space games with actual not realistic size, multiplied by 0.01. In such scaled world people feel difference specially if game use real physics like Aurora do. Also camera perspective correction make toy effect. So just do it as is.

First big problem was terrain. Huge large scale terrain was a big performance problem of the game. We tried to use repeated built-in terrain to cover the ground, because at 7 km you see really a lot of the things. Anyway it adds about 400 draw calls that was not awesome, also it looks blurry and repeatedly. Solution was to use mesh terrains with a high detailed texture and global normal map. At least it was much more faster and not blurry.

Problem was there is no repeated terrain on the market to use, so combining them together looks with seems and wrong lighting. Also we don't understand how it should looks like ( scale ) at 7km, so result work was also unrealistic. The cool solution is to use real satellite images and heightmap data. We got understand that at 7km altitude visual look of terrain is much more depends on texture, but not the heightmap.

Second problem was a speed. It is about 1200km per/hour ship movement. Also ship was a real rigidbody that moved by adding force to it. Camera moved with the ship. We noticed some glitches and spent some time discovering what was wrong. Some code was not in FixedUpdate and also we used interpolations on some physics objects. So turn off interpolation because we can't synchronize time for different interpolated physics bodies, and move every things into FixedUpdate. At low speed you sometimes will never notice it.

Third problem that everything looks slowly. Yeahh we are at 1200km/h but looks like we stand at one point. The reason that the human eye have no object to determine actual speed. So if you want to game high speed movement feeling you should add small objects close to the camera. If you travel by the car and look at the highway you will feel the speed, but if you look at the sky you feel nothing, except dramatic universe hugeness :). We  fix it with particles added to the ship that moves to you with high speed.

Fourth problem is the gun positioning. At low distances you can shoot according to camera forward vector and hit the target. But at long distances you should correct your weapon according to distance to the object. So we raycasted to the target and position each gun individually ( we have a lot of them ) to hit the target.

Fifth problem was limit of  Unity world. I mean Unity use float to describe game coordinates, so with huge coordinate value you will get an error during render and at some point you can't move at all. So we make -80 km to 80 km movement about 20 minutes level fly. If we take bigger coordinate we will get warning.



Thursday 18 February 2016

Skyship Aurora Store Data Tutorial



Sources of custom PlayerPrefs and example how it to use you can DOWNLOAD here:
https://www.dropbox.com/s/2t1jwiwb1zjbl3d/StoringTutorial.zip?dl=0

Common problem of many games is storing data. Scores, health, screen resolution, huge world of Skyrim everything you need to store somewhere or somehow. Unity have buit-in cross platfrom thing called PlayerPrefs, Everything ok with them except they not working on Xbox and you could not save custom data types with them. Of course you can use System.IO for serializing custom classes into a file or upload them on server, but System.IO is not working on Windows Phone. If you dont want to make your code full of #if define symbols mess you need something universal. So what our needs:

1. Class that will store data
2. Able to save/load at any part of the code
3. Working on PC/Console/Webplayer/Mobile

So the only way is to write custom PlayerPrefs that can be serialized into various data structures like xml, binary etc.  Also we need a singleton witch we can access at any part of the code like StoreData.Instance.SetInt("MyInt", 10 ). We need to attach such singleton to a gameobject at very first scene of the game and mark them as DontDestroyOnLoad cuz loading new scene will destroy any gameobject and data associated with them. So at very first scene at Awake function we need to load data for example from Steam cloud ( but as for me I recommend to save data into a file and mark them synchronized with cloud in steamworks panel) , wait til everything will be loaded from xbox storage container etc, then prompt Press any key to continue. Process input and continue the game. At least everything platform related things  will be in one place, so you can control bugs easily and separate game logic from system things. It is a very good idea to separate everything from game logic like input, save/load, achievements etc.

Huge plus of such custom player prefs that you can fast rebuilt game that was madden on PlayerPrefs into you custom PlayerPrefs. I also not recommend to call your custom PlayerPrefs like GamePrefs or anything else with prefix Game cuz in the future you will use GameObject much more often then GamePrefs so you need to switch in class helper in Monodevelop or Visual Studio which not optimal.

One more thing that you will need to know about PlayerPrefs that on xbox you can't use binnary serializer but instead you will need a XML serialization, but keep in mind that xml is much bigger then binary data and saving can take time, BTW you can't save longer then 1 second at xbox otherwise you will not pass Microsoft certification. Often solution is to pack data into zip and serialize it, or completely redesign your game witch is not funny :D

Also if your game is quite big you need to separate configuration like resolution, music volume ( better into .ini file ) from saved world. But for Skyship we have only one save file and it is not so big.

Webplayer often was much more complicated for saving data cuz weplayer policiy deny saving any files on desktop. So the only 2 ways - saving everything on server or use playerprefs. For a small games player prefs is quite ok but for middle size game like Anoxemia or SkyshipAurora it will be not comfortable to use PlayerPrefs for saving everything. Solution is quite tricky:

1.Serialize custom PlayerPrefs into byte array
2. Convert byte array into base64 string
3. Save base64 data string into original PlayerPrefs like PlayerPrefs.SetString( "GameData", mBase64Str ).

Thats it. Ofcourse for big games like MMORPG you will need to store data on server with authorization and things like this.

Into this custom PlayerPrefs you can add custom types of data that you can serialize.

Sources of custom PlayerPrefs and example how it to use you can DOWNLOAD here:
https://www.dropbox.com/s/2t1jwiwb1zjbl3d/StoringTutorial.zip?dl=0



Saturday 13 February 2016

Unity unsupported multi-object editing

Some componets in Unity like Shuriken Particles or Terrain does not support multi-editing:


Solution is to switch to debug mode of Unity Editor


Then you will get a lot of internal information of script in Inspector. Like


You should not touch InstanceID or Local Identifier but you can change Tree Distance or Detail Object Distance and parameters will be changed on all multi-selected objects. Thats it. 

Particles system data will be looks like this:


Wednesday 10 February 2016

Skyship Aurora Unity Water Tutorial



DOWNLOAD complete project:
https://www.dropbox.com/s/0gdq68f9axi2f7l/LongDistanceWater.zip?dl=0

We gonna make water like this:



Now there is a lot of different waters for Unity. Some of them are very awesome and looks extremely real. But water is often suitable for first person human look and also sources and shaders are extremely complicated for modify and learning. For such game like Skyship Aurora we need not very complicated but fast water with reflection and waves. I've looked at existed solutions and found only HORIZON[ON]. Quite awesome thing for long distance rendering, but at same time quite complicated and hard to modifying.  Best of all works all together with built-in terrains rendering system. So  I've decided to make my own water again :). Once I've did it, so I have a vector to move on. Let's check previous water tutorial and modify it for long distance:

http://kostiantyn-dvornik.blogspot.com/2013/05/unity-worlds-coolest-tutorial-about.html

Warning:
Keep in mind that this solution works with deffered rendering legacy only. So please turn on this rendering method for your project.

First we need to remove all underwater things and leave only above water stuff, also we need to remove tranparent soft edge blending ( cuz we need to make as fast shader as possible ).

Let's take a look at our previous solution. We have polygon with water material applied, mirror reflection script that makes reflection texture and script that detects underwater and change shader lod that makes underwater effect. Everything else should be removed like this.



You can download start (cleanup) project Unity 5 here:
https://www.dropbox.com/s/ssu9l6ajdvm9u82/LongDistanceWaterStart.zip?dl=0

This small dot at huge cube is a human size

Shader looks like this:

Ok lets remove step by step things that we don't need. Because of water have no transparent effect lets remove grab pass that do transparent things of water and texture accoding to this

Before:
sampler2D _GrabTexture : register(s0);
sampler2D _BumpMap : register(s2);
sampler2D _ReflectionTex : register(s3);

sampler2D _CameraDepthTexture; // : register(s4);

After:
sampler2D _BumpMap;
sampler2D _ReflectionTex;

only 2 textures we need to make such long distance water. After remove everything that cause error when shader compiles.

You will get this small shader that makes water:

Shader "Dvornik/Long Distance Water" {
Properties {
 _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
 _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
 _Refraction ("Refraction", Range (0.00, 100.0)) = 1.0 
 _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
 _DepthColor ("Depth Color", Color) = (1,1,1,0.5)
 _BumpReflectionStr ("_BumpReflectionStr", Range(0.00,1.00)) = 0.5
 _ReflectionTex ("_ReflectionTex", 2D) = "white" {}
 _BumpMap ("Normalmap", 2D) = "bump" {}
}

SubShader 
#pragma surface surf BlinnPhong
sampler2D _BumpMap;
float4 _ReflectColor;
struct Input {
void surf (Input IN, inout SurfaceOutput o) 
 float2 offset = o.Normal * _Refraction;
}


 Tags { "RenderType"="Opaque" }

CGPROGRAM

#pragma target 3.0

sampler2D _ReflectionTex;

float _Shininess;
float _Refraction;
float _BumpReflectionStr;

 float2 uv_MainTex;
 float2 uv_BumpMap;
 float3 worldRefl; 
 float4 screenPos;
 float3 viewDir;
 INTERNAL_DATA
};


 //Specular stuff
 o.Gloss = _SpecColor.a;
 o.Specular = _Shininess;

 //Normal stuff
 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap - 33.0 * _Time.y ));
 o.Normal += UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap - 100.0 * _Time.y));
 o.Normal *= 0.5;

 IN.screenPos.xy = offset * IN.screenPos.z + IN.screenPos.xy; 

 float3 worldRefl = WorldReflectionVector(IN, o.Normal*half3(_BumpReflectionStr,_BumpReflectionStr,_BumpReflectionStr));
 worldRefl.y = -worldRefl.y;
 worldRefl.x = -worldRefl.x;

 half4 reflcol = tex2Dproj(_ReflectionTex, IN.screenPos);
 reflcol = reflcol * _ReflectColor;

 half4 resCol = reflcol; 

 o.Emission = resCol;
 o.Albedo = o.Emission;

ENDCG
}

FallBack "Reflective/Bumped Diffuse"
}


And result image will looks like this:


looks bad, but just because of settings. So lets continue our experiments. I tweaked a lot of parameters like wave speed and color like this:


Also I've found not so tilable texture that can be used in wave generation.

Result shader looks like this:

Shader "Dvornik/Long Distance Water" {
Properties {
 _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
 _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
 _Refraction ("Refraction", Range (0.00, 1.0)) = 0.02 
 _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5) 
 _ReflectionTex ("_ReflectionTex", 2D) = "white" {}
 _BumpMap ("Normalmap", 2D) = "bump" {}
}

SubShader 


 Tags { "RenderType"="Opaque" }

CGPROGRAM

#pragma surface surf BlinnPhong

sampler2D _BumpMap;
sampler2D _ReflectionTex;

float4 _ReflectColor;
float _Shininess;
float _Refraction;
float _BumpReflectionStr;

struct Input {
 float2 uv_MainTex;
 float2 uv_BumpMap; 
 float4 screenPos;
 float3 viewDir;
 INTERNAL_DATA
};

void surf (Input IN, inout SurfaceOutput o) 

 //Specular stuff
 o.Gloss = _SpecColor.a;
 o.Specular = _Shininess;

 //Normal stuff
 o.Normal = UnpackNormal(tex2D(_BumpMap, 2.0 * IN.uv_BumpMap + 0.01 * _Time.y ));
 o.Normal += UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap - 0.01 * _Time.y));
 o.Normal *= 0.5;

  float2 offset = o.Normal * _Refraction;
 IN.screenPos.xy = offset * IN.screenPos.z + IN.screenPos.xy; 
  
 half4 reflcol = tex2Dproj(_ReflectionTex, IN.screenPos);
 reflcol = reflcol * _ReflectColor;

 half4 resCol = reflcol; 

 o.Emission = resCol;
 o.Albedo = o.Emission;

}
ENDCG
}

FallBack "Reflective/Bumped Diffuse"
}

And we got it:

Note:
In mirror reflection script you can tweak layers that will be render for reflection to minimize drawcalls and make your render faster.


Thursday 4 February 2016

Smart TV voice

Have no idea was it implemented or not. But when you say something - volume of TV become lover. Like you can discuss film.