MaterialParameter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. #include "Base.h"
  2. #include "MaterialParameter.h"
  3. #include "Node.h"
  4. namespace gameplay
  5. {
  6. MaterialParameter::MaterialParameter(const char* name) :
  7. _type(MaterialParameter::NONE), _count(1), _dynamic(false), _name(name ? name : ""), _uniform(NULL), _loggerDirtyBits(0)
  8. {
  9. clearValue();
  10. }
  11. MaterialParameter::~MaterialParameter()
  12. {
  13. clearValue();
  14. }
  15. void MaterialParameter::clearValue()
  16. {
  17. // Release parameters
  18. switch (_type)
  19. {
  20. case MaterialParameter::SAMPLER:
  21. if (_value.samplerValue)
  22. const_cast<Texture::Sampler*>(_value.samplerValue)->release();
  23. break;
  24. case MaterialParameter::SAMPLER_ARRAY:
  25. if (_value.samplerArrayValue)
  26. {
  27. for (unsigned int i = 0; i < _count; ++i)
  28. {
  29. const_cast<Texture::Sampler*>(_value.samplerArrayValue[i])->release();
  30. }
  31. }
  32. break;
  33. default:
  34. // Ignore all other cases.
  35. break;
  36. }
  37. // Free dynamic data
  38. if (_dynamic)
  39. {
  40. switch (_type)
  41. {
  42. case MaterialParameter::FLOAT:
  43. case MaterialParameter::FLOAT_ARRAY:
  44. case MaterialParameter::VECTOR2:
  45. case MaterialParameter::VECTOR3:
  46. case MaterialParameter::VECTOR4:
  47. case MaterialParameter::MATRIX:
  48. SAFE_DELETE_ARRAY(_value.floatPtrValue);
  49. break;
  50. case MaterialParameter::INT:
  51. case MaterialParameter::INT_ARRAY:
  52. SAFE_DELETE_ARRAY(_value.intPtrValue);
  53. break;
  54. case MaterialParameter::METHOD:
  55. SAFE_RELEASE(_value.method);
  56. break;
  57. case MaterialParameter::SAMPLER_ARRAY:
  58. SAFE_DELETE_ARRAY(_value.samplerArrayValue);
  59. break;
  60. default:
  61. // Ignore all other cases.
  62. break;
  63. }
  64. _dynamic = false;
  65. _count = 1;
  66. }
  67. memset(&_value, 0, sizeof(_value));
  68. _type = MaterialParameter::NONE;
  69. }
  70. const char* MaterialParameter::getName() const
  71. {
  72. return _name.c_str();
  73. }
  74. Texture::Sampler* MaterialParameter::getSampler(unsigned int index) const
  75. {
  76. if (_type == MaterialParameter::SAMPLER)
  77. return const_cast<Texture::Sampler*>(_value.samplerValue);
  78. if (_type == MaterialParameter::SAMPLER_ARRAY && index < _count)
  79. return const_cast<Texture::Sampler*>(_value.samplerArrayValue[index]);
  80. return NULL;
  81. }
  82. void MaterialParameter::setValue(float value)
  83. {
  84. clearValue();
  85. _value.floatValue = value;
  86. _type = MaterialParameter::FLOAT;
  87. }
  88. void MaterialParameter::setValue(int value)
  89. {
  90. clearValue();
  91. _value.intValue = value;
  92. _type = MaterialParameter::INT;
  93. }
  94. void MaterialParameter::setValue(const float* values, unsigned int count)
  95. {
  96. clearValue();
  97. _value.floatPtrValue = const_cast<float*> (values);
  98. _count = count;
  99. _type = MaterialParameter::FLOAT_ARRAY;
  100. }
  101. void MaterialParameter::setValue(const int* values, unsigned int count)
  102. {
  103. clearValue();
  104. _value.intPtrValue = const_cast<int*> (values);
  105. _count = count;
  106. _type = MaterialParameter::INT_ARRAY;
  107. }
  108. void MaterialParameter::setValue(const Vector2& value)
  109. {
  110. clearValue();
  111. // Copy data by-value into a dynamic array.
  112. float* array = new float[2];
  113. memcpy(array, &value.x, sizeof(float) * 2);
  114. _value.floatPtrValue = array;
  115. _dynamic = true;
  116. _count = 1;
  117. _type = MaterialParameter::VECTOR2;
  118. }
  119. void MaterialParameter::setValue(const Vector2* values, unsigned int count)
  120. {
  121. GP_ASSERT(values);
  122. clearValue();
  123. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  124. _count = count;
  125. _type = MaterialParameter::VECTOR2;
  126. }
  127. void MaterialParameter::setValue(const Vector3& value)
  128. {
  129. clearValue();
  130. // Copy data by-value into a dynamic array.
  131. float* array = new float[3];
  132. memcpy(array, &value.x, sizeof(float) * 3);
  133. _value.floatPtrValue = array;
  134. _dynamic = true;
  135. _count = 1;
  136. _type = MaterialParameter::VECTOR3;
  137. }
  138. void MaterialParameter::setValue(const Vector3* values, unsigned int count)
  139. {
  140. GP_ASSERT(values);
  141. clearValue();
  142. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  143. _count = count;
  144. _type = MaterialParameter::VECTOR3;
  145. }
  146. void MaterialParameter::setValue(const Vector4& value)
  147. {
  148. clearValue();
  149. // Copy data by-value into a dynamic array.
  150. float* array = new float[4];
  151. memcpy(array, &value.x, sizeof(float) * 4);
  152. _value.floatPtrValue = array;
  153. _dynamic = true;
  154. _count = 1;
  155. _type = MaterialParameter::VECTOR4;
  156. }
  157. void MaterialParameter::setValue(const Vector4* values, unsigned int count)
  158. {
  159. GP_ASSERT(values);
  160. clearValue();
  161. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  162. _count = count;
  163. _type = MaterialParameter::VECTOR4;
  164. }
  165. void MaterialParameter::setValue(const Matrix& value)
  166. {
  167. // If this parameter is already storing a single dynamic matrix, no need to clear it.
  168. if (!(_dynamic && _count == 1 && _type == MaterialParameter::MATRIX && _value.floatPtrValue != NULL))
  169. {
  170. clearValue();
  171. // Allocate a new dynamic matrix.
  172. _value.floatPtrValue = new float[16];
  173. }
  174. memcpy(_value.floatPtrValue, value.m, sizeof(float) * 16);
  175. _dynamic = true;
  176. _count = 1;
  177. _type = MaterialParameter::MATRIX;
  178. }
  179. void MaterialParameter::setValue(const Matrix* values, unsigned int count)
  180. {
  181. GP_ASSERT(values);
  182. clearValue();
  183. _value.floatPtrValue = const_cast<Matrix&> (values[0]).m;
  184. _count = count;
  185. _type = MaterialParameter::MATRIX;
  186. }
  187. void MaterialParameter::setValue(const Texture::Sampler* sampler)
  188. {
  189. GP_ASSERT(sampler);
  190. clearValue();
  191. const_cast<Texture::Sampler*>(sampler)->addRef();
  192. _value.samplerValue = sampler;
  193. _type = MaterialParameter::SAMPLER;
  194. }
  195. void MaterialParameter::setValue(const Texture::Sampler** samplers, unsigned int count)
  196. {
  197. GP_ASSERT(samplers);
  198. clearValue();
  199. for (unsigned int i = 0; i < count; ++i)
  200. {
  201. const_cast<Texture::Sampler*>(samplers[i])->addRef();
  202. }
  203. _value.samplerArrayValue = samplers;
  204. _count = count;
  205. _type = MaterialParameter::SAMPLER_ARRAY;
  206. }
  207. Texture::Sampler* MaterialParameter::setValue(const char* texturePath, bool generateMipmaps)
  208. {
  209. GP_ASSERT(texturePath);
  210. clearValue();
  211. Texture::Sampler* sampler = Texture::Sampler::create(texturePath, generateMipmaps);
  212. if (sampler)
  213. {
  214. _value.samplerValue = sampler;
  215. _type = MaterialParameter::SAMPLER;
  216. }
  217. return sampler;
  218. }
  219. void MaterialParameter::setFloat(float value)
  220. {
  221. setValue(value);
  222. }
  223. void MaterialParameter::setFloatArray(const float* values, unsigned int count, bool copy)
  224. {
  225. GP_ASSERT(values);
  226. clearValue();
  227. if (copy)
  228. {
  229. _value.floatPtrValue = new float[count];
  230. memcpy(_value.floatPtrValue, values, sizeof(float) * count);
  231. _dynamic = true;
  232. }
  233. else
  234. {
  235. _value.floatPtrValue = const_cast<float*> (values);
  236. }
  237. _count = count;
  238. _type = MaterialParameter::FLOAT_ARRAY;
  239. }
  240. void MaterialParameter::setInt(int value)
  241. {
  242. setValue(value);
  243. }
  244. void MaterialParameter::setIntArray(const int* values, unsigned int count, bool copy)
  245. {
  246. GP_ASSERT(values);
  247. clearValue();
  248. if (copy)
  249. {
  250. _value.intPtrValue = new int[count];
  251. memcpy(_value.intPtrValue, values, sizeof(int) * count);
  252. _dynamic = true;
  253. }
  254. else
  255. {
  256. _value.intPtrValue = const_cast<int*> (values);
  257. }
  258. _count = count;
  259. _type = MaterialParameter::INT_ARRAY;
  260. }
  261. void MaterialParameter::setVector2(const Vector2& value)
  262. {
  263. setValue(value);
  264. }
  265. void MaterialParameter::setVector2Array(const Vector2* values, unsigned int count, bool copy)
  266. {
  267. GP_ASSERT(values);
  268. clearValue();
  269. if (copy)
  270. {
  271. _value.floatPtrValue = new float[2 * count];
  272. memcpy(_value.floatPtrValue, const_cast<float*> (&values[0].x), sizeof(float) * 2 * count);
  273. _dynamic = true;
  274. }
  275. else
  276. {
  277. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  278. }
  279. _count = count;
  280. _type = MaterialParameter::VECTOR2;
  281. }
  282. void MaterialParameter::setVector3(const Vector3& value)
  283. {
  284. setValue(value);
  285. }
  286. void MaterialParameter::setVector3Array(const Vector3* values, unsigned int count, bool copy)
  287. {
  288. GP_ASSERT(values);
  289. clearValue();
  290. if (copy)
  291. {
  292. _value.floatPtrValue = new float[3 * count];
  293. memcpy(_value.floatPtrValue, const_cast<float*> (&values[0].x), sizeof(float) * 3 * count);
  294. _dynamic = true;
  295. }
  296. else
  297. {
  298. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  299. }
  300. _count = count;
  301. _type = MaterialParameter::VECTOR3;
  302. }
  303. void MaterialParameter::setVector4(const Vector4& value)
  304. {
  305. setValue(value);
  306. }
  307. void MaterialParameter::setVector4Array(const Vector4* values, unsigned int count, bool copy)
  308. {
  309. GP_ASSERT(values);
  310. clearValue();
  311. if (copy)
  312. {
  313. _value.floatPtrValue = new float[4 * count];
  314. memcpy(_value.floatPtrValue, const_cast<float*> (&values[0].x), sizeof(float) * 4 * count);
  315. _dynamic = true;
  316. }
  317. else
  318. {
  319. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  320. }
  321. _count = count;
  322. _type = MaterialParameter::VECTOR4;
  323. }
  324. void MaterialParameter::setMatrix(const Matrix& value)
  325. {
  326. setValue(value);
  327. }
  328. void MaterialParameter::setMatrixArray(const Matrix* values, unsigned int count, bool copy)
  329. {
  330. GP_ASSERT(values);
  331. clearValue();
  332. if (copy)
  333. {
  334. _value.floatPtrValue = new float[16 * count];
  335. memcpy(_value.floatPtrValue, const_cast<Matrix&> (values[0]).m, sizeof(float) * 16 * count);
  336. _dynamic = true;
  337. }
  338. else
  339. {
  340. _value.floatPtrValue = const_cast<Matrix&> (values[0]).m;
  341. }
  342. _count = count;
  343. _type = MaterialParameter::MATRIX;
  344. }
  345. Texture::Sampler* MaterialParameter::setSampler(const char* texturePath, bool generateMipmaps)
  346. {
  347. return setValue(texturePath, generateMipmaps);
  348. }
  349. void MaterialParameter::setSampler(const Texture::Sampler* value)
  350. {
  351. setValue(value);
  352. }
  353. void MaterialParameter::setSamplerArray(const Texture::Sampler** values, unsigned int count, bool copy)
  354. {
  355. GP_ASSERT(values);
  356. clearValue();
  357. if (copy)
  358. {
  359. _value.samplerArrayValue = new const Texture::Sampler*[count];
  360. memcpy(_value.samplerArrayValue, values, sizeof(Texture::Sampler*) * count);
  361. _dynamic = true;
  362. }
  363. else
  364. {
  365. _value.samplerArrayValue = values;
  366. }
  367. for (unsigned int i = 0; i < count; ++i)
  368. {
  369. const_cast<Texture::Sampler*>(_value.samplerArrayValue[i])->addRef();
  370. }
  371. _count = count;
  372. _type = MaterialParameter::SAMPLER_ARRAY;
  373. }
  374. void MaterialParameter::bind(Effect* effect)
  375. {
  376. GP_ASSERT(effect);
  377. // If we had a Uniform cached that is not from the passed in effect,
  378. // we need to update our uniform to point to the new effect's uniform.
  379. if (!_uniform || _uniform->getEffect() != effect)
  380. {
  381. _uniform = effect->getUniform(_name.c_str());
  382. if (!_uniform)
  383. {
  384. if ((_loggerDirtyBits & UNIFORM_NOT_FOUND) == 0)
  385. {
  386. // This parameter was not found in the specified effect, so do nothing.
  387. GP_WARN("Material parameter for uniform '%s' not found in effect: '%s'.", _name.c_str(), effect->getId());
  388. _loggerDirtyBits |= UNIFORM_NOT_FOUND;
  389. }
  390. return;
  391. }
  392. }
  393. switch (_type)
  394. {
  395. case MaterialParameter::FLOAT:
  396. effect->setValue(_uniform, _value.floatValue);
  397. break;
  398. case MaterialParameter::FLOAT_ARRAY:
  399. effect->setValue(_uniform, _value.floatPtrValue, _count);
  400. break;
  401. case MaterialParameter::INT:
  402. effect->setValue(_uniform, _value.intValue);
  403. break;
  404. case MaterialParameter::INT_ARRAY:
  405. effect->setValue(_uniform, _value.intPtrValue, _count);
  406. break;
  407. case MaterialParameter::VECTOR2:
  408. effect->setValue(_uniform, reinterpret_cast<Vector2*>(_value.floatPtrValue), _count);
  409. break;
  410. case MaterialParameter::VECTOR3:
  411. effect->setValue(_uniform, reinterpret_cast<Vector3*>(_value.floatPtrValue), _count);
  412. break;
  413. case MaterialParameter::VECTOR4:
  414. effect->setValue(_uniform, reinterpret_cast<Vector4*>(_value.floatPtrValue), _count);
  415. break;
  416. case MaterialParameter::MATRIX:
  417. effect->setValue(_uniform, reinterpret_cast<Matrix*>(_value.floatPtrValue), _count);
  418. break;
  419. case MaterialParameter::SAMPLER:
  420. effect->setValue(_uniform, _value.samplerValue);
  421. break;
  422. case MaterialParameter::SAMPLER_ARRAY:
  423. effect->setValue(_uniform, _value.samplerArrayValue, _count);
  424. break;
  425. case MaterialParameter::METHOD:
  426. if (_value.method)
  427. _value.method->setValue(effect);
  428. break;
  429. default:
  430. {
  431. if ((_loggerDirtyBits & PARAMETER_VALUE_NOT_SET) == 0)
  432. {
  433. GP_WARN("Material parameter value not set for: '%s' in effect: '%s'.", _name.c_str(), effect->getId());
  434. _loggerDirtyBits |= PARAMETER_VALUE_NOT_SET;
  435. }
  436. break;
  437. }
  438. }
  439. }
  440. void MaterialParameter::bindValue(Node* node, const char* binding)
  441. {
  442. GP_ASSERT(binding);
  443. if (strcmp(binding, "&Node::getBackVector") == 0)
  444. {
  445. bindValue<Node, Vector3>(node, &Node::getBackVector);
  446. }
  447. else if (strcmp(binding, "&Node::getDownVector") == 0)
  448. {
  449. bindValue<Node, Vector3>(node, &Node::getDownVector);
  450. }
  451. else if (strcmp(binding, "&Node::getTranslationWorld") == 0)
  452. {
  453. bindValue<Node, Vector3>(node, &Node::getTranslationWorld);
  454. }
  455. else if (strcmp(binding, "&Node::getTranslationView") == 0)
  456. {
  457. bindValue<Node, Vector3>(node, &Node::getTranslationView);
  458. }
  459. else if (strcmp(binding, "&Node::getForwardVector") == 0)
  460. {
  461. bindValue<Node, Vector3>(node, &Node::getForwardVector);
  462. }
  463. else if (strcmp(binding, "&Node::getForwardVectorWorld") == 0)
  464. {
  465. bindValue<Node, Vector3>(node, &Node::getForwardVectorWorld);
  466. }
  467. else if (strcmp(binding, "&Node::getForwardVectorView") == 0)
  468. {
  469. bindValue<Node, Vector3>(node, &Node::getForwardVectorView);
  470. }
  471. else if (strcmp(binding, "&Node::getLeftVector") == 0)
  472. {
  473. bindValue<Node, Vector3>(node, &Node::getLeftVector);
  474. }
  475. else if (strcmp(binding, "&Node::getRightVector") == 0)
  476. {
  477. bindValue<Node, Vector3>(node, &Node::getRightVector);
  478. }
  479. else if (strcmp(binding, "&Node::getRightVectorWorld") == 0)
  480. {
  481. bindValue<Node, Vector3>(node, &Node::getRightVectorWorld);
  482. }
  483. else if (strcmp(binding, "&Node::getUpVector") == 0)
  484. {
  485. bindValue<Node, Vector3>(node, &Node::getUpVector);
  486. }
  487. else if (strcmp(binding, "&Node::getUpVectorWorld") == 0)
  488. {
  489. bindValue<Node, Vector3>(node, &Node::getUpVectorWorld);
  490. }
  491. else if (strcmp(binding, "&Node::getActiveCameraTranslationWorld") == 0)
  492. {
  493. bindValue<Node, Vector3>(node, &Node::getActiveCameraTranslationWorld);
  494. }
  495. else if (strcmp(binding, "&Node::getActiveCameraTranslationView") == 0)
  496. {
  497. bindValue<Node, Vector3>(node, &Node::getActiveCameraTranslationView);
  498. }
  499. else if (strcmp(binding, "&Node::getScaleX") == 0)
  500. {
  501. bindValue<Node, float>(node, &Node::getScaleX);
  502. }
  503. else if (strcmp(binding, "&Node::getScaleY") == 0)
  504. {
  505. bindValue<Node, float>(node, &Node::getScaleY);
  506. }
  507. else if (strcmp(binding, "&Node::getScaleZ") == 0)
  508. {
  509. bindValue<Node, float>(node, &Node::getScaleZ);
  510. }
  511. else if (strcmp(binding, "&Node::getTranslationX") == 0)
  512. {
  513. bindValue<Node, float>(node, &Node::getTranslationX);
  514. }
  515. else if (strcmp(binding, "&Node::getTranslationY") == 0)
  516. {
  517. bindValue<Node, float>(node, &Node::getTranslationY);
  518. }
  519. else if (strcmp(binding, "&Node::getTranslationZ") == 0)
  520. {
  521. bindValue<Node, float>(node, &Node::getTranslationZ);
  522. }
  523. else
  524. {
  525. GP_WARN("Unsupported material parameter binding '%s'.", binding);
  526. }
  527. }
  528. unsigned int MaterialParameter::getAnimationPropertyComponentCount(int propertyId) const
  529. {
  530. switch (propertyId)
  531. {
  532. case ANIMATE_UNIFORM:
  533. {
  534. switch (_type)
  535. {
  536. // These types don't support animation.
  537. case NONE:
  538. case MATRIX:
  539. case SAMPLER:
  540. case SAMPLER_ARRAY:
  541. case METHOD:
  542. return 0;
  543. case FLOAT:
  544. case FLOAT_ARRAY:
  545. case INT:
  546. case INT_ARRAY:
  547. return _count;
  548. case VECTOR2:
  549. return 2 * _count;
  550. case VECTOR3:
  551. return 3 * _count;
  552. case VECTOR4:
  553. return 4 * _count;
  554. default:
  555. return 0;
  556. }
  557. }
  558. break;
  559. }
  560. return 0;
  561. }
  562. void MaterialParameter::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  563. {
  564. GP_ASSERT(value);
  565. switch (propertyId)
  566. {
  567. case ANIMATE_UNIFORM:
  568. {
  569. switch (_type)
  570. {
  571. case FLOAT:
  572. value->setFloat(0, _value.floatValue);
  573. break;
  574. case FLOAT_ARRAY:
  575. GP_ASSERT(_value.floatPtrValue);
  576. for (unsigned int i = 0; i < _count; i++)
  577. {
  578. value->setFloat(i, _value.floatPtrValue[i]);
  579. }
  580. break;
  581. case INT:
  582. value->setFloat(0, _value.intValue);
  583. break;
  584. case INT_ARRAY:
  585. GP_ASSERT(_value.intPtrValue);
  586. for (unsigned int i = 0; i < _count; i++)
  587. {
  588. value->setFloat(i, _value.intPtrValue[i]);
  589. }
  590. break;
  591. case VECTOR2:
  592. value->setFloats(0, _value.floatPtrValue, _count * 2);
  593. break;
  594. case VECTOR3:
  595. value->setFloats(0, _value.floatPtrValue, _count * 3);
  596. break;
  597. case VECTOR4:
  598. value->setFloats(0, _value.floatPtrValue, _count * 4);
  599. break;
  600. case NONE:
  601. case MATRIX:
  602. case METHOD:
  603. case SAMPLER:
  604. case SAMPLER_ARRAY:
  605. // Unsupported material parameter types for animation.
  606. break;
  607. default:
  608. break;
  609. }
  610. }
  611. break;
  612. }
  613. }
  614. void MaterialParameter::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  615. {
  616. GP_ASSERT(value);
  617. GP_ASSERT(blendWeight >= 0.0f && blendWeight <= 1.0f);
  618. switch (propertyId)
  619. {
  620. case ANIMATE_UNIFORM:
  621. {
  622. switch (_type)
  623. {
  624. case FLOAT:
  625. _value.floatValue = Curve::lerp(blendWeight, _value.floatValue, value->getFloat(0));
  626. break;
  627. case FLOAT_ARRAY:
  628. applyAnimationValue(value, blendWeight, 1);
  629. break;
  630. case INT:
  631. _value.intValue = Curve::lerp(blendWeight, _value.intValue, value->getFloat(0));
  632. break;
  633. case INT_ARRAY:
  634. GP_ASSERT(_value.intPtrValue);
  635. for (unsigned int i = 0; i < _count; i++)
  636. _value.intPtrValue[i] = Curve::lerp(blendWeight, _value.intPtrValue[i], value->getFloat(i));
  637. break;
  638. case VECTOR2:
  639. applyAnimationValue(value, blendWeight, 2);
  640. break;
  641. case VECTOR3:
  642. applyAnimationValue(value, blendWeight, 3);
  643. break;
  644. case VECTOR4:
  645. applyAnimationValue(value, blendWeight, 4);
  646. break;
  647. case NONE:
  648. case MATRIX:
  649. case METHOD:
  650. case SAMPLER:
  651. case SAMPLER_ARRAY:
  652. // Unsupported material parameter types for animation.
  653. break;
  654. default:
  655. break;
  656. }
  657. }
  658. break;
  659. }
  660. }
  661. void MaterialParameter::applyAnimationValue(AnimationValue* value, float blendWeight, int components)
  662. {
  663. GP_ASSERT(value);
  664. GP_ASSERT(_value.floatPtrValue);
  665. unsigned int count = _count * components;
  666. for (unsigned int i = 0; i < count; i++)
  667. _value.floatPtrValue[i] = Curve::lerp(blendWeight, _value.floatPtrValue[i], value->getFloat(i));
  668. }
  669. void MaterialParameter::cloneInto(MaterialParameter* materialParameter) const
  670. {
  671. GP_ASSERT(materialParameter);
  672. materialParameter->_type = _type;
  673. materialParameter->_count = _count;
  674. materialParameter->_dynamic = _dynamic;
  675. materialParameter->_uniform = _uniform;
  676. switch (_type)
  677. {
  678. case NONE:
  679. break;
  680. case FLOAT:
  681. materialParameter->setValue(_value.floatValue);
  682. break;
  683. case FLOAT_ARRAY:
  684. materialParameter->setValue(_value.floatPtrValue, _count);
  685. break;
  686. case INT:
  687. materialParameter->setValue(_value.intValue);
  688. break;
  689. case INT_ARRAY:
  690. materialParameter->setValue(_value.intPtrValue, _count);
  691. break;
  692. case VECTOR2:
  693. {
  694. Vector2* value = reinterpret_cast<Vector2*>(_value.floatPtrValue);
  695. if (_count == 1)
  696. {
  697. GP_ASSERT(value);
  698. materialParameter->setValue(*value);
  699. }
  700. else
  701. {
  702. materialParameter->setValue(value, _count);
  703. }
  704. break;
  705. }
  706. case VECTOR3:
  707. {
  708. Vector3* value = reinterpret_cast<Vector3*>(_value.floatPtrValue);
  709. if (_count == 1)
  710. {
  711. GP_ASSERT(value);
  712. materialParameter->setValue(*value);
  713. }
  714. else
  715. {
  716. materialParameter->setValue(value, _count);
  717. }
  718. break;
  719. }
  720. case VECTOR4:
  721. {
  722. Vector4* value = reinterpret_cast<Vector4*>(_value.floatPtrValue);
  723. if (_count == 1)
  724. {
  725. GP_ASSERT(value);
  726. materialParameter->setValue(*value);
  727. }
  728. else
  729. {
  730. materialParameter->setValue(value, _count);
  731. }
  732. break;
  733. }
  734. case MATRIX:
  735. {
  736. Matrix* value = reinterpret_cast<Matrix*>(_value.floatPtrValue);
  737. if (_count == 1)
  738. {
  739. GP_ASSERT(value);
  740. materialParameter->setValue(*value);
  741. }
  742. else
  743. {
  744. materialParameter->setValue(value, _count);
  745. }
  746. break;
  747. }
  748. case SAMPLER:
  749. materialParameter->setValue(_value.samplerValue);
  750. break;
  751. case SAMPLER_ARRAY:
  752. materialParameter->setValue(_value.samplerArrayValue, _count);
  753. break;
  754. case METHOD:
  755. materialParameter->_value.method = _value.method;
  756. GP_ASSERT(materialParameter->_value.method);
  757. materialParameter->_value.method->addRef();
  758. break;
  759. default:
  760. GP_ERROR("Unsupported material parameter type(%d).", _type);
  761. break;
  762. }
  763. NodeCloneContext context;
  764. this->AnimationTarget::cloneInto(materialParameter, context);
  765. }
  766. MaterialParameter::MethodBinding::MethodBinding(MaterialParameter* param) :
  767. _parameter(param), _autoBinding(false)
  768. {
  769. }
  770. }