twinkling stars
in
Programming Questions
•
2 years ago
Is it possible to make the 'stars' 'twinkle' all over the screen when the mouseX position moves instead of the spotlight that I have going on right now? This is my sketch so far:
int[][] fire;
// Flame colors
color[] palette;
float angle;
int[] calc1,calc2,calc3,calc4,calc5;
PGraphics pg;
int numCircles = 500;
Circle[] circles = new Circle[numCircles];
void setup(){
size(1440, 800, P2D);
noFill();
smooth();
for (int i=0; i<numCircles; i++){
circles[i] = new Circle(random(width),random(height));
}
// Create buffered image for 3d cube
pg = createGraphics(width, height, P3D);
calc1 = new int[width];
calc3 = new int[width];
calc4 = new int[width];
calc2 = new int[height];
calc5 = new int[height];
colorMode(HSB);
fire = new int[width][height];
palette = new color[250];
// Generate the palette
for(int x = 0; x < palette.length; x++) {
//Hue 0 to 85: red to yellow
//Saturation is maximum: 255
//Lightness is 0..255 for x=0..128, and 255 for x=128..255
palette[x] = color(x/3, 255, constrain(x*3, 5, 255));
}
// this speeds up the effect by 10fps
for (int x = 0; x < width; x++) {
calc1[x] = x % width;
calc3[x] = (x - 1 + width) % width;
calc4[x] = (x + 1) % width;
}
for(int y = 0; y < height; y++) {
calc2[y] = (y + 1) % height;
calc5[y] = (y + 2) % height;
}
void draw() {
angle = angle + 0.05;
// draw cube
pg.beginDraw();
pg.translate(width >> 1, height >> 1);
pg.rotateX(sin(angle/2));
pg.rotateY(cos(angle/2));
pg.background(0);
pg.stroke(128);
pg.scale(mouseY/4);
pg.noFill();
pg.box(4);
pg.endDraw();
loadPixels();
int counter = 0;
// Do the fire calculations for every pixel, from top to bottom
for (int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
fire[x][y] =
((fire[calc3[x]][calc2[y]]
+ fire[calc1[x]][calc2[y]]
+ fire[calc4[x]][calc2[y]]
+ fire[calc1[x]][calc5[y]]) << 5) / 129;
// Output everything to screen using our palette colors
pixels[counter] = palette[fire[x][y]];
// Extract the red value using right shift and bit mask
// equivalent of red(pg.pixels[x+y*w])
if ((pg.pixels[counter++] >> 16 & 0xFF) == 128) {
// Only map 3D cube 'lit' pixels onto fire array needed for next frame
fire[x][y] = 128;
}
}
}
updatePixels();
}
if(keyPressed) {
if (key == 'a' || key == 'A') {
fill(0x90880000);
rect(random(height),random(width),random(height),random(width));
}
if (key == 'b' || key == 'B') {
fill(0x90000066);
rect(random(width),random(height),random(width),random(height));
}
if (key == 'c' || key == 'C') {
fill(0x90005500);
rect(random(height),random(width),random(height),random(width));
}
if (key == 'd' || key == 'D') {
fill(0x90330066);
rect(random(width),random(height),random(width),random(height));
}
if (key == 'e' || key == 'E') {
fill(0x90777777);
rect(random(height),random(width),random(height),random(width));
}
}
for (int i=0; i<numCircles; i++) {
circles[i].display(); // display all the circles
}
}
class Circle {
float x,y; // location
float dim; // dimension
color c; // color
Circle(float x, float y) {
this.x = x;
this.y = y;
dim = random(5);
c = color(random(55));
}
void display() {
float distance = dist(x,y,mouseX,mouseY); // distance between circle and mouse
if (distance < 355) { // if distance is smaller than 255
fill(355-distance);
} else { // if distance is bigger than 255
fill(c);
}
ellipse(x,y,dim,dim); // a circle at position xy
}
}
int[][] fire;
// Flame colors
color[] palette;
float angle;
int[] calc1,calc2,calc3,calc4,calc5;
PGraphics pg;
int numCircles = 500;
Circle[] circles = new Circle[numCircles];
void setup(){
size(1440, 800, P2D);
noFill();
smooth();
for (int i=0; i<numCircles; i++){
circles[i] = new Circle(random(width),random(height));
}
// Create buffered image for 3d cube
pg = createGraphics(width, height, P3D);
calc1 = new int[width];
calc3 = new int[width];
calc4 = new int[width];
calc2 = new int[height];
calc5 = new int[height];
colorMode(HSB);
fire = new int[width][height];
palette = new color[250];
// Generate the palette
for(int x = 0; x < palette.length; x++) {
//Hue 0 to 85: red to yellow
//Saturation is maximum: 255
//Lightness is 0..255 for x=0..128, and 255 for x=128..255
palette[x] = color(x/3, 255, constrain(x*3, 5, 255));
}
// this speeds up the effect by 10fps
for (int x = 0; x < width; x++) {
calc1[x] = x % width;
calc3[x] = (x - 1 + width) % width;
calc4[x] = (x + 1) % width;
}
for(int y = 0; y < height; y++) {
calc2[y] = (y + 1) % height;
calc5[y] = (y + 2) % height;
}
void draw() {
angle = angle + 0.05;
// draw cube
pg.beginDraw();
pg.translate(width >> 1, height >> 1);
pg.rotateX(sin(angle/2));
pg.rotateY(cos(angle/2));
pg.background(0);
pg.stroke(128);
pg.scale(mouseY/4);
pg.noFill();
pg.box(4);
pg.endDraw();
loadPixels();
int counter = 0;
// Do the fire calculations for every pixel, from top to bottom
for (int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
fire[x][y] =
((fire[calc3[x]][calc2[y]]
+ fire[calc1[x]][calc2[y]]
+ fire[calc4[x]][calc2[y]]
+ fire[calc1[x]][calc5[y]]) << 5) / 129;
// Output everything to screen using our palette colors
pixels[counter] = palette[fire[x][y]];
// Extract the red value using right shift and bit mask
// equivalent of red(pg.pixels[x+y*w])
if ((pg.pixels[counter++] >> 16 & 0xFF) == 128) {
// Only map 3D cube 'lit' pixels onto fire array needed for next frame
fire[x][y] = 128;
}
}
}
updatePixels();
}
if(keyPressed) {
if (key == 'a' || key == 'A') {
fill(0x90880000);
rect(random(height),random(width),random(height),random(width));
}
if (key == 'b' || key == 'B') {
fill(0x90000066);
rect(random(width),random(height),random(width),random(height));
}
if (key == 'c' || key == 'C') {
fill(0x90005500);
rect(random(height),random(width),random(height),random(width));
}
if (key == 'd' || key == 'D') {
fill(0x90330066);
rect(random(width),random(height),random(width),random(height));
}
if (key == 'e' || key == 'E') {
fill(0x90777777);
rect(random(height),random(width),random(height),random(width));
}
}
for (int i=0; i<numCircles; i++) {
circles[i].display(); // display all the circles
}
}
class Circle {
float x,y; // location
float dim; // dimension
color c; // color
Circle(float x, float y) {
this.x = x;
this.y = y;
dim = random(5);
c = color(random(55));
}
void display() {
float distance = dist(x,y,mouseX,mouseY); // distance between circle and mouse
if (distance < 355) { // if distance is smaller than 255
fill(355-distance);
} else { // if distance is bigger than 255
fill(c);
}
ellipse(x,y,dim,dim); // a circle at position xy
}
}
1