aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/module/basicfs/include/NoiseFSCommon.h
blob: 2441b8451dc41418877770d520eab4e0c47a9008 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2008-2015, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */


#ifndef __NOISE_FS_COMMON_H__
#define __NOISE_FS_COMMON_H__

#include "../../fieldsampler/include/FieldSamplerCommon.h"
#include "SimplexNoise.h"

namespace nvidia
{
namespace basicfs
{

struct NoiseType
{
	enum Enum
	{
		SIMPLEX,
		CURL
	};
};

//struct NoiseFSParams
#define INPLACE_TYPE_STRUCT_NAME NoiseFSParams
#define INPLACE_TYPE_STRUCT_FIELDS \
	INPLACE_TYPE_FIELD(float,			noiseStrength) \
	INPLACE_TYPE_FIELD(PxVec3,			noiseSpaceFreq) \
	INPLACE_TYPE_FIELD(float,			noiseTimeFreq) \
	INPLACE_TYPE_FIELD(uint32_t,			noiseOctaves) \
	INPLACE_TYPE_FIELD(float,			noiseStrengthOctaveMultiplier) \
	INPLACE_TYPE_FIELD(PxVec3,			noiseSpaceFreqOctaveMultiplier) \
	INPLACE_TYPE_FIELD(float,			noiseTimeFreqOctaveMultiplier) \
	INPLACE_TYPE_FIELD(uint32_t,			noiseType) \
	INPLACE_TYPE_FIELD(uint32_t,			noiseSeed) \
	INPLACE_TYPE_FIELD(PxTransform,	worldToShape) \
	INPLACE_TYPE_FIELD(InplaceBool,				useLocalSpace)
#include INPLACE_TYPE_BUILD()


APEX_CUDA_CALLABLE PX_INLINE PxVec3 evalNoise(const NoiseFSParams& params, const PxVec3& pos, uint32_t totalElapsedMS)
{
	PxVec3 point;
	if (params.useLocalSpace)
	{
		const PxVec3 posInShape = params.worldToShape.transform(pos);
		point = PxVec3(params.noiseSpaceFreq.x * posInShape.x, params.noiseSpaceFreq.y * posInShape.y, params.noiseSpaceFreq.z * posInShape.z);
	}
	else
	{
		point = PxVec3(params.noiseSpaceFreq.x * pos.x, params.noiseSpaceFreq.y * pos.y, params.noiseSpaceFreq.z * pos.z);
	}
	float time = params.noiseTimeFreq * (totalElapsedMS * 1e-3f);

	PxVec3 result;
	if (params.noiseType == NoiseType::CURL)
	{
		PxVec4 dFx;
		dFx.setZero();
		PxVec4 dFy;
		dFy.setZero();
		PxVec4 dFz;
		dFz.setZero();
		float amp = 1.0f;
		int seed = (int)params.noiseSeed;
		for (uint32_t i = 0; i < params.noiseOctaves; ++i)
		{
			dFx += amp * SimplexNoise::eval4D(point.x, point.y, point.z, time, ++seed);
			dFy += amp * SimplexNoise::eval4D(point.x, point.y, point.z, time, ++seed);
			dFz += amp * SimplexNoise::eval4D(point.x, point.y, point.z, time, ++seed);

			amp *= params.noiseStrengthOctaveMultiplier;
			point.x *= params.noiseSpaceFreqOctaveMultiplier.x;
			point.y *= params.noiseSpaceFreqOctaveMultiplier.y;
			point.z *= params.noiseSpaceFreqOctaveMultiplier.z;
			time *= params.noiseTimeFreqOctaveMultiplier;
		}
		//build curl noise as a result
		result.x = dFz.y - dFy.z;
		result.y = dFx.z - dFz.x;
		result.z = dFy.x - dFx.y;
	}
	else
	{
		PxVec4 noise;
		noise.setZero();
		float amp = 1.0f;
		int seed = (int)params.noiseSeed;
		for (uint32_t i = 0; i < params.noiseOctaves; ++i)
		{
			noise += amp * SimplexNoise::eval4D(point.x, point.y, point.z, time, ++seed);

			amp *= params.noiseStrengthOctaveMultiplier;
			point.x *= params.noiseSpaceFreqOctaveMultiplier.x;
			point.y *= params.noiseSpaceFreqOctaveMultiplier.y;
			point.z *= params.noiseSpaceFreqOctaveMultiplier.z;
			time *= params.noiseTimeFreqOctaveMultiplier;
		}
		//get noise gradient as a result
		result = noise.getXYZ();
	}
	result *= params.noiseStrength;
	return result;
}

APEX_CUDA_CALLABLE PX_INLINE PxVec3 executeNoiseFS_GRID(const NoiseFSParams& params, const PxVec3& pos, uint32_t totalElapsedMS)
{
	return evalNoise(params, pos, totalElapsedMS);
}


APEX_CUDA_CALLABLE PX_INLINE PxVec3 executeNoiseFS(const NoiseFSParams& params, const PxVec3& pos, uint32_t totalElapsedMS)
{
	return evalNoise(params, pos, totalElapsedMS);
}

}
} // namespace nvidia

#endif