当前位置:   article > 正文

Laya 实操十三:3D场景创建+transform_laya3d transformpoint

laya3d transformpoint

创建3D场景

  1. export default class Test extends Laya.Script {
  2. constructor() {
  3. super();
  4. //搭建场景
  5. var scene3d = new Laya.Scene3D();
  6. Laya.stage.addChild(scene3d);
  7. //创建相机
  8. var camera = new Laya.Camera(0,0.3,100);
  9. camera.fieldOfView = 60;
  10. camera.clearColor = new Laya.Vector4(0,0,0,1);
  11. scene3d.addChild(camera);
  12. camera.transform.translate(new Laya.Vector3(0,1,0));
  13. camera.cullingMask = 1<<2;//层级
  14. //camera.removeAllLayers();
  15. //camera.addLayer(1);
  16. //camera.cullingMask = (1<<1)|(1<<2);
  17. //物体
  18. //几何网格/网格图元
  19. var cube = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createBox(1,1,1));
  20. scene3d.addChild(cube);
  21. //位置
  22. cube.transform.translate(new Laya.Vector3(0,0,-10));
  23. //层级
  24. cube.layer = 2;
  25. //材质
  26. var m = new Laya.UnlitMaterial();
  27. //贴图
  28. Laya.Texture2D.load("res/background.png",Laya.Handler.create(this,function(tex){
  29. m.albedoTexture = tex;
  30. }));
  31. //绘制
  32. cube.meshRenderer.material = m;
  33. }
  34. }

 transform

  1. export default class Test extends Laya.Script {
  2. constructor() {
  3. super();
  4. //搭建场景
  5. var scene3d = new Laya.Scene3D();
  6. Laya.stage.addChild(scene3d);
  7. //创建相机
  8. var camera = new Laya.Camera(0,0.3,100);
  9. camera.fieldOfView = 60;
  10. camera.clearColor = new Laya.Vector4(0,0,0,1);
  11. scene3d.addChild(camera);
  12. camera.transform.translate(new Laya.Vector3(0,1,0));
  13. camera.cullingMask = 1<<2;//层级
  14. //camera.removeAllLayers();
  15. //camera.addLayer(1);
  16. //camera.cullingMask = (1<<1)|(1<<2);
  17. //物体
  18. //几何网格/网格图元
  19. var cube = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createBox(1,1,1));
  20. scene3d.addChild(cube);
  21. //位置
  22. cube.transform.translate(new Laya.Vector3(0,0,-10));
  23. //层级
  24. cube.layer = 2;
  25. //材质
  26. var m = new Laya.UnlitMaterial();
  27. //贴图
  28. Laya.Texture2D.load("res/background.png",Laya.Handler.create(this,function(tex){
  29. m.albedoTexture = tex;
  30. }));
  31. //绘制
  32. cube.meshRenderer.material = m;
  33. console.log(cube.transform.localPosition);
  34. console.log(cube.transform.position);
  35. this.cube = cube;
  36. }
  37. cube = null;
  38. onUpdate(){
  39. //平移
  40. /*
  41. var pos = this.cube.transform.position;
  42. pos.z -= 5 * Laya.timer.delta/1000;
  43. this.cube.transform.position = pos;
  44. */
  45. /*
  46. var offset = new Laya.Vector3(0,0,-5 * Laya.timer.delta/1000);
  47. this.cube.transform.translate(offset,false);
  48. */
  49. //缩放
  50. /*
  51. var s = new Laya.Vector3(5,2,1);
  52. this.cube.transform.scale = s;
  53. */
  54. //旋转
  55. //欧拉
  56. //yaw-y,pitch-x,roll-z
  57. //this.cube.transform.rotationEuler = new Laya.Vector3(15,0,0);
  58. //四元数
  59. /*
  60. var rot = new Laya.Quaternion();
  61. Laya.Quaternion.createFromYawPitchRoll(0,15,0,rot);
  62. this.cube.transform.rotation = rot;
  63. */
  64. //持续
  65. //this.cube.transform.rotate(new Laya.Vector3(0,30 * Laya.timer.delta/1000,0),false,false);
  66. //转动后移动
  67. /*
  68. this.cube.transform.rotationEuler = new Laya.Vector3(15,0,0);
  69. var forward = new Laya.Vector3(0,0,0);
  70. this.cube.transform.getForward(forward);
  71. var s = 5 * Laya.timer.delta/1000;
  72. var offset = new Laya.Vector3(s*forward.x,s*forward.y,s*forward.z);
  73. this.cube.transform.translate(offset,true);
  74. */
  75. //朝向
  76. //this.cube.transform.lookAt(new Laya.Vector3(0,-10,-20),new Laya.Vector3(0,1,0));
  77. }
  78. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/214621
推荐阅读
  

闽ICP备14008679号