Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Fastest perlin noise (gradiant & value noise)  (Read 2052 times)
0 Members and 1 Guest are viewing this topic.
Offline DzzD
« Posted 2011-05-09 23:07:17 »

NB: comment/uncomment block to switch between value/grad noise & linear/cubic interpolation
NB2: output range 0-255 => inded you can multiply by 1.0/256.0 to get usual double output

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  
public class FastNoise
{
    public static int noise(double x,double y,int nbOctave)
    {
        int result=0;      
        int frequence256=256;
        int sx=(int)((x)*frequence256);
        int sy=(int)((y)*frequence256);
        int octave=nbOctave;    
        while(octave!=0)
        {
            int bX=sx&0xFF;
            int bY=sy&0xFF;
 
            int sxp=sx>>8;
            int syp=sy>>8;
             
 
            //Compute noise for each corner of current cell
           int Y1376312589_00=syp*1376312589;
            int Y1376312589_01=Y1376312589_00+1376312589;
 
            int XY1376312589_00=sxp+Y1376312589_00;
            int XY1376312589_10=XY1376312589_00+1;
            int XY1376312589_01=sxp+Y1376312589_01;
            int XY1376312589_11=XY1376312589_01+1;
 
            int XYBASE_00=(XY1376312589_00<<13)^XY1376312589_00;
            int XYBASE_10=(XY1376312589_10<<13)^XY1376312589_10;
            int XYBASE_01=(XY1376312589_01<<13)^XY1376312589_01;
            int XYBASE_11=(XY1376312589_11<<13)^XY1376312589_11;
 
            int alt1=(XYBASE_00 * (XYBASE_00 * XYBASE_00 * 15731 + 789221) + 1376312589) ;
            int alt2=(XYBASE_10 * (XYBASE_10 * XYBASE_10 * 15731 + 789221) + 1376312589) ;
            int alt3=(XYBASE_01 * (XYBASE_01 * XYBASE_01 * 15731 + 789221) + 1376312589) ;
            int alt4=(XYBASE_11 * (XYBASE_11 * XYBASE_11 * 15731 + 789221) + 1376312589) ;
             
            /*
             *NOTE : on  for true grandiant noise uncomment following block
             * for true gradiant we need to perform scalar product here, gradiant vector are created/deducted using
             * the above pseudo random values (alt1...alt4) : by cutting thoses values in twice values to get for each a fixed x,y vector
             * gradX1= alt1&0xFF
             * gradY1= (alt1&0xFF00)>>8
             *
             * the last part of the PRN (alt1&0xFF0000)>>8 is used as an offset to correct one of the gradiant problem wich is zero on cell edge
             *
             * source vector (sXN;sYN) for scalar product are computed using (bX,bY)
             *
             * each four values  must be replaced by the result of the following
             * altN=(gradXN;gradYN) scalar (sXN;sYN)
             *
             * all the rest of the code (interpolation+accumulation) is identical for value & gradiant noise
             */

             
             
            /*START BLOCK FOR TRUE GRADIANT NOISE*/
             
             int grad1X=(alt1&0xFF)-128;
             int grad1Y=((alt1>>8)&0xFF)-128;
             int grad2X=(alt2&0xFF)-128;
             int grad2Y=((alt2>>8)&0xFF)-128;
             int grad3X=(alt3&0xFF)-128;
             int grad3Y=((alt3>>8)&0xFF)-128;
             int grad4X=(alt4&0xFF)-128;
             int grad4Y=((alt4>>8)&0xFF)-128;
             
             
             int sX1=bX>>1;
             int sY1=bY>>1;
             int sX2=128-sX1;
             int sY2=sY1;
             int sX3=sX1;
             int sY3=128-sY1;
             int sX4=128-sX1;
             int sY4=128-sY1;
             alt1=(grad1X*sX1+grad1Y*sY1)+16384+((alt1&0xFF0000)>>9); //to avoid seams to be 0 we use an offset
            alt2=(grad2X*sX2+grad2Y*sY2)+16384+((alt2&0xFF0000)>>9);
             alt3=(grad3X*sX3+grad3Y*sY3)+16384+((alt3&0xFF0000)>>9);
             alt4=(grad4X*sX4+grad4Y*sY4)+16384+((alt4&0xFF0000)>>9);
             
            /*END BLOCK FOR TRUE GRADIANT NOISE */
             
             
            /*START BLOCK FOR VALUE NOISE*/
            /*
             alt1&=0xFFFF;
             alt2&=0xFFFF;
             alt3&=0xFFFF;
             alt4&=0xFFFF;
             */

            /*END BLOCK FOR VALUE NOISE*/
             
             
            /*START BLOCK FOR LINEAR INTERPOLATION*/
            //BiLinear interpolation
           /*
            int f24=(bX*bY)>>8;
            int f23=bX-f24;
            int f14=bY-f24;
            int f13=256-f14-f23-f24;
 
            int val=(alt1*f13+alt2*f23+alt3*f14+alt4*f24);
            */

            /*END BLOCK FOR LINEAR INTERPOLATION*/
             
             
             
            //BiCubic interpolation ( in the form alt(bX) = alt[n] - (3*bX^2 - 2*bX^3) * (alt[n] - alt[n+1]) )
           /*START BLOCK FOR BICUBIC INTERPOLATION*/
            int bX2=(bX*bX)>>8;
            int bX3=(bX2*bX)>>8;
            int _3bX2=3*bX2;
            int _2bX3=2*bX3;
            int alt12= alt1 - (((_3bX2 - _2bX3) * (alt1-alt2)) >> 8);
            int alt34= alt3 - (((_3bX2 - _2bX3) * (alt3-alt4)) >> 8);
             
             
            int bY2=(bY*bY)>>8;
            int bY3=(bY2*bY)>>8;
            int _3bY2=3*bY2;
            int _2bY3=2*bY3;
            int val= alt12 - (((_3bY2 - _2bY3) * (alt12-alt34)) >> 8);
             
            val*=256;
            /*END BLOCK FOR BICUBIC INTERPOLATION*/
             
             
            //Accumulate in result
           result+=(val<<octave);
             
            octave--;
            sx<<=1;
            sy<<=1;
             
        }
        return result>>>(16+nbOctave+1);  
    }
}

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-05-11 06:16:14 »

I think there is a glitch in the code viewer here on JGO. The code after "/*START BLOCK FOR TRUE GRADIANT NOISE*/" shouldn't be in green. I have seen this in other threads too. /* and */ are glitchy since for some reason it doesn't interpret */ correctly.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

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

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

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

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (53 views)
2014-07-14 18:02:53
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

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!