Einfacher Plasma Effekt mit Java 2D

Thomas Darimont

Erfahrenes Mitglied
Hallo,

hier findet man ein schönes tutorial zum Thema Plasma:

http://student.kuleuven.be/~m0216922/CG/plasma.html

Weitere coole Grafik tutorials:
http://student.kuleuven.be/~m0216922/CG/index.html


schaut mal hier:
Java:
/**
 * 
 */
package de.tutorials;

import static java.lang.Math.sin;
import static java.lang.Math.sqrt;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;

/**
 * @author Tom
 * 
 */
public class PlasmaExample extends JFrame {

	BufferStrategy bufferStrategy;
	boolean stop;

	int w = 256;
	int h = 256;

	Runnable renderLoop = new Runnable() {
		public void run() {

			BufferedImage image = new BufferedImage(w, h,
					BufferedImage.TYPE_INT_ARGB);

			DataBufferInt dataBufferInt = (DataBufferInt) image.getRaster()
					.getDataBuffer();

			int paletteShift = 0;

			int[] colorIndecesLookupTable = generateLookUpTable();

			while (!stop) {

				Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();

				g.clearRect(0, 0, getWidth(), getHeight());

				int[] colorPalette = generateColorPalette(256, Color.WHITE,
						Color.BLACK);

				for (int x = 0; x < w; x++) {
					for (int y = 0; y < h; y++) {

						int color = colorPalette[(colorIndecesLookupTable[y * w
								+ x] + paletteShift)
								% colorPalette.length];

						dataBufferInt.setElem(y * w + x, color);
					}
				}

				g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
				g.dispose();

				bufferStrategy.show();

				try {
					TimeUnit.MILLISECONDS.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				paletteShift += 4;
			}

			setVisible(false);
			System.exit(0);
		}

		private int[] generateLookUpTable() {
			int[] colorIndeceslookupTable = new int[w * h];
			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					int colorIndex = (int) (128.0
							+ (128.0 * sin(x / 16.0))
							+ 128.0
							+ (128.0 * sin(y / 32.0))
							+ 128.0
							+ (128.0 * sin(sqrt((x - w / 2.0) * (x - w / 2.0)
									+ (y - h / 2.0) * (y - h / 2.0)) / 8.0))
							+ 128.0 + (128.0 * sin(sqrt(x * x + y * y) / 8.0)));
					colorIndeceslookupTable[w * y + x] = colorIndex;
				}
			}
			return colorIndeceslookupTable;
		}

		private int[] generateColorPalette(int colors, Color start, Color end) {
			int[] palette = new int[2 * colors];

			int startRgb = start.getRGB();
			int colorShift = (end.getRGB() - startRgb) / colors;

			for (int i = 0; i < colors; i++) {
				int color = startRgb + i * colorShift;
				palette[i] = color;
				palette[2 * colors - i - 1] = color;
			}

			return palette;
		};
	};

	public PlasmaExample() {
		super("PlasmaExample");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(640, 480);
		setVisible(true);
		createBufferStrategy(2);
		bufferStrategy = getBufferStrategy();

		// Fullscreen ?
		GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getDefaultScreenDevice().setFullScreenWindow(this);

		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					stop = true;
				}
			}
		});

		Executors.newSingleThreadExecutor().execute(renderLoop);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new PlasmaExample();
	}
}

Gruß Tom
 

Anhänge

  • plasma.JPG
    plasma.JPG
    56,8 KB · Aufrufe: 210
Hallöchen,
Also ich finde das echt super.
Hatte erst das Tutorial von der Seite versucht, um für ein 2d Spiel einen Dynamischen Himmel zu machen (per 3D Random Noise), der sich bewegt, egal wie.
Aber das war zu langsam, also hab ich das Plasma hier mir mal angeguckt.
An sich nicht schlecht, aber das generateColorPalette, muss doch nicht unbedingt in den Loop rein, da sich da ja eh nichts dynamisch verändert?
Bei mir geht es jedenfalls auch, wenn man die Palette einmal außerhalb erzeugt, hab es aber eh ein bisschen anders gemacht.

Was ich jetzt noch gerne etwas genauer wüsste ist, wie genau dass mit der Farbpalette funktioniert. Manchmal fängt es an zu flackern oder es gibt ein Noise was ziemlich blöd ist. Wäre schick, wenn das vielleicht auch noch mal auf Deutsch erläutern könnte? O:

Aber wie gesagt ich find es trotzdem sehr gut danke :)
 
Zurück