[cocos]使用shader画一个色盘

fsh

#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;



float angle(vec2 center, vec2 point)
{
	float angle = atan( (center.y - point.y) , (center.x - point.x) );
	angle = (angle >= 0.0) ? angle : 2.0 * 3.141592654 + angle;
	float singlePice = 360.0 / (3.141592654 * 2.0);
	return angle * singlePice;
}

float getColorWidget(float angle, float max, float min)
{
	float widget = 0.0;

	float _angle = angle - min;
	float _max = max - min;
	float _min = 0.0;
	float middle = _max * 0.5;

	if (_angle > _min && _angle <= _max)
		widget = ( _angle > middle ? _max - _angle : _angle ) / middle;
	return widget;
}

void main() 
{
	vec4 sum = texture2D(CC_Texture0, v_texCoord);
	float angle = angle(vec2(0.5, 0.5), vec2( v_texCoord.x, v_texCoord.y ));
	vec2 cord = vec2( abs( 0.5 - v_texCoord.x ), abs( 0.5 - v_texCoord.y) );
	float dis = sqrt((cord.x * cord.x + cord.y * cord.y));
	if (dis > 0.25)
	{
		sum.r = 0.0;
		sum.g = 0.0;
		sum.b = 0.0;
	}
	else
	{
		sum.r = getColorWidget(angle, 180.0, 0.0);
		sum.g = getColorWidget(angle, 300.0, 120.0);
		sum.b = getColorWidget(angle < 60.0 ? angle + 360.0 : angle, 420.0, 240.0);
	}
	sum.a = 1.0;
	gl_FragColor = sum * v_fragmentColor;
}

另外一个fsh

#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;



float angle(vec2 center, vec2 point)
{
	float angle = atan( (center.y - point.y) , (center.x - point.x) );
	angle = (angle >= 0.0) ? angle : 2.0 * 3.141592654 + angle;
	float singlePice = 360.0 / (3.141592654 * 2.0);
	return angle * singlePice;
}

float getColorWidget(float angle, float max, float min)
{
	float widget = 0.0;

	float _angle = angle - min;
	float _max = max - min;
	float _min = 0.0;
	float middle = _max * 0.5;

	if (_angle > _min && _angle <= _max)
		widget = ( _angle > middle ? _max - _angle : _angle ) / middle;
	return widget;
}

void main() 
{
	vec4 sum = texture2D(CC_Texture0, v_texCoord);
	if (sum.a > 0.01)
	{
		float angle = angle(vec2(0.5, 0.5), vec2( v_texCoord.x, v_texCoord.y ));
		vec2 cord = vec2( abs( 0.5 - v_texCoord.x ), abs( 0.5 - v_texCoord.y) );
		float dis = sqrt((cord.x * cord.x + cord.y * cord.y));
		if (dis > 0.25)
		{
			sum.r = 0.0;
			sum.g = 0.0;
			sum.b = 0.0;
			sum.a = 0.0;
		}
		else
		{
			sum.r = getColorWidget(angle, 180.0, 0.0);
			sum.g = getColorWidget(angle, 300.0, 120.0);
			sum.b = getColorWidget(angle < 60.0 ? angle + 360.0 : angle, 420.0, 240.0);
			sum.a = 1.0;
		}
	}
	gl_FragColor = sum * v_fragmentColor;
}

 

vsh

attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
                    

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
                                
void main()
{                    
    gl_Position = CC_PMatrix * a_position;
    v_fragmentColor = a_color;
    v_texCoord = a_texCoord;
} 

lua


    local prog = cc.GLProgram:create("test.vsh","sepan.fsh")
    prog:link()
    prog:updateUniforms()
    local progStat= cc.GLProgramState:create(prog)

    local sp = cc.Sprite:create("123123123123.png")
                        :pos(display.cx, display.cy)
                        :addTo(self)
                        :setScale(1)
                        :setAnchorPoint(0.5, 0.5)

    sp:setGLProgram(prog)
    sp:setGLProgramState(progStat)

 

[cocos]使用shader画一个色盘_第1张图片

[cocos]使用shader画一个色盘_第2张图片

[cocos]使用shader画一个色盘_第3张图片

你可能感兴趣的:(OpenGL,Cocos2d,lua)