1 /** 2 * Hilo 3 * Copyright 2015 alibaba.com 4 * Licensed under the MIT License 5 */ 6 7 /** 8 * @language=zh 9 * @class Camera3d 伪3D虚拟摄像机。 10 * @module hilo/game/Camera3d 11 * @requires hilo/core/Hilo 12 * @requires hilo/core/Class 13 * @property {Number} fv 镜头视点距离(屏幕视点相对眼睛距离,绝对了坐标缩放比例)。 14 * @property {Number} fx 镜头视点X(屏幕视点相对屏幕左上角X距离)。 15 * @property {Number} fy 镜头视点Y(屏幕视点相对屏幕左上角Y距离)。 16 * @property {Object} stage 3D对象所在容器,可以是stage或container,结合ticker时是必须参数,用来Z深度排序。 17 * @property {Number} x 镜头三维坐标x。 18 * @property {Number} y 镜头三维坐标y。 19 * @property {Number} z 镜头三维坐标z。 20 * @property {Number} rotationX X轴旋转角度。 21 * @property {Number} rotationY Y轴旋转角度。 22 * @property {Number} rotationZ Z轴旋转角度。 23 */ 24 var Camera3d = (function(){ 25 26 var degtorad = Math.PI / 180; 27 28 //Rotate the axis. 29 function rotateX(x, y, z, ca, sa) {//rotate x 30 return { 31 x: x, 32 y: y * ca - z * sa, 33 z: y * sa + z * ca 34 }; 35 } 36 function rotateY(x, y, z, ca, sa) {//rotate y 37 return { 38 x: x * ca - z * sa, 39 y: y, 40 z: x * sa + z * ca 41 }; 42 } 43 function rotateZ(x, y, z, ca, sa) {//rotate z 44 return { 45 x: x * ca - y * sa, 46 y: x * sa + y * ca, 47 z: z 48 }; 49 } 50 51 var Camera3d = Class.create(/** @lends Camera3d.prototype */{ 52 53 constructor: function(properties){ 54 properties.x = properties.x || 0; 55 properties.y = properties.y || 0; 56 properties.z = properties.z || 0; 57 properties.rotationX = properties.rotationX || 0; 58 properties.rotationY = properties.rotationY || 0; 59 properties.rotationZ = properties.rotationZ || 0; 60 61 Hilo.copy(this, properties); 62 }, 63 64 /** 65 * @language=zh 66 * 仿射矩阵位移变换,不同于直接修改Camera3d.x/y/z. 是在Camera3d依次做坐标位移 - 旋转变换 后,再加上一个位移变换。主要功能可以做Zoomin/out 功能 67 * @param {Number} x x坐标 68 * @param {Number} y y坐标 69 * @param {Number} z z坐标 70 */ 71 translate : function(x,y,z){ 72 this.tx = x; 73 this.ty = y; 74 this.tz = z; 75 }, 76 77 /** 78 * @language=zh 79 * 旋转X轴方向角度,相当于欧拉角系统的 beta。 80 * @param {Number} angle 旋转角度。 81 */ 82 rotateX : function(angle){ 83 this.rotationX = angle; 84 }, 85 86 /** 87 * @language=zh 88 * 旋转Y轴方向角度,相当于欧拉角系统的 gamma。 89 * @param {Number} angle 旋转角度。 90 */ 91 rotateY : function(angle){ 92 this.rotationY = angle; 93 }, 94 95 /** 96 * @language=zh 97 * 旋转Z轴方向角度,相当于欧拉角系统的 alpha。 98 * @param {Number} angle 旋转角度。 99 */ 100 rotateZ : function(angle){ 101 this.rotationZ = angle; 102 }, 103 104 /** 105 * @language=zh 106 * 将三维坐标转换投影为二维坐标。 107 * @param {object} vector3D 三维坐标对象,必须含有x, y, z属性。 108 * @param {View} view Hilo.View对象,用于自动转换坐标。 109 * @returns {Object} 二维坐标对象,包括缩放和z属性,例子:{x:x, y:y, z:z, scale} 110 */ 111 project : function(vector3D, view){ 112 113 var rx = this.rotationX * degtorad, 114 ry = this.rotationY * degtorad, 115 rz = this.rotationZ * degtorad, 116 117 cosX = Math.cos(rx), sinX = Math.sin(rx), 118 cosY = Math.cos(ry), sinY = Math.sin(ry), 119 cosZ = Math.cos(rz), sinZ = Math.sin(rz), 120 121 // 旋转变换前的 仿射矩阵位移, 122 dx = vector3D.x - this.x, 123 dy = vector3D.y - this.y, 124 dz = vector3D.z - this.z; 125 126 // 旋转矩阵变换 127 var vector = rotateZ(dx, dy, dz, cosZ, sinZ); 128 vector = rotateY(vector.x, vector.y, vector.z, cosY, sinY); 129 vector = rotateX(vector.x, vector.y, vector.z, cosX, sinX); 130 131 // 最后的仿射矩阵变换 132 if(this.tx) vector.x -= this.tx; 133 if(this.ty) vector.y -= this.ty; 134 if(this.tz) vector.z -= this.tz; 135 136 var perspective = this.fv / (this.fv + vector.z), 137 _x = vector.x * perspective, 138 _y = -vector.y * perspective; 139 140 var result = { 141 x : _x + this.fx, 142 y : _y + this.fy, 143 z : -vector.z, 144 scale : perspective 145 }; 146 147 if(view){ 148 view.x = result.x; 149 view.y = result.y; 150 view.z = result.z; 151 view.scaleX = result.scale; 152 view.scaleY = result.scale; 153 } 154 155 return result; 156 }, 157 158 /** 159 * @language=zh 160 * Z深度排序。 161 */ 162 sortZ : function(){ 163 this.stage.children.sort(function(view_a, view_b){ 164 return view_a.z > view_b.z; 165 }); 166 }, 167 168 /** 169 * @language=zh 170 * Ticker 轮询使用。 171 */ 172 tick : function(){ 173 this.sortZ(); 174 } 175 176 }); 177 178 return Camera3d; 179 180 })();