ppematclassesbase.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //! Created once, on manager init. Script-side representation of C++ material class, separate handling.
  2. class PPEClassBase
  3. {
  4. protected PPEManager m_Manager;
  5. protected string m_MaterialPath = "";
  6. protected Material m_Material;
  7. protected ref map<int, ref array<int>> m_ParameterUpdateQueueMap;
  8. protected ref array<int> m_ParameterRemovalQueue;
  9. protected ref array<int> m_UpdatedParameters;
  10. protected ref map<int,ref PPEMatClassParameterCommandData> m_MaterialParamMapStructure; //<param_idx, <ParamData>>
  11. void PPEClassBase(string mat_path_override = "")
  12. {
  13. Init(mat_path_override);
  14. CreateMaterial();
  15. CreateDataStructure();
  16. RegisterMaterialParameters();
  17. }
  18. protected void Init(string mat_path_override = "")
  19. {
  20. if (mat_path_override != "")
  21. {
  22. m_MaterialPath = mat_path_override;
  23. }
  24. else
  25. {
  26. m_MaterialPath = GetDefaultMaterialPath();
  27. }
  28. m_Manager = PPEManagerStatic.GetPPEManager();
  29. }
  30. protected void CreateMaterial()
  31. {
  32. if (m_MaterialPath != "")
  33. m_Material = GetGame().GetWorld().GetMaterial(m_MaterialPath);
  34. }
  35. Material GetMaterial()
  36. {
  37. return m_Material;
  38. }
  39. //do not override!
  40. protected void CreateDataStructure()
  41. {
  42. m_MaterialParamMapStructure = new map<int,ref PPEMatClassParameterCommandData>;
  43. //m_ParameterUpdateQueue = new array<int>;
  44. m_ParameterUpdateQueueMap = new map<int, ref array<int>>;
  45. m_ParameterRemovalQueue = new array<int>;
  46. m_UpdatedParameters = new array<int>;
  47. }
  48. //! inserted into associative array by parameter int value, parameter registration order does not matter (still ordered, though)
  49. protected void RegisterMaterialParameters();
  50. protected void RegisterParameterScalarBool(int idx, string parameter_name, bool default_value)
  51. {
  52. PPETemplateDefBool p = new PPETemplateDefBool(parameter_name,default_value);
  53. PPEMatClassParameterBool parameter_data = new PPEMatClassParameterBool(GetPostProcessEffectID(),idx,this);
  54. parameter_data.RegisterDefaults(p);
  55. m_MaterialParamMapStructure.Set(idx, parameter_data);
  56. }
  57. protected void RegisterParameterScalarInt(int idx, string parameter_name, int default_value, int min, int max)
  58. {
  59. PPETemplateDefInt p = new PPETemplateDefInt(parameter_name,default_value,min,max);
  60. PPEMatClassParameterInt parameter_data = new PPEMatClassParameterInt(GetPostProcessEffectID(),idx,this);
  61. parameter_data.RegisterDefaults(p);
  62. m_MaterialParamMapStructure.Set(idx, parameter_data);
  63. }
  64. //!WARNING - min/max values are usually taken from Workbench defaults, may not be actual min/max values the renderer can handle! When in doubt, try some higher/lower values...
  65. protected void RegisterParameterScalarFloat(int idx, string parameter_name, float default_value, float min, float max)
  66. {
  67. PPETemplateDefFloat p = new PPETemplateDefFloat(parameter_name,default_value,min,max);
  68. PPEMatClassParameterFloat parameter_data = new PPEMatClassParameterFloat(GetPostProcessEffectID(),idx,this);
  69. parameter_data.RegisterDefaults(p);
  70. m_MaterialParamMapStructure.Set(idx, parameter_data);
  71. }
  72. //!WARNING - min/max values are usually taken from Workbench defaults, may not be actual min/max values the renderer can handle! When in doubt, try some higher/lower values...
  73. protected void RegisterParameterScalarFloatEx(int idx, string parameter_name, float default_value, float min, float max, typename type)
  74. {
  75. PPETemplateDefFloat p = new PPETemplateDefFloat(parameter_name,default_value,min,max);
  76. PPEMatClassParameterFloat parameter_data;
  77. bool boo = Class.CastTo(parameter_data,type.Spawn());
  78. //Print("RegisterParameterColorEx: " + boo );
  79. parameter_data.RegisterDefaults(p);
  80. parameter_data.SetMaterialIndex(GetPostProcessEffectID());
  81. parameter_data.SetParameterIndex(idx);
  82. parameter_data.SetParent(this);
  83. m_MaterialParamMapStructure.Set(idx, parameter_data);
  84. }
  85. //!WARNING - min/max values are usually taken from Workbench defaults, may not be actual min/max values the renderer can handle! When in doubt, try some higher/lower values...
  86. protected void RegisterParameterColor(int idx, string parameter_name, float r, float g, float b, float a)
  87. {
  88. PPETemplateDefColor p = new PPETemplateDefColor(parameter_name,r,g,b,a);
  89. PPEMatClassParameterColor parameter_data = new PPEMatClassParameterColor(GetPostProcessEffectID(),idx,this);
  90. parameter_data.RegisterDefaults(p);
  91. m_MaterialParamMapStructure.Set(idx, parameter_data);
  92. }
  93. //!WARNING - min/max values are usually taken from Workbench defaults, may not be actual min/max values the renderer can handle! When in doubt, try some higher/lower values...
  94. protected void RegisterParameterColorEx(int idx, string parameter_name, float r, float g, float b, float a, typename type)
  95. {
  96. PPETemplateDefColor p = new PPETemplateDefColor(parameter_name,r,g,b,a);
  97. PPEMatClassParameterColor parameter_data;
  98. bool boo = Class.CastTo(parameter_data,type.Spawn());
  99. //Print("RegisterParameterColorEx: " + boo );
  100. parameter_data.RegisterDefaults(p);
  101. parameter_data.SetMaterialIndex(GetPostProcessEffectID());
  102. parameter_data.SetParameterIndex(idx);
  103. parameter_data.SetParent(this);
  104. m_MaterialParamMapStructure.Set(idx, parameter_data);
  105. }
  106. protected void RegisterParameterVector(int idx, string parameter_name, array<float> default_values) //needs to be compatible with every type of vector (vector2 to vector4), hence array<float>...
  107. {
  108. PPETemplateDefVector p = new PPETemplateDefVector(parameter_name,default_values);
  109. PPEMatClassParameterVector parameter_data = new PPEMatClassParameterVector(GetPostProcessEffectID(),idx,this);
  110. parameter_data.RegisterDefaults(p);
  111. m_MaterialParamMapStructure.Set(idx, parameter_data);
  112. }
  113. //TEXTURE and RESOURCE types are not overridable during runtime..currently unused and unhandled
  114. protected void RegisterParameterTexture(int idx, string parameter_name, string default_path)
  115. {
  116. PPETemplateDefTexture p = new PPETemplateDefTexture(parameter_name,default_path);
  117. PPEMatClassParameterTexture parameter_data = new PPEMatClassParameterTexture(GetPostProcessEffectID(),idx,this);
  118. parameter_data.RegisterDefaults(p);
  119. m_MaterialParamMapStructure.Set(idx, parameter_data);
  120. }
  121. protected void RegisterParameterResource(int idx, string parameter_name, string default_path)
  122. {
  123. PPETemplateDefResource p = new PPETemplateDefResource(parameter_name,default_path);
  124. PPEMatClassParameterResource parameter_data = new PPEMatClassParameterResource(GetPostProcessEffectID(),idx,this);
  125. parameter_data.RegisterDefaults(p);
  126. m_MaterialParamMapStructure.Set(idx, parameter_data);
  127. }
  128. //------------------------------------------------------------------------------------
  129. //! Distributes requester data to the material class structure and links them to appropriate parameter
  130. void InsertParamValueData(PPERequestParamDataBase request_data)
  131. {
  132. PPEMatClassParameterCommandData param_data;
  133. bool exists = m_MaterialParamMapStructure.Find(request_data.GetParameterID(),param_data);
  134. if ( !exists )
  135. {
  136. Error("PPEClassBase | InsertParamValueData | mat/par/req: " + GetPostProcessEffectID() + "/" + request_data.GetParameterID() + "/" + request_data.GetRequesterIDX() + " not registered in m_MaterialParamMapStructure!");
  137. return;
  138. }
  139. request_data.SetDataActive(true);
  140. request_data.SetUpdatingDataValues(true); //marks request data as updating
  141. param_data.InsertRequestData(request_data);//<request_ID, data>
  142. //DbgPrnt("PPEDebug | InsertParamValueData | mat/par/req: " + GetPostProcessEffectID() + "/" + request_data.GetParameterID() + "/" + request_data.GetRequesterIDX() + " | requester: " + request_data.m_Requester);
  143. }
  144. //TODO - rework
  145. //!unused, see 'RemoveActiveRequestFromMaterials' for more info
  146. void RemoveRequest(int req_idx)
  147. {
  148. /*for (int i = 0; i < m_ActiveMaterialRequestMap.Count(); i++)
  149. {
  150. ActiveParameterRequestsMap dbg = m_ActiveMaterialRequestMap.Get(i);
  151. //DbgPrnt("PPEDebug | dbg size: " + dbg.Count());
  152. if ( m_ActiveMaterialRequestMap.Get(i).Contains(req_idx) )
  153. {
  154. m_ActiveMaterialRequestMap.Get(i).Remove(req_idx);
  155. dbg = m_ActiveMaterialRequestMap.Get(i);
  156. if ( !m_ActiveMaterialRequestMap.Get(i) || m_ActiveMaterialRequestMap.Get(i).Count() < 1 )
  157. {
  158. if (m_ParameterUpdateQueue.IsValidIndex(i))
  159. {
  160. DbgPrnt("PPEDebug | PPEClassBase - RemoveRequest | Removing: " + i);
  161. DbgPrnt("PPEDebug | PPEClassBase - RemoveRequest | exit 4 - last request removal");
  162. m_ParameterRemovalQueue.Insert(m_ParameterUpdateQueue.Get(i));
  163. }
  164. else
  165. DbgPrnt("PPEDebug | PPEClassBase - RemoveRequest | Update queue no. " + i + " already removed!");
  166. }
  167. }
  168. //Adds to update one more time
  169. m_Manager.SetMaterialParamUpdating(GetPostProcessEffectID(),i);
  170. }*/
  171. }
  172. //! generic update method, take care when overriding!
  173. void OnUpdate(float timeslice, int order)
  174. {
  175. int parameter_idx = -1;
  176. //DbgPrnt("PPEDebug | PPEClassBase - OnUpdate | mat_id: " + GetPostProcessEffectID());
  177. if ( m_ParameterUpdateQueueMap.Contains(order) )
  178. {
  179. //Print(m_ParameterUpdateQueueMap.Get(order));
  180. for ( int i = 0; i < m_ParameterUpdateQueueMap.Get(order).Count(); i++ )
  181. {
  182. //DbgPrnt("PPEDebug | PPEClassBase - OnUpdate | parameter_idx: " + m_ParameterUpdateQueue.Get(i));
  183. Param p_values; //TODO - move to material classes?
  184. bool setting_defaults = false;
  185. parameter_idx = m_ParameterUpdateQueueMap.Get(order).Get(i);
  186. m_MaterialParamMapStructure.Get(parameter_idx).Update(timeslice,p_values,setting_defaults,order);
  187. InsertUpdatedParameter(parameter_idx);
  188. }
  189. m_Manager.InsertUpdatedMaterial(GetPostProcessEffectID());
  190. ParamUpdateQueueCleanup(order);
  191. }
  192. /*if ( !m_ParameterUpdateQueueMap.Contains(order) || m_ParameterUpdateQueueMap.Get(order).Count() < 1 )
  193. m_Manager.RemoveMaterialUpdating(GetPostProcessEffectID(),order); //stops material from updating when no parameters are.*/
  194. }
  195. //! Clamps the values being set to defaults, if there is no request setting non-zero values on the parameter
  196. void SetFinalParameterValue(int parameter_idx)
  197. {
  198. int var_type = GetParameterCommandData(parameter_idx).GetParameterVarType();
  199. Param values = GetParameterCommandData(parameter_idx).GetCurrentValues();
  200. switch (var_type)
  201. {
  202. case PPEConstants.VAR_TYPE_BOOL:
  203. bool value_var_bool = Param1<bool>.Cast(values).param1;
  204. m_Material.SetParamByIndex(parameter_idx,value_var_bool);
  205. //Print("DebugValues | PPEConstants.VAR_TYPE_BOOL | bool val: " + value_var_bool);
  206. break;
  207. case PPEConstants.VAR_TYPE_INT:
  208. int value_var_int = Param1<int>.Cast(values).param1;
  209. m_Material.SetParamByIndex(parameter_idx,value_var_int);
  210. //Print("DebugValues | PPEConstants.VAR_TYPE_BOOL | bool val: " + value_var_bool);
  211. break;
  212. case PPEConstants.VAR_TYPE_FLOAT:
  213. float value_var_float = Param1<float>.Cast(values).param1;
  214. m_Material.SetParamByIndex(parameter_idx,value_var_float);
  215. //Print("DebugValues | PPEConstants.VAR_TYPE_FLOAT | float val: " + value_var_float);
  216. break;
  217. case PPEConstants.VAR_TYPE_COLOR:
  218. float color[4] = {0,0,0,0};
  219. color[0] = Param4<float,float,float,float>.Cast(values).param1;
  220. color[1] = Param4<float,float,float,float>.Cast(values).param2;
  221. color[2] = Param4<float,float,float,float>.Cast(values).param3;
  222. color[3] = Param4<float,float,float,float>.Cast(values).param4;
  223. m_Material.SetParamByIndex(parameter_idx,color);
  224. //Print("DebugValues | PPEConstants.VAR_TYPE_COLOR | color val:: " + color[0] + " " + color[1] + " " + color[2] + " " + color[3]);
  225. break;
  226. }
  227. }
  228. void ApplyValueChanges()
  229. {
  230. int parameter_id;
  231. for (int i = 0; i < m_UpdatedParameters.Count(); i++)
  232. {
  233. parameter_id = m_UpdatedParameters.Get(i);
  234. SetFinalParameterValue(parameter_id);
  235. }
  236. m_UpdatedParameters.Clear();
  237. }
  238. protected void InsertUpdatedParameter(int mat_id)
  239. {
  240. if ( m_UpdatedParameters.Find(mat_id) == -1 )
  241. m_UpdatedParameters.Insert(mat_id);
  242. }
  243. //! Queue selected parameter for removal from the update queue
  244. void ParamUpdateRemove(int parameter_idx)
  245. {
  246. if ( m_ParameterRemovalQueue.Find(parameter_idx) == -1 )
  247. m_ParameterRemovalQueue.Insert(parameter_idx);
  248. }
  249. //! Queue specific parameter of this material to update
  250. void SetParameterUpdating(int order, int parameter_id)
  251. {
  252. if ( !m_ParameterUpdateQueueMap.Contains(order) )
  253. {
  254. m_ParameterUpdateQueueMap.Set(order,new array<int>);
  255. }
  256. if ( m_ParameterUpdateQueueMap.Get(order).Find(parameter_id) == -1 )
  257. {
  258. m_ParameterUpdateQueueMap.Get(order).Insert(parameter_id);
  259. }
  260. }
  261. protected void ParamUpdateQueueCleanup(int order)
  262. {
  263. //DbgPrnt("PPEDebug | PPEClassBase - ParamUpdateQueueCleanup | mat_id: " + GetPostProcessEffectID() + " | UpdateQueue count: " + m_ParameterUpdateQueue.Count());
  264. //DbgPrnt("PPEDebug | PPEClassBase - ParamUpdateQueueCleanup | mat_id: " + GetPostProcessEffectID() + " | RemovalQueue count: " + m_ParameterRemovalQueue.Count());
  265. for ( int i = 0; i < m_ParameterUpdateQueueMap.Get(order).Count(); i++ )
  266. {
  267. if ( m_ParameterRemovalQueue.Find(m_ParameterUpdateQueueMap.Get(order).Get(i)) != -1 )
  268. {
  269. //DbgPrnt("PPEDebug | PPEClassBase - ParamUpdateQueueCleanup | removing update of: " + m_ParameterUpdateQueue.Get(i));
  270. //m_ParameterUpdateQueue.RemoveItem(m_ParameterUpdateQueue.Get(i));
  271. }
  272. }
  273. m_ParameterUpdateQueueMap.Get(order).Clear();
  274. }
  275. //! override this if you want to use different path by default; '.emat' is appended automatically
  276. string GetDefaultMaterialPath();
  277. void ChangeMaterialPathUsed(string path)
  278. {
  279. m_MaterialPath = path;
  280. m_Material = null;
  281. CreateMaterial();
  282. }
  283. string GetCurrentMaterialPath()
  284. {
  285. return m_MaterialPath;
  286. }
  287. //! Overriden in all material classes!
  288. int GetPostProcessEffectID()
  289. {
  290. return PostProcessEffectType.None;
  291. }
  292. //! Some PP effects are handled as hard-coded exceptions, outside of material system. Default == PPEExceptions.NONE (systemic behaviour)
  293. /*int GetExceptionID()
  294. {
  295. return PPEExceptions.NONE;
  296. }*/
  297. PPEMatClassParameterCommandData GetParameterCommandData(int parameter_idx)
  298. {
  299. return m_MaterialParamMapStructure.Get(parameter_idx);
  300. }
  301. #ifdef DEVELOPER
  302. //Debug//
  303. //-----//
  304. /*void DumpMap()
  305. {
  306. DbgPrnt("PPEClassDebug | m_ActiveMaterialRequestMap COUNT: " + m_ActiveMaterialRequestMap.Count());
  307. for (int i = 0; i < m_ActiveMaterialRequestMap.Count(); i++)
  308. {
  309. DbgPrnt("PPEClassDebug | m_ActiveRequest#: " + i);
  310. //ActiveMaterialRequests request = m_ActiveMaterialRequestMap.Get(i);
  311. DbgPrnt("PPEClassDebug | request: " + request);
  312. for (int j = 0; j < request.Count(); j++)
  313. {
  314. DbgPrnt("PPEClassDebug | request#: " + j);
  315. array<bool,float,int,int> values = request.Get(j);
  316. foreach (auto val : values)
  317. {
  318. DbgPrnt("PPEClassDebug | relative: " + val);
  319. }
  320. }
  321. }
  322. DbgPrnt("------------");
  323. }*/
  324. #endif
  325. void DbgPrnt(string text)
  326. {
  327. //Debug.Log(""+text);
  328. }
  329. };