add two shaderTest case for Trace

comment out one(it is not working on IOS device)
This commit is contained in:
Huabing.Xu 2014-05-09 20:34:15 -07:00
parent 4a49668940
commit e84d96234b
5 changed files with 298 additions and 38 deletions

View File

@ -1,8 +1,6 @@
#include "ShaderTest.h"
#include "../testResource.h"
#include "cocos2d.h"
#include "ShaderTest.vsh.h"
#include "shaderTest.psh.h"
static int sceneIdx = -1;
@ -12,7 +10,7 @@ static Layer* createShaderLayer(int nIndex)
{
switch (sceneIdx)
{
case 0: return new ShaderFalme();
case 0: return new ShaderLensFlare();
case 1: return new ShaderMandelbrot();
case 2: return new ShaderJulia();
case 3: return new ShaderHeart();
@ -21,10 +19,9 @@ static Layer* createShaderLayer(int nIndex)
case 6: return new ShaderBlur();
case 7: return new ShaderRetroEffect();
case 8: return new ShaderMonjori();
case 9: return new ShaderStarNest();
case 10: return new ShaderRelentless();
//case 9: return new ShaderFireBall();
case 9: return new ShaderGlow();
}
return NULL;
}
@ -713,7 +710,7 @@ bool UniformShaderNode::initWithVertex(const char *vert, const char *frag)
_time = 0;
auto s = Director::getInstance()->getWinSize();
_resolution = Vector2(s.width * CC_CONTENT_SCALE_FACTOR(), s.height * CC_CONTENT_SCALE_FACTOR());
_resolution = Vector2(s.width , s.height );
getGLProgramState()->setUniformVec2("resolution", _resolution);
scheduleUpdate();
@ -729,7 +726,7 @@ bool UniformShaderNode::initWithVertex(const char *vert, const char *frag)
void UniformShaderNode::loadShaderVertex(const char *vert, const char *frag)
{
auto shader = GLProgram::createWithByteArrays(vert, frag);
auto shader = GLProgram::createWithFilenames(vert, frag);
this->setGLProgram(shader);
}
@ -742,8 +739,8 @@ void UniformShaderNode::setPosition(const Vector2 &newPosition)
{
Node::setPosition(newPosition);
auto position = getPosition();
//_center = Vector2(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR());
// getGLProgramState()->setUniformVec2("center", _center);
_center = Vector2(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR());
getGLProgramState()->setUniformVec2("center", _center);
}
void UniformShaderNode::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
@ -778,30 +775,30 @@ void UniformShaderNode::onDraw(const Matrix &transform, bool transformUpdated)
}
ShaderRelentless::ShaderRelentless()
ShaderLensFlare::ShaderLensFlare()
{
init();
}
std::string ShaderRelentless::title() const
std::string ShaderLensFlare::title() const
{
return "ShaderToy Test";
}
std::string ShaderRelentless::subtitle() const
std::string ShaderLensFlare::subtitle() const
{
return "Relentless";
return "Lens Flare ";
}
bool ShaderRelentless::init()
bool ShaderLensFlare::init()
{
if (ShaderTestDemo::init())
{
auto sn = UniformShaderNode::shaderNodeWithVertex(shadertestvsh, shadertoyRelentlessFrag);
auto sn = UniformShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "Shaders/shadertoy_LensFlare.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
sn->setContentSize(Size(s.width,s.height));
sn->setContentSize(Size(s.width/2,s.height/2));
addChild(sn);
return true;
@ -810,30 +807,30 @@ bool ShaderRelentless::init()
return false;
}
ShaderStarNest::ShaderStarNest()
ShaderFireBall::ShaderFireBall()
{
init();
}
std::string ShaderStarNest::title() const
std::string ShaderFireBall::title() const
{
return "ShaderToy Test";
}
std::string ShaderStarNest::subtitle() const
std::string ShaderFireBall::subtitle() const
{
return "Star Nest";
return "Fire Ball";
}
bool ShaderStarNest::init()
bool ShaderFireBall::init()
{
if (ShaderTestDemo::init())
{
auto sn = UniformShaderNode::shaderNodeWithVertex(shadertestvsh, starNestFrg);
auto sn = UniformShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "Shaders/shadertoy_FireBall.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
sn->setContentSize(Size(s.width,s.height));
sn->setContentSize(Size(s.width/2,s.height/2));
addChild(sn);
return true;
@ -843,30 +840,30 @@ bool ShaderStarNest::init()
}
ShaderFalme::ShaderFalme()
ShaderGlow::ShaderGlow()
{
init();
}
std::string ShaderFalme::title() const
std::string ShaderGlow::title() const
{
return "ShaderToy Test";
}
std::string ShaderFalme::subtitle() const
std::string ShaderGlow::subtitle() const
{
return "Flame";
return "Glow";
}
bool ShaderFalme::init()
bool ShaderGlow::init()
{
if (ShaderTestDemo::init())
{
auto sn = UniformShaderNode::shaderNodeWithVertex(shadertestvsh, starFlame);
auto sn = UniformShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "Shaders/shadertoy_Glow.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
sn->setContentSize(Size(s.width,s.height));
sn->setContentSize(Size(s.width/2,s.height/2));
addChild(sn);
return true;

View File

@ -170,30 +170,30 @@ protected:
};
class ShaderRelentless : public ShaderTestDemo
class ShaderLensFlare : public ShaderTestDemo
{
public:
ShaderRelentless();
ShaderLensFlare();
virtual std::string title() const override;
virtual std::string subtitle() const override;
virtual bool init();
};
class ShaderStarNest : public ShaderTestDemo
class ShaderFireBall : public ShaderTestDemo
{
public:
ShaderStarNest();
ShaderFireBall();
virtual std::string title() const override;
virtual std::string subtitle() const override;
virtual bool init();
};
class ShaderFalme : public ShaderTestDemo
class ShaderGlow : public ShaderTestDemo
{
public:
ShaderFalme();
ShaderGlow();
virtual std::string title() const override;
virtual std::string subtitle() const override;

View File

@ -0,0 +1,55 @@
uniform vec2 center;
uniform vec2 resolution;
vec2 iCenter = center;
vec2 iResolution = resolution; // viewport resolution (in pixels)
float iGlobalTime = CC_Time[1]; // shader playback time (in seconds)
//uniform float iChannelTime[4]; // channel playback time (in seconds)
//uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)
vec4 iMouse = vec4(0,0,0,0); // mouse pixel coords. xy: current (if MLB down),
float snoise(vec3 uv, float res)
{
const vec3 s = vec3(1e0, 1e2, 1e4);
uv *= res;
vec3 uv0 = floor(mod(uv, res))*s;
vec3 uv1 = floor(mod(uv+vec3(1.), res))*s;
vec3 f = fract(uv); f = f*f*(3.0-2.0*f);
vec4 v = vec4(uv0.x+uv0.y+uv0.z, uv1.x+uv0.y+uv0.z,
uv0.x+uv1.y+uv0.z, uv1.x+uv1.y+uv0.z);
vec4 r = fract(sin(v*1e-3)*1e5);
float r0 = mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y);
r = fract(sin((v + uv1.z - uv0.z)*1e-3)*1e5);
float r1 = mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y);
return mix(r0, r1, f.z)*2.-1.;
}
void main(void)
{
//vec2 p = -.5 + gl_FragCoord.xy / iResolution.xy;
vec2 p = (gl_FragCoord.xy - center.xy) / iResolution.xy;
p.x *= iResolution.x/iResolution.y;
float color = 3.0 - (3.*length(2.*p));
vec3 coord = vec3(atan(p.x,p.y)/6.2832+.5, length(p)*.4, .5);
for(int i = 1; i <= 3; i++)
{
float power = pow(2.0, float(i));
color += (1.5 / power) * snoise(coord + vec3(0.,-iGlobalTime*.05, iGlobalTime*.01), power*16.);
}
gl_FragColor = vec4( color, pow(max(color,0.),2.)*0.4, pow(max(color,0.),3.)*0.15 , 1.0);
}

View File

@ -0,0 +1,108 @@
uniform vec2 center;
uniform vec2 resolution;
vec2 iResolution = resolution; // viewport resolution (in pixels)
float iGlobalTime = CC_Time[1]; // shader playback time (in seconds)
//uniform float iChannelTime[4]; // channel playback time (in seconds)
//uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)
vec4 iMouse = vec4(0,0,0,0); // mouse pixel coords. xy: current (if MLB down), zw: click
//uniform sampler2D iChannel0; // input channel. XX = 2D/Cube
void main(void)
{
float pointRadius = 0.06;
float linkSize = 0.04;
float noiseStrength = 0.08; // range: 0-1
float minDimension = min(iResolution.x, iResolution.y);
vec2 bounds = vec2(iResolution.x / minDimension, iResolution.y / minDimension);
//vec2 uv = gl_FragCoord.xy / minDimension;
vec2 uv = (2. * gl_FragCoord.xy - center.xy) / iResolution.xy;
vec3 pointR = vec3(0.0, 0.0, 1.0);
vec3 pointG = vec3(0.0, 0.0, 1.0);
vec3 pointB = vec3(0.0, 0.0, 1.0);
// Make the points orbit round the origin in 3 dimensions.
// Coefficients are arbitrary to give different behaviours.
// The Z coordinate should always be >0.0, as it's used directly to
// multiply the radius to give the impression of depth.
pointR.x += 0.32 * sin(1.32 * iGlobalTime);
pointR.y += 0.3 * sin(1.03 * iGlobalTime);
pointR.z += 0.4 * sin(1.32 * iGlobalTime);
pointG.x += 0.31 * sin(0.92 * iGlobalTime);
pointG.y += 0.29 * sin(0.99 * iGlobalTime);
pointG.z += 0.38 * sin(1.24 * iGlobalTime);
pointB.x += 0.33 * sin(1.245 * iGlobalTime);
pointB.y += 0.3 * sin(1.41 * iGlobalTime);
pointB.z += 0.41 * sin(1.11 * iGlobalTime);
// Centre the points in the display
vec2 midUV = vec2(bounds.x * 0.5, bounds.y * 0.5);
pointR.xy += midUV;
pointG.xy += midUV;
pointB.xy += midUV;
// Calculate the vectors from the current fragment to the coloured points
vec2 vecToR = pointR.xy - uv;
vec2 vecToG = pointG.xy - uv;
vec2 vecToB = pointB.xy - uv;
vec2 dirToR = normalize(vecToR.xy);
vec2 dirToG = normalize(vecToG.xy);
vec2 dirToB = normalize(vecToB.xy);
float distToR = length(vecToR);
float distToG = length(vecToG);
float distToB = length(vecToB);
// Calculate the dot product between vectors from the current fragment to each pair
// of adjacent coloured points. This helps us determine how close the current fragment
// is to a link between points.
float dotRG = dot(dirToR, dirToG);
float dotGB = dot(dirToG, dirToB);
float dotBR = dot(dirToB, dirToR);
// Start with a bright coloured dot around each point
gl_FragColor.x = 1.0 - smoothstep(distToR, 0.0, pointRadius * pointR.z);
gl_FragColor.y = 1.0 - smoothstep(distToG, 0.0, pointRadius * pointG.z);
gl_FragColor.z = 1.0 - smoothstep(distToB, 0.0, pointRadius * pointB.z);
gl_FragColor.w = 1.0;
// We want to show a coloured link between adjacent points.
// Determine the strength of each link at the current fragment.
// This tends towards 1.0 as the vectors to each point tend towards opposite directions.
float linkStrengthRG = 1.0 - smoothstep(dotRG, -1.01, -1.0 + (linkSize * pointR.z * pointG.z));
float linkStrengthGB = 1.0 - smoothstep(dotGB, -1.01, -1.0 + (linkSize * pointG.z * pointB.z));
float linkStrengthBR = 1.0 - smoothstep(dotBR, -1.01, -1.0 + (linkSize * pointB.z * pointR.z));
// If the current fragment is in a link, we need to know how much the
// linked points contribute of their colour.
float sumDistRG = distToR + distToG;
float sumDistGB = distToG + distToB;
float sumDistBR = distToB + distToR;
float contribRonRG = 1.0 - (distToR / sumDistRG);
float contribRonBR = 1.0 - (distToR / sumDistBR);
float contribGonRG = 1.0 - (distToG / sumDistRG);
float contribGonGB = 1.0 - (distToG / sumDistGB);
float contribBonGB = 1.0 - (distToB / sumDistGB);
float contribBonBR = 1.0 - (distToB / sumDistBR);
// Additively blend the link colours into the fragment.
gl_FragColor.x += (linkStrengthRG * contribRonRG) + (linkStrengthBR * contribRonBR);
gl_FragColor.y += (linkStrengthGB * contribGonGB) + (linkStrengthRG * contribGonRG);
gl_FragColor.z += (linkStrengthBR * contribBonBR) + (linkStrengthGB * contribBonGB);
// Use an underlying texture to provide some noise
float noiseMin = 1.0 - noiseStrength;
gl_FragColor.xyz *= (1.0 - noiseStrength) + (noiseStrength * 0.);
}

View File

@ -0,0 +1,100 @@
uniform vec2 center;
uniform vec2 resolution;
vec2 iResolution = resolution; // viewport resolution (in pixels)
float iGlobalTime = CC_Time[1]; // shader playback time (in seconds)
//uniform float iChannelTime[4]; // channel playback time (in seconds)
//uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)
vec4 iMouse = vec4(0,0,0,0); // mouse pixel coords. xy: current (if MLB down), zw: click
//uniform sampler2D iChannel0; // input channel. XX = 2D/Cube
/*by musk License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Trying to get some interesting looking lens flares.
13/08/13:
published
muuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuusk!*/
float noise(float t)
{
return 0.;
}
float noise(vec2 t)
{
return 0.;
}
vec3 lensflare(vec2 uv,vec2 pos)
{
vec2 main = uv-pos;
vec2 uvd = uv*(length(uv));
float ang = atan(main.x,main.y);
float dist=length(main); dist = pow(dist,.1);
float n = noise(vec2(ang*16.0,dist*32.0));
float f0 = 1.0/(length(uv-pos)*16.0+1.0);
f0 = f0+f0*(sin(noise((pos.x+pos.y)*2.2+ang*4.0+5.954)*16.0)*.1+dist*.1+.8);
float f1 = max(0.01-pow(length(uv+1.2*pos),1.9),.0)*7.0;
float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25;
float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23;
float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),.0)*00.21;
vec2 uvx = mix(uv,uvd,-0.5);
float f4 = max(0.01-pow(length(uvx+0.4*pos),2.4),.0)*6.0;
float f42 = max(0.01-pow(length(uvx+0.45*pos),2.4),.0)*5.0;
float f43 = max(0.01-pow(length(uvx+0.5*pos),2.4),.0)*3.0;
uvx = mix(uv,uvd,-.4);
float f5 = max(0.01-pow(length(uvx+0.2*pos),5.5),.0)*2.0;
float f52 = max(0.01-pow(length(uvx+0.4*pos),5.5),.0)*2.0;
float f53 = max(0.01-pow(length(uvx+0.6*pos),5.5),.0)*2.0;
uvx = mix(uv,uvd,-0.5);
float f6 = max(0.01-pow(length(uvx-0.3*pos),1.6),.0)*6.0;
float f62 = max(0.01-pow(length(uvx-0.325*pos),1.6),.0)*3.0;
float f63 = max(0.01-pow(length(uvx-0.35*pos),1.6),.0)*5.0;
vec3 c = vec3(.0);
c.r+=f2+f4+f5+f6; c.g+=f22+f42+f52+f62; c.b+=f23+f43+f53+f63;
c = c*1.3 - vec3(length(uvd)*.05);
c+=vec3(f0);
return c;
}
vec3 cc(vec3 color, float factor,float factor2) // color modifier
{
float w = color.x+color.y+color.z;
return mix(color,vec3(w)*factor,w*factor2);
}
void main(void)
{
//vec2 uv = gl_FragCoord.xy / iResolution.xy - 0.5;
vec2 uv = (gl_FragCoord.xy - center.xy) / iResolution.xy;
uv.x *= iResolution.x/iResolution.y; //fix aspect ratio
vec3 mouse = vec3(iMouse.xy/iResolution.xy - 0.5,iMouse.z-.5);
mouse.x *= iResolution.x/iResolution.y; //fix aspect ratio
if (iMouse.z<.5)
{
mouse.x=sin(iGlobalTime)*.5;
mouse.y=sin(iGlobalTime*.913)*.5;
}
vec3 color = vec3(1.4,1.2,1.0)*lensflare(uv,mouse.xy);
color -= noise(gl_FragCoord.xy)*.015;
color = cc(color,.5,.1);
gl_FragColor = vec4(color,1.0);
}