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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
// SUBSTANCE HOOK
C++RAW
#include "UnityPrefix.h"
#include "Runtime/Mono/MonoBehaviour.h"
#include "Runtime/Graphics/ProceduralTexture.h"
#include "Runtime/Graphics/SubstanceArchive.h"
#include "Runtime/Graphics/ProceduralMaterial.h"
#include "Runtime/Graphics/Texture2D.h"
#include "Runtime/Math/Color.h"
#include "Configuration/UnityConfigure.h"
#include "Runtime/Scripting/ScriptingManager.h"
#include "Runtime/Scripting/ScriptingExportUtility.h"
#if ENABLE_SUBSTANCE
#include "Runtime/Graphics/SubstanceSystem.h"
#endif
#include "Runtime/Scripting/Scripting.h"
CSRAW
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections;
namespace UnityEngine
{
// The global Substance engine processor usage (as used for the ProceduralMaterial.substanceProcessorUsage property).
CONDITIONAL ENABLE_SUBSTANCE
ENUM ProceduralProcessorUsage
// Exact control of processor usage is not available.
Unsupported = 0,
// A single physical processor core is used for material generation.
One = 1,
// Half of all physical processor cores are used for material generation.
Half = 2,
// All physical processor cores are used for material generation.
All = 3
END
// Substance memory budget.
CONDITIONAL ENABLE_SUBSTANCE
ENUM ProceduralCacheSize
// A limit of 128MB for the cache or the working memory.
Tiny = 0,
// A limit of 256MB for the cache or the working memory.
Medium = 1,
// A limit of 512MB for the cache or the working memory.
Heavy = 2,
// No limit for the cache or the working memory.
NoLimit = 3,
// A limit of 1B (one byte) for the cache or the working memory.
None = 4
END
// ProceduralMaterial loading behavior
CONDITIONAL ENABLE_SUBSTANCE
ENUM ProceduralLoadingBehavior
// Do not generate the textures, allowing a custom loading
DoNothing = 0,
// Generate the textures when loading to favor application's size (default on supported platform)
Generate = 1,
// Bake the textures to speed-up loading, then it may be generated later on
BakeAndKeep = 2,
// Bake the textures and remove dependencies (default on unsupported platform)
BakeAndDiscard = 3,
// Generate the textures when loading and cache them to speed-up subsequent startups
Cache = 4
END
// The type of a Procedural property.
CONDITIONAL ENABLE_SUBSTANCE
ENUM ProceduralPropertyType
// Procedural boolean property. Use with ProceduralMaterial.GetProceduralBoolean.
Boolean = 0,
// Procedural float property. Use with ProceduralMaterial.GetProceduralFloat.
Float = 1,
// Procedural Vector2 property. Use with ProceduralMaterial.GetProceduralVector.
Vector2 = 2,
// Procedural Vector3 property. Use with ProceduralMaterial.GetProceduralVector.
Vector3 = 3,
// Procedural Vector4 property. Use with ProceduralMaterial.GetProceduralVector.
Vector4 = 4,
// Procedural Color property without alpha. Use with ProceduralMaterial.GetProceduralColor.
Color3 = 5,
// Procedural Color property with alpha. Use with ProceduralMaterial.GetProceduralColor.
Color4 = 6,
// Procedural Enum property. Use with ProceduralMaterial.GetProceduralEnum.
Enum = 7,
// Procedural Texture property. Use with ProceduralMaterial.GetProceduralTexture.
Texture = 8
END
// The type of generated image in a Procedural Material.
CONDITIONAL ENABLE_SUBSTANCE
ENUM ProceduralOutputType
// Undefined type.
Unknown = 0,
// Diffuse type.
Diffuse = 1,
// NormalMap (BumpMap) type.
Normal = 2,
// HeightMap type.
Height = 3,
// Emmisive type.
Emissive = 4,
// Specular (GlossMap) type.
Specular = 5,
// Opacity (Tranparence) type.
Opacity = 6
END
// Describes a Procedural property.
CONDITIONAL ENABLE_SUBSTANCE
CSRAW [StructLayout (LayoutKind.Sequential)]
CLASS ProceduralPropertyDescription
// The name of the Procedural property. Used to get and set the values.
CSRAW public string name;
// The user friendly label of the Procedural property. Used to display Procedural property friendly names.
CSRAW public string label;
// The name of the GUI group. Used to display Procedural property in groups.
CSRAW public string group;
// The [[ProceduralPropertyType]] describes what type of property this is.
CSRAW public ProceduralPropertyType type;
// If true, the Float or Vector property is constrained to values within a specified range.
CSRAW public bool hasRange;
// If hasRange is true, minimum specifies the minimum allowed value for this Float or Vector property.
CSRAW public float minimum;
// If hasRange is true, maximum specifies the maximum allowed value for this Float or Vector property.
CSRAW public float maximum;
// Specifies the step size of this Float or Vector property. Zero is no step.
CSRAW public float step;
// The available options for a Procedural property of type Enum.
CSRAW public string[] enumOptions;
END
C++RAW
#if ENABLE_SUBSTANCE
struct MonoProceduralPropertyDescription
{
ScriptingStringPtr name;
ScriptingStringPtr label;
ScriptingStringPtr group;
ProceduralPropertyType type;
bool hasRange;
float minimum;
float maximum;
float step;
ScriptingArrayPtr enumOptions;
};
void ProceduralPropertyDescriptionToMono (const SubstanceInput& src, MonoProceduralPropertyDescription& dest)
{
dest.name = scripting_string_new(src.name);
dest.label = scripting_string_new(src.label);
dest.group = scripting_string_new(src.group);
dest.type = src.type;
dest.hasRange = src.IsFlagEnabled(SubstanceInput::Flag_Clamp);
dest.minimum = src.minimum;
dest.maximum = src.maximum;
dest.step = src.step;
dest.enumOptions = Scripting::StringVectorToMono (src.GetEnumOptions ());
}
#endif
// ProceduralMaterial class.
// TODO: Make example use material instead of sharedMaterial when instancing works.
// TODO: Make example lerp between property min and max value when GetProceduralPropertyDescription is implemented.
CONDITIONAL !UNITY_FLASH && !UNITY_WEBGL
CLASS ProceduralMaterial : Material
// Default constructor. This should not be used.
// TODO: Constructor that takes a source ProceduralMaterial as parameter and clones it.
CSRAW public ProceduralMaterial () : base ("") {}
// Get an array of descriptions of all the properties this Procedural Material has.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM ProceduralPropertyDescription[] GetProceduralPropertyDescriptions ()
{
const std::vector<SubstanceInput>& inputs = self->GetSubstanceInputs ();
return VectorToScriptingClassArray<SubstanceInput, MonoProceduralPropertyDescription> (inputs, GetScriptingManager().GetCommonClasses().substancePropertyDescription, ProceduralPropertyDescriptionToMono);
}
// Checks if the Procedural Material has a property of a given name.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM bool HasProceduralProperty (string inputName) { return self->HasSubstanceProperty (inputName); }
// Get a named Procedural boolean property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM bool GetProceduralBoolean (string inputName) { return self->GetSubstanceBoolean (inputName); }
// Set a named Procedural boolean property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void SetProceduralBoolean (string inputName, bool value) { self->SetSubstanceBoolean (inputName, value); }
// Get a named Procedural float property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM float GetProceduralFloat (string inputName) { return self->GetSubstanceFloat (inputName); }
// Set a named Procedural float property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void SetProceduralFloat (string inputName, float value) { self->SetSubstanceFloat (inputName, value); }
// Get a named Procedural vector property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM Vector4 GetProceduralVector (string inputName) { return self->GetSubstanceVector (inputName); }
// Set a named Procedural vector property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void SetProceduralVector (string inputName, Vector4 value) { self->SetSubstanceVector (inputName, value); }
// Get a named Procedural color property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM Color GetProceduralColor (string inputName) { return self->GetSubstanceColor (inputName); }
// Set a named Procedural color property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void SetProceduralColor (string inputName, Color value) { self->SetSubstanceColor (inputName, value); }
// Get a named Procedural enum property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM int GetProceduralEnum (string inputName) { return self->GetSubstanceEnum (inputName); }
// Set a named Procedural enum property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void SetProceduralEnum (string inputName, int value) { self->SetSubstanceEnum (inputName, value); }
// Get a named Procedural texture property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM Texture2D GetProceduralTexture (string inputName) { return Scripting::ScriptingWrapperFor (self->GetSubstanceTexture (inputName)); }
// Set a named Procedural texture property.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void SetProceduralTexture (string inputName, Texture2D value) { self->SetSubstanceTexture (inputName, value); }
// Checks if a named Procedural property is cached for efficient runtime tweaking.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM bool IsProceduralPropertyCached (string inputName) { return self->IsSubstancePropertyCached (inputName); }
// Specifies if a named Procedural property should be cached for efficient runtime tweaking.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void CacheProceduralProperty (string inputName, bool value) { self->CacheSubstanceProperty (inputName, value); }
// Clear the Procedural cache.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void ClearCache () { self->ClearCache (); }
// Set & get the Procedural cache budget.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM_PROP ProceduralCacheSize cacheSize
{
return self->GetProceduralMemoryBudget ();
}
{
self->SetProceduralMemoryBudget(value);
}
// Set & get the update rate in millisecond of the animated substance
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM_PROP int animationUpdateRate
{
return self->GetAnimationUpdateRate ();
}
{
self->SetAnimationUpdateRate(value);
}
// Triggers an asyncronous rebuild of all dirty textures.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void RebuildTextures ()
{
self->RebuildTextures ();
}
// Triggers an immediate (synchronous) rebuild of all dirty textures.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM void RebuildTexturesImmediately ()
{
self->RebuildTexturesImmediately ();
}
// Checks if the Procedural Material is currently in the process of rebuilding the textures.
// Note that it doesn't show if it has not changed.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM_PROP bool isProcessing
{
return self->IsProcessing ();
}
// Remove the current pending rebuilds of the Procedural Material.
CUSTOM static void StopRebuilds ()
{
ProceduralMaterial::StopProcessing ();
}
// Should the Procedural Material be generated at load time?
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM_PROP bool isLoadTimeGenerated
{
return self->IsFlagEnabled (ProceduralMaterial::Flag_Animated)
|| self->GetLoadingBehavior ()!=ProceduralLoadingBehavior_None;
}
{
self->SetLoadingBehavior( value ? ProceduralLoadingBehavior_Generate : ProceduralLoadingBehavior_None );
}
// Get Procedural Material loading behavior
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM_PROP ProceduralLoadingBehavior loadingBehavior
{
return self->GetLoadingBehavior ();
}
// Checks if the Procedural Materials are supported on the current platform.
CUSTOM_PROP static bool isSupported
{
return IsSubstanceSupported ();
}
// Used to specify the Substance engine CPU usage.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM_PROP static ProceduralProcessorUsage substanceProcessorUsage
{
return ProceduralMaterial::GetProceduralProcessorUsage ();
}
{
ProceduralMaterial::SetProceduralProcessorUsage (value);
}
// XML string of "input/value" pairs (setting the preset rebuilds the textures)
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM_PROP string preset
{
return scripting_string_new(self->GetPreset ());
}
{
self->SetPreset (value);
}
// Get generated textures.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM Texture[] GetGeneratedTextures ()
{
return CreateScriptingArrayFromUnityObjects (self->GetPingedTextures (), ClassID (Texture));
}
// Get generated texture by name.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM ProceduralTexture GetGeneratedTexture (string textureName)
{
return Scripting::ScriptingWrapperFor (self->GetGeneratedTexture (textureName));
}
// Readable substances keep the generated texture data accessible to GetPixels32 (format must be RAW (RGBA or ARGB)).
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM_PROP bool isReadable
{
return self->IsFlagEnabled (ProceduralMaterial::Flag_Readable);
}
{
self->EnableFlag (ProceduralMaterial::Flag_Readable, value);
}
END
CONDITIONAL !UNITY_FLASH && !UNITY_WEBGL
CLASS ProceduralTexture : Texture
// The output type of this Texture.
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM ProceduralOutputType GetProceduralOutputType ()
{
return self->GetType ();
}
// Retrieve the procedural material parent to this texture
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM internal ProceduralMaterial GetProceduralMaterial ()
{
return Scripting::ScriptingWrapperFor(self->GetSubstanceMaterial ());
}
// Check if the texture has already been generated, at least one time
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM internal bool HasBeenGenerated ()
{
return self->HasBeenGenerated ();
}
// Get pixels from a generated texture.
// The ProceduralMaterial must have the flag isReadable set before a call to RebuildTexturesImmediately and be configured as 'RAW' format (RGBA/ARGB).
CONDITIONAL ENABLE_SUBSTANCE
CUSTOM Color32[] GetPixels32(int x, int y, int blockWidth, int blockHeight)
{
int w = blockWidth; if( w < 1 ) w = 1;
int h = blockHeight; if( h < 1 ) h = 1;
ScriptingArray* colors = CreateScriptingArray<ColorRGBA32>(GetMonoManager().GetCommonClasses().color32, w * h);
ColorRGBA32* firstElement = Scripting::GetScriptingArrayStart<ColorRGBA32>(colors);
self->GetPixels32(x, y, blockWidth, blockHeight, firstElement);
return colors;
}
END
CSRAW
} // namespace UnityEngine
|