heightmap.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /// vwf/model/heightmap.js is a driver for the heightmap.vwf component.
  2. ///
  3. /// @module vwf/model/heightmap
  4. /// @requires vwf/model
  5. define( [ "module", "vwf/model", "vwf/utility" ], function( module, model, utility ) {
  6. return model.load( module, {
  7. // == Module Definition ====================================================================
  8. // -- initialize ---------------------------------------------------------------------------
  9. initialize: function() {
  10. this.nodes = {};
  11. },
  12. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  13. childSource, childType, childIndex, childName, callback ) {
  14. var protos = getPrototypes( this.kernel, childExtendsID );
  15. if ( isHeightmap( protos ) ) {
  16. var node = this.nodes[ childID ];
  17. // Create the local copy of the node properties
  18. if ( this.nodes[ childID ] === undefined ){
  19. // Suspend the queue until the load is complete
  20. callback( false );
  21. // Load the source file
  22. var heightmapImage = new Image();
  23. // heightmapImage.src = childSource;
  24. heightmapImage.src = utility.resolveURI( childSource );
  25. var driver = this;
  26. heightmapImage.onload = function() {
  27. // Create a canvas that we can draw the image into and read back
  28. // the pixels
  29. var canvas = document.createElement( "canvas" );
  30. canvas.setAttribute( "width", heightmapImage.width );
  31. canvas.setAttribute( "height", heightmapImage.height );
  32. // document.body.appendChild( canvas );
  33. var context = canvas.getContext( "2d" );
  34. context.drawImage( heightmapImage, 0, 0 );
  35. var heightmap = context.getImageData( 0, 0, heightmapImage.width,
  36. heightmapImage.height );
  37. // Store the heightmap on the node for future use
  38. driver.nodes[ childID ] = {
  39. heightmap: heightmap.data,
  40. width: heightmapImage.width,
  41. height: heightmapImage.height,
  42. }
  43. // Resume the queue now that the heightmap image has completely loaded
  44. callback( true );
  45. }
  46. }
  47. }
  48. },
  49. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  50. // See if we have stored this node (meaning it is a heightmap node)
  51. var node = this.nodes[ nodeID ];
  52. // If it is not a heightmap node, we don't care about it, so return
  53. if ( !node ) {
  54. return;
  55. }
  56. switch ( propertyName ) {
  57. case "heightmap":
  58. return node.heightmap;
  59. case "heightmapWidth":
  60. return node.width;
  61. case "heightmapHeight":
  62. return node.height;
  63. }
  64. }
  65. } );
  66. function getPrototypes( kernel, extendsID ) {
  67. var prototypes = [];
  68. var id = extendsID;
  69. while ( id !== undefined ) {
  70. prototypes.push( id );
  71. id = kernel.prototype( id );
  72. }
  73. return prototypes;
  74. }
  75. function isHeightmap( prototypes ) {
  76. var found = false;
  77. if ( prototypes ) {
  78. for ( var i = 0; i < prototypes.length && !found; i++ ) {
  79. found = ( prototypes[ i ] === "http://vwf.example.com/heightmap.vwf" );
  80. }
  81. }
  82. return found;
  83. }
  84. } );