Saturday, 24 February 2018

Reference for converting GLSL to HLSL - Unity 3d ShaderToy


  • Replace iGlobalTime shader input (“shader playback time in seconds”) with _Time.y
  • Replace iResolution.xy (“viewport resolution in pixels”) with _ScreenParams.xy
  • Replace vec2 types with float2mat2 with float2x2 etc.
  • Replace vec3(1) shortcut constructors in which all elements have same value with explicit float3(1,1,1)
  • Replace Texture2D with Tex2D
  • Replace atan(x,y) with atan2(y,x) <- Note parameter ordering!
  • Replace mix() with lerp()
  • Replace *= with mul()
  • Replace signed integer vector ive2, ivec3, ivec4 with int2, int3,int4
  • Replace iTime with _Time
  • Remove third (bias) parameter from Texture2D lookups
  • mainImage(out vec4 fragColor, in vec2 fragCoord) is the fragment shader function, equivalent to float4 mainImage(float2 fragCoord : SV_POSITION) : SV_Target
  • UV coordinates in GLSL have 0 at the top and increase downwards, in HLSL 0 is at the bottom and increases upwards, so you may need to use uv.y = 1 – uv.y at some point.
  • Replace fract with frac
  • Replace function dFdx with ddx
  • Replace function dfdy with ddy
  • fragCoord in GLSL
    Default Unity shader naming :

    HLSL - Shader Lab in Unity
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};

In frag method:

fixed4 frag (v2f i) : SV_Target

{

   // GLSL - fragCoord, fragCoord.xy = i.vertex.xy

    i.vertex.position 

}

In GLSL we would have method:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{

}



  • Replace mod with fmod





I took some information from https://alastaira.wordpress.com/2015/08/07/unity-shadertoys-a-k-a-converting-glsl-shaders-to-cghlsl/

Wednesday, 14 February 2018

Principles for fast and good game development


Above video is a John Romero's presentation in WeAreDevelopers 2017. Just in case you are not sure who is John Romero, he was one of the main creators behind FPS legends such as Doom, Wolfenstein 3D, Quake. 

I personally think is the easiest way to improve skills in any field is to listen the experts. I wasted too much time in the past trying to figure out too many things on my own. I summarize principles that John Romero's team was following. There is not a paradigm in those, however it is good to print them and place them on your office wall as a good reminder to your team.


  1. Polish game as you go
  2. Always maintain shippable code
  3. No prototypes for small game/team
  4. Game can always be run by your team
  5. Have defaults for your core methods, so when someone override it with faulty code other team members won't be blocked by it.
  6. Keep code super simple, refactor as you go
  7. Test properly your code before you commit it, so testers do not have to waste too much time creating bug reports etc.
  8. Write code specifically for the game you are working on, do not try to make your code ready for future games as in the future you will be writing better code as you become more experienced
  9. Run your code after writing few lines of code so you can quickly detect problem if occur 
  10. Write the code is it is very easy to understand by other developers


My personal favourite that is related to point 10

Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.

Wednesday, 1 November 2017

Hubble Telescope made with Unity 3d



Hubble telescope finished and ready to be downloaded from http://bit.ly/hubbletelescopeunity3d.

Most of the textures are in 4096x4096 px format and all materials are PBR.
Screenshots below were not modified, it is direct screen from Unity 3d game engine.









Tuesday, 12 September 2017

Simple extension class in ASP.NET C# application

We are going to write simple extension class to automatically add tax and give us gross price for net price. Very common feature in most of applications that are dealing with accounting, finance, trading, e commerce etc.

Seing code like this in lots of different places in application is not uncommon.

decimal grossPrice = (etPrice * 0.20) + netPrice;

It is not the end of the world if you are using that only in one place of the application, but in most case this code might appear in many places. As a developer I hate magic number (0.20 in this case) and not using DRY (do not repeat yourself) approach.

There is very simple approach to this problem I like to use - using extension.

Let's get straight to the code.

Constant class
public static class TaxConstants
  {
    public static decimal TAX_VALUE = (decimal)0.20;
  }

Extension class

namespace AbDeveloper.Extensions

public static class PriceExtension
  {
    public static decimal AddTaxToNetValue(this decimal price)
    {
      return price + price * Constants.TaxConstants.TAX_VALUE;
    }
  }


We can now easily test the functionality in test project.

using namespace AbDeveloper.Extensions;

[TestClass]
  public class PriceExtensionTest
  {
    [TestMethod]
    public void TestAddTaxToNetValue()
    {
      decimal netPrice = 20;
      decimal grossPrice = netPrice.AddTaxToNetValue();

      Assert.AreEqual(24, grossPrice, "Expected gross price is incorrect. Check AddTaxToNetValue method in PriceExtension class.");
    }
  }



At this stage test will pass and grossPrice will be equal 24 with an input of 20. If however tax rate will change to 0.25 (25%) test will fail.

In order to use extension in class make sure you will add "using namespace" expression.







Wednesday, 30 August 2017

The Richest Man in Babylon - 5 rules of gold

The Richest Man in Babylon - Book written in 1920s by George S.Clason still holds wisdom in terms of investing, business, money - http://amzn.to/2wTuigD


One of the chapters explains 5 rules of gold that wise man should follow. Below is my short explanation of how to apply those rules in modern world.


1. Gold cometh gladly and in increasing quantity to any man who will put by not less than one-tenth of his earnings to create an estate for his future and that of his family.

Make sure to save at least 10% of your net earnings..


2. Gold laboreth diligently and contentedly for the wise owner who finds for it profitable employment, multiplying even as the flocks of the field.

You can invest in bonds, stocks, low cost index funds(my personal preference), investment funds, ISA stock(UK only as I am aware), gold silver, crypto currencies such as bitcoin, ethereum. By buying stocks, investment funds and indexes you can also benefit from dividends. Avoid investing in stocks that do not pay dividends.


3. Gold clingeth to the protection of the cautious owner who invests it under the advice of men wise in its handling.

Invest your money wisely, avoid high leverage, get rich quick schemes, if something is too good to be true it's probably scam. When you are investing make sure to diversify your portfolio by investing in different types of markets.


4. Gold slippeth away from the man who invests it in businesses or purposes with which he is not familiar or which are not approved by those who are skilled in its keep.

Avoid investing in business you do not understand. This is very simple rule that greatest investor Warren Buffet follow.


5. Gold flees the man who would force it to impossible earnings or who followeth the alluring advice of tricksters and schemers or who trusts it to his own inexperience and romantic desires in investment.

Well connected with 3rd rule but it is very important to follow. Again I will quote Warren Buffet -"The first rule is not to lose. The second rule is not to forget the first rule".
 

Thursday, 4 August 2016

Piano Room PBR - Unity 3d asset



PBR Piano Room package was released to Unity3d store.




Piano is the main of focus of this package. It is divided into few submeshes:
Piano Bottom Body : 702 tris
Keys : 2724 tris in total (separate keys - 7 octaves) 51 white keys, 36 black
Piano Note Reader : 154 tris
Piano Top : 872 tris
Piano Interior Details : 312 tris
Piano Wheels and Details : 4860 tris
Piano Name : 252 tris

Each submesh material contains 4 textures(albedo, normal, ambient oclusion, metallic).
Normal maps are baked from very high poly(over 600k tris).

Room interior scene included:
Flower
Wall Lamp
Bench
Windows
Door
Coving
Skirting
Wall Details
Wooden Floor

Additional features:
Set time of a day dynamically
Set glowing background for exterior

Future planned release : 

- Map all keys to Midi output
- Play songs from tab notes
- Enable VR

Get the package here.




Thursday, 28 July 2016

Getting enum values to list of string List<string> C#

So you created an enum and you want to extract all the values to List<string>?

Example enum:


public enum FilterTypeLookup {
Textbox = 1,
Standard_Dropdown = 2,
Extended_Dropdown = 3,
Normal_Dropdown = 4,
Link = 5,
Label = 6,
Checkbox = 7,
Dropdown_with_Textbox_and_Button = 8,
Textbox_with_text_before_and_text_after = 9,
Radio_Button = 10,
Date_Picker = 11,
Month = 12,
Year = 13,
Customer_Dropdown = 14,
AutoComplete = 15
}

Probably the easiest way to do it is to use GetNames method on Enum.

return Enum.GetNames(typeof(FilterTypeLookup )).ToList();


If we want to make it more generic we can write helper method.

 public static List<string> GetNamesForEnums<T>()
        {
            //Type type = t.GetType();
            return Enum.GetNames(typeof(T)).ToList();
        }



Wednesday, 20 July 2016

PBR Wood Textures Pack

Recently I released PBR Wood texture for Unity 3d store. Each material will contain at least 4 textures(albedo, normal, ambient oclusion, height). Some material will also contain gloss and metallic texture. All textures are seamless and at least 1024x1024 px.

You can grab them at Unity store

For last 2 months it is top paid wood texture pack.












Monday, 18 July 2016

Adding multiple appSettings to webConfig file and getting value in ASP.MET MVC

Let's say we want to add few app settings file to a webconfig file, so if we have to change value of the key on the server, IIS does not have to recompile the whole application leading to better client/customer experience.

1. In webconfig add new section.

<configuration>
<configsections>

<section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
</section>
</configsections>
...


2. Create new config file in solution, let's call it CodeBasedOnReferer.config.
3. Add new key

<DatabaseConfig>
    <add key="google.com" value="Google_123"></add>
</DatabaseConfig>

4. To obtain the code in aplication in controller:

NameValueCollection settings = (NameValueCollection) ConfigurationManager.GetSection("DatabaseConfig");
string code = settings (Request.UrlReferrer.Authority);

5. You can test your application using for instance fiddler. To create request open fiddler. Just in case you do not have it : http://www.telerik.com/fiddler


6. Your code should be Google_123.

Wednesday, 11 May 2016

Reading content from dll - c# or vb

So you ever wanted to read code from dll regardless if it was written in c# or vb?

There is very good free tool on the market provided by JetBrains.

https://www.jetbrains.com/decompiler/

1. After installation open application.
2. Shortcut ctrl + O, or click on first left icon under Assembly Explorer and choose dll.


3. Double click on class to see the code.