@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
156 lines (138 loc) • 7.25 kB
JavaScript
/**
* Returns GLSL custom shader code
* @param shaderType vertex or fragment
* @param cameraFacing is in camera facing mode?
* @returns GLSL custom shader code
*/
/** @internal */
export function GetCustomCode(shaderType, cameraFacing) {
if (shaderType === "vertex") {
const obj = {
CUSTOM_VERTEX_DEFINITIONS: `
attribute float grl_widths;
attribute vec3 grl_offsets;
attribute float grl_colorPointers;
varying float grlCounters;
varying float grlColorPointer;
attribute vec4 grl_previousAndSide;
attribute vec4 grl_nextAndCounters;
vec2 grlFix( vec4 i, float aspect ) {
vec2 res = i.xy / i.w;
res.x *= aspect;
return res;
}
attribute vec3 grl_slopes;
attribute float grl_counters;
`,
CUSTOM_VERTEX_UPDATE_POSITION: `
vec3 grlPositionOffset = grl_offsets;
positionUpdated += grlPositionOffset;
positionUpdated = (positionUpdated + grl_offsets) + (grl_slopes * grl_widths);
`,
CUSTOM_VERTEX_MAIN_END: `
grlColorPointer = grl_colorPointers;
float grlAspect = grl_aspect_resolution_lineWidth.x;
float grlBaseWidth = grl_aspect_resolution_lineWidth.w;
vec3 grlPrevious = grl_previousAndSide.xyz;
float grlSide = grl_previousAndSide.w;
vec3 grlNext = grl_nextAndCounters.xyz;
grlCounters = grl_nextAndCounters.w;
float grlWidth = grlBaseWidth * grl_widths;
vec3 worldDir = normalize(grlNext - grlPrevious);
vec3 nearPosition = positionUpdated + (worldDir * 0.01);
mat4 grlMatrix = viewProjection * finalWorld;
vec4 grlFinalPosition = grlMatrix * vec4(positionUpdated , 1.0);
vec4 screenNearPos = grlMatrix * vec4(nearPosition, 1.0);
vec2 grlLinePosition = grlFix(grlFinalPosition, grlAspect);
vec2 grlLineNearPosition = grlFix(screenNearPos, grlAspect);
vec2 grlDir = normalize(grlLineNearPosition - grlLinePosition);
vec4 grlNormal = vec4(-grlDir.y, grlDir.x, 0., 1.);
grlNormal.xy *= -.5 * grlWidth;
grlNormal.xy *= .5 * grlWidth;
grlNormal *= grl_projection;
grlNormal.xy *= grlFinalPosition.w;
grlNormal.xy /= (vec4(grl_aspect_resolution_lineWidth.yz, 0., 1.) * grl_projection).xy;
grlFinalPosition.xy += grlNormal.xy * grlSide;
gl_Position = grlFinalPosition;
vPositionW = vec3(grlFinalPosition);
grlCounters = grl_counters;
`,
};
if (cameraFacing) {
obj["!gl_Position\\=viewProjection\\*worldPos;"] = "//"; // not needed for camera facing GRL
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return obj;
}
if (shaderType === "fragment") {
return {
CUSTOM_FRAGMENT_DEFINITIONS: `
varying float grlCounters;
varying float grlColorPointer;
uniform sampler2D grl_colors;
`,
CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR: `
float grlColorMode = grl_colorMode_visibility_colorsWidth_useColors.x;
float grlVisibility = grl_colorMode_visibility_colorsWidth_useColors.y;
float grlColorsWidth = grl_colorMode_visibility_colorsWidth_useColors.z;
float grlUseColors = grl_colorMode_visibility_colorsWidth_useColors.w;
float grlUseDash = grl_dashOptions.x;
float grlDashArray = grl_dashOptions.y;
float grlDashOffset = grl_dashOptions.z;
float grlDashRatio = grl_dashOptions.w;
grlFinalColor.a *= step(grlCounters, grlVisibility);
if(grlFinalColor.a == 0.) discard;
if(grlUseDash == 1.){
grlFinalColor.a *= ceil(mod(grlCounters + grlDashOffset, grlDashArray) - (grlDashArray * grlDashRatio));
if (grlFinalColor.a == 0.) discard;
}
if (grlColorMode == ${0 /* GreasedLineMeshColorMode.COLOR_MODE_SET */}.) {
grlFinalColor.rgb = grl_singleColor;
} else if (grlColorMode == ${1 /* GreasedLineMeshColorMode.COLOR_MODE_ADD */}.) {
grlFinalColor.rgb += grl_singleColor;
} else if (grlColorMode == ${2 /* GreasedLineMeshColorMode.COLOR_MODE_MULTIPLY */}.) {
grlFinalColor.rgb *= grl_singleColor;
}
if (grlUseColors == 1.) {
vec4 grlColor = texture2D(grl_colors, vec2(grlCounters, 0.), 0.);
vec2 lookup = vec2(fract(grlColorPointer / grl_textureSize.x), 1.0 - floor(grlColorPointer / grl_textureSize.x) / max(grl_textureSize.y - 1.0, 1.0));
vec4 grlColor = texture2D(grl_colors, lookup, 0.0);
if (grlColorMode == ${0 /* GreasedLineMeshColorMode.COLOR_MODE_SET */}.) {
grlFinalColor = grlColor;
} else if (grlColorMode == ${1 /* GreasedLineMeshColorMode.COLOR_MODE_ADD */}.) {
grlFinalColor += grlColor;
} else if (grlColorMode == ${2 /* GreasedLineMeshColorMode.COLOR_MODE_MULTIPLY */}.) {
grlFinalColor *= grlColor;
}
}
`,
};
}
return null;
}
//# sourceMappingURL=greasedLinePluginMaterialShadersGLSL.js.map