Skip to content

Commit

Permalink
Add VectorField Loader (#981)
Browse files Browse the repository at this point in the history
Add VectorField Loader
  • Loading branch information
durswd authored Dec 22, 2023
1 parent 4ec8ee1 commit d006c4a
Show file tree
Hide file tree
Showing 16 changed files with 530 additions and 26 deletions.
4 changes: 4 additions & 0 deletions Dev/Cpp/Effekseer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ file(GLOB effekseer_h
Effekseer/Model/SplineGenerator.h
Effekseer/Network/*.h
Effekseer/Network/data/*.h
Effekseer/VectorField/VectorField.h
Effekseer/VectorField/VectorFieldLoader.h
)

set(effekseer_src
Expand Down Expand Up @@ -155,6 +157,8 @@ set(effekseer_src
Effekseer/Network/Effekseer.Server.cpp
Effekseer/Network/Effekseer.Session.cpp
Effekseer/Network/Effekseer.Socket.cpp
Effekseer/VectorField/VectorField.cpp
Effekseer/VectorField/VectorFieldLoader.cpp
)

add_library(${PROJECT_NAME} STATIC
Expand Down
3 changes: 2 additions & 1 deletion Dev/Cpp/Effekseer/Effekseer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
#include "Effekseer/SIMD/Vec4f.h"
#include "Effekseer/Sound/Effekseer.SoundPlayer.h"
#include "Effekseer/Utils/Effekseer.CustomAllocator.h"

#include "Effekseer/VectorField/VectorField.h"
#include "Effekseer/VectorField/VectorFieldLoader.h"
#endif
30 changes: 18 additions & 12 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Base.Pre.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//----------------------------------------------------------------------------------

#ifdef _WIN32
//#include <windows.h>
// #include <windows.h>
#elif defined(_PSVITA)
#elif defined(_PS4)
#elif defined(_SWITCH)
Expand Down Expand Up @@ -81,6 +81,7 @@ class MaterialLoader;
class SoundLoader;
class ModelLoader;
class CurveLoader;
class VectorFieldLoader;

class Texture;
class SoundData;
Expand Down Expand Up @@ -128,7 +129,7 @@ using ThreadNativeHandleType = std::thread::native_handle_type;
static_assert(std::is_class<decltype(val)>::value != true, "val must not be class/struct"); \
if ((val) != nullptr) \
{ \
delete[](val); \
delete[] (val); \
(val) = nullptr; \
}

Expand Down Expand Up @@ -332,11 +333,11 @@ T Clamp(T t, U max_, V min_)
}

/**
@brief Convert UTF16 into UTF8
@param dst a pointer to destination buffer
@param dst_size a length of destination buffer
@param src a source buffer
@return length except 0
@brief Convert UTF16 into UTF8
@param dst a pointer to destination buffer
@param dst_size a length of destination buffer
@param src a source buffer
@return length except 0
*/
inline int32_t ConvertUtf16ToUtf8(char* dst, int32_t dst_size, const char16_t* src)
{
Expand Down Expand Up @@ -380,11 +381,11 @@ inline int32_t ConvertUtf16ToUtf8(char* dst, int32_t dst_size, const char16_t* s
}

/**
@brief Convert UTF8 into UTF16
@param dst a pointer to destination buffer
@param dst_size a length of destination buffer
@param src a source buffer
@return length except 0
@brief Convert UTF8 into UTF16
@param dst a pointer to destination buffer
@param dst_size a length of destination buffer
@param src a source buffer
@return length except 0
*/
inline int32_t ConvertUtf8ToUtf16(char16_t* dst, int32_t dst_size, const char* src)
{
Expand Down Expand Up @@ -729,6 +730,9 @@ RefPtr<T> MakeRefPtr(Arg&&... args)
return RefPtr<T>(new T(args...));
}

class VectorField;
class VectorFieldLoader;

using SettingRef = RefPtr<Setting>;
using ManagerRef = RefPtr<Manager>;
using EffectRef = RefPtr<Effect>;
Expand All @@ -737,6 +741,7 @@ using SoundDataRef = RefPtr<SoundData>;
using ModelRef = RefPtr<Model>;
using MaterialRef = RefPtr<Material>;
using CurveRef = RefPtr<Curve>;
using VectorFieldRef = RefPtr<VectorField>;

using SpriteRendererRef = RefPtr<SpriteRenderer>;
using RibbonRendererRef = RefPtr<RibbonRenderer>;
Expand All @@ -752,6 +757,7 @@ using MaterialLoaderRef = RefPtr<MaterialLoader>;
using SoundLoaderRef = RefPtr<SoundLoader>;
using ModelLoaderRef = RefPtr<ModelLoader>;
using CurveLoaderRef = RefPtr<CurveLoader>;
using VectorFieldLoaderRef = RefPtr<VectorFieldLoader>;
using ProceduralModelGeneratorRef = RefPtr<ProceduralModelGenerator>;

/**
Expand Down
1 change: 1 addition & 0 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Model/ProceduralModelGenerator.h"
#include "Model/ProceduralModelParameter.h"
#include "Utils/Effekseer.BinaryReader.h"
#include "VectorField/VectorFieldLoader.h"

#include <array>
#include <functional>
Expand Down
1 change: 1 addition & 0 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Effekseer.TextureLoader.h"
#include "Model/ModelLoader.h"
#include "Model/ProceduralModelGenerator.h"
#include "VectorField/VectorFieldLoader.h"

//----------------------------------------------------------------------------------
//
Expand Down
12 changes: 12 additions & 0 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,16 @@ class ResourceManager : public ReferenceObject
CachedCurves.SetLoader(loader);
}

VectorFieldLoaderRef GetVectorFieldLoader() const
{
return CachedVectorFields.GetLoader();
}

void SetVectorFieldLoader(VectorFieldLoaderRef loader)
{
CachedVectorFields.SetLoader(loader);
}

ProceduralModelGeneratorRef GetProceduralMeshGenerator() const
{
return CachedProceduralModels.GetLoader();
Expand Down Expand Up @@ -332,6 +342,7 @@ class ResourceManager : public ReferenceObject
CachedMaterials.SetIsCacheEnabled(value);
CachedSounds.SetIsCacheEnabled(value);
CachedCurves.SetIsCacheEnabled(value);
CachedVectorFields.SetIsCacheEnabled(value);
CachedProceduralModels.SetIsCacheEnabled(value);
}

Expand All @@ -340,6 +351,7 @@ class ResourceManager : public ReferenceObject
CachedResources<SoundLoaderRef, SoundDataRef> CachedSounds;
CachedResources<MaterialLoaderRef, MaterialRef> CachedMaterials;
CachedResources<CurveLoaderRef, CurveRef> CachedCurves;
CachedResources<VectorFieldLoaderRef, VectorFieldRef> CachedVectorFields;
CachedParameterResources<ProceduralModelGeneratorRef, ProceduralModelParameter, ModelRef> CachedProceduralModels;
};

Expand Down
23 changes: 11 additions & 12 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Setting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Effekseer.TextureLoader.h"
#include "Model/ModelLoader.h"
#include "Model/ProceduralModelGenerator.h"
#include "VectorField/VectorFieldLoader.h"

#include "Renderer/Effekseer.ModelRenderer.h"
#include "Renderer/Effekseer.RibbonRenderer.h"
Expand Down Expand Up @@ -158,33 +159,31 @@ void Setting::SetMaterialLoader(MaterialLoaderRef loader)
resourceManager_->SetMaterialLoader(loader);
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
CurveLoaderRef Setting::GetCurveLoader() const
{
return resourceManager_->GetCurveLoader();
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Setting::SetCurveLoader(CurveLoaderRef loader)
{
resourceManager_->SetCurveLoader(loader);
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
VectorFieldLoaderRef Setting::GetVectorFieldLoader() const
{
return resourceManager_->GetVectorFieldLoader();
}

void Setting::SetVectorFieldLoader(VectorFieldLoaderRef loader)
{
resourceManager_->SetVectorFieldLoader(loader);
}

ProceduralModelGeneratorRef Setting::GetProceduralMeshGenerator() const
{
return resourceManager_->GetProceduralMeshGenerator();
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Setting::SetProceduralMeshGenerator(ProceduralModelGeneratorRef generator)
{
resourceManager_->SetProceduralMeshGenerator(generator);
Expand Down
20 changes: 20 additions & 0 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ class Setting : public ReferenceObject
*/
void SetCurveLoader(CurveLoaderRef loader);

/**
@brief
\~English get a vector field loader
\~Japanese ベクトル場ローダーを取得する。
@return
\~English loader
\~Japanese ローダー
*/
VectorFieldLoaderRef GetVectorFieldLoader() const;

/**
@brief
\~English specfiy a vector field loader
\~Japanese ベクトル場ローダーを設定する。
@param loader
\~English loader
\~Japanese ローダー
*/
void SetVectorFieldLoader(VectorFieldLoaderRef loader);

/**
@brief
\~English get a mesh generator
Expand Down
110 changes: 110 additions & 0 deletions Dev/Cpp/Effekseer/Effekseer/VectorField/VectorField.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "VectorField.h"

namespace Effekseer
{
void VectorField::Init(const std::array<int32_t, 3>& size, int32_t stride)
{
size_ = size;
stride_ = stride;
data_.resize((size_[0] * size_[1] * size_[2]) * stride_);
}

std::array<int32_t, 3> VectorField::GetSize() const
{
return size_;
}

std::array<float, 3> VectorField::GetData(const std::array<int32_t, 3>& position, VectorFieldSamplingMode sampling_mode) const
{
auto p = position;
if (sampling_mode == VectorFieldSamplingMode::Clamp)
{
for (int i = 0; i < 3; i++)
{
p[i] = Clamp(p[i], size_[i] - 1, 0);
}
}
else if (sampling_mode == VectorFieldSamplingMode::Repeat)
{
for (int i = 0; i < 3; i++)
{
p[i] = p[i] % size_[i];
}
}

std::array<float, 3> ret;
ret.fill(0.0f);
for (int i = 0; i < stride_; i++)
{
ret[i] = data_[(position[0] + position[1] * size_[0] + position[2] * size_[0] * size_[1]) * stride_ + i];
}

return ret;
}

void VectorField::SetData(const std::array<float, 3>& value, const std::array<int32_t, 3>& position)
{
for (int i = 0; i < stride_; i++)
{
data_[(position[0] + position[1] * size_[0] + position[2] * size_[0] * size_[1]) * stride_ + i] = value[i];
}
}

std::array<float, 3> VectorField::GetData(const std::array<float, 3>& position, VectorFieldSamplingMode sampling_mode) const
{
const auto lerp = [&](std::array<float, 3> v1, std::array<float, 3> v2, float a)
{
std::array<float, 3> ret{};
for (int i = 0; i < stride_; i++)
{
ret[i] = (v2[i] - v1[i]) * a + v1[i];
}
return ret;
};

std::array<int32_t, 3> iv[2];
std::array<float, 3> dv;
for (size_t i = 0; i < 3; i++)
{
iv[0][i] = static_cast<int>(position[i]);
dv[i] = position[i] - iv[0][i];
iv[1][i] = iv[0][i] + 1;
}

std::array<float, 3> v[2][2][2];
for (int z = 0; z < 2; z++)
{
for (int y = 0; y < 2; y++)
{
for (int x = 0; x < 2; x++)
{
v[x][y][z] = GetData(std::array<int32_t, 3>{iv[x][0], iv[y][1], iv[z][2]}, sampling_mode);
}
}
}

std::array<float, 3> vxy[2][2];
for (int y = 0; y < 2; y++)
{
for (int x = 0; x < 2; x++)
{
vxy[x][y] = lerp(v[x][y][0], v[x][y][1], dv[2]);
}
}

std::array<float, 3> vx[2];
for (int x = 0; x < 2; x++)
{
vx[x] = lerp(vxy[x][0], vxy[x][1], dv[1]);
}

auto ret = lerp(vx[0], vx[1], dv[0]);

for (size_t i = stride_; i < ret.size(); i++)
{
ret[i] = 0.0f;
}

return ret;
}
} // namespace Effekseer
39 changes: 39 additions & 0 deletions Dev/Cpp/Effekseer/Effekseer/VectorField/VectorField.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "../Effekseer.Resource.h"
#include "../SIMD/Vec3f.h"
#include "../Utils/Effekseer.CustomAllocator.h"
#include <array>
#include <stdint.h>

namespace Effekseer
{

enum class VectorFieldSamplingMode
{
Clamp,
Repeat,
};

class VectorField : public Resource
{
private:
std::array<int32_t, 3> size_;
CustomAlignedVector<float> data_;
int32_t stride_ = 0;

public:
void Init(const std::array<int32_t, 3>& size, int32_t stride);

std::array<int32_t, 3> GetSize() const;

std::array<float, 3> GetData(const std::array<int32_t, 3>& position, VectorFieldSamplingMode sampling_mode) const;

void SetData(const std::array<float, 3>& value, const std::array<int32_t, 3>& position);

std::array<float, 3> GetData(const std::array<float, 3>& position, VectorFieldSamplingMode sampling_mode) const;
};

using VectorFieldRef = RefPtr<VectorField>;

} // namespace Effekseer
Loading

0 comments on commit d006c4a

Please sign in to comment.