Ejemplo n.º 1
0
func PerlinRandomHeight(x uint16, y uint16) uint16 {
	maxh := float64(65536)

	noisectx := perlin.NewPerlinNoise(1234)
	noise := noisectx.At2d(float64(x)*0.1, float64(y)*0.1)
	return uint16((maxh / 2) + ((maxh / 2) * noise))
}
Ejemplo n.º 2
0
func NewTestGenerator(seed int64) *TestGenerator {
	perlin := perlin.NewPerlinNoise(seed)

	randSource := rand.NewSource(time.Now().Unix())
	randGen := rand.New(randSource)

	return &TestGenerator{
		randSource: randSource,
		randGen:    randGen,
		heightSource: &Sum{
			Inputs: []ISource{
				&Turbulence{
					Dx:     &Scale{50, 1, &Offset{20.1, 0, perlin}},
					Dy:     &Scale{50, 1, &Offset{10.1, 0, perlin}},
					Factor: 50,
					Source: &Scale{
						Wavelength: 200,
						Amplitude:  50,
						Source:     perlin,
					},
				},
				&Turbulence{
					Dx:     &Scale{40, 1, &Offset{20.1, 0, perlin}},
					Dy:     &Scale{40, 1, &Offset{10.1, 0, perlin}},
					Factor: 10,
					Source: &Mult{
						A: &Scale{
							Wavelength: 40,
							Amplitude:  20,
							Source:     perlin,
						},
						// Local steepness.
						B: &Scale{
							Wavelength: 200,
							Amplitude:  1,
							Source:     &Add{perlin, 0.6},
						},
					},
				},
				&Scale{
					Wavelength: 5,
					Amplitude:  2,
					Source:     perlin,
				},
			},
		},
	}
}
Ejemplo n.º 3
0
func main() {
	w := 512
	h := 512

	perlin := perlin.NewPerlinNoise(0)

	gen := &Sum{
		Inputs: []ISource{
			&Turbulence{
				Dx:     &Scale{50, 1, &Offset{20.1, 0, perlin}},
				Dy:     &Scale{50, 1, &Offset{10.1, 0, perlin}},
				Factor: 100,
				Source: &Scale{
					Wavelength: 200,
					Amplitude:  100,
					Source:     perlin,
				},
			},
			&Turbulence{
				Dx:     &Scale{40, 1, &Offset{20.1, 0, perlin}},
				Dy:     &Scale{40, 1, &Offset{10.1, 0, perlin}},
				Factor: 10,
				Source: &Mult{
					A: &Scale{
						Wavelength: 40,
						Amplitude:  20,
						Source:     perlin,
					},
					// Local steepness.
					B: &Scale{
						Wavelength: 200,
						Amplitude:  1,
						Source:     &Add{perlin, 0.6},
					},
				},
			},
			&Scale{
				Wavelength: 5,
				Amplitude:  2,
				Source:     perlin,
			},
		},
	}

	values := make([]float64, h*h)

	var valueStat Stat

	for row := 0; row < h; row++ {
		for col := 0; col < w; col++ {
			value := gen.At2d(float64(col), float64(row))
			valueStat.Add(value)
			values[row*w+col] = value
		}
	}

	img := image.NewGray(image.Rect(0, 0, w, h))

	base := valueStat.min
	scale := 255 / (valueStat.max - valueStat.min)

	for row := 0; row < h; row++ {
		for col := 0; col < w; col++ {
			scaled := scale * (values[row*w+col] - base)
			img.Set(col, row, color.Gray{uint8(scaled)})
		}
	}

	log.Printf("value stats %#v", valueStat)

	outFile, err := os.Create("output.png")
	if err != nil {
		log.Fatal(err)
	}
	defer outFile.Close()
	png.Encode(outFile, img)
}