CCPUMaterialTranslator.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUMaterialTranslator.h"
  23. #include "extensions/Particle3D/PU/CCPUMaterialManager.h"
  24. NS_CC_BEGIN
  25. enum MaterialToken
  26. {
  27. TOKEN_MAT_TECHNIQUE,
  28. TOKEN_MAT_PASS,
  29. //pass
  30. TOKEN_MAT_LIGHTING,
  31. TOKEN_MAT_AMIBIENT,
  32. TOKEN_MAT_DIFFUSE,
  33. TOKEN_MAT_SPECULAR,
  34. TOKEN_MAT_EMISSIVE,
  35. TOKEN_MAT_BLEND,
  36. TOKEN_MAT_DEPTH_CHECK,
  37. TOKEN_MAT_DEPTH_WRITE,
  38. TOKEN_MAT_TEXTURE_UNIT,
  39. //texture unit
  40. TOKEN_MAT_TEXTURE_FILE,
  41. TOKEN_MAT_TEXTURE_WRAP,
  42. //status
  43. TOKEN_MAT_ON,
  44. TOKEN_MAT_OFF,
  45. TOKEN_MAT_BLEND_ADD,
  46. TOKEN_MAT_BLEND_ALPHA,
  47. TOKEN_MAT_BLEND_COLOR,
  48. TOKEN_MAT_BLEND_MODULATE,
  49. TOKEN_MAT_BLEND_REPLACE,
  50. TOKEN_MAT_BLEND_SRC_COLOR,
  51. TOKEN_MAT_BLEND_ONE,
  52. TOKEN_MAT_BLEND_ZERO,
  53. TOKEN_MAT_BLEND_DEST_COLOR,
  54. TOKEN_MAT_TEXTURE_REPEAT,
  55. TOKEN_MAT_TEXTURE_CLAMP,
  56. TOKEN_MAT_TEXTURE_MIRROR,
  57. };
  58. static const std::string matToken[] = {
  59. "technique",
  60. "pass",
  61. //pass
  62. "lighting",
  63. "ambient",
  64. "diffuse",
  65. "specular",
  66. "emissive",
  67. "scene_blend",
  68. "depth_check",
  69. "depth_write",
  70. "texture_unit",
  71. //texture unit
  72. "texture",
  73. "tex_address_mode",
  74. //status
  75. "on",
  76. "off",
  77. "add",
  78. "alpha_blend",
  79. "colour_blend",
  80. "modulate",
  81. "replace",
  82. "src_colour",
  83. "one",
  84. "zero",
  85. "dest_colour",
  86. "repeat",
  87. "clamp",
  88. "mirror",
  89. };
  90. CCPUMaterialTranslator::CCPUMaterialTranslator()//:mTechnique(0)
  91. {
  92. }
  93. CCPUMaterialTranslator::~CCPUMaterialTranslator()
  94. {
  95. }
  96. void CCPUMaterialTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode *node)
  97. {
  98. PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
  99. //PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
  100. _material = new (std::nothrow) PUMaterial();
  101. _material->fileName = obj->file;
  102. _material->name = obj->name;
  103. _material->autorelease();
  104. _ms->addMaterial(_material);
  105. obj->context = _material;
  106. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  107. {
  108. if((*i)->type == ANT_OBJECT)
  109. {
  110. PUObjectAbstractNode* child = reinterpret_cast<PUObjectAbstractNode*>(*i);
  111. if(child->cls == matToken[TOKEN_MAT_TECHNIQUE])
  112. {
  113. PUMaterialTechniqueTranslator materialTechniqueTranslator;
  114. materialTechniqueTranslator.translate(compiler, *i);
  115. }
  116. }
  117. }
  118. }
  119. void CCPUMaterialTranslator::setMaterialSystem( PUMaterialCache *ms )
  120. {
  121. _ms = ms;
  122. }
  123. void PUMaterialTechniqueTranslator::translate( PUScriptCompiler* compiler, PUAbstractNode *node )
  124. {
  125. PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
  126. PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
  127. if (parent)
  128. obj->context = parent->context;
  129. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  130. {
  131. if((*i)->type == ANT_OBJECT)
  132. {
  133. PUObjectAbstractNode* child = reinterpret_cast<PUObjectAbstractNode*>(*i);
  134. if(child->cls == matToken[TOKEN_MAT_PASS])
  135. {
  136. PUMaterialPassTranslator materialPassTranslator;
  137. materialPassTranslator.translate(compiler, *i);
  138. }
  139. }
  140. }
  141. }
  142. void PUMaterialPassTranslator::translate( PUScriptCompiler* compiler, PUAbstractNode *node )
  143. {
  144. PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
  145. PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
  146. if (parent)
  147. obj->context = parent->context;
  148. PUMaterial *material = static_cast<PUMaterial *>(obj->context);
  149. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  150. {
  151. if((*i)->type == ANT_PROPERTY)
  152. {
  153. PUPropertyAbstractNode *prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
  154. if (prop->name == matToken[TOKEN_MAT_LIGHTING])
  155. {
  156. if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_LIGHTING], VAL_STRING))
  157. {
  158. std::string val;
  159. if(getString(*prop->values.front(), &val))
  160. {
  161. if (val == matToken[TOKEN_MAT_ON])
  162. {
  163. material->isEnabledLight = true;
  164. }
  165. else if (val == matToken[TOKEN_MAT_OFF])
  166. {
  167. material->isEnabledLight = false;
  168. }
  169. }
  170. }
  171. }
  172. else if (prop->name == matToken[TOKEN_MAT_AMIBIENT])
  173. {
  174. if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_AMIBIENT], VAL_VECTOR4))
  175. {
  176. Vec4 val;
  177. if(getVector4(prop->values.begin(), prop->values.end(), &val))
  178. {
  179. material->ambientColor = val;
  180. }
  181. }
  182. }
  183. else if (prop->name == matToken[TOKEN_MAT_DIFFUSE])
  184. {
  185. if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_AMIBIENT], VAL_VECTOR4))
  186. {
  187. Vec4 val;
  188. if(getVector4(prop->values.begin(), prop->values.end(), &val))
  189. {
  190. material->diffuseColor = val;
  191. }
  192. }
  193. }
  194. else if (prop->name == matToken[TOKEN_MAT_SPECULAR])
  195. {
  196. PUAbstractNodeList::const_iterator it = prop->values.begin();
  197. PUAbstractNodeList::const_iterator end = prop->values.end();
  198. unsigned int n = 0;
  199. Vec4 color;
  200. float shininess = 0.0f;
  201. while(it != end)
  202. {
  203. float v = 0;
  204. if(getFloat(**it, &v))
  205. {
  206. switch(n)
  207. {
  208. case 0:
  209. color.x = v;
  210. break;
  211. case 1:
  212. color.y = v;
  213. break;
  214. case 2:
  215. color.z = v;
  216. break;
  217. case 3:
  218. color.w = v;
  219. break;
  220. case 4:
  221. shininess = v;
  222. break;
  223. }
  224. }
  225. ++n;
  226. ++it;
  227. }
  228. material->specularColor = color;
  229. material->shininess = shininess;
  230. }
  231. else if (prop->name == matToken[TOKEN_MAT_EMISSIVE])
  232. {
  233. if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_AMIBIENT], VAL_VECTOR4))
  234. {
  235. Vec4 val;
  236. if(getVector4(prop->values.begin(), prop->values.end(), &val))
  237. {
  238. material->emissiveColor = val;
  239. }
  240. }
  241. }
  242. else if (prop->name == matToken[TOKEN_MAT_BLEND])
  243. {
  244. //if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_BLEND], VAL_STRING))
  245. if (!prop->values.empty())
  246. {
  247. std::string val;
  248. if(getString(*prop->values.front(), &val))
  249. {
  250. if (val == matToken[TOKEN_MAT_BLEND_ADD])
  251. {
  252. material->blendFunc.src = GL_ONE;
  253. material->blendFunc.dst= GL_ONE;
  254. }
  255. else if (val == matToken[TOKEN_MAT_BLEND_ALPHA])
  256. {
  257. material->blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  258. }
  259. else if (val == matToken[TOKEN_MAT_BLEND_COLOR])
  260. {
  261. material->blendFunc.src = GL_SRC_COLOR;
  262. material->blendFunc.dst= GL_ONE_MINUS_SRC_COLOR;
  263. }
  264. else if (val == matToken[TOKEN_MAT_BLEND_MODULATE])
  265. {
  266. material->blendFunc.src = GL_DST_COLOR;
  267. material->blendFunc.dst= GL_ZERO;
  268. }
  269. else if (val == matToken[TOKEN_MAT_BLEND_REPLACE])
  270. {
  271. material->blendFunc.src = GL_ONE;
  272. material->blendFunc.dst= GL_ZERO;
  273. }
  274. else if (val == matToken[TOKEN_MAT_BLEND_SRC_COLOR])
  275. {
  276. material->blendFunc.src = GL_SRC_COLOR;
  277. }
  278. else if (val == matToken[TOKEN_MAT_BLEND_DEST_COLOR])
  279. {
  280. material->blendFunc.src = GL_DST_COLOR;
  281. }
  282. }
  283. if (prop->values.size() == 2)
  284. {
  285. if (getString(*prop->values.back(), &val))
  286. {
  287. if (val == matToken[TOKEN_MAT_BLEND_ONE])
  288. {
  289. material->blendFunc.dst = GL_ONE;
  290. }
  291. else if (val == matToken[TOKEN_MAT_BLEND_ZERO])
  292. {
  293. material->blendFunc.dst = GL_ZERO;
  294. }
  295. else if (val == matToken[TOKEN_MAT_BLEND_SRC_COLOR])
  296. {
  297. material->blendFunc.dst = GL_SRC_COLOR;
  298. }
  299. else if (val == matToken[TOKEN_MAT_BLEND_DEST_COLOR])
  300. {
  301. material->blendFunc.dst = GL_DST_COLOR;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. else if (prop->name == matToken[TOKEN_MAT_DEPTH_CHECK])
  308. {
  309. if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_DEPTH_CHECK], VAL_STRING))
  310. {
  311. std::string val;
  312. if(getString(*prop->values.front(), &val))
  313. {
  314. if (val == matToken[TOKEN_MAT_ON])
  315. {
  316. material->depthTest = true;
  317. }
  318. else if (val == matToken[TOKEN_MAT_OFF])
  319. {
  320. material->depthTest = false;
  321. }
  322. }
  323. }
  324. }
  325. else if (prop->name == matToken[TOKEN_MAT_DEPTH_WRITE])
  326. {
  327. if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_DEPTH_WRITE], VAL_STRING))
  328. {
  329. std::string val;
  330. if(getString(*prop->values.front(), &val))
  331. {
  332. if (val == matToken[TOKEN_MAT_ON])
  333. {
  334. material->depthWrite = true;
  335. }
  336. else if (val == matToken[TOKEN_MAT_OFF])
  337. {
  338. material->depthWrite = false;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. else if ((*i)->type == ANT_OBJECT)
  345. {
  346. PUObjectAbstractNode* child = reinterpret_cast<PUObjectAbstractNode*>(*i);
  347. if(child->cls == matToken[TOKEN_MAT_TEXTURE_UNIT])
  348. {
  349. PUMaterialTextureUnitTranslator materialTextureUnitTranslator;
  350. materialTextureUnitTranslator.translate(compiler, *i);
  351. }
  352. }
  353. }
  354. }
  355. void PUMaterialTextureUnitTranslator::translate( PUScriptCompiler* compiler, PUAbstractNode *node )
  356. {
  357. PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
  358. PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
  359. PUMaterial *material = static_cast<PUMaterial *>(parent->context);
  360. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  361. {
  362. if((*i)->type == ANT_PROPERTY)
  363. {
  364. PUPropertyAbstractNode *prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
  365. if (prop->name == matToken[TOKEN_MAT_TEXTURE_FILE])
  366. {
  367. //if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_TEXTURE_FILE], VAL_STRING))
  368. if (!prop->values.empty())
  369. {
  370. std::string val;
  371. if(getString(*prop->values.front(), &val))
  372. {
  373. material->textureFile = val;
  374. }
  375. }
  376. }
  377. else if (prop->name == matToken[TOKEN_MAT_TEXTURE_WRAP])
  378. {
  379. if (passValidateProperty(compiler, prop, matToken[TOKEN_MAT_TEXTURE_WRAP], VAL_STRING))
  380. {
  381. std::string val;
  382. if(getString(*prop->values.front(), &val))
  383. {
  384. if (val == matToken[TOKEN_MAT_TEXTURE_CLAMP])
  385. {
  386. material->wrapMode = GL_CLAMP_TO_EDGE;
  387. }
  388. else if (val == matToken[TOKEN_MAT_TEXTURE_REPEAT])
  389. {
  390. material->wrapMode = GL_REPEAT;
  391. }
  392. else if (val == matToken[TOKEN_MAT_TEXTURE_MIRROR])
  393. {
  394. material->wrapMode = GL_MIRRORED_REPEAT;//GL_MIRROR_CLAMP_EXT;
  395. }
  396. }
  397. }
  398. }
  399. }
  400. }
  401. }
  402. NS_CC_END