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
|
#include "UnityPrefix.h"
#include "ProceduralMaterial.h"
int ProceduralPreset_parseValues(std::vector<std::string>& values, std::string name, std::string line)
{
values.clear();
int start = line.find(name);
if (start==std::string::npos)
{
return 0;
}
line = line.substr(start+name.size());
if (line.size()<3 || line[0]!='=' || line[1]!='\"')
{
return 0;
}
line = line.substr(2);
int p=0;
string val;
while (p<line.size() && line[p]!='\"')
{
if (line[p]==',')
{
values.push_back(val); val = "";
}
else
{
val += line[p];
}
++p;
}
if (val.size()>0)
{
values.push_back(val);
}
return values.size();
}
inline std::string ProceduralPreset_getLine(std::string::const_iterator& pos, const std::string& preset)
{
std::string::const_iterator i = pos;
std::string::const_iterator it = std::find(i, preset.end(), '\n');
pos = it;
if (it!=preset.end()) ++pos;
return std::string(i, it);
}
bool ProceduralMaterial::SetPreset(const std::string& preset)
{
std::string::const_iterator pos = preset.begin();
std::string line;
line = ProceduralPreset_getLine(pos, preset);
if (line.find("formatversion=\"1.0\"")==std::string::npos)
{
return false;
}
while (pos!=preset.end() && line.find("/sbspreset")==std::string::npos)
{
line = ProceduralPreset_getLine(pos, preset);
if (line.find("presetinput")!=std::string::npos)
{
std::vector<std::string> identifier;
if (ProceduralPreset_parseValues(identifier, "identifier", line)!=1)
continue;
SubstanceInput* input = FindSubstanceInput(identifier[0]);
if (input==NULL || identifier[0]=="$normalformat" || identifier[0]=="$outputsize")
continue;
std::vector<std::string> type;
if (ProceduralPreset_parseValues(type, "type", line)!=1)
continue;
unsigned int internalType = atoi(type[0].c_str());
if (internalType!=input->internalType || internalType==Substance_IType_Image)
continue;
std::vector<std::string> value;
ProceduralPreset_parseValues(value, "value", line);
float f[4];
int count = GetRequiredInputComponentCount((SubstanceInputType)internalType);
if (value.size()!=count)
continue;
for (int i=0 ; i<count ; ++i)
{
f[i] = (float)atof(value[i].c_str());
}
SetSubstanceVector(identifier[0], Vector4f(f));
}
}
RebuildTextures();
return true;
}
std::string ProceduralMaterial::GetPreset() const
{
std::string preset = "<sbspresets formatversion=\"1.0\" count=\"1\">\n";
preset += " <sbspreset pkgurl=\"\" description=\"\" label=\"\">\n";
char tmp[256];
for (SubstanceInputs::const_iterator i=m_Inputs.begin() ; i!=m_Inputs.end() ; ++i)
{
preset += " <presetinput identifier=\"";
preset += i->name;
preset += "\" uid=\"";
snprintf(tmp, 256, "%d", i->internalIdentifier);
preset += tmp;
preset += "\" type=\"";
snprintf(tmp, 256, "%d", (int)i->internalType);
preset += tmp;
preset += "\" value=\"";
if (i->internalType!=Substance_IType_Image)
{
bool isInteger = IsSubstanceAnyIntType(i->internalType);
int count = GetRequiredInputComponentCount(i->internalType);
for (int j=0 ; j<count ; ++j)
{
if (j>0) preset += ",";
if (isInteger) snprintf(tmp, 256, "%d", (int)i->value.scalar[j]);
else snprintf(tmp, 256, "%f", i->value.scalar[j]);
preset += tmp;
}
}
preset += "\"/>\n";
}
preset += " </sbspreset>\n";
preset += "</sbspresets>\n";
return preset;
}
ProceduralTexture* ProceduralMaterial::GetGeneratedTexture(const std::string& textureName)
{
for (Textures::iterator it=m_Textures.begin() ; it!=m_Textures.end() ; ++it)
{
if (it->IsValid() && (*it)->GetName()==textureName)
{
return &**it;
}
}
return NULL;
}
|