-
Notifications
You must be signed in to change notification settings - Fork 50
Added perlin noise feature for topography #922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5815d93
00e4cd6
e1fe7af
5b82799
4fcf5dc
7166381
4d1e4de
8e63820
c14b101
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| Copyright (C) 2018-2026 by the authors of the World Builder code. | ||
|
|
||
| This file is part of the World Builder. | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Lesser General Public License as published | ||
| by the Free Software Foundation, either version 2 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Lesser General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Lesser General Public License | ||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #ifndef WORLD_BUILDER_FEATURES_CONTINENTAL_PLATE_MODELS_TOPOGRAPHY_PERLIN_NOISE_H | ||
| #define WORLD_BUILDER_FEATURES_CONTINENTAL_PLATE_MODELS_TOPOGRAPHY_PERLIN_NOISE_H | ||
|
|
||
| #include "world_builder/features/continental_plate_models/topography/interface.h" | ||
| #include "world_builder/features/feature_utilities.h" | ||
| #include "world_builder/objects/surface.h" | ||
| #include <array> | ||
| #include <cstdint> | ||
|
|
||
| namespace WorldBuilder | ||
| { | ||
|
|
||
| namespace Features | ||
| { | ||
| using namespace FeatureUtilities; | ||
| namespace ContinentalPlateModels | ||
| { | ||
| namespace Topography | ||
| { | ||
| /** | ||
| * Perlin noise based topography model for oceanic plates. | ||
| * | ||
| * This model generates topography using layered Perlin noise (octaves). | ||
| * Parameters exposed to input files are similar to the continental | ||
| * implementation: min/max depth, min/max topography, frequency, | ||
| * octaves, persistence, lacunarity and operation. | ||
| */ | ||
| class PerlinNoise final: public Interface | ||
| { | ||
| public: | ||
| /** | ||
| * Constructor | ||
| * @param world pointer to the containing World instance | ||
| */ | ||
| PerlinNoise(WorldBuilder::World *world); | ||
| /** | ||
| * Destructor | ||
| */ | ||
| ~PerlinNoise() override final; | ||
|
|
||
| /** | ||
| * Declare parameter entries used by this model to the Parameters system. | ||
| */ | ||
| static void declare_entries(Parameters &prm, const std::string &parent_name = ""); | ||
| /** | ||
| * Parse parameter values from the Parameters instance and prepare | ||
| * any internal state (e.g. permutation table shuffling). | ||
| */ | ||
| void parse_entries(Parameters &prm, const std::vector<Point<2>> &coordinates) override final; | ||
|
|
||
| /** | ||
| * Compute the topography at the given position. Respects depth | ||
| * ranges and combines the generated value with the input topography | ||
| * according to `operation`. | ||
| */ | ||
| double get_topography(const Point<3> &position, | ||
| const Objects::NaturalCoordinate &position_in_natural_coordinates, | ||
| double topography) const override final; | ||
|
|
||
| private: | ||
| // parameters | ||
| double min_depth; | ||
| Objects::Surface min_depth_surface; | ||
| double max_depth; | ||
| Objects::Surface max_depth_surface; | ||
| double min_topography; | ||
| double max_topography; | ||
| double frequency; | ||
| unsigned int octaves; | ||
| double persistence; | ||
| double lacunarity; | ||
| Operations operation; | ||
|
|
||
| // Perlin noise implementation details | ||
| using state_type = std::array<std::uint8_t, 256>; | ||
| state_type p; | ||
|
|
||
| /** | ||
| * Smoothstep function used by Perlin interpolation. | ||
| */ | ||
| double fade(double t) const; | ||
|
|
||
| /** | ||
| * Linear interpolation helper. | ||
| */ | ||
| double lerp(double t, double a, double b) const; | ||
|
|
||
| /** | ||
| * Gradient function for Perlin noise. | ||
| */ | ||
| double grad(int hash, double x, double y, double z) const; | ||
|
|
||
| /** | ||
| * Base Perlin noise function returning value in [-1,1]. | ||
| */ | ||
| double noise(double x, double y, double z) const; | ||
|
|
||
| /** | ||
| * Layered (octave) Perlin noise normalized to [-1,1]. | ||
| */ | ||
| double octave_noise(double x, double y, double z, unsigned int octaves_in) const; | ||
| }; | ||
| } // namespace Topography | ||
| } // namespace OceanicPlateModels | ||
| } // namespace Features | ||
| } // namespace WorldBuilder | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| Copyright (C) 2018-2026 by the authors of the World Builder code. | ||
|
|
||
| This file is part of the World Builder. | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Lesser General Public License as published | ||
| by the Free Software Foundation, either version 2 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Lesser General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Lesser General Public License | ||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #ifndef WORLD_BUILDER_FEATURES_OCEANIC_PLATE_MODELS_TOPOGRAPHY_PERLIN_NOISE_H | ||
| #define WORLD_BUILDER_FEATURES_OCEANIC_PLATE_MODELS_TOPOGRAPHY_PERLIN_NOISE_H | ||
|
|
||
| #include "world_builder/features/oceanic_plate_models/topography/interface.h" | ||
| #include "world_builder/features/feature_utilities.h" | ||
| #include "world_builder/objects/surface.h" | ||
| #include <array> | ||
| #include <cstdint> | ||
|
|
||
| namespace WorldBuilder | ||
| { | ||
|
|
||
| namespace Features | ||
| { | ||
| using namespace FeatureUtilities; | ||
| namespace OceanicPlateModels | ||
| { | ||
| namespace Topography | ||
| { | ||
| /** | ||
| * Perlin noise based topography model for oceanic plates. | ||
| * | ||
| * This model generates topography using layered Perlin noise (octaves). | ||
| * Parameters exposed to input files are similar to the continental | ||
| * implementation: min/max depth, min/max topography, frequency, | ||
| * octaves, persistence, lacunarity and operation. | ||
| */ | ||
| class PerlinNoise final: public Interface | ||
|
Comment on lines
+40
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doc string is much better than the one you have for the continental .h file. I would copy this one and put it there too |
||
| { | ||
| public: | ||
| /** | ||
| * Constructor | ||
| * @param world pointer to the containing World instance | ||
| */ | ||
| PerlinNoise(WorldBuilder::World *world); | ||
|
|
||
| /** | ||
| * Destructor | ||
| */ | ||
| ~PerlinNoise() override final; | ||
|
|
||
| /** | ||
| * Declare parameter entries used by this model to the Parameters system. | ||
| */ | ||
| static void declare_entries(Parameters &prm, const std::string &parent_name = ""); | ||
|
Comment on lines
+50
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And you have all the function documentation here that should be copied to continental_plate as well 😄 |
||
|
|
||
| /** | ||
| * Parse parameter values from the Parameters instance and prepare | ||
| * any internal state (e.g. permutation table shuffling). | ||
| */ | ||
| void parse_entries(Parameters &prm, const std::vector<Point<2>> &coordinates) override final; | ||
|
|
||
| /** | ||
| * Compute the topography at the given position. Respects depth | ||
| * ranges and combines the generated value with the input topography | ||
| * according to `operation`. | ||
| */ | ||
| double get_topography(const Point<3> &position, | ||
| const Objects::NaturalCoordinate &position_in_natural_coordinates, | ||
| double topography) const override final; | ||
|
|
||
| private: | ||
| // parameters | ||
| double min_depth; | ||
| Objects::Surface min_depth_surface; | ||
| double max_depth; | ||
| Objects::Surface max_depth_surface; | ||
| double min_topography; | ||
| double max_topography; | ||
| double frequency; | ||
| unsigned int octaves; | ||
| double persistence; | ||
| double lacunarity; | ||
| Operations operation; | ||
|
|
||
| // Perlin noise implementation details | ||
| using state_type = std::array<std::uint8_t, 256>; | ||
| state_type p; | ||
|
|
||
| /** | ||
| * Smoothstep function used by Perlin interpolation. | ||
| */ | ||
| double fade(double t) const; | ||
|
|
||
| /** | ||
| * Linear interpolation helper. | ||
| */ | ||
| double lerp(double t, double a, double b) const; | ||
|
|
||
| /** | ||
| * Gradient function for Perlin noise. | ||
| */ | ||
| double grad(int hash, double x, double y, double z) const; | ||
|
|
||
| /** | ||
| * Base Perlin noise function returning value in [-1,1]. | ||
| */ | ||
| double noise(double x, double y, double z) const; | ||
|
|
||
| /** | ||
| * Layered (octave) Perlin noise normalized to [-1,1]. | ||
| */ | ||
| double octave_noise(double x, double y, double z, unsigned int octaves_in) const; | ||
| }; | ||
| } // namespace Topography | ||
| } // namespace OceanicPlateModels | ||
| } // namespace Features | ||
| } // namespace WorldBuilder | ||
|
|
||
| #endif | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing doc strings for these functions