// 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& inputs = self->GetSubstanceInputs (); return VectorToScriptingClassArray (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(GetMonoManager().GetCommonClasses().color32, w * h); ColorRGBA32* firstElement = Scripting::GetScriptingArrayStart(colors); self->GetPixels32(x, y, blockWidth, blockHeight, firstElement); return colors; } END CSRAW } // namespace UnityEngine