Change slice syntax such that positions appear in parentheses

This allows to ignore slice declarations using `#define slice(x)`, which
mill be useful for custom mesh material shaders, allowing to write them
such that they can be used standalone in a mesh viewer but also as slices
for OpenClonk, in which case lighting and color modulation will be applied
automatically.
issue1247
Armin Burgmeier 2014-12-04 20:43:07 -05:00
parent 4981182cf8
commit 7f1cf15274
9 changed files with 27 additions and 22 deletions

View File

@ -6,13 +6,13 @@ uniform sampler2D ambientTex;
uniform mat3x2 ambientTransform; uniform mat3x2 ambientTransform;
uniform float ambientBrightness; uniform float ambientBrightness;
slice texture+6 slice(texture+6)
{ {
// Ambient light // Ambient light
float ambient = texture2D(ambientTex, ambientTransform * vec3(gl_FragCoord.xy, 1.0)).r * ambientBrightness; float ambient = texture2D(ambientTex, ambientTransform * vec3(gl_FragCoord.xy, 1.0)).r * ambientBrightness;
} }
slice light+1 slice(light+1)
{ {
// Add ambience to brightness // Add ambience to brightness
#ifdef LANDSCAPE #ifdef LANDSCAPE

View File

@ -35,7 +35,7 @@ float queryMatMap(int pix)
#endif #endif
} }
slice coordinate slice(coordinate)
{ {
// full pixel steps in the landscape texture (depends on landscape resolution) // full pixel steps in the landscape texture (depends on landscape resolution)
vec2 fullStep = vec2(1.0, 1.0) / resolution; vec2 fullStep = vec2(1.0, 1.0) / resolution;
@ -52,14 +52,14 @@ slice coordinate
vec2 materialCoo = texCoo * resolution / materialSize; vec2 materialCoo = texCoo * resolution / materialSize;
} }
slice texture slice(texture)
{ {
// our pixel color (without/with interpolation) // our pixel color (without/with interpolation)
vec4 landscapePx = texture2D(landscapeTex[0], centerCoo); vec4 landscapePx = texture2D(landscapeTex[0], centerCoo);
vec4 realLandscapePx = texture2D(landscapeTex[0], texCoo); vec4 realLandscapePx = texture2D(landscapeTex[0], texCoo);
} }
slice material slice(material)
{ {
// Get material pixels // Get material pixels
@ -75,7 +75,7 @@ slice material
#endif #endif
} }
slice normal slice(normal)
{ {
// Normal calculation // Normal calculation
vec3 normal = extend_normal(mix(realLandscapePx.yz, landscapePx.yz, scalerPx.a) vec3 normal = extend_normal(mix(realLandscapePx.yz, landscapePx.yz, scalerPx.a)
@ -91,7 +91,7 @@ slice normal
} }
slice color { slice(color) {
#define color gl_FragColor #define color gl_FragColor
color = materialPx; color = materialPx;
#ifdef HAVE_2PX #ifdef HAVE_2PX

View File

@ -13,7 +13,7 @@ uniform sampler2D lightTex;
// and therefore will often never arrive at 0 light intensity. // and therefore will often never arrive at 0 light intensity.
const float lightDarknessLevel = 8.0 / 256.0; const float lightDarknessLevel = 8.0 / 256.0;
slice texture+5 slice(texture+5)
{ {
// Query light texture // Query light texture
vec4 lightPx = texture2D(lightTex, lightCoord.st); vec4 lightPx = texture2D(lightTex, lightCoord.st);
@ -21,7 +21,7 @@ slice texture+5
vec3 lightDir = extend_normal(vec2(1.0, 1.0) - lightPx.yz * 3.0); vec3 lightDir = extend_normal(vec2(1.0, 1.0) - lightPx.yz * 3.0);
} }
slice light slice(light)
{ {
// Light direction // Light direction
float light = 2.0 * lightBright * dot(normal, lightDir); float light = 2.0 * lightBright * dot(normal, lightDir);
@ -30,7 +30,7 @@ slice light
#endif #endif
} }
slice color+5 slice(color+5)
{ {
// Add light // Add light
color = vec4(light * color.rgb, color.a); color = vec4(light * color.rgb, color.a);
@ -39,7 +39,7 @@ slice color+5
#endif #endif
} }
slice finish+5 slice(finish+5)
{ {
#ifdef LIGHT_DEBUG #ifdef LIGHT_DEBUG

View File

@ -1,7 +1,7 @@
#define HAVE_2PX #define HAVE_2PX
slice texture+5 slice(texture+5)
{ {
// find scaler coordinate // find scaler coordinate
vec2 scalerCoo = scalerOffset + mod(pixelCoo, vec2(1.0, 1.0)) * scalerPixel; vec2 scalerCoo = scalerOffset + mod(pixelCoo, vec2(1.0, 1.0)) * scalerPixel;
@ -43,7 +43,7 @@ slice texture+5
} }
slice color+10 { slice(color+10) {
// Mix second color into main color according to scaler // Mix second color into main color according to scaler
color = mix(color2, color, scalerPx.r); color = mix(color2, color, scalerPx.r);
} }

View File

@ -1,13 +1,13 @@
uniform vec4 clrMod; uniform vec4 clrMod;
slice init slice(init)
{ {
#define color gl_FragColor #define color gl_FragColor
vec4 baseColor = gl_Color; vec4 baseColor = gl_Color;
color = baseColor; color = baseColor;
} }
slice color slice(color)
{ {
// TODO: Instead of a conditional, we could compute the color as // TODO: Instead of a conditional, we could compute the color as
// color = A + B*color + C*clrMod + D*color*clrMod; // color = A + B*color + C*clrMod + D*color*clrMod;

View File

@ -3,13 +3,13 @@ uniform mat3x2 lightTransform;
uniform sampler2D normalTex; uniform sampler2D normalTex;
#endif #endif
slice texture+4 slice(texture+4)
{ {
// prepare texture coordinate for light lookup in LightShader.c // prepare texture coordinate for light lookup in LightShader.c
vec2 lightCoord = lightTransform * vec3(gl_FragCoord.xy, 1.0); vec2 lightCoord = lightTransform * vec3(gl_FragCoord.xy, 1.0);
} }
slice normal slice(normal)
{ {
#ifdef HAVE_NORMALMAP #ifdef HAVE_NORMALMAP
vec4 normalPx = texture2D(normalTex, texcoord.xy); vec4 normalPx = texture2D(normalTex, texcoord.xy);

View File

@ -1,7 +1,7 @@
uniform vec4 overlayClr; uniform vec4 overlayClr;
uniform sampler2D overlayTex; uniform sampler2D overlayTex;
slice texture+1 slice(texture+1)
{ {
// Get overlay color from overlay texture // Get overlay color from overlay texture
vec4 overlay = baseColor * overlayClr * texture2D(overlayTex, texcoord.xy); vec4 overlay = baseColor * overlayClr * texture2D(overlayTex, texcoord.xy);
@ -9,5 +9,3 @@ slice texture+1
float alpha0 = 1.0 - (1.0 - color.a) * (1.0 - overlay.a); float alpha0 = 1.0 - (1.0 - color.a) * (1.0 - overlay.a);
color = vec4(mix(color.rgb, overlay.rgb, overlay.a / alpha0), alpha0); color = vec4(mix(color.rgb, overlay.rgb, overlay.a / alpha0), alpha0);
} }

View File

@ -1,6 +1,6 @@
uniform sampler2D baseTex; uniform sampler2D baseTex;
slice texture slice(texture)
{ {
color = baseColor * texture2D(baseTex, texcoord.xy); color = baseColor * texture2D(baseTex, texcoord.xy);
} }

View File

@ -100,12 +100,19 @@ void C4Shader::AddSlices(const char *szWhat, const char *szText, const char *szS
// New slice? We need a newline followed by "slice" // New slice? We need a newline followed by "slice"
if (iDepth < 0 && isspace(*pPos)) { if (iDepth < 0 && isspace(*pPos)) {
if (SEqual2(pPos+1, "slice") && isspace(*(pPos+6))) { if (SEqual2(pPos+1, "slice") && !isalnum(*(pPos+6))) {
const char *pSliceEnd = pPos; pPos += 6; const char *pSliceEnd = pPos; pPos += 6;
while(isspace(*pPos)) pPos++;
if(*pPos != '(') { pPos++; continue; }
pPos++;
// Now let's parse the position // Now let's parse the position
iPosition = ParsePosition(szWhat, &pPos); iPosition = ParsePosition(szWhat, &pPos);
if (iPosition != -1) { if (iPosition != -1) {
// Make sure a closing parenthesis
while(isspace(*pPos)) pPos++;
if(*pPos != ')') { pPos++; continue; }
pPos++;
// Make sure an opening brace follows // Make sure an opening brace follows
while(isspace(*pPos)) pPos++; while(isspace(*pPos)) pPos++;