Before Unity 5, texture properties could have options inside the
curly brace block, e.g. TexGen CubeReflect
. These were controlling fixed function
texture coordinate generation. This functionality was removed in Unity 5.0; if you need
texgen you should write a vertex shaderA program that runs on each vertex of a 3D model when the model is being rendered. More info
See in Glossary instead.
This page shows how to implement each of fixed function TexGen modes from Unity 4.
TexGen CubeReflect
is typically used for simple cubemapA collection of six square textures that can represent the reflections in an environment or the skybox drawn behind your geometry. The six squares form the faces of an imaginary cube that surrounds an object; each face represents the view along the directions of the world axes (up, down, left, right, forward and back). More info
See in Glossary reflections.
It reflects view direction along the normal in view space, and uses that as the UV
coordinate.
Shader "TexGen/CubeReflect" {
Properties {
_Cube ("Cubemap", Cube) = "" { /* used to be TexGen CubeReflect */ }
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 uv : TEXCOORD0;
};
v2f vert (float4 v : POSITION, float3 n : NORMAL)
{
v2f o;
o.pos = UnityObjectToClipPos(v);
// TexGen CubeReflect:
// reflect view direction along the normal,
// in view space
float3 viewDir = normalize(ObjSpaceViewDir(v));
o.uv = reflect(-viewDir, n);
o.uv = mul(UNITY_MATRIX_MV, float4(o.uv,0));
return o;
}
samplerCUBE _Cube;
half4 frag (v2f i) : SV_Target
{
return texCUBE(_Cube, i.uv);
}
ENDCG
}
}
}
TexGen CubeNormal
is typically used with cubemaps too.
It uses view space normal as the UV coordinate.
Shader "TexGen/CubeNormal" {
Properties {
_Cube ("Cubemap", Cube) = "" { /* used to be TexGen CubeNormal */ }
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 uv : TEXCOORD0;
};
v2f vert (float4 v : POSITION, float3 n : NORMAL)
{
v2f o;
o.pos = UnityObjectToClipPos(v);
// TexGen CubeNormal:
// use view space normal of the object
o.uv = mul((float3x3)UNITY_MATRIX_IT_MV, n);
return o;
}
samplerCUBE _Cube;
half4 frag (v2f i) : SV_Target
{
return texCUBE(_Cube, i.uv);
}
ENDCG
}
}
}
TexGen ObjectLinear
used object space vertex position as UV coordinate.
Shader "TexGen/ObjectLinear" {
Properties {
_MainTex ("Texture", 2D) = "" { /* used to be TexGen ObjectLinear */ }
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 uv : TEXCOORD0;
};
v2f vert (float4 v : POSITION)
{
v2f o;
o.pos = UnityObjectToClipPos(v);
// TexGen ObjectLinear:
// use object space vertex position
o.uv = v.xyz;
return o;
}
sampler2D _MainTex;
half4 frag (v2f i) : SV_Target
{
return tex2D(_MainTex, i.uv.xy);
}
ENDCG
}
}
}
TexGen EyeLinear
used view space vertex position as UV coordinate.
Shader "TexGen/EyeLinear" {
Properties {
_MainTex ("Texture", 2D) = "" { /* used to be TexGen EyeLinear */ }
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 uv : TEXCOORD0;
};
v2f vert (float4 v : POSITION)
{
v2f o;
o.pos = UnityObjectToClipPos(v);
// TexGen EyeLinear:
// use view space vertex position
o.uv = UnityObjectToViewPos(v);
return o;
}
sampler2D _MainTex;
half4 frag (v2f i) : SV_Target
{
return tex2D(_MainTex, i.uv.xy);
}
ENDCG
}
}
}
TexGen SphereMap
computes UV coordinates for spherical environment mapping.
See OpenGL TexGen reference for the formula.
Shader "TexGen/SphereMap" {
Properties {
_MainTex ("Texture", 2D) = "" { /* used to be TexGen SphereMap */ }
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert (float4 v : POSITION, float3 n : NORMAL)
{
v2f o;
o.pos = UnityObjectToClipPos(v);
// TexGen SphereMap
float3 viewDir = normalize(ObjSpaceViewDir(v));
float3 r = reflect(-viewDir, n);
r = mul((float3x3)UNITY_MATRIX_MV, r);
r.z += 1;
float m = 2 * length(r);
o.uv = r.xy / m + 0.5;
return o;
}
sampler2D _MainTex;
half4 frag (v2f i) : SV_Target
{
return tex2D(_MainTex, i.uv);
}
ENDCG
}
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!