Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

218 rindas
7.5 KiB

  1. -- 处理3D相机的移动控制
  2. require("CSharpConstants")
  3. local Camera3DControllerTool = class("Camera3DControllerTool")
  4. function Camera3DControllerTool:ctor()
  5. -- 相机的球面坐标系
  6. -- 半径,离原点的距离,一般是一个正数
  7. -- 经度,0~2PI,0代表正前方,PI代表正后方
  8. -- 纬度,HALF_PI代表水平位置,+PI代表底,0代表顶
  9. self.spherical = cc.vec3(30,1.4,1.4);
  10. -- 相机旋转中心点
  11. self.rotationCenter = cc.vec3(0,0,0);
  12. end
  13. function Camera3DControllerTool:moveTo(button , offsetPos)
  14. -- ALT + 鼠标右键拉近拉远
  15. if button == MouseButtons.Right then
  16. --local offset = (offsetPos.x + -offsetPos.y) * 0.1;
  17. local offset = -offsetPos.x + offsetPos.y;
  18. self.spherical.x = math.max(self.spherical.x + offset * 0.00075 * self.spherical.x);
  19. -- 拉得太近了,就把原点丢远一点
  20. if self.spherical.x <= 1 then
  21. -- 丢10格
  22. local fullOut = 30;
  23. local ray = app.mainCamera:pickRay(0.5,0.5);
  24. -- 沿着屏幕射线把旋转中心点往前移动
  25. self.rotationCenter = cc.vec3Add(cc.vec3Mul(ray:getDirection() , fullOut) , self.rotationCenter);
  26. self.spherical.x = self.spherical.x + fullOut;
  27. end
  28. -- ALT + 鼠标中键平移
  29. elseif button == MouseButtons.Middle then
  30. local offset = app.mainCamera:moveByViewPlane(cc.vec3(offsetPos.x * 0.00075 * self.spherical.x , -offsetPos.y * 0.00075 * self.spherical.x, 0));
  31. self.rotationCenter.x = self.rotationCenter.x + offset.x;
  32. self.rotationCenter.y = self.rotationCenter.y + offset.y;
  33. self.rotationCenter.z = self.rotationCenter.z + offset.z;
  34. -- ALT + 鼠标左键旋转
  35. elseif button == MouseButtons.Left then
  36. self.spherical.y = self.spherical.y + offsetPos.x * 0.003;
  37. self.spherical.z = self.spherical.z + offsetPos.y * 0.003;
  38. end
  39. self:update();
  40. end
  41. function Camera3DControllerTool:update()
  42. -- 计算相对rotationCenter的位置
  43. local pos = Math.SphericalToCartesian(cc.vec3(self.spherical.x , self.spherical.y , self.spherical.z));
  44. pos.x = pos.x + self.rotationCenter.x
  45. pos.y = pos.y + self.rotationCenter.y
  46. pos.z = pos.z + self.rotationCenter.z
  47. app.mainCamera:getNode():setTranslation(pos);
  48. app.mainCamera:lookAt(self.rotationCenter);
  49. end
  50. function Camera3DControllerTool:MoveDown(button, x, y)
  51. -- 按下ALT控制相机
  52. if app.cs.getModifierKeys() ~= Keys.Alt then return end
  53. -- 鼠标的按下位置
  54. self.startPos = app.visibleRect:convertScreenToGLView(x, y);
  55. -- 相机的位置
  56. self.originPos = app.mainCamera:getNode():getPosition();
  57. end
  58. function Camera3DControllerTool:MouseMove(button , x , y)
  59. local glViewPos = app.visibleRect:convertScreenToGLView(x, y);
  60. if self.startPos then
  61. local delta = cc.pSub(glViewPos , self.startPos);
  62. -- 计算结束点
  63. self:moveTo(button , delta);
  64. self.startPos = glViewPos;
  65. end
  66. local point = self:convertScreenToWorldPos(x, y);
  67. local strMapCoord = "";
  68. if app.editor and app.editor.Map then
  69. local tilePos = app.editor.Map:getTilePosition(point.x , point.z);
  70. strMapCoord = string.format(" TilePos:%d,%d" , tilePos.x , tilePos.y);
  71. end
  72. local ort = cc.Orientation:new();
  73. ort:fromQuaternion(app.mainCamera:getNode():getQuaternion());
  74. local dir = ort:toDirection();
  75. return string.format("Mouse:%d,%d Coordinate:%.2f,%.2f,%.2f GLView:%d,%d CameraPos:%.2f,%.2f,%.2f CameraDir:%.2f,%.2f,%.2f" .. strMapCoord , x , y , point.x , point.y , point.z , glViewPos.x , glViewPos.y
  76. , app.mainCamera:getNode():getTranslationX()
  77. , app.mainCamera:getNode():getTranslationY()
  78. , app.mainCamera:getNode():getTranslationZ()
  79. , dir.x
  80. , dir.y
  81. , dir.z
  82. );
  83. end
  84. function Camera3DControllerTool:MoveUp(button, x, y)
  85. if not self.startPos then return end
  86. -- 计算结束点
  87. --self:moveTo(app.visibleRect:convertScreenToGLView(x, y));
  88. self.startPos = nil;
  89. self.originPos = nil;
  90. end
  91. function Camera3DControllerTool:KeyDown(Control , Alt , Shift , KeyCode)
  92. -- F按下
  93. if Control == false and Alt == false and Shift == false and KeyCode == Keys.F then
  94. local node;
  95. -- 如果有物件被选中,就定位到第一个物件
  96. if app.editor and app.editor.SelectedNodes and next(app.editor.SelectedNodes) then
  97. node = next(app.editor.SelectedNodes);
  98. end
  99. -- 如果没物件被选中,则定位到当前物件
  100. if not node and app.editor and app.editor.node then
  101. node = app.editor.node;
  102. end
  103. if node then
  104. self.rotationCenter = node:getWorldTranslation();
  105. local box = node:getSelectBox();
  106. local max = box:getMax();
  107. local distance = math.max(max.x , max.y , max.z , 1)
  108. self.spherical = cc.vec3(distance ,1.4,1.4);
  109. else
  110. self.rotationCenter = cc.vec3(0,0,0);
  111. self.spherical = cc.vec3(30,1.4,1.4);
  112. end
  113. self:update();
  114. end
  115. end
  116. -- 摆放物件时使用这个函数计算物件应该放在什么位置
  117. function Camera3DControllerTool:convertScreenToWorldPos(mouseX , mouseY)
  118. local floorPlane = cc.Plane:new(cc.vec3(0,1,0) , 0);
  119. -- 计算出mainlayer的世界位置
  120. local screenSize = cc.Director:getInstance():getWinSizeInPixels();
  121. local ray = app.mainCamera:pickRay(mouseX / screenSize.width , mouseY / screenSize.height);
  122. local distance = ray:intersectsPlane(floorPlane);
  123. if distance >= 0 then
  124. return ray:getPoint(distance);
  125. else
  126. return ray:getPoint(0);
  127. end
  128. end
  129. -- 刷新设置时调用
  130. function Camera3DControllerTool:refreshSetting()
  131. app.background:removeAllChildren();
  132. -- 画手机外壳
  133. if app.setting.ShowIPhone5 then
  134. local bgIphone5 = cc.Sprite:create("Editor/iphone5.png");
  135. app.background:addChild(bgIphone5)
  136. end
  137. -- 设置OpenGL背景颜色
  138. gl.clearColor(app.setting.BackgroundColor.r / 255 , app.setting.BackgroundColor.g / 255 , app.setting.BackgroundColor.b / 255 ,0);
  139. if app.setting.ShowGrid then
  140. -- 创建网格
  141. local vertexFormat = {};
  142. table.insert(vertexFormat , {size = 3 , usage = cc.VertexFormatUsage.POSITION});
  143. table.insert(vertexFormat , {size = 4 , usage = cc.VertexFormatUsage.COLOR});
  144. local vertices = ManualVertices:new(vertexFormat);
  145. vertices:setColor(app.setting.GridColor);
  146. local viewSize = cc.size(100,100);
  147. local gridWidth = app.setting.GridSize.width
  148. local gridHeight = app.setting.GridSize.height
  149. local gridCountY = math.floor(viewSize.height / gridHeight);
  150. vertices:setColor(app.setting.GridColor);
  151. -- 画横向网格
  152. for i = -gridCountY , gridCountY do
  153. if i ~= 0 then
  154. vertices:drawLine(cc.vec3(-viewSize.width , 0 , i * gridHeight) , cc.vec3(viewSize.width , 0 , i * gridHeight))
  155. end
  156. end
  157. vertices:setColor(app.setting.GridColor);
  158. -- 画竖向网格
  159. local gridCountX = math.floor(viewSize.width / gridWidth);
  160. for i = -gridCountX , gridCountX do
  161. if i ~= 0 then
  162. vertices:drawLine(cc.vec3(i * gridWidth , 0 , -viewSize.height) , cc.vec3(i * gridWidth , 0 , viewSize.height))
  163. end
  164. end
  165. vertices:setColor(app.setting.AxisColor);
  166. -- 画轴
  167. vertices:drawLine(cc.vec3(-viewSize.width , 0 , 0) , cc.vec3(viewSize.width , 0 , 0))
  168. vertices:drawLine(cc.vec3(0 , 0 , -viewSize.height) , cc.vec3(0 , 0 , viewSize.height))
  169. local mesh = cc.Mesh:createMesh(vertexFormat , vertices:getVertexCount());
  170. mesh:setPrimitiveType(cc.PrimitiveType.LINES);
  171. mesh:setVertexData(vertices:getVertices() , 0 , vertices:getVertexCount());
  172. mesh:setBoundingBox(vertices:getBoundingBox());
  173. local model = cc.Model:create(mesh);
  174. local material = cc.Material:create("core/preload/shaders/ccDefault.material#ShaderPositionColor3D");
  175. material:setParameterAutoBinding("CC_MVPMatrix" , "WORLD_VIEW_PROJECTION_MATRIX");
  176. model:setMaterialPointer(material);
  177. local node = cc.Node3D:create();
  178. node:addComponent(model);
  179. app.background:addChild(node);
  180. end
  181. end
  182. return Camera3DControllerTool;