MainClass.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. module Sandbox {
  2. export class MainClass {
  3. engine: BABYLON.Engine;
  4. scene: BABYLON.Scene;
  5. camera: BABYLON.TargetCamera;
  6. start() {
  7. BABYLON.Engine.CodeRepository = "/src/";
  8. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  9. if (BABYLON.Engine.isSupported() == false) {
  10. console.log("web browser doesn't support WebGL");
  11. return;
  12. }
  13. // Get the canvas element from our HTML below
  14. var canvas = <HTMLCanvasElement>document.querySelector("#renderCanvas");
  15. // Load the BABYLON 3D engine
  16. this.engine = new BABYLON.Engine(canvas, true);
  17. // Now create a basic Babylon Scene object
  18. this.scene = new BABYLON.Scene(this.engine);
  19. // Change the scene background color to green.
  20. this.scene.clearColor = new BABYLON.Color3(56/256, 87/256, 145/256);
  21. // This creates and positions a free camera
  22. this.camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -100), this.scene);
  23. this.camera.setTarget(BABYLON.Vector3.Zero());
  24. //this.camera.attachControl(canvas);
  25. var mesh = BABYLON.Mesh.CreateBox("box01", 70, this.scene);
  26. mesh.position = new BABYLON.Vector3(0, -30, 0);
  27. var diffuseTexture = new BABYLON.Texture("assets/rock.png", this.scene);
  28. var normalsHeightTexture = new BABYLON.Texture("assets/rock_nh.png", this.scene);
  29. //var diffuseTexture = new BABYLON.Texture("assets/BrickWall.png", this.scene);
  30. //var normalsHeightTexture = new BABYLON.Texture("assets/BrickWall_nh.png", this.scene);
  31. var material = new BABYLON.StandardMaterial("mtl01", this.scene);
  32. material.diffuseTexture = diffuseTexture;
  33. //material.bumpTexture = normalsHeightTexture;
  34. //material.useParallax = true;
  35. //material.specularPower = 500.0;
  36. mesh.material = material;
  37. material.specularColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  38. var combo = <HTMLSelectElement>document.querySelector("#effectList");
  39. combo.onchange = () => {
  40. switch (combo.value) {
  41. case "none":
  42. material.bumpTexture = null;
  43. break;
  44. case "bump":
  45. material.bumpTexture = normalsHeightTexture;
  46. //material.useParallax = false;
  47. break;
  48. case "parallax":
  49. material.bumpTexture = normalsHeightTexture;
  50. //material.useParallax = true;
  51. //material.useParallaxOcclusion = false;
  52. break;
  53. case "POM":
  54. material.bumpTexture = normalsHeightTexture;
  55. //material.useParallax = true;
  56. //material.useParallaxOcclusion = true;
  57. break;
  58. }
  59. material.markDirty();
  60. };
  61. //var label = <HTMLLabelElement>document.querySelector("#scaleBiasLabel");
  62. //label.innerHTML = "Scale Bias: " + material.parallaxScaleBias;
  63. //document.addEventListener("keydown", (event) => {
  64. // if (event.key == "Up") {
  65. // material.parallaxScaleBias += 0.01;
  66. // label.innerHTML = "Scale Bias: " + material.parallaxScaleBias.toFixed(2);
  67. // }
  68. // else if (event.key == "Down") {
  69. // material.parallaxScaleBias -= 0.01;
  70. // label.innerHTML = "Scale Bias: " + material.parallaxScaleBias.toFixed(2);
  71. // }
  72. //});
  73. //this.scene.debugLayer.show();
  74. // This creates a light, aiming 0,1,0 - to the sky.
  75. //var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), this.scene);
  76. //var light = new BABYLON.DirectionalLight("light1", new BABYLON.Vector3(0, 0, 1), this.scene);
  77. var light = new BABYLON.PointLight("light1", new BABYLON.Vector3(50, 50, -70), this.scene);
  78. //light.intensity = 0.5;
  79. //light.specular = new BABYLON.Color3(0, 0, 0);
  80. var self = this;
  81. //BABYLON.Animation.CreateAndStartAnimation("automotion", mesh, "rotation.x", 60, 480, 0, Math.PI * 2, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  82. //BABYLON.Animation.CreateAndStartAnimation("automotion", mesh, "rotation.y", 60, 400, 0, Math.PI * 2, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  83. //BABYLON.Animation.CreateAndStartAnimation("automotion", mesh, "rotation.z", 60, 800, 0, Math.PI * 2, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  84. // Register a render loop to repeatedly render the scene
  85. this.engine.runRenderLoop(function () {
  86. canvas.width = window.innerWidth-50;
  87. canvas.height = window.innerHeight-50;
  88. self.scene.render();
  89. });
  90. // Watch for browser/canvas resize events
  91. window.addEventListener("resize", function () {
  92. self.engine.resize();
  93. });
  94. // Variables for Mouse interaction
  95. var mouseDown = false;
  96. var isRotating = false;
  97. var shiftState = false;
  98. var initialMousePosition: BABYLON.Vector2;
  99. var lastMousePosition: BABYLON.Vector2;
  100. var thresholdToEnterRotating = 5 * 5; // Raised to power of 2
  101. var initialTransformationMatrix: BABYLON.Matrix;
  102. this.scene.registerBeforeRender(function () {
  103. //light.position = camera.position;
  104. //mousemov = false;
  105. });
  106. this.scene.afterRender = function () {
  107. };
  108. // Handle User Input
  109. canvas.addEventListener("pointermove", (event) => {
  110. if (mouseDown) {
  111. // Detect a change in shift key
  112. if (shiftState != event.shiftKey) {
  113. // Reset the states as we change the rotation mode
  114. initialMousePosition = new BABYLON.Vector2(event.clientX, event.clientY);
  115. initialTransformationMatrix = mesh.getPivotMatrix();
  116. shiftState = event.shiftKey;
  117. }
  118. var curMousePosition = new BABYLON.Vector2(event.clientX, event.clientY);
  119. var deltaPos = curMousePosition.subtract(initialMousePosition);
  120. // Check if we have to enter rotating mode
  121. if (isRotating == false) {
  122. if (deltaPos.lengthSquared() > thresholdToEnterRotating) {
  123. isRotating = true;
  124. }
  125. else {
  126. return;
  127. }
  128. }
  129. // We are already or just got in rotation mode
  130. var degToRad = Math.PI / 180;
  131. var rotationFactor = 1 / 4; // 4 pixels for 1 degree
  132. var rotatingMatrix = BABYLON.Matrix.RotationYawPitchRoll(
  133. event.shiftKey ? 0 : (-deltaPos.x * degToRad * rotationFactor),
  134. -deltaPos.y * degToRad * rotationFactor,
  135. event.shiftKey ? (deltaPos.x * degToRad * rotationFactor) : 0);
  136. var wmtx = self.camera.getWorldMatrix().clone();
  137. var invwmtx = wmtx.clone();
  138. invwmtx.invert();
  139. wmtx.setTranslation(BABYLON.Vector3.Zero());
  140. invwmtx.setTranslation(BABYLON.Vector3.Zero());
  141. mesh.setPivotMatrix(initialTransformationMatrix.multiply(invwmtx).multiply(rotatingMatrix).multiply(wmtx));
  142. }
  143. });
  144. canvas.addEventListener("pointerdown", function (event) {
  145. mouseDown = true;
  146. initialMousePosition = new BABYLON.Vector2(event.clientX, event.clientY);
  147. initialTransformationMatrix = mesh.getPivotMatrix().clone();
  148. shiftState = event.shiftKey;
  149. });
  150. canvas.addEventListener("pointerup", function (event) {
  151. mouseDown = false;
  152. });
  153. }
  154. }
  155. }