POV-Ray : Newsgroups : povray.text.scene-files : Help needed with Noise Function! : Re: Help needed with Noise Function! Server Time
8 Jul 2024 12:21:37 EDT (-0400)
  Re: Help needed with Noise Function!  
From: Tor Olav Kristensen
Date: 18 Jan 2001 20:48:43
Message: <3A679CD3.CB5F8C27@online.no>
Lonnie Ezell wrote:
> 
> I originally posted this in povray.advanced-users, and they suggested I post
> it here.
> 
> I have what I believe is a working 2D Perlin Noise macro. Eventually I hope
> to have 1D, 2D, 3D & 4D macros. After solving a number of small bugs in the
> script when I try to use it, POV crashes on me. I've tried reinstalling and
> the same results.
> 
> Could someone look it over for me to see if you can spot what's wrong.

See code below and my comments in povray.advanced-users

-- 
Best regards,

Tor Olav

mailto:tor### [at] hotmailcom
http://www.crosswinds.net/~tok/tokrays.html


// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#version 3.1;

#include "colors.inc"

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#macro LinearInterpolate(A, B, M_x)

  (A*(1 - M_x) + B*M_x)

#end // macro LinearInterpolate



#macro CosineInterpolate(A, B, M_x)

  LinearInterpolate(A, B, (1 - cos(M_x*pi))/2)

#end // macro CosineInterpolate



#macro CubicInterpolate(V0, V1, V2, V3, M_x)

  #local P = V3 - V2 - V0 + V1;
  #local Q = V0 - V1 - P;
  #local R = V2 - V0;
  #local S = V1; 
  
  (P*pow(M_x, 3) + Q*pow(M_x, 2) + R*M_x + S)

#end // macro CubicInterpolate
                 

	                                      
#macro NOISE_Init(N, SeedArray)

  #local NrOfOctaves = dimension_size(SeedArray, 1);
  #declare SeedArray[0] = seed(N);
  #local I = 1;
  #while (I < NrOfOctaves)
    #declare SeedArray[I] = seed(N*I*pow(I + 1, 2)/(I + 2));
    #local I = I + 1;
  #end

#end // macro NOISE_Init



// This macro should be rewritten
#macro NOISE_Rand_2D(M_x, M_y, Octave, SeedArray)

  (rand(SeedArray[Octave])*M_x + rand(SeedArray[Octave])*M_y)/2

#end // macro NOISE_Rand_2D


	                                      
#macro NOISE_Smooth_2D(M_x, M_y, Octave, SeedArray)

  #local Corners =
    NOISE_Rand_2D(M_x - 1, M_y - 1, Octave, SeedArray) +
    NOISE_Rand_2D(M_x + 1, M_y - 1, Octave, SeedArray) +
    NOISE_Rand_2D(M_x - 1, M_y + 1, Octave, SeedArray) +
    NOISE_Rand_2D(M_x + 1, M_y + 1, Octave, SeedArray);
  #local Sides =
    NOISE_Rand_2D(M_x - 1, M_y,     Octave, SeedArray) + 
    NOISE_Rand_2D(M_x + 1, M_y,     Octave, SeedArray) +
    NOISE_Rand_2D(M_x,     M_y - 1, Octave, SeedArray) +
    NOISE_Rand_2D(M_x,     M_y + 1, Octave, SeedArray);
  #local Center =
    NOISE_Rand_2D(M_x,     M_y,     Octave, SeedArray);
                              
  (Corners/16 + Sides/8 + Center/4)

#end // macro NOISE_Smooth_2D



#macro NOISE_Interp_2D(M_x, M_y, Octave, SeedArray)

  #local Int_x  = int(M_x);
  #local Frac_x = M_x - Int_x;
  #local Int_y  = int(M_y);
  #local Frac_y = M_y - Int_y;
   
  #local V1 = NOISE_Smooth_2D(Int_x,     Int_y,     Octave, SeedArray);
  #local V2 = NOISE_Smooth_2D(Int_x + 1, Int_y,     Octave, SeedArray);
  #local V3 = NOISE_Smooth_2D(Int_x,     Int_y + 1, Octave, SeedArray);
  #local V4 = NOISE_Smooth_2D(Int_x + 1, Int_y + 1, Octave, SeedArray);
   
  #local I1 = CosineInterpolate(V1, V2, Frac_x);
  #local I2 = CosineInterpolate(V3, V4, Frac_x);
   
  CosineInterpolate(I1, I2, Frac_y)

#end // macro NOISE_Interp_2D



#macro NOISE_Noise_2D(M_x, M_y, Persist, SeedArray)

  #local NrOfOctaves = dimension_size(SeedArray, 1);
  #local Total = 0;
  #local I = 0;
  #while(I < NrOfOctaves)
    #local Freq = pow(2, I);
    #local Ampl = pow(Persist, I);  
    #local Total = 
      Total + Ampl*NOISE_Interp_2D(M_x*Freq, M_y*Freq, I, SeedArray);
    #local I = I + 1;
  #end    
   
  Total   

#end // macro NOISE_Noise_2D

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare NOISE_Octaves = 2;
#declare NOISE_Persistence = 0.5;
#declare AnyNumber = 12345;

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#declare NOISE_SeedArray = array[NOISE_Octaves]

NOISE_Init(AnyNumber, NOISE_SeedArray)

#declare Xnr = 10;
#declare Znr = 10;

union {
  #declare I = 0;
  #while (I < Xnr)
    #declare J = 0;
    #while (J < Znr)
      #declare Perlin = 
        NOISE_Noise_2D(I, J, NOISE_Persistence, NOISE_SeedArray);
      box {
        -<1, 1, 1>/2,
         <1, 1, 1>/2
        pigment { color <0, Perlin, 0> }
        translate <I, 0, J>
      }
      #declare J = J + 1;
    #end // while         
    #declare I = I + 1;
  #end // while
  translate (<1, 0, 1> - <Xnr, 0, Znr>)/2
}


light_source {
  <0, 60, 0>
  color White
}                             


camera {
  location <0, 15, 0>
  look_at <0, 0, 0>
}                     

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.