Shader does not support cocos2d?

Discuss anything about DraStic here.
Post Reply
User avatar
大小姐
Posts:3
Joined:Tue May 26, 2015 4:20 am
Shader does not support cocos2d?

Post by 大小姐 » Wed May 27, 2015 3:32 am

This is my transplant come from cocos2d shader
But drastic will be black Screen

Code: Select all

<vertex>

attribute vec4 a_color;  
attribute vec4 a_position;  
varying vec4 v_color;  
uniform mat4 u_MVPMatrix;  
void main()  
{  
    v_color = a_color;  
    gl_Position = u_MVPMatrix * a_position;  
}
</vertex>
<fragment>
varying vec4 v_color;  
 
uniform sampler2D tex0;  
precision highp float;  
uniform float time;  
uniform vec2 resolution;  
const float PI = 3.1415926535897932;  

const float speed = 0.2;  
const float speed_x = 0.3;  
const float speed_y = 0.3;  
 
const float intensity = 3.0;  
const int steps = 8;  
const float frequency = 4.0;  
const int angle = 7; 
 
const float delta = 20.0;  
const float intence = 400.0;  
const float emboss = 0.3;  
 
float col(vec2 coord)  
{  
    float delta_theta = 2.0 * PI / float(angle);  
    float col = 0.0;  
    float theta = 0.0;  
    for (int i = 0; i < steps; i++)  
    {
        vec2 adjc = coord;
    theta = delta_theta * float(i);
    adjc.x += cos(theta)*time*speed + time * speed_x;  
        adjc.y -= sin(theta)*time*speed - time * speed_y;  
        col = col + cos((adjc.x*cos(theta) - adjc.y*sin(theta))  
            *frequency)*intensity;  
    }
    return cos(col);
}  

void main(void)  
{  
    vec2 p = (gl_FragCoord.xy) / resolution.xy, c1 = p, c2 = p;  
    float cc1 = col(c1);  
 
    c2.x += resolution.x/delta;  
    float dx = emboss*(cc1-col(c2))/delta;  
 
    c2.x = p.x;  
    c2.y += resolution.y/delta;  
    float dy = emboss*(cc1-col(c2))/delta;  
 
    c1.x += dx;  
    c1.y += dy;  
    float alpha = 1.+dot(dx,dy)*intence;  
    gl_FragColor = texture2D(tex0,c1)*(alpha) *v_color*(alpha);  
}

</fragment>

User avatar
大小姐
Posts:3
Joined:Tue May 26, 2015 4:20 am

Post by 大小姐 » Wed May 27, 2015 3:37 am

Another Shader,same black screen

Code: Select all

    <vertex>
attribute vec4 a_position;
attribute vec2 a_texcoord0;

varying vec2 v_position;

void main()
{
  gl_Position = a_position;
      
  v_position = a_texcoord0;
}

    </vertex>
   <fragment>
uniform sampler2D sampler0;
varying vec2 v_position;

uniform vec2 u_texelDelta;
uniform vec2 u_pixelDelta;

const vec2 HALF_PIXEL = vec2(0.5, 0.5);

float spline36_0_1(float x) {
	return ((13.0 / 11.0 * x - 453.0 / 209.0) * x - 3.0 / 209.0) * x + 1.0;
}

float spline36_1_2(float x) {
	return ((-6.0 / 11.0 * x + 612.0 / 209.0) * x - 1038.0 / 209.0) * x + 540.0 / 209.0;
}

float spline36_2_3(float x) {
	return ((1.0 / 11.0 * x - 159.0 / 209.0) * x + 434.0 / 209.0) * x - 384.0 / 209.0;
}

vec4 rgb(int inputX, int inputY) {
	return texture2D(sampler0, (vec2(inputX, inputY) + HALF_PIXEL) * u_texelDelta);
}

vec4 interpolateHorizontally(vec2 inputPos, ivec2 inputPosFloor, int dy) {
	float sumOfWeights = 0.0;
	vec4 sumOfWeightedPixel = vec4(0.0);

	float x;
	float weight;

	x = inputPos.x - float(inputPosFloor.x - 2);
	weight = spline36_2_3(x);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * rgb(inputPosFloor.x - 2, inputPosFloor.y + dy);

	--x;
	weight = spline36_1_2(x);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * rgb(inputPosFloor.x - 1, inputPosFloor.y + dy);

	--x;
	weight = spline36_0_1(x);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * rgb(inputPosFloor.x + 0, inputPosFloor.y + dy);

	x = 1.0 - x;
	weight = spline36_0_1(x);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * rgb(inputPosFloor.x + 1, inputPosFloor.y + dy);

	++x;
	weight = spline36_1_2(x);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * rgb(inputPosFloor.x + 2, inputPosFloor.y + dy);

	++x;
	weight = spline36_2_3(x);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * rgb(inputPosFloor.x + 3, inputPosFloor.y + dy);

	return sumOfWeightedPixel / sumOfWeights;
}

vec4 process(vec2 outputPos) {
	vec2 inputPos = outputPos / u_texelDelta;
	ivec2 inputPosFloor = ivec2(inputPos);

	// Vertical interporation
	float sumOfWeights = 0.0;
	vec4 sumOfWeightedPixel = vec4(0.0);

	float weight;
	float y;

	y = inputPos.y - float(inputPosFloor.y - 2);
	weight = spline36_2_3(y);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * interpolateHorizontally(inputPos, inputPosFloor, -2);

	--y;
	weight = spline36_1_2(y);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * interpolateHorizontally(inputPos, inputPosFloor, -1);

	--y;
	weight = spline36_0_1(y);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * interpolateHorizontally(inputPos, inputPosFloor, +0);

	y = 1.0 - y;
	weight = spline36_0_1(y);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * interpolateHorizontally(inputPos, inputPosFloor, +1);

	++y;
	weight = spline36_1_2(y);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * interpolateHorizontally(inputPos, inputPosFloor, +2);

	++y;
	weight = spline36_2_3(y);
	sumOfWeights += weight;
	sumOfWeightedPixel += weight * interpolateHorizontally(inputPos, inputPosFloor, +3);

	return vec4((sumOfWeightedPixel / sumOfWeights).xyz, 1.0);
}

void main()
{
  gl_FragColor.rgba = process(v_position);
}
	</fragment>

Post Reply