About Me

Hi. I'm Josh Ols. Lead Graphics Developer for RUST LTD.

Contact:
crunchy.bytes.blog[at]gmail[dot]com

View Joshua Ols's profile on LinkedIn

Meta

Entries in Unity3D (29)

Saturday
May182013

Unity Shader Gotchas #4 - Custom Alpha Blending

Be careful when using both custom alpha blending (eg. premultiplied alpha) and a custom lighting function. You need to make sure to return the alpha in the custom lighting function or it will not be applied. Basically all that means is the following:

half4 LightingCustom (SurfaceOutput s, half3 lightDir, half atten) {
    half4 c;
    
    // Lighting Code
    
    // Don't forget this part!
    c.a = s.Alpha;
    return c;
}

Saturday
May182013

Unity Shader Gotchas #3 - CustomSurfaceOutput

UPDATE

So it turns out I was completely wrong about this. In reality, I was seeing problems from trying to have two texture coordinates, the world space position, world space normal, world space reflection, and all the vectors needed to do world space normal maps.

Please disregard what I said in this article. 

 

ORIGINAL

Background
If you want to make a CustomSurfaceOutput struct then you will encounter some restrictions. For starters, it is required to have fields for Alpha, Albedo, Normal, Specular, and Emission. Gloss is not required, and can either be dropped or replaced.

The Problem
Assuming we replace Gloss with a float3, this only leaves room for one more float3. Any more than that, and the compiler will throw up an error saying you have run out of channels to pass data.

The Solution
As it turns out, you can squeeze out another float1 field by intelligently packing your fields. If all your others are float3 fields, you can pack your Alpha, Specular, and custom float1 field at the front of the struct and the compiler will allow it.

Saturday
May182013

Unity Shader Gotchas #2 - Naming Conventions

UPDATE 2

This article is now obselete, as Unity 5 switched to a system where they use a specialized "Meta" pass that allows Enlighten to pick up all diffuse and emissive color from an object regardless of how they were calculated.

 

UPDATE

Unity now posts the conventions for the shader names and parameters to integrate with Beast. Please see the "Material Properties" section of "Lightmapping in Depth":

http://docs.unity3d.com/410/Documentation/Manual/LightmappingInDepth.html

 

ORIGINAL

Background
So as it turns out, Unity cares about how you name your shaders. Specifically, the category you put them in affects whether or not certain features are enabled for that shader. The root category name can be whatever you want, so long as the sub-categories match Unity's conventions.

This also applies to whatever you choose for your fallback shader. It must support the same feature, or unity will disable the feature in the parent shader.

Most common cases:
Tessellated Shadows - "<project>/Tessellation/<shader name>"

Static & Dynamic cutout shadows - "<project>/Transparent/Cutout/<shader name>"

Emissive material for light baking - "<project>/Self-Illumin/<shader name>"

The Problem
As near as I can tell, these are all mutually exlusive and only allow one per shader. So you couldn't have a light emitting material with tessellated cutout shadows.

The Solution
There isn't really a solution to this as far as I can see. All you can do is pick the one that is most important for the shader.

Saturday
May182013

Unity Shader Gotchas #1 - _MainTex, _MainColor

UPDATE

This article is now obselete, as Unity 5 switched to a system where they use a specialized "Meta" pass that allows Enlighten to pick up all diffuse and emissive color from an object regardless of how they were calculated.

 

Background
Something I didn't know until recently is that the parameters _MainTex & _MainColor have special meaning in Unity3D's surface shader system. They are actually the parameters that Unity reads for bouncing color for baking lightmaps, emitting light for baking lightmaps, and for doing both static and dynamic cutout shadows. As a result, you need to use these parameters in your shader if you want to take advantage of these systems.

Normally, most people won't even think about this as they have no reason to explicitly remove these parameters from their shaders.

The Problem
When it comes physically-based shaders, metallic materials have an albedo of zero. So if you are using _MainTex & _MainColor for your albedo, then the lightmapper will not bounce any light from the materials that have metal. As a result, metal-heavy environments will have significantly less bounced light. On the flip side, not using these parameters at all will cause the lightmapper to assume your material is pure white and fully opaque.

The Solution
I was fortunate enough to discover that it is valid to put these parameters with the regular surface shader parameters, without using them in the actual shader code. Then you can set them in the UI, and Unity will still detect and use them like normal for lightmap baking and static+dynamic cutout shadows.

Essentially, you now have a bounce map & color that you can then use to get around this problem, and even gain some artistic control of the environment's bounced baked lighting. Since these also control cutouts, you can also do something like Valve's distance field cutout maps to cut down on texture sizes. This way, your cutout textures can be at a much lower resolution than your regular textures.

Saturday
Jan192013

Museum of the Microstar Demo

Ladies and gentlemen...I give you Museum of the Microstar! Our entry into Unity3D's DX11 contest.

In it, you will find some crazy stuff including displacement tessellation, and compute shader driven particle systems. In particular, all materials in this demo utilize the latest and greatest version of my physically-based BRDF material shaders. They are all linear HDR compatible, have consistent visuals between forward and deferred mode, and integrate with Unity's baked lighting systems.

Follow the link and check it out! ;)