This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathParticleEffects.cpp
188 lines (170 loc) · 6.34 KB
/
ParticleEffects.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
// Developed by Minigraph
//
// Author(s): James Stanard
//
#include "ParticleEffects.h"
#include "../Core/ParticleEffectManager.h"
#include "TextureManager.h"
#include <fstream>
#pragma warning(push)
#pragma warning(disable : 4100) // unreferenced formal parameter
#include "json.hpp"
#pragma warning(pop)
using namespace std;
using namespace Graphics;
using namespace Math;
namespace
{
map<wstring, uint32_t> s_TextureArrayLookup;
vector<TextureRef> s_TextureReferences;
uint32_t GetTextureIndex(const wstring& name)
{
// Look for the texture already being assigned an index. If it's not found,
// load the texture and assign it an index.
auto iter = s_TextureArrayLookup.find(name);
if (iter != s_TextureArrayLookup.end())
{
return iter->second;
}
else
{
uint32_t index = (uint32_t)s_TextureArrayLookup.size();
s_TextureArrayLookup[name] = index;
// Load the texture and register it with the effect manager
TextureRef texture = TextureManager::LoadDDSFromFile(name, kMagenta2D, true);
s_TextureReferences.push_back(texture);
ParticleEffectManager::RegisterTexture(index, *texture.Get());
return index;
}
}
void InstantiateEffect( ParticleEffectProperties& effectProperties )
{
effectProperties.EmitProperties.TextureID = GetTextureIndex(effectProperties.TexturePath);
ParticleEffectManager::InstantiateEffect(effectProperties);
}
}
//---------------------------------------------------------------------
//
// JSON Init File Handling
//
//---------------------------------------------------------------------
#define MAKE_FLOAT(it) (float)it.value()
#define MAKE_UINT(it) (uint32_t)it.value()
#define MAKE_WSTRING(it) Utility::UTF8ToWideString(it.value())
#define MAKE_COLOR(it) Color((float)it.value()[0], (float)it.value()[1], (float)it.value()[2], (float)it.value()[3])
#define MAKE_VECTOR3(it) Vector3((float)it.value()[0], (float)it.value()[1], (float)it.value()[2])
#define MAKE_VECTOR4(it) Vector4((float)it.value()[0], (float)it.value()[1], (float)it.value()[2], (float)it.value()[3])
#define MAKE_XMFLOAT2(it) XMFLOAT2((float)it.value()[0], (float)it.value()[1])
#define MAKE_XMFLOAT3(it) XMFLOAT3((float)it.value()[0], (float)it.value()[1], (float)it.value()[2])
void ParticleEffects::InitFromJSON(const wstring& InitJsonFile)
{
using json = nlohmann::json;
json particle_setup;
ifstream(InitJsonFile) >> particle_setup;
if (!particle_setup.is_object() || particle_setup.find("ParticleEmitters") == particle_setup.end())
return;
for (auto& emitter : particle_setup["ParticleEmitters"])
{
ParticleEffectProperties Effect = ParticleEffectProperties();
for (json::iterator it = emitter.begin(); it != emitter.end(); ++it)
{
if (it.key() == "TexturePath")
{
Effect.TexturePath = MAKE_WSTRING(it);
}
else if (it.key() == "MinStartColor")
{
Effect.MinStartColor = MAKE_COLOR(it);
}
else if (it.key() == "MaxStartColor")
{
Effect.MaxStartColor = MAKE_COLOR(it);
}
else if (it.key() == "MinEndColor")
{
Effect.MinEndColor = MAKE_COLOR(it);
}
else if (it.key() == "MaxEndColor")
{
Effect.MaxEndColor = MAKE_COLOR(it);
}
else if (it.key() == "MaxEndColor")
{
Effect.MaxEndColor = MAKE_COLOR(it);
}
else if (it.key() == "TotalActiveLifetime")
{
Effect.TotalActiveLifetime = MAKE_FLOAT(it);
if (Effect.TotalActiveLifetime == 0.0f)
Effect.TotalActiveLifetime = FLT_MAX;
}
else if (it.key() == "EmitRate")
{
Effect.EmitRate = MAKE_FLOAT(it);
}
else if (it.key() == "Size")
{
Effect.Size = MAKE_VECTOR4(it);
}
else if (it.key() == "Velocity")
{
Effect.Velocity = MAKE_VECTOR4(it);
}
else if (it.key() == "LifeMinMax")
{
Effect.LifeMinMax = MAKE_XMFLOAT2(it);
}
else if (it.key() == "MassMinMax")
{
Effect.MassMinMax = MAKE_XMFLOAT2(it);
}
else if (it.key() == "Spread")
{
Effect.Spread = MAKE_XMFLOAT3(it);
}
else if (it.key() == "EmitProperties")
{
for (json::iterator it2 = it.value().begin(); it2 != it.value().end(); ++it2)
{
if (it2.key() == "Gravity")
{
Effect.EmitProperties.Gravity = MAKE_XMFLOAT3(it2);
}
else if (it2.key() == "FloorHeight")
{
Effect.EmitProperties.FloorHeight = MAKE_FLOAT(it2);
}
else if (it2.key() == "MaxParticles")
{
Effect.EmitProperties.MaxParticles = MAKE_UINT(it2);
}
else if (it2.key() == "EmitPosW")
{
Effect.EmitProperties.EmitPosW = MAKE_XMFLOAT3(it2);
}
else if (it2.key() == "LastEmitPosW")
{
Effect.EmitProperties.LastEmitPosW = MAKE_XMFLOAT3(it2);
}
else
{
Utility::Printf("Warning: Emitter property \"%s\" not found \n", it2.key().c_str());
}
}
}
else
{
Utility::Printf("Warning: Effect property \"%s\" not found \n", it.key().c_str());
}
}
InstantiateEffect(Effect);
}
}