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 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 (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 (GetScriptingManager().GetCommonClasses().string, count); for (int i=0;i(array,i,scripting_string_new(self->GetShaderKeywords ()[i])); } return array; } { Material::ShaderKeywordsT names; for (int i=0;i (value, i))); self->SetShaderKeywords (names); } END CSRAW }