MainClass.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.useParallaxOcclusion;
  36. material.parallaxScaleBias
  37. material.specularPower = 500.0;
  38. mesh.material = material;
  39. material.specularColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  40. var combo = <HTMLSelectElement>document.querySelector("#effectList");
  41. combo.onchange = () => {
  42. switch (combo.value) {
  43. case "none":
  44. material.bumpTexture = null;
  45. break;
  46. case "bump":
  47. material.bumpTexture = normalsHeightTexture;
  48. material.useParallax = false;
  49. break;
  50. case "parallax":
  51. material.bumpTexture = normalsHeightTexture;
  52. material.useParallax = true;
  53. material.useParallaxOcclusion = false;
  54. break;
  55. case "POM":
  56. material.bumpTexture = normalsHeightTexture;
  57. material.useParallax = true;
  58. material.useParallaxOcclusion = true;
  59. break;
  60. }
  61. material.markDirty();
  62. };
  63. var label = <HTMLLabelElement>document.querySelector("#scaleBiasLabel");
  64. label.innerHTML = "Scale Bias: " + material.parallaxScaleBias;
  65. document.addEventListener("keydown", (event) => {
  66. if (event.key == "Up") {
  67. material.parallaxScaleBias += 0.01;
  68. label.innerHTML = "Scale Bias: " + material.parallaxScaleBias.toFixed(2);
  69. }
  70. else if (event.key == "Down") {
  71. material.parallaxScaleBias -= 0.01;
  72. label.innerHTML = "Scale Bias: " + material.parallaxScaleBias.toFixed(2);
  73. }
  74. });
  75. //this.scene.debugLayer.show();
  76. // This creates a light, aiming 0,1,0 - to the sky.
  77. //var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), this.scene);
  78. //var light = new BABYLON.DirectionalLight("light1", new BABYLON.Vector3(0, 0, 1), this.scene);
  79. var light = new BABYLON.PointLight("light1", new BABYLON.Vector3(50, 50, -70), this.scene);
  80. //light.intensity = 0.5;
  81. //light.specular = new BABYLON.Color3(0, 0, 0);
  82. var self = this;
  83. //BABYLON.Animation.CreateAndStartAnimation("automotion", mesh, "rotation.x", 60, 480, 0, Math.PI * 2, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  84. //BABYLON.Animation.CreateAndStartAnimation("automotion", mesh, "rotation.y", 60, 400, 0, Math.PI * 2, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  85. //BABYLON.Animation.CreateAndStartAnimation("automotion", mesh, "rotation.z", 60, 800, 0, Math.PI * 2, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  86. // Register a render loop to repeatedly render the scene
  87. this.engine.runRenderLoop(function () {
  88. canvas.width = window.innerWidth-50;
  89. canvas.height = window.innerHeight-50;
  90. self.scene.render();
  91. });
  92. // Watch for browser/canvas resize events
  93. window.addEventListener("resize", function () {
  94. self.engine.resize();
  95. });
  96. // Variables for Mouse interaction
  97. var mouseDown = false;
  98. var isRotating = false;
  99. var shiftState = false;
  100. var initialMousePosition: BABYLON.Vector2;
  101. var lastMousePosition: BABYLON.Vector2;
  102. var thresholdToEnterRotating = 5 * 5; // Raised to power of 2
  103. var initialTransformationMatrix: BABYLON.Matrix;
  104. this.scene.registerBeforeRender(function () {
  105. //light.position = camera.position;
  106. //mousemov = false;
  107. });
  108. this.scene.afterRender = function () {
  109. };
  110. // Handle User Input
  111. canvas.addEventListener("pointermove", (event) => {
  112. if (mouseDown) {
  113. // Detect a change in shift key
  114. if (shiftState != event.shiftKey) {
  115. // Reset the states as we change the rotation mode
  116. initialMousePosition = new BABYLON.Vector2(event.clientX, event.clientY);
  117. initialTransformationMatrix = mesh.getPivotMatrix();
  118. shiftState = event.shiftKey;
  119. }
  120. var curMousePosition = new BABYLON.Vector2(event.clientX, event.clientY);
  121. var deltaPos = curMousePosition.subtract(initialMousePosition);
  122. // Check if we have to enter rotating mode
  123. if (isRotating == false) {
  124. if (deltaPos.lengthSquared() > thresholdToEnterRotating) {
  125. isRotating = true;
  126. }
  127. else {
  128. return;
  129. }
  130. }
  131. // We are already or just got in rotation mode
  132. var degToRad = Math.PI / 180;
  133. var rotationFactor = 1 / 4; // 4 pixels for 1 degree
  134. var rotatingMatrix = BABYLON.Matrix.RotationYawPitchRoll(
  135. event.shiftKey ? 0 : (-deltaPos.x * degToRad * rotationFactor),
  136. -deltaPos.y * degToRad * rotationFactor,
  137. event.shiftKey ? (deltaPos.x * degToRad * rotationFactor) : 0);
  138. var wmtx = self.camera.getWorldMatrix().clone();
  139. var invwmtx = wmtx.clone();
  140. invwmtx.invert();
  141. wmtx.setTranslation(BABYLON.Vector3.Zero());
  142. invwmtx.setTranslation(BABYLON.Vector3.Zero());
  143. mesh.setPivotMatrix(initialTransformationMatrix.multiply(invwmtx).multiply(rotatingMatrix).multiply(wmtx));
  144. }
  145. });
  146. canvas.addEventListener("pointerdown", function (event) {
  147. mouseDown = true;
  148. initialMousePosition = new BABYLON.Vector2(event.clientX, event.clientY);
  149. initialTransformationMatrix = mesh.getPivotMatrix().clone();
  150. shiftState = event.shiftKey;
  151. });
  152. canvas.addEventListener("pointerup", function (event) {
  153. mouseDown = false;
  154. });
  155. }
  156. }
  157. }