Scene3D.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. function Scene3D()
  2. {
  3. this.canvas;
  4. this.engine;
  5. this.scene;
  6. this.camera;
  7. this.light;
  8. };
  9. Scene3D.prototype.setLights = function (){
  10. var DLight1 = new BABYLON.DirectionalLight("dl", new BABYLON.Vector3(1,-1,1), this.scene);
  11. //DLight1.specular=new BABYLON.Color3(2, 2, 2);
  12. var DLight2 = new BABYLON.DirectionalLight("dl", new BABYLON.Vector3(-1,-1,1), this.scene);
  13. //DLight2.specular=new BABYLON.Color3(2, 2, 2);
  14. var SpotLight = new BABYLON.SpotLight("sl", new BABYLON.Vector3(-10,10,0), new BABYLON.Vector3(10,-10,0), Math.PI*1.5, 1, this.scene)
  15. /*
  16. var light0 = new BABYLON.PointLight("Omni0", new BABYLON.Vector3(1.9,3.2,4.5), this.scene);
  17. //light0.diffuse = new BABYLON.Color3(1, 0, 0);
  18. //light0.specular = new BABYLON.Color3(1, 1, 1);
  19. light0.intensity = 3.3;
  20. */
  21. /*
  22. var light1 = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(600, 600, 0), this.scene);
  23. light1.diffuse = new BABYLON.Color3(1, 1, 1);
  24. light1.specular = new BABYLON.Color3(0.3, 0.3, 0.3);
  25. light1.position = new BABYLON.Vector3(250, 400, 0);
  26. light1.intensity = 2;
  27. */
  28. /*
  29. var light1 = new BABYLON.PointLight("Omni1", new BABYLON.Vector3(8.6,13.4,-0.9), this.scene);
  30. //light1.diffuse = new BABYLON.Color3(1, 0, 0);
  31. //light1.specular = new BABYLON.Color3(1, 1, 1);
  32. light1.intensity = 8.4;
  33. var light2 = new BABYLON.PointLight("Omni2", new BABYLON.Vector3(-3.5,-6.9,7.2), this.scene);
  34. //light2.diffuse = new BABYLON.Color3(1, 0, 0);
  35. //light2.specular = new BABYLON.Color3(1, 1, 1);
  36. light2.intensity = 7.3;
  37. */
  38. }
  39. Scene3D.prototype.setCamera = function () {
  40. // Create a rotating camera
  41. this.camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI/2, 0, 1000, BABYLON.Vector3.Zero(), this.scene);
  42. this.camera.attachControl(this.canvas, true);
  43. this.camera.lowerBetaLimit = 0.1;
  44. this.camera.upperBetaLimit = (Math.PI / 2) * 0.99;
  45. this.camera.lowerRadiusLimit = 125;
  46. this.camera.checkCollisions = true;
  47. };
  48. Scene3D.prototype.setFreeCamera=function()
  49. {
  50. var followCam = new BABYLON.FollowCamera("fcam", this.camera.position, this.scene);
  51. followCam.radius = 200;
  52. followCam.maxCameraSpeed = 10;
  53. return followCam;
  54. };
  55. Scene3D.prototype.setProductCamera = function (position) {
  56. this.camera.setTarget=position;
  57. };
  58. Scene3D.prototype.createScene = function () {
  59. // Attach it to handle user inputs (keyboard, mouse, touch)
  60. this.canvas=document.getElementById('canvas3d');
  61. this.engine = new BABYLON.Engine(this.canvas, true);
  62. this.scene = new BABYLON.Scene(this.engine);
  63. this.setCamera();
  64. this.setLights();
  65. //this.scene.debugLayer.show();
  66. };
  67. //实时渲染
  68. Scene3D.prototype.render= function () {
  69. this.scene.collisionsEnabled = true;
  70. var scene=this.scene;
  71. this.engine.runRenderLoop(
  72. function () {
  73. scene.render();
  74. }
  75. );
  76. };