MainClass.ts 14 KB

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