Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpencil_fx_swirl_frag.glsl « fx « shaders « gpencil « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ce64350b3d32e0040bfdf47f114f2605e13decf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;

uniform sampler2D strokeColor;
uniform sampler2D strokeDepth;

uniform vec2 Viewport;
uniform vec3 loc;
uniform int radius;
uniform float angle;
uniform int transparent;

uniform float pixsize;   /* rv3d->pixsize */
uniform float pixelsize; /* U.pixelsize */
uniform float pixfactor;

out vec4 FragColor;

float defaultpixsize = pixsize * pixelsize * (1000.0 / pixfactor);

/* project 3d point to 2d on screen space */
vec2 toScreenSpace(vec4 vertex)
{
	/* need to calculate ndc because this is not done by vertex shader */
	vec3 ndc = vec3(vertex).xyz / vertex.w;
					
	vec2 sc;
	sc.x = ((ndc.x + 1.0) / 2.0) * Viewport.x;
	sc.y = ((ndc.y + 1.0) / 2.0) * Viewport.y;
	
	return sc;
}

/* This swirl shader is a modified version of original Geeks3d.com code */
void main()
{
	vec2 uv = vec2(gl_FragCoord.xy);
	float stroke_depth;
	vec4 outcolor;
	
	vec4 center3d = ProjectionMatrix * ViewMatrix * vec4(loc.xyz, 1.0); 
	vec2 center = toScreenSpace(center3d);
	vec2 tc = uv - center;

	float dist = length(tc);
	float pxradius = (ProjectionMatrix[3][3] == 0.0) ? (radius / (loc.z * defaultpixsize)) : (radius / defaultpixsize);
	pxradius = max(pxradius, 1);
	
	if (dist <= pxradius) {
		float percent = (pxradius - dist) / pxradius;
		float theta = percent * percent * angle * 8.0;
		float s = sin(theta);
		float c = cos(theta);
		tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c)));
		tc += center;

		stroke_depth = texelFetch(strokeDepth, ivec2(tc), 0).r;
		outcolor = texelFetch(strokeColor, ivec2(tc), 0);
	}
	else {
		if (transparent == 1) {
			discard;
		}
		stroke_depth = texelFetch(strokeDepth, ivec2(uv), 0).r;
		outcolor = texelFetch(strokeColor, ivec2(uv), 0);
	}

	gl_FragDepth = stroke_depth;
	FragColor = outcolor;
}