Showing posts with label unity. Show all posts
Showing posts with label unity. Show all posts

Tuesday, February 25, 2014

Unity 4.3.x doesn't support Google Native Client

While spending some time tonight working with Unity's platform preprocessor defines I noticed I wasn't getting proper results when checking if the UNITY_NACL symbol was defined.  Oddly enough when I set Unity to make a Native Client build, it defined the UNITY_WEBPLAYER symbol instead. After asking on Twitter and making some Google searches, I finally found an semi-buried explanation here and this nugget in the Unity3d 4.3 release notes

  • Google Native Client support is not functional in Unity 4.3. If you need to publish to Native Client you can still use Unity 4.2.2.
This change isn't readily apparent when looking at the build window, as you can see in this picture, there isn't any information to indicate the Native Client build isn't supported.  I suspect this is the first step to phasing out NACL support entirely, similar to the removal of Flash build exports



.  Hopefully this helps anyone who stumbles into this when trying to use the UNITY_NACL define

Monday, November 19, 2012

Creating a Colored Unlit Alpha Texture Shader for Unity

Last week I wrote about Creating a Colored Unlit Texture Shader for Unity.  I've used the shader from that post quite a lot on my new game Match'n Flip; however I recently realized it didn't work with alpha textures (textures with transparency).  So I decided to make a short post about creating the same kind of colored shader for a texture with alpha.

Applying a color combine with an alpha texture opens up some interesting possibilities, because you can control the opacity of the texture by modifying the color multiplier's alpha value.  Below is a picture of the default Unity Unlit Transparent shader, and on the right are 2 examples of my modified colored unlit transparent shader.  The opaque section of the object on the top right is semi-transparent because I set the color multiplier's alpha value to 50%(0.5), which the object on the bottom left is full opacity but has a red tint applied.


The shader itself is pretty simple, I modified Unity's Unlit-Alpha shader to support a Color property and handle the color combine operation.  The shader will appear in the shader dropdown menu under NAKAI/Unlit/Transparent Colored.  In case you're wondering, the Unlit-Alpha shader was written using Unity's ShaderLab Shader Syntax, I highly recommend reading up on the operations involved.  As before, you are free to copy and use this shader as you wish, Enjoy!

// Copied from Unlit-Alpha.shader, but adds color combine(tinting)
// Unlit alpha-blended shader.
// - no lighting
// - no lightmap support
// - Added per-material color

// Change this string to move shader to new location
Shader "NAKAI/Unlit/Transparent Colored" {
    Properties {
        // Adds Color field we can modify
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    
    SubShader {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        LOD 100
    
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha 

        Pass {
            Lighting Off
            SetTexture [_MainTex] { 
                // Sets _Color as the 'constant' variable
                constantColor[_Color]
                
                // Multiplies color (in constant) with texture
                combine constant * texture
            } 
        }
    }
}


Wednesday, November 14, 2012

Creating a Colored Unlit Texture Shader for Unity

When dealing with Unity materials, one easy and convenient feature is the ability to change the Main Color of a shader to tint the color of the texture it uses.  This can be used for a lot of different situations, like changing the color of a tile on a selection grid or changing the background color of a 2d game, to name a few.

Unfortunately I found the ability to specify a shader color missing in Unity's Unlit Texture shader.  The Unlit Texture shader isn't affected by lights, which is very convenient when you don't want to include lights in a scene or want to manually control the brightness of a texture.  It makes sense why the Unlit Texture shader doesn't include the color combiner support, the Unlit Texture shader is the simplest and fastest shader they have, and adding a color combiner would add extra instructions which would rarely be used.  Since I needed the ability to color tint an Unlit Texture for my new project, I decided to extend the existing Unlit Texture shader to support a color multiply operation.

The source for the new shader is below, you are free to copy it for your own needs*.  Use the 'Create--Shader' command in unity, then paste this code into the new file.  The shader will appear in the shader dropdown menu under NAKAI/Unlit/Texture Colored.  In case you're wondering, the Unlit Texture shader was written using Unity's ShaderLab Shader Syntax and I added a few extra lines to support the color combine operation.  Here's a picture of it in action, Enjoy!




// Unlit color shader. Very simple textured and colored shader.
// - no lighting
// - no lightmap support
// - per-material color

// Change this string to move shader to new location
Shader "NAKAI/Unlit/Texture Colored" {
    Properties {
        // Adds Color field we can modify
        _Color ("Main Color", Color) = (1, 1, 1, 1)        
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 100
        
        Pass {
            Lighting Off
            
            SetTexture [_MainTex] { 
                // Sets our color as the 'constant' variable
                constantColor [_Color]
                
                // Multiplies color (in constant) with texture
                combine constant * texture
            } 
        }
    }
}


*You are free to use this for your own games.  Please do not sell it on the Unity asset store or anywhere else. :)

Saturday, September 24, 2011

Handling Texture Tiling and Offset in a Unity3d Vertex and Fragment Shader using TRANSFORM_TEX macro

When doing some test work, I found my Cg shader wasn't handling texture offset and tiling properly, resulting in distortion such as below:

The Unity shader on the left properly handles a 3x tiling factor for the texture, while my shader on the right doesn't properly handle the same 3x tiling factor



My shader setup was pretty simple, here's a snippet of the important parts:

#include "UnityCG.cginc"

sampler2D _MainTex;
float4 _Color;

v2f vert ( appdata_base v )
{
    v2f o; 

    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

    // Texture offset - BAD
    o.uv = v.texcoord;
    return o;
}

half4 frag (v2f i) : COLOR
{    
    return tex2D(_MainTex, i.uv) * _Color;
}

I bolded the problematic part, which is the Texture UV handling in the 'vert' program.  Simply grabbing the v.texcoord will work when there is not tiling or offsets in the texture, but breaks once those elements are introduced

After snooping around, I found the solution is to use the TRANSFORM_TEX macro in the UnityCG.cginc to make sure the texture's offset and tiling are properly applied.  A small caveat to this is you must, "declare float4 properties for each texture before the vertex program, with _ST appended" (Quoted from Unity3d shader docs).

Here's a snippet of my shader with the new changes:

#include "UnityCG.cginc"

sampler2D _MainTex;
float4 _Color;
uniform float4 _MainTex_ST; // Needed for TRANSFORM_TEX(v.texcoord, _MainTex)

v2f vert ( appdata_base v )
{
    v2f o; 

    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

    // Texture offset - GOOD
    o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    return o;
}

half4 frag (v2f i) : COLOR
{    
    return tex2D(_MainTex, i.uv) * _Color;
}

With this happy little change, our shader now properly handles the tiling and offsets of the texture.
 

For those curious, I gleaned this information from the Unity3D Vertex and Fragment Programs and the ancient Unity 2.x Shader Conversion Guide.  The info pulled from the Shader conversion guide regards the _MainTex_ST property needed for TRANSFORM_TEX to work. 

UPDATE - Dec 1, 2013 - The Unity 2.x Shader Conversion Guide seems to have disappeared from the internet.  This information is all still relevant for Unity 4.x release versions