The Essential Guide to 3D in Flash Rob Bateman & Richard Olsson The Essential Guide to 3D in Flash Copyright © 2010 by Rob Bateman & Richard Olsson All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2541-6 ISBN-13 (electronic): 978-1-4302-2542-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer- sbm.com, or visit http://www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. Credits Publisher and President: Compositor: Paul Manning Bronkella Publishing LLC Lead Editor: Indexer: Ben Renow-Clarke Toma Mulligan Technical Reviewer: Artist: Gregory Caldwell April Milne Editorial Board: Cover Designer: Clay Andres, Steve Anglin, Mark Beckner, Ewan Anna Ishchenko Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Cover Art and Turtle Model: Hassell, Michelle Lowman, Matthew Moodie, Duncan Peter Kapelyan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Suzanne Monkey Model: Wade, Tom Welsh www.blender.org Coordinating Editor: Skeleton Model: Anne Collett www.geocities.jp/oirahakobito2/, [email protected] Copy Editor: Heather Lang ii Contents at a Glance Contents at a Glance ...............................................................................................iii Contents...................................................................................................................iv About the Author......................................................................................................x About the Technical Reviewer...............................................................................xi Acknowledgments.................................................................................................xii Introduction............................................................................................................xiii Chapter 1: Getting Started.......................................................................................1 Chapter 2: Creating Your First 3D Project..............................................................9 Chapter 3: The View, Scene, and Camera...........................................................19 Chapter 4: Primitives, Models, and Sprites.........................................................47 Chapter 5: Materials, Lights, and Shading...........................................................87 Chapter 6: Vector Shapes and Text in 3D..........................................................121 Chapter 7: Procedural 3D Content......................................................................147 Chapter 8: Interactivity.........................................................................................171 Chapter 9: Animation............................................................................................193 Chapter 10: Optimizing Tips and Tricks.............................................................217 Index.......................................................................................................................251 iii Contents Contents at a Glance ...............................................................................................iii Contents...................................................................................................................iv About the Author......................................................................................................x About the Technical Reviewer...............................................................................xi Acknowledgments.................................................................................................xii Introduction............................................................................................................xiii Chapter 1: Getting Started.......................................................................................1 Getting the Away3D library......................................................................................1 Setting up a project........................................................................................................................................2 Using Adobe Flash CS4/CS5........................................................................................................................2 Using Flash Builder........................................................................................................................................3 Using FDT.......................................................................................................................................................5 Using FlashDevelop.......................................................................................................................................6 Open source workflow using the Flex SDK and Makefiles..........................................................................7 Summary ....................................................................................................................8 Chapter 2: Creating Your First 3D Project..............................................................9 Starting up the engine .............................................................................................9 Adding 3D objects to the scene............................................................................10 Understanding constructors and initialization objects................................................................................12 Lighting the scene...................................................................................................12 Animating objects in 3D.........................................................................................14 Enabling interactivity in 3D.....................................................................................15 Summary..................................................................................................................15 Chapter 3: The View, Scene, and Camera...........................................................19 Understanding the basics .....................................................................................19 View..............................................................................................................................................................19 iv Scene............................................................................................................................................................20 Camera.........................................................................................................................................................20 Exploring the fundamentals of 3D..........................................................................20 Working with coordinates in 3D space........................................................................................................21 The rendering process.................................................................................................................................22 Setting up the chapter base class...........................................................................23 Creating and using the view .................................................................................25 Centering the vanishing point......................................................................................................................26 Clipping the viewport....................................................................................................................................26 Managing the scene.................................................................................................28 Adding and removing 3D objects................................................................................................................29 Accessing 3D objects in the scene.............................................................................................................30 Working with nested 3D objects..................................................................................................................31 Moving, rotating, and scaling in 3D.............................................................................................................32 Using containers as pivots...........................................................................................................................34 Creating and using cameras...................................................................................36 The Camera3D object..................................................................................................................................37 The TargetCamera3D object.......................................................................................................................42 The HoverCamera3D object........................................................................................................................43 Summary ..................................................................................................................46 Chapter 4: Primitives, Models, and Sprites.........................................................47 Knowing the basic terminology............................................................................47 Vertices.........................................................................................................................................................47 Faces and segments....................................................................................................................................48 Meshes and primitives.................................................................................................................................48 Billboards and sprites...................................................................................................................................48 Setting up this chapter’s base class.......................................................................48 Understanding common primitives.....................................................................50 The plane primitive.......................................................................................................................................51 Back-face culling..........................................................................................................................................52 The cube primitive........................................................................................................................................52 v The sphere primitive.....................................................................................................................................53 Understanding wire primitives and line segments................................................54 Wireframe primitives....................................................................................................................................55 Combining wireframe and regular primitives..............................................................................................56 Drawing irregular lines in space..................................................................................................................57 Using regular polygons...........................................................................................59 Working with external models .............................................................................61 Workflow when loading a model..................................................................................................................61 Optimizing external resources for size and speed................................................63 Converting a model to ActionScript.............................................................................................................64 Using the converted model..........................................................................................................................65 Creating a library of models.........................................................................................................................66 Applying bitmap filter effects to 3D objects...........................................................68 Using 3D sprites......................................................................................................69 Creating smoke using 3D sprites................................................................................................................69 Tutorial: Creating a twisted image gallery..............................................................72 Laying out the application shell...................................................................................................................73 Creating the TV sets....................................................................................................................................76 Loading the gallery image............................................................................................................................79 Creating the menu items..............................................................................................................................79 Displaying the content..................................................................................................................................81 Adding movement and interactivity.............................................................................................................83 Summary ..................................................................................................................85 Chapter 5: Materials, Lights, and Shading...........................................................87 Understanding Away3D materials........................................................................87 Using color and bitmap materials.........................................................................91 Working with wire materials.................................................................................95 Using lights and shading materials......................................................................97 Lighting in Away3D......................................................................................................................................98 Creating and configuring light sources......................................................................................................100 Controlling the intensity of a light source..................................................................................................100 Shading materials in Away3D....................................................................................................................102 vi Using normal map shading........................................................................................................................106 Using environment shading.......................................................................................................................113 Using animated and interactive materials............................................................116 Using the MovieMaterial class...................................................................................................................116 Using the VideoMaterial class...................................................................................................................118 Summary ................................................................................................................119 Chapter 6: Vector Shapes and Text in 3D..........................................................121 Working with vector graphics.............................................................................121 Vector graphics vs. raster graphics...........................................................................................................122 Creating lines and curves..........................................................................................................................122 Using the Away3D drawing API................................................................................................................123 Preparing the chapter base class.........................................................................124 Drawing 3D vector shapes...................................................................................126 Creating simple shapes with straight lines................................................................................................126 Creating curved shapes.............................................................................................................................128 Creating open-ended line segments.........................................................................................................129 Creating nonplaner shapes........................................................................................................................130 Creating shapes with holes........................................................................................................................131 Importing 3D vector shapes..................................................................................132 Extracting vector shapes from an SWF file...............................................................................................132 Animating imported vector shapes............................................................................................................135 Importing 3D Text...................................................................................................136 Extracting vector data from a font.............................................................................................................137 Extruding text..............................................................................................................................................139 Warping text along a path..........................................................................................................................140 Knowing the limitations of vector graphics in Away3D ......................................144 Summary................................................................................................................145 Chapter 7: Procedural 3D Content......................................................................147 Preparing the chapter base class ......................................................................147 Building a pyramid primitive................................................................................149 Starting with AbstractPrimitive...................................................................................................................149 vii Setting up the constructor..........................................................................................................................150 Adding public properties............................................................................................................................151 Building the Pyramid mesh........................................................................................................................152 Mapping UV coordinates...........................................................................................................................155 Using the extrusions tools....................................................................................159 Creating a ribbon using the PathExtrusion class......................................................................................159 Creating a vase with the LatheExtrusion class.........................................................................................161 Using mesh modifiers............................................................................................163 Creating a terrain using the HeightMapModifier.......................................................................................163 Summary ................................................................................................................168 Chapter 8: Interactivity.........................................................................................171 Setting up the chapter base class......................................................................171 Interacting with 3D objects in a scene...............................................................174 Introducing the MouseEvent3D object......................................................................................................175 Using MouseEvent3D’s scene coordinates..............................................................................................177 Using MouseEvent3D’s UV coordinates...................................................................................................179 First-person camera keyboard controls...............................................................182 Walking with the keyboard.........................................................................................................................183 Looking around by dragging the mouse....................................................................................................185 Summary ................................................................................................................191 Chapter 9: Animation............................................................................................193 The basics of scripted animation.......................................................................193 Using basic tweening...........................................................................................195 Path tweening.......................................................................................................197 Importing animation.............................................................................................202 Working with MD2 animations...................................................................................................................202 Working with COLLADA animations.........................................................................................................207 Creating programmatic animation with bones.....................................................209 Defining an animation rig...........................................................................................................................210 Bone tweening............................................................................................................................................210 Summary ................................................................................................................214 viii Chapter 10: Optimizing Tips and Tricks.............................................................217 Preparing the chapter base class ......................................................................217 Optimizing geometry ...........................................................................................219 Using level-of-detail objects.......................................................................................................................219 Culling and clipping polygons and meshes...............................................................................................223 Using models effectively............................................................................................................................228 Optimizing materials..............................................................................................232 Optimizing shading.....................................................................................................................................233 Conserving material instances..................................................................................................................237 Exploring general best practice techniques ........................................................239 Switching between 3D coordinate systems..............................................................................................239 Changing camera lenses...........................................................................................................................245 Summary ................................................................................................................249 Index.......................................................................................................................251 ix About the Author Rob Bateman is a web developer and community leader who has been involved in programming for over ten years. He specializes in content for the Flash platform and has always held a particular interest in 3D on the Web. In 2007, he cofounded the Away3D engine with Alexander Zadorozhny and has been leading the development of core features for the last two years. Rob lives and works in London, UK where his production and consultancy company Away Media Ltd. provides expert services in the field of browser- based 3D content. A regular speaker on the international conference circuit, he is an active member of the Flash community and hosts frequent training programs teaching Away3D to web designers and developers. His blog at www.infiniteturtles.co.uk provides further examples and musing on 3D in Flash, as well as information on upcoming appearances, training courses, and new Away3D releases. Richard Olsson is a Swedish freelance Flash developer based in the city of Malmö and has been a member of the core Away3D development team since the end of 2008. He found his interest in programming at the early age of 12, writing BASIC on an old Commodore 64 that was passed on to him from a relative who wanted something more modern. After a short detour to 3D graphics, thinking he wanted to be a 3D modeler, he went back to programming, exploring C/C++ and OpenGL before ending up working with the Flash platform. Richard is commissioned by several advertising agencies across Europe to assist with Flash 3D application development and has worked with some of the world’s largest brands. He is also a frequent Flash instructor and a contributor to the Blender open source 3D suite. On the Web, Richard himself, as well as his work and experiments, can be found at www.richardolsson.se. x