Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T>
- static void GenerateGenericLightShader(T& out, const char* lightsName, int index, const char* swizzle)
- {
- out.Write("switch (g_aColorChansLight%d%d[i])\n", swizzle, index);
- out.Write("{\n");
- out.Write("case 0:\n");
- out.Write("{\n");
- out.Write("lacc.%s += " LIGHT_COL";\n", swizzle, LIGHT_COL_PARAMS(lightsName, index, swizzle));
- out.Write("} break;\n");
- out.Write("case 1:\n");
- out.Write("{\n");
- out.Write("ldir = normalize(" LIGHT_POS".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(lightsName, index));
- out.Write("lacc.%s += %sdot(ldir, _norm0)) * " LIGHT_COL";\n",
- swizzle, chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0," : "(", LIGHT_COL_PARAMS(lightsName, index, swizzle));
- out.Write("} break;\n");
- out.Write("case 2:\n");
- out.Write("{\n");
- out.Write("ldir = " LIGHT_POS".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(lightsName, index));
- out.Write("dist2 = dot(ldir, ldir);\n"
- "dist = sqrt(dist2);\n"
- "ldir = ldir / dist;\n"
- "attn = max(0.0, dot(ldir, normalize(" LIGHT_DIR".xyz)));\n",
- LIGHT_DIR_PARAMS(lightsName, index));
- // attn*attn may overflow
- out.Write("attn = max(0.0, " LIGHT_COSATT".x + " LIGHT_COSATT".y*attn + " LIGHT_COSATT".z*attn*attn) / dot(" LIGHT_DISTATT".xyz, float3(1.0,dist,dist2));\n",
- LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_DISTATT_PARAMS(lightsName, index));
- out.Write("} break;\n");
- out.Write("case 3:\n");
- out.Write("{\n");
- // specular
- out.Write("ldir = normalize(" LIGHT_POS".xyz);\n", LIGHT_POS_PARAMS(lightsName, index));
- out.Write("attn = (dot(_norm0,ldir) >= 0.0) ? max(0.0, dot(_norm0, normalize(" LIGHT_DIR".xyz))) : 0.0;\n", LIGHT_DIR_PARAMS(lightsName, index));
- // attn*attn may overflow
- out.Write("attn = max(0.0, " LIGHT_COSATT".x + " LIGHT_COSATT".y*attn + " LIGHT_COSATT".z*attn*attn) / (" LIGHT_DISTATT".x + " LIGHT_DISTATT".y*attn + " LIGHT_DISTATT".z*attn*attn);\n",
- LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_COSATT_PARAMS(lightsName, index),
- LIGHT_DISTATT_PARAMS(lightsName, index), LIGHT_DISTATT_PARAMS(lightsName, index), LIGHT_DISTATT_PARAMS(lightsName, index));
- out.Write("} break;\n");
- out.Write("}\n");
- out.Write("switch (g_aColorChansLightDiffuse%d%d[i])\n", swizzle, index);
- out.Write("{\n");
- out.Write("case 0:\n");
- out.Write("{\n");
- out.Write("lacc.%s += attn * " LIGHT_COL";\n", swizzle, LIGHT_COL_PARAMS(lightsName, index, swizzle));
- out.Write("} break;\n");
- out.Write("case 1:\n");
- out.Write("{\n");
- out.Write("lacc.%s += attn * %sdot(ldir, _norm0)) * " LIGHT_COL";\n",
- swizzle,
- chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0," : "(",
- LIGHT_COL_PARAMS(lightsName, index, swizzle));
- out.Write("} break;\n");
- out.Write("}\n");
- }
- template<class T>
- static void GenerateGenericLightingShader(T& out, int components, const char* materialsName, const char* lightsName, const char* inColorName, const char* dest, int numColorChans)
- {
- out.Write("vector <int,8> g_aColorChan0;\n");
- out.Write("vector <int,8> g_aColorChan1;\n");
- out.Write("vector <int,8> g_aColorChansLightxyz0;\n");
- out.Write("vector <int,8> g_aColorChansLightxyz1;\n");
- out.Write("vector <int,8> g_aColorChansLightw0;\n");
- out.Write("vector <int,8> g_aColorChansLightw1;\n");
- out.Write("vector <int,8> g_aColorChansLightDiffusexyz0;\n");
- out.Write("vector <int,8> g_aColorChansLightDiffusexyz1;\n");
- out.Write("vector <int,8> g_aColorChansLightDiffusew0;\n");
- out.Write("vector <int,8> g_aColorChansLightDiffuseW1;\n");
- for (unsigned int j = 0; j < numColorChans; j++)
- {
- out.Write("{\n");
- out.Write("switch (g_aColorChan%d[%d])\n", j, 0);
- out.Write("{\n");
- if (components & (VB_HAS_COL0 << j))
- {
- out.Write("case 0:\n");
- out.Write("{\n");
- out.Write("mat =%s%d;\n", inColorName, j);
- out.Write("} break;\n");
- }
- if (components & VB_HAS_COL0)
- {
- out.Write("case 1:n");
- out.Write("{\n");
- out.Write("mat = %s0;\n", inColorName);
- out.Write("} break;\n");
- }
- out.Write("case 2:n");
- out.Write("{\n");
- out.Write("mat = float4(1.0,1.0,1.0,1.0);\n");
- out.Write("} break;\n");
- out.Write("case 3:\n");
- out.Write("{\n");
- out.Write("mat = %s[%d];\n", materialsName, j + 2);
- out.Write("} break;\n");
- out.Write("}\n");
- out.Write("switch (g_aColorChan%d[%d])\n", j, 1);
- out.Write("{\n");
- if (components & (VB_HAS_COL0 << j))
- {
- out.Write("case 0:\n");
- out.Write("{\n");
- out.Write("lacc = %s%d;\n", inColorName, j);
- out.Write("} break;\n");
- }
- if (components & VB_HAS_COL0)
- {
- out.Write("case 1:n");
- out.Write("{\n");
- out.Write("lacc = %s0;\n", inColorName);
- out.Write("} break;\n");
- }
- out.Write("case 2:n");
- out.Write("{\n");
- out.Write("lacc = float4(1.0,1.0,1.0,1.0);\n");
- out.Write("} break;\n");
- out.Write("case 3:\n");
- out.Write("{\n");
- out.Write("lacc = %s[%d];\n", materialsName, j);
- out.Write("} break;\n");
- out.Write("case 4:\n");
- out.Write("{\n");
- out.Write("lacc = float4(1.0,1.0,1.0,1.0);\n");
- out.Write("} break;\n");
- out.Write("}\n");
- //id -1
- out.Write("switch (g_aColorChan%d[%d])\n", j, 2);
- out.Write("{\n");
- if (components & (VB_HAS_COL0 << j))
- {
- out.Write("case 0:\n");
- out.Write("{\n");
- object.Write("mat.w = %s%d.w;\n", inColorName, j);
- out.Write("} break;\n");
- }
- if (components & VB_HAS_COL0)
- {
- out.Write("case 1:n");
- out.Write("{\n");
- object.Write("mat.w = %s0.w;\n", inColorName);
- out.Write("} break;\n");
- }
- out.Write("case 2:n");
- out.Write("{\n");
- object.Write("mat.w = 1.0;\n");
- out.Write("} break;\n");
- out.Write("case 3:\n");
- out.Write("{\n");
- object.Write("mat.w = %s[%d].w;\n", materialsName, j + 2);
- out.Write("} break;\n");
- out.Write("}\n");
- out.Write("switch (g_aColorChan%d[%d])\n", j, 1);
- out.Write("{\n");
- if (components & (VB_HAS_COL0 << j))
- {
- out.Write("case 0:\n");
- out.Write("{\n");
- object.Write("lacc.w = %s%d.w;\n", inColorName, j);
- out.Write("} break;\n");
- }
- if (components & VB_HAS_COL0)
- {
- out.Write("case 1:n");
- out.Write("{\n");
- object.Write("lacc.w = %s0.w;\n", inColorName);
- out.Write("} break;\n");
- }
- out.Write("case 2:n");
- out.Write("{\n");
- object.Write("lacc.w = 1.0;\n");
- out.Write("} break;\n");
- out.Write("case 3:\n");
- out.Write("{\n");
- object.Write("lacc.w = %s[%d].w;\n", materialsName, j);
- out.Write("} break;\n");
- out.Write("case 4:\n");
- out.Write("{\n");
- object.Write("lacc.w = 1.0;\n");
- out.Write("} break;\n");
- out.Write("}\n");
- out.Write("for( i = 0; i < 8; i++)");
- out.Write("{\n");
- out.Write("if (g_aColorChansLight%d%d[i] != -1)\n", "xyz", j);
- out.Write("{\n");
- GenerateGenericLightShader(out, lightsName, j, "xyz");
- out.Write("}\n");
- out.Write("if (g_aColorChansLight%d%d[i] != -1)\n", "w", j);
- out.Write("{\n");
- GenerateGenericLightShader(out, lightsName, j, "w");
- out.Write("}\n");
- out.Write("}\n");
- out.Write("%s%d = mat * clamp(lacc,0.0,1.0);\n", dest, j);
- out.Write("}\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement