terrFeatureGLSL.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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/glsl/terrFeatureGLSL.h"
  24. #include "terrain/terrFeatureTypes.h"
  25. #include "materials/materialFeatureTypes.h"
  26. #include "materials/materialFeatureData.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "shaderGen/langElement.h"
  29. #include "shaderGen/shaderOp.h"
  30. #include "shaderGen/featureMgr.h"
  31. #include "core/module.h"
  32. MODULE_BEGIN( TerrainFeatGLSL )
  33. MODULE_INIT_AFTER( ShaderGenFeatureMgr )
  34. MODULE_INIT
  35. {
  36. FEATUREMGR->registerFeature( MFT_TerrainBaseMap, new TerrainBaseMapFeatGLSL );
  37. FEATUREMGR->registerFeature( MFT_TerrainParallaxMap, new TerrainParallaxMapFeatGLSL );
  38. FEATUREMGR->registerFeature( MFT_TerrainDetailMap, new TerrainDetailMapFeatGLSL );
  39. FEATUREMGR->registerFeature( MFT_TerrainNormalMap, new TerrainNormalMapFeatGLSL );
  40. FEATUREMGR->registerFeature( MFT_TerrainLightMap, new TerrainLightMapFeatGLSL );
  41. FEATUREMGR->registerFeature( MFT_TerrainSideProject, new NamedFeatureGLSL( "Terrain Side Projection" ) );
  42. FEATUREMGR->registerFeature( MFT_TerrainAdditive, new TerrainAdditiveFeatGLSL );
  43. }
  44. MODULE_END;
  45. Var* TerrainFeatGLSL::_getUniformVar( const char *name, const char *type, ConstantSortPosition csp )
  46. {
  47. Var *theVar = (Var*)LangElement::find( name );
  48. if ( !theVar )
  49. {
  50. theVar = new Var;
  51. theVar->setType( type );
  52. theVar->setName( name );
  53. theVar->uniform = true;
  54. theVar->constSortPos = csp;
  55. }
  56. return theVar;
  57. }
  58. Var* TerrainFeatGLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList )
  59. {
  60. String name( String::ToString( "outDetCoord%d", getProcessIndex() ) );
  61. Var *inDet = (Var*)LangElement::find( name );
  62. if ( !inDet )
  63. {
  64. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  65. inDet = connectComp->getElement( RT_TEXCOORD );
  66. inDet->setName( name );
  67. inDet->setType( "vec4" );
  68. inDet->mapsToSampler = true;
  69. }
  70. return inDet;
  71. }
  72. Var* TerrainFeatGLSL::_getNormalMapTex()
  73. {
  74. String name( String::ToString( "normalMap%d", getProcessIndex() ) );
  75. Var *normalMap = (Var*)LangElement::find( name );
  76. if ( !normalMap )
  77. {
  78. normalMap = new Var;
  79. normalMap->setType( "sampler2D" );
  80. normalMap->setName( name );
  81. normalMap->uniform = true;
  82. normalMap->sampler = true;
  83. normalMap->constNum = Var::getTexUnitNum();
  84. }
  85. return normalMap;
  86. }
  87. Var* TerrainFeatGLSL::_getDetailIdStrengthParallax()
  88. {
  89. String name( String::ToString( "detailIdStrengthParallax%d", getProcessIndex() ) );
  90. Var *detailInfo = (Var*)LangElement::find( name );
  91. if ( !detailInfo )
  92. {
  93. detailInfo = new Var;
  94. detailInfo->setType( "vec3" );
  95. detailInfo->setName( name );
  96. detailInfo->uniform = true;
  97. detailInfo->constSortPos = cspPotentialPrimitive;
  98. }
  99. return detailInfo;
  100. }
  101. void TerrainBaseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  102. const MaterialFeatureData &fd )
  103. {
  104. MultiLine *meta = new MultiLine;
  105. output = meta;
  106. // Generate the incoming texture var.
  107. Var *inTex;
  108. {
  109. Var *inPos = (Var*)LangElement::find( "inPosition" );
  110. if ( !inPos )
  111. inPos = (Var*)LangElement::find( "position" );
  112. inTex = new Var( "texCoord", "vec3" );
  113. Var *oneOverTerrainSize = _getUniformVar( "oneOverTerrainSize", "float", cspPass );
  114. // NOTE: The y coord here should be negative to have
  115. // the texture maps not end up flipped which also caused
  116. // normal and parallax mapping to be incorrect.
  117. //
  118. // This mistake early in development means that the layer
  119. // id bilinear blend depends on it being that way.
  120. //
  121. // So instead i fixed this by flipping the base and detail
  122. // coord y scale to compensate when rendering.
  123. //
  124. meta->addStatement( new GenOp( " @ = @.xyz * vec3( @, @, -@ );\r\n",
  125. new DecOp( inTex ), inPos, oneOverTerrainSize, oneOverTerrainSize, oneOverTerrainSize ) );
  126. }
  127. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  128. // Pass the texture coord to the pixel shader.
  129. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  130. outTex->setName( "outTexCoord" );
  131. outTex->setType( "vec3" );
  132. outTex->mapsToSampler = true;
  133. meta->addStatement( new GenOp( " @.xy = @.xy;\r\n", outTex, inTex ) );
  134. // If this shader has a side projected layer then we
  135. // pass the dot product between the +Y and the normal
  136. // thru outTexCoord.z for use in blending the textures.
  137. if ( fd.features.hasFeature( MFT_TerrainSideProject ) )
  138. {
  139. Var *inNormal = (Var*)LangElement::find( "normal" );
  140. meta->addStatement(
  141. new GenOp( " @.z = pow( abs( dot( normalize( vec3( @.x, @.y, 0.0 ) ), vec3( 0, 1, 0 ) ) ), 10.0 );\r\n",
  142. outTex, inNormal, inNormal ) );
  143. }
  144. else
  145. meta->addStatement( new GenOp( " @.z = 0;\r\n", outTex ) );
  146. // HACK: This is sort of lazy... we generate the tanget
  147. // vector here so that we're sure it exists in the parallax
  148. // and normal features which will expect "T" to exist.
  149. //
  150. // If this shader doesn't use it the shader compiler will
  151. // optimize away this code.
  152. //
  153. Var *inTangentZ = getVertTexCoord( "tcTangentZ" );
  154. Var *inTanget = new Var( "T", "vec3" );
  155. Var *squareSize = _getUniformVar( "squareSize", "float", cspPass );
  156. meta->addStatement( new GenOp( " @ = normalize( vec3( @, 0.0, @ ) );\r\n",
  157. new DecOp( inTanget ), squareSize, inTangentZ ) );
  158. }
  159. void TerrainBaseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  160. const MaterialFeatureData &fd )
  161. {
  162. // grab connector texcoord register
  163. Var *texCoord = getInTexCoord( "outTexCoord", "vec3", true, componentList );
  164. // We do nothing more if this is a prepass.
  165. if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
  166. return;
  167. // create texture var
  168. Var *diffuseMap = new Var;
  169. diffuseMap->setType( "sampler2D" );
  170. diffuseMap->setName( "baseTexMap" );
  171. diffuseMap->uniform = true;
  172. diffuseMap->sampler = true;
  173. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  174. MultiLine *meta = new MultiLine;
  175. Var *baseColor = new Var;
  176. baseColor->setType( "vec4" );
  177. baseColor->setName( "baseColor" );
  178. meta->addStatement( new GenOp( " @ = texture2D( @, @.xy );\r\n", new DecOp( baseColor ), diffuseMap, texCoord ) );
  179. meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul ) ) );
  180. output = meta;
  181. }
  182. ShaderFeature::Resources TerrainBaseMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  183. {
  184. Resources res;
  185. res.numTexReg = 1;
  186. // We only sample from the base map during a diffuse pass.
  187. if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
  188. res.numTex = 1;
  189. return res;
  190. }
  191. TerrainDetailMapFeatGLSL::TerrainDetailMapFeatGLSL()
  192. : mTerrainDep( "shaders/common/terrain/terrain.glsl" )
  193. {
  194. addDependency( &mTerrainDep );
  195. }
  196. void TerrainDetailMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  197. const MaterialFeatureData &fd )
  198. {
  199. const U32 detailIndex = getProcessIndex();
  200. // If this is a prepass and we don't have a
  201. // matching normal map... we have nothing to do.
  202. if ( fd.features.hasFeature( MFT_PrePassConditioner ) &&
  203. !fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) )
  204. return;
  205. // Grab incoming texture coords... the base map feature
  206. // made sure this was created.
  207. Var *inTex = (Var*)LangElement::find( "texCoord" );
  208. AssertFatal( inTex, "The texture coord is missing!" );
  209. // Grab the input position.
  210. Var *inPos = (Var*)LangElement::find( "inPosition" );
  211. if ( !inPos )
  212. inPos = (Var*)LangElement::find( "position" );
  213. // Get the object space eye position.
  214. Var *eyePos = _getUniformVar( "eyePos", "vec3", cspPotentialPrimitive );
  215. MultiLine *meta = new MultiLine;
  216. // Get the distance from the eye to this vertex.
  217. Var *dist = (Var*)LangElement::find( "dist" );
  218. if ( !dist )
  219. {
  220. dist = new Var;
  221. dist->setType( "float" );
  222. dist->setName( "dist" );
  223. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  224. new DecOp( dist ), inPos, eyePos ) );
  225. }
  226. // grab connector texcoord register
  227. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  228. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  229. outTex->setName( String::ToString( "outDetCoord%d", detailIndex ) );
  230. outTex->setType( "vec4" );
  231. outTex->mapsToSampler = true;
  232. // Get the detail scale and fade info.
  233. Var *detScaleAndFade = new Var;
  234. detScaleAndFade->setType( "vec4" );
  235. detScaleAndFade->setName( String::ToString( "detailScaleAndFade%d", detailIndex ) );
  236. detScaleAndFade->uniform = true;
  237. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  238. // Setup the detail coord.
  239. //
  240. // NOTE: You see here we scale the texture coord by 'xyx'
  241. // to generate the detail coord. This y is here because
  242. // its scale is flipped to correct for the non negative y
  243. // in texCoord.
  244. //
  245. // See TerrainBaseMapFeatHLSL::processVert().
  246. //
  247. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) );
  248. // And sneak the detail fade thru the w detailCoord.
  249. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  250. outTex, detScaleAndFade, dist, detScaleAndFade ) );
  251. output = meta;
  252. }
  253. void TerrainDetailMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  254. const MaterialFeatureData &fd )
  255. {
  256. const U32 detailIndex = getProcessIndex();
  257. // If this is a prepass and we don't have a
  258. // matching normal map... we have nothing to do.
  259. if ( fd.features.hasFeature( MFT_PrePassConditioner ) &&
  260. !fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) )
  261. return;
  262. Var *inTex = getVertTexCoord( "outTexCoord" );
  263. MultiLine *meta = new MultiLine;
  264. // Get the layer samples.
  265. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  266. if ( !layerSample )
  267. {
  268. layerSample = new Var;
  269. layerSample->setType( "vec4" );
  270. layerSample->setName( "layerSample" );
  271. // Get the layer texture var
  272. Var *layerTex = new Var;
  273. layerTex->setType( "sampler2D" );
  274. layerTex->setName( "layerTex" );
  275. layerTex->uniform = true;
  276. layerTex->sampler = true;
  277. layerTex->constNum = Var::getTexUnitNum();
  278. // Read the layer texture to get the samples.
  279. meta->addStatement( new GenOp( " @ = round( texture2D( @, @.xy ) * 255.0f );\r\n",
  280. new DecOp( layerSample ), layerTex, inTex ) );
  281. }
  282. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  283. if ( !layerSize )
  284. {
  285. layerSize = new Var;
  286. layerSize->setType( "float" );
  287. layerSize->setName( "layerSize" );
  288. layerSize->uniform = true;
  289. layerSize->constSortPos = cspPass;
  290. }
  291. // Grab the incoming detail coord.
  292. Var *inDet = _getInDetailCoord( componentList );
  293. // Get the detail id.
  294. Var *detailInfo = _getDetailIdStrengthParallax();
  295. // Create the detail blend var.
  296. Var *detailBlend = new Var;
  297. detailBlend->setType( "float" );
  298. detailBlend->setName( String::ToString( "detailBlend%d", detailIndex ) );
  299. // Calculate the blend for this detail texture.
  300. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  301. new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
  302. // Get a var and accumulate the blend amount.
  303. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  304. if ( !blendTotal )
  305. {
  306. blendTotal = new Var;
  307. blendTotal->setName( "blendTotal" );
  308. blendTotal->setType( "float" );
  309. meta->addStatement( new GenOp( " @ = 0.0;\r\n", new DecOp( blendTotal ) ) );
  310. }
  311. // Add to the blend total.
  312. meta->addStatement( new GenOp( " @ += @;\r\n", blendTotal, detailBlend ) );
  313. //meta->addStatement( new GenOp( " @ += @ * @.y * @.w;\r\n",
  314. //blendTotal, detailBlend, detailInfo, inDet ) );
  315. // Nothing more to do for a detail texture in prepass.
  316. if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
  317. {
  318. output = meta;
  319. return;
  320. }
  321. Var *detailColor = (Var*)LangElement::find( "detailColor" );
  322. if ( !detailColor )
  323. {
  324. detailColor = new Var;
  325. detailColor->setType( "vec4" );
  326. detailColor->setName( "detailColor" );
  327. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  328. }
  329. // Get the detail texture.
  330. Var *detailMap = new Var;
  331. detailMap->setType( "sampler2D" );
  332. detailMap->setName( String::ToString( "detailMap%d", detailIndex ) );
  333. detailMap->uniform = true;
  334. detailMap->sampler = true;
  335. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  336. // If we're using SM 3.0 then take advantage of
  337. // dynamic branching to skip layers per-pixel.
  338. if ( GFX->getPixelShaderVersion() >= 3.0f )
  339. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  340. meta->addStatement( new GenOp( " {\r\n" ) );
  341. // Note that we're doing the standard greyscale detail
  342. // map technique here which can darken and lighten the
  343. // diffuse texture.
  344. //
  345. // We take two color samples and lerp between them for
  346. // side projection layers... else a single sample.
  347. //
  348. if ( fd.features.hasFeature( MFT_TerrainSideProject, detailIndex ) )
  349. {
  350. meta->addStatement( new GenOp( " @ = ( mix( texture2D( @, @.yz ), texture2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
  351. detailColor, detailMap, inDet, detailMap, inDet, inTex ) );
  352. }
  353. else
  354. {
  355. meta->addStatement( new GenOp( " @ = ( texture2D( @, @.xy ) * 2.0 ) - 1.0;\r\n",
  356. detailColor, detailMap, inDet ) );
  357. }
  358. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  359. detailColor, detailInfo, inDet ) );
  360. Var *baseColor = (Var*)LangElement::find( "baseColor" );
  361. Var *outColor = (Var*)LangElement::find( "col" );
  362. meta->addStatement( new GenOp( " @ = mix( @, @ + @, @ );\r\n",
  363. outColor, outColor, baseColor, detailColor, detailBlend ) );
  364. meta->addStatement( new GenOp( " }\r\n" ) );
  365. output = meta;
  366. }
  367. ShaderFeature::Resources TerrainDetailMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  368. {
  369. Resources res;
  370. if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
  371. {
  372. // If this is a prepass and we don't have a
  373. // matching normal map... we use no resources.
  374. if ( !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() ) )
  375. return res;
  376. // If this is the first matching normal map then
  377. // it also samples from the layer tex.
  378. if ( !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() - 1 ) )
  379. res.numTex += 1;
  380. }
  381. else
  382. {
  383. // If this is the first detail pass then it
  384. // also samples from the layer tex.
  385. if ( !fd.features.hasFeature( MFT_TerrainDetailMap, getProcessIndex() - 1 ) )
  386. res.numTex += 1;
  387. res.numTex += 1;
  388. }
  389. res.numTexReg += 1;
  390. return res;
  391. }
  392. void TerrainNormalMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  393. const MaterialFeatureData &fd )
  394. {
  395. // We only need to process normals during the prepass.
  396. if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
  397. return;
  398. MultiLine *meta = new MultiLine;
  399. // Make sure the world to tangent transform
  400. // is created and available for the pixel shader.
  401. getOutViewToTangent( componentList, meta, fd );
  402. output = meta;
  403. }
  404. void TerrainNormalMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  405. const MaterialFeatureData &fd )
  406. {
  407. // We only need to process normals during the prepass.
  408. if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
  409. return;
  410. MultiLine *meta = new MultiLine;
  411. Var *viewToTangent = getInViewToTangent( componentList );
  412. // This var is read from GBufferConditionerGLSL and
  413. // used in the prepass output.
  414. Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
  415. if ( !gbNormal )
  416. {
  417. gbNormal = new Var;
  418. gbNormal->setName( "gbNormal" );
  419. gbNormal->setType( "vec3" );
  420. meta->addStatement( new GenOp( " @ = @[2];\r\n", new DecOp( gbNormal ), viewToTangent ) );
  421. }
  422. const U32 normalIndex = getProcessIndex();
  423. Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
  424. AssertFatal( detailBlend, "The detail blend is missing!" );
  425. // If we're using SM 3.0 then take advantage of
  426. // dynamic branching to skip layers per-pixel.
  427. if ( GFX->getPixelShaderVersion() >= 3.0f )
  428. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  429. meta->addStatement( new GenOp( " {\r\n" ) );
  430. // Get the normal map texture.
  431. Var *normalMap = _getNormalMapTex();
  432. /// Get the texture coord.
  433. Var *inDet = _getInDetailCoord( componentList );
  434. Var *inTex = getVertTexCoord( "outTexCoord" );
  435. // Sample the normal map.
  436. //
  437. // We take two normal samples and lerp between them for
  438. // side projection layers... else a single sample.
  439. LangElement *texOp;
  440. if ( fd.features.hasFeature( MFT_TerrainSideProject, normalIndex ) )
  441. {
  442. texOp = new GenOp( "mix( texture2D( @, @.yz ), texture2D( @, @.xz ), @.z )",
  443. normalMap, inDet, normalMap, inDet, inTex );
  444. }
  445. else
  446. texOp = new GenOp( "texture2D(@, @.xy)", normalMap, inDet );
  447. // create bump normal
  448. Var *bumpNorm = new Var;
  449. bumpNorm->setName( "bumpNormal" );
  450. bumpNorm->setType( "vec4" );
  451. LangElement *bumpNormDecl = new DecOp( bumpNorm );
  452. meta->addStatement( expandNormalMap( texOp, bumpNormDecl, bumpNorm, fd ) );
  453. // Normalize is done later...
  454. // Note: The reverse mul order is intentional. Affine matrix.
  455. meta->addStatement( new GenOp( " @ = mix( @, @.xyz * @, min( @, @.w ) );\r\n",
  456. gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet ) );
  457. // End the conditional block.
  458. meta->addStatement( new GenOp( " }\r\n" ) );
  459. // If this is the last normal map then we
  460. // can test to see the total blend value
  461. // to see if we should clip the result.
  462. //if ( fd.features.getNextFeatureIndex( MFT_TerrainNormalMap, normalIndex ) == -1 )
  463. //meta->addStatement( new GenOp( " clip( @ - 0.0001f );\r\n", blendTotal ) );
  464. output = meta;
  465. }
  466. ShaderFeature::Resources TerrainNormalMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  467. {
  468. Resources res;
  469. // We only need to process normals during the prepass.
  470. if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
  471. {
  472. // If this is the first normal map then it
  473. // will generate the worldToTanget transform.
  474. if ( !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() - 1 ) )
  475. res.numTexReg = 3;
  476. res.numTex = 1;
  477. }
  478. return res;
  479. }
  480. TerrainParallaxMapFeatGLSL::TerrainParallaxMapFeatGLSL()
  481. : mIncludeDep( "shaders/common/gl/torque.glsl" )
  482. {
  483. addDependency( &mIncludeDep );
  484. }
  485. void TerrainParallaxMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  486. const MaterialFeatureData &fd )
  487. {
  488. if ( LangElement::find( "outNegViewTS" ) )
  489. return;
  490. MultiLine *meta = new MultiLine;
  491. // Grab the input position.
  492. Var *inPos = (Var*)LangElement::find( "inPosition" );
  493. if ( !inPos )
  494. inPos = (Var*)LangElement::find( "position" );
  495. // Get the object space eye position and the
  496. // object to tangent transform.
  497. Var *eyePos = _getUniformVar( "eyePos", "vec3" , cspPotentialPrimitive );
  498. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta,fd );
  499. // Now send the negative view vector in tangent space to the pixel shader.
  500. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  501. Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
  502. outNegViewTS->setName( "outNegViewTS" );
  503. outNegViewTS->setType( "vec3" );
  504. meta->addStatement( new GenOp( " @ = @ * vec3( @ - @.xyz );\r\n",
  505. outNegViewTS, objToTangentSpace, eyePos, inPos ) );
  506. output = meta;
  507. }
  508. void TerrainParallaxMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  509. const MaterialFeatureData &fd )
  510. {
  511. MultiLine *meta = new MultiLine;
  512. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  513. // We need the negative tangent space view vector
  514. // as in parallax mapping we step towards the camera.
  515. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  516. if ( !negViewTS )
  517. {
  518. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  519. if ( !inNegViewTS )
  520. {
  521. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  522. inNegViewTS->setName( "outNegViewTS" );
  523. inNegViewTS->setType( "vec3" );
  524. }
  525. negViewTS = new Var( "negViewTS", "vec3" );
  526. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  527. }
  528. // Get the rest of our inputs.
  529. Var *detailInfo = _getDetailIdStrengthParallax();
  530. Var *normalMap = _getNormalMapTex();
  531. Var *texCoord = _getInDetailCoord( componentList );
  532. // Call the library function to do the rest.
  533. meta->addStatement( new GenOp( " @.xy += parallaxOffset( @, @.xy, @, @.z );\r\n",
  534. texCoord, normalMap, texCoord, negViewTS, detailInfo ) );
  535. output = meta;
  536. }
  537. ShaderFeature::Resources TerrainParallaxMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  538. {
  539. Resources res;
  540. // If this is the first parallax feature then
  541. // it will generate the tangetEye vector and
  542. // the worldToTanget transform.
  543. if ( getProcessIndex() == 0 || !fd.features.hasFeature( MFT_TerrainParallaxMap, getProcessIndex() - 1 ) )
  544. res.numTexReg = 4;
  545. // If this isn't the prepass then we will
  546. // be adding a normal map.
  547. if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
  548. res.numTex = 1;
  549. return res;
  550. }
  551. void TerrainLightMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  552. const MaterialFeatureData &fd )
  553. {
  554. // grab connector texcoord register
  555. Var *inTex = (Var*)LangElement::find( "outTexCoord" );
  556. if ( !inTex )
  557. return;
  558. // Get the lightmap texture.
  559. Var *lightMap = new Var;
  560. lightMap->setType( "sampler2D" );
  561. lightMap->setName( "lightMapTex" );
  562. lightMap->uniform = true;
  563. lightMap->sampler = true;
  564. lightMap->constNum = Var::getTexUnitNum();
  565. // Create a 'lightMask' value which is read by
  566. // RTLighting to mask out the directional lighting.
  567. Var *lightMask = new Var;
  568. lightMask->setType( "vec3" );
  569. lightMask->setName( "lightMask" );
  570. output = new GenOp( " @ = texture2D( @, @.xy ).rgb;\r\n", new DecOp( lightMask ), lightMap, inTex );
  571. }
  572. ShaderFeature::Resources TerrainLightMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  573. {
  574. Resources res;
  575. res.numTex = 1;
  576. return res;
  577. }
  578. void TerrainAdditiveFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  579. const MaterialFeatureData &fd )
  580. {
  581. Var *color = (Var*) LangElement::find( "col" );
  582. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  583. if ( !color || !blendTotal )
  584. return;
  585. MultiLine *meta = new MultiLine;
  586. meta->addStatement( new GenOp( " if ( @ - 0.0001 < 0.0 ) discard;\r\n", blendTotal ) );
  587. meta->addStatement( new GenOp( " @.a = @;\r\n", color, blendTotal ) );
  588. output = meta;
  589. }