customMaterialDefinition.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 i = 0;
  109. for (SimFieldDictionaryIterator itr(getFieldDictionary()); *itr; ++itr)
  110. {
  111. SimFieldDictionary::Entry* entry = *itr;
  112. if (dStrStartsWith(entry->slotName, samplerDecl))
  113. {
  114. if (i >= MAX_TEX_PER_PASS)
  115. {
  116. logError("Too many sampler declarations, you may only have %i", MAX_TEX_PER_PASS);
  117. return false;
  118. }
  119. if (dStrlen(entry->slotName) == dStrlen(samplerDecl))
  120. {
  121. logError("sampler declarations must have a sampler name, e.g. sampler[\"diffuseMap\"]");
  122. return false;
  123. }
  124. mSamplerNames[i] = entry->slotName + dStrlen(samplerDecl);
  125. mSamplerNames[i].insert(0, '$');
  126. mTexFilename[i] = entry->value;
  127. ++i;
  128. }
  129. }
  130. return true;
  131. }
  132. //--------------------------------------------------------------------------
  133. // On remove
  134. //--------------------------------------------------------------------------
  135. void CustomMaterial::onRemove()
  136. {
  137. Parent::onRemove();
  138. }
  139. //--------------------------------------------------------------------------
  140. // Map this material to the texture specified in the "mapTo" data variable
  141. //--------------------------------------------------------------------------
  142. void CustomMaterial::_mapMaterial()
  143. {
  144. if( String(getName()).isEmpty() )
  145. {
  146. Con::warnf( "Unnamed Material! Could not map to: %s", mMapTo.c_str() );
  147. return;
  148. }
  149. if( mMapTo.isEmpty() )
  150. return;
  151. MATMGR->mapMaterial(mMapTo, getName());
  152. }
  153. const GFXStateBlockData* CustomMaterial::getStateBlockData() const
  154. {
  155. return mStateBlockData;
  156. }