MethaneAsteroids

Форк
0
105 строк · 4.3 Кб
1
/******************************************************************************
2

3
Copyright 2019-2023 Evgeny Gorodetskiy
4

5
Licensed under the Apache License, Version 2.0 (the "License"),
6
you may not use this file except in compliance with the License.
7
You may obtain a copy of the License at
8

9
    http://www.apache.org/licenses/LICENSE-2.0
10

11
Unless required by applicable law or agreed to in writing, software
12
distributed under the License is distributed on an "AS IS" BASIS,
13
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
See the License for the specific language governing permissions and
15
limitations under the License.
16

17
*******************************************************************************
18

19
FILE: PerlinNoise.cpp
20
Multi-octave simplex noise generator in range [0, 1]
21

22
******************************************************************************/
23

24
#include "PerlinNoise.h"
25
#include <Methane/Instrumentation.h>
26

27
#include <hlsl++.h>
28
#include <FastNoise/FastNoise.h>
29

30
namespace Methane::Graphics
31
{
32

33
template<typename VectorType>
34
float GetPerlinNoise(const FastNoise::Simplex& simplex_noise, const VectorType& pos, int seed) noexcept;
35

36
template<> float GetPerlinNoise(const FastNoise::Simplex& simplex_noise, const hlslpp::float2& pos, int seed) noexcept    { return simplex_noise.GenSingle2D(pos.x, pos.y, seed); }
37
template<> float GetPerlinNoise(const FastNoise::Simplex& simplex_noise, const hlslpp::float3& pos, int seed) noexcept    { return simplex_noise.GenSingle3D(pos.x, pos.y, pos.z, seed); }
38
template<> float GetPerlinNoise(const FastNoise::Simplex& simplex_noise, const hlslpp::float4& pos, int seed) noexcept    { return simplex_noise.GenSingle4D(pos.x, pos.y, pos.z, pos.w, seed); }
39

40
template<> float GetPerlinNoise(const FastNoise::Simplex& simplex_noise, const Data::RawVector2F& pos, int seed) noexcept { return simplex_noise.GenSingle2D(pos[0], pos[1], seed); }
41
template<> float GetPerlinNoise(const FastNoise::Simplex& simplex_noise, const Data::RawVector3F& pos, int seed) noexcept { return simplex_noise.GenSingle3D(pos[0], pos[1], pos[2], seed); }
42
template<> float GetPerlinNoise(const FastNoise::Simplex& simplex_noise, const Data::RawVector4F& pos, int seed) noexcept { return simplex_noise.GenSingle4D(pos[0], pos[1], pos[2], pos[3], seed); }
43

44
PerlinNoise::PerlinNoise(float persistence, size_t octaves_count, int seed)
45
    : m_weights(GetWeights(persistence, octaves_count))
46
    , m_norm_multiplier(0.5F / GetWeightsSum(m_weights))
47
    , m_seed(seed)
48
{
49
    META_FUNCTION_TASK();
50
}
51

52
float PerlinNoise::operator()(const hlslpp::float2& pos) const noexcept { return GetValue(pos); }
53
float PerlinNoise::operator()(const hlslpp::float3& pos) const noexcept { return GetValue(pos); }
54
float PerlinNoise::operator()(const hlslpp::float4& pos) const noexcept { return GetValue(pos); }
55

56
float PerlinNoise::operator()(const Data::RawVector2F& pos) const noexcept { return GetValue(pos); }
57
float PerlinNoise::operator()(const Data::RawVector3F& pos) const noexcept { return GetValue(pos); }
58
float PerlinNoise::operator()(const Data::RawVector4F& pos) const noexcept { return GetValue(pos); }
59

60
template<typename VectorType>
61
float PerlinNoise::GetValue(VectorType pos) const noexcept
62
{
63
    META_FUNCTION_TASK();
64
    const FastNoise::Simplex& simplex_noise = GetSimplexNoise();
65
    float noise = 0.F;
66
    for (const float weight : m_weights)
67
    {
68
        noise += weight * GetPerlinNoise(simplex_noise, pos, m_seed);
69
        pos *= 2.F;
70
    }
71
    return noise * m_norm_multiplier + 0.5F;
72
}
73

74
PerlinNoise::Weights PerlinNoise::GetWeights(float persistence, size_t octaves_count) noexcept
75
{
76
    META_FUNCTION_TASK();
77
    Weights weights;
78
    weights.reserve(octaves_count);
79
    for (size_t i = 0; i < octaves_count; ++i)
80
    {
81
        weights.emplace_back(persistence);
82
        persistence *= persistence;
83
    }
84
    return weights;
85
}
86

87
float PerlinNoise::GetWeightsSum(const PerlinNoise::Weights& weights) noexcept
88
{
89
    META_FUNCTION_TASK();
90
    float weights_sum = 0.F;
91
    for(float weight : weights)
92
    {
93
        weights_sum += weight;
94
    }
95
    return weights_sum;
96
}
97

98
const FastNoise::Simplex& PerlinNoise::GetSimplexNoise() const
99
{
100
    META_FUNCTION_TASK();
101
    static const auto s_simplex_noise_ptr = FastNoise::New<FastNoise::Simplex>();
102
    return *s_simplex_noise_ptr;
103
}
104

105
} // namespace Methane::Graphics
106

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.