|
| 1 | +import Noise |
| 2 | + |
| 3 | +import MaxPNG |
| 4 | + |
| 5 | +func color_noise_png(r_noise:Noise, g_noise:Noise, b_noise:Noise, |
| 6 | + width:Int, height:Int, value_offset:Double, invert:Bool = false, path:String) |
| 7 | +{ |
| 8 | + var pixbuf:[UInt8] = [] |
| 9 | + pixbuf.reserveCapacity(3 * width * height) |
| 10 | + for (r, (g, b)) in zip(r_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset), |
| 11 | + zip(g_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset), |
| 12 | + b_noise.sample_area_saturated_to_u8(width: width, height: height, offset: value_offset))) |
| 13 | + { |
| 14 | + if invert |
| 15 | + { |
| 16 | + pixbuf.append(UInt8.max - r) |
| 17 | + pixbuf.append(UInt8.max - g) |
| 18 | + pixbuf.append(UInt8.max - b) |
| 19 | + } |
| 20 | + else |
| 21 | + { |
| 22 | + pixbuf.append(r) |
| 23 | + pixbuf.append(g) |
| 24 | + pixbuf.append(b) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + guard let properties = PNGProperties(width: width, height: height, bit_depth: 8, color: .rgb, interlaced: false) |
| 29 | + else |
| 30 | + { |
| 31 | + fatalError("failed to set png properties") |
| 32 | + } |
| 33 | + |
| 34 | + do |
| 35 | + { |
| 36 | + try png_encode(path: path, raw_data: pixbuf, properties: properties) |
| 37 | + } |
| 38 | + catch |
| 39 | + { |
| 40 | + print(error) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func banner_simplex2d(width:Int, height:Int, seed:Int) |
| 45 | +{ |
| 46 | + color_noise_png(r_noise: SimplexNoise2D(amplitude: 0.5*255, frequency: 0.015, seed: seed), |
| 47 | + g_noise: SimplexNoise2D(amplitude: 0.5*255, frequency: 0.0075, seed: seed + 1), |
| 48 | + b_noise: SimplexNoise2D(amplitude: 0.5*255, frequency: 0.00375, seed: seed + 2), |
| 49 | + width: width, |
| 50 | + height: height, |
| 51 | + value_offset: 0.65*255, |
| 52 | + path: "banner_simplex2d.png") |
| 53 | +} |
| 54 | + |
| 55 | +func banner_supersimplex2d(width:Int, height:Int, seed:Int) |
| 56 | +{ |
| 57 | + color_noise_png(r_noise: SuperSimplexNoise2D(amplitude: 0.5*255, frequency: 0.01, seed: seed), |
| 58 | + g_noise: SuperSimplexNoise2D(amplitude: 0.5*255, frequency: 0.005, seed: seed + 1), |
| 59 | + b_noise: SuperSimplexNoise2D(amplitude: 0.5*255, frequency: 0.0025, seed: seed + 2), |
| 60 | + width: width, |
| 61 | + height: height, |
| 62 | + value_offset: 0.65*255, |
| 63 | + path: "banner_supersimplex2d.png") |
| 64 | +} |
| 65 | + |
| 66 | +func banner_supersimplex3d(width:Int, height:Int, seed:Int) |
| 67 | +{ |
| 68 | + color_noise_png(r_noise: SuperSimplexNoise3D(amplitude: 0.5*255, frequency: 0.01, seed: seed), |
| 69 | + g_noise: SuperSimplexNoise3D(amplitude: 0.5*255, frequency: 0.005, seed: seed + 1), |
| 70 | + b_noise: SuperSimplexNoise3D(amplitude: 0.5*255, frequency: 0.0025, seed: seed + 2), |
| 71 | + width: width, |
| 72 | + height: height, |
| 73 | + value_offset: 0.65*255, |
| 74 | + path: "banner_supersimplex3d.png") |
| 75 | +} |
| 76 | + |
| 77 | +func banner_cell2d(width:Int, height:Int, seed:Int) |
| 78 | +{ |
| 79 | + color_noise_png(r_noise: CellNoise2D(amplitude: 3*255, frequency: 0.03, seed: seed), |
| 80 | + g_noise: CellNoise2D(amplitude: 3*255, frequency: 0.015, seed: seed + 1), |
| 81 | + b_noise: CellNoise2D(amplitude: 3*255, frequency: 0.0075, seed: seed + 2), |
| 82 | + width: width, |
| 83 | + height: height, |
| 84 | + value_offset: 0, |
| 85 | + invert: true, |
| 86 | + path: "banner_cell2d.png") |
| 87 | +} |
| 88 | + |
| 89 | +func banner_cell3d(width:Int, height:Int, seed:Int) |
| 90 | +{ |
| 91 | + color_noise_png(r_noise: CellNoise3D(amplitude: 3*255, frequency: 0.03, seed: seed), |
| 92 | + g_noise: CellNoise3D(amplitude: 3*255, frequency: 0.015, seed: seed + 1), |
| 93 | + b_noise: CellNoise3D(amplitude: 3*255, frequency: 0.0075, seed: seed + 2), |
| 94 | + width: width, |
| 95 | + height: height, |
| 96 | + value_offset: 0, |
| 97 | + invert: true, |
| 98 | + path: "banner_cell3d.png") |
| 99 | +} |
| 100 | + |
| 101 | +public |
| 102 | +func banners(width:Int, ratio:Double) |
| 103 | +{ |
| 104 | + let height:Int = Int(Double(width) / ratio) |
| 105 | + banner_simplex2d(width: width, height: height, seed: 5) |
| 106 | + banner_supersimplex2d(width: width, height: height, seed: 8) |
| 107 | + banner_supersimplex3d(width: width, height: height, seed: 0) |
| 108 | + banner_cell2d(width: width, height: height, seed: 0) |
| 109 | + banner_cell3d(width: width, height: height, seed: 0) |
| 110 | +} |
0 commit comments