Thanks! And do I receive this message? âNullPointerExceptionâ for this line
inputs.pixels[ i ] = (x << 16) | y;
PImage inputs;
PGraphics grad;
PShader gradShader;
int N = 9;
float strength = 1.;
boolean bShowPoints = true;
boolean bSaveFrame = false;
void setup() {
size( 750, 1000, P3D );
//fullScreen( P3D, SPAN );
colorMode( HSB, 360,100,100 );
grad = createGraphics( width, height, P3D );
gradShader = new PShader( this, gradVert, gradFrag );
initControlPoints();
gradShader.set( "strength", strength );
noLoop();
}
void initControlPoints() {
float[][] pos = {
{ 525, 150 }, { 990, 500 }, { 550, 925 },
{ 275, 725 }, { 500, 125 }, { 250, 200 }
};
color[] cols = {
color( 179, 100, 100 ), color( 68, 100, 100 ), color( 303, 100, 100 ),
color( 299, 100, 100 ), color( 276, 100, 100 ), color( 193, 100, 100 )
};
for( int i=0; i<N; i++ ) {
int x = (int)(65536 * pos[i][0] / grad.width);
int y = (int)(65536 * pos[i][1] / grad.height);
inputs.pixels[ i ] = (x << 16) | y;
inputs.pixels[ i+N ] = cols[i];
}
//inputs = createImage( N, 2, ARGB );
//inputs.loadPixels();
//float[] hues = new float[N];
// for( int i=0; i<N; i++ ) hues[i] = random(360);
//hues = sort( hues );
//for( int i=0; i<N; i++ ) {
//float ang = TAU*i/N;
// float r = random( 0.2, 0.5 );
// int x = (int)((0.5 + r*cos(ang)) * 65536);
// int y = (int)((0.5 + r*sin(ang)) * 65536);
//color c = color( hues[i], 100, 100 );
//inputs.pixels[ i ] = (x << 16) | y;
//inputs.pixels[ i+N ] = c;
//}
//inputs.updatePixels();
//gradShader.set( "inputs", inputs );
//gradShader.set( "N", N );
}
void draw() {
gradShader.set( "bShowPoints", bShowPoints );
grad.beginDraw();
grad.noStroke();
grad.shader( gradShader );
grad.rect( 0, 0, grad.width, grad.height );
grad.resetShader();
grad.endDraw();
image( grad, 0, 0 );
if( bShowPoints )
text( str(N)+" "+str(strength), 8, 16 );
if( bSaveFrame ) {
String fileName = getClass().getSimpleName() + "-" +
nf(year(),4) + "-" + nf(month(),2) + "-" + nf(day(),2) + "-" +
nf(hour(),2) + "-" + nf(minute(),2) + "-" + nf(second(),2) + ".TIFF";
save( fileName );
println( "saved " + fileName );
bSaveFrame = false;
}
}
void keyPressed() {
if( keyCode == ENTER ) initControlPoints();
else if( keyCode == DOWN ) {
strength /= 1.1;
gradShader.set( "strength", strength );
}
else if( keyCode == UP ) {
strength *= 1.1;
gradShader.set( "strength", strength );
}
else if( keyCode == LEFT ) { N = max(N-1, 1); initControlPoints(); }
else if( keyCode == RIGHT ) { N++; initControlPoints(); }
else if( key == ' ' ) bShowPoints = !bShowPoints;
else if( key == '+' ) bSaveFrame = true;
redraw();
}
String[] gradVert = {"""
#version 330
uniform mat4 transformMatrix;
in vec4 position;
void main() {
gl_Position = transformMatrix * position;
}
"""};
String[] gradFrag = {"""
#version 330
precision highp float;
uniform vec2 resolution;
uniform sampler2D inputs;
uniform int N;
uniform bool bShowPoints;
uniform float strength;
out vec4 fragColor;
void main() {
float aspect = resolution.x/resolution.y;
vec2 uv = gl_FragCoord.xy / resolution.y;
vec3 col = vec3(0.);
float totw = 0.;
for( int i=0; i<N; i++ ) {
vec4 posData = texelFetch( inputs, ivec2( i, 0 ), 0 ) * 255.0;
float x = (posData.a * 256 + posData.r)*aspect;
float y = posData.g * 256 + posData.b;
vec2 pos = vec2( x, y ) / 65536.0;
vec3 c = texelFetch( inputs, ivec2( i, 1 ), 0 ).rgb;
float d = length( uv - pos );
float w = exp( -d*d*N*strength );
if( w > 0 ) {
col += c * w;
totw += w;
}
}
if( totw > 0. ) col /= totw;
if( bShowPoints ) {
for( int i=0; i<N; i++ ) {
vec4 posData = texelFetch( inputs, ivec2( i, 0 ), 0 ) * 255.0;
float x = (posData.a * 256 + posData.r)*aspect;
float y = posData.g * 256 + posData.b;
vec2 pos = vec2( x, y ) / 65536.0;
vec3 c = texelFetch( inputs, ivec2( i, 1 ), 0 ).rgb;
float d = length( uv - pos );
if( d < 0.01 ) col = vec3(1.);
if( d < 0.007 ) col = c;
}
}
fragColor = vec4( col, 1. );
}
"""};