func GenerateMap(width, depth int, gridSize int) *Map { m := new(Map) m.width = width m.depth = depth m.gridSize = gridSize m.minHeight = 1000000 m.maxHeight = 0 diag := math.Hypot(float64(m.width/2), float64(m.depth/2)) for z := 0; z < depth; z++ { for x := 0; x < width; x++ { fx := float64(x) + float64(z%2)*0.5 fz := float64(z) d := math.Hypot(float64(m.width/2)-fx, float64(m.depth/2)-fz) d = 1.0 - d/diag h := noise.OctaveNoise2d(fx, fz, 4, 0.25, 1.0/28) h = (h + 1.0) * 0.5 h = math.Sqrt(h) * 1024 * (math.Pow(d, 2)) h = math.Max(h, 120) m.heightMap = append(m.heightMap, float32(h)) m.minHeight = math.Min(m.minHeight, h) m.maxHeight = math.Max(m.maxHeight, h) } } return m }
func GenerateMap(width, height int, gridSize int) *Map { m := new(Map) m.width = width m.height = height m.gridSize = gridSize m.grid = make([]int, width*height) diag := math.Hypot(float64(m.width/2), float64(m.height/2)) for y := 0; y < height; y++ { for x := 0; x < width; x++ { fx := float64(x) fy := float64(y) // calculate inverse distance from center d := 1.0 - math.Hypot(float64(m.width/2)-fx, float64(m.height/2)-fy)/diag d = d v := noise.OctaveNoise2d(fx, fy, 4, 0.5, 1.0/64) v = (v + 1.0) / 2 v = v * 256 m.grid[y*width+x] = int(v) m.gridLines = append(m.gridLines, float32(x*m.gridSize), 0, float32(x*m.gridSize), float32((m.height-1)*m.gridSize)) } m.gridLines = append(m.gridLines, 0, float32(y*m.gridSize), float32((m.width-1)*m.gridSize), float32(y*m.gridSize)) } m.RebuildVertices() return m }
func (m *Map) getColorForVertex(v Vertex) [3]float32 { n := (float64(v.y) - m.minHeight) / (m.maxHeight - m.minHeight) * float64(len(colorMap)-1) i0 := int(math.Floor(n)) i1 := int(math.Ceil(n)) f := n - math.Floor(n) if f < 0.01 { i1 = i0 } c0 := colorMap[i0] c1 := colorMap[i1] color := [3]float32{} for i := 0; i < 3; i++ { color[i] = c0[i] + (c1[i]-c0[i])*float32(f) if n > 2 { color[i] += float32(noise.OctaveNoise2d(float64(v.x), float64(v.z), 1, 0.25, 1/32.0)) * 0.04 } } return color }