MainClass.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /// <reference path="simpleinteractionhelper.ts" />
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  8. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  9. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  10. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  11. return c > 3 && r && Object.defineProperty(target, key, r), r;
  12. };
  13. var Sandbox;
  14. (function (Sandbox) {
  15. var Vector2 = BABYLON.Vector2;
  16. var Group2D = BABYLON.Group2D;
  17. var Canvas2D = BABYLON.Canvas2D;
  18. var Size = BABYLON.Size;
  19. var PrimitivePointerInfo = BABYLON.PrimitivePointerInfo;
  20. var TestA = (function () {
  21. function TestA() {
  22. }
  23. TestA = __decorate([
  24. className("TestA")
  25. ], TestA);
  26. return TestA;
  27. }());
  28. Sandbox.TestA = TestA;
  29. var TestB = (function (_super) {
  30. __extends(TestB, _super);
  31. function TestB() {
  32. _super.apply(this, arguments);
  33. }
  34. TestB = __decorate([
  35. className("TestB")
  36. ], TestB);
  37. return TestB;
  38. }(TestA));
  39. Sandbox.TestB = TestB;
  40. function className(name) {
  41. return function (target) {
  42. target["__bjsclassName__"] = name;
  43. };
  44. }
  45. Sandbox.className = className;
  46. function getClassName(object) {
  47. var name = null;
  48. if (object instanceof Object) {
  49. var classObj = Object.getPrototypeOf(object);
  50. name = classObj.constructor["__bjsclassName__"];
  51. }
  52. if (!name) {
  53. name = typeof object;
  54. }
  55. return name;
  56. }
  57. Sandbox.getClassName = getClassName;
  58. var MainClass = (function () {
  59. function MainClass() {
  60. }
  61. MainClass.prototype.start = function () {
  62. BABYLON.Engine.CodeRepository = "/src/";
  63. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  64. if (BABYLON.Engine.isSupported() === false) {
  65. console.log("web browser doesn't support WebGL");
  66. return;
  67. }
  68. var tb = new TestB();
  69. var res = getClassName(tb);
  70. res = getClassName(12);
  71. var pb = Object.getPrototypeOf(tb);
  72. var pa = Object.getPrototypeOf(pb);
  73. var nameB = pb.constructor["__className__"];
  74. var nameA = pa.constructor["__className__"];
  75. // Get the canvas element from our HTML below
  76. var htmlCanvasElement = document.querySelector("#renderCanvas");
  77. //var canvas2d = <HTMLCanvasElement>document.querySelector("#canvas2d");
  78. //var context = canvas2d.getContext("2d");
  79. //context.font = "8pt Lucida Console";
  80. //context.fillStyle = "red";
  81. //context.textBaseline = 'top';
  82. //context.fillText("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, 0);
  83. // Load the BABYLON 3D engine
  84. this.engine = new BABYLON.Engine(htmlCanvasElement, true);
  85. // Now create a basic Babylon Scene object
  86. this.scene = new BABYLON.Scene(this.engine);
  87. //this.scene.debugLayer.show();
  88. //var instData = new BABYLON.Rectangle2DInstanceData();
  89. //instData._instancesArray = new Float32Array(100);
  90. //instData._instanceOffset = 0;
  91. //instData.zBias = 1;
  92. //instData.transform = Matrix.Identity();
  93. //instData.properties = new Vector3(1, 2, 3);
  94. //var effect = this.engine.createEffect({ vertex: "rect2d", fragment: "rect2d" }, ["zBias", "transform", "properties"], [], [], "");
  95. //var node = instData.getClassTreeInfo();
  96. //setTimeout(() => {
  97. // var offsets = node.classContent.getOffsetLocations(effect);
  98. // },
  99. // 1000);
  100. // Change the scene background color to green.
  101. this.scene.clearColor = new BABYLON.Color3(56 / 256, 87 / 256, 145 / 256);
  102. // This creates and positions a free camera
  103. var hpi = Math.PI / 2;
  104. this.camera = new BABYLON.ArcRotateCamera("camera1", -hpi, hpi, 150, new BABYLON.Vector3(0, 0, 0), this.scene);
  105. this.camera.setTarget(BABYLON.Vector3.Zero());
  106. this.camera.attachControl(htmlCanvasElement);
  107. //this.scene.debugLayer.show();
  108. var canvas = Canvas2D.CreateScreenSpace(this.scene, "ScreenCanvas", new Vector2(0, 100), new Size(600, 600), Canvas2D.CACHESTRATEGY_DONTCACHE);
  109. canvas.backgroundFill = Canvas2D.GetSolidColorBrushFromHex("#C0808080");
  110. canvas.backgroundRoundRadius = 50;
  111. var g1 = Group2D.CreateGroup2D(canvas, "g1", new Vector2(300, 300), new Size(600, 600));
  112. g1.pointerEventObservable.add(function (d, s) {
  113. if (s.mask === PrimitivePointerInfo.PointerDown) {
  114. canvas.setPointerCapture(d.pointerId, d.relatedTarget);
  115. }
  116. else if (s.mask === PrimitivePointerInfo.PointerUp) {
  117. canvas.releasePointerCapture(d.pointerId, d.relatedTarget);
  118. }
  119. console.log("Pointer Down on " + d.relatedTarget.id);
  120. }, PrimitivePointerInfo.PointerDown | PrimitivePointerInfo.PointerUp);
  121. //var text = Text2D.Create(g1, "text", 100, 100, "20pt Lucida Console", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  122. //text.origin = new Vector2(0.0, 1.0);
  123. var rectList = Array();
  124. var ss = 600;
  125. var rs = 100;
  126. for (var i = 0; i < 50; i++) {
  127. var posX = Math.random() * ss;
  128. var posY = Math.random() * ss;
  129. var w = Math.random() * rs;
  130. var h = Math.random() * rs;
  131. var rect = BABYLON.Rectangle2D.Create(g1, "rect" + i.toString(), posX, posY, w, h, BABYLON.Canvas2D.GetSolidColorBrush(new BABYLON.Color4(Math.random(), Math.random(), Math.random(), 1)));
  132. rect.roundRadius = Math.random() * Math.min(rect.size.width, rect.size.height) * 0.4;
  133. rect.origin = new Vector2(0.5, 0.5);
  134. rectList.push(rect);
  135. }
  136. var z = canvas.getActualZOffset();
  137. z = g1.getActualZOffset();
  138. z = rectList[0].getActualZOffset();
  139. z = rectList[1].getActualZOffset();
  140. setInterval(function () {
  141. //g1.rotation += 0.003;
  142. //rectList.forEach(r => r.rotation += 0.003);
  143. //rectList[49].position = new Vector2(rectList[49].position.x + 0.1, rectList[49].position.y+0.6);
  144. // sprite.rotation += 0.005;
  145. //rect.rotation += 0.005;
  146. }, 10);
  147. //var ii = new IntersectInfo2D();
  148. //htmlCanvasElement.addEventListener("pointerdown", (event: PointerEvent) => {
  149. // var pickPos = new Vector2(event.offsetX, canvas.actualSize.height - event.offsetY);
  150. // ii.pickPosition = pickPos;
  151. // ii.findFirstOnly = false;
  152. // if (canvas.intersect(ii)) {
  153. // console.log(`pickPos: ${pickPos.toString()}, intersect: ${ii.intersectedPrimitives[0].prim.id} at: ${ii.intersectedPrimitives[0].intersectionLocation.toString()}`);
  154. // }
  155. //});
  156. //var texture = new Texture("assets/rock2.png", this.scene, true, true, Texture.NEAREST_SAMPLINGMODE);
  157. //var sprite = Sprite2D.Create(canvas, "sprite1", 250, 250, texture, new Size(128, 128), new Vector2(0,0));
  158. //sprite.origin = Vector2.Zero();
  159. //var canvas = Canvas2D.CreateWorldSpace(this.scene, "ScreenCanvas", new Vector3(0, 0, 0), Quaternion.RotationYawPitchRoll(Math.PI / 4, Math.PI/4, 0), new Size(100, 100), 4, Mesh.DEFAULTSIDE, Canvas2D.CACHESTRATEGY_CANVAS);
  160. //canvas.backgroundFill = Canvas2D.GetSolidColorBrushFromHex("#C0C0C040");
  161. //canvas.backgroundRoundRadius = 50;
  162. //var g1 = Group2D.CreateGroup2D(canvas, "group", Vector2.Zero(), null);
  163. //g1.scale = 4;
  164. //var rect = Rectangle2D.Create(g1, "rect", 50, 50, 25, 25, null, Canvas2D.GetGradientColorBrush(new Color4(0.9, 0.3, 0.9, 1), new Color4(1.0, 1.0, 1.0, 1)));
  165. //rect.borderThickness = 2;
  166. //rect.roundRadius = 2;
  167. //var insideRect = Rectangle2D.Create(rect, "insideRect", 0, 0, 10, 10, Canvas2D.GetSolidColorBrushFromHex("#0040F0FF"));
  168. //insideRect.roundRadius = 1;
  169. //setInterval(() => {
  170. // rect.rotation += 0.01;
  171. //}, 10);
  172. //let canvas2 = Canvas2D.CreateScreenSpace(this.scene, "ScreenCanvas", new Vector2(0, 0), new Size(this.engine.getRenderWidth(), this.engine.getRenderHeight()), Canvas2D.CACHESTRATEGY_DONTCACHE);
  173. //var g1 = Group2D.CreateGroup2D(canvas2, "g1", new Vector2(0, 100));
  174. ////g1.rotation = Math.PI / 5;
  175. //var g2 = Group2D.CreateGroup2D(g1, "g2", new Vector2(0, 0), new Size(500, 500));
  176. //var texture = new Texture("assets/rock2.png", this.scene, true, true, Texture.NEAREST_SAMPLINGMODE);
  177. //var sprite = Sprite2D.Create(g2, "sprite1", 250, 250, texture, new Size(128, 128), new Vector2(0,0));
  178. ////sprite.invertY = true;
  179. //sprite.origin = Vector2.Zero();
  180. //var r = Rectangle2D.Create(g1, "rect1", 200, 200, 90, 90, Canvas2D.GetSolidColorBrush(new Color4(0.3, 0.3, 0.9, 1)));
  181. //r.origin = new Vector2(0, 1);
  182. //r.roundRadius = 0;
  183. //var r3 = Rectangle2D.Create(g1, "rect3", 200, 50, 200, 90, Canvas2D.GetSolidColorBrush(new Color4(0.3, 0.9, 0.9, 1)));
  184. //r3.origin = new Vector2(0, 0);
  185. //r3.roundRadius = 0;
  186. //let rrList = new Array<Rectangle2D>();
  187. //for (let i = 0; i < 20; i++) {
  188. // let rr = Rectangle2D.Create(g1, `rr${i}`, 20 + i*20, 50, 40, 40, Canvas2D.GetSolidColorBrush(new Color4(0.3 + i/40, i/20, 0.1, 1)));
  189. // rr.origin = new Vector2(0, 0);
  190. // rr.roundRadius = 0;
  191. // rrList.push(rr);
  192. //}
  193. ////var r5 = Rectangle2D.Create(g2, "rect3", 10, 10, 50, 50, null, Canvas2D.GetGradientColorBrush(new Color4(0.9, 0.3, 0.9, 1), new Color4(1.0, 1.0, 1.0, 1)));
  194. ////r5.origin = new Vector2(0, 0);
  195. ////r5.roundRadius = 1;
  196. ////var r2 = Rectangle2D.Create(g2, "rect1", 110, 10, 200, 200, Canvas2D.GetSolidColorBrush(new Color4(0.3, 0.3, 0.9, 1)), Canvas2D.GetGradientColorBrush(new Color4(0.9, 0.3, 0.9, 1), new Color4(1.0, 1.0, 1.0, 1)));
  197. ////r2.borderThickness = 10;
  198. ////r2.origin = new Vector2(0, 0);
  199. ////r2.roundRadius = 10;
  200. //var text = Text2D.Create(g1, "text", 0, 0, "20pt Lucida Console", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  201. //let wc = Canvas2D.CreateWorldSpace(this.scene, "worldSpace", new Vector3(0, 0, 100), Quaternion.Identity(), new Size(100, 100), 4, Mesh.DEFAULTSIDE, Canvas2D.CACHESTRATEGY_CANVAS);
  202. //var r4 = Rectangle2D.Create(wc, "rect1", -40, 10, 200, 200, Canvas2D.GetSolidColorBrush(new Color4(0.3, 0.3, 0.9, 1)), Canvas2D.GetGradientColorBrush(new Color4(0.9, 0.3, 0.9, 1), new Color4(1.0, 1.0, 1.0, 1)));
  203. //r4.borderThickness = 10;
  204. //r4.rotation = 0.2;
  205. //r4.origin = new Vector2(0, 0);
  206. //r4.roundRadius = 10;
  207. // setInterval(() => {
  208. // rrList.forEach(r => r.rotation += 0.009);
  209. //// g2.position = new Vector2(g2.position.x + 0.1, g2.position.y);
  210. //// sprite.rotation += 0.005;
  211. //// g1.scale += 0.001;
  212. //// r3.position = new Vector2(r3.position.x + 0.0, r3.position.y + 0.1);
  213. // //text.position = new Vector2(text.position.x + 0.0, text.position.y + 0.1);
  214. // //r.rotation += 0.005;
  215. // //r3.rotation -= 0.005;
  216. //// g1.scale += 0.0001;
  217. //}, 10);
  218. // This creates a light, aiming 0,1,0 - to the sky.
  219. //var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), this.scene);
  220. //var light = new BABYLON.DirectionalLight("light1", new BABYLON.Vector3(0, 0, 1), this.scene);
  221. var light = new BABYLON.PointLight("light1", new BABYLON.Vector3(50, 50, -70), this.scene);
  222. //light.intensity = 10;
  223. //light.intensity = 0.5;
  224. //light.specular = new BABYLON.Color3(0, 0, 0);
  225. var self = this;
  226. //var box = Mesh.CreateBox("box", 1, this.scene);
  227. //box.position = new Vector3(1, 2, 3);
  228. ////this.scene.constantlyUpdateMeshUnderPointer = true;
  229. //var box2 = Mesh.CreateBox("box", 2, this.scene);
  230. //box2.position = new Vector3(-5, 0, 0);
  231. //box2.parent = box;
  232. //var mtl = new StandardMaterial("mtl", this.scene);
  233. //mtl.diffuseColor = new Color3(0, 1, 0);
  234. //box2.material = mtl;
  235. //var sih = new SimpleInteractionHelper(this.scene);
  236. //var points = new Array<Vector3>();
  237. //points.push(new Vector3(0, 0, 0));
  238. //points.push(new Vector3(0, 1, 0));
  239. //points.push(new Vector3(1, 1, 0));
  240. //points.push(new Vector3(1, 0, 0));
  241. //points.push(new Vector3(0, 0, 0));
  242. //var lineMesh = Mesh.CreateLines("lines", points, this.scene);
  243. //lineMesh.geometry.boundingBias = new Vector2(0, 0.01);
  244. // Register a render loop to repeatedly render the scene
  245. this.engine.runRenderLoop(function () {
  246. htmlCanvasElement.width = window.innerWidth - 20;
  247. htmlCanvasElement.height = window.innerHeight - 20;
  248. if (self.scene) {
  249. self.scene.render();
  250. }
  251. });
  252. // Watch for browser/canvas resize events
  253. window.addEventListener("resize", function () {
  254. if (self.scene) {
  255. self.engine.resize();
  256. }
  257. });
  258. this.scene.registerBeforeRender(function () {
  259. //light.position = camera.position;
  260. //mousemov = false;
  261. });
  262. this.scene.afterRender = function () {
  263. };
  264. };
  265. return MainClass;
  266. }());
  267. Sandbox.MainClass = MainClass;
  268. })(Sandbox || (Sandbox = {}));
  269. //# sourceMappingURL=MainClass.js.map