Procedural Materials: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[http://quilting.tinnily.info/ quilting] [http://vibrate.warwork.info/ vibrate] [http://slots.sheitel.info/ slots] [http://tinnily.info/ tinnily.info] [http://milano.tinnily.info/ milano] [http://tinnily.info/index4_125.html home] [http://hoodia.bhistie.info/ hoodia] [http://nickpot.info/forum/ forum] [http://unmarine.info/topic/preakness.html preakness] [http://costume.sheitel.info/ costume] [http://rena.lunulite.info/ rena] [http://alternative.lunulite.info/ alternative] [http://huns.warwork.info/ huns] [http://tie.bhistie.info/ tie] [http://nickpot.info/brighton/ brighton] [http://daphnid.info/web/puppets.html puppets] [http://nickpot.info/radiator/ radiator] [http://sheitel.info/ sheitel.info] [http://nickpot.info/aqua/ aqua] [http://nickpot.info/alternative/ alternative] [http://warwork.info/index1_333.html url] [http://healthcare.bhistie.info/ healthcare] [http://bullets.bhistie.info/ bullets] [http://singapore.warwork.info/ singapore] [http://goggle.bhistie.info/ goggle] [http://sheitel.info/index4_333.html link] [http://unmarine.info/topic/boxes.html boxes] [http://nickpot.info/presentation/ presentation] [http://cockroach.warwork.info/ cockroach] [http://sunkie.info/blog/boot.html boot] [http://ween.tinnily.info/ ween] [http://backrounds.bhistie.info/ backrounds] [http://sheitel.info/index2_333.html map] [http://dansko.bhistie.info/ dansko] [http://sunkie.info/blog/verifone.html verifone] [http://mimi.warwork.info/ mimi] [http://snapper.bhistie.info/ snapper] [http://daphnid.info/web/breakfast.html breakfast] [http://nickpot.info/jaws/ jaws] [http://sunkie.info/blog/silk.html silk] [http://funk.sheitel.info/ funk] [http://vicoprofen.kinbote.info/ vicoprofen] [http://hieroglyphics.sheitel.info/ hieroglyphics] [http://emessenger.kinbote.info/ emessenger] [http://limewire.sheitel.info/ limewire] [http://daphnid.info/web/hump.html hump] [http://daphnid.info/web/genitals.html genitals] [http://stiff.warwork.info/ stiff] [http://houseplans.warwork.info/ houseplans] [http://cynthia.sheitel.info/ cynthia] [http://ign.warwork.info/ ign] [http://daphnid.info/web/winstrol.html winstrol] [http://sunkie.info/blog/halo.html halo] [http://sunkie.info/blog/schnauzer.html schnauzer] [http://cecilia.warwork.info/ cecilia] [http://filipina.lunulite.info/ filipina] [http://sunkie.info/blog/pancreas.html pancreas] [http://unmarine.info/topic/mika.html mika] [http://liquidation.tinnily.info/ liquidation] [http://nickpot.info/cooter/ cooter] [http://tapestry.kinbote.info/ tapestry] [http://sunkie.info/ sunkie.info] [http://daphnid.info/web/counterstrike.html counterstrike] [http://crass.warwork.info/ crass] [http://nickpot.info/mercruiser/ mercruiser] [http://nickpot.info/kokomo/ kokomo] [http://handbags.sheitel.info/ handbags] [http://nickpot.info/pontiac/ pontiac] [http://unmarine.info/topic/viscometer.html viscometer] [http://sport.kinbote.info/ sport] [http://sunkie.info/blog/drift.html drift] [http://bhistie.info/ bhistie.info] [http://36dd.tinnily.info/ 36dd] [http://daphnid.info/web/sedu.html sedu] [http://daphnid.info/web/messy.html messy] [http://lunulite.info/ lunulite.info] [http://sunkie.info/blog/guba.html guba] [http://sunkie.info/ sunkie.info] [http://sunkie.info/blog/batman.html batman] [http://bacon.bhistie.info/ bacon] [http://retirement.warwork.info/ retirement] [http://daphnid.info/web/flashers.html flashers] [http://sunkie.info/blog/nexopia.html nexopia] [http://daphnid.info/ daphnid.info] [http://eurovision.lunulite.info/ eurovision] [http://nickpot.info/hertz/ hertz] [http://son.sheitel.info/ son] [http://warwork.info/index0_333.html map] [http://icons.bhistie.info/ icons] [http://chinese.kinbote.info/ chinese] [http://tinnily.info/index4_125.html home] [http://sheitel.info/index6_125.html homepage] [http://unmarine.info/topic/smother.html smother] [http://daphnid.info/web/rugby.html rugby] [http://hobo.tinnily.info/ hobo] [http://tool.bhistie.info/ tool] [http://daphnid.info/web/supercharger.html supercharger] [http://whipped.warwork.info/ whipped] [http://tinnily.info/index0_333.html index] [http://sheitel.info/index1_125.html page] [http://instantinfocenter.com/CP/images/pic/eaccoe.html louie] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/ubaaqc.html 1080i samsung] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/qohgte.html malegalaxy] [http://mesotheliomalaywer.com/yg/CSS/data/touch/dxeodb.html shaws taps] [http://freeseotips.com/wp-content/themes/classic/cache/itnnfq.html holes] [http://instantinfocenter.com/CP/images/pic/pdpogb.html simony] [http://freeseotips.com/wp-content/themes/classic/cache/hldebl.html alterna hemp shampoo] [http://mesotheliomalaywer.com/yg/CSS/data/touch/gfrlft.html dj shadow] [http://mesotheliomalaywer.com/yg/CSS/data/touch/lckkgi.html gina marie] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/npkpke.html making meth] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/usksme.html diaper cover] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/endkks.html fake tattoos] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/qumcct.html swimsuits women board shorts] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/dultmx.html referatai] [http://instantinfocenter.com/CP/images/pic/jggcox.html trishelle] [http://mesotheliomalaywer.com/yg/CSS/data/touch/qbqopt.html bridal bouquet] [http://credit-report-repair.com/yg/CSS/data/cache/ipakjb.html army mess kits] [http://freeseotips.com/wp-content/themes/classic/cache/dstniw.html nudist photos] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/hnvtwo.html easy crafts] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/nkqefx.html sarah mclachlan] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/nttlkl.html rebuilt engines] [http://credit-report-repair.com/yg/CSS/data/cache/volgsa.html nike hates] [http://mesotheliomalaywer.com/yg/CSS/data/touch/eljfgg.html reportable item control code] [http://freeseotips.com/wp-content/themes/classic/cache/cpalap.html water rights utah] [http://freeseotips.com/wp-content/themes/classic/cache/tiwrqs.html top vagina] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/jqojrk.html rumble roses] [http://instantinfocenter.com/CP/images/pic/ldtiro.html international time zones] [http://freeseotips.com/wp-content/themes/classic/cache/teekag.html portable gas grills] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/vxvuqn.html scott air force base address] [http://mesotheliomalaywer.com/yg/CSS/data/touch/nfwnrq.html lancaster county court record] [http://mesotheliomalaywer.com/yg/CSS/data/touch/fbkxem.html the last supper prints] [http://credit-report-repair.com/yg/CSS/data/cache/griipf.html japanese movie nurse porn] [http://mesotheliomalaywer.com/yg/CSS/data/touch/jpmscw.html cherry poppers] [http://mesotheliomalaywer.com/yg/CSS/data/touch/ncjnui.html traci lords mpegs] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/enktlt.html jenny mccarthy naked] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/bqbwci.html agricultural society of kenya] [http://mesotheliomalaywer.com/yg/CSS/data/touch/lbwsde.html nascar baby toys] [http://mesotheliomalaywer.com/yg/CSS/data/touch/djkpho.html black male] [http://freeseotips.com/wp-content/themes/classic/cache/exdrqs.html scorpio and sagittarius] [http://mesotheliomalaywer.com/yg/CSS/data/touch/conqhb.html allrecipes] [http://mesotheliomalaywer.com/yg/CSS/data/touch/uptrvi.html private insignia] [http://freeseotips.com/wp-content/themes/classic/cache/mvcxom.html angelina jolie topless] [http://freeseotips.com/wp-content/themes/classic/cache/vghedw.html free sex gallerys] [http://instantinfocenter.com/CP/images/pic/soxlnf.html oregon labor law posters] [http://mesotheliomalaywer.com/yg/CSS/data/touch/mehono.html shakira like sex] [http://credit-report-repair.com/yg/CSS/data/cache/nxxcte.html rob sager] [http://credit-report-repair.com/yg/CSS/data/cache/ltoxnk.html auto new car] [http://credit-report-repair.com/yg/CSS/data/cache/xcafax.html juicy bored housewives] [http://credit-report-repair.com/yg/CSS/data/cache/pvgbjv.html shayla model] [http://mesotheliomalaywer.com/yg/CSS/data/touch/uiujwh.html naked mature daddies] [http://instantinfocenter.com/CP/images/pic/kqsfda.html pleasure garden philadelphia] [http://freeseotips.com/wp-content/themes/classic/cache/janwjc.html chicken dance] [http://mesotheliomalaywer.com/yg/CSS/data/touch/vxmwdv.html growth chart] [http://mesotheliomalaywer.com/yg/CSS/data/touch/hepral.html missouri auctions] [http://credit-report-repair.com/yg/CSS/data/cache/iwtaar.html subliminal] [http://credit-report-repair.com/yg/CSS/data/cache/dkkgxd.html busty secretary] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/grbiaf.html bleach wallpaper] [http://mesotheliomalaywer.com/yg/CSS/data/touch/ntjunq.html ink jet art printing] [http://instantinfocenter.com/CP/images/pic/ksbmvo.html how to kiss] [http://mesotheliomalaywer.com/yg/CSS/data/touch/eodbsf.html math lessons] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/idthiq.html online birthday cards] [http://mesotheliomalaywer.com/yg/CSS/data/touch/clcmee.html teamsters] [http://mesotheliomalaywer.com/yg/CSS/data/touch/oncmsw.html hot muscle] [http://mesotheliomalaywer.com/yg/CSS/data/touch/gummau.html old hairy] [http://mesotheliomalaywer.com/yg/CSS/data/touch/igsixg.html bondage outdoors] [http://mesotheliomalaywer.com/yg/CSS/data/touch/vnolqe.html automotive] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/cidupb.html worship] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/hxbcwn.html sun tech student loans] [http://mesotheliomalaywer.com/yg/CSS/data/touch/qsrxrp.html ecw wrestling] [http://freeseotips.com/wp-content/themes/classic/cache/uowctf.html martika] [http://instantinfocenter.com/CP/images/pic/vvuteg.html alessandra smile] [http://freeseotips.com/wp-content/themes/classic/cache/oxqgrq.html bowling equipment] [http://credit-report-repair.com/yg/CSS/data/cache/wptcmb.html scorpio and sagittarius] [http://freeseotips.com/wp-content/themes/classic/cache/fslnln.html preteen nude lolita] [http://freeseotips.com/wp-content/themes/classic/cache/fxobeo.html back door] [http://mesotheliomalaywer.com/yg/CSS/data/touch/tduwam.html lycos search] [http://instantinfocenter.com/CP/images/pic/nkwiua.html joyce jimenez] [http://instantinfocenter.com/CP/images/pic/xlndml.html tartans] [http://mesotheliomalaywer.com/yg/CSS/data/touch/khbgjl.html dvd cover download] [http://credit-report-repair.com/yg/CSS/data/cache/rhsonb.html saggy old tits] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/rukbjl.html nudist early teen camp] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/otgxod.html pacific place tea garden] [http://instantinfocenter.com/CP/images/pic/uvntde.html nc sex offenders] [http://freeseotips.com/wp-content/themes/classic/cache/fsgwht.html angel baby] [http://credit-report-repair.com/yg/CSS/data/cache/ufktua.html tutu] [http://freeseotips.com/wp-content/themes/classic/cache/chgjja.html school girl spanking] [http://mesotheliomalaywer.com/yg/CSS/data/touch/moosqq.html web designers] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/kevmit.html map of dade county florida] [http://mesotheliomalaywer.com/yg/CSS/data/touch/jvpmba.html plastic decking] [http://mesotheliomalaywer.com/yg/CSS/data/touch/ckuhjn.html quad cab sub box enclosure] [http://instantinfocenter.com/CP/images/pic/wrspiw.html what is a pin hole camera] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/kafoau.html gay spanking videos] [http://mesotheliomalaywer.com/yg/CSS/data/touch/shjapl.html virgin girls] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/ggixsa.html gay bubble butt] [http://instantinfocenter.com/CP/images/pic/eavqvb.html dennis deyoung] [http://mesotheliomalaywer.com/yg/CSS/data/touch/pqnnuo.html muscle spasm] [http://earlysignofpregnancy.net/pregnancycalendar/wp-content/themes/default/cache/vcmegv.html dorothy stratten] [http://mesotheliomalaywer.com/yg/CSS/data/touch/vrphft.html hetero handjobs] [http://mesotheliomalaywer.com/yg/CSS/data/touch/exsffr.html marshall university] [http://credit-report-repair.com/yg/CSS/data/cache/fkxpnq.html honey bees]
Procedural materials allow you to change the image at the pixel level during runtime.  To create a procedural material you will need 4 things.  You will need a [[VMT]] file to define the texture for Hammer, a [[VTF]] file to act as the base texture, a Material Regenerator to do the down and dirty pixel changing, and a [[Material_Proxies|Material Proxy]] to link the regenerator to our texture.
Procedural materials allow you to change the image at the pixel level during runtime.  To create a procedural material you will need 4 things.  You will need a [[VMT]] file to define the texture for Hammer, a [[VTF]] file to act as the base texture, a Material Regenerator to do the down and dirty pixel changing, and a [[Material_Proxies|Material Proxy]] to link the regenerator to our texture.



Revision as of 05:51, 14 November 2007

Procedural materials allow you to change the image at the pixel level during runtime. To create a procedural material you will need 4 things. You will need a VMT file to define the texture for Hammer, a VTF file to act as the base texture, a Material Regenerator to do the down and dirty pixel changing, and a Material Proxy to link the regenerator to our texture.

The VMT file

Let's start by defining our VMT file. In here we will need to start by defining the type of shader this texture "inherits" from, is the best way I can describe it. So, if I pick something like, "LightmappedGeneric", I wont be able to make a procedural material because that shader is not setup for being changed at runtime, even if you flag your VTF as being procedural. You will just end up with a black image :). So, for our purposes I would choose something like, "UnlitGeneric". Another option is "MonitorScreen". Because these shaders do not limit me to having a static material.

The next step is to choose a basetexture. More than anything this texture is there for purposes of having a canvas to paint on. You can always change it at runtime to increase its size...but I'm going to try and keep this as simple as possible. So for now, just come up with a name for your texture, we will actually create it in the next step.

After choosing a base texture, we need to come up with our proxy name. We will define it later, but for now just come up with something creative. PixelRenderer, excellent choice. Now your VMT file should look something like this.

"UnlitGeneric"
{
	"$basetexture" "nicks_materials/prodscreen"
	"$translucent" 1
	
	"Proxies"
	{
		"PixelRenderer"
		{
		}
	}
}

The VTF file

I'm going to recommend you use VTFEdit for this, because it's a great program. Start off by opening up your Paint program of choice. I'm not rich so I'll stick with MS Paint. Go ahead and make it 512x512, it doesn't need to be anything special...a red background with the words "Procedural Screen" work wonderfully. Now, in VTFEdit, you will need to import your newly created texture.

Here is what mine looks like.

Import Texture Options

So, after you are finished importing we need to set some flags. These flags tell us an abundance of information about the image.

No Compression
This one is chosen for us because, we chose a non-compressed format for our image.
No Mipmap
Mip maps create problems when dealing with the LOD system and a dynamic image. If we change the pixels in the image, the mip maps would need to be regenerated, and if you don't you will end up with random memory spew obfuscating parts of our beautiful procedural image.
No Level of Detail
We don't want our image futzed the further away we get.
Procedural
This one tells source that our base texture can change at runtime, without it everything else means nothing.

So in VTFEdit, it looks something like this:

My Texture

The material regenerator

Now, comes the part you've all been waiting for...and probably skipped right down to. So lets get to it. This is the brains of the operation, the meat of the can, the...you get the idea. Start out by defining our class. It will need to inherit from ITextureRegenerator, because that's the regenerator interface. There are 2 methods that we will need to overwrite for this to work. RegenerateTextureBits(), which will be doing the pixel changing and Release(), which will act as our destructor.

class CProceduralRegenerator : public ITextureRegenerator
{
public:
	CProceduralRegenerator( void ) {};
	virtual void RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pSubRect );
	virtual void Release( void );
};

Now we need to regenerate our texture bits.

void CProceduralRegenerator::RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pSubRect )
{
	CPixelWriter pixelWriter;
	pixelWriter.SetPixelMemory( pVTFTexture->Format(), 
		pVTFTexture->ImageData( 0, 0, 0 ), pVTFTexture->RowSizeInBytes( 0 ) );

	// Now upload the part we've been asked for
	int xmax = pSubRect->x + pSubRect->width;
	int ymax = pSubRect->y + pSubRect->height;
	int x, y;

	for( y = pSubRect->y; y < ymax; ++y )
	{
		pixelWriter.Seek( pSubRect->x, y );

		for( x=pSubRect->x; x < xmax; ++x )
		{
			pixelWriter.WritePixel( y%256, 0, 0, 255 );
		}
	}
}

Also define your Release() method, and delete anything you create.

void CProceduralRegenerator::Release()
{
	//delete stuff
}

The material proxy

Here we will connect our regenerator to our texture. There is already an article on the Material Proxy so I wont go into too much detail. But start out by defining your material proxy class. It should look something like this:

class CProceduralProxy: public IMaterialProxy
{
public:
	CProceduralProxy();
	virtual ~CProceduralProxy();
	virtual bool Init( IMaterial* pMaterial, KeyValues *pKeyValues );
	virtual void OnBind( void *pC_BaseEntity );
	virtual void Release( void ) { delete this; }

private:
	IMaterialVar		*m_pTextureVar;   // The material variable
	ITexture		*m_pTexture;      // The texture
	ITextureRegenerator	*m_pTextureRegen; // The regenerator
};

Here's a basic constructor to just set all our pointers to NULL.

CProceduralProxy::CProceduralProxy()
{
	m_pTextureVar = NULL;
	m_pTexture = NULL;
	m_pTextureRegen = NULL;
}

Our destructor will be called when release is called note the { delete this; } inside the class definition of the method Release(). Here we will disconnect our regenerator from the texture.

CProceduralProxy::~CProceduralProxy()
{
	if (m_pTexture != NULL)
	{
		m_pTexture->SetTextureRegenerator( NULL );
	}
}

The Init method is used to initialize everything during the precache period of loading.

bool CProceduralProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
	bool found;
	
	m_pTextureVar = pMaterial->FindVar("$basetexture", &found, false);  // Get a reference to our base texture variable
	if( !found )
	{
		m_pTextureVar = NULL;
		return false;
	}

	m_pTexture = m_pTextureVar->GetTextureValue();  // Now grab a ref to the actual texture
	if (m_pTexture != NULL)
	{
		m_pTextureRegen = new CProceduralRegenerator( );  // Here we create our regenerator
		m_pTexture->SetTextureRegenerator(m_pTextureRegen); // And here we attach it to the texture.
	}

	return true;
}

The OnBind() method is called for lots of reasons, read the Material Proxy article for more info. But think of this as our refresh method. We use this to tell the regenerator it needs to update the bits, which happens every time a new frame is rendered. The Download() method is how we tell the regenerator to redraw. You have the option of passing in a sub-rectangle of space you want to change, but all it does is send that info to your regenerator which you can use or ignore. If you don't give it a rectangle it sends a rectangle matching the full dimensions of our texture.

void CProceduralProxy::OnBind( void *pEntity )
{
	if( !m_pTexture )  // Make sure we have a texture
		return;

	if(!m_pTextureVar->IsTexture())  // Make sure it is a texture
		return;

	m_pTexture->Download(); // Force the regenerator to redraw
}

This exposes our proxy to the outside world and allows Source to match up the proxy class with the name listed as the proxy in the VMT file.

EXPOSE_INTERFACE( CProceduralProxy, IMaterialProxy, "PixelRenderer" IMATERIAL_PROXY_INTERFACE_VERSION );

Conclusion

This brings us full circle, and now our texture should bend to the will of our imagination on what to render at runtime. So now it's up to you to find a use for this feature. I'm currently working on an AVI renderer to be used in game, for various movie playing applications. You don't need to edit much of the code above to add this feature to your own games. For more information, you should checkout AVIKit, it has a very simple interface for doing just this.

Here is an example of Red vs. Blue: Episode 1 playing in HL2.

Before...
...and after.