Materials

From Ogre Wiki

Jump to: navigation, search

Contents

Materials

The best way to get information on materials is starting to read the manual. Material Scripts

Attached is a collection of material templates that can be used as the launching pad for your own OGRE projects. Programmatic materials - e.g. materials created systematically via C++ code - can be found here.


Get Syntax highlighting for Material files using Crimson Editor.

Basic

Simple image map

The simplest material - apply an image to a mesh.

Material Requirements

  • image.png - Texture

Material Template

material Template/texture_map
{
   technique
   {
      pass
       {
         texture_unit
         {
           texture image.png
           scale 0.1 0.1
           // this means that the texture will be tiled 10 times (1/10 = 0.1)
         }
      }   
   }
}

Solid colour

Use a script like this to create a solid coloured material:

Material Template

material Template/Red
 {
   technique
   {
     pass
     {
       texture_unit
       {
         colour_op_ex source1 src_manual src_current 1 0 0
       }
     } 
   }
 }

'source1' uses the first source without modifications. In this case, the first source is the very next argument. 'src_manual' means to use the colour denoted by the three numbers at the end. The numbers represent RGB values and can be between 0 and 1. 'src_current' is the texture_unit that is being blended with 'src_manual', but in this case, since the operation is to use the first source unchanged, the resulting texture is simply the solid color as denoted by '1 0 0'.

Transparent colour

Use a script like this to create a transparent colour material:

Material Template

material Template/Red50
{
  technique
  {
    pass
    {
      scene_blend alpha_blend
      depth_write off
      
      texture_unit
      {
        colour_op_ex source1 src_manual src_current 1 0 0
        alpha_op_ex source1 src_manual src_current 0.5
      }
    }
  }
}

The 'colour_op_ex' line is explained above. The 'alpha_op_ex' has the same argument structure, the only difference is that the value at the end specifies the alpha value to use. Here, the colour is being set to 50% transparent. You can experiment with different arguments for scene_blend to give the material different kinds of transparency.

Simple texture transition

Suppose you have three quads representing a ground plane. Quad A you want to be dirt, Quad B you want to be a combination of dirt and grass, and finally quad C is grass. Here is a simple material for Quad B that does not require external creation of a dirt/grass texture.

Material Requirements

  • the blending file must be in 8 bit format (24 bit doesn't work)
  • be shure that you really save the following images itself (and not the target page)
  • Material_grass.png - Source texture

Image:Material_grass.png

  • Material_dirt.jpg - Desination texture

Image:Material_dirt.jpg

  • Material_alpha_blend.png - A PNG file that has the alpha mask we want to borrow for the transition.

Image:Material_alpha_blend.png

The Rule of blending image: The black (or color) area will be the source texture, and the transparent area will be destination texture

Material Template

material Template/texture_blend
{
   technique
   {
      pass
      {
         texture_unit
         {
           texture Material_grass.png
         }
         texture_unit
         {
            texture Material_alpha_blend.png             
            colour_op alpha_blend
         }
         texture_unit
         {
            texture Material_dirt.jpg
            colour_op_ex blend_current_alpha src_texture src_current
         }
      }
   }
}

The result

Image:Result.jpg

Pro: Easy, no programming required
Con: You have to build a new transition material for each transition you would like.
Advanced Tip: See Alpha Splatting.


Dynamic texture transition

You can programatically control the amount of grass/dirt shown with some minor adjustments to the above example.

Dynamic Material Template

material dynamic_texture_blend
{
   technique
   {
      pass
      {
         texture_unit
         {
           texture Material_grass.png
         }
      }
      pass
      {
         scene_blend alpha_blend
         texture_unit
         {
            texture Material_alpha_blend.png             
            //The alpha_op for this texunit is programatically set, see below
            //alpha_op_ex modulate src_manual src_texture SOME_NUMBER
         }
         texture_unit
         {
            texture Material_dirt.jpg
            colour_op_ex blend_current_alpha src_texture src_current
         }
      }
   }
}

Then in your source code:

Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().getByName("dynamic_texture_blend");
Ogre::TextureUnitState* ptus = materialPtr->getTechnique(0)->getPass(1)->getTextureUnitState(0); //2nd pass, first texture unit
ptus->setAlphaOperation(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_TEXTURE, your_transparency_level_varaible_goes_here);

Stained Glass Window

Suppose you have a quad, and you want it to be partially transparent, much like a stained glass window.

Material Requirements

  • image.png - Texture you want to be transparent

Material Template

material Template/TransparentTexture
{
  technique
  {
    pass
    {
      lighting off
      scene_blend alpha_blend
      depth_write off
      
      texture_unit
      {
        texture image.png
        alpha_op_ex source1 src_manual src_texture 0.5	  
      }
    }
  }
}

Transparent map

Suppose you have a alpha-masked texture of a tree, and you want to apply this texture to a quad.

Material Requirements

  • image.png - An image with an alpha layer that dictates what part of the image should be visible (and by how much)

Material Template

material cutout
{
	technique
	{
		pass
		{
			lighting off

			ambient 1 1 1 1
			diffuse 1 1 1 1
			specular 0 0 0 0
			emissive 0 0 0

			scene_blend alpha_blend
			depth_write off

			texture_unit
			{
				texture image.png
				tex_coord_set 0
				colour_op modulate
			}
		}
	}
}

Note: This material script was produced by the maya6.0e exporter. It may contain more information than is necessary. However, If we use this script for many object nearby each other, we will get problem like this Blending scene problem. So, this is the script we can use if we want to make many object with transparent near each other like a forest.

Material Template

material cutout
{
	technique
	{
		pass
		{
			lighting off

			ambient 1 1 1 1
			diffuse 1 1 1 1
			specular 0 0 0 0
			emissive 0 0 0

			alpha_rejection greater 128 // by increase this number, we will get more transparent objects
			depth_write on

			texture_unit
			{
				texture image.png
				tex_coord_set 0
				colour_op modulate
			}
		}
	}
}

Chroma Key

Quoted from OGRE MVP Christian "DWORD" Larsen: "It's not possible really, unless you use shaders or modify your textures dynamically. You're better off saving your textures in a format containing an alpha channel."

Quoted from OGRE Project Lead sinbad: "Colour keying went out with palettised textures and other dinosaur techniques Wink Alpha channels are in." HAve a look at thisforum thread.

Alternatively, you could use this function: Creating transparency based on a key colour in code

Old Chroma key info below:
Suppose you have a texture with no alpha-layer and you want to make all pixels of a given color (be it green, blue, etc) transparent in the texture.

Material Requirements

  • An RGB color - say (0,255,0) for green
  • image.png - An image with the RGB color applied as the alpha mask

Material Template

TO DO

Porcelain and Steel shader

This shader can be used to made porcelain or steel objects based on environment maps. First step, create an environment map. That is the map that your object will reflect.

Image:Boncau_env.jpg

And this is the shader:

   material porcelainshad
   {
       technique
       {
           pass
           {
               ambient 1.0 1.0 1.0
               diffuse 1.0 1.0 1.0
               specular 1.0 1.0 1.0 9800.0
               emissive 0.0 0.0 0.0 1
               texture_unit
               {
                   texture env_room.jpg
                   color_op_ex blend_manual src_texture src_current 0.1
                   env_map spherical
               }
           }
       }
   }

env_room is the environment map. With

           specular 1.0 1.0 1.0 9800.0

we have the Porcelain shader


And with steel shader, increase the specular

           specular 0.9 0.9 0.9 10000.0

Lightmap

A lightmap is just a texture for blending in OGRE. Normally, the lightmap texture is a grayscale texture like this

lightmap.jpg

Image:Lmaa.jpg

The black area will be the shadowed and in the white are, the obejct will be illuminated. And we have material:

rock.jpg

Image:Aaa.jpg

And we will blend them like this:

   material rock_lightmap
   {
       technique
       {
           pass
           {
               ambient 1.0 1.0 1.0
               diffuse 1.0 1.0 1.0
               specular 1.0 1.0 1.0 9800.0
               emissive 0.0 0.0 0.0 1
               texture_unit
               {
                   texture rock.jpg // the detail texture
                   tex_coord_set 0 // UV set for the detail map (if you have multi-UV)
                   colour_op modulate
               }
           }
           pass
           {
               scene_blend modulate
               texture_unit
               {
                   lightmap.jpg // the lightmap texture
                   tex_coord_set 0 // UV set for the lightmap (if you have multi-UV)
                   colour_op modulate
               }
           }
       }
   }

And the result of the blending:

Image:Untitled-1.jpg


Some advanced points: If you create multi UV for model in 3D program, you have to set their UV set in tex_coord_set. You can check the .XML file for more information.

vertexbuffer positions="true" normals="true" colours_diffuse="true" colours_specular="false" texture_coords="1"

The .MESH file converted from .XML will contain these information too.

Intermediate Materials

Detail Textures

TO DO

Advanced Materials

Alpha Splatting

This technique is used to allow multiple detail textures to be blended over each other according to an alpha map, that determines for each texture, where it is to be put. This is most often used for terrain, where you have many different undergrounds like dirt, walkways, grass, stone etc. This cannot be done with one pre-rendered texture, because it would require too much texture memory or look too coarse grained otherwise.

Image:Alpha_blending_done.jpg

Material Requirements

  • Image:alpha_blending_mesh.gif A mesh, for instance a landscape
  • Image:alpha_blending_uvmap.gif A uvmap for the mesh
  • Image:alpha_blending_alpha.gif A texture containing the alpha map (this texture can contain colour channel informations, but it is ignored)
  • Image:alpha_blending_texturen.gif detail textures for each underground type

Material Template

material Material/SOLID/erdboden.jpg
{
    receive_shadows on

    technique
    {
        // base pass
        pass
        {
            // no lighting
            lighting off

            texture_unit
            {
                // we use the grass texture as the base, other textures are blended over it
                texture grass.jpg
                // scale the texture so, that it looks nice
                scale 0.1 0.1
                // only blend colour
                // colour_op_ex source1 src_texture src_texture
            }
        }

        // dirt pass
        pass
        {
            // no lighting
            lighting off

            // blend with former pass
            scene_blend alpha_blend

            // only overwrite fragments with the same depth
            depth_func equal

            // alpha map for the dirt
            texture_unit
            {
                texture alpha_dirt.png

                // use alpha from this texture
                alpha_op_ex source1 src_texture src_texture
                // and colour from last pass
                colour_op_ex source2 src_texture src_texture
            }

            // detail texture
            texture_unit
            {
                texture dirt.jpg
                scale 0.15 0.15
                // alpha blend colour with colour from last pass
                colour_op_ex blend_diffuse_alpha src_texture src_current
            }
        }

        // ... further detail passes like the former one ...

        // lighting pass
        pass
        {
            ambient 0.5 0.5 0.5 1
            diffuse 0.3 0.3 0.3 
            

            depth_func equal
            scene_blend zero src_colour
        }
    }
}

BumpMapping

First of all, Dot3Bump is a perfect example for bump mapping.

Here are my 3 examples files:

SolPSDNet.png: color texture

Image:SolPSDNet.png

SolPSDNetbump.png: bump texture

Image:SolPSDNetbump.png

SolPSDNetb.png: normal map obtained with bump texture

Image:SolPSDNetb.png



STEP 1 : Convert bump to normal map

I only found this for photoshop users. You will need a plug-in for photoshop by Nvidia: 'NVIDIA Normal Map Filter' (Go here(Nvidia site) and search for: "Normal Map Filter", you'll find in first results).

GIMP version available here

Load your bump map in photoshop and go to filter/nvidia tool and normal map. Play around with the scale parameter for stronger effects.

After conversion, save your file as "SolPSDNetb.png" for example.


STEP 2 : Read example-advancedmaterial

Find example-advanced.material and look for Examples/BumpMapping/MultiLight section. Copy and paste it to your .material file.

Here is the code:

material Examples/BumpMapping/MultiLight
 { 
 
 	// this is the preferred technique which uses both vertex and
 	// fragment programs and supports coloured lights
 	technique
 	{
 		// base ambient pass
 		pass
 		{
 			// base colours, not needed for rendering, but as information
 			// to lighting pass categorisation routine

 			ambient 1 1 1
 			diffuse 0 0 0 
 			specular 0 0 0 0 

 			// really basic vertex program
 			// NB we don't use fixed functions here because GL does not like
 			// mixing fixed functions and vertex programs (depth fighting can
 			// be an issue)
 			vertex_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture
 			{
 				param_named_auto worldViewProj worldviewproj_matrix
 				param_named_auto ambient ambient_light_colour
 			}
 			
 		}
 		// wow do the lighting pass
 		// NB we don't do decal texture here because this is repeated per light
 		pass
 		{
 			// base colours, not needed for rendering, but as information
 			// to lighting pass categorisation routine
 			ambient 0 0 0 
 			
 			// do this for each light
 			iteration once_per_light
 
 			scene_blend add
 
 			// vertex program reference
 			vertex_program_ref Examples/BumpMapVP
 			{
 				param_named_auto lightPosition light_position_object_space 0
 				param_named_auto worldViewProj worldviewproj_matrix
 			}
 
 			// fragment program
 			fragment_program_ref Examples/BumpMapFP
 			{
 				param_named_auto lightDiffuse light_diffuse_colour 0 
 			}
 			
 			// base bump map
 			texture_unit
 			{
 				texture NMBumpsOut.png
 				colour_op replace
 			}

 			// normalisation cube map
 			texture_unit
 			{
 				cubic_texture nm.png combinedUVW
 				tex_coord_set 1
 				tex_address_mode clamp
 			}
 		}
 		
 		// decal pass
 		pass
 		{
 			// base colours, not needed for rendering, but as information
 			// to lighting pass categorisation routine

 			lighting off

 			// really basic vertex program
 			// NB we don't use fixed function here because GL does not like
 			// mixing fixed function and vertex programs (depth fighting can
 			// be an issue)

 			vertex_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture
 			{
 				param_named_auto worldViewProj worldviewproj_matrix
 				param_named ambient float4 1 1 1 1
 			}

 			scene_blend dest_colour zero

 			texture_unit
 			{
 				texture RustedMetal.jpg 
 			}
 			
 		}
 	}
 
 	// this is the fallback which cards which don't have fragment program 
 	// support will use
 	// Note: this still requires vertex program support
 	technique
 	{
 		// base ambient pass
 		pass
 		{
 			// base colours, not needed for rendering, but as information
 			// to lighting pass categorisation routine

 			ambient 1 1 1
 			diffuse 0 0 0 
 			specular 0 0 0 0

 			// really basic vertex program
 			// NB we don't use fixed function here because GL does not like
 			// mixing fixed function and vertex programs (depth fighting can
 			// be an issue)

 			vertex_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture
 			{
 				param_named_auto worldViewProj worldviewproj_matrix
 				param_named_auto ambient ambient_light_colour
 			}
 			
 		}
 		// now do the lighting pass
 		// NB we don't do decal texture here because this is repeated per light
 		pass
 		{
 			// base colours, not needed for rendering, but as information
 			// to lighting pass categorisation routine

 			ambient 0 0 0 

 			// do this for each light
 			iteration once_per_light
 
 		
 			scene_blend add
 
 			// vertex program reference
 			vertex_program_ref Examples/BumpMapVP
 			{
 				param_named_auto lightPosition light_position_object_space 0
 				param_named_auto worldViewProj worldviewproj_matrix
 			}
 			
 			// base bump map
 			texture_unit
 			{
 				texture NMBumpsOut.png
 				colour_op replace
 			}

 			// normalisation cube map, with dot product on bump map
 			texture_unit
 			{
 				cubic_texture nm.png combinedUVW
 				tex_coord_set 1
 				tex_address_mode clamp
 				colour_op_ex dotproduct src_texture src_current
 				colour_op_multipass_fallback dest_colour zero
 			}
 		}
 		
 		// decal pass
 		pass
 		{
 			lighting off

 			// really basic vertex program
 			// NB we don't use fixed function here because GL does not like
 			// mixing fixed function and vertex programs, depth fighting can
 			// be an issue

 			vertex_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture
 			{
 				param_named_auto worldViewProj worldviewproj_matrix
 				param_named ambient float4 1 1 1 1
 			}
 			scene_blend dest_colour zero
 			texture_unit
 			{
 				texture RustedMetal.jpg 
 			}
 			 
 		}
  
 	}
 }

After that, you can find the "header" of CG program in the same example-advanced.material file (only copy CG program header for bump maping):

// bump map vertex program, support for this is required
vertex_program Examples/BumpMapVP cg
{
	source Example_BumpMapping.cg
	entry_point main_vp
	profiles vs_1_1 arbvp1
}

// bump map fragment program, support for this is optional
fragment_program Examples/BumpMapFP cg
{
	source Example_BumpMapping.cg
	entry_point main_fp
	profiles ps_1_1 arbfp1 fp20
} 

// bump map with specular vertex program, support for this is required
vertex_program Examples/BumpMapVPSpecular cg
{
	source Example_BumpMapping.cg
	entry_point specular_vp
	profiles vs_1_1 arbvp1
}

// bump map fragment program, support for this is optional
fragment_program Examples/BumpMapFPSpecular cg
{
	source Example_BumpMapping.cg
	entry_point specular_fp
	profiles ps_1_1 arbfp1 fp20
}

Copy and paste the headers at the beginning of your .material file.


STEP 3 : Lat work, include your textures

You have to replace the following in your .material :

RustedMetal.jpg by your color map SolPSDNet.png

NMBumpsOut.png by your NORMAL MAP (not bump) SolPSDNetb.png

And thats all folks!

Important Hint: To use bumpmapping, we'll need tangent vectors for our meshes.

OGRE can do this for you ahead of time if you use the '-t' parameter to the command line tools OgreXmlConverter or OgreMeshUpgrade. Otherwise, you can generate them in code like this:

    // load the meshes with non-default HBU options
    MeshPtr pMesh = MeshManager::getSingleton().load("Block02.mesh",
          ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,    
          HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY,
          HardwareBuffer::HBU_STATIC_WRITE_ONLY,
          true, true);
    // so we can still read it
          
    // build tangent vectors, all our meshes use only one texture coordset 
    // Note: we can build into VES_TANGENT now (SM2+)
    
    unsigned short src, dest;
    if (!pMesh->suggestTangentVectorBuildParams(VES_TANGENT, src, dest))
    {
       pMesh->buildTangentVectors(VES_TANGENT, src, dest);
    }

Read also BumpMapping in this wiki.

Manual

Also have a look in the OGRE manual

Personal tools
administration