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
|
C++RAW
#include "UnityPrefix.h"
#include "Configuration/UnityConfigure.h"
#include "Runtime/Mono/MonoExportUtility.h"
#include "Runtime/Shaders/Material.h"
#include "Runtime/Shaders/Shader.h"
#include "External/shaderlab/Library/intshader.h"
#include "Runtime/Camera/Renderqueue.h"
#include "External/shaderlab/Library/properties.h"
#include "Runtime/Misc/ResourceManager.h"
#include "Runtime/Shaders/ShaderNameRegistry.h"
#include "Runtime/Shaders/ShaderKeywords.h"
#include "Runtime/Shaders/ComputeShader.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "Runtime/Scripting/ScriptingManager.h"
#include "Runtime/Scripting/Backend/ScriptingTypeRegistry.h"
#include "Runtime/Scripting/ScriptingExportUtility.h"
#include "Runtime/Scripting/Backend/ScriptingBackendApi.h"
#include "Runtime/Misc/GraphicsScriptingUtility.h"
#include "Runtime/Scripting/ScriptingObjectWithIntPtrField.h"
C++RAW
PPtr<Shader> s_ScriptingCurrentShader;
const ChannelAssigns* s_ScriptingCurrentChannels;
CSRAW
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections;
namespace UnityEngine
{
CLASS Shader : Object
CUSTOM static Shader Find (string name)
{
return Scripting::ScriptingWrapperFor(GetScriptMapper().FindShader(name));
}
CUSTOM internal static Shader FindBuiltin (string name)
{
return Scripting::ScriptingWrapperFor(GetBuiltinResource<Shader> (name));
}
AUTO_PROP bool isSupported IsSupported
CONDITIONAL UNITY_EDITOR
CUSTOM_PROP internal string customEditor { return scripting_string_new (self->GetCustomEditorName()); }
CUSTOM static void EnableKeyword (string keyword) {
g_ShaderKeywords.Enable( keywords::Create( keyword ) );
}
CUSTOM static void DisableKeyword (string keyword) {
g_ShaderKeywords.Disable( keywords::Create( keyword ) );
}
AUTO_PROP int maximumLOD GetMaximumShaderLOD SetMaximumShaderLOD
CUSTOM_PROP static int globalMaximumLOD { return Shader::GetGlobalMaximumShaderLOD(); } { Shader::SetGLobalMaximumShaderLOD (value); }
CUSTOM_PROP int renderQueue { return self->GetShaderLabShader()->GetRenderQueue(); }
CSRAW public static void SetGlobalColor (string propertyName, Color color) {
SetGlobalColor(Shader.PropertyToID(propertyName), color);
}
CUSTOM static void SetGlobalColor (int nameID, Color color) {
ShaderLab::PropertySheet *props = ShaderLab::g_GlobalProperties;
ShaderLab::FastPropertyName propName; propName.index = nameID;
props->SetVector (propName, color.GetPtr ());
}
CSRAW public static void SetGlobalVector (string propertyName, Vector4 vec) {
SetGlobalColor (propertyName, vec);
}
CSRAW public static void SetGlobalVector (int nameID, Vector4 vec) {
SetGlobalColor (nameID, vec);
}
CSRAW public static void SetGlobalFloat (string propertyName, float value) {
SetGlobalFloat(Shader.PropertyToID(propertyName), value);
}
CUSTOM static void SetGlobalFloat (int nameID, float value) {
ShaderLab::PropertySheet *props = ShaderLab::g_GlobalProperties;
ShaderLab::FastPropertyName propName; propName.index = nameID;
props->SetFloat (propName, value);
}
CSRAW public static void SetGlobalInt (string propertyName, int value) { SetGlobalFloat(propertyName, (float)value); }
CSRAW public static void SetGlobalInt (int nameID, int value) { SetGlobalFloat(nameID, (float)value); }
CSRAW public static void SetGlobalTexture (string propertyName, Texture tex) {
SetGlobalTexture (Shader.PropertyToID(propertyName), tex);
}
CUSTOM static void SetGlobalTexture (int nameID, Texture tex) {
Texture& texture = *tex;
ShaderLab::PropertySheet *props = ShaderLab::g_GlobalProperties;
ShaderLab::FastPropertyName propName; propName.index = nameID;
props->SetTexture (propName, &texture);
}
CSRAW public static void SetGlobalMatrix (string propertyName, Matrix4x4 mat) {
SetGlobalMatrix (Shader.PropertyToID(propertyName), mat);
}
CUSTOM static void SetGlobalMatrix (int nameID, Matrix4x4 mat) {
ShaderLab::PropertySheet *props = ShaderLab::g_GlobalProperties;
ShaderLab::FastPropertyName propName; propName.index = nameID;
props->SetValueProp (propName, 16, mat.GetPtr());
}
CUSTOM static void SetGlobalTexGenMode (string propertyName, TexGenMode mode) {
ShaderLab::PropertySheet *props = ShaderLab::g_GlobalProperties;
props->GetTexEnv(ScriptingStringToProperty (propertyName))->SetTexGen ((TexGenMode)mode);
}
CUSTOM static void SetGlobalTextureMatrixName (string propertyName, string matrixName) {
if(propertyName.Length() == 0)
{
ErrorString ("SetGlobalTextureMatrixName: Invalid empty propertyName");
return;
}
if(matrixName.Length() == 0)
{
ErrorString ("SetGlobalTextureMatrixName: Invalid empty matrixName");
return;
}
ShaderLab::PropertySheet *props = ShaderLab::g_GlobalProperties;
props->GetTexEnv(ScriptingStringToProperty (propertyName))->SetMatrixName (ScriptingStringToProperty (matrixName));
}
CONDITIONAL !UNITY_FLASH
CUSTOM static void SetGlobalBuffer (string propertyName, ComputeBuffer buffer) {
ShaderLab::PropertySheet *props = ShaderLab::g_GlobalProperties;
props->SetComputeBuffer (ScriptingStringToProperty (propertyName), buffer ? buffer->GetBufferHandle() : ComputeBufferID());
}
CUSTOM static int PropertyToID (string name) {
return ScriptingStringToProperty (name).index;
}
CUSTOM static void WarmupAllShaders () {
WarmupAllShaders ();
}
END
NONSEALED_CLASS Material : Object
CSRAW public Material (string contents) { Internal_CreateWithString (this, contents); }
CSRAW public Material (Shader shader) { Internal_CreateWithShader (this, shader); }
CSRAW public Material (Material source) : base () { Internal_CreateWithMaterial (this, source); }
AUTO_PTR_PROP Shader shader GetShader SetShader
CSRAW public Color color { get { return GetColor ("_Color"); } set { SetColor ("_Color", value); } }
CSRAW public Texture mainTexture { get { return GetTexture ("_MainTex"); } set { SetTexture ("_MainTex", value); } }
CSRAW public Vector2 mainTextureOffset { get { return GetTextureOffset("_MainTex"); } set { SetTextureOffset("_MainTex", value); } }
CSRAW public Vector2 mainTextureScale { get { return GetTextureScale("_MainTex"); } set { SetTextureScale("_MainTex", value); } }
CSRAW public void SetColor (string propertyName, Color color)
{
SetColor (Shader.PropertyToID(propertyName), color);
}
CUSTOM void SetColor (int nameID, Color color)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
self->SetColor (propName, color);
}
CSRAW public Color GetColor (string propertyName)
{
return GetColor (Shader.PropertyToID(propertyName));
}
CUSTOM Color GetColor (int nameID)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
return self->GetColor (propName);
}
CSRAW public void SetVector (string propertyName, Vector4 vector)
{
SetColor (propertyName, new Color (vector.x, vector.y, vector.z, vector.w));
}
CSRAW public void SetVector (int nameID, Vector4 vector)
{
SetColor (nameID, new Color (vector.x, vector.y, vector.z, vector.w));
}
CSRAW public Vector4 GetVector (string propertyName)
{
Color temp = GetColor (propertyName);
return new Vector4 (temp.r, temp.g, temp.b, temp.a);
}
CSRAW public Vector4 GetVector (int nameID)
{
Color temp = GetColor (nameID);
return new Vector4 (temp.r, temp.g, temp.b, temp.a);
}
CSRAW public void SetTexture (string propertyName, Texture texture)
{
SetTexture (Shader.PropertyToID(propertyName), texture);
}
CUSTOM void SetTexture (int nameID, Texture texture)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
self->SetTexture (propName, texture);
}
CSRAW public Texture GetTexture (string propertyName)
{
return GetTexture (Shader.PropertyToID(propertyName));
}
CUSTOM Texture GetTexture (int nameID)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
return Scripting::ScriptingWrapperFor (self->GetTexture (propName));
}
// Workaround for gcc/msvc where passing small mono structures by value does not work
CUSTOM private static void Internal_GetTextureOffset (Material mat, string name, out Vector2 output)
{
*output = mat->GetTextureOffset( ScriptingStringToProperty(name) );
}
CUSTOM private static void Internal_GetTextureScale (Material mat, string name, out Vector2 output)
{
*output = mat->GetTextureScale( ScriptingStringToProperty(name) );
}
CUSTOM void SetTextureOffset (string propertyName, Vector2 offset)
{
self->SetTextureOffset( ScriptingStringToProperty(propertyName), offset );
}
CSRAW public Vector2 GetTextureOffset (string propertyName)
{
Vector2 r;
Internal_GetTextureOffset(this, propertyName, out r);
return r;
}
CUSTOM void SetTextureScale (string propertyName, Vector2 scale)
{
self->SetTextureScale( ScriptingStringToProperty(propertyName), scale );
}
CSRAW public Vector2 GetTextureScale (string propertyName)
{
Vector2 r;
Internal_GetTextureScale(this, propertyName, out r);
return r;
}
CSRAW public void SetMatrix (string propertyName, Matrix4x4 matrix)
{
SetMatrix (Shader.PropertyToID(propertyName), matrix);
}
CUSTOM void SetMatrix (int nameID, Matrix4x4 matrix)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
self->SetMatrix (propName, matrix);
}
CSRAW public Matrix4x4 GetMatrix (string propertyName)
{
return GetMatrix (Shader.PropertyToID(propertyName));
}
CUSTOM Matrix4x4 GetMatrix (int nameID)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
return self->GetMatrix (propName);
}
CSRAW public void SetFloat (string propertyName, float value)
{
SetFloat (Shader.PropertyToID(propertyName), value);
}
CUSTOM void SetFloat (int nameID, float value)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
self->SetFloat (propName, value);
}
CSRAW public float GetFloat (string propertyName)
{
return GetFloat (Shader.PropertyToID(propertyName));
}
CUSTOM float GetFloat (int nameID)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
return self->GetFloat (propName);
}
CSRAW public void SetInt (string propertyName, int value) { SetFloat(propertyName, (float)value); }
CSRAW public void SetInt (int nameID, int value) { SetFloat(nameID, (float)value); }
CSRAW public int GetInt (string propertyName) { return (int)GetFloat(propertyName); }
CSRAW public int GetInt (int nameID) { return (int)GetFloat(nameID); }
CONDITIONAL !UNITY_FLASH
CUSTOM void SetBuffer (string propertyName, ComputeBuffer buffer) {
FastPropertyName fpName = ScriptingStringToProperty(propertyName);
self->SetComputeBuffer (fpName, buffer ? buffer->GetBufferHandle() : ComputeBufferID());
}
CSRAW public bool HasProperty (string propertyName)
{
return HasProperty (Shader.PropertyToID(propertyName));
}
CUSTOM bool HasProperty (int nameID)
{
ShaderLab::FastPropertyName propName; propName.index = nameID;
return self->HasProperty (propName);
}
CUSTOM string GetTag (string tag, bool searchFallbacks, string defaultValue = "") {
return scripting_string_new (self->GetTag (tag, !searchFallbacks, defaultValue));
}
CUSTOM void Lerp (Material start, Material end, float t)
{
const ShaderLab::PropertySheet &s1 = start->GetProperties();
const ShaderLab::PropertySheet &s2 = end->GetProperties();
self->GetWritableProperties().LerpProperties( s1, s2, clamp01(t) );
}
AUTO_PROP int passCount GetPassCount
CUSTOM bool SetPass (int pass) {
Material& mat = *self;
if (pass >= mat.GetPassCount())
{
ErrorStringMsg("Trying to access pass %d, but material '%s' subshader (0) has only %d valid passes.",
pass,
mat.GetName(),
mat.GetPassCount());
return false;
}
if (!CheckShouldRenderPass (pass, mat))
return false;
s_ScriptingCurrentShader = mat.GetShaderPPtr();
s_ScriptingCurrentChannels = mat.SetPass(pass);
return s_ScriptingCurrentChannels != NULL;
}
AUTO_PROP int renderQueue GetActualRenderQueue SetCustomRenderQueue
OBSOLETE warning Use the Material constructor instead.
CSRAW static public Material Create (string scriptContents)
{
return new Material (scriptContents);
}
CUSTOM private static void Internal_CreateWithString ([Writable]Material mono, string contents)
{
Material *mat = Material::CreateMaterial (contents.AsUTF8().c_str(), 0, true);
Scripting::ConnectScriptingWrapperToObject (mono.GetScriptingObject(), mat);
mat->ApplyMaterialPropertyDrawers();
}
CUSTOM private static void Internal_CreateWithShader ([Writable]Material mono, Shader shader)
{
Material *mat = Material::CreateMaterial (*shader, 0, true);
Scripting::ConnectScriptingWrapperToObject (mono.GetScriptingObject(), mat);
mat->ApplyMaterialPropertyDrawers();
}
CUSTOM private static void Internal_CreateWithMaterial ([Writable]Material mono, Material source)
{
Material *mat = Material::CreateMaterial (*source, 0, true);
Scripting::ConnectScriptingWrapperToObject (mono.GetScriptingObject(), mat);
mat->ApplyMaterialPropertyDrawers();
}
CUSTOM void CopyPropertiesFromMaterial (Material mat) {
if (mat != NULL)
self->CopyPropertiesFromMaterial(*mat);
else
ErrorString ("Trying to copy properties from null material.");
}
CUSTOM void EnableKeyword (string keyword) {
self->EnableKeyword (keyword);
}
CUSTOM void DisableKeyword (string keyword) {
self->DisableKeyword (keyword);
}
CONDITIONAL ENABLE_MONO || UNITY_WINRT
CUSTOM_PROP String[] shaderKeywords {
const size_t count = self->GetShaderKeywords ().size ();
ScriptingArrayPtr array = CreateScriptingArray<ScriptingStringPtr> (GetScriptingManager().GetCommonClasses().string, count);
for (int i=0;i<count;i++)
{
Scripting::SetScriptingArrayElement<ScriptingStringPtr>(array,i,scripting_string_new(self->GetShaderKeywords ()[i]));
}
return array;
}
{
Material::ShaderKeywordsT names;
for (int i=0;i<GetScriptingArraySize (value);i++)
names.push_back (scripting_cpp_string_for (Scripting::GetScriptingArrayElementNoRef<ScriptingStringPtr> (value, i)));
self->SetShaderKeywords (names);
}
END
CSRAW
}
|