home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Templates / Flash / flashmo_101_3d_carousel / org / papervision3d / cameras / Camera3D.as next >
Text File  |  2007-07-18  |  8KB  |  205 lines

  1. /*
  2.  *  PAPER    ON   ERVIS  NPAPER ISION  PE  IS ON  PERVI IO  APER  SI  PA
  3.  *  AP  VI  ONPA  RV  IO PA     SI  PA ER  SI NP PE     ON AP  VI ION AP
  4.  *  PERVI  ON  PE VISIO  APER   IONPA  RV  IO PA  RVIS  NP PE  IS ONPAPE
  5.  *  ER     NPAPER IS     PE     ON  PE  ISIO  AP     IO PA ER  SI NP PER
  6.  *  RV     PA  RV SI     ERVISI NP  ER   IO   PE VISIO  AP  VISI  PA  RV3D
  7.  *  ______________________________________________________________________
  8.  *  papervision3d.org + blog.papervision3d.org + osflash.org/papervision3d
  9.  */
  10.  
  11. /*
  12.  * Copyright 2006 (c) Carlos Ulloa Matesanz, noventaynueve.com.
  13.  *
  14.  * Permission is hereby granted, free of charge, to any person
  15.  * obtaining a copy of this software and associated documentation
  16.  * files (the "Software"), to deal in the Software without
  17.  * restriction, including without limitation the rights to use,
  18.  * copy, modify, merge, publish, distribute, sublicense, and/or sell
  19.  * copies of the Software, and to permit persons to whom the
  20.  * Software is furnished to do so, subject to the following
  21.  * conditions:
  22.  *
  23.  * The above copyright notice and this permission notice shall be
  24.  * included in all copies or substantial portions of the Software.
  25.  *
  26.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  28.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  30.  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  31.  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  32.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  33.  * OTHER DEALINGS IN THE SOFTWARE.
  34.  */
  35.  
  36. // ______________________________________________________________________
  37. //                                               CameraObject3D: Camera3D
  38.  
  39. package org.papervision3d.cameras
  40. {
  41. import org.papervision3d.Papervision3D;
  42. import org.papervision3d.core.Number3D;
  43. import org.papervision3d.core.Matrix3D;
  44. import org.papervision3d.core.proto.CameraObject3D;
  45. import org.papervision3d.objects.DisplayObject3D;
  46.  
  47. /**
  48. * The Camera3D class creates a camera that views the area around a target object.
  49. * <p/>
  50. * A camera defines the view from which a scene will be rendered. Different camera settings would present a scene from different points of view.
  51. * <p/>
  52. * 3D cameras simulate still-image, motion picture, or video cameras of the real world. When rendering, the scene is drawn as if you were looking through the camera lens.
  53. */
  54. public class Camera3D extends CameraObject3D
  55. {
  56.     // __________________________________________________________________________
  57.     //                                                                     PUBLIC
  58.  
  59.     /**
  60.     * A DisplayObject3D object that specifies the current position the camera is looking at.
  61.     */
  62.     public var target :DisplayObject3D;
  63.  
  64.  
  65.     /**
  66.     * A Number3D object that specifies the desired position of the camera in 3D space. Only used when calling update().
  67.     */
  68.     public var goto :Number3D;
  69.  
  70.     /**
  71.     * A Number3D object that specifies the desired rotation of the camera in 3D space. Only used when calling update().
  72.     */
  73. //    public var gotoRotation :Number3D;
  74.  
  75.     /**
  76.     * A Number3D object that specifies the desired position of the camera's target in 3D space. Only used when calling update().
  77.     */
  78. //    public var gotoTarget :Number3D;
  79.  
  80.     // __________________________________________________________________________
  81.     //                                                                      N E W
  82.     // NN  NN EEEEEE WW    WW
  83.     // NNN NN EE     WW WW WW
  84.     // NNNNNN EEEE   WWWWWWWW
  85.     // NN NNN EE     WWW  WWW
  86.     // NN  NN EEEEEE WW    WW
  87.  
  88.     /**
  89.     * The Camera3D constructor creates cameras that view the area around a target object.
  90.     *
  91.     * Its initial position can be specified in the initObject.
  92.     *
  93.     * @param    zoom        This value specifies the scale at which the 3D objects are rendered. Higher values magnify the scene, compressing distance. Use it in conjunction with focus.
  94.     * <p/>
  95.     * @param    focus        This value is a positive number representing the distance of the observer from the front clipping plane, which is the closest any object can be to the camera. Use it in conjunction with zoom.
  96.     * <p/>
  97.     * @param    initObject    An optional object that contains user defined properties with which to populate the newly created DisplayObject3D.
  98.     * <p/>
  99.     * It includes x, y, z, rotationX, rotationY, rotationZ, scaleX, scaleY scaleZ and a user defined extra object.
  100.     * <p/>
  101.     * If extra is not an object, it is ignored. All properties of the extra field are copied into the new instance. The properties specified with extra are publicly available.
  102.     * <p/>
  103.     * The following initObject property is also recognized by the constructor:
  104.     * <ul>
  105.     * <li><b>sort</b>: A Boolean value that determines whether the 3D objects are z-depth sorted between themselves when rendering. The default value is true.</li>
  106.     * </ul>
  107.     */
  108.     public function Camera3D( target:DisplayObject3D=null, zoom:Number=2, focus:Number=100, initObject:Object=null )
  109.     {
  110.         super( zoom, focus, initObject );
  111.  
  112.         this.target = target || DisplayObject3D.ZERO;
  113.  
  114.         this.goto = new Number3D( this.x, this.y, this.z );
  115. //        this.goTarget = new Number3D( this.target.x, this.target.y, this.target.z );
  116.     }
  117.  
  118.     // ___________________________________________________________________________________________________
  119.     //                                                                                   T R A N S F O R M
  120.     // TTTTTT RRRRR    AA   NN  NN  SSSSS FFFFFF OOOO  RRRRR  MM   MM
  121.     //   TT   RR  RR  AAAA  NNN NN SS     FF    OO  OO RR  RR MMM MMM
  122.     //   TT   RRRRR  AA  AA NNNNNN  SSSS  FFFF  OO  OO RRRRR  MMMMMMM
  123.     //   TT   RR  RR AAAAAA NN NNN     SS FF    OO  OO RR  RR MM M MM
  124.     //   TT   RR  RR AA  AA NN  NN SSSSS  FF     OOOO  RR  RR MM   MM
  125.  
  126.     /**
  127.     * [internal-use] Transforms world coordinates into camera space.
  128.     */
  129.     public override function transformView( transform:Matrix3D=null ):void
  130.     {
  131.         this.lookAt( this.target );
  132.  
  133.         super.transformView();
  134.     }
  135.  
  136.     // ___________________________________________________________________________________________________
  137.     //
  138.     // UU  UU PPPPP  DDDDD    AA   TTTTTT EEEEEE
  139.     // UU  UU PP  PP DD  DD  AAAA    TT   EE
  140.     // UU  UU PPPPP  DD  DD AA  AA   TT   EEEE
  141.     // UU  UU PP     DD  DD AAAAAA   TT   EE
  142.     //  UUUU  PP     DDDDD  AA  AA   TT   EEEEEE
  143.  
  144.     /**
  145.     * [experimental] Hovers the camera around as the user moves the mouse, without changing the distance to the target. This greatly enhances the 3D illusion.
  146.     *
  147.     * @param    type    Type of movement.
  148.     * @param    mouseX    Indicates the x coordinate of the mouse position in relation to the canvas MovieClip.
  149.     * @param    mouseY    Indicates the y coordinate of the mouse position in relation to the canvas MovieClip.
  150.     */
  151.     public function hover( type:Number, mouseX:Number, mouseY:Number ):void
  152.     {
  153.         var target   :DisplayObject3D = this.target;
  154.         var goto     :Number3D = this.goto;
  155. //        var gotoTarget :Number3D = this.gotoTarget;
  156.  
  157.         var camSpeed :Number = 8;
  158.  
  159.         switch( type )
  160.         {
  161.             case 0:
  162.                 // Sphere mapped camera (free)
  163.                 var dX       :Number = goto.x - target.x;
  164.                 var dZ       :Number = goto.z - target.z;
  165.  
  166.                 var ang      :Number = Math.atan2( dZ, dX );
  167.                 var dist     :Number = Math.sqrt( dX*dX + dZ*dZ );
  168.                 var xMouse   :Number = 0.5 * mouseX;
  169.  
  170.                 var camX :Number = dist * Math.cos( ang - xMouse );
  171.                 var camZ :Number = dist * Math.sin( ang - xMouse );
  172.                 var camY :Number = goto.y - 300 * mouseY;
  173.  
  174.                 this.x -= (this.x - camX) /camSpeed;
  175.                 this.y -= (this.y - camY) /camSpeed;
  176.                 this.z -= (this.z - camZ) /camSpeed;
  177.                 break;
  178.  
  179.             case 1:
  180.                 this.x -= (this.x - 1000 * mouseX) /camSpeed;
  181.                 this.y -= (this.y - 1000 * mouseY) /camSpeed;
  182. //                this.z -= (this.z - ) /camSpeed;
  183.                 break;
  184.  
  185. /*
  186.             // BROKEN
  187.             case ???:
  188.                 // Sphere mapped camera (fixed)
  189.                 var dX = cam.pos.gx - cam.target.x;
  190.                 var dZ = cam.pos.gz - cam.target.z;
  191.                 ang -= ( ang - (Math.atan2( dZ, dX ) - iCanvas._xmouse/300) ) /camSpeed;
  192.                 dist -= ( dist - Math.sqrt( dX*dX + dZ*dZ ) ) /camSpeed;
  193.                 var camX = dist * Math.cos( ang );
  194.                 var camZ = dist * Math.sin( ang );
  195.                 var camY = -iCanvas._ymouse/3;
  196.  
  197.                 cam.pos.x = camX;
  198.                 cam.pos.y -= (cam.pos.y - (camY + cam.pos.gy) ) /camSpeed;
  199.                 cam.pos.z = camZ;
  200.                 break;
  201. */
  202.         }
  203.     }
  204. }
  205. }