经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTMLCSS » HTML5 » 查看文章
Layabox 3D游戏开发学习笔记---射线检测,鼠标控制物体运动
来源:cnblogs  作者:彼岸花在开  时间:2018/9/25 20:05:26  对本文有异议

核心要点:3D物体碰撞是靠射线检测,射线与碰撞器相撞获取对应的碰撞点信息。

  1. class RayPicking03 {
  2. private ray: Laya.Ray;
  3. private point: Laya.Vector2 = new Laya.Vector2();
  4. private _outHitInfo: Laya.RaycastHit;
  5. private _position: Laya.Vector3;
  6. private _upVector3: Laya.Vector3;
  7. private _vector3: Laya.Vector3;
  8. private _offsetVector3: Laya.Vector3;
  9. private box: Laya.MeshSprite3D;
  10. private _rotateV3:Laya.Vector3;
  11. private camera: Laya.Camera;
  12. private label: Laya.Label;
  13. constructor() {
  14. Laya3D.init(0, 0, true);//初始化3D
  15. Laya.stage.scaleMode = "full";//屏幕缩放方式
  16. Laya.stage.screenMode = "none";
  17. Laya.Stat.show();
  18. var scene: Laya.Scene = Laya.stage.addChild(new Laya.Scene()) as Laya.Scene;//添加场景
  19.  
  20. this.point = new Laya.Vector2();//位置信息
  21. this.ray = new Laya.Ray(new Laya.Vector3(0, 0, 0), new Laya.Vector3(0, 0, 0));//初始化射线
  22. this._outHitInfo = new Laya.RaycastHit();//初始化射线信息
  23. this._position = new Laya.Vector3(0, 0.25, 0);//位置
  24. this._upVector3 = new Laya.Vector3(0, 1, 0);
  25. this._rotateV3 = new Laya.Vector3(1, 0, 1);
  26. this._vector3 = new Laya.Vector3();
  27. this._offsetVector3 = new Laya.Vector3(0, 0.15, 0)
  28. //初始化照相机
  29. this.camera = scene.addChild(new Laya.Camera(0, 0.1, 100)) as Laya.Camera;
  30. this.camera.transform.translate(new Laya.Vector3(0, 2, 5));
  31. this.camera.transform.rotate(new Laya.Vector3(-15, 0, 0), true, false);
  32. this.camera.clearColor = null;
  33. //方向光
  34. var directionLight: Laya.DirectionLight = scene.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
  35. directionLight.color = new Laya.Vector3(0.6, 0.6, 0.6);
  36. directionLight.direction = new Laya.Vector3(1, -1, -1);
  37. var plane: Laya.MeshSprite3D = scene.addChild(new Laya.MeshSprite3D(new Laya.PlaneMesh(6, 6, 10, 10))) as Laya.MeshSprite3D;//创建平面物体
  38. var planeMat: Laya.StandardMaterial = new Laya.StandardMaterial();//标准材质
  39. planeMat.diffuseTexture = Laya.Texture2D.load("../bin/res/layabox.png");//添加材质
  40. planeMat.albedo = new Laya.Vector4(0.9, 0.9, 0.9, 1);
  41. plane.meshRender.material = planeMat;
  42. var meshCollider = plane.addComponent(Laya.MeshCollider) as Laya.MeshCollider;//网格碰撞器
  43. meshCollider.mesh = plane.meshFilter.sharedMesh;//网格过滤器,获取共享网格
  44.  
  45. this.box = scene.addChild(new Laya.MeshSprite3D(new Laya.SphereMesh(0.2, 8, 8))) as Laya.MeshSprite3D;//创建立方体
  46. var mat: Laya.StandardMaterial = new Laya.StandardMaterial();
  47. mat.diffuseTexture = Laya.Texture2D.load("../bin/res/layabox.png");
  48. this.box.meshRender.material = mat;
  49. Laya.timer.frameLoop(1, this, this.checkHit);
  50. this.loadUI();
  51. }
  52. private checkHit(): void {
  53. this.box.transform.position = this._position;
  54. //this.box.transform.rotate(this._rotateV3, true, false)
  55. //从屏幕空间生成射线
  56. this.point.elements[0] = Laya.MouseManager.instance.mouseX;//鼠标X坐标
  57. this.point.elements[1] = Laya.MouseManager.instance.mouseY;//鼠标Y坐标
  58. this.camera.viewportPointToRay(this.point, this.ray);//从摄像机到鼠标点击位置生成射线
  59. Laya.Physics.rayCast(this.ray, this._outHitInfo, 30, 0);//生成射线
  60. }
  61. private loadUI(): void {
  62. this.label = new Laya.Label();
  63. this.label.text = "点击移动";
  64. this.label.pos(Laya.Browser.clientWidth / 2.5, 100);
  65. this.label.fontSize = 50;
  66. this.label.color = "#40FF40";
  67. Laya.stage.addChild(this.label);
  68. //鼠标事件
  69. Laya.stage.on(Laya.Event.MOUSE_UP, this, function (): void {
  70. if (this._outHitInfo.distance !== -1) {
  71. Laya.Vector3.add(this._outHitInfo.position, this._offsetVector3, this._vector3);
  72. Laya.Tween.to(this._position, { x: this._vector3.x, y: this._vector3.y, z: this._vector3.z }, 500/**,Ease.circIn*/);
  73. }
  74. });
  75. }
  76. }

 

 

 

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号