PDA

View Full Version : Gouraud shading engine



bhjodokast
September 11th, 2008, 12:17 PM
My goal is to create a fast Gouraud shading engine for Flash. It should be possible (see: http://en.wikipedia.org/wiki/Gouraud_shading).

I searched the tutorials and the forums. I found this tutorial: http://www.kirupa.com/developer/mx2004/shinyorbs.htm

Here's how it works: on steps "ix" and "x.", it shows how the highlight is positioned to give the illusion of a light source reflecting.

Using ActionScript: based on the coordinates of the object relative to the viewport, we can move the light reflection around to replicate Gouraud shading.

For cube shaped objects: I was thinking of creating a class for these objects that will have the current orientation of the cube as properties of the object. So, all of the calculations will be in the class (encapsulated), but when using the class, we could reference the property and simply calculate the "Z" depth based on the cube's scale. The X/Y coordinates will be fed in and the property value returned will tell us where to position the reflections.

This means the class takes care of half the computational requirements, in theory.

Anyone find something like this that's already been created? I'll post a sample sometime soon.

Gazurt
September 11th, 2008, 01:44 PM
This is just what I am looking for...

pwdVergesser
September 11th, 2008, 03:19 PM
My goal is to create a fast Gouraud shading engine for Flash. It should be possible (see: http://en.wikipedia.org/wiki/Gouraud_shading).

I searched the tutorials and the forums. I found this tutorial: http://www.kirupa.com/developer/mx2004/shinyorbs.htm

Here's how it works: on steps "ix" and "x.", it shows how the highlight is positioned to give the illusion of a light source reflecting.

Using ActionScript: based on the coordinates of the object relative to the viewport, we can move the light reflection around to replicate Gouraud shading.

For cube shaped objects: I was thinking of creating a class for these objects that will have the current orientation of the cube as properties of the object. So, all of the calculations will be in the class (encapsulated), but when using the class, we could reference the property and simply calculate the "Z" depth based on the cube's scale. The X/Y coordinates will be fed in and the property value returned will tell us where to position the reflections.

This means the class takes care of half the computational requirements, in theory.

Anyone find something like this that's already been created? I'll post a sample sometime soon.

"hmmm..." in these days I wouldn´t write it within Flash. I would prefer to write a shader in Pixel Bender that is doing the Gourad shadings and then use it as pbj-filter in Flash. Cheers pwd

bhjodokast
September 17th, 2008, 04:17 PM
These are useful:
http://www.kirupa.com/developer/actionscript/3dindex.htm

(from Senocular)

The nice thing about writing it in AS is the OOP support.

bhjodokast
October 14th, 2008, 02:32 AM
So far I've got a start. I'm trying to make it as simple as possible to make it fast. I'm creating a racing game, so I'm wondering what can be sped up.

1. Each slice of the road is the same object. I duplicate it, and refer to it's name after loading. To position the pieces, I have a 1-150 integer (intCounter) that I use with with the property to make a road:

StreetMCobj.duplicateMovieClip("Street" + intCounter, this.getNextHighestDepth(), {_x:206.8-(intCounter*2), _y:128+(intCounter*1.9), _width:134.4+(intCounter*4)});
This code draws the black asphalt textured road and some grass on the side. Now I have all my objects to animate:

Movie Clip: Target="_level0.Street1031"
Movie Clip: Target="_level0.Street1032", etc.

2. I have another clip that has a yellow line down the middle. Alternating the two gives me a smooth road (although the Y-Axis needs to be tweaked... you'll see if you try it, but I'll worry about that later. add a curve to the calculations, etc.)

3. For road effects, say I'm working with _level0.Street1032. If it needs to show a reflection slowly as you pass, I 'play' the clip as I'm moving it. I switch textures the same way for gravel roads, forks in the road, etc. I can play the clip as I'm moving it to give a transition to the surface.

4. The vehicle I'm doing the same way, but with the fake Gouraud shading I was looking for. So when you adjust X/Y position with your mouse, the car "Movie Clip" plays forward or backward to show it changing angle and lighting. Meanwhile the clip is moved around the X/Y!

It looks OK... it's fast so far. Not sure how it's going to be when I add the traffic, weapons and stuff.

Thoughts?

bhjodokast
October 27th, 2008, 09:34 PM
Changes from above:
- from 150, moving down to 30 or so blurred road textures is much faster.
- moving the animation to MCs seems OK. there are plenty of areas to increase performance, such as using low texture images.