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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
#include "UnityPrefix.h"
#include "Runtime/Animation/GenericAnimationBindingCache.h"
#include "Runtime/Animation/AnimationClipBindings.h"
#include "Renderer.h"
#include "External/shaderlab/Library/FastPropertyName.h"
#include "External/shaderlab/Library/properties.h"
#include "Runtime/Shaders/Material.h"
#include "Runtime/Shaders/MaterialProperties.h"
#include "Runtime/Interfaces/IAnimationBinding.h"
#define MATERIAL_ANIMATION 1
class RendererAnimationBinding : public IAnimationBinding
{
#if UNITY_EDITOR
virtual void GetAllAnimatableProperties (Object& targetObject, std::vector<EditorCurveBinding>& outProperties) const
{
Renderer& renderer = static_cast<Renderer&> (targetObject);
for (int i=0;i<renderer.GetMaterialCount();i++)
AddPPtrBinding (outProperties, targetObject.GetClassID(), Format("m_Materials.Array.data[%d]", i));
}
#endif
virtual float GetFloatValue (const UnityEngine::Animation::BoundCurve& bind) const
{
AssertString("unsupported"); return 0.0F;
}
virtual void SetFloatValue (const UnityEngine::Animation::BoundCurve& bound, float value) const
{
AssertString("unsupported");
}
virtual void SetPPtrValue (const UnityEngine::Animation::BoundCurve& bound, SInt32 value) const
{
Renderer& renderer = *static_cast<Renderer*> (bound.targetObject);
int index = reinterpret_cast<int> (bound.targetPtr);
if (index < renderer.GetMaterialCount ())
renderer.SetMaterial(PPtr<Material> (value), index);
}
virtual SInt32 GetPPtrValue (const UnityEngine::Animation::BoundCurve& bound) const
{
Renderer& renderer = *static_cast<Renderer*> (bound.targetObject);
int index = reinterpret_cast<int> (bound.targetPtr);
if (index < renderer.GetMaterialCount ())
return renderer.GetMaterial(index).GetInstanceID();
else
return 0;
}
virtual bool GenerateBinding (const UnityStr& attribute, bool pptrCurve, UnityEngine::Animation::GenericBinding& outputBinding) const
{
int index = ParseIndexAttributeIndex (attribute, "m_Materials.Array.data[");
if (index != -1 && pptrCurve)
{
outputBinding.attribute = index;
return true;
}
return false;
}
virtual ClassIDType BindValue (Object& target, const UnityEngine::Animation::GenericBinding& outputBinding, UnityEngine::Animation::BoundCurve& bound) const
{
bound.targetPtr = reinterpret_cast<void*> (outputBinding.attribute);
return ClassID(Material);
}
};
#if MATERIAL_ANIMATION
const char* kMaterialPrefix = "material.";
class RendererMaterialAnimationBinding : public IAnimationBinding
{
public:
#if UNITY_EDITOR
virtual void GetAllAnimatableProperties (Object& targetObject, std::vector<EditorCurveBinding>& outProperties) const
{
Renderer& renderer = static_cast<Renderer&> (targetObject);
if (renderer.GetMaterialCount() == 0)
return;
int startIndex = outProperties.size();
for (int i=0;i<renderer.GetMaterialCount();i++)
{
Material* material = renderer.GetMaterial(i);
if (material == NULL)
continue;
ExtractAllMaterialAnimatableAttributes (*material, targetObject.GetClassID(), outProperties, startIndex);
}
}
static void ExtractAllMaterialAnimatableAttributes (Material& targetObject, int classID, std::vector<EditorCurveBinding>& outProperties, int startIndex)
{
Material& material = static_cast<Material&> (targetObject);
const ShaderLab::PropertySheet& properties = material.GetProperties();
const string materialPrefix = kMaterialPrefix;
// Get all float properties
const ShaderLab::PropertySheet::Floats& floats = properties.GetFloatsMap();
for (ShaderLab::PropertySheet::Floats::const_iterator i=floats.begin();i != floats.end();i++)
{
AddBindingCheckUnique (outProperties, startIndex, classID, materialPrefix + i->first.GetName());
}
// Get all vector properties
const ShaderLab::PropertySheet::Vectors& vectors = properties.GetVectorMap();
for (ShaderLab::PropertySheet::Vectors::const_iterator i=vectors.begin();i != vectors.end();i++)
{
string prefix = materialPrefix + i->first.GetName();
if (properties.GetColorTag(i->first))
{
AddBindingCheckUnique (outProperties, startIndex, classID, prefix + ".r");
AddBindingCheckUnique (outProperties, startIndex, classID, prefix + ".g");
AddBindingCheckUnique (outProperties, startIndex, classID, prefix + ".b");
AddBindingCheckUnique (outProperties, startIndex, classID, prefix + ".a");
}
else
{
AddBindingCheckUnique (outProperties, startIndex, classID, prefix + ".x");
AddBindingCheckUnique (outProperties, startIndex, classID, prefix + ".y");
AddBindingCheckUnique (outProperties, startIndex, classID, prefix + ".z");
AddBindingCheckUnique (outProperties, startIndex, classID, prefix + ".w");
}
}
}
#endif
struct MaterialBinding
{
enum { kFloat4, kColor4, kFloat1 };
UInt32 propertyName : 28;
UInt32 colIndex : 2;
UInt32 type : 2;
};
static UInt32 BindingToUInt32 (MaterialBinding binding)
{
UInt32 data;
data = binding.propertyName;
data |= binding.colIndex << 28;
data |= binding.type << 30;
return data;
}
static MaterialBinding UInt32ToBinding (UInt32 data)
{
MaterialBinding binding;
binding.propertyName = data & 0x3FFFFFFF;
binding.colIndex = (data >> 28) & 0x3;
binding.type = (data >> 30) & 0x3;
return binding;
}
virtual float GetFloatValue (const UnityEngine::Animation::BoundCurve& bind) const
{
Renderer& renderer = *static_cast<Renderer*> (bind.targetObject);
MaterialBinding binding = *reinterpret_cast<const MaterialBinding*> (&bind.targetPtr);
ShaderLab::FastPropertyName name;
name.index = binding.propertyName;
const MaterialPropertyBlock* block = renderer.GetPropertyBlock();
// Extract from material property block
if (block)
{
if (binding.type == MaterialBinding::kFloat1)
{
const float* value = block->FindFloat (name);
if (value != NULL)
return *value;
}
else if (binding.type == MaterialBinding::kFloat4)
{
const Vector4f* value = block->FindVector (name);
if (value != NULL)
return value->GetPtr()[binding.colIndex];
}
else if (binding.type == MaterialBinding::kColor4)
{
ColorRGBAf color;
if (block->GetColor (name, color))
return color.GetPtr()[binding.colIndex];
}
}
// Extract from material
for (int i=0;i<renderer.GetMaterialCount ();i++)
{
Material* material = renderer.GetMaterial(0);
if (material == NULL)
continue;
if (!material->HasProperty (name))
continue;
if (binding.type == MaterialBinding::kFloat1)
return material->GetFloat(name);
else if (binding.type == MaterialBinding::kColor4)
return material->GetColor(name).GetPtr()[binding.colIndex];
else if (binding.type == MaterialBinding::kFloat4)
return material->GetColor(name).GetPtr()[binding.colIndex];
}
return 0.0F;
}
virtual void SetFloatValue (const UnityEngine::Animation::BoundCurve& bound, float value) const
{
Renderer& renderer = *static_cast<Renderer*> (bound.targetObject);
MaterialBinding binding = *reinterpret_cast<const MaterialBinding*> (&bound.targetPtr);
MaterialPropertyBlock& block = renderer.GetPropertyBlockRememberToUpdateHash();
ShaderLab::FastPropertyName name;
name.index = binding.propertyName;
if (binding.type == MaterialBinding::kFloat1)
{
block.ReplacePropertyFloat (name, value);
}
else if (binding.type == MaterialBinding::kFloat4)
{
block.ReplacePartialFloatProperty (name, value, 4, binding.colIndex);
}
else if (binding.type == MaterialBinding::kColor4)
{
block.ReplacePartialFloatColorProperty (name, value, 4, binding.colIndex);
}
renderer.ComputeCustomPropertiesHash();
// Force a repaint
#if UNITY_EDITOR
renderer.SetDirty();
#endif
}
virtual void SetPPtrValue (const UnityEngine::Animation::BoundCurve& bound, SInt32 value) const { }
virtual SInt32 GetPPtrValue (const UnityEngine::Animation::BoundCurve& bound) const { return 0; }
virtual bool GenerateBinding (const UnityStr& attributeStr, bool pptrCurve, UnityEngine::Animation::GenericBinding& outputBinding) const
{
if (pptrCurve)
return false;
if (!BeginsWith(attributeStr, kMaterialPrefix))
return false;
MaterialBinding binding;
const char* attribute = attributeStr.c_str() + strlen(kMaterialPrefix);
const char* a = attribute;
// Parse this:
// mainColor.r
// mainColor.x
// mainColor.y
// floatPropertyName
// Find shader propertyname
int dotIndex = -1;
const char* lastCharacter;
while (*a != 0)
{
if (*a == '.' && dotIndex == -1)
dotIndex = a - attribute;
a++;
}
lastCharacter = a - 1;
// No '.' found, thus it must be a float property
if (dotIndex == -1)
{
binding.propertyName = ShaderLab::GenerateFastPropertyName28BitHash(attribute);
binding.type = MaterialBinding::kFloat1;
binding.colIndex = 0;
}
// Calculate different property types
else
{
binding.propertyName = ShaderLab::GenerateFastPropertyName28BitHash(string(attribute, attribute + dotIndex).c_str());
// There must be exactly one property ('r', 'g' etc after the .)
if (dotIndex + 2 != strlen(attribute))
return false;
binding.type = MaterialBinding::kFloat4;
switch (*lastCharacter)
{
case 'r':
case 'g':
case 'b':
case 'a':
binding.type = MaterialBinding::kColor4;
}
switch (*lastCharacter)
{
// r color or x vector
case 'r':
case 'x':
binding.colIndex = 0;
break;
// g color or y vector
case 'g':
case 'y':
binding.colIndex = 1;
break;
// b or z vector
case 'b':
case 'z':
binding.colIndex = 2;
break;
// alpha or w vector
case 'a':
case 'w':
binding.colIndex = 3;
break;
default:
return false;
}
}
outputBinding.attribute = BindingToUInt32 (binding);
return true;
}
virtual ClassIDType BindValue (Object& target, const UnityEngine::Animation::GenericBinding& outputBinding, UnityEngine::Animation::BoundCurve& bound) const
{
MaterialBinding materialBinding = UInt32ToBinding(outputBinding.attribute);
ShaderLab::FastPropertyName prop;
prop.InitBy28BitHash (materialBinding.propertyName);
materialBinding.propertyName = prop.index;
bound.targetPtr = *reinterpret_cast<void**> (&materialBinding);
return ClassID(float);
}
};
#endif
static RendererAnimationBinding* gRendererBinding = NULL;
#if MATERIAL_ANIMATION
static RendererMaterialAnimationBinding* gMaterialBinding = NULL;
#endif
void InitializeRendererAnimationBindingInterface ()
{
gRendererBinding = UNITY_NEW (RendererAnimationBinding, kMemAnimation);
UnityEngine::Animation::GetGenericAnimationBindingCache ().RegisterIAnimationBinding (ClassID(Renderer), UnityEngine::Animation::kRendererMaterialPPtrBinding, gRendererBinding);
#if MATERIAL_ANIMATION
gMaterialBinding = UNITY_NEW (RendererMaterialAnimationBinding, kMemAnimation);
UnityEngine::Animation::GetGenericAnimationBindingCache ().RegisterIAnimationBinding (ClassID(Renderer), UnityEngine::Animation::kRendererMaterialPropertyBinding, gMaterialBinding);
#endif
}
void CleanupRendererAnimationBindingInterface ()
{
UNITY_DELETE (gRendererBinding, kMemAnimation);
#if MATERIAL_ANIMATION
UNITY_DELETE (gMaterialBinding, kMemAnimation);
#endif
}
#if ENABLE_UNIT_TESTS
#include "External/UnitTest++/src/UnitTest++.h"
SUITE (MaterialBindingTests)
{
TEST (MaterialBindingUInt32Conversion)
{
RendererMaterialAnimationBinding::MaterialBinding binding;
binding.propertyName = 12345678;
binding.colIndex = 3;
binding.type = 3;
RendererMaterialAnimationBinding::MaterialBinding converted = RendererMaterialAnimationBinding::UInt32ToBinding (RendererMaterialAnimationBinding::BindingToUInt32 (binding));
CHECK_EQUAL (converted.propertyName, binding.propertyName);
CHECK_EQUAL (converted.colIndex, binding.colIndex);
CHECK_EQUAL (converted.type, binding.type);
}
TEST (MaterialBindingCorrectlyEncodesAllBits)
{
CHECK_EQUAL ( RendererMaterialAnimationBinding::BindingToUInt32(RendererMaterialAnimationBinding::UInt32ToBinding (0xFFFFFFFF)), 0xFFFFFFFF);
CHECK_EQUAL ( RendererMaterialAnimationBinding::BindingToUInt32(RendererMaterialAnimationBinding::UInt32ToBinding (0)), 0);
}
}
#endif
|