Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: BurntPizza (posted 2014-06-12 08:22:45, viewed 66 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188  
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.concurrent.*;

public class Worley3D {
   
   static List<BufferedImage> worley3D(int frames, int width, int height, float maxz, List<Point3D> seeds) throws Exception {
      List<Frame> frameList = new ArrayList<>();
      
      float z = 0;
      float zInc = maxz / frames;
      
      List<Proc> procs = new ArrayList<>();
      
      for (int frame = 0; frame < frames; frame++, z += zInc)
         procs.add(new Proc(width, height, z, frame, seeds));
      
      // run on all but 1 core (unless on a single core obviously)
      // don't want to lock/burn up anyone's computers
      ExecutorService exec = Executors.newFixedThreadPool(Math.max(1, Runtime.getRuntime().availableProcessors() - 1));
      
      List<Future<Frame>> results = exec.invokeAll(procs);
      
      exec.shutdown();
      
      for (Future<Frame> future : results)
         frameList.add(future.get());
      
      Collections.sort(frameList, new Comparator<Frame>() {
         @Override
         public int compare(Frame o1, Frame o2) {
            return o1.number - o2.number;
         }
      });
      
      List<BufferedImage> images = new ArrayList<>();
      
      for (Frame f : frameList)
         images.add(f.image);
      
      return images;
   }
   
   static float distToClosest(Point3D thisPoint, List<Point3D> points) {
      Point3D closest = points.get(0);
      float closestDist = Float.MAX_VALUE;
      for (Point3D p : points) {
         float dist = p.distanceSquared(thisPoint);
         if (dist < closestDist) {
            closestDist = dist;
            closest = p;
         }
      }
      return closest.distance(thisPoint);
   }
   
   static List<Point3D> genSeeds(int number, float maxx, float maxy, float maxz) {
      List<Point3D> points = new ArrayList<Point3D>(number);
      
      for (int i = 0; i < number; i++) {
         points.add(new Point3D((float) Math.random() * maxx, (float) Math.random() * maxy, (float) Math.random() * maxz));
      }
      
      return points;
   }
   
   public static void main(String[] a) throws Exception {
      
      int NUM_FRAMES = 333;
      
      float sweepZDepth = NUM_FRAMES; // defaulting to 1.0 z/frame sweep speed
      
      int width = 400, height = 400;
      
      List<BufferedImage> images = worley3D(NUM_FRAMES, width, height, sweepZDepth, genSeeds(NUM_FRAMES * 15, width, height, sweepZDepth));
      
      // Write frames to PNGs, change the path to fit
      
      /*
      int i = 0;
      for (BufferedImage image : images) {
         ImageIO.write(image, "PNG", new File("3d/frame" + String.format("%04d", i++) + ".png"));
      }
      */
      
      // Write animated GIF; credit to Riven: http://riven8192.blogspot.com/2010/02/image-java-animated-gifs.html
      
      /*
      List<GifFrame> gifFrames = new ArrayList<GifFrame>();
      
      for (BufferedImage image : images) {
         int transparantColor = 0xFF00FF;
         BufferedImage gif = ImageUtil.convertRGBAToGIF(image, transparantColor);
         long delay = 30;
         String disposal = GifFrame.RESTORE_TO_BGCOLOR;
         gifFrames.add(new GifFrame(gif, delay, disposal));
      }
      
      int loopCount = 0;
      FileOutputStream out = new FileOutputStream("noise3D.gif");
      ImageUtil.saveAnimatedGIF(out, gifFrames, loopCount);
      out.close();
      */
   }
}

class Point3D {
   
   float x, y, z;
   
   public Point3D(float x, float y, float z) {
      this.x = x;
      this.y = y;
      this.z = z;
   }
   
   public Point3D() {
      
   }
   
   public float distanceSquared(Point3D other) {
      float dx = other.x - x;
      float dy = other.y - y;
      float dz = other.z - z;
      return dx * dx + dy * dy + dz * dz;
   }
   
   public float distance(Point3D other) {
      return (float) Math.sqrt(distanceSquared(other));
   }
}

class Frame {
   final BufferedImage image;
   final int number;
   
   Frame(int num, BufferedImage img) {
      number = num;
      image = img;
   }
}

class Proc implements Callable<Frame> {
   
   final float z;
   
   final int width, height, num;
   
   final float[] dists;
   
   final List<Point3D> seeds;
   
   Proc(int width, int height, float z, int num, List<Point3D> seeds) {
      this.width = width;
      this.height = height;
      this.z = z;
      this.num = num;
      dists = new float[width * height];
      this.seeds = seeds;
   }
   
   @Override
   public Frame call() {
      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      
      float maxDist = -1;
      
      for (int y = 0; y < height; y++)
         for (int x = 0; x < width; x++) {
            float dist = Worley3D.distToClosest(new Point3D(x, y, z), seeds);
            dists[x + y * width] = dist;
            maxDist = Math.max(maxDist, dist);
         }
      
      float scale = 255 / maxDist;
      
      for (int y = 0; y < height; y++)
         for (int x = 0; x < width; x++) {
            float dist = dists[x + y * width];
            dist = Math.min(dist * scale, 255);
            int color = (int) dist;
            color = color << 16 | color << 8 | color;
            image.setRGB(x, y, color);
         }
      
      return new Frame(num, image);
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!