index.vwf.yaml 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # A-Frame & VWF simple scene
  2. # Copyright 2017 Krestianstvo.org project
  3. ---
  4. extends: http://vwf.example.com/aframe/ascene.vwf
  5. properties:
  6. assets: "assets.json"
  7. children:
  8. sky:
  9. extends: http://vwf.example.com/aframe/asky.vwf
  10. properties:
  11. color: "#ECECEC"
  12. groundPlane:
  13. extends: http://vwf.example.com/aframe/aplane.vwf
  14. properties:
  15. height: 50
  16. width: 50
  17. repeat: "10 10"
  18. rotation: [-90, 0, 0]
  19. wireframe: false
  20. src: "#bg2"
  21. turtle:
  22. extends: http://vwf.example.com/aframe/asphere.vwf
  23. properties:
  24. position: [1, 1.25, -4]
  25. color: "#e0e014"
  26. radius: 0.2
  27. wireframe: true
  28. angleInRadians: 0
  29. iteration:
  30. set: |
  31. this.iteration = value;
  32. this.redrawEvent();
  33. value: 3
  34. angle:
  35. set: |
  36. this.angle = value;
  37. this.redrawEvent();
  38. value: 60
  39. stepLength:
  40. set: |
  41. this.stepLength = value;
  42. this.redrawEvent();
  43. value: 0.5
  44. rule:
  45. set: |
  46. this.rule = value;
  47. this.redrawEvent();
  48. value: 'F++F++F'
  49. axiomF:
  50. set: |
  51. this.axiomF = value;
  52. this.redrawEvent();
  53. value: 'F-F++F-F'
  54. axiomG:
  55. set: |
  56. this.axiomG = value;
  57. this.redrawEvent();
  58. value: ''
  59. lsys: ''
  60. readyForDraw: true
  61. children:
  62. drawNode:
  63. extends: http://vwf.example.com/aframe/aentity.vwf
  64. children:
  65. linepath:
  66. extends: http://vwf.example.com/aframe/linepath.vwf
  67. properties:
  68. color: "green"
  69. path: []
  70. width: 0.02
  71. lsysLang:
  72. extends: http://vwf.example.com/ohm/node.vwf
  73. properties:
  74. grammar:
  75. semantics:
  76. ohmLang: |
  77. LSys { Gen<x>
  78. = ReadRule+
  79. ReadRule
  80. = letters | symbols
  81. letters = "F" | "G"
  82. symbols = "-" | "+" }
  83. methods:
  84. initLang:
  85. body: |
  86. console.log("add operations to semantics")
  87. this.addOperationLang();
  88. addOperationLang:
  89. body: |
  90. this.semantics.addOperation('gen(x)', {
  91. Gen: function(e)
  92. {
  93. return e.gen(this.args.x);
  94. },
  95. ReadRule: function(e)
  96. {
  97. return e.gen(this.args.x);
  98. },
  99. letters: function(_)
  100. {
  101. for (var propName in this.args.x)
  102. {
  103. if (propName == this.sourceString)
  104. return this.args.x[propName]
  105. }
  106. return this.sourceString
  107. },
  108. symbols: function(_)
  109. {
  110. return this.sourceString;
  111. }
  112. });
  113. turtleLang:
  114. extends: http://vwf.example.com/ohm/node.vwf
  115. properties:
  116. grammar:
  117. semantics:
  118. ohmLang: |
  119. Turtle {
  120. Draw<x, y>
  121. = (drawLetter | turn)+
  122. drawLetter
  123. = letter
  124. turn
  125. = "+" | "-" }
  126. methods:
  127. initLang:
  128. body: |
  129. console.log("add operations to semantics")
  130. this.addOperationLang();
  131. addOperationLang:
  132. body: |
  133. var turtleID = this.parent.id;
  134. var self = this;
  135. this.semantics.addOperation('draw(x,y)', {
  136. Draw: function(e)
  137. {
  138. e.draw(this.args.x, this.args.y);
  139. },
  140. drawLetter: function(e)
  141. {
  142. //vwf_view.kernel.callMethod(turtleID, 'goForward', [this.args.x]);
  143. self.parent.goForward(this.args.x);
  144. },
  145. turn: function(e)
  146. {
  147. if (this.sourceString == "+")
  148. //vwf_view.kernel.callMethod(turtleID, 'turn', [this.args.y]);
  149. self.parent.turn(this.args.y);
  150. if (this.sourceString == "-")
  151. //vwf_view.kernel.callMethod(turtleID, 'turn', [-1 * this.args.y]);
  152. self.parent.turn(-1*this.args.y);
  153. }
  154. });
  155. events:
  156. redrawEvent:
  157. methods:
  158. parseLSys: |
  159. var str = this.rule;
  160. var axioms = {"F": this.axiomF, "G": this.axiomG};
  161. for (var i = 1; i < this.iteration; i++)
  162. {
  163. var match = this.lsysLang.grammar.match(str, 'Gen<"y">');
  164. if (match.succeeded()){
  165. var res = this.lsysLang.semantics(match).gen(axioms);
  166. str = res.join("");
  167. }
  168. }
  169. console.log(str);
  170. this.lsys = str;
  171. makeLSys: |
  172. if (this.readyForDraw){
  173. this.drawNode.position = [0, 0, 0]
  174. this.angleInRadians = 0;
  175. this.drawNode.linepath.path = [];
  176. this.parseLSys();
  177. this.drawLSys();
  178. this.drawNode.position = [0, 0, 0]
  179. }
  180. drawLSys: |
  181. var match = this.turtleLang.grammar.match(this.lsys, 'Draw<"1","1">');
  182. if (match.succeeded()){
  183. var res = this.turtleLang.semantics(match).draw(this.stepLength, this.angle);
  184. }
  185. turn:
  186. parameters:
  187. - angle
  188. body: |
  189. var angle0 = this.angleInRadians;
  190. var targetAngle = angle * Math.PI / 180.0;
  191. this.angleInRadians = angle0 + targetAngle;
  192. goForward:
  193. parameters:
  194. - step
  195. body: |
  196. let pos = AFRAME.utils.coordinates.parse(this.drawNode.position);
  197. var x0 = pos.x;
  198. var y0 = pos.y;
  199. var xx = Math.sin(this.angleInRadians);
  200. var yy = Math.cos(this.angleInRadians);
  201. let startPosition = AFRAME.utils.coordinates.parse(this.drawNode.position);
  202. let endPosition = {x: x0 + step * xx, y: y0 + step * yy, z: pos.z};
  203. var drawPath = this.drawNode.linepath.path;
  204. drawPath.push(startPosition);
  205. drawPath.push(endPosition);
  206. vwf_view.kernel.setProperty(this.drawNode.linepath.id, 'path', drawPath);
  207. this.drawNode.position = endPosition;
  208. setTurtleParams:
  209. parameters:
  210. - val
  211. body: |
  212. this.readyForDraw = false;
  213. val.forEach(el => {
  214. this[el[0]] = el[1]
  215. })
  216. this.readyForDraw = true;
  217. this.makeLSys();
  218. initialize:
  219. body: |
  220. this.redrawEvent = function(){this.makeLSys()}
  221. vwf_view.kernel.callMethod(this.id, "makeLSys");
  222. console.log("initialising turtle");
  223. methods:
  224. initialize:
  225. body: |
  226. console.log("initialising scene");
  227. drawLSys1: |
  228. this.turtle.makeLSys()
  229. testTurtle: |
  230. this.turtle.goForward(1);
  231. this.turtle.goForward(1);
  232. this.turtle.turn(45);
  233. this.turtle.goForward(1);
  234. this.turtle.goForward(1);
  235. this.turtle.turn(45);
  236. this.turtle.goForward(1);