Tuesday, April 16, 2024
HomeSample Page

Sample Page Title


If you scroll down the feedback on that web page you linked, you’ll see a put up with an easier implementation of the shader:

void mainImage( out vec4 O, vec2 U )
{
	float t = iTime/.1 + iMouse.x;
	vec2  R =  iResolution.xy, S = vec2(160,100),
          p = ( U+U - R ) / R * S,
          q = vec2(cos(-t / 165.), cos( t / 45.))  * S - p;
    t = 1. + cos( size( vec2(cos( t / 98.),  sin( t / 178.)) * S - p ) / 30.) 
           + cos( size( vec2(sin(-t / 124.), cos( t / 104.)) * S - p ) / 20.) 
           + sin( size(q) / 25. ) * sin(q.x / 20.) * sin(q.y / 15.);
    O = .5 + .5* cos( (iTime+iMouse.y) / vec4(63,78,45,1) + ( t + vec4(0,1,-.5,0) ) *3.14 );
}

That’s simply tailored to Cocos2d-x v4 or the axmol fork (identical code):

#ifdef GL_ES
precision mediump float;
#endif

various vec4 v_fragmentColor;
various vec2 v_texCoord;

uniform float u_time; // time in seconds
uniform vec2 u_resolution; 
uniform vec2 u_position; 

const float MATH_PI	= float(3.14159265359);

void fundamental()
{
	float t = u_time / .1 + u_position.x;
	vec2  R =  u_resolution.xy, S = vec2(160,100),
          p = (v_texCoord + v_texCoord - R) / R * S,
          q = vec2(cos(-t / 165.), cos( t / 45.))  * S - p;
    t = 1. + cos( size( vec2(cos( t / 98.),  sin( t / 178.)) * S - p ) / 30.) 
           + cos( size( vec2(sin(-t / 124.), cos( t / 104.)) * S - p ) / 20.) 
           + sin( size(q) / 25. ) * sin(q.x / 20.) * sin(q.y / 15.);
    gl_FragColor = .5 + .5 * cos((u_time + u_position.y) / vec4(63, 78, 45, 1) + ( t + vec4(0, 1, -.5, 0) ) * MATH_PI);
}

To use the shader, create this system first:

auto* program = Device::getInstance()->newProgram(positionTextureColor_vert, plasma_frag);

then get a programState from it:
_plasmaProgramState= new (std::nothrow) ProgramState(program);

Then arrange the required uniforms:

_plasmaTimeLoc = _plasmaProgramState->getUniformLocation("u_time");
_plasmaResolutionLoc = _plasmaProgramState->getUniformLocation("u_resolution");
_plasmaPosLoc = _plasmaProgramState->getUniformLocation("u_position");

// Set preliminary values
auto time = 0.f;
_plasmaProgramState->setUniform(_plasmaTimeLoc, &time, sizeof(time));

auto decision = Vec2(node->getContentSize()) / node->getContentSize().width;
_plasmaProgramState->setUniform(_plasmaResolutionLoc, &decision, sizeof(decision));

// It's equal to iMouse within the authentic shader. You can use contact coordinates if you wish to get the 
// identical impact.
auto place = Vec2(node->getContentSize()) /  2; 
_plasmaProgramState->setUniform(_plasmaPosLoc, &place, sizeof(place));

Then schedule an replace to deal with the time:

void replace(float dt)
{
    _timeInSeconds += dt;
    _plasmaProgramState->setUniform(_plasmaTimeLoc, &_timeInSeconds, sizeof(_timeInSeconds));
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments