MainClass.ts 9.2 KB

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