node3.vwf.yaml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. # Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  2. # Secretary of Defense (Personnel & Readiness).
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. # in compliance with the License. You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software distributed under the License
  10. # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. # or implied. See the License for the specific language governing permissions and limitations under
  12. # the License.
  13. ## The component representation of a 3D node
  14. ##
  15. ## @name node3.vwf
  16. ## @namespace
  17. ---
  18. extends: http://vwf.example.com/node.vwf
  19. implements:
  20. - http://vwf.example.com/animation.vwf
  21. - http://vwf.example.com/node3/animation.vwf
  22. properties:
  23. ## Node casts shadows
  24. ##
  25. ## @name node3.vwf#castShadows
  26. ## @property
  27. castShadows:
  28. ## Node receives shadows
  29. ##
  30. ## @name node3.vwf#receiveShadows
  31. ## @property
  32. receiveShadows:
  33. ## World transform of 3D node
  34. ##
  35. ## @name node3.vwf#worldTransform
  36. ## @property
  37. worldTransform:
  38. set: |
  39. var parentInverse = goog.vec.Mat4.create();
  40. if ( this.parent && goog.vec.Mat4.invert( this.transformFromValue( this.parent.worldTransform ), parentInverse ) ) {
  41. var transform = goog.vec.Mat4.multMat( parentInverse, this.transformFromValue( value ), goog.vec.Mat4.create() );
  42. this.transform = transform;
  43. } else {
  44. this.transform = value;
  45. }
  46. get: |
  47. if( this.parent && this.parent.worldTransform && this.transform )
  48. {
  49. return goog.vec.Mat4.multMat( this.parent.worldTransform, this.transform, goog.vec.Mat4.create() );
  50. }
  51. else return this.transform; //@ sourceURL=node3.worldTransform.get.vwf
  52. ## Transform of 3D node
  53. ##
  54. ## @name node3.vwf#transform
  55. ## @property
  56. transform:
  57. set: |
  58. var transform = this.transformFromValue( value ); // parse incoming value
  59. if ( ! goog.vec.Mat4.equals( this.transform || goog.vec.Mat4.createIdentity(), transform ) ) {
  60. this.transform = transform;
  61. this.transformChanged( transform );
  62. } //@ sourceURL=node3.transform.set.vwf
  63. get: |
  64. return this.transform || goog.vec.Mat4.createIdentity(); //@ sourceURL=node3.transform.get.vwf
  65. ## Translation of 3D node
  66. ## node.translation <= [ x, y, z ]
  67. ##
  68. ## @name node3.vwf#translation
  69. ## @property
  70. translation:
  71. # node.translation <= [ x, y, z ]
  72. set: |
  73. var translation = this.translationFromValue( value ); // parse incoming value
  74. var transform = this.transform || goog.vec.Mat4.createIdentity();
  75. goog.vec.Mat4.setColumnValues( transform, 3, translation[0], translation[1], translation[2], 1 );
  76. this.transform = transform; //@ sourceURL=node3.translation.set.vwf
  77. # node.translation => [ x, y, z ]
  78. get: |
  79. var translation = goog.vec.Vec3.create();
  80. goog.vec.Mat4.getColumn( this.transform || goog.vec.Mat4.createIdentity(), 3, translation );
  81. return translation;
  82. ## Rotation of 3D node
  83. ## node.rotation <= [ x, y, z, angle ]
  84. ##
  85. ## @name node3.vwf#rotation
  86. ## @property
  87. rotation:
  88. # node.rotation <= [ x, y, z, angle ]
  89. set: |
  90. var rotation = this.rotationFromValue( value ); // parse incoming value
  91. this.quaternion =
  92. goog.vec.Quaternion.fromAngleAxis(
  93. rotation[3] * Math.PI / 180,
  94. goog.vec.Vec3.createFromValues( rotation[0], rotation[1], rotation[2] ),
  95. goog.vec.Quaternion.create()
  96. );
  97. # node.rotation => [ x, y, z, angle ]
  98. get: |
  99. var rotation = goog.vec.Vec4.create();
  100. rotation[3] = goog.vec.Quaternion.toAngleAxis(
  101. this.quaternion || goog.vec.Quaternion.createFromValues( 0, 0, 0, 1 ),
  102. rotation
  103. ) * 180 / Math.PI;
  104. return rotation;
  105. ## Rotation matrix of 3D node
  106. ##
  107. ## @name node3.vwf#rotatioMatrix
  108. ## @property
  109. rotationMatrix:
  110. get: |
  111. return this.unscaledTransform(
  112. this.transform || goog.vec.Mat4.createIdentity(),
  113. goog.vec.Vec3.create(),
  114. goog.vec.Mat4.create()
  115. );
  116. ## Quaternion of 3D node
  117. ## node.quaternion <= [ x, y, z, w ]
  118. ##
  119. ## @name node3.vwf#quaternion
  120. ## @property
  121. quaternion:
  122. # node.quaternion <= [ x, y, z, w ]
  123. set: |
  124. var transform = this.transform || goog.vec.Mat4.createIdentity();
  125. var translation = goog.vec.Vec4.create(); goog.vec.Mat4.getColumn( transform, 3, translation );
  126. var scale = this.transformScale( transform, goog.vec.Vec3.create() );
  127. var quaternion = this.quaternionFromValue( value ); // parse incoming value
  128. goog.vec.Quaternion.toRotationMatrix4(
  129. quaternion,
  130. transform
  131. );
  132. this.scaledTransform( transform, scale, transform );
  133. goog.vec.Mat4.setColumn( transform, 3, translation );
  134. this.transform = transform; //@ sourceURL=node3.quaternion.set
  135. # node.quaternion => [ x, y, z, w ]
  136. get: |
  137. return goog.vec.Quaternion.fromRotationMatrix4(
  138. this.unscaledTransform(
  139. this.transform || goog.vec.Mat4.createIdentity(),
  140. goog.vec.Vec3.create(),
  141. goog.vec.Mat4.create()
  142. ),
  143. goog.vec.Quaternion.create()
  144. );
  145. ## Scale of 3D node
  146. ## node.scale <= [ x, y, z ], node.scale <= s
  147. ##
  148. ## @name node3.vwf#scale
  149. ## @property
  150. scale:
  151. # node.scale <= [ x, y, z ], node.scale <= s
  152. set: |
  153. var scale = this.scaleFromValue( value ); // parse incoming value
  154. var transform = this.transform || goog.vec.Mat4.createIdentity();
  155. this.scaledTransform(
  156. this.unscaledTransform(
  157. transform,
  158. goog.vec.Vec3.create(),
  159. transform
  160. ),
  161. scale,
  162. transform
  163. );
  164. this.transform = transform;
  165. # node.scale => [ x, y, z ]
  166. get: |
  167. return this.transformScale(
  168. this.transform || goog.vec.Mat4.createIdentity(),
  169. goog.vec.Vec3.create()
  170. );
  171. ## Enabled
  172. ##
  173. ## @name node3.vwf#enabled
  174. ## @property
  175. enabled: true
  176. ## Bounding box of 3D node
  177. ##
  178. ## @name node3.vwf#boundingbox
  179. ## @property
  180. boundingbox:
  181. set: |
  182. this.logger.info("WARNING: node3 boundingbox cannot be set" )
  183. ## World bounding box of 3D node
  184. ##
  185. ## @name node3.vwf#worldBoundingBox
  186. ## @property
  187. worldBoundingBox:
  188. set: |
  189. this.logger.info("WARNING: node3 worldBoundingBox cannot be set" )
  190. get: |
  191. var worldTransform = this.worldTransform;
  192. if ( worldTransform ) {
  193. var worldBoundingBox;
  194. for(var i=0;i<this.children.length;i++) {
  195. var childBoundingBox = this.children[i].worldBoundingBox;
  196. if(childBoundingBox) {
  197. if(!worldBoundingBox) {
  198. worldBoundingBox = childBoundingBox;
  199. }
  200. else {
  201. worldBoundingBox.max.x = Math.max(worldBoundingBox.max.x, childBoundingBox.max.x);
  202. worldBoundingBox.max.y = Math.max(worldBoundingBox.max.y, childBoundingBox.max.y);
  203. worldBoundingBox.max.z = Math.max(worldBoundingBox.max.z, childBoundingBox.max.z);
  204. worldBoundingBox.min.x = Math.min(worldBoundingBox.min.x, childBoundingBox.min.x);
  205. worldBoundingBox.min.y = Math.min(worldBoundingBox.min.y, childBoundingBox.min.y);
  206. worldBoundingBox.min.z = Math.min(worldBoundingBox.min.z, childBoundingBox.min.z);
  207. }
  208. }
  209. }
  210. if( !worldBoundingBox ) {
  211. worldBoundingBox = this.boundingbox;
  212. if ( worldBoundingBox ) {
  213. var coord0=goog.vec.Mat4.multVec4(worldTransform,[worldBoundingBox.min.x,worldBoundingBox.min.y,worldBoundingBox.min.z,1], goog.vec.Vec4.create() );
  214. var coord1=goog.vec.Mat4.multVec4(worldTransform,[worldBoundingBox.max.x,worldBoundingBox.min.y,worldBoundingBox.min.z,1], goog.vec.Vec4.create() );
  215. var coord2=goog.vec.Mat4.multVec4(worldTransform,[worldBoundingBox.min.x,worldBoundingBox.max.y,worldBoundingBox.min.z,1], goog.vec.Vec4.create() );
  216. var coord3=goog.vec.Mat4.multVec4(worldTransform,[worldBoundingBox.max.x,worldBoundingBox.max.y,worldBoundingBox.min.z,1], goog.vec.Vec4.create() );
  217. var coord4=goog.vec.Mat4.multVec4(worldTransform,[worldBoundingBox.min.x,worldBoundingBox.min.y,worldBoundingBox.max.z,1], goog.vec.Vec4.create() );
  218. var coord5=goog.vec.Mat4.multVec4(worldTransform,[worldBoundingBox.max.x,worldBoundingBox.min.y,worldBoundingBox.max.z,1], goog.vec.Vec4.create() );
  219. var coord6=goog.vec.Mat4.multVec4(worldTransform,[worldBoundingBox.min.x,worldBoundingBox.max.y,worldBoundingBox.max.z,1], goog.vec.Vec4.create() );
  220. var coord7=goog.vec.Mat4.multVec4(worldTransform,[worldBoundingBox.max.x,worldBoundingBox.max.y,worldBoundingBox.max.z,1], goog.vec.Vec4.create() );
  221. worldBoundingBox.min.x=Math.min(coord0[0],coord1[0],coord2[0],coord3[0],coord4[0],coord5[0],coord6[0],coord7[0]);
  222. worldBoundingBox.max.x=Math.max(coord0[0],coord1[0],coord2[0],coord3[0],coord4[0],coord5[0],coord6[0],coord7[0]);
  223. worldBoundingBox.min.y=Math.min(coord0[1],coord1[1],coord2[1],coord3[1],coord4[1],coord5[1],coord6[1],coord7[1]);
  224. worldBoundingBox.max.y=Math.max(coord0[1],coord1[1],coord2[1],coord3[1],coord4[1],coord5[1],coord6[1],coord7[1]);
  225. worldBoundingBox.min.z=Math.min(coord0[2],coord1[2],coord2[2],coord3[2],coord4[2],coord5[2],coord6[2],coord7[2]);
  226. worldBoundingBox.max.z=Math.max(coord0[2],coord1[2],coord2[2],coord3[2],coord4[2],coord5[2],coord6[2],coord7[2]);
  227. }
  228. }
  229. return worldBoundingBox;
  230. }
  231. else {
  232. return this.boundingbox;
  233. }
  234. ## Offset from center of 3D node
  235. ##
  236. ## @name node3.vwf#centerOffset
  237. ## @property
  238. centerOffset:
  239. set: |
  240. this.logger.info( "WARNING: node3 centerOffset cannot be set" )
  241. ## Vertices of 3D node
  242. ##
  243. ## @name node3.vwf#vertices
  244. ## @property
  245. vertices:
  246. set: |
  247. this.logger.info( "WARNING: node3 vertices cannot be set" )
  248. ## Vertex indices of 3D node
  249. ##
  250. ## @name node3.vwf#vertexIndices
  251. ## @property
  252. vertexIndices:
  253. set: |
  254. this.logger.info( "WARNING: node3 vertexIndices cannot be set" )
  255. ## Speed value
  256. ##
  257. ## @name node3.vwf#speed
  258. ## @property
  259. speed: 1
  260. ## Playing value
  261. ##
  262. ## @name node3.vwf#playing
  263. ## @property
  264. playing: false
  265. ## Looping value
  266. ##
  267. ## @name node3.vwf#looping
  268. ## @property
  269. looping: false
  270. ## Look at value
  271. ##
  272. ## @name lookAt
  273. ## @property
  274. lookAt: ""
  275. ## Visible value
  276. ##
  277. ## @name node3.vwf#visible
  278. ## @property
  279. visible:
  280. set: |
  281. this.visible = value;
  282. this.children.forEach( function( child ) {
  283. child.visible = value;
  284. } );
  285. value: true
  286. events:
  287. keyDown:
  288. keyPress:
  289. keyUp:
  290. pointerClick:
  291. pointerDown:
  292. pointerMove:
  293. pointerUp:
  294. pointerOver:
  295. pointerOut:
  296. pointerWheel:
  297. transformChanged:
  298. touchHold:
  299. touchTap:
  300. touchDoubleTap:
  301. touchDrag:
  302. touchDragStart:
  303. touchDragEnd:
  304. touchDragUp:
  305. touchDragDown:
  306. touchDragLeft:
  307. touchDragRight:
  308. touchSwipe:
  309. touchSwipeUp:
  310. touchSwipeDown:
  311. touchSwipeLeft:
  312. touchSwipeRight:
  313. touchTransform:
  314. touchTransformStart:
  315. touchTransformEnd:
  316. touchRotate:
  317. touchPinch:
  318. touchPinchIn:
  319. touchPinchOut:
  320. touchStart:
  321. touchRelease:
  322. scripts:
  323. - |
  324. // Parse a parameter as a transform specification.
  325. this.transformFromValue = function( value ) {
  326. return value && value.length >= 16 ?
  327. value :
  328. goog.vec.Mat4.createIdentity();
  329. };
  330. // Parse a parameter as a translation specification.
  331. this.translationFromValue = function( value ) {
  332. return value && value.length >= 3 ?
  333. value :
  334. goog.vec.Vec3.create();
  335. };
  336. // Parse a parameter as a rotation specification.
  337. this.rotationFromValue = function( value ) {
  338. return value && value.length >= 4 ?
  339. value :
  340. goog.vec.Vec4.createFromValues( 1, 0, 0, 0 );
  341. };
  342. // Parse a parameter as a quaternion specification.
  343. this.quaternionFromValue = function( value ) {
  344. return value && value.length >= 4 ?
  345. value :
  346. goog.vec.Quaternion.createFromValues( 0, 0, 0, 1 );
  347. };
  348. // Parse a parameter as a scale specification.
  349. this.scaleFromValue = function( value ) {
  350. return Object.prototype.toString.call( value ).search( /\[object .*Array\]/ ) == 0 ?
  351. value.length >= 3 ?
  352. value :
  353. goog.vec.Vec3.createFromValues( 1, 1, 1 ) :
  354. Object.prototype.toString.call( value ) == "[object Number]" ?
  355. goog.vec.Vec3.createFromValues( value, value, value ) :
  356. goog.vec.Vec3.createFromValues( 1, 1, 1 );
  357. };
  358. // Get the scale vector for a transform and rescale to unity scale.
  359. this.unscaledTransform = function( transform, scale, resultTransform ) {
  360. var column = goog.vec.Vec4.create();
  361. goog.vec.Mat4.getColumn( transform, 0, column );
  362. goog.vec.Mat4.setColumn( resultTransform, 0, goog.vec.Vec4.scale( column, 1 / ( scale[0] = goog.vec.Vec4.magnitude( column ) ), column ) );
  363. goog.vec.Mat4.getColumn( transform, 1, column );
  364. goog.vec.Mat4.setColumn( resultTransform, 1, goog.vec.Vec4.scale( column, 1 / ( scale[1] = goog.vec.Vec4.magnitude( column ) ), column ) );
  365. goog.vec.Mat4.getColumn( transform, 2, column );
  366. goog.vec.Mat4.setColumn( resultTransform, 2, goog.vec.Vec4.scale( column, 1 / ( scale[2] = goog.vec.Vec4.magnitude( column ) ), column ) );
  367. return resultTransform;
  368. };
  369. // Set the scale vector for a transform with unity scale.
  370. this.scaledTransform = function( transform, scale, resultTransform ) {
  371. var column = goog.vec.Vec4.create();
  372. goog.vec.Mat4.getColumn( transform, 0, column );
  373. goog.vec.Mat4.setColumn( resultTransform, 0, goog.vec.Vec4.scale( column, scale[0], column ) );
  374. goog.vec.Mat4.getColumn( transform, 1, column );
  375. goog.vec.Mat4.setColumn( resultTransform, 1, goog.vec.Vec4.scale( column, scale[1], column ) );
  376. goog.vec.Mat4.getColumn( transform, 2, column );
  377. goog.vec.Mat4.setColumn( resultTransform, 2, goog.vec.Vec4.scale( column, scale[2], column ) );
  378. return resultTransform;
  379. };
  380. // Get the scale vector for a transform.
  381. this.transformScale = function( transform, scale ) {
  382. var column = goog.vec.Vec4.create();
  383. goog.vec.Mat4.getColumn( transform, 0, column );
  384. scale[0] = goog.vec.Vec4.magnitude( column );
  385. goog.vec.Mat4.getColumn( transform, 1, column );
  386. scale[1] = goog.vec.Vec4.magnitude( column );
  387. goog.vec.Mat4.getColumn( transform, 2, column );
  388. scale[2] = goog.vec.Vec4.magnitude( column );
  389. return scale;
  390. };
  391. this.state = function( test ) {
  392. vwf.logger.info( test, "x", Array.prototype.slice.apply( this.transform || goog.vec.Mat4.createIdentity() ) );
  393. vwf.logger.info( test, "t", Array.prototype.slice.apply( this.translation || goog.vec.Vec3.create() ) );
  394. vwf.logger.info( test, "o", Array.prototype.slice.apply( this.rotation || goog.vec.Vec4.createFromValues( 1, 0, 0, 0 ) ) );
  395. vwf.logger.info( test, "s", Array.prototype.slice.apply( this.scale || goog.vec.Vec3.createFromValues( 1, 1, 1 ) ) );
  396. vwf.logger.info( test, "q", Array.prototype.slice.apply( this.quaternion || goog.vec.Quaternion.createFromValues( 0, 0, 0, 1 ) ) );
  397. };
  398. this.test1 = function() {
  399. this.transform = [];
  400. this.translation = [ 50, 0, 0 ];
  401. this.scale = 2;
  402. this.state( "test1" );
  403. };
  404. this.test2 = function() {
  405. this.scale = 2;
  406. this.state( "test2 scale 2" );
  407. this.scale = 1;
  408. this.state( "test2 scale 1" );
  409. this.rotation = [ 0, 0, 1, 30 ];
  410. this.state( "test2 rotation 30" );
  411. this.scale = 3;
  412. this.state( "test2 scale 3" );
  413. this.rotation = [ 0, 0, 1, -30 ];
  414. this.state( "test2 rotation -30" );
  415. this.scale = 1;
  416. this.state( "test2 scale 1" );
  417. }; //@ sourceURL=node3.vwf