GLSL, TRANSFORM_TEX and Tiling

Alright. I’ve gotten as far as figuring out that the tiling and offset values in a shader are dictated in the UnityCG include files where one calls TRANSFORM_TEX pre-processor and I’ve seen CG examples of applying this… but I’m a bit lost as to applying it in a GLSL context.

Anyone care to share an example?

Cheers

Request doubled.

Alrighty. I actually got this working… sort of.

In my Vertex shader one has to declare:

uniform sampler2D _MainTex;
uniform vec4 _MainTex_ST;
varying vec2 TextureCoordinate;

and then call:

TextureCoordinate =  gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;

And then in your fragment shader, you declare:

uniform sampler2D _MainTex;
varying vec2 TextureCoordinate;

and use:

texture2D(_MainTex, TextureCoordinate)

However, when I try and directly use:

TextureCoordinate =  vec2(TRANSFORM_TEX(gl_MultiTexCoord0, _MainTex));

I get the following:

Shader error in 'GLSLFastSpec': GLSL Error in Vertex Shader: ERROR: 0:176: '' : syntax error preprocessor command must not be preceded by any other statement in that line
ERROR: 0:176: '*' :  wrong operand types no operation '*' exists that takes a left-hand operand of type '2-component vector of float' and a right operand of type 'uniform sampler2D' (or there is no acceptable conversion)
ERROR: 0:176: 'pre-mature EOF' : syntax error syntax error
 at line 0

Something about the Pre-processor that’s broken. Not sure what though. Inside UnityCG.glslinc we have:

// Transforms float2 UV by scale/bias property (new method)
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
// Transforms float4 UV by a texture matrix (old method)
#define TRANSFORM_UV(idx) (gl_TextureMatrix[idx] * gl_TexCoord[0] ).xy

I pretty much just hand-wrote the TRANSFORM Text myself in my shader directly and it worked. How odd.

Actually, you don’t need “TextureCoordinate”. You can use

gl_TexCoord[0].xy =  gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;

Ooooooorrrr Not.

Sigh

(Well, I’m paraphrasing Douglas Adams but the sentiment is warranted)

2 Likes

I’m not understanding your troubles, but thanks for this find – it works perfectly. I’m not too skilled with decoding what #pragma debug spits out yet.

Well, I’m surprised that there’s something I discovered that you didn’t know. I’ve been reading most of the Shaderlab threads with your expertise, you’ve single-handedly got me up to speed on the subject. (if the speed were largely slowish… Shaders are clumsy awkward vodoo)

:slight_smile: The problem is, while ShaderLab itself has good documentation, and there are lots of references for GLSL, it’s very hard for me to figure out how to access the data that I need from Unity to make shaders happen. I get the impression that Unity Tech is anti-GLSL, sees shaders as part of their source code that they’re forced, at this time, to let loose without a source code license, and with the stigma of shaders basically being magic to most developers (due in my opinion to lack of good educational material), they don’t feel the desire to help us write our own. That sentiment could be way off the mark, but it’s how it comes off to me.

Ya, I’m not quite grokking how to use the tangent attributes in GLSL. where does that data come from and how does it get there.

I have half a mind to start writing a set of GLSL shaders in the wiki for he community. Just to stretch my legs.