Example #1
0
void BumpFeatHLSL::processPix(   Vector<ShaderComponent*> &componentList, 
                                 const MaterialFeatureData &fd )
{
   MultiLine *meta = new MultiLine;

   // Get the texture coord.
   Var *texCoord = getInTexCoord( "texCoord", "float2", true, componentList );

   // Sample the bumpmap.
   Var *bumpMap = getNormalMapTex();
   LangElement *texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
   Var *bumpNorm = new Var( "bumpNormal", "float4" );
   meta->addStatement( expandNormalMap( texOp, new DecOp( bumpNorm ), bumpNorm, fd ) );

   // We transform it into world space by reversing the 
   // multiplication by the worldToTanget transform.
   Var *wsNormal = new Var( "wsNormal", "float3" );
   Var *worldToTanget = getInWorldToTangent( componentList );
   meta->addStatement( new GenOp( "   @ = normalize( mul( @.xyz, @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) );

   // TODO: Restore this!
   /*
   // Check to see if we're rendering world space normals.
   if ( fd.materialFeatures[MFT_NormalsOut]  )
   {
      Var *inNormal = getInTexCoord( "normal", "float3", false, componentList );

      LangElement *normalOut;
      Var *outColor = (Var*)LangElement::find( "col" );
      if ( outColor )
         normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, @.a )", inNormal, outColor );
      else
         normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, 1 )", inNormal );

      meta->addStatement( new GenOp( "   @; // MFT_NormalsOut\r\n", 
         assignColor( normalOut, Material::None ) ) );
         
      output = meta;
      return;
   }
   */

   output = meta;
}
Example #2
0
void BumpFeatGLSL::processPix(   Vector<ShaderComponent*> &componentList, 
                                 const MaterialFeatureData &fd )
{
   MultiLine *meta = new MultiLine;
	output = meta;

   // Get the texture coord.
   Var *texCoord = getInTexCoord( "texCoord", "vec2", true, componentList );

   // Sample the bumpmap.
   Var *bumpMap = getNormalMapTex();
	LangElement *texOp = NULL;
	
	//Handle atlased textures
   // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
	if(fd.features[MFT_NormalMapAtlas])
   {
		// This is a big block of code, so put a comment in the shader code
      meta->addStatement( new GenOp( "   // Atlased texture coordinate calculation (see BumpFeat*LSL for details)\r\n") );
		
      Var *atlasedTex = new Var;
      atlasedTex->setName("atlasedBumpCoord");
      atlasedTex->setType( "vec2" );
      LangElement *atDecl = new DecOp(atlasedTex);
		
      // Parameters of the texture atlas
      Var *atParams  = new Var;
      atParams->setType( "float4" );
      atParams->setName("bumpAtlasParams");
      atParams->uniform = true;
      atParams->constSortPos = cspPotentialPrimitive;
		
      // Parameters of the texture (tile) this object is using in the atlas
      Var *tileParams  = new Var;
      tileParams->setType( "float4" );
      tileParams->setName("bumpAtlasTileParams");
      tileParams->uniform = true;
      tileParams->constSortPos = cspPotentialPrimitive;
		
		const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
		if(is_sm3)
      {
         // Figure out the mip level
         meta->addStatement( new GenOp( "   float2 _dx_bump = ddx(@ * @.z);\r\n", texCoord, atParams ) );
         meta->addStatement( new GenOp( "   float2 _dy_bump = ddy(@ * @.z);\r\n", texCoord, atParams ) );
         meta->addStatement( new GenOp( "   float mipLod_bump = 0.5 * log2(max(dot(_dx_bump, _dx_bump), dot(_dy_bump, _dy_bump)));\r\n"));
         meta->addStatement( new GenOp( "   mipLod_bump = clamp(mipLod_bump, 0.0, @.w);\r\n", atParams));
			
         // And the size of the mip level
         meta->addStatement(new GenOp("   float mipPixSz_bump = pow(2.0, @.w - mipLod_bump);\r\n", atParams));
         meta->addStatement( new GenOp( "   float2 mipSz_bump = mipPixSz_bump / @.xy;\r\n", atParams ) );
      }
      else
      {
         meta->addStatement(new GenOp("   float2 mipSz = float2(1.0, 1.0);\r\n"));
      }
		
		// Tiling mode
      if( true ) // Wrap
         meta->addStatement( new GenOp( "   @ = frac(@);\r\n", atDecl, texCoord ) );
      else       // Clamp
         meta->addStatement(new GenOp("   @ = saturate(@);\r\n", atDecl, texCoord));
		
      // Finally scale/offset, and correct for filtering
      meta->addStatement( new GenOp( "   @ = @ * ((mipSz_bump * @.xy - 1.0) / mipSz_bump) + 0.5 / mipSz_bump + @.xy * @.xy;\r\n", 
											  atlasedTex, atlasedTex, atParams, atParams, tileParams));
		
      // Add a newline
      meta->addStatement(new GenOp( "\r\n"));
		
      if(is_sm3)
		{
         texOp = new GenOp( "tex2Dlod(@, float4(@, 0.0, mipLod_bump))", bumpMap, texCoord );
		}
		else
		{
         texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
		}
	}
	else
	{
      texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
	}
		
   Var *bumpNorm = new Var( "bumpNormal", "float4" );
   meta->addStatement( expandNormalMap( texOp, new DecOp( bumpNorm ), bumpNorm, fd ) );

	// If we have a detail normal map we add the xy coords of
   // it to the base normal map.  This gives us the effect we
   // want with few instructions and minial artifacts.
   if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
   {
      bumpMap = new Var;
      bumpMap->setType( "sampler2D" );
      bumpMap->setName( "detailBumpMap" );
      bumpMap->uniform = true;
      bumpMap->sampler = true;
      bumpMap->constNum = Var::getTexUnitNum();
		
      texCoord = getInTexCoord( "detCoord", "vec2", true, componentList );
      texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
		
      Var *detailBump = new Var;
      detailBump->setName( "detailBump" );
      detailBump->setType( "float4" );
      meta->addStatement( expandNormalMap( texOp, new DecOp( detailBump ), detailBump, fd ) );
		
      Var *detailBumpScale = new Var;
      detailBumpScale->setType( "float" );
      detailBumpScale->setName( "detailBumpStrength" );
      detailBumpScale->uniform = true;
      detailBumpScale->constSortPos = cspPass;
      meta->addStatement( new GenOp( "   @.xy += @.xy * @;\r\n", bumpNorm, detailBump, detailBumpScale ) );
   }
	
   // We transform it into world space by reversing the 
   // multiplication by the worldToTanget transform.
   Var *wsNormal = new Var( "wsNormal", "vec3" );
   Var *worldToTanget = getInWorldToTangent( componentList );
   meta->addStatement( new GenOp( "   @ = normalize( tMul( @.xyz, @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) );
}