Skip to content

Commit

Permalink
fix triplanar (yx swapped), 2nd triplanar layer possible
Browse files Browse the repository at this point in the history
  • Loading branch information
cryham committed Mar 6, 2014
1 parent d13e391 commit 449c988
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
26 changes: 15 additions & 11 deletions data/materials/terrain.shader
Expand Up @@ -30,12 +30,15 @@
#define PARALLAX_SCALE 0.03
#define PARALLAX_BIAS -0.04

#define TRIPLANAR_TYPE @shGlobalSettingString(terrain_triplanarType)
#define TRIPLANAR_FULL (TRIPLANAR_TYPE == 2)
#define TRIPLANAR_1 (TRIPLANAR_TYPE == 1)
#define TRIPLANAR (TRIPLANAR_TYPE) && !RENDER_COMPOSITE_MAP
// 1 layer triplanar only
#define TRIPLANAR_LAYER @shGlobalSettingString(terrain_triplanarLayer)
/// triplanar
#define TRIPLANAR_TYPE @shGlobalSettingString(terrain_triplanarType)
#define TRIPLANAR_FULL (TRIPLANAR_TYPE == 2)
#define TRIPLANAR_1 (TRIPLANAR_TYPE == 1)
#define TRIPLANAR (TRIPLANAR_TYPE) && !RENDER_COMPOSITE_MAP
// 1 layer only
#define TRIPLANAR_LAYER @shGlobalSettingString(terrain_triplanarLayer)
// 2 layers only
#define TRIPLANAR_LAYER2 @shGlobalSettingString(terrain_triplanarLayer2)

#if (MRT) || (FOG) || (SHADOWS)
#define NEED_DEPTH 1
Expand Down Expand Up @@ -340,12 +343,13 @@
///---------------------------------------------------------------------------------------------
#if TRIPLANAR

#if (TRIPLANAR_FULL) || (TRIPLANAR_LAYER == @shIterator)
#if (TRIPLANAR_FULL) || (TRIPLANAR_LAYER == @shIterator) || (TRIPLANAR_LAYER2 == @shIterator)
/// triplanar on all or on this layer

coord1 = wPos.yz * uvMul@shPropertyString(uv_component_@shIterator);
coord2 = wPos.zx * uvMul@shPropertyString(uv_component_@shIterator);
coord3 = wPos.xy * uvMul@shPropertyString(uv_component_@shIterator);
coord3.x *= -1.f;

// parallax
#if PARALLAX_MAPPING
Expand All @@ -355,17 +359,17 @@
#endif

// Sample color maps for each projection, at those UV coords.
col1 = shSample(diffuseMap@shIterator, coord1);
if (blend_weights.y > 0) col2 = shSample(diffuseMap@shIterator, coord2);
col1 = shSample(diffuseMap@shIterator, coord1.yx);
if (blend_weights.y > 0) col2 = shSample(diffuseMap@shIterator, coord2.yx);
if (blend_weights.z > 0) col3 = shSample(diffuseMap@shIterator, coord3);

// Finally, blend the results of the 3 planar projections.
diffuseSpec = col1.xyzw * blend_weights.xxxx + col2.xyzw * blend_weights.yyyy + col3.xyzw * blend_weights.zzzz;

// normal
#if NORMAL_MAPPING
col1 = shSample(normalMap@shIterator, coord1) * 2 - 1;
if (blend_weights.y > 0) col2 = shSample(normalMap@shIterator, coord2) * 2 - 1;
col1 = shSample(normalMap@shIterator, coord1.yx) * 2 - 1;
if (blend_weights.y > 0) col2 = shSample(normalMap@shIterator, coord2.yx) * 2 - 1;
if (blend_weights.z > 0) col3 = shSample(normalMap@shIterator, coord3) * 2 - 1;
TSnormal = normalize(col1.xyz * blend_weights.xxx + col2.xyz * blend_weights.yyy + col3.xyz * blend_weights.zzz);
#endif
Expand Down
3 changes: 2 additions & 1 deletion source/ogre/common/Shadows.cpp
Expand Up @@ -135,7 +135,8 @@ void CScene::changeShadows()
mFactory->setGlobalSetting("terrain_normal", b2s(pSet->ter_mtr >= 2));
mFactory->setGlobalSetting("terrain_parallax", b2s(pSet->ter_mtr >= 3));
mFactory->setGlobalSetting("terrain_triplanarType", toStr(pSet->ter_tripl));
mFactory->setGlobalSetting("terrain_triplanarLayer", toStr(sc->td.triplanar1Layer));
mFactory->setGlobalSetting("terrain_triplanarLayer", toStr(sc->td.triplanarLayer1));
mFactory->setGlobalSetting("terrain_triplanarLayer2", toStr(sc->td.triplanarLayer2));

mFactory->setGlobalSetting("water_reflect", b2s(pSet->water_reflect));
mFactory->setGlobalSetting("water_refract", b2s(pSet->water_refract));
Expand Down
3 changes: 2 additions & 1 deletion source/ogre/common/Sky.cpp
Expand Up @@ -200,7 +200,8 @@ void App::SetFactoryDefaults()
fct.setGlobalSetting("terrain_normal", b2s(pSet->ter_mtr >= 2));
fct.setGlobalSetting("terrain_parallax", b2s(pSet->ter_mtr >= 3));
fct.setGlobalSetting("terrain_triplanarType", toStr(pSet->ter_tripl));
fct.setGlobalSetting("terrain_triplanarLayer", toStr(scn->sc->td.triplanar1Layer));
fct.setGlobalSetting("terrain_triplanarLayer", toStr(scn->sc->td.triplanarLayer1));
fct.setGlobalSetting("terrain_triplanarLayer2", toStr(scn->sc->td.triplanarLayer2));

fct.setGlobalSetting("water_reflect", b2s(pSet->water_reflect));
fct.setGlobalSetting("water_refract", b2s(pSet->water_refract));
Expand Down
11 changes: 8 additions & 3 deletions source/ogre/common/data/SceneXml.cpp
Expand Up @@ -735,7 +735,7 @@ void TerData::Default()
{
iVertsX = 512*2 +1;
fTriangleSize = 1.f; // scale
triplanar1Layer = 8; // off
triplanarLayer1 = 8; triplanarLayer2 = 8; // off
errorNorm = 1.7;
normScale = 1.f;

Expand Down Expand Up @@ -777,12 +777,17 @@ void TerData::UpdVals()
//------------------------------------------
void TerData::UpdLayers()
{
layers.clear(); int li = 0; triplanar1Layer = 8;
layers.clear(); int li = 0;
triplanarLayer1 = 8; triplanarLayer2 = 8;
for (int i=0; i < ciNumLay; ++i)
{
if (layersAll[i].on)
{
if (layersAll[i].triplanar) triplanar1Layer = li;
if (layersAll[i].triplanar)
if (triplanarLayer1 < 8)
triplanarLayer2 = li;
else
triplanarLayer1 = li;
++li;
layers.push_back(i);
}
Expand Down
2 changes: 1 addition & 1 deletion source/ogre/common/data/SceneXml.h
Expand Up @@ -54,7 +54,7 @@ class TerData /// Terrain
TerLayer layerRoad; // road[4]+pipe[4] //todo...
void UpdLayers();

int triplanar1Layer; // which should have triplanar most (eg high mountains)
int triplanarLayer1, triplanarLayer2; // which should have triplanar most (eg high mountains)
float normScale; // scale terrain normals

// methods
Expand Down

0 comments on commit 449c988

Please sign in to comment.