Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. Chamada que cria:
  2. this.boxEditor = new BoxEditor.BoxEditor(this.canvas, this.wgl, this.projectionMatrix, this.camera, [GRID_WIDTH, GRID_HEIGHT, GRID_DEPTH], (function () {
  3. boxEditorLoaded = true;
  4. if (boxEditorLoaded && simulatorRendererLoaded) {
  5. start.call(this);
  6. }
  7. }).bind(this),
  8. (function () {
  9. this.redrawUI();
  10. }).bind(this));
  11.  
  12.  
  13.  
  14.  
  15. definição:
  16. function BoxEditor (canvas, wgl, projectionMatrix, camera, gridSize, onLoaded, onChange) {
  17. this.canvas = canvas;
  18.  
  19. this.wgl = wgl;
  20.  
  21. this.gridWidth = gridSize[0];
  22. this.gridHeight = gridSize[1];
  23. this.gridDepth = gridSize[2];
  24. this.gridDimensions = [this.gridWidth, this.gridHeight, this.gridDepth];
  25.  
  26. this.projectionMatrix = projectionMatrix;
  27. this.camera = camera;
  28.  
  29. this.onChange = onChange;
  30.  
  31. //the cube geometry is a 1x1 cube with the origin at the bottom left corner
  32.  
  33. this.cubeVertexBuffer = wgl.createBuffer();
  34. wgl.bufferData(this.cubeVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([
  35. // Front face
  36. 0.0, 0.0, 1.0,
  37. 1.0, 0.0, 1.0,
  38. 1.0, 1.0, 1.0,
  39. 0.0, 1.0, 1.0,
  40.  
  41. // Back face
  42. 0.0, 0.0, 0.0,
  43. 0.0, 1.0, 0.0,
  44. 1.0, 1.0, 0.0,
  45. 1.0, 0.0, 0.0,
  46.  
  47. // Top face
  48. 0.0, 1.0, 0.0,
  49. 0.0, 1.0, 1.0,
  50. 1.0, 1.0, 1.0,
  51. 1.0, 1.0, 0.0,
  52.  
  53. // Bottom face
  54. 0.0, 0.0, 0.0,
  55. 1.0, 0.0, 0.0,
  56. 1.0, 0.0, 1.0,
  57. 0.0, 0.0, 1.0,
  58.  
  59. // Right face
  60. 1.0, 0.0, 0.0,
  61. 1.0, 1.0, 0.0,
  62. 1.0, 1.0, 1.0,
  63. 1.0, 0.0, 1.0,
  64.  
  65. // Left face
  66. 0.0, 0.0, 0.0,
  67. 0.0, 0.0, 1.0,
  68. 0.0, 1.0, 1.0,
  69. 0.0, 1.0, 0.0
  70. ]), wgl.STATIC_DRAW);
  71.  
  72.  
  73.  
  74. this.cubeIndexBuffer = wgl.createBuffer();
  75. wgl.bufferData(this.cubeIndexBuffer, wgl.ELEMENT_ARRAY_BUFFER, new Uint16Array([
  76. 0, 1, 2, 0, 2, 3, // front
  77. 4, 5, 6, 4, 6, 7, // back
  78. 8, 9, 10, 8, 10, 11, // top
  79. 12, 13, 14, 12, 14, 15, // bottom
  80. 16, 17, 18, 16, 18, 19, // right
  81. 20, 21, 22, 20, 22, 23 // left
  82. ]), wgl.STATIC_DRAW);
  83.  
  84.  
  85. this.cubeWireframeVertexBuffer = wgl.createBuffer();
  86. wgl.bufferData(this.cubeWireframeVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([
  87. 0.0, 0.0, 0.0,
  88. 1.0, 0.0, 0.0,
  89. 1.0, 1.0, 0.0,
  90. 0.0, 1.0, 0.0,
  91.  
  92. 0.0, 0.0, 1.0,
  93. 1.0, 0.0, 1.0,
  94. 1.0, 1.0, 1.0,
  95. 0.0, 1.0, 1.0]), wgl.STATIC_DRAW);
  96.  
  97. this.cubeWireframeIndexBuffer = wgl.createBuffer();
  98. wgl.bufferData(this.cubeWireframeIndexBuffer, wgl.ELEMENT_ARRAY_BUFFER, new Uint16Array([
  99. 0, 1, 1, 2, 2, 3, 3, 0,
  100. 4, 5, 5, 6, 6, 7, 7, 4,
  101. 0, 4, 1, 5, 2, 6, 3, 7
  102. ]), wgl.STATIC_DRAW);
  103.  
  104.  
  105. //there's one grid vertex buffer for the planes normal to each axis
  106. this.gridVertexBuffers = [];
  107.  
  108. for (var axis = 0; axis < 3; ++axis) {
  109. this.gridVertexBuffers[axis] = wgl.createBuffer();
  110.  
  111. var vertexData = [];
  112.  
  113.  
  114. var points; //the points that make up this grid plane
  115.  
  116. if (axis === 0) {
  117.  
  118. points = [
  119. [0, 0, 0],
  120. [0, this.gridHeight, 0],
  121. [0, this.gridHeight, this.gridDepth],
  122. [0, 0, this.gridDepth]
  123. ];
  124.  
  125. } else if (axis === 1) {
  126. points = [
  127. [0, 0, 0],
  128. [this.gridWidth, 0, 0],
  129. [this.gridWidth, 0, this.gridDepth],
  130. [0, 0, this.gridDepth]
  131. ];
  132. } else if (axis === 2) {
  133.  
  134. points = [
  135. [0, 0, 0],
  136. [this.gridWidth, 0, 0],
  137. [this.gridWidth, this.gridHeight, 0],
  138. [0, this.gridHeight, 0]
  139. ];
  140. }
  141.  
  142.  
  143. for (var i = 0; i < 4; ++i) {
  144. vertexData.push(points[i][0]);
  145. vertexData.push(points[i][1]);
  146. vertexData.push(points[i][2]);
  147.  
  148. vertexData.push(points[(i + 1) % 4][0]);
  149. vertexData.push(points[(i + 1) % 4][1]);
  150. vertexData.push(points[(i + 1) % 4][2]);
  151. }
  152.  
  153.  
  154. wgl.bufferData(this.gridVertexBuffers[axis], wgl.ARRAY_BUFFER, new Float32Array(vertexData), wgl.STATIC_DRAW);
  155. }
  156.  
  157. this.pointVertexBuffer = wgl.createBuffer();
  158. wgl.bufferData(this.pointVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([-1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0]), wgl.STATIC_DRAW);
  159.  
  160.  
  161. this.quadVertexBuffer = wgl.createBuffer();
  162. wgl.bufferData(this.quadVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]), wgl.STATIC_DRAW);
  163.  
  164.  
  165. /////////////////////////////////////////////////
  166. // box state
  167.  
  168. this.boxes = [];
  169.  
  170.  
  171. ////////////////////////////////////////////////
  172. // interaction stuff
  173.  
  174. //mouse x and y are in [-1, 1] (clip space)
  175. this.mouseX = 999;
  176. this.mouseY = 999;
  177.  
  178. this.keyPressed = []; //an array of booleans that maps a key code to whether or not it's pressed
  179. for (var i = 0; i < 256; ++i) {
  180. this.keyPressed[i] = false;
  181. }
  182.  
  183. /*
  184. interactions:
  185. click on a plane and hold down to begin drawing
  186. when mouse is released we enter extrusion mode for new box
  187. click again to create box
  188.  
  189. click and drag on side of boxes to resize
  190.  
  191. click and drag on side of boxes whilst holding shift to move
  192.  
  193.  
  194. //while we're not interacting, this is null
  195. //while we are interacting this contains an object
  196. /*
  197.  
  198. {
  199. mode: the interaction mode,
  200.  
  201. during resizing or translating or extrusion:
  202. box: box we're currently manipulating,
  203. axis: axis of plane we're manipulating: 0, 1 or 2
  204. side: side of plane we're manipulating: -1 or 1
  205. point: the point at which the interaction started
  206.  
  207.  
  208. during translation we also have:
  209. startMax: the starting max along the interaction axis
  210. startMin: the starting min along the interaction axis
  211.  
  212.  
  213. during drawing
  214. box: box we're currently drawing
  215. point: the point at which we started drawing
  216. axis: the axis of the plane which we're drawing on
  217. side: the side of the plane which we're drawin on
  218.  
  219. }
  220. */
  221. this.interactionState = null;
  222.  
  223.  
  224. ///////////////////////////////////
  225. // load programs
  226.  
  227.  
  228. wgl.createProgramsFromFiles({
  229. backgroundProgram: {
  230. vertexShader: 'shaders/background.vert',
  231. fragmentShader: 'shaders/background.frag'
  232. },
  233. boxProgram: {
  234. vertexShader: 'shaders/box.vert',
  235. fragmentShader: 'shaders/box.frag'
  236. },
  237. boxWireframeProgram: {
  238. vertexShader: 'shaders/boxwireframe.vert',
  239. fragmentShader: 'shaders/boxwireframe.frag'
  240. },
  241. gridProgram: {
  242. vertexShader: 'shaders/grid.vert',
  243. fragmentShader: 'shaders/grid.frag'
  244. },
  245. pointProgram: {
  246. vertexShader: 'shaders/point.vert',
  247. fragmentShader: 'shaders/point.frag'
  248. }
  249. }, (function (programs) {
  250. for (var programName in programs) {
  251. this[programName] = programs[programName];
  252. }
  253.  
  254. onLoaded();
  255. }).bind(this));
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement