- Home /
cross product in cg shader - error "can't find __getVectorIndex"
Anyone know what this is about? This is my first real Unity shader (it's a port of a cg shader from a different project I'm moving to Unity). Here's the shader:
Shader "Starboretum/Flora" {
Properties {
_MainTex ("Texture", 2D) = ""
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
const float4 uCoords = {0,1,1,0};
const float4 vCoords = {0.5f,0.5f,0.5f,0.5f};
const int4 vCEnd = {0,0,1,1};
const int4 vCStart = {1,1,0,0};
const int4 wStart = {0,0,-1,1};
const int4 wEnd = {1,-1,0,0};
v2f vert (appdata v)
{
int corner = (int)(v.tangent.w);
float3 prev = v.tangent.xyz;
float3 start = v.vertex.xyz;
float3 end = v.normal.xyz;
float3 dif1 = (start - prev);
float3 dif2 = end - start;
// get the direction of the line, using the previous or next segments as a guide depending on which end we're at
float3 dir = dif1 * vCStart[corner] + dif2 * vCEnd[corner];
float3 d1 = _WorldSpaceCameraPos - start;
float3 d2 = _WorldSpaceCameraPos - end;
// get a vector from the segment to the bamera
float3 camDif = d1 * vCStart[corner] + d2 * vCEnd[corner];
// obtain the correct width of the segment
float widthStart = v.texcoord.x;
float widthEnd = v.texcoord.y;
float width = widthStart * wStart[corner] + widthEnd * wEnd[corner];
// get a vector perpendicular to the direction to the camera
float3 left = normalize(cross(dir, camDif));
// expand the line segment by the width, and add on the length if it's one of the end verts
v.vertex.xyz += left * width + dif2 * vCEnd[corner];
// insert correct UVs
v.texcoord.x = uCoords[corner];
v.texcoord.y = vCoords[corner];
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
o.color = v.color;
return o;
}
half4 frag (v2f i) : COLOR
{
half4 texcol = tex2D (_MainTex, i.uv);
return texcol * i.color;
}
ENDCG
}
}
FallBack "VertexLit"
}
The line
float3 left = normalize(cross(dir, camDif));
Gives the error
Shader error in 'Starboretum/Flora': Program 'vert', can't find __getVectorIndex function in stdlib at line 1313
When Unity tries to compile the shader.
I know it's that line because I commented stuff out and put it back in until the error reappeared and it was that line. I also tried taking out the normalize and it still happened.
Any ideas?
Thanks.
If I comment out lines 68-76, it compiles, and the error returns if I uncomment any of them.
This suggests that it's an instruction limit problem but adding #pragma target 3.0 does not resolve the issue, which suggests that it's NOT an instruction limit issue as sm3 has no instruction limit on vertex programs according to this page.
Answer by Jasper-Flick · Feb 21, 2014 at 12:39 PM
Using a variable to index a vector is causing the problem, which you use in multiple places. I have not tried it, but maybe using a mask instead works. For example,
v.texcoord.y = vCoords[corner]
becomes
float4 mask = float4(corner == 0, corner == 1, corner == 2, corner == 3);
v.texcoord.y = dot(vCoords, mask);
That fixed it! Thanks! (well, it compiles but doesn't 'work' but that's a different problem)
Wow I really thought this would never get fixed or answered and I was going to ship out to sea and never come back.