terrFeatureHLSL.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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 "terrain/hlsl/terrFeatureHLSL.h"
  24. #include "terrain/terrFeatureTypes.h"
  25. #include "materials/materialFeatureTypes.h"
  26. #include "materials/materialFeatureData.h"
  27. #include "materials/processedMaterial.h"
  28. #include "gfx/gfxDevice.h"
  29. #include "shaderGen/langElement.h"
  30. #include "shaderGen/shaderOp.h"
  31. #include "shaderGen/featureType.h"
  32. #include "shaderGen/featureMgr.h"
  33. #include "shaderGen/shaderGen.h"
  34. #include "core/module.h"
  35. namespace
  36. {
  37. void register_hlsl_shader_features_for_terrain(GFXAdapterType type)
  38. {
  39. if (type != Direct3D11)
  40. return;
  41. FEATUREMGR->registerFeature( MFT_TerrainBaseMap, new TerrainBaseMapFeatHLSL );
  42. FEATUREMGR->registerFeature( MFT_TerrainParallaxMap, new NamedFeatureHLSL( "Terrain Parallax Texture" ) );
  43. FEATUREMGR->registerFeature( MFT_TerrainDetailMap, new TerrainDetailMapFeatHLSL );
  44. FEATUREMGR->registerFeature( MFT_TerrainNormalMap, new TerrainNormalMapFeatHLSL );
  45. FEATUREMGR->registerFeature( MFT_TerrainMacroMap, new NamedFeatureHLSL("TerrainMacroMap Deprecated")); // new TerrainMacroMapFeatHLSL);
  46. FEATUREMGR->registerFeature( MFT_TerrainLightMap, new TerrainLightMapFeatHLSL );
  47. FEATUREMGR->registerFeature( MFT_TerrainSideProject, new NamedFeatureHLSL( "Terrain Side Projection" ) );
  48. FEATUREMGR->registerFeature( MFT_TerrainHeightBlend, new TerrainHeightMapBlendHLSL );
  49. FEATUREMGR->registerFeature( MFT_TerrainORMMap, new TerrainORMMapFeatHLSL );
  50. FEATUREMGR->registerFeature( MFT_DeferredTerrainBlankInfoMap, new TerrainBlankInfoMapFeatHLSL );
  51. }
  52. };
  53. MODULE_BEGIN( TerrainFeatHLSL )
  54. MODULE_INIT_AFTER( ShaderGen )
  55. MODULE_INIT
  56. {
  57. SHADERGEN->getFeatureInitSignal().notify(&register_hlsl_shader_features_for_terrain);
  58. }
  59. MODULE_END;
  60. TerrainFeatHLSL::TerrainFeatHLSL()
  61. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" ))
  62. {
  63. addDependency( &mTorqueDep );
  64. }
  65. Var* TerrainFeatHLSL::_getUniformVar( const char *name, const char *type, ConstantSortPosition csp )
  66. {
  67. Var *theVar = (Var*)LangElement::find( name );
  68. if ( !theVar )
  69. {
  70. theVar = new Var;
  71. theVar->setType( type );
  72. theVar->setName( name );
  73. theVar->uniform = true;
  74. theVar->constSortPos = csp;
  75. }
  76. return theVar;
  77. }
  78. Var* TerrainFeatHLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList )
  79. {
  80. String name( String::ToString( "detCoord%d", getProcessIndex() ) );
  81. Var *inDet = (Var*)LangElement::find( name );
  82. if ( !inDet )
  83. {
  84. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  85. inDet = connectComp->getElement( RT_TEXCOORD );
  86. inDet->setName( name );
  87. inDet->setStructName( "IN" );
  88. inDet->setType( "float4" );
  89. }
  90. return inDet;
  91. }
  92. Var* TerrainFeatHLSL::_getInMacroCoord( Vector<ShaderComponent*> &componentList )
  93. {
  94. String name( String::ToString( "macroCoord%d", getProcessIndex() ) );
  95. Var *inDet = (Var*)LangElement::find( name );
  96. if ( !inDet )
  97. {
  98. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  99. inDet = connectComp->getElement( RT_TEXCOORD );
  100. inDet->setName( name );
  101. inDet->setStructName( "IN" );
  102. inDet->setType( "float4" );
  103. }
  104. return inDet;
  105. }
  106. Var* TerrainFeatHLSL::_getDetailMapSampler()
  107. {
  108. String name("detailMapSampler");
  109. Var* detailMapSampler = (Var*)LangElement::find(name);
  110. if(!detailMapSampler)
  111. {
  112. detailMapSampler = new Var;
  113. detailMapSampler->setName(name);
  114. detailMapSampler->setType("SamplerState");
  115. detailMapSampler->uniform = true;
  116. detailMapSampler->sampler = true;
  117. detailMapSampler->constNum = Var::getTexUnitNum();
  118. }
  119. return detailMapSampler;
  120. }
  121. Var* TerrainFeatHLSL::_getDetailMapArray()
  122. {
  123. String name("detailMapArray");
  124. Var* detailMapArray = (Var*)LangElement::find(name);
  125. if(!detailMapArray)
  126. {
  127. detailMapArray = new Var;
  128. detailMapArray->setName(name);
  129. detailMapArray->setType("Texture2DArray");
  130. detailMapArray->uniform = true;
  131. detailMapArray->texture = true;
  132. detailMapArray->constNum = _getDetailMapSampler()->constNum;
  133. }
  134. return detailMapArray;
  135. }
  136. Var* TerrainFeatHLSL::_getNormalMapSampler()
  137. {
  138. String name("normalMapSampler");
  139. Var* normalMapSampler = (Var*)LangElement::find(name);
  140. if (!normalMapSampler)
  141. {
  142. normalMapSampler = new Var;
  143. normalMapSampler->setName(name);
  144. normalMapSampler->setType("SamplerState");
  145. normalMapSampler->uniform = true;
  146. normalMapSampler->sampler = true;
  147. normalMapSampler->constNum = Var::getTexUnitNum();
  148. }
  149. return normalMapSampler;
  150. }
  151. Var* TerrainFeatHLSL::_getNormalMapArray()
  152. {
  153. String name("normalMapArray");
  154. Var* normalMapArray = (Var*)LangElement::find(name);
  155. if (!normalMapArray)
  156. {
  157. normalMapArray = new Var;
  158. normalMapArray->setName(name);
  159. normalMapArray->setType("Texture2DArray");
  160. normalMapArray->uniform = true;
  161. normalMapArray->texture = true;
  162. normalMapArray->constNum = _getNormalMapSampler()->constNum;
  163. }
  164. return normalMapArray;
  165. }
  166. Var* TerrainFeatHLSL::_getOrmMapSampler()
  167. {
  168. String name("ormMapSampler");
  169. Var* ormMapSampler = (Var*)LangElement::find(name);
  170. if (!ormMapSampler)
  171. {
  172. ormMapSampler = new Var;
  173. ormMapSampler->setName(name);
  174. ormMapSampler->setType("SamplerState");
  175. ormMapSampler->uniform = true;
  176. ormMapSampler->sampler = true;
  177. ormMapSampler->constNum = Var::getTexUnitNum();
  178. }
  179. return ormMapSampler;
  180. }
  181. Var* TerrainFeatHLSL::_getOrmMapArray()
  182. {
  183. String name("ormMapArray");
  184. Var* ormMapArray = (Var*)LangElement::find(name);
  185. if (!ormMapArray)
  186. {
  187. ormMapArray = new Var;
  188. ormMapArray->setName(name);
  189. ormMapArray->setType("Texture2DArray");
  190. ormMapArray->uniform = true;
  191. ormMapArray->texture = true;
  192. ormMapArray->constNum = _getOrmMapSampler()->constNum;
  193. }
  194. return ormMapArray;
  195. }
  196. Var* TerrainFeatHLSL::_getDetailIdStrengthParallax()
  197. {
  198. String name( String::ToString( "detailIdStrengthParallax", getProcessIndex() ) );
  199. Var *detailInfo = (Var*)LangElement::find( name );
  200. if ( !detailInfo )
  201. {
  202. detailInfo = new Var;
  203. detailInfo->setType( "float4" );
  204. detailInfo->setName( name );
  205. detailInfo->uniform = true;
  206. detailInfo->constSortPos = cspPotentialPrimitive;
  207. detailInfo->arraySize = getProcessIndex();
  208. }
  209. detailInfo->arraySize = mMax(detailInfo->arraySize, getProcessIndex() + 1);
  210. return detailInfo;
  211. }
  212. Var* TerrainFeatHLSL::_getMacroIdStrengthParallax()
  213. {
  214. String name( String::ToString( "macroIdStrengthParallax%d", getProcessIndex() ) );
  215. Var *detailInfo = (Var*)LangElement::find( name );
  216. if ( !detailInfo )
  217. {
  218. detailInfo = new Var;
  219. detailInfo->setType( "float3" );
  220. detailInfo->setName( name );
  221. detailInfo->uniform = true;
  222. detailInfo->constSortPos = cspPotentialPrimitive;
  223. }
  224. return detailInfo;
  225. }
  226. void TerrainBaseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  227. const MaterialFeatureData &fd )
  228. {
  229. MultiLine *meta = new MultiLine;
  230. output = meta;
  231. // Generate the incoming texture var.
  232. Var *inTex;
  233. {
  234. Var *inPos = (Var*)LangElement::find( "inPosition" );
  235. if ( !inPos )
  236. inPos = (Var*)LangElement::find( "position" );
  237. inTex = new Var( "texCoord", "float3" );
  238. Var *oneOverTerrainSize = _getUniformVar( "oneOverTerrainSize", "float", cspPass );
  239. // NOTE: The y coord here should be negative to have
  240. // the texture maps not end up flipped which also caused
  241. // normal and parallax mapping to be incorrect.
  242. //
  243. // This mistake early in development means that the layer
  244. // id bilinear blend depends on it being that way.
  245. //
  246. // So instead i fixed this by flipping the base and detail
  247. // coord y scale to compensate when rendering.
  248. //
  249. meta->addStatement( new GenOp( " @ = @.xyz * float3( @, @, -@ );\r\n",
  250. new DecOp( inTex ), inPos, oneOverTerrainSize, oneOverTerrainSize, oneOverTerrainSize ) );
  251. }
  252. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  253. // Pass the texture coord to the pixel shader.
  254. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  255. outTex->setName( "outTexCoord" );
  256. outTex->setStructName( "OUT" );
  257. outTex->setType( "float3" );
  258. meta->addStatement( new GenOp( " @.xy = @.xy;\r\n", outTex, inTex ) );
  259. // If this shader has a side projected layer then we
  260. // pass the dot product between the +Y and the normal
  261. // thru outTexCoord.z for use in blending the textures.
  262. if ( fd.features.hasFeature( MFT_TerrainSideProject ) )
  263. {
  264. Var *inNormal = (Var*)LangElement::find( "normal" );
  265. meta->addStatement(
  266. new GenOp( " @.z = pow( abs( dot( normalize( float3( @.x, @.y, 0 ) ), float3( 0, 1, 0 ) ) ), 10.0 );\r\n",
  267. outTex, inNormal, inNormal ) );
  268. }
  269. else
  270. meta->addStatement( new GenOp( " @.z = 0;\r\n", outTex ) );
  271. // HACK: This is sort of lazy... we generate the tanget
  272. // vector here so that we're sure it exists in the parallax
  273. // and normal features which will expect "T" to exist.
  274. //
  275. // If this shader doesn't use it the shader compiler will
  276. // optimize away this code.
  277. //
  278. Var *inTangentZ = getVertTexCoord( "tcTangentZ" );
  279. Var *inTanget = new Var( "T", "float3" );
  280. Var *squareSize = _getUniformVar( "squareSize", "float", cspPass );
  281. meta->addStatement( new GenOp( " @ = normalize( float3( @, 0, @ ) );\r\n",
  282. new DecOp( inTanget ), squareSize, inTangentZ ) );
  283. }
  284. void TerrainBaseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  285. const MaterialFeatureData &fd )
  286. {
  287. // grab connector texcoord register
  288. Var *texCoord = getInTexCoord( "texCoord", "float3", componentList );
  289. // create texture var
  290. Var *diffuseMap = new Var;
  291. diffuseMap->setType( "SamplerState" );
  292. diffuseMap->setName( "baseTexMap" );
  293. diffuseMap->uniform = true;
  294. diffuseMap->sampler = true;
  295. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  296. MultiLine *meta = new MultiLine;
  297. Var *baseColor = new Var;
  298. baseColor->setType( "float4" );
  299. baseColor->setName( "baseColor" );
  300. Var *diffuseTex = new Var;
  301. diffuseTex->setType("Texture2D");
  302. diffuseTex->setName("baseTexture");
  303. diffuseTex->uniform = true;
  304. diffuseTex->texture = true;
  305. diffuseTex->constNum = diffuseMap->constNum;
  306. meta->addStatement(new GenOp(" @ = @.Sample( @, @.xy );\r\n", new DecOp(baseColor), diffuseTex, diffuseMap, texCoord));
  307. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  308. if (fd.features.hasFeature(MFT_isDeferred))
  309. {
  310. target= ShaderFeature::RenderTarget1;
  311. }
  312. meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul,NULL,target ) ) );
  313. if (fd.features[MFT_isDeferred])
  314. {
  315. // Set base ORM info
  316. Var* ormConfig;
  317. OutputTarget targ = RenderTarget1;
  318. targ = RenderTarget2;
  319. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(targ));
  320. if (!ormConfig)
  321. {
  322. // create color var
  323. ormConfig = new Var;
  324. ormConfig->setType("fragout");
  325. ormConfig->setName(getOutputTargetVarName(targ));
  326. ormConfig->setStructName("OUT");
  327. }
  328. meta->addStatement(new GenOp(" @ = float4(0.0, 1.0, 1.0, 0.0);\r\n", ormConfig));
  329. }
  330. output = meta;
  331. }
  332. ShaderFeature::Resources TerrainBaseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  333. {
  334. Resources res;
  335. res.numTexReg = 1;
  336. res.numTex = 1;
  337. return res;
  338. }
  339. U32 TerrainBaseMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  340. {
  341. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 | ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  342. }
  343. TerrainDetailMapFeatHLSL::TerrainDetailMapFeatHLSL()
  344. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  345. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  346. {
  347. addDependency( &mTorqueDep );
  348. addDependency( &mTerrainDep );
  349. }
  350. void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  351. const MaterialFeatureData &fd )
  352. {
  353. const S32 detailIndex = getProcessIndex();
  354. // Grab incoming texture coords... the base map feature
  355. // made sure this was created.
  356. Var *inTex = (Var*)LangElement::find( "texCoord" );
  357. AssertFatal( inTex, "The texture coord is missing!" );
  358. // Grab the input position.
  359. Var *inPos = (Var*)LangElement::find( "inPosition" );
  360. if ( !inPos )
  361. inPos = (Var*)LangElement::find( "position" );
  362. // Get the object space eye position.
  363. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  364. MultiLine *meta = new MultiLine;
  365. // If we have parallax mapping then make sure we've sent
  366. // the negative view vector to the pixel shader.
  367. if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
  368. !LangElement::find( "outNegViewTS" ) )
  369. {
  370. // Get the object to tangent transform which
  371. // will consume 3 output registers.
  372. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
  373. // Now use a single output register to send the negative
  374. // view vector in tangent space to the pixel shader.
  375. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  376. Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
  377. outNegViewTS->setName( "outNegViewTS" );
  378. outNegViewTS->setStructName( "OUT" );
  379. outNegViewTS->setType( "float3" );
  380. meta->addStatement( new GenOp( " @ = mul( @, float3( @ - @.xyz ) );\r\n",
  381. outNegViewTS, objToTangentSpace, eyePos, inPos ) );
  382. }
  383. // Get the distance from the eye to this vertex.
  384. Var *dist = (Var*)LangElement::find( "dist" );
  385. if ( !dist )
  386. {
  387. dist = new Var;
  388. dist->setType( "float" );
  389. dist->setName( "dist" );
  390. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  391. new DecOp( dist ), inPos, eyePos ) );
  392. }
  393. // grab connector texcoord register
  394. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  395. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  396. outTex->setName( String::ToString( "detCoord%d", detailIndex ) );
  397. outTex->setStructName( "OUT" );
  398. outTex->setType( "float4" );
  399. // Get the detail scale and fade info.
  400. Var* detScaleAndFade = (Var*)LangElement::find("detailScaleAndFade");
  401. if (detScaleAndFade == NULL)
  402. {
  403. detScaleAndFade = new Var;
  404. detScaleAndFade->setType("float4");
  405. detScaleAndFade->setName("detailScaleAndFade");
  406. detScaleAndFade->uniform = true;
  407. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  408. }
  409. detScaleAndFade->arraySize = mMax(detScaleAndFade->arraySize, detailIndex + 1);
  410. // Setup the detail coord.
  411. //
  412. // NOTE: You see here we scale the texture coord by 'xyx'
  413. // to generate the detail coord. This y is here because
  414. // its scale is flipped to correct for the non negative y
  415. // in texCoord.
  416. //
  417. // See TerrainBaseMapFeatHLSL::processVert().
  418. //
  419. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(detScaleAndFade, detailIndex) ) );
  420. // And sneak the detail fade thru the w detailCoord.
  421. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  422. outTex, new IndexOp(detScaleAndFade, detailIndex), dist, new IndexOp(detScaleAndFade, detailIndex)) );
  423. output = meta;
  424. }
  425. void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  426. const MaterialFeatureData &fd )
  427. {
  428. const S32 detailIndex = getProcessIndex();
  429. Var *inTex = getVertTexCoord( "texCoord" );
  430. MultiLine *meta = new MultiLine;
  431. // We need the negative tangent space view vector
  432. // as in parallax mapping we step towards the camera.
  433. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  434. if ( !negViewTS &&
  435. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  436. {
  437. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  438. if ( !inNegViewTS )
  439. {
  440. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  441. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  442. inNegViewTS->setName( "outNegViewTS" );
  443. inNegViewTS->setStructName( "IN" );
  444. inNegViewTS->setType( "float3" );
  445. }
  446. negViewTS = new Var( "negViewTS", "float3" );
  447. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  448. }
  449. // Get the layer samples.
  450. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  451. if ( !layerSample )
  452. {
  453. layerSample = new Var;
  454. layerSample->setType( "float4" );
  455. layerSample->setName( "layerSample" );
  456. // Get the layer texture var
  457. Var *layerTex = new Var;
  458. layerTex->setType( "SamplerState" );
  459. layerTex->setName( "layerTex" );
  460. layerTex->uniform = true;
  461. layerTex->sampler = true;
  462. layerTex->constNum = Var::getTexUnitNum();
  463. Var* layerTexObj = new Var;
  464. layerTexObj->setName("layerTexObj");
  465. layerTexObj->setType("Texture2D");
  466. layerTexObj->uniform = true;
  467. layerTexObj->texture = true;
  468. layerTexObj->constNum = layerTex->constNum;
  469. // Read the layer texture to get the samples.
  470. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  471. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  472. }
  473. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  474. if ( !layerSize )
  475. {
  476. layerSize = new Var;
  477. layerSize->setType( "float" );
  478. layerSize->setName( "layerSize" );
  479. layerSize->uniform = true;
  480. layerSize->constSortPos = cspPass;
  481. }
  482. // Grab the incoming detail coord.
  483. Var *inDet = _getInDetailCoord( componentList );
  484. // Get the detail id.
  485. Var *detailInfo = _getDetailIdStrengthParallax();
  486. // Create the detail blend var.
  487. Var *detailBlend = new Var;
  488. detailBlend->setType( "float" );
  489. detailBlend->setName( String::ToString( "detailBlend%d", detailIndex ) );
  490. // Calculate the blend for this detail texture.
  491. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  492. new DecOp( detailBlend ), new IndexOp(detailInfo, detailIndex), inTex, layerSize, layerSample ) );
  493. // If we had a parallax feature... then factor in the parallax
  494. // amount so that it fades out with the layer blending.
  495. if (fd.features.hasFeature(MFT_TerrainParallaxMap, detailIndex))
  496. {
  497. Var* normalMapArray = _getNormalMapArray();
  498. Var* normalMapSampler = _getNormalMapSampler();
  499. // Call the library function to do the rest.
  500. if (fd.features.hasFeature(MFT_IsBC3nm, detailIndex))
  501. {
  502. meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnmTexArray( @, @, float3(@.xy, @.x), @, @.z * @ );\r\n",
  503. inDet, normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, detailIndex), negViewTS, new IndexOp(detailInfo, detailIndex), detailBlend));
  504. }
  505. else
  506. {
  507. meta->addStatement(new GenOp(" @.xy += parallaxOffsetTexArray( @, @, float3(@.xy, @.x), @, @.z * @ );\r\n",
  508. inDet, normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, detailIndex), negViewTS, new IndexOp(detailInfo, detailIndex), detailBlend));
  509. }
  510. }
  511. Var *detailColor = (Var*)LangElement::find(String::ToString("detailColor%d", detailIndex));
  512. if ( !detailColor )
  513. {
  514. detailColor = new Var;
  515. detailColor->setType( "float4" );
  516. detailColor->setName( String::ToString("detailColor%d", detailIndex) );
  517. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  518. }
  519. //Sampled detail texture that is not expanded
  520. Var* detailMapArray = _getDetailMapArray();
  521. Var* detailMapSampler = _getDetailMapSampler();
  522. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  523. {
  524. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z ) * 2.0 ) - 1.0;\r\n",
  525. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), inTex));
  526. }
  527. else
  528. {
  529. meta->addStatement(new GenOp(" @ = ( @.Sample( @, float3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  530. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex)));
  531. }
  532. meta->addStatement(new GenOp(" @ *= @.y * @.w;\r\n",
  533. detailColor, new IndexOp(detailInfo, detailIndex), inDet));
  534. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  535. {
  536. // Check to see if we have a gbuffer normal.
  537. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  538. // If we have a gbuffer normal and we don't have a
  539. // normal map feature then we need to lerp in a
  540. // default normal else the normals below this layer
  541. // will show thru.
  542. if (gbNormal &&
  543. !fd.features.hasFeature(MFT_TerrainNormalMap, detailIndex))
  544. {
  545. Var* viewToTangent = getInViewToTangent(componentList);
  546. meta->addStatement(new GenOp(" @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  547. gbNormal, gbNormal, viewToTangent, detailBlend, inDet));
  548. }
  549. // If we're using SM 3.0 then take advantage of
  550. // dynamic branching to skip layers per-pixel.
  551. if (GFX->getPixelShaderVersion() >= 3.0f)
  552. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  553. meta->addStatement(new GenOp(" {\r\n"));
  554. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  555. if (fd.features.hasFeature(MFT_isDeferred))
  556. target = ShaderFeature::RenderTarget1;
  557. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  558. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  559. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  560. outColor, detailColor, detailBlend));
  561. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  562. meta->addStatement(new GenOp(" }\r\n"));
  563. }
  564. output = meta;
  565. }
  566. ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  567. {
  568. Resources res;
  569. if ( getProcessIndex() == 0 )
  570. {
  571. // If this is the first detail pass then we
  572. // samples from the layer tex.
  573. res.numTex += 1;
  574. res.numTexReg += 1;
  575. // Add Detail TextureArray
  576. res.numTex += 1;
  577. res.numTexReg += 1;
  578. }
  579. return res;
  580. }
  581. U32 TerrainDetailMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  582. {
  583. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  584. }
  585. TerrainMacroMapFeatHLSL::TerrainMacroMapFeatHLSL()
  586. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  587. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  588. {
  589. addDependency( &mTorqueDep );
  590. addDependency( &mTerrainDep );
  591. }
  592. void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  593. const MaterialFeatureData &fd )
  594. {
  595. const S32 detailIndex = getProcessIndex();
  596. // Grab incoming texture coords... the base map feature
  597. // made sure this was created.
  598. Var *inTex = (Var*)LangElement::find( "texCoord" );
  599. AssertFatal( inTex, "The texture coord is missing!" );
  600. // Grab the input position.
  601. Var *inPos = (Var*)LangElement::find( "inPosition" );
  602. if ( !inPos )
  603. inPos = (Var*)LangElement::find( "position" );
  604. // Get the object space eye position.
  605. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  606. MultiLine *meta = new MultiLine;
  607. // Get the distance from the eye to this vertex.
  608. Var *dist = (Var*)LangElement::find( "macroDist" );
  609. if ( !dist )
  610. {
  611. dist = new Var;
  612. dist->setType( "float" );
  613. dist->setName( "macroDist" );
  614. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  615. new DecOp( dist ), inPos, eyePos ) );
  616. }
  617. // grab connector texcoord register
  618. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  619. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  620. outTex->setName( String::ToString( "macroCoord%d", detailIndex ) );
  621. outTex->setStructName( "OUT" );
  622. outTex->setType( "float4" );
  623. // Get the detail scale and fade info.
  624. Var* macroScaleAndFade = (Var*)LangElement::find("macroScaleAndFade");
  625. if (macroScaleAndFade == NULL)
  626. {
  627. macroScaleAndFade = new Var;
  628. macroScaleAndFade->setType("float4");
  629. macroScaleAndFade->setName("macroScaleAndFade");
  630. macroScaleAndFade->uniform = true;
  631. macroScaleAndFade->constSortPos = cspPotentialPrimitive;
  632. }
  633. macroScaleAndFade->arraySize = mMax(macroScaleAndFade->arraySize, detailIndex + 1);
  634. // Setup the detail coord.
  635. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(macroScaleAndFade, detailIndex)) );
  636. // And sneak the detail fade thru the w detailCoord.
  637. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  638. outTex, new IndexOp(macroScaleAndFade, detailIndex), dist, new IndexOp(macroScaleAndFade, detailIndex)) );
  639. output = meta;
  640. }
  641. void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  642. const MaterialFeatureData &fd )
  643. {
  644. const S32 detailIndex = getProcessIndex();
  645. Var *inTex = getVertTexCoord( "texCoord" );
  646. MultiLine *meta = new MultiLine;
  647. // We need the negative tangent space view vector
  648. // as in parallax mapping we step towards the camera.
  649. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  650. if ( !negViewTS &&
  651. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  652. {
  653. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  654. if ( !inNegViewTS )
  655. {
  656. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  657. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  658. inNegViewTS->setName( "outNegViewTS" );
  659. inNegViewTS->setStructName( "IN" );
  660. inNegViewTS->setType( "float3" );
  661. }
  662. negViewTS = new Var( "negViewTS", "float3" );
  663. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  664. }
  665. // Get the layer samples.
  666. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  667. if ( !layerSample )
  668. {
  669. layerSample = new Var;
  670. layerSample->setType( "float4" );
  671. layerSample->setName( "layerSample" );
  672. // Get the layer texture var
  673. Var *layerTex = new Var;
  674. layerTex->setType( "SamplerState" );
  675. layerTex->setName( "macrolayerTex" );
  676. layerTex->uniform = true;
  677. layerTex->sampler = true;
  678. layerTex->constNum = Var::getTexUnitNum();
  679. // Read the layer texture to get the samples.
  680. Var *layerTexObj = new Var;
  681. layerTexObj->setType("Texture2D");
  682. layerTexObj->setName("macroLayerTexObj");
  683. layerTexObj->uniform = true;
  684. layerTexObj->texture = true;
  685. layerTexObj->constNum = layerTex->constNum;
  686. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  687. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  688. }
  689. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  690. if ( !layerSize )
  691. {
  692. layerSize = new Var;
  693. layerSize->setType( "float" );
  694. layerSize->setName( "layerSize" );
  695. layerSize->uniform = true;
  696. layerSize->constSortPos = cspPass;
  697. }
  698. // Grab the incoming detail coord.
  699. Var *inDet = _getInMacroCoord( componentList );
  700. // Get the detail id.
  701. Var *detailInfo = _getMacroIdStrengthParallax();
  702. // Create the detail blend var.
  703. Var *detailBlend = new Var;
  704. detailBlend->setType( "float" );
  705. detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) );
  706. // Calculate the blend for this detail texture.
  707. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  708. new DecOp( detailBlend ), new IndexOp(detailInfo, detailIndex), inTex, layerSize, layerSample ) );
  709. // Check to see if we have a gbuffer normal.
  710. Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
  711. // If we have a gbuffer normal and we don't have a
  712. // normal map feature then we need to lerp in a
  713. // default normal else the normals below this layer
  714. // will show thru.
  715. if ( gbNormal &&
  716. !fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) )
  717. {
  718. Var *viewToTangent = getInViewToTangent( componentList );
  719. meta->addStatement( new GenOp( " @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  720. gbNormal, gbNormal, viewToTangent, detailBlend, inDet ) );
  721. }
  722. Var *detailColor = (Var*)LangElement::find("macroColor");
  723. if (!detailColor)
  724. {
  725. detailColor = new Var;
  726. detailColor->setType( "float4" );
  727. detailColor->setName( "macroColor" );
  728. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  729. }
  730. // If we're using SM 3.0 then take advantage of
  731. // dynamic branching to skip layers per-pixel.
  732. if ( GFX->getPixelShaderVersion() >= 3.0f )
  733. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  734. meta->addStatement( new GenOp( " {\r\n" ) );
  735. Var* detailMapArray = _getDetailMapArray();
  736. Var* detailMapSampler = _getDetailMapSampler();
  737. // Note that we're doing the standard greyscale detail
  738. // map technique here which can darken and lighten the
  739. // diffuse texture.
  740. //
  741. // We take two color samples and lerp between them for
  742. // side projection layers... else a single sample.
  743. //
  744. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  745. {
  746. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z ) * 2.0 ) - 1.0;\r\n",
  747. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), inTex));
  748. }
  749. else
  750. {
  751. meta->addStatement(new GenOp(" @ = ( @.Sample( @, float3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  752. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex)));
  753. }
  754. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  755. detailColor, new IndexOp(detailInfo, detailIndex), inDet ) );
  756. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  757. if (fd.features.hasFeature(MFT_isDeferred))
  758. target= ShaderFeature::RenderTarget1;
  759. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  760. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  761. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  762. outColor, detailColor, detailBlend));
  763. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  764. meta->addStatement( new GenOp( " }\r\n" ) );
  765. output = meta;
  766. }
  767. ShaderFeature::Resources TerrainMacroMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  768. {
  769. Resources res;
  770. if ( getProcessIndex() == 0 )
  771. {
  772. // If this is the first detail pass then we
  773. // samples from the layer tex.
  774. res.numTex += 1;
  775. }
  776. res.numTex += 1;
  777. // Finally we always send the detail texture
  778. // coord to the pixel shader.
  779. res.numTexReg += 1;
  780. return res;
  781. }
  782. U32 TerrainMacroMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  783. {
  784. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  785. }
  786. void TerrainNormalMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  787. const MaterialFeatureData &fd )
  788. {
  789. // We only need to process normals during the deferred.
  790. if ( !fd.features.hasFeature( MFT_DeferredConditioner ) )
  791. return;
  792. MultiLine *meta = new MultiLine;
  793. if ( !fd.features.hasFeature(MFT_TerrainHeightBlend) )
  794. {
  795. // Make sure the world to tangent transform
  796. // is created and available for the pixel shader.
  797. getOutViewToTangent(componentList, meta, fd);
  798. }
  799. output = meta;
  800. }
  801. void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  802. const MaterialFeatureData &fd )
  803. {
  804. // We only need to process normals during the deferred.
  805. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  806. return;
  807. MultiLine *meta = new MultiLine;
  808. const S32 normalIndex = getProcessIndex();
  809. Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
  810. AssertFatal( detailBlend, "The detail blend is missing!" );
  811. /// Get the texture coord.
  812. Var* inDet = _getInDetailCoord(componentList);
  813. Var* inTex = getVertTexCoord("texCoord");
  814. Var* detailInfo = _getDetailIdStrengthParallax();
  815. // Sample the normal map.
  816. //
  817. // We take two normal samples and lerp between them for
  818. // side projection layers... else a single sample.
  819. LangElement* texOp;
  820. Var* normalMapSampler = _getNormalMapSampler();
  821. Var* normalMapArray = _getNormalMapArray();
  822. if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
  823. {
  824. texOp = new GenOp("lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z )",
  825. normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex), normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex), inTex);
  826. }
  827. else
  828. texOp = new GenOp("@.Sample(@, float3(@.xy, @.x))", normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex));
  829. // create bump normal
  830. Var* bumpNorm = new Var;
  831. bumpNorm->setName(String::ToString("bumpNormal%d", normalIndex));
  832. bumpNorm->setType("float4");
  833. LangElement* bumpNormDecl = new DecOp(bumpNorm);
  834. meta->addStatement(expandNormalMap(texOp, bumpNormDecl, bumpNorm, fd));
  835. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  836. {
  837. Var* viewToTangent = getInViewToTangent(componentList);
  838. // This var is read from GBufferConditionerHLSL and
  839. // used in the deferred output.
  840. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  841. if (!gbNormal)
  842. {
  843. gbNormal = new Var;
  844. gbNormal->setName("gbNormal");
  845. gbNormal->setType("float3");
  846. meta->addStatement(new GenOp(" @ = @[2];\r\n", new DecOp(gbNormal), viewToTangent));
  847. }
  848. // If we're using SM 3.0 then take advantage of
  849. // dynamic branching to skip layers per-pixel.
  850. if (GFX->getPixelShaderVersion() >= 3.0f)
  851. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  852. meta->addStatement(new GenOp(" {\r\n"));
  853. // Normalize is done later...
  854. // Note: The reverse mul order is intentional. Affine matrix.
  855. meta->addStatement(new GenOp(" @ = lerp( @, mul( @.xyz, @ ), min( @, @.w ) );\r\n",
  856. gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet));
  857. // End the conditional block.
  858. meta->addStatement(new GenOp(" }\r\n"));
  859. }
  860. output = meta;
  861. }
  862. ShaderFeature::Resources TerrainNormalMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  863. {
  864. Resources res;
  865. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  866. {
  867. return res;
  868. }
  869. S32 featureIndex = 0, firstNormalMapIndex = 0;
  870. for (int idx = 0; idx < fd.features.getCount(); ++idx) {
  871. const FeatureType &type = fd.features.getAt(idx, &featureIndex);
  872. if (type == MFT_TerrainNormalMap) {
  873. firstNormalMapIndex = getMin(firstNormalMapIndex, featureIndex);
  874. }
  875. }
  876. // We only need to process normals during the deferred.
  877. if (getProcessIndex() == firstNormalMapIndex)
  878. {
  879. res.numTexReg += 1;
  880. res.numTex += 1;
  881. }
  882. return res;
  883. }
  884. void TerrainLightMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  885. const MaterialFeatureData &fd )
  886. {
  887. // grab connector texcoord register
  888. Var *inTex = (Var*)LangElement::find( "texCoord" );
  889. if ( !inTex )
  890. return;
  891. // Get the lightmap texture.
  892. Var *lightMap = new Var;
  893. lightMap->setType( "SamplerState" );
  894. lightMap->setName( "lightMapTex" );
  895. lightMap->uniform = true;
  896. lightMap->sampler = true;
  897. lightMap->constNum = Var::getTexUnitNum();
  898. MultiLine *meta = new MultiLine;
  899. // Find or create the lightMask value which is read by
  900. // RTLighting to mask out the lights.
  901. //
  902. // The first light is always the sunlight so we apply
  903. // the shadow mask to only the first channel.
  904. //
  905. Var *lightMask = (Var*)LangElement::find( "lightMask" );
  906. if ( !lightMask )
  907. {
  908. lightMask = new Var( "lightMask", "float4" );
  909. meta->addStatement( new GenOp( " @ = 1;\r\n", new DecOp( lightMask ) ) );
  910. }
  911. Var* lightMapTex = new Var;
  912. lightMapTex->setName("lightMapTexObj");
  913. lightMapTex->setType("Texture2D");
  914. lightMapTex->uniform = true;
  915. lightMapTex->texture = true;
  916. lightMapTex->constNum = lightMap->constNum;
  917. meta->addStatement(new GenOp(" @[0] = @.Sample( @, @.xy ).r;\r\n", lightMask, lightMapTex, lightMap, inTex));
  918. output = meta;
  919. }
  920. ShaderFeature::Resources TerrainLightMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  921. {
  922. Resources res;
  923. res.numTex = 1;
  924. return res;
  925. }
  926. //standard matInfo map contains data of the form .r = bitflags, .g = (will contain AO),
  927. //.b = specular strength, a= spec power.
  928. void TerrainORMMapFeatHLSL::processVert(Vector<ShaderComponent*> &componentList,
  929. const MaterialFeatureData &fd)
  930. {
  931. const S32 detailIndex = getProcessIndex();
  932. // Grab incoming texture coords... the base map feature
  933. // made sure this was created.
  934. Var *inTex = (Var*)LangElement::find("texCoord");
  935. AssertFatal(inTex, "The texture coord is missing!");
  936. // Grab the input position.
  937. Var *inPos = (Var*)LangElement::find("inPosition");
  938. if (!inPos)
  939. inPos = (Var*)LangElement::find("position");
  940. // Get the object space eye position.
  941. Var *eyePos = _getUniformVar("eyePos", "float3", cspPotentialPrimitive);
  942. MultiLine *meta = new MultiLine;
  943. // If we have parallax mapping then make sure we've sent
  944. // the negative view vector to the pixel shader.
  945. if (fd.features.hasFeature(MFT_TerrainParallaxMap) &&
  946. !LangElement::find("outNegViewTS"))
  947. {
  948. // Get the object to tangent transform which
  949. // will consume 3 output registers.
  950. Var *objToTangentSpace = getOutObjToTangentSpace(componentList, meta, fd);
  951. // Now use a single output register to send the negative
  952. // view vector in tangent space to the pixel shader.
  953. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  954. Var *outNegViewTS = connectComp->getElement(RT_TEXCOORD);
  955. outNegViewTS->setName("outNegViewTS");
  956. outNegViewTS->setStructName("OUT");
  957. outNegViewTS->setType("float3");
  958. meta->addStatement(new GenOp(" @ = mul( @, float3( @ - @.xyz ) );\r\n",
  959. outNegViewTS, objToTangentSpace, eyePos, inPos));
  960. }
  961. // Get the distance from the eye to this vertex.
  962. Var *dist = (Var*)LangElement::find("dist");
  963. if (!dist)
  964. {
  965. dist = new Var;
  966. dist->setType("float");
  967. dist->setName("dist");
  968. meta->addStatement(new GenOp(" @ = distance( @.xyz, @ );\r\n",
  969. new DecOp(dist), inPos, eyePos));
  970. }
  971. // grab connector texcoord register
  972. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  973. Var *outTex = (Var*)LangElement::find(String::ToString("detCoord%d", detailIndex));
  974. if (outTex == NULL)
  975. {
  976. outTex = connectComp->getElement(RT_TEXCOORD);
  977. outTex->setName(String::ToString("detCoord%d", detailIndex));
  978. outTex->setStructName("OUT");
  979. outTex->setType("float4");
  980. }
  981. // Get the detail scale and fade info.
  982. Var *detScaleAndFade = (Var*)LangElement::find("detailScaleAndFade");
  983. if (detScaleAndFade == NULL)
  984. {
  985. detScaleAndFade = new Var;
  986. detScaleAndFade->setType("float4");
  987. detScaleAndFade->setName("detailScaleAndFade");
  988. detScaleAndFade->uniform = true;
  989. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  990. }
  991. detScaleAndFade->arraySize = mMax(detScaleAndFade->arraySize, detailIndex + 1);
  992. // Setup the detail coord.
  993. //
  994. // NOTE: You see here we scale the texture coord by 'xyx'
  995. // to generate the detail coord. This y is here because
  996. // its scale is flipped to correct for the non negative y
  997. // in texCoord.
  998. //
  999. // See TerrainBaseMapFeatHLSL::processVert().
  1000. //
  1001. meta->addStatement(new GenOp(" @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(detScaleAndFade, detailIndex)));
  1002. // And sneak the detail fade thru the w detailCoord.
  1003. meta->addStatement(new GenOp(" @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  1004. outTex, new IndexOp(detScaleAndFade, detailIndex), dist, new IndexOp(detScaleAndFade, detailIndex)));
  1005. output = meta;
  1006. }
  1007. U32 TerrainORMMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  1008. {
  1009. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  1010. }
  1011. void TerrainORMMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  1012. const MaterialFeatureData &fd)
  1013. {
  1014. /// Get the texture coord.
  1015. Var *inDet = _getInDetailCoord(componentList);
  1016. Var *inTex = getVertTexCoord("texCoord");
  1017. Var* detailInfo = _getDetailIdStrengthParallax();
  1018. const S32 compositeIndex = getProcessIndex();
  1019. // Sample the normal map.
  1020. //
  1021. // We take two normal samples and lerp between them for
  1022. // side projection layers... else a single sample.
  1023. LangElement *texOp;
  1024. Var* ormMapArray = _getOrmMapArray();
  1025. Var* ormMapSampler = _getOrmMapSampler();
  1026. if (fd.features.hasFeature(MFT_TerrainSideProject, compositeIndex))
  1027. {
  1028. texOp = new GenOp("lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z )",
  1029. ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex), ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex), inTex);
  1030. }
  1031. else
  1032. texOp = new GenOp("@.Sample(@, float3(@.xy, @.x))", ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex));
  1033. // search for material var
  1034. Var * ormConfig;
  1035. OutputTarget targ = RenderTarget1;
  1036. if (fd.features[MFT_isDeferred])
  1037. {
  1038. targ = RenderTarget2;
  1039. }
  1040. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1041. MultiLine * meta = new MultiLine;
  1042. if (!ormConfig)
  1043. {
  1044. // create color var
  1045. ormConfig = new Var;
  1046. ormConfig->setType("fragout");
  1047. ormConfig->setName(getOutputTargetVarName(targ));
  1048. ormConfig->setStructName("OUT");
  1049. }
  1050. Var *detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  1051. AssertFatal(detailBlend, "The detail blend is missing!");
  1052. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  1053. Var *matinfoCol = new Var(matinfoName, "float3");
  1054. if (compositeIndex == 0)
  1055. {
  1056. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", ormConfig));
  1057. }
  1058. meta->addStatement(new GenOp(" @ = @.rgb;\r\n", new DecOp(matinfoCol), texOp));
  1059. if (fd.features.hasFeature(MFT_InvertRoughness, compositeIndex))
  1060. {
  1061. meta->addStatement(new GenOp(" @.b = 1.0 - @.b;\r\n", matinfoCol, matinfoCol));
  1062. }
  1063. meta->addStatement(new GenOp(" @ = lerp(float3(1.0, 1.0, 0.0), @, @.y * @.w);\r\n", matinfoCol, matinfoCol, new IndexOp(detailInfo, compositeIndex), inDet));
  1064. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  1065. {
  1066. meta->addStatement(new GenOp(" @.gba += @ * @;\r\n", ormConfig, matinfoCol, detailBlend));
  1067. }
  1068. output = meta;
  1069. }
  1070. ShaderFeature::Resources TerrainORMMapFeatHLSL::getResources(const MaterialFeatureData &fd)
  1071. {
  1072. Resources res;
  1073. res.numTex = 1;
  1074. return res;
  1075. }
  1076. // reminder, the matinfo buffer is flags, smooth, ao, metal
  1077. U32 TerrainBlankInfoMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  1078. {
  1079. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::RenderTarget1;
  1080. }
  1081. void TerrainBlankInfoMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  1082. const MaterialFeatureData &fd)
  1083. {
  1084. S32 compositeIndex = getProcessIndex();
  1085. // search for material var
  1086. Var *material;
  1087. OutputTarget ormConfig = RenderTarget1;
  1088. if (fd.features[MFT_isDeferred])
  1089. {
  1090. ormConfig = RenderTarget2;
  1091. }
  1092. material = (Var*)LangElement::find(getOutputTargetVarName(ormConfig));
  1093. MultiLine * meta = new MultiLine;
  1094. if (!material)
  1095. {
  1096. // create color var
  1097. material = new Var;
  1098. material->setType("fragout");
  1099. material->setName(getOutputTargetVarName(ormConfig));
  1100. material->setStructName("OUT");
  1101. }
  1102. if (compositeIndex == 0)
  1103. {
  1104. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", material));
  1105. }
  1106. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  1107. AssertFatal(detailBlend, "The detail blend is missing!");
  1108. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  1109. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  1110. {
  1111. meta->addStatement(new GenOp(" @.gba += float3(@, @, 0.0);\r\n", material, detailBlend, detailBlend));
  1112. }
  1113. output = meta;
  1114. }
  1115. void TerrainHeightMapBlendHLSL::processVert(Vector<ShaderComponent*>& componentList,
  1116. const MaterialFeatureData& fd)
  1117. {
  1118. // We only need to process normals during the deferred.
  1119. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  1120. return;
  1121. MultiLine* meta = new MultiLine;
  1122. // Make sure the world to tangent transform
  1123. // is created and available for the pixel shader.
  1124. getOutViewToTangent(componentList, meta, fd);
  1125. output = meta;
  1126. }
  1127. void TerrainHeightMapBlendHLSL::processPix(Vector<ShaderComponent*>& componentList,
  1128. const MaterialFeatureData& fd)
  1129. {
  1130. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  1131. if (fd.features.hasFeature(MFT_isDeferred))
  1132. target = ShaderFeature::RenderTarget1;
  1133. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  1134. if (!outColor)
  1135. return;
  1136. MultiLine* meta = new MultiLine;
  1137. // Count number of detail layers
  1138. int detailCount = 0;
  1139. while (true)
  1140. {
  1141. if(LangElement::find(String::ToString("detailBlend%d", detailCount)) == NULL)
  1142. {
  1143. break;
  1144. }
  1145. ++detailCount;
  1146. }
  1147. if ( detailCount == 0 )
  1148. {
  1149. return;
  1150. }
  1151. // Compute blend factors
  1152. for (S32 idx = 0; idx < detailCount; ++idx)
  1153. {
  1154. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1155. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1156. Var* blendDepth = (Var*)LangElement::find(String::ToString("blendDepth%d", idx));
  1157. if (!blendDepth)
  1158. {
  1159. blendDepth = new Var;
  1160. blendDepth->setType("float");
  1161. blendDepth->setName(String::ToString("blendDepth%d", idx));
  1162. blendDepth->uniform = true;
  1163. blendDepth->constSortPos = cspPrimitive;
  1164. }
  1165. Var* blendContrast = (Var*)LangElement::find(String::ToString("blendContrast%d", idx));
  1166. if (!blendContrast)
  1167. {
  1168. blendContrast = new Var;
  1169. blendContrast->setType("float");
  1170. blendContrast->setName(String::ToString("blendContrast%d", idx));
  1171. blendContrast->uniform = true;
  1172. blendContrast->constSortPos = cspPrimitive;
  1173. }
  1174. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1175. if (!detailH)
  1176. {
  1177. detailH = new Var;
  1178. detailH->setType("float");
  1179. detailH->setName(String::ToString("detailH%d", idx));
  1180. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1181. new DecOp(detailH)));
  1182. meta->addStatement(new GenOp(" if (@ > 0.0f) {\r\n", detailBlend));
  1183. if (bumpNormal != NULL)
  1184. {
  1185. meta->addStatement(new GenOp(" @ = clamp(@.a + @, 0.0, 1.0);\r\n",
  1186. detailH, bumpNormal, blendDepth));
  1187. }
  1188. else
  1189. {
  1190. meta->addStatement(new GenOp(" @ = clamp(0.5 + @, 0.0, 1.0);\r\n",
  1191. detailH, blendDepth));
  1192. }
  1193. meta->addStatement(new GenOp(" @ = ((@ - 0.5f) * max(@, 0.0f)) + 0.5f;\r\n",
  1194. detailH, detailH, blendContrast));
  1195. meta->addStatement(new GenOp(" }\r\n"));
  1196. }
  1197. }
  1198. meta->addStatement(new GenOp("\r\n"));
  1199. Var* depth = (Var*)LangElement::find("baseBlendDepth");
  1200. if (depth == NULL)
  1201. {
  1202. depth = new Var;
  1203. depth->setType("float");
  1204. depth->setName("baseBlendDepth");
  1205. depth->uniform = true;
  1206. depth->constSortPos = cspPrimitive;
  1207. }
  1208. Var* ma = (Var*)LangElement::find("ma");
  1209. if (ma == NULL)
  1210. {
  1211. ma = new Var;
  1212. ma->setType("float");
  1213. ma->setName("ma");
  1214. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1215. new DecOp(ma)));
  1216. }
  1217. for (S32 idx = 0; idx < detailCount; ++idx)
  1218. {
  1219. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1220. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1221. meta->addStatement(new GenOp(" @ = max(@, @ + @);\r\n",
  1222. ma, ma, detailH, detailBlend));
  1223. }
  1224. meta->addStatement(new GenOp(" @ -= @;\r\n",
  1225. ma, depth));
  1226. meta->addStatement(new GenOp("\r\n"));
  1227. for (S32 idx = 0; idx < detailCount; ++idx)
  1228. {
  1229. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1230. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1231. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1232. if (!detailB)
  1233. {
  1234. detailB = new Var;
  1235. detailB->setType("float");
  1236. detailB->setName(String::ToString("detailB%d", idx));
  1237. meta->addStatement(new GenOp(" @ = max(@ + @ - @, 0);\r\n",
  1238. new DecOp(detailB), detailH, detailBlend, ma));
  1239. }
  1240. }
  1241. meta->addStatement(new GenOp("\r\n"));
  1242. // Compute albedo
  1243. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n",
  1244. outColor, outColor));
  1245. meta->addStatement(new GenOp(" @.rgb += (",
  1246. outColor));
  1247. for (S32 idx = 0; idx < detailCount; ++idx)
  1248. {
  1249. Var* detailColor = (Var*)LangElement::find(String::ToString("detailColor%d", idx));
  1250. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1251. if (idx > 0)
  1252. {
  1253. meta->addStatement(new GenOp(" + "));
  1254. }
  1255. meta->addStatement(new GenOp("@.rgb * @", detailColor, detailB));
  1256. }
  1257. meta->addStatement(new GenOp(") / ("));
  1258. for (S32 idx = 0; idx < detailCount; ++idx)
  1259. {
  1260. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1261. if (idx > 0)
  1262. {
  1263. meta->addStatement(new GenOp(" + "));
  1264. }
  1265. meta->addStatement(new GenOp("@", detailB));
  1266. }
  1267. meta->addStatement(new GenOp(");\r\n"));
  1268. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n",
  1269. outColor, outColor));
  1270. meta->addStatement(new GenOp("\r\n"));
  1271. // Compute ORM
  1272. Var* ormOutput;
  1273. OutputTarget targ = DefaultTarget;
  1274. if (fd.features[MFT_isDeferred])
  1275. {
  1276. targ = RenderTarget2;
  1277. }
  1278. ormOutput = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1279. meta->addStatement(new GenOp(" @.gba = (",
  1280. ormOutput));
  1281. for (S32 idx = 0; idx < detailCount; ++idx)
  1282. {
  1283. Var* matinfoCol = (Var*)LangElement::find(String::ToString("matinfoCol%d", idx));
  1284. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1285. if (idx > 0)
  1286. {
  1287. meta->addStatement(new GenOp(" + "));
  1288. }
  1289. if (matinfoCol)
  1290. {
  1291. meta->addStatement(new GenOp("@ * @", matinfoCol, detailB));
  1292. }
  1293. else
  1294. {
  1295. meta->addStatement(new GenOp("float3(1.0, 1.0, 0.0) * @", detailB));
  1296. }
  1297. }
  1298. meta->addStatement(new GenOp(") / ("));
  1299. for (S32 idx = 0; idx < detailCount; ++idx)
  1300. {
  1301. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1302. if (idx > 0)
  1303. {
  1304. meta->addStatement(new GenOp(" + "));
  1305. }
  1306. meta->addStatement(new GenOp("@", detailB));
  1307. }
  1308. meta->addStatement(new GenOp(");\r\n"));
  1309. meta->addStatement(new GenOp("\r\n"));
  1310. // Compute normal-specific blending factors
  1311. // LukasPJ: I'm not sure why this is necessary, it might not be.
  1312. Var* normalMa = (Var*)LangElement::find("normalMa");
  1313. if (normalMa == NULL)
  1314. {
  1315. normalMa = new Var;
  1316. normalMa->setType("float");
  1317. normalMa->setName("normalMa");
  1318. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1319. new DecOp(normalMa)));
  1320. }
  1321. for (S32 idx = 0; idx < detailCount; ++idx)
  1322. {
  1323. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1324. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1325. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1326. meta->addStatement(new GenOp(" @ = max(@, @ + min(@, @.w));\r\n",
  1327. normalMa, normalMa, detailH, detailBlend, detCoord));
  1328. }
  1329. meta->addStatement(new GenOp(" @ -= @;\r\n",
  1330. normalMa, depth));
  1331. meta->addStatement(new GenOp("\r\n"));
  1332. for (S32 idx = 0; idx < detailCount; ++idx)
  1333. {
  1334. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1335. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1336. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1337. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1338. if (!normalDetailB)
  1339. {
  1340. normalDetailB = new Var;
  1341. normalDetailB->setType("float");
  1342. normalDetailB->setName(String::ToString("normalDetailB%d", idx));
  1343. meta->addStatement(new GenOp(" @ = max(@ + min(@, @.w) - @, 0);\r\n",
  1344. new DecOp(normalDetailB), detailH, detailBlend, detCoord, normalMa));
  1345. }
  1346. }
  1347. // Compute normals
  1348. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  1349. if (!gbNormal)
  1350. {
  1351. gbNormal = new Var;
  1352. gbNormal->setName("gbNormal");
  1353. gbNormal->setType("float3");
  1354. meta->addStatement(new GenOp(" @;\r\n", new DecOp(gbNormal)));
  1355. }
  1356. if (gbNormal != NULL)
  1357. {
  1358. meta->addStatement(new GenOp(" @ = (",
  1359. gbNormal));
  1360. for (S32 idx = 0; idx < detailCount; ++idx)
  1361. {
  1362. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1363. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1364. Var* viewToTangent = getInViewToTangent(componentList);
  1365. if (idx > 0)
  1366. {
  1367. meta->addStatement(new GenOp(" + "));
  1368. }
  1369. if (bumpNormal != NULL)
  1370. {
  1371. meta->addStatement(new GenOp("mul(@.xyz, @) * @", bumpNormal, viewToTangent, normalDetailB));
  1372. }
  1373. else
  1374. {
  1375. meta->addStatement(new GenOp("@[2] * @", viewToTangent, normalDetailB));
  1376. }
  1377. }
  1378. meta->addStatement(new GenOp(") / ("));
  1379. for (S32 idx = 0; idx < detailCount; ++idx)
  1380. {
  1381. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1382. if (idx > 0)
  1383. {
  1384. meta->addStatement(new GenOp(" + "));
  1385. }
  1386. meta->addStatement(new GenOp("@", normalDetailB));
  1387. }
  1388. meta->addStatement(new GenOp(");\r\n"));
  1389. }
  1390. output = meta;
  1391. }