You are previewing Adobe Flash 11 Stage3D (Molehill) Game Programming.
O'Reilly logo
Adobe Flash 11 Stage3D (Molehill) Game Programming

Book Description

A step-by-step guide for creating stunning 3D games in Flash 11 Stage3D (Molehill) using AS3 and AGAL

  • The first book on Adobe's Flash 11 Stage3D, previously codenamed Molehill

  • Build hardware-accelerated 3D games with a blazingly fast frame rate.

  • Full of screenshots and ActionScript 3 source code, each chapter builds upon a real-world example game project step-by-step.

  • Light-hearted and informal, this book is your trusty sidekick on an epic quest to create your very own 3D Flash game.

  • In Detail

    Adobe’s Stage3D (Molehill) is a set of 3D APIs that has brought 3D to the Flash platform. Being a completely new technology, there were almost no resources to get you acquainted with this revolutionary platform, until now.

    This book will show you how to make your very own next-gen 3D games in Flash. If you have ever dreamed of writing your own console-style 3d game in Flash, get ready to be blown away by the hardware accelerated power of Stage3D. This book will lead you step-by-step through the process of programming a 3D game in Actionscript 3 using this exciting new technology. Filled with examples, pictures and source code, this is a practical and fun-to-read guide that will benefit both 3D programming beginners and expert game developers alike.

    Starting with simple tasks like setting up Flash to render a simple 3d shape, each chapter presents a deeper and more complete videogame as an example project. Right from a simple tech demo, your game will grow to become a finished product - your very own playable 3d game filled with animation, special effects, sounds, and tons of action. The goal of this book is to teach you how to program a complete game in Molehill that has a beginning, middle, and game over.

    As you progress further into your epic quest, you will learn all sorts of useful tricks such as ways to create eye-catching special effects using textures, special blend modes for transparent particle systems, fantastic vertex and fragment programs that are used to craft beautiful shaders and much more. You will learn how to upload the geometry of your 3D models to video RAM for ultra-fast rendering. You will dive into the magical art of AGAL shader programming. You will learn optimization tricks to achieve blazingly fast frame rate even at full screen resolutions. With each chapter, you will “level up” your game programming skills, earning the title of Molehill Master – you will be able to honestly call yourself a 3D game programmer.

    This book is written for beginners by a veteran game developer. It will become your trusty companion filled with the knowledge you need to make your very own 3D games in Flash.

    Table of Contents

    1. Adobe Flash 11 Stage3D (Molehill) Game Programming
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. www.PacktPub.com
          1. Support files, eBooks, discount offers and more
            1. Why Subscribe?
            2. Free Access for Packt account holders
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code for this book
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Let's Make a Game Using Molehill!
      1. Your epic adventure awaits!
      2. What is Molehill?
      3. What Molehill is NOT
      4. What you need to know already
      5. Basic 3D terminology
      6. Common 3D content terms
        1. Mesh
        2. Polygon
        3. Vertex
        4. Texture
        5. Shaders
        6. Vertex program
        7. Fragment program
        8. 3D Content level one achieved!
      7. Common 3D coding terms
        1. Vectors
        2. Normals
        3. Matrices
        4. 3D Coding level one achieved!
        5. Pop quiz - earn some experience points
        6. Have a go hero - your first side quest
      8. Summary
      9. Level 1 achieved!
    8. 2. Blueprint of a Molehill
      1. The old fashioned way
      2. The Molehill way: Stage3D
      3. Using 2D Flash text and sprites
      4. Why is Stage3D so fast?
      5. The structure of a Molehill application
        1. Stage
        2. Stage3D
        3. Context3D
        4. VertexBuffer3D
        5. IndexBuffer3D
        6. Program3D
      6. Flowchart of a Molehill program
      7. Time for action - things we do once, during the setup
        1. What just happened?
      8. Time for action - things we do over and over again
        1. What just happened?
        2. Pop quiz - earn some experience points
      9. Summary
      10. Level 2 achieved!
    9. 3. Fire up the Engines!
      1. Step 1: Downloading Flash 11 (Molehill) from Adobe
      2. Time for action - getting the plugin
      3. Time for action - getting the Flash 11 profile for CS5
      4. Time for action - upgrading Flex
      5. Time for action - upgrading the Flex playerglobal.swc
      6. Time for action - using SWF Version 13 when compiling in Flex
      7. Time for action - updating your template HTML file
        1. Stage3D is now set up!
      8. Step 2: Start coding
      9. Time for action - creating an empty project
        1. What just happened?
      10. Time for action - importing Stage3D-specific classes
        1. What just happened?
      11. Time for action - initializing Molehill
        1. What just happened?
      12. Time for action - defining some variables
        1. What just happened?
      13. Time for action - embedding a texture
        1. What just happened?
      14. Time for action - defining the geometry of your 3D mesh
        1. What just happened?
      15. Time for action - starting your engines
        1. What just happened?
      16. Time for action - adding to the onContext3DCreate function
        1. What just happened?
      17. Time for action - uploading our data
        1. What just happened?
      18. Time for action - setting up the camera
        1. What just happened?
      19. Time for action - let's animate
        1. What just happened?
      20. Time for action - setting the render state and drawing the mesh
        1. What just happened?
        2. Quest complete—time to reap the rewards
        3. Congratulations!
      21. The entire source code
        1. Pop quiz - earn some experience points
        2. Have a go hero - a fun side quest
      22. Summary
      23. Level 3 achieved!
    10. 4. Basic Shaders: I can see Something!
      1. AGAL: Adobe Graphics Assembly Language
        1. What does one line of AGAL look like?
        2. What is a register?
        3. What is a component?
        4. Working with four components at the same time
        5. Different registers for different jobs
          1. Vertex attribute registers: va0..va7
          2. Constant registers: vc0..vc127 and fc0..fc27
          3. Temporary registers: vt0..vt7 and ft0..ft7
          4. Output registers: op and oc
          5. Varying registers: v0..v7
          6. Texture samplers: fs0..fs7
      2. A basic AGAL shader example
        1. The vertex program
      3. Time for action - writing your first vertex program
        1. What just happened?
        2. The fragment program
      4. Time for action - writing your first fragment program
        1. What just happened?
      5. Compiling the AGAL source code
      6. Time for action - compiling AGAL
        1. What just happened?
      7. Time to Render!
      8. Time for action - rendering
        1. What just happened?
      9. Creating a shader demo
      10. Adding an FPS counter
      11. Time for action - creating the FPS GUI
        1. What just happened?
      12. Time for action - adding the GUI to our inits
        1. What just happened?
      13. Time for action - adding multiple shaders to the demo
        1. What just happened?
      14. Time for action - initializing the shaders
        1. What just happened?
      15. Time for action - animating the shaders
        1. What just happened?
      16. Time for action - uploading data to Stage3D
        1. What just happened?
        2. Quest complete—time to reap the rewards
        3. Congratulations!
        4. Pop quiz - earn some experience points
        5. Have a go hero - shader experiments
      17. Summary
      18. Level 4 achieved!
    11. 5. Building a 3D World
      1. Creating vertex buffers
        1. Importing 3D models into Flash
      2. Time for action - coding the Stage3dObjParser class
        1. What just happened?
      3. Time for action - creating the class constructor function
        1. What just happened?
      4. Time for action - coding the parsing functions
        1. What just happened?
      5. Time for action - processing the data
        1. What just happened?
      6. Time for action - coding some handy utility functions
        1. What just happened?
        2. Our mesh parsing class is complete!
      7. The render loop
      8. Time for action - starting the render loop
        1. What just happened?
      9. Time for action - adding the score to the GUI
        1. What just happened?
      10. Time for action - upgrading your init routines
        1. What just happened?
      11. Time for action - parsing our mesh data
        1. What just happened?
      12. Time for action - animating the scene
        1. What just happened?
        2. Quest complete—time to reap the rewards
      13. Folder structure
        1. Pop quiz - earn some experience points
        2. Have a go hero - a fun side-quest: make some more models!
      14. Summary
      15. Level 5 achieved!
    12. 6. Textures: Making Things Look Pretty
      1. Time for a plan: creating a "real" game
      2. Using textures in Stage3D
        1. Power-of-two
        2. UV coordinates
        3. Transparent textures
        4. Animating UV coordinates in a shader
      3. Time for action - updating UV coordinates each frame
        1. What just happened?
        2. Texture atlases
        3. Animated textures
        4. Manipulating texture data
      4. Render states
        1. Backface culling
      5. Time for action - rendering a mesh's backfaces
        1. What just happened?
        2. Depth testing
      6. Time for action - making a mesh not affect the zbuffer
        1. What just happened?
        2. Blend modes
      7. Time for action - rendering an opaque mesh
        1. What just happened?
      8. Time for action - rendering a mesh with transparent regions
        1. What just happened?
      9. Time for action - rendering a mesh so it lightens the scene
        1. What just happened?
      10. Increasing your performance
        1. Opaque is faster
        2. Avoiding overdraw
        3. Avoiding state changes
        4. Use simple shaders
        5. Draw fewer meshes
      11. Adding texture effects to our demo
      12. Time for action embedding the new art
        1. What just happened?
      13. Time for action - adding the variables we need
        1. What just happened?
      14. Time for action - upgrading the GUI
        1. What just happened?
      15. Time for action - listening for key presses
      16. Time for action - upgrading our render loop
        1. What just happened?
      17. Time for action - upgrading the renderTerrain function
        1. What just happened?
      18. Time for action - upgrading our Stage3D inits
        1. What just happened?
      19. Time for action - simplifying the initShaders function
        1. What just happened?
      20. Time for action - parsing the new meshes
        1. What just happened?
      21. Time for action - rendering different meshes as appropriate
        1. What just happened?
      22. Time for action - switching textures
        1. What just happened?
      23. Time for action - switching blend modes
        1. What just happened?
      24. Your demo has been upgraded!
        1. Pop quiz - earn some experience points
      25. Summary
      26. Level 6 achieved!
    13. 7. Timers, Inputs, and Entities: Gameplay Goodness!
      1. Our current quest
        1. Keeping it simple
        2. Making it reusable
      2. Making our game more interactive
        1. Adding a HUD overlay graphic
      3. Time for action - adding a GUI overlay
        1. What just happened?
        2. Keeping track of time: a game timer class
      4. Time for action - creating the game timer class
        1. What just happened?
      5. Time for action - adding the GameTimer class constructor
        1. What just happened?
      6. Time for action - implementing the tick function
        1. What just happened?
        2. A game input class
      7. Time for action - creating the GameInput class
        1. What just happened?
      8. Time for action - coding the GameInput class constructor
        1. What just happened?
      9. Time for action - detecting mouse movement
        1. What just happened?
      10. Time for action - detecting the keyboard input
        1. What just happened?
      11. Time for action - detecting key release events
        1. What just happened?
      12. Time for action - detecting the input focus
        1. What just happened?
        2. An abstract entity class
      13. Time for action - creating the Stage3dEntity class
        1. What just happened?
      14. Time for action - creating the Stage3dEntity class constructor
        1. What just happened?
      15. Hiding complex code by using get and set functions
      16. Time for action - getting and setting the transform
        1. What just happened?
      17. Time for action - getting and setting the entity position
        1. What just happened?
      18. Time for action - getting and setting the entity rotation
      19. Time for action - getting and setting the entity's scale
        1. What just happened?
      20. Time for action - updating the transform or values on demand
        1. What just happened?
      21. Time for action - creating the movement utility functions
        1. What just happened?
      22. Time for action - implementing vector utility functions
      23. Time for action - adding some handy entity utility functions
        1. What just happened?
      24. Time for action - cloning an entity
        1. What just happened?
      25. Time for action - rendering an entity
        1. What just happened?
      26. Design art for our new improved game world
      27. Upgrading our game
      28. Time for action - importing our new classes
        1. What just happened?
      29. Time for action - adding new variables to our game
        1. What just happened?
      30. Time for action - embedding the new art
      31. Time for action - upgrading the game inits
        1. What just happened?
      32. Time for action - upgrading the GUI
      33. Time for action - simplifying the shaders
      34. Time for action - using the new textures
        1. What just happened?
      35. Time for action - spawning some game entities
        1. What just happened?
      36. Time for action - upgrading the render function
        1. What just happened?
      37. Time for action - creating a simulation step function
        1. What just happened?
      38. Time for action - creating a heartbeat function
      39. Time for action - upgrading the enterFrame function
        1. What just happened?
      40. Let's see all this in action!
        1. Pop quiz - earn some experience points
        2. Have a go hero - a fun side-quest: engage the afterburners!
      41. Summary
      42. Level 7 achieved!
    14. 8. Eye-Candy Aplenty!
      1. Our current quest
        1. Designing for performance
        2. Designing for reusability
        3. Animating using AGAL
      2. A basic particle entity class
      3. Time for action - extending the entity class for particles
        1. What just happened?
      4. Time for action - adding particle properties
        1. What just happened?
      5. Time for action - coding the particle class constructor
        1. What just happened?
      6. Time for action - cloning particles
        1. What just happened?
      7. Time for action - generating numbers used for animation
        1. What just happened?
      8. Time for action - simulating the particles
        1. What just happened?
      9. Time for action - respawning particles
        1. What just happened?
      10. Time for action - rendering particles
        1. What just happened?
      11. Keyframed vertex animation shader
      12. Time for action - creating a keyframed particle vertex program
        1. What just happened?
      13. Time for action - creating a static particle vertex program
        1. What just happened?
      14. Time for action - creating a particle fragment program
        1. What just happened?
      15. Time for action - compiling the particle shader
      16. A particle system manager class
      17. Time for action - coding a particle system manager class
        1. What just happened?
      18. Time for action - defining a type of particle
        1. What just happened?
      19. Time for action - simulating all particles at once
        1. What just happened?
      20. Time for action - rendering all particles at once
        1. What just happened?
      21. Time for action - spawning particles on demand
        1. What just happened?
      22. Time for action - creating new particles if needed
        1. What just happened?
      23. Keyframed particle meshes
        1. Selecting a particle texture
      24. Time for action - sculpting a single particle
      25. Time for action - sculpting a group of particles
      26. Time for action - sculpting the second keyframe
        1. What just happened?
      27. Incorporating the particle system class in our game
      28. Time for action - adding particles to your game
      29. Time for action - preparing a type of particle for use
        1. What just happened?
      30. Time for action - upgrading the renderScene function
        1. What just happened?
      31. Time for action - adding particles to the gameStep function
        1. What just happened?
      32. Time for action - keeping track of particle statistics
        1. What just happened?
      33. Let's see the new particle system in action!
        1. Pop quiz - earn some experience points
        2. Have a go hero - inventing some cool particle effects
      34. Summary
      35. Level 8 achieved!
    15. 9. A World Filled with Action
      1. Extending the entity class for "actors"
      2. Time for action - creating a game actor class
        1. What just happened?
      3. Time for action - extending the actor's properties
        1. What just happened?
      4. Time for action - coding the GameActor class constructor
        1. What just happened?
      5. Time for action - creating a step animation function
        1. What just happened?
      6. Time for action - animating actors
        1. Implementing artificial intelligence (AI)
      7. Time for action - using timers
      8. Time for action - shooting at enemies
        1. What just happened?
      9. Time for action - cloning an actor
      10. Time for action - handling an actor's death
      11. Time for action - respawning an actor
      12. Collision detection
      13. Time for action - detecting collisions
      14. Time for action - detecting sphere-to-sphere collisions
      15. Time for action - detecting bounding-box collisions
      16. An "actor reuse pool" system
      17. Time for action - creating an actor pool
      18. Time for action - defining a clone parent
      19. Time for action - animating the entire actor pool
        1. What just happened?
      20. Time for action - rendering an actor pool
      21. Time for action - spawning an actor
      22. Time for action - checking for collisions between actors
      23. Restricting display to nearby actors for better framerate
      24. Time for action - hiding actors that are far away
        1. What just happened?
      25. Time for action - destroying every actor in the pool
        1. What just happened?
      26. Easy world creation using a map image
      27. Time for action - implementing a level parser class
        1. What just happened?
      28. Time for action - spawning actors based on a map image
        1. What just happened?
      29. Time for action - parsing the map image pixels
        1. What just happened?
      30. Upgrading the input routines
      31. Time for action - adding more properties to the input class
      32. Time for action - handling click events
      33. Time for action - upgrading the key events
        1. What just happened?
        2. Pop quiz - earn some experience points
        3. Have a go hero - a fun side-quest: art attack!
      34. Summary
      35. Level 9 achieved!
    16. 10. 3... 2... 1... ACTION!
      1. Our final quest
      2. Getting to the finish line
      3. Time for action - drawing a title screen
      4. Time for action - importing required classes
        1. What just happened?
      5. Adding new variables to our game
      6. Time for action - tracking the game state
        1. What just happened?
      7. Time for action - adding variables for timer-based events
        1. What just happened?
      8. Time for action - adding movement related variables
        1. What just happened?
      9. Time for action - keeping track of all entities
        1. What just happened?
      10. Time for action - upgrading the HUD
        1. What just happened?
      11. Time for action - defining variables used by Stage3D
        1. What just happened?
      12. Adding art to our game
      13. Time for action - embedding our new art assets (AS3 version)
        1. What just happened?
      14. Time for action - embedding our new art assets (CS5 version)
        1. What just happened?
      15. Time for action - embedding all the meshes
      16. Time for action - keeping track of art assets
        1. What just happened?
      17. Upgrading the final game source code
      18. Time for action - upgrading the inits
        1. What just happened?
      19. Time for action - initializing Stage3D
        1. What just happened?
      20. Time for action - upgrading the initGUI function
        1. What just happened?
      21. Time for action - upgrading the texture inits
        1. What just happened?
      22. Time for action - upgrading the shaders
        1. What just happened?
      23. Time for action - defining new actor types and behaviors
        1. What just happened?
      24. Time for action - initializing the terrain meshes
        1. What just happened?
      25. Time for action - initializing the enemies
        1. What just happened?
      26. Time for action - initializing the bullets
        1. What just happened?
      27. Time for action - initializing the asteroids
        1. What just happened?
      28. Time for action - initializing the space stations
        1. What just happened?
      29. Time for action - initializing the particle models
        1. What just happened?
      30. Time for action - creating the game level
        1. What just happened?
      31. Time for action - upgrading the render loop
        1. What just happened?
      32. Defining gameplay-specific events
      33. Time for action - tracking game events
        1. What just happened?
      34. Time for action - handling game over
        1. What just happened?
      35. Time for action - updating the score display
        1. What just happened?
      36. Time for action - updating the FPS display
        1. What just happened?
      37. Time for action - handling collision events
        1. What just happened?
      38. Time for action - handling the player input
        1. What just happened?
      39. Time for action - upgrading the gameStep function
        1. What just happened?
      40. Time for action - upgrading the heartbeat function
        1. What just happened?
      41. Time for action - upgrading the enterFrame function
        1. What just happened?
      42. Publish... distribute... profit!
        1. Have a go hero - a fun side quest
      43. Summary
      44. Level 10 achieved. Universe saved!
      45. Where to go from here?
      46. A note from the author
    17. A. AGAL Operand Reference
      1. What does one line of AGAL look like?
      2. Registers available for AGAL programs
      3. COPYING DATA
      4. ALGEBRAIC OPERANDS
      5. MATH OPERANDS
      6. TRIGONOMETRY OPERANDS
      7. CONDITIONAL OPERANDS
      8. VECTOR and MATRIX OPERANDS
      9. TEXTURE SAMPLING OPERAND
    18. B. Pop quiz Answers
      1. Chapter 1
        1. Let's Make a Game Using Molehill!
      2. Chapter 2
        1. Blueprint of a Molehill
      3. Chapter 3
        1. Fire up the Engines
      4. Chapter 4
        1. Basic Shaders: I can see something!
      5. Chapter 5
        1. Building a 3D World
      6. Chapter 6
        1. Textures: Making Things Look Pretty
      7. Chapter 7
        1. Timers, Inputs, and Entities: Gameplay Goodness!
      8. Chapter 8
        1. Eye-Candy Aplenty!
      9. Chapter 9
        1. A World Filled with Action