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