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
« Unity Shader Gotcha #8 - Custom Deferred Lighting | Main | Unity Shader Gotcha #6 - Lighting 2.0 »
Saturday
May182013

Unity Shader Gotcha #7 - World Space & Detail Textures

UPDATE

This article is now obselete, as Unity 5 automatically packs texture coordinates in their generated surface shader code in order to make best use of the available interpolators.

 

ORIGINAL

Sometimes you want to use world-space vectors for things like normal mapped light probe sampling and cube map reflections. The problem is they eat up a lot of channels for passing data:

struct Input {
    float2 uv_MainTex;
    float3 viewDir;
    float3 worldRefl;
    float3 worldNormal;
    INTERNAL_DATA
};

As a result, it is now impossible on pre-SM4.0 targets to add any more channels if you want to do anything that involves multiple texture coordinates or custom vertex data (ex. detail textures, terrain). You will get an error if you try to do something like:

struct Input {
    float2 uv_MainTex;
    float2 uv2_Detail;
    float3 viewDir;
    float3 worldRefl;
    float3 worldNormal;
    INTERNAL_DATA
};

The closest thing you can do is add an extra uniform parameter to the shader to do texture transformations, and that only really helps with detail mapping. So while this feature is great for making cool per-pixel lighting effects, it comes with a steep cost in terms of flexibility. :(

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (17)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    If you enjoy football, you almost certainly have a favored group from the National Football League or two and have a list of players who like to have seen.
  • Response
    Football is actually one particular of the biggest sports in America. It has a key following.
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Response: judi bola
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Response: Andropenis
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Response: courteney cox
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Response: Alpha Fuel XT
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Response: Ultrasize Reviews
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Response: Get the facts
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #7 - World Space & Detail Textures

Reader Comments (1)

Would it be possible to sacrifice performance for flexibility, and calculate some of this in the pixel-shader? If you have the position and normal, it appears to me that you could calculate the reflection-vector, viewdir etc. per pixel.

It is true though that surface shaders take up a lot of interpolators, and it is now more the rule than the exception, that I use a straight vertex/fragment program instead.
May 31, 2013 | Unregistered Commenterpixelmager

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.