Professional Papervision3D
Part I: Getting Started Chapter 1: Understanding Flash3D ...... 3 Chapter 2: Getting Started With Papervision3D ...... 33 Chapter 3: Rezzing Primitives...... 75 Chapter 4: Adding Materials ...... 127
Part II: Working with Models and Data Chapter 5: Creating 3D Models ...... 173 Chapter 6: Working with Particle Systems ...... 221 Chapter 7: Geocoding, XML, and Databases ...... 253 Chapter 8: Gliding on AIR...... 285
Part III: Building Games and Websites Chapter 9: Incorporating 3D Physics ...... 321 Chapter 10: Building 3D Games for Wii ...... 365 Chapter 11: Integrating the Flash Media Server ...... 417 Chapter 12: Developing 3D Websites ...... 459
Part IV: Extending PV3D and Beyond Chapter 13: Making 3D Movies ...... 491 Chapter 14: Taking Virtual Tours ...... 521 Chapter 15: Adding Services ...... 553 Chapter 16: Flash & Math: Exploring Flash 10 and Beyond ...... 585
Appendix A: Pocket Reference ...... 623 Appendix B: Companion Website ...... 693 Index ...... 697
Professional Papervision3D
Michael Lively
A John Wiley and Sons, Ltd., Publication Professional Papervision3D This edition first published 2010 © 2010 John Wiley & Sons, Limited
Registered office John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex, PO19 8SQ, United Kingdom
For details of our global editorial offices, for customer services and for information about how to apply for permission to reuse the copyright material in this book please see our website at www.wiley.com.
The right of the author to be identified as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988.
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, except as permitted by the UK Copyright, Designs and Patents Act 1988, without the prior permission of the publisher.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Designations used by companies to distinguish their products are often claimed as trademarks. All brand names and product names used in this book are trade names, service marks, trademarks or registered trademarks of their respective owners. The publisher is not associated with any product or vendor mentioned in this book. This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. It is sold on the understanding that the publisher is not engaged in rendering professional services. If professional advice or other expert assistance is required, the services of a competent professional should be sought.
A catalogue record for this book is available from the British Library.
ISBN 978-0-470-74266-2
Set in 9/11 Palatino Roman by Macmillan Publishing Solutions Printed in Great Britain by Bell & Bain, Glasgow To Carlotta, my gift from God . . . and Josh, Sam, James, Mary, Jonathan, Rachael, Abigail, Davy, and Naomi . . . God’s gifts to us.
About the Author
Michael Lively is presently working at the University of Central Florida simulating nano - technology in Papervision3D and CS4. He has worked for 22 years in government and academia and has been programming in Flash and Flex for the past 10 years, building learning management systems. He has worked as the Director of Instructional Design at Northern Kentucky University, Manager of Instructional Design at the University of Cincinnati, Department Chair at Palm Beach Community College in Electronics Engineering Technology, and as a freelance consultant. He holds a BA in Mathematics, BS in Chemistry, BSEE in Electrical Engineering, MS in Physics, and is presently completing his Ph.D. in Quantum Physics. Acknowledgments
It takes many people to publish a book, and first and foremost, I like to thank everyone at Wiley for their support: specifically I would like to thank Chris Webb, Associate Publisher, whose encouragement and professionalism guided me through the entire writing process, and Kenyon Brown, development editor, who kept everything moving on track and in the right direction.
Also, a big thanks to Dan Orlando, technical editor of the book who ’ s technical insight made this book possible. We were very fortunate to have Dan aboard and he played an integral part in the book ’ s creation.
Finally, many thanks to my lovely wife Carlotta who spent many hours helping me proof and edit the book. Credits
Associate Publisher Associate Production Director Chris Webb Martin Tribe
Assistant Editor Project Coordinator, Cover Colleen Goldring David Mayhew
Publishing Assistant Copy Editor Ellie Scott Anne O’Rorke
Development Editor Compositor Kenyon Brown Macmillan Publishing Solutions, Chennai, India
Technical Editor Proofreader Dan Orlando Martin Horsgood
Project Editor Indexer Juliet Booker Jack Lewis – j&j indexing
Production Editor Cover Image Claire Spinks © Punchstock/Score by Aflo
Editorial Director Michelle Leete
Contents
Acknowledgments viii Introduction xxi
Part I: Getting Started Chapter 1: Understanding Flash3D 3 3D Coordinates in Flash 10 4 Building a 3D Flash 9 Engine in 18 Lines of Code 5 Applying Thales Theorem 5 Deriving the Scaling Equation 6 Rendering to the Screen 8 Coding Animation 9 Running the Code 11 Vanishing Point (Asymptotic Zero) 12 Adding a Camera 13 Using CS4 (3D Flash 10 Engine in 13 Lines of Code) 14 Vanishing Point 14 Using the Big 3 15 Translation 15 Rotation 21 Scaling (Basic Principles of Animation) 28 Summary 32
Chapter 2: Getting Started With Papervision3D 33 Getting Papervision3D 33 Diving into 3D 34 Shapes Versus Objects 35 Hidden Surface Drawing 37 Painter’s Algorithm 37 The View Frustum (Culling and Clipping) 38 Exploring the Guts of Papervision3D 39 DisplayObject3D 41 Running Papervision3D Four Different Ways (Hello World) 41 Using Flash or Flex 42 Contents
Building a Class 43 Writing an OOP (Object Oriented) Class 43 Formulating a Papervision3D Class 50 Running Papervision3D Applications 57 Document Class 57 Class Path 58 Flex Builder 59 Adding Motion 60 Setting the Stage 65 Align 65 Scale Mode 66 Quality 66 Using BasicView (Hello Plane) 67 Encapsulation 69 Undulating Sphere using BasicView 69 Incorporating CS4 72 Summary 73
Chapter 3: Rezzing Primitives 75 Understanding Primitives 75 Exploring the Guts of a Papervision3D Primitive 76 Rezzing Primitives to the Stage 79 Prim Template Code (Using a Wireframe Material) 80 Plane 81 Cube 83 Making Ellipsoids 95 Creating Custom Prims 96 Pinched Sphere 96 Pyramid 97 Geodesic 99 Curved Plane 100 Puckered Cube 101 Torus 102 Tube 105 Hourglass 107 Second Life Tree 108 Double-Sided Plane 110 Creating CS4 Primitives 110 Second Life Tree in Flash CS4 111 Plane 114 Cube 114 xii Contents
Pyramid 118 CS4 Super Prim (Plane, Cylinder, Cone, Sphere, Half Torus . . . ) 120 Dissecting the Big Three 125 Camera and Viewport 126 Summary 126
Chapter 4: Adding Materials 127 Applying Textures 127 Using Simple Materials (or just Materials) 129 Material Types 130 Adding Materials to a Prim 130 Bitmaps 134 Movie Clips 142 Understanding Light 147 Using Shaders 148 Adding Brightness 160 Bump Map 162 Summary 169
Part II: Working with Models and Data Chapter 5: Creating 3D Models 173 Modeling for Papervision3D 173 Bringing Down the Count 174 Gauging Performance in PV3D 175 Choosing a Modeling Application 176 Blender 177 3DSMax 178 Google SketchUp 178 Swift3D 179 Papervision3D Importers 179 What’s MD2? 180 Model Pack 181 Creating MD2 Files (MilkShape to the Rescue) 185 Making Things Move 186 What’s Up with KMZ? 187 Hello Collada 187 Single and Multiple Objects with Simple Images 188 Single Object with Multiple Images 191 Interactive Objects (or Vehicles) and Environments 194
xiii Contents
Animated Objects 198 What’s Up with SketchUp? 200 Binary versus XML 201 Writing Your Own Parsers 201 Papervision3D and Collada 202 Simple Blender Exporters 202 Simple 3DSMax Exporters 204 Understanding Collada Parsers 204 Processing Images (and Pixel Bender) 206 What’s Pixel Bender? 206 Getting Started with Pixel Bender 207 Writing a Pixel Bender Filter 208 Adding Parameters 210 Typical Point Processes 210 Grayscale 211 Complement 211 Brightness 211 Exposure 211 Color Balance 211 Binary Contrast 212 Simple Pixelate 212 Simple Blur 212 Sharpen 213 Simple Point Light 213 Bloom Brightness 214 Multiple Image Processing 214 CrossFade 214 Subtraction and Addition 214 Bringing Pixel Bender into Flash 215 Adding Pixel Bender to Papervision3D 215 Creating a Multiple Light Source 217 Animating Your Light Sources 218 Summary 219
Chapter 6: Working with Particle Systems 221 Particle Systems in PV3D 221 Creating Particles in PV3D 222 Putting Stars in a Skybox 223 Creating a Generic CS4 Particle Class 227
xiv Contents
Plug-in Media 244 Flint Particles 249 Viewing Particle Examples 249 Using Flint in Papervision3D 249 Getting More Bang Out of Flash 251 Summary 251
Chapter 7: Geocoding, XML, and Databases 253 Geocoding 253 Putting a Google Map on a PV3D Prim 254 Using Air (Flash Sandbox Security Issue) 255 Putting it on the Web with Flex 4 (Gumbo) 259 XML 262 What Is XML Anyway? 263 Taking a Historic Tour 265 Building a Planetarium in PV3D 268 Building a Flash CS4 Planetarium 273 Working with MySQL and PHP 275 What’s a Database Anyway? 275 Using Flex with a Database 276 Installing a Test Server 276 Creating a Map Maker Database 277 Automatically Generating PHP 278 Summary 284
Chapter 8: Gliding on AIR 285 Building a Simple Modeling Program 285 Placing Particles at Vertices 286 Modeling Terrains 287 Creating a Terrain Editor in AIR 293 Drawing Code 294 Using Radio Buttons 295 Adding Sliders 296 Using Buttons 296 Working with Perlin Noise 298 Adding Key listeners 299 Incorporating Adobe AIR 306 Accessing your Local File System 307 Saving BitmapData 307
xv Contents
Saving to Your Local PC Using Flash 10 310 Turning Your Webcam into a PV3D Heightmap 311 Using SQLite 312 Deploying AIR 312 Sculpting a Prim Using face3d 313 Summary 317
Part III: Building Games and Websites Chapter 9: Incorporating 3D Physics 321 Understanding the Guts of Physics 321 Adding Physics to Your Camera 322 Creating Camera Perspectives 323 Cruising the Stars Using Cameras 324 Adding Physics to the DisplayObject3D Class 326 Building Your Own Physics Classes 328 Simple Pendulums: Line, Ball, Animation 329 Double Pendulum: What’s pinning? 330 Torsion Pendulum: Cylinder on a Rod 331 Foucault Pendulum: A Little More Realism 331 Interacting with Objects in PV3D 332 Building an Interactive Cube: Face Level 332 Browser Double-Click Problems 337 Building Interactive Particles: Object Level 338 Rotating Small Sphere 338 Clicking on Particles 339 Rolling Over the x, y, z Gizmo 340 Taking it to the Next Level: 3D Wall Course 341 The Big Problem 344 Rapid Large-Scale Development 345 Using 3rd Party Physics Engines 346 WOW Physics 347 Jiglibflash 348 Implementing Jiglibflash 348 Building an Example Viewer 356 AS3Dmod 360 The Heart of AS3Dmod 361 Making National Treasure: Porting 361 Summary 363
xvi Contents
Chapter 10: Building 3D Games for Wii 365 Shooting Pool in Space 366 Five Game States: Start, Play, Win, Lose, Cancel 367 What’s Modal Control? 367 Adding a Background Image 368 Using View States 368 Building a Custom Skybox 373 Building a Jiglib Bounding Box 375 Creating Your Game Elements 377 Following Game Flow 386 Optimizing Your Game for the Web 390 Using Your Wii 393 Wii Starter Code 393 Drawing with Silly String 397 Adding Wii to Your Game 402 Playing Wii Jiglib Pong 404 Building the Bounding Box 406 Adding Jiglib 406 Moving at Constant Velocity 408 Adding AI 408 Making Game Levels and a Scoring System 408 Creating Textboxes 410 Adding Reflectivity and a SkyBox 411 Building a Wii Navigation System 411 Adding Mouse-Controlled Paddle Motion 414 Summary 415
Chapter 11: Integrating the Flash Media Server 417 Getting Started 417 Configuring Your Development Environment 419 Connecting to the FMS 419 The Big Mistake 421 Checking on Your Program’s Performance 422 Running a RSO Hello World 423 Dragging Objects 436 Building a Race Track 437 Creating a Track 438 Adding the FMS to Your Slot Car 445 Turning Your Code into a Game 456
xvii Contents
FMS Alternatives 456 Red 5 457 Wowza 457 Adobe Flash Collaboration Service 457 Summary 458
Chapter 12: Developing 3D Websites 459 What You Need to Know 459 Why Use 3D 459 Building it Right in Flash 460 Learning Photoshop 461 Creating a Site Map 461 Converting CSIS to 3D 462 Making it 3D 463 Using Reflection 464 Making your Collage 465 Making Clouds 466 Making Trees 468 Placing your Panels with XML 470 Building a Nav System 474 Hey! Where’s the Preloader? 476 Flirting with Web Spiders 483 Using HTML 484 Using SWFObject 485 Summary 488
Part IV: Extending PV3D and Beyond Chapter 13: Making 3D Movies 491 Houston . . . We’ve Got a Problem 491 Using Flash CS4 492 Rebuilding CSIS Seven Revolutions in Flash CS4 492 Adding a Preloader 499 Bridging the Gap with Flash Catalyst 500 Laying it out in Photoshop 500 Creating Code Automatically in Flash Catalyst 501 Sending Your Work to Flash Builder 503
xviii Contents
Pushing the Envelope with Flash Builder 503 Birthing a Spark 504 Animating in 3D Using Flash Builder 505 Going 3D with Photoshop 511 Getting Started Tips 511 Unlocking the Power of Google 3D Warehouse 511 Recording 3D Animations 512 Creating an Animation Recorder in PV3D 512 Storing Your Results Using the DOM 515 Animating Spark 516 Building a PV3D Studio 518 VizualPV3D 518 Summary 519
Chapter 14: Taking Virtual Tours 521 Hello “Uncle Sam” 521 Using Drones – the Yang of VR 522 Creating Augmented Reality 523 Playing a Game in Augmented Reality 527 Building 3D Worlds 550 Summary 551
Chapter 15: Adding Services 553 Tweeting 553 Creating Your Layout in Photoshop 554 Adding Interactivity in Flash Catalyst 554 Building Connectivity in Flash Builder 559 Adding 3D to Your Flickr Menu 565 Using Photoshop 565 Adding 3D in Flash Catalyst 566 Hooking it up in Flash Builder 566 Mining Yahoo’s Treasure Trove 570 Checking Your Local Weather 571 Building a CNN News Feed 577 Connecting Your Service 577 Binding Your Data 578 Configuring Your Data 578 Summary 583
xix Contents
Chapter 16: Flash & Math: Exploring Flash 10 and Beyond 585 Rebuilding the 3D Pool “Shooting” 586 Laying out Your Game’s Framework 586 Adding Code in Flash Builder 589 Optimizing PV3D for Flash 10 590 Rendering 3D Models in Flash 10 591 Creating a 3D Image Ball 591 Using drawTriangles to Build a Shaded TIE Fighter 594 An Orbiting Ship using drawTriangles 599 Taking drawTriangles to the Next Level 606 Building a Google Wormhole 606 Creating a Molecule Viewer 610 Mapping a Single Image onto an Octahedron 617 Using ILayoutElement 620 Summary 621
Appendix A: Pocket Reference 623
Appendix B: Companion Website 693
Index 697
xx Introduction
Flash programmers have always enjoyed a freedom of expression unparalleled in other programming platforms. And with the release of AS3 and CS4, Adobe has propelled that freedom of expression into the third dimension.
But long before AS3, Flash developers were experimenting with 3D. And applications like Papervision3D formalized these endeavors into a robust object - oriented class structure. The acceptance and popularity of Papervision3D has become a driver of change in the Flash developer community.
Originally conceived by Carlos Ulloa in November 2005 and converted to Great White by Ralph Hauwert, Papervision 3D has grown from approximately 20 to over 300 classes. It ’ s maintained by a 10+ member core and committer team. And with the release of Papervision3D, 2.0, developers worldwide have been contributing to this phenomenal open source project.
But what ’ s so special about 3D? Why should you even care? Besides the “ wow! ” factor, 3D has the highest learning impact on your brain: driven by the most efficient information processor in your body – your visual cortex. If you want to inject information into your client rapidly – do it visually!
It ’ s not just visual stimulation that creates a good learning environment; it ’ s also tapping into your client ’ s emotions. This is the essence of brain - based learning; an emotionally driven 3D visual experience. That ’ s why 3D RPGs (role - playing games), like Second Life, have so much popularity on the web. They ’ ve tapped into their client ’ s emotionally driven visual cortex.
Creating complete web experiences like the one described previously required extending Papervision3D beyond its present framework. And one of the purposes of Professional Papervision3D is to provide you with the necessary tools to do so.
Flash 3D coding has come a long way since the early days of Flash. And the 3D coding architectures of the future will not only be fast (incorporating such technologies as Pixel Bender and Alchemy), but also self - generating. Full 3D websites including 3D models will be created using graphical methods very similar to those of Flash Catalyst.
This book and its website bridge the gap between the old and the new, showing how Papervision3D ’ s solid OOP (object oriented programming) structure can be extended to add additional features, and looking into the next generation of 3D architecture, which incorporates auto - code generation using tools such as Flash Catalyst and Flash Builder.
Who This Book Is For Professional Papervision3D and its companion website have been specifically written for beginning and intermediate developers. It ’ s been created for those who are seeking both basic knowledge and the tools required to extend Papervision3D beyond its present capabilities. Introduction
An emphasis has been placed on learning OOP throughout the book. And various class packages are built and the principles behind their construction explained. In addition, much starter code has been created that can be used for more advanced projects.
Many basic concepts are explained in simple understandable terms and illustrated through numerous code examples. In addition, each major topic in the book has a video associated with it found on the book ’ s website. This approach is designed to maximize your grasp of the content and convey concepts difficult to explain in text. Finally, a number of supporting topics such as the use of Photoshop, 3DSMax, and Blender are covered on the book ’ s website as well.
If you ’ re trying to learn PV3D for the first time, need to master OOP, want to learn PV3D supporting technologies or to extend PV3D to do something that ’ s not currently possible – this is the book for you!
What This Book Covers The book covers everything that you need to know to get started in Papervision3D, and how to extend Papervision3D and create your own Papervision3D classes. It covers how to work with 3D models and add data connectivity to your programs. It covers how to build Wii games and 3D websites. And shows you how to extend what you ’ ve learned to build 3D models in CS4.
How This Book Is Structured The chapters in Professional Papervision3D are divided into four major parts:
❑ Getting Started ❑ Working with Models and Data ❑ Building Games and Websites ❑ Extending PV3D and Beyond
Each part has four chapters and each chapter builds on the next with an emphasis on digging into PV3D ’ s classes, understanding important OOP principles, and creating your own supporting 3D classes. Each part (and their chapters) is described below.
Part I: Getting Started In Part I, you learn all the basics to get you up to speed in using PV3D. You learn how to build 3D engines, how to download and get PV3D running, how to build classes, and how to work with primitives and materials. Finally, you extend these concepts to build CS4 primitives with materials.
Chapter 1, “ Understanding Flash3D ” — Flash 3D is one of the fastest - moving areas in web technology. In this chapter, you learn how to build a simple 3D engine in both CS3 and CS4. Using what you learn
xxii Introduction
about 3D engines you create a torus worm, carousel, and image ball. Finally, you examine Disney ’ s rules for creating realistic animation, and convert a timeline animation into ActionScript.
Chapter 2, “ Getting Started With Papervision3D ” — In this chapter, you ’ re introduced to the basics of Papervision3d, Painter ’ s Algorithm, and the view frustum. You learn about culling and clipping and examine the guts of Papervision3d. You instantiate your first primitive (a sphere) and add a wireframe material. You extend your application using BasicView, and examine the different ways to run an application.
Chapter 3, “ Rezzing Primitives ” — In this pivotal chapter, you examine how prims are made in both Papervision3D and CS4. You create your own custom prim by examining the parametric equations of different prims. And in the case of creating a Second Life tree, you analyze the potential savings between using Papervision3D and CS4. You learn about the powerful new classes and methods in the CS4 library (such as Matrix3D, drawTriangles, and vectors). And learn to create your own CS4 super prim, using a switch case and timer.
Chapter 4, “ Adding Materials ” — In this chapter you turn the corner from 3D techno - babble to application building. You learn the basics of how materials are used to create objects and about their lighting. You learn how to add brightness to a Papervision3D light source, create shades, and make bump maps. And you extend these concepts to CS4.
Part II: Working with Models and Data In Part II, you learn how to create your own 3D models and import then into PV3D. You learn to create particle systems, bring Google Maps into PV3D and CS4, and build data - driven applications in Flash, Flex, and Air.
Chapter 5, “ Creating 3D Models — In this chapter, you start with modeling and end up with Pixel Bender. Regardless of how PV3D changes over time the principles presented in this chapter will be around for a while. You ’ ll still need parsers to bring in vertex data regardless of your software environment. And Pixel Bender, the new kid on the block, will obviously become the cornerstone of any new 3D package hitting the scene.
Chapter 6, “ Working with Particle Systems ” — This chapter gives a broad overview of particles in both PV3D and CS4. You start with the PV3D particle system and add some of your own particles to it and create a starry panorama. You build a 3D particle system from scratch and create a Flash CS4 glowworm. You learn how to slice and explode particle systems, and how to use them to interact with video. You take a look at the great work that Plug - in Media is doing and learn how to incorporate FLINT into PV3D.
Chapter 7, “ Geocoding, XML, and Databases ” — In this chapter, you turn the corner from learning the inner workings of PV3D to using it to build data - driven web applications. You build a number of applications in Air, Flash CS4, and Flex, which illustrate the use of XML, PHP, and MySQL. You learn how to use the Flex data wizard to automatically create PHP code, which is used to make server requests.
Chapter 8, “ Gliding on AIR ” — In this chapter, you build your first editor in Adobe Air. During the development process you master the use of a few basic Flex components that you ’ ll use again to create other editors. Accessing your local PC file system using Air and Flash10, you save your editor results to
xxiii Introduction
your local hard drive. You learn about creating grayscale height maps and using the geometry.vertices property to bring those maps into PV3D. Using this you create a PV3D terrain viewer to view your heightmaps. Extending your editor you capture your webcam, programmatically change it into grayscale, and bring it into your PV3D terrain viewer.
Part III: Building Games and Websites In Part III you explore bringing physics into PV3D by creating custom physics classes, you modify the DisplayObject3D class, and you learn to use Jiglib. You build Wii controlled games, investigate creating multiplayer games using the Flash Media Server, and create a 3D website.
Chapter 9, “ Incorporating 3D Physics ” — In this chapter, you examine a number of approaches to bring physics into PV3D. You start by creating a spring camera, and beefing up the DisplayObject3D class to add gravity effects, to create orbiting planets. You create custom classes for oscillation and learn how to make particles interactive using the interactive scene manager (or ISM). You learn how to build large - scale applications using states, modules, and porting. You examine both WOW and Jiglibflash physics engines. Finally, you build a Jiglib Hello World example and a Jiglib example viewer.
Chapter 10, “ Building 3D Games for Wii ” — Building games is great fun and in this chapter you learn the basics of building game systems, which include game states and multi - levels. This chapter explores two different games: pool “ shooting ” and Pong. In the pool game you learn to build an entire game system based on Flex View States. And in the Pong game you learn to build a multi - level Wii controlled game. Both games are built in Flex, but the Pong game can easily be built in Flash, as it ’ s an ActionScript package. You build bounding boxes for your games by hacking the Jiglib PV3D plugin class and create a skybox.
Chapter 11, “ Integrating the Flash Media Server ” — Creating Rich Internet Applications has long been the goal of Macromedia (now Adobe). An integral part of that has been using the Flash Media Server to create interactive web experiences. In this chapter, you learn how to get your users interacting with 3D objects using remote shared objects. You also create the starter code for a slot car racing game. Finally, you examine alternatives to the FMS such as Red 5, Wowza, and the Flash Collaboration Service.
Chapter 12, “ Developing 3D Websites ” — In this chapter you convert the CSIS 2D site to a 3D site. You create custom tree, cloud, and collage classes and build a 3D navigation system from scratch using PV3D components. You examine a few important reality checks when it comes to building websites: using a design doc, learning Photoshop, and combining 2D with 3D to get an optimized 3D experience. Finally, you learn how to optimize your website for search engines by adding html text to your html - swf wrapper file and by using the SWFObject.
Part IV: Extending PV3D and Beyond In Part IV, you learn to take PV3D to the next level of performance by using Flash CS4, Photoshop3D, Flash Catalyst, and Flash Builder. You add augmented reality, services, and examine a number of CS4 rendering engines.
Chapter 13, “ Making 3D Movies ” — This chapter highlights one of the greatest challenges developers face today – the rapid change in programming architecture and platforms. This is demonstrated by building the Seven Revolutions project from the previous chapter in Flash CS4. Next, you examine Flash Catalyst and its integration into Flash Builder. Finally, the new Animate Super Effects class is used to xxiv Introduction
create a panel animation, Photoshop3D is examined, and a PV3D animation engine that saves its results to the DOM is developed.
Chapter 14, “ Taking Virtual Tours ” — As technology advances, Virtual Reality is playing a key role. In this chapter you examine using VR in the military, using augmented reality, and building 3D worlds. The majority of the chapter treats augmented reality using the FLARToolkit, which was created by Saqoosha. Saqoosha ’ s starter kit code is examined and extended to create PV3D BasicView starter code. Using this starter code a number of items are imported in FLAR including the Jiglib pong game created in the chapter on games.
Chapter 15, “ Adding Services ” — In this chapter you learn to hook up Web Services using Flash Catalyst and Flash Builder. You build a Twitter viewer, Weather checker, Flickr picker, and CNN news reader. Flash Builder has a full range of data connectivity options . . . WSDL, PHP, Coldfusion, BlazeDS, and LCDS. This makes data connectivity easier and gives you more time for creative development.
Chapter 16, “ Flash & Math: Exploring Flash 10 and Beyond ” — Flash 3D coding has come a long way, and over the next few years will transcend even optimistic expectations. In this chapter you scratch the surface of such technologies by rebuilding your 3D pool “ shooting ” game in Flash Catalyst, and visiting a number of CS4 rendering examples.
Appendices The book has two appendices:
Appendix A is a pocket reference that contains all the important formulas and code snippets from each chapter. It purpose is for quick reference when programming applications that may use similar code snippets.
Appendix B describes the book ’ s companion website in detail, describing its purpose and various training courses designed to help convey the book ’ s material and the use of supporting technologies.
What You Need to Use This Book Professional Papervision3D starts by emphasizing the use of Flash and moves to Flex 3 (Gumbo), Air, Flash Catalyst and Flash Builder. In addition, Papervision3D uses a number of supporting technologies such as Blender, Photoshop, 3DSMax, and so on.
The applications that you need to complete the exercises in this book fully are Flash CS4, Photoshop, Pixel Bender, Blender (or 3DSMax), Flash Catalyst, MilkShape, and Flash Builder.
To help you master a number of these supporting technologies the book ’ s website has the following video tutorials to help you get started:
❑ Photoshop ❑ Illustrator (Basics) ❑ 3DS Max
xxv Introduction
❑ Blender ❑ SketchUp ❑ Flash CS4 ❑ Flash Catalyst ❑ Flash Builder (Gumbo)
If you ’ re on a budget, Blender is free (at www.blender.org ) and the Adobe products can be downloaded for full 30 days trials from www.adobe.com , the Eclipse + Flex SDK version is free, and Pixel Bender is free at http://labs.adobe.com/technologies/pixelbender/ .
The Book ’s Companion Website The book ’ s companion website (at www.professionalpapervision.com ) is an integral part of conveying the book ’ s contents. The days when code was small and easily explained in a few written pages are gone. Today, many Flash applications are thousands of lines of code with large supporting libraries. Using video to help convey the concepts behind such applications is the primary goal of the book ’ s website.
You ’ ll find a large set of video resources on the book ’ s website designed to help you come up to speed on the book ’ s topics. The website consists of:
❑ Book chapter videos on each major topic ❑ Training videos on topics supporting PV3D ❑ Chapter code for download and viewing ❑ Additional topics from the book ❑ Bonus examples and new Flash 3D technologies ❑ Current 3D topics from UCF
Using the book and the book ’ s training videos together should help you come up to speed rapidly in Flash 3D and its supporting technologies.
How to Use This Book Each major topic in the book has a video associated with it. This approach is designed to maximize your grasp of the content and convey concepts difficult to explain in text. In some cases, portions of the book were written to go hand - in - hand with its counterpart video content. To maximize your absorption of the contents, first read a section in the book, watch its video, try the code examples, and then try extending the code ’ s capability.
xxvi Introduction Conventions To help you get the most from the text and keep track of what ’ s happening, we ’ ve used a number of conventions throughout the book:
❑ We show file names, URLs, and code within the text like so: persistence.properties . ❑ We present code in two different ways:
In code examples we highlight new and important code with a gray background. The gray highlighting is not used for code that’s less important in the present context, or has been shown before. Source Code Many Flash applications are thousands of lines of code and have large supporting libraries. Due to the size of many 3D applications you no longer have the luxury of showing every line of code that goes behind each application. And in many instances, in this book, after a discussion of how code works the reader is sent to the book ’ s website to download the entire application for review.
The amount of code written for the book was significant (about 119,000 files), and as the book was written the versions of PV3D and other supporting software changed. So as opposed to trying to keep track of every different version of PV3D, the version of PV3D (and other supporting software) used to create each application was included with each application.
All of the source code used in this book is available for download at http://www.wrox.com . Once at the site, simply locate the book ’ s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book ’ s detail page to obtain all the source code for the book.
Because many books have similar titles, you may find it easiest to search by ISBN; this book ’ s ISBN is 978 - 0 - 470 - 74266 - 2.
After you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/download .aspx to see the code available for this book and all other Wrox books.
Errata We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save other readers hours of frustration and at the same time you ’ ll be helping us provide even higher quality information.
To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book
xxvii Introduction
list including links to each book ’ s errata is also available at http://www.wrox.com/misc-pages/ booklist.shtml .
If you don ’ t spot “ your ” error on the Book Errata page, go to http://www.wrox.com/contact/ techsupport.shtml and complete the form there to send us the error you ’ ve found. We ’ ll check the information and, if appropriate, post a message to the book ’ s errata page and fix the problem in subsequent editions of the book.
p2p.wrox.com For author and peer discussion, join the P2P forums at http://p2p.wrox.com. The forums comprise a web - based system for you to post messages relating to Wrox books and related technologies and to interact with other readers and technology users. The forums offer a subscription feature to email you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com you ’ ll find a number of different forums that ’ ll help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
1. Go to http://p2p.wrox.com and click the Register link. 2. Read the terms of use and click Agree. 3. Complete the required information to join as well as any optional information you wish to provide and click Submit. 4. You will receive an email with information describing how to verify your account and complete the joining process.
You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.
Once you join, you can post new messages and respond to messages posted by other users. You can read messages at any time on the web. If you would like to have new messages from a particular forum emailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
xxviii Part I: Getting Started
Chapter 1: Understanding Flash3D
Chapter 2: Getting StartedPart With Papervision3DXX
Chapter 3: RezzingPart Primitives Title
Chapter 4: Adding Materials Part 1: Getting Started
In Part I, you learn all the basics to get you up to speed for using Papervision3D (PV3D). You learn how to build 3D engines, download and get PV3D running, build classes, and work with primitives and materials. Finally, you extend these concepts to build CS4 primitives with materials.
2 Understanding Flash3D
Flash programmers have always enjoyed a freedom of expression unparalleled by other programming platforms. And with the release of CS4, Adobe has propelled that freedom of expression into the 3rd dimension.
But 3D didn ’ t start with AS3. Flash developers were experimenting with 3D long before. And applications like Papervision3D formalized these endeavors into a robust object - oriented class structure. The acceptance and popularity of Papervision3D has become a driver of change in the Flash developer community. But underneath the 300 classes that make up Papervision3D still beats the original algorithms used by the early developers of Flash 3D.
Understanding how to create 3D in Flash is essential to fully grasping Papervision3D and applications like it. As you learn how 3D engines were originally constructed, you ’ ll gain both an insight into Papervision3D ’ s core architecture and an appreciation of its robust structure. Its complexity will fade into a set of fundamental 3D algorithms.
Papervision3D, at its core, is a perspective projection engine, where projection simply means transforming a 3D “ object ” space into 2D Flash x and y screen space. And surprisingly, the whole process hinges on one equation: a perspective scaling equation derived from Thales Theorem:
T = scale = focal length/(focal length + z)
In the equation above, T is the perspective scale, and z is the distance from the projection plane. The focal length (or calculated “ screen ” location) determines the amount of perspective provided in the view. You use this equation to create an illusion of depth by scaling 2D surfaces.
In this chapter, you use Thales Theorem to create a simple 3D engine in just 19 lines of code. Then using Flash CS4 you rebuild your engine in just 13 lines of code. You cover the big 3: translation, rotation and scaling. And applying what you ’ ve learned you create a 3D torus worm, carousel, and image ball. Finally, you cover the basics of making believable animation, and how to turn a timeline animation into pure ActionScript. Part 1: Getting Started
But before you get started it ’ s important that you understand the coordinate system you ’ ll be rendering your objects in. It ’ s a little different to the way you learned it in math class, and well worth reviewing.
3D Coordinates in Flash 10 Understanding the Flash coordinate system is vital to rendering in Papervision3D or CS4. With the transition to Flash Player 10, every ActionScript display object has a z property. Adding a 3rd dimension allows an object to move towards and away from the viewpoint of the user using perspective scaling.
As a side note, this extra dimension is easily handled using a 3D transformation matrix that incorporates the three fundamental transformations found in all physical systems, such as translation, rotation, and scaling. Every 3D object within Papervision3D has a Matrix3D. The Matrix3D class supports complex transformations of 3D geometry, such as rotation, scaling, and translation.
But unlike other 3D systems, especially those you might have met in math class, the y and z - axes are reversed as shown in Figure 1.1.