你现在是一个C++程序员请使用C++编写一个可以在UE5蓝图中使用的节点 这个节点的作用是接收两个输入的值根据这两个值输出一维perlin噪声值。请给出完整的所有代码包括cpp代码、头文件代码以及其他所需代码并详细描述操作过程
以下是一个可以在UE5蓝图中使用的节点,用于接收两个输入的值,根据这两个值输出一维perlin噪声值的完整代码:
头文件代码(PerlinNoiseNode.h):
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "PerlinNoiseNode.generated.h"
UCLASS()
class YOURPROJECT_API UPerlinNoiseNode : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintPure, Category = "PerlinNoise")
static float GetPerlinNoise(float X, float Y);
private:
static float Noise(float X);
static float Interpolate(float A, float B, float T);
static float SmoothedNoise(float X);
static float InterpolatedNoise(float X);
static float PerlinNoise(float X, float Y);
};
cpp代码(PerlinNoiseNode.cpp):
#include "PerlinNoiseNode.h"
float UPerlinNoiseNode::GetPerlinNoise(float X, float Y)
{
return PerlinNoise(X, Y);
}
float UPerlinNoiseNode::Noise(float X)
{
int32 N = (int32)X;
N = (N << 13) ^ N;
return (1.0f - ((N * (N * N * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f);
}
float UPerlinNoiseNode::Interpolate(float A, float B, float T)
{
float F = (1.0f - cos(T * PI)) * 0.5f;
return A * (1.0f - F) + B * F;
}
float UPerlinNoiseNode::SmoothedNoise(float X)
{
return Noise(X) / 2.0f + Noise(X - 1.0f) / 4.0f + Noise(X + 1.0f) / 4.0f;
}
float UPerlinNoiseNode::InterpolatedNoise(float X)
{
int32 IntegerX = (int32)X;
float FractionalX = X - IntegerX;
float V1 = SmoothedNoise(IntegerX);
float V2 = SmoothedNoise(IntegerX + 1);
return Interpolate(V1, V2, FractionalX);
}
float UPerlinNoiseNode::PerlinNoise(float X, float Y)
{
float Total = 0.0f;
float Persistence = 0.5f;
int32 Octaves = 4;
for (int32 i = 0; i < Octaves; i++)
{
float Frequency = pow(2.0f, i);
float Amplitude = pow(Persistence, i);
Total += InterpolatedNoise(X * Frequency) * Amplitude;
}
return Total;
}
在UE5蓝图中使用时,只需将节点拖放到蓝图中,连接两个输入节点(类型为float),并将节点的输出连接到需要使用perlin噪声的节点即可。
操作过程:
- 创建一个新的蓝图,并打开蓝图编辑器。
- 在蓝图编辑器中,拖放一个自定义节点(即PerlinNoiseNode)到蓝图中。
- 连接两个输入节点(类型为float)到PerlinNoiseNode节点的输入端口上。
- 将PerlinNoiseNode节点的输出连接到需要使用perlin噪声的节点上。
- 完成以上操作后,保存蓝图并运行游戏即可。
原文地址: https://www.cveoy.top/t/topic/bRdU 著作权归作者所有。请勿转载和采集!