Last active
July 30, 2019 01:50
-
-
Save PEDx/a7796dac2c5ee9c06331e084e02fbe2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var VSHADER_SOURCE = | |
"attribute vec4 a_position; \n" + | |
"attribute vec2 a_texCoord; \n" + | |
"varying mediump vec2 v_texCoord; \n" + | |
"uniform mat4 u_ModelMatrix; \n" + // 变换矩阵 | |
"uniform float u_Radius; \n" + // 半径 | |
"uniform float u_ArcLength; \n" + // 弯曲弧长 | |
"uniform int u_Direction; \n" + // 卷曲方向 | |
"const float PI = 3.1415926 ; \n" + | |
"const int DIRECTION_LEFT = 1 ; \n" + | |
"const int DIRECTION_RIGHT = 2 ; \n" + | |
"const int DIRECTION_BOTTOM = 3 ; \n" + | |
"vec3 cal_pos(float radius, float length, int direction, vec3 position); \n" + | |
"bool predicate(float arc,float len,float x,float y); \n" + | |
"float cal_distance(float arc,float len,float x,float y); \n" + | |
"void main() \n" + | |
"{ \n" + | |
" vec3 result_vec3 = vec3(a_position.x, a_position.y, a_position.z) ; \n" + | |
// 判定需要弯曲的牌面坐标 | |
" result_vec3 = cal_pos(u_Radius, u_ArcLength, u_Direction, result_vec3);\n" + | |
" vec4 a_temp = vec4(result_vec3.x, result_vec3.y, result_vec3.z , a_position.w); \n" + | |
" gl_Position = (CC_PMatrix * CC_MVMatrix * u_ModelMatrix) * a_temp; \n" + | |
" v_texCoord = a_texCoord; \n" + | |
"} \n" + | |
"vec3 cal_pos(float radius, float length, int direction, vec3 position) {\n" + | |
" float _pos = direction == DIRECTION_BOTTOM ? position.y : position.x ;\n" + | |
" if(_pos >= length && direction != DIRECTION_RIGHT) {\n" + | |
" return position;\n" + | |
" }else if(direction == DIRECTION_RIGHT && _pos <= length) {\n" + | |
" return position;\n" + | |
" } \n" + | |
" if (direction == DIRECTION_RIGHT) {\n" + | |
" _pos = -_pos; \n" + | |
" }\n" + | |
" float sign = pow(_pos, -1.0) * abs(_pos) == -1.0 ? -1.0 : 1.0; \n" + | |
" float _arc = (sign * length - _pos) / radius;\n" + | |
" float posZ = radius * sin(_arc / 2.0);\n" + | |
" _pos = length - sign * radius * sin(_arc);\n" + | |
" if (_arc > PI) {\n" + | |
" posZ = radius;\n" + | |
" _pos = length + sign * (_arc - PI) * radius;\n" + | |
" }\n" + | |
" if (direction == DIRECTION_BOTTOM) {\n" + | |
" position.y = _pos ;\n" + | |
" }else {\n" + | |
" position.x = _pos ;\n" + | |
" }\n" + | |
" return vec3(position.x, position.y, posZ);\n" + | |
"} \n" + | |
' bool predicate(float arc,float len,float x,float y) { \n' + | |
' if(arc == 0.0) return y < len; \n' + | |
' float _arc = PI / 2.0 - arc; \n' + | |
' float _x = len / cos(_arc); \n' + | |
' if (x * tan(_arc) + _x <= y) return false; \n' + | |
' return true; \n' + | |
' } \n' + | |
' float cal_distance(float arc,float len,float x,float y) { \n' + | |
' float _arc = PI / 2.0 - arc; \n' + | |
' float _x = len / cos(_arc); \n' + | |
' return abs(tan(_arc) * x - y + _x) / sqrt( pow(tan(_arc), 2.0) + 1.0); \n' + | |
' } '; | |
// Fragment shader program | |
var FSHADER_SOURCE = | |
"precision lowp float; \n" + | |
"varying vec2 v_texCoord; \n" + | |
"uniform sampler2D u_Sampler; \n" + | |
"void main() \n" + | |
"{ \n" + | |
" vec4 color1 = texture2D(u_Sampler, v_texCoord); \n" + | |
" gl_FragColor = color1; \n" + | |
"}"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment