🚚 Move shader code to GLSL files
This commit is contained in:
+4
-68
@@ -142,7 +142,6 @@ var BrokenGlass = class BrokenGlass {
|
||||
if (utils.isInShellProcess()) {
|
||||
|
||||
const {Clutter, GdkPixbuf, Cogl, Shell} = imports.gi;
|
||||
const shaderSnippets = Me.imports.src.shaderSnippets;
|
||||
|
||||
// This shader creates a complex-looking effect with rather simple means. Here is how it
|
||||
// works: The window is drawn five times on top of each other (see the SHARD_LAYERS
|
||||
@@ -214,74 +213,11 @@ if (utils.isInShellProcess()) {
|
||||
// This is called by the constructor. This means, it's only called when the effect
|
||||
// is used for the first time.
|
||||
vfunc_build_pipeline() {
|
||||
const declarations = `
|
||||
// Inject some common shader snippets.
|
||||
${shaderSnippets.standardUniforms()}
|
||||
const decl =
|
||||
utils.loadGLSLResource(`/shaders/${BrokenGlass.getNick()}-declarations.glsl`);
|
||||
const code = utils.loadGLSLResource(`/shaders/${BrokenGlass.getNick()}.glsl`);
|
||||
|
||||
uniform sampler2D uShardTexture;
|
||||
uniform vec2 uSeed;
|
||||
uniform vec2 uEpicenter;
|
||||
uniform float uShardScale;
|
||||
uniform float uBlowForce;
|
||||
uniform float uGravity;
|
||||
|
||||
const float SHARD_LAYERS = 5;
|
||||
const float ACTOR_SCALE = 2.0;
|
||||
const float PADDING = ACTOR_SCALE / 2.0 - 0.5;
|
||||
`;
|
||||
|
||||
const code = `
|
||||
cogl_color_out = vec4(0, 0, 0, 0);
|
||||
|
||||
float progress = uForOpening ? 1.0-uProgress : uProgress;
|
||||
|
||||
// Draw the individual shard layers.
|
||||
for (float i=0; i<SHARD_LAYERS; ++i) {
|
||||
|
||||
// To enable drawing shards outside of the window bounds, the actor was scaled
|
||||
// by ACTOR_SCALE. Here we scale and move the texture coordinates so that the
|
||||
// window gets drawn at the correct position again.
|
||||
vec2 coords = cogl_tex_coord_in[0].st * ACTOR_SCALE - PADDING;
|
||||
|
||||
// Scale and rotate around our epicenter.
|
||||
coords -= uEpicenter;
|
||||
|
||||
// Scale each layer a bit differently.
|
||||
coords /= mix(1.0, 1.0 + uBlowForce*(i+2)/SHARD_LAYERS, progress);
|
||||
|
||||
// Rotate each layer a bit differently.
|
||||
float rotation = (mod(i, 2.0)-0.5)*0.2*progress;
|
||||
coords = vec2(coords.x * cos(rotation) - coords.y * sin(rotation),
|
||||
coords.x * sin(rotation) + coords.y * cos(rotation));
|
||||
|
||||
// Move down each layer a bit.
|
||||
float gravity = (uForOpening ? -1.0 : 1.0) * uGravity*0.1*(i+1)*progress*progress;
|
||||
coords += vec2(0, gravity);
|
||||
|
||||
// Restore correct position.
|
||||
coords += uEpicenter;
|
||||
|
||||
// Retrieve information from the shard texture for our layer.
|
||||
vec2 shardCoords = (coords + uSeed) * uSize / uShardScale / 500.0;
|
||||
vec2 shardMap = texture2D(uShardTexture, shardCoords).rg;
|
||||
|
||||
// The green channel contains a random value in [0..1] for each shard. We
|
||||
// discretize this into SHARD_LAYERS bins and check if our layer falls into
|
||||
// the bin of the current shard.
|
||||
float shardGroup = floor(shardMap.g * SHARD_LAYERS * 0.999);
|
||||
|
||||
if (shardGroup == i && (shardMap.x - pow(progress+0.1, 2)) > 0) {
|
||||
cogl_color_out = texture2D(uTexture, coords);
|
||||
}
|
||||
}
|
||||
|
||||
// Shell.GLSLEffect uses straight alpha. So we have to convert from premultiplied.
|
||||
if (cogl_color_out.a > 0) {
|
||||
cogl_color_out.rgb /= cogl_color_out.a;
|
||||
}
|
||||
`;
|
||||
|
||||
this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, declarations, code, true);
|
||||
this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, decl, code, true);
|
||||
}
|
||||
|
||||
// This is overridden to bind the shard texture for drawing. Sadly, this seems to be
|
||||
|
||||
Reference in New Issue
Block a user