customMaterialDefinition.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "materials/customMaterialDefinition.h"
  24. #include "materials/materialManager.h"
  25. #include "console/consoleTypes.h"
  26. #include "materials/shaderData.h"
  27. #include "gfx/sim/cubemapData.h"
  28. #include "gfx/gfxCubemap.h"
  29. #include "gfx/sim/gfxStateBlockData.h"
  30. //****************************************************************************
  31. // Custom Material
  32. //****************************************************************************
  33. IMPLEMENT_CONOBJECT(CustomMaterial);
  34. ConsoleDocClass( CustomMaterial,
  35. "@brief Material object which provides more control over surface properties.\n\n"
  36. "CustomMaterials allow the user to specify their own shaders via the ShaderData datablock. "
  37. "Because CustomMaterials are derived from Materials, they can hold a lot of the same properties. "
  38. "It is up to the user to code how these properties are used.\n\n"
  39. "@tsexample\n"
  40. "singleton CustomMaterial( WaterBasicMat )\n"
  41. "{\n"
  42. " sampler[\"reflectMap\"] = \"$reflectbuff\";\n"
  43. " sampler[\"refractBuff\"] = \"$backbuff\";\n\n"
  44. " cubemap = NewLevelSkyCubemap;\n"
  45. " shader = WaterBasicShader;\n"
  46. " stateBlock = WaterBasicStateBlock;\n"
  47. " version = 2.0;\n"
  48. "};\n"
  49. "@endtsexample\n\n"
  50. "@see Material, GFXStateBlockData, ShaderData\n\n"
  51. "@ingroup Materials\n"
  52. );
  53. //----------------------------------------------------------------------------
  54. // Constructor
  55. //----------------------------------------------------------------------------
  56. CustomMaterial::CustomMaterial()
  57. {
  58. mFallback = NULL;
  59. mMaxTex = 0;
  60. mVersion = 1.1f;
  61. mTranslucent = false;
  62. dMemset( mFlags, 0, sizeof( mFlags ) );
  63. mShaderData = NULL;
  64. mRefract = false;
  65. mStateBlockData = NULL;
  66. mForwardLit = false;
  67. }
  68. //--------------------------------------------------------------------------
  69. // Init fields
  70. //--------------------------------------------------------------------------
  71. void CustomMaterial::initPersistFields()
  72. {
  73. addField("version", TypeF32, Offset(mVersion, CustomMaterial),
  74. "@brief Specifies pixel shader version for hardware.\n\n"
  75. "Valid pixel shader versions include 2.0, 3.0, etc. "
  76. "@note All features aren't compatible with all pixel shader versions.");
  77. addField("fallback", TYPEID< Material >(), Offset(mFallback, CustomMaterial),
  78. "@brief Alternate material for targeting lower end hardware.\n\n"
  79. "If the CustomMaterial requires a higher pixel shader version than the one "
  80. "it's using, it's fallback Material will be processed instead. "
  81. "If the fallback material wasn't defined, Torque 3D will assert and attempt to use a very "
  82. "basic material in it's place.\n\n");
  83. addField("shader", TypeRealString, Offset(mShaderDataName, CustomMaterial),
  84. "@brief Name of the ShaderData to use for this effect.\n\n");
  85. addField("stateBlock", TYPEID< GFXStateBlockData >(), Offset(mStateBlockData, CustomMaterial),
  86. "@brief Name of a GFXStateBlockData for this effect.\n\n");
  87. addField("target", TypeRealString, Offset(mOutputTarget, CustomMaterial),
  88. "@brief String identifier of this material's target texture.");
  89. addField("forwardLit", TypeBool, Offset(mForwardLit, CustomMaterial),
  90. "@brief Determines if the material should recieve lights in Basic Lighting. "
  91. "Has no effect in Advanced Lighting.\n\n");
  92. Parent::initPersistFields();
  93. }
  94. //--------------------------------------------------------------------------
  95. // On add - verify data settings
  96. //--------------------------------------------------------------------------
  97. bool CustomMaterial::onAdd()
  98. {
  99. if (Parent::onAdd() == false)
  100. return false;
  101. mShaderData = dynamic_cast<ShaderData*>(Sim::findObject( mShaderDataName ) );
  102. if(mShaderDataName.isNotEmpty() && mShaderData == NULL)
  103. {
  104. logError("Failed to find ShaderData %s", mShaderDataName.c_str());
  105. return false;
  106. }
  107. const char* samplerDecl = "sampler";
  108. S32 samplerDeclLen = dStrlen(samplerDecl);
  109. S32 i = 0;
  110. for (SimFieldDictionaryIterator itr(getFieldDictionary()); *itr; ++itr)
  111. {
  112. SimFieldDictionary::Entry* entry = *itr;
  113. if (dStrStartsWith(entry->slotName, samplerDecl))
  114. {
  115. if (i >= MAX_TEX_PER_PASS)
  116. {
  117. logError("Too many sampler declarations, you may only have %i", MAX_TEX_PER_PASS);
  118. return false;
  119. }
  120. if (dStrlen(entry->slotName) == samplerDeclLen)
  121. {
  122. logError("sampler declarations must have a sampler name, e.g. sampler[\"diffuseMap\"]");
  123. return false;
  124. }
  125. // Assert sampler names are defined on ShaderData
  126. S32 pos = -1;
  127. String samplerName = entry->slotName + samplerDeclLen;
  128. samplerName.insert(0, '$');
  129. mShaderData->hasSamplerDef(samplerName, pos);
  130. if(pos == -1)
  131. {
  132. const char *error = (avar("CustomMaterial(%s) bind sampler[%s] and is not present on ShaderData(%s)",
  133. getName(), samplerName.c_str(), mShaderDataName.c_str() ));
  134. Con::errorf(error);
  135. pos = i;
  136. #ifdef TORQUE_OPENGL
  137. GFXAssertFatal(0, error);
  138. continue;
  139. #endif
  140. }
  141. mSamplerNames[pos] = samplerName;
  142. mTexFilename[pos] = entry->value;
  143. ++i;
  144. }
  145. }
  146. return true;
  147. }
  148. //--------------------------------------------------------------------------
  149. // On remove
  150. //--------------------------------------------------------------------------
  151. void CustomMaterial::onRemove()
  152. {
  153. Parent::onRemove();
  154. }
  155. //--------------------------------------------------------------------------
  156. // Map this material to the texture specified in the "mapTo" data variable
  157. //--------------------------------------------------------------------------
  158. void CustomMaterial::_mapMaterial()
  159. {
  160. if( String(getName()).isEmpty() )
  161. {
  162. Con::warnf( "Unnamed Material! Could not map to: %s", mMapTo.c_str() );
  163. return;
  164. }
  165. if( mMapTo.isEmpty() )
  166. return;
  167. MATMGR->mapMaterial(mMapTo, getName());
  168. }
  169. const GFXStateBlockData* CustomMaterial::getStateBlockData() const
  170. {
  171. return mStateBlockData;
  172. }