-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
4,136 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
packages/webgl/src/Shader/Fragment/Filter/FragmentShaderSourceBlurFilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @description ブラーフィルタのフラグメントシェーダーソース | ||
* Fragment shader source of blur filter | ||
* | ||
* @param {number} half_blur | ||
* @return {string} | ||
* @method | ||
* @public | ||
*/ | ||
export const getFragmentSource = (half_blur: number): string => | ||
{ | ||
const halfBlurFixed: string = half_blur.toFixed(1); | ||
|
||
return `#version 300 es | ||
precision mediump float; | ||
uniform sampler2D u_texture; | ||
uniform vec4 u_mediump; | ||
in vec2 v_coord; | ||
out vec4 o_color; | ||
void main() { | ||
vec2 offset = u_mediump.xy; | ||
float fraction = u_mediump.z; | ||
float samples = u_mediump.w; | ||
vec4 color = texture(u_texture, v_coord); | ||
for (float i = 1.0; i < ${halfBlurFixed}; i += 1.0) { | ||
color += texture(u_texture, v_coord + offset * i); | ||
color += texture(u_texture, v_coord - offset * i); | ||
} | ||
color += texture(u_texture, v_coord + offset * ${halfBlurFixed}) * fraction; | ||
color += texture(u_texture, v_coord - offset * ${halfBlurFixed}) * fraction; | ||
color /= samples; | ||
o_color = color; | ||
}`; | ||
|
||
}; | ||
|
||
/** | ||
* @class | ||
*/ | ||
export class FragmentShaderSourceBlurFilter | ||
{ | ||
/** | ||
* @param {number} half_blur | ||
* @return {string} | ||
* @method | ||
* @static | ||
*/ | ||
static TEMPLATE (half_blur: number): string | ||
{ | ||
const halfBlurFixed: string = half_blur.toFixed(1); | ||
|
||
return `#version 300 es | ||
precision mediump float; | ||
uniform sampler2D u_texture; | ||
uniform vec4 u_mediump; | ||
in vec2 v_coord; | ||
out vec4 o_color; | ||
void main() { | ||
vec2 offset = u_mediump.xy; | ||
float fraction = u_mediump.z; | ||
float samples = u_mediump.w; | ||
vec4 color = texture(u_texture, v_coord); | ||
for (float i = 1.0; i < ${halfBlurFixed}; i += 1.0) { | ||
color += texture(u_texture, v_coord + offset * i); | ||
color += texture(u_texture, v_coord - offset * i); | ||
} | ||
color += texture(u_texture, v_coord + offset * ${halfBlurFixed}) * fraction; | ||
color += texture(u_texture, v_coord - offset * ${halfBlurFixed}) * fraction; | ||
color /= samples; | ||
o_color = color; | ||
}`; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/webgl/src/Shader/Fragment/Filter/FragmentShaderSourceColorMatrixFilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @class | ||
*/ | ||
export class FragmentShaderSourceColorMatrixFilter | ||
{ | ||
/** | ||
* @return {string} | ||
* @method | ||
* @static | ||
*/ | ||
static TEMPLATE (): string | ||
{ | ||
return `#version 300 es | ||
precision mediump float; | ||
uniform sampler2D u_texture; | ||
uniform vec4 u_mediump[5]; | ||
in vec2 v_coord; | ||
out vec4 o_color; | ||
void main() { | ||
mat4 mul = mat4(u_mediump[0], u_mediump[1], u_mediump[2], u_mediump[3]); | ||
vec4 add = u_mediump[4]; | ||
vec4 color = texture(u_texture, v_coord); | ||
color.rgb /= max(0.0001, color.a); | ||
color = clamp(color * mul + add, 0.0, 1.0); | ||
color.rgb *= color.a; | ||
o_color = color; | ||
}`; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
packages/webgl/src/Shader/Fragment/Filter/FragmentShaderSourceConvolutionFilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { FUNCTION_IS_INSIDE } from "../FragmentShaderLibrary"; | ||
|
||
/** | ||
* @class | ||
*/ | ||
export class FragmentShaderSourceConvolutionFilter | ||
{ | ||
/** | ||
* @param {number} mediump_length | ||
* @param {number} x | ||
* @param {number} y | ||
* @param {boolean} preserve_alpha | ||
* @param {boolean} clamp | ||
* @return {string} | ||
* @method | ||
* @static | ||
*/ | ||
static TEMPLATE ( | ||
mediump_length: number, | ||
x: number, y: number, | ||
preserve_alpha: boolean, clamp: boolean | ||
): string { | ||
|
||
const halfX: number = Math.floor(x * 0.5); | ||
const halfY: number = Math.floor(y * 0.5); | ||
const size: number = x * y; | ||
|
||
let matrixStatement: string = ""; | ||
const matrixIndex: number = clamp ? 1 : 2; | ||
for (let idx: number = 0; idx < size; ++idx) { | ||
|
||
const index: number = matrixIndex + Math.floor(idx / 4); | ||
|
||
const component: number = idx % 4; | ||
|
||
matrixStatement += ` | ||
result += getWeightedColor(${idx}, u_mediump[${index}][${component}]); | ||
`; | ||
} | ||
|
||
const preserve_alphaStatement: string = preserve_alpha | ||
? "result.a = texture(u_texture, v_coord).a;" | ||
: ""; | ||
const clampStatement: string = clamp | ||
? "" | ||
: ` | ||
vec4 substitute_color = u_mediump[1]; | ||
color = mix(substitute_color, color, isInside(uv)); | ||
`; | ||
|
||
return `#version 300 es | ||
precision mediump float; | ||
uniform sampler2D u_texture; | ||
uniform vec4 u_mediump[${mediump_length}]; | ||
in vec2 v_coord; | ||
out vec4 o_color; | ||
${FUNCTION_IS_INSIDE()} | ||
vec4 getWeightedColor (in int i, in float weight) { | ||
vec2 rcp_size = u_mediump[0].xy; | ||
int i_div_x = i / ${x}; | ||
int i_mod_x = i - ${x} * i_div_x; | ||
vec2 offset = vec2(i_mod_x - ${halfX}, ${halfY} - i_div_x); | ||
vec2 uv = v_coord + offset * rcp_size; | ||
vec4 color = texture(u_texture, uv); | ||
color.rgb /= max(0.0001, color.a); | ||
${clampStatement} | ||
return color * weight; | ||
} | ||
void main() { | ||
float rcp_divisor = u_mediump[0].z; | ||
float bias = u_mediump[0].w; | ||
vec4 result = vec4(0.0); | ||
${matrixStatement} | ||
result = clamp(result * rcp_divisor + bias, 0.0, 1.0); | ||
${preserve_alphaStatement} | ||
result.rgb *= result.a; | ||
o_color = result; | ||
}`; | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
packages/webgl/src/Shader/Fragment/Filter/FragmentShaderSourceDisplacementMapFilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { FUNCTION_IS_INSIDE } from "../FragmentShaderLibrary"; | ||
|
||
/** | ||
* @class | ||
*/ | ||
export class FragmentShaderSourceDisplacementMapFilter | ||
{ | ||
/** | ||
* @param {number} mediump_length | ||
* @param {number} component_x | ||
* @param {number} component_y | ||
* @param {string} mode | ||
* @return {string} | ||
* @method | ||
* @static | ||
*/ | ||
static TEMPLATE ( | ||
mediump_length: number, | ||
component_x: number, component_y: number, | ||
mode: string | ||
): string { | ||
|
||
let cx: string; | ||
let cy: string; | ||
let modeStatement: string; | ||
|
||
switch (component_x) { | ||
|
||
case 1: // BitmapDataChannel.RED | ||
cx = "map_color.r"; | ||
break; | ||
|
||
case 2: // BitmapDataChannel.GREEN | ||
cx = "map_color.g"; | ||
break; | ||
|
||
case 4: // BitmapDataChannel.BLUE | ||
cx = "map_color.b"; | ||
break; | ||
|
||
case 8: // BitmapDataChannel.ALPHA | ||
cx = "map_color.a"; | ||
break; | ||
|
||
default: | ||
cx = "0.5"; | ||
break; | ||
|
||
} | ||
|
||
switch (component_y) { | ||
|
||
case 1: // BitmapDataChannel.RED | ||
cy = "map_color.r"; | ||
break; | ||
|
||
case 2: // BitmapDataChannel.GREEN | ||
cy = "map_color.g"; | ||
break; | ||
|
||
case 4: // BitmapDataChannel.BLUE | ||
cy = "map_color.b"; | ||
break; | ||
|
||
case 8: // BitmapDataChannel.ALPHA | ||
cy = "map_color.a"; | ||
break; | ||
|
||
default: | ||
cy = "0.5"; | ||
break; | ||
|
||
} | ||
|
||
switch (mode) { | ||
|
||
case "clamp": | ||
modeStatement = ` | ||
vec4 source_color = texture(u_textures[0], uv); | ||
`; | ||
break; | ||
|
||
case "ignore": | ||
// 置き換え後の座標が範囲外なら、置き換え前の座標をとる(x軸とy軸を別々に判定する) | ||
modeStatement = ` | ||
vec4 source_color =texture(u_textures[0], mix(v_coord, uv, step(abs(uv - vec2(0.5)), vec2(0.5)))); | ||
`; | ||
break; | ||
|
||
case "color": | ||
modeStatement = ` | ||
vec4 substitute_color = u_mediump[2]; | ||
vec4 source_color = mix(substitute_color, texture(u_textures[0], uv), isInside(uv)); | ||
`; | ||
break; | ||
|
||
case "wrap": | ||
default: | ||
modeStatement = ` | ||
vec4 source_color = texture(u_textures[0], fract(uv)); | ||
`; | ||
break; | ||
} | ||
|
||
return `#version 300 es | ||
precision mediump float; | ||
uniform sampler2D u_textures[2]; | ||
uniform vec4 u_mediump[${mediump_length}]; | ||
in vec2 v_coord; | ||
out vec4 o_color; | ||
${FUNCTION_IS_INSIDE()} | ||
void main() { | ||
vec2 uv_to_st_scale = u_mediump[0].xy; | ||
vec2 uv_to_st_offset = u_mediump[0].zw; | ||
vec2 scale = u_mediump[1].xy; | ||
vec2 st = v_coord * uv_to_st_scale - uv_to_st_offset; | ||
vec4 map_color = texture(u_textures[1], st); | ||
vec2 offset = vec2(${cx}, ${cy}) - 0.5; | ||
vec2 uv = v_coord + offset * scale; | ||
${modeStatement} | ||
o_color = mix(texture(u_textures[0], v_coord), source_color, isInside(st)); | ||
}`; | ||
} | ||
} |
Oops, something went wrong.