Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Chamada que cria:
- this.boxEditor = new BoxEditor.BoxEditor(this.canvas, this.wgl, this.projectionMatrix, this.camera, [GRID_WIDTH, GRID_HEIGHT, GRID_DEPTH], (function () {
- boxEditorLoaded = true;
- if (boxEditorLoaded && simulatorRendererLoaded) {
- start.call(this);
- }
- }).bind(this),
- (function () {
- this.redrawUI();
- }).bind(this));
- definição:
- function BoxEditor (canvas, wgl, projectionMatrix, camera, gridSize, onLoaded, onChange) {
- this.canvas = canvas;
- this.wgl = wgl;
- this.gridWidth = gridSize[0];
- this.gridHeight = gridSize[1];
- this.gridDepth = gridSize[2];
- this.gridDimensions = [this.gridWidth, this.gridHeight, this.gridDepth];
- this.projectionMatrix = projectionMatrix;
- this.camera = camera;
- this.onChange = onChange;
- //the cube geometry is a 1x1 cube with the origin at the bottom left corner
- this.cubeVertexBuffer = wgl.createBuffer();
- wgl.bufferData(this.cubeVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([
- // Front face
- 0.0, 0.0, 1.0,
- 1.0, 0.0, 1.0,
- 1.0, 1.0, 1.0,
- 0.0, 1.0, 1.0,
- // Back face
- 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0,
- 1.0, 1.0, 0.0,
- 1.0, 0.0, 0.0,
- // Top face
- 0.0, 1.0, 0.0,
- 0.0, 1.0, 1.0,
- 1.0, 1.0, 1.0,
- 1.0, 1.0, 0.0,
- // Bottom face
- 0.0, 0.0, 0.0,
- 1.0, 0.0, 0.0,
- 1.0, 0.0, 1.0,
- 0.0, 0.0, 1.0,
- // Right face
- 1.0, 0.0, 0.0,
- 1.0, 1.0, 0.0,
- 1.0, 1.0, 1.0,
- 1.0, 0.0, 1.0,
- // Left face
- 0.0, 0.0, 0.0,
- 0.0, 0.0, 1.0,
- 0.0, 1.0, 1.0,
- 0.0, 1.0, 0.0
- ]), wgl.STATIC_DRAW);
- this.cubeIndexBuffer = wgl.createBuffer();
- wgl.bufferData(this.cubeIndexBuffer, wgl.ELEMENT_ARRAY_BUFFER, new Uint16Array([
- 0, 1, 2, 0, 2, 3, // front
- 4, 5, 6, 4, 6, 7, // back
- 8, 9, 10, 8, 10, 11, // top
- 12, 13, 14, 12, 14, 15, // bottom
- 16, 17, 18, 16, 18, 19, // right
- 20, 21, 22, 20, 22, 23 // left
- ]), wgl.STATIC_DRAW);
- this.cubeWireframeVertexBuffer = wgl.createBuffer();
- wgl.bufferData(this.cubeWireframeVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([
- 0.0, 0.0, 0.0,
- 1.0, 0.0, 0.0,
- 1.0, 1.0, 0.0,
- 0.0, 1.0, 0.0,
- 0.0, 0.0, 1.0,
- 1.0, 0.0, 1.0,
- 1.0, 1.0, 1.0,
- 0.0, 1.0, 1.0]), wgl.STATIC_DRAW);
- this.cubeWireframeIndexBuffer = wgl.createBuffer();
- wgl.bufferData(this.cubeWireframeIndexBuffer, wgl.ELEMENT_ARRAY_BUFFER, new Uint16Array([
- 0, 1, 1, 2, 2, 3, 3, 0,
- 4, 5, 5, 6, 6, 7, 7, 4,
- 0, 4, 1, 5, 2, 6, 3, 7
- ]), wgl.STATIC_DRAW);
- //there's one grid vertex buffer for the planes normal to each axis
- this.gridVertexBuffers = [];
- for (var axis = 0; axis < 3; ++axis) {
- this.gridVertexBuffers[axis] = wgl.createBuffer();
- var vertexData = [];
- var points; //the points that make up this grid plane
- if (axis === 0) {
- points = [
- [0, 0, 0],
- [0, this.gridHeight, 0],
- [0, this.gridHeight, this.gridDepth],
- [0, 0, this.gridDepth]
- ];
- } else if (axis === 1) {
- points = [
- [0, 0, 0],
- [this.gridWidth, 0, 0],
- [this.gridWidth, 0, this.gridDepth],
- [0, 0, this.gridDepth]
- ];
- } else if (axis === 2) {
- points = [
- [0, 0, 0],
- [this.gridWidth, 0, 0],
- [this.gridWidth, this.gridHeight, 0],
- [0, this.gridHeight, 0]
- ];
- }
- for (var i = 0; i < 4; ++i) {
- vertexData.push(points[i][0]);
- vertexData.push(points[i][1]);
- vertexData.push(points[i][2]);
- vertexData.push(points[(i + 1) % 4][0]);
- vertexData.push(points[(i + 1) % 4][1]);
- vertexData.push(points[(i + 1) % 4][2]);
- }
- wgl.bufferData(this.gridVertexBuffers[axis], wgl.ARRAY_BUFFER, new Float32Array(vertexData), wgl.STATIC_DRAW);
- }
- this.pointVertexBuffer = wgl.createBuffer();
- 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);
- this.quadVertexBuffer = wgl.createBuffer();
- 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);
- /////////////////////////////////////////////////
- // box state
- this.boxes = [];
- ////////////////////////////////////////////////
- // interaction stuff
- //mouse x and y are in [-1, 1] (clip space)
- this.mouseX = 999;
- this.mouseY = 999;
- this.keyPressed = []; //an array of booleans that maps a key code to whether or not it's pressed
- for (var i = 0; i < 256; ++i) {
- this.keyPressed[i] = false;
- }
- /*
- interactions:
- click on a plane and hold down to begin drawing
- when mouse is released we enter extrusion mode for new box
- click again to create box
- click and drag on side of boxes to resize
- click and drag on side of boxes whilst holding shift to move
- //while we're not interacting, this is null
- //while we are interacting this contains an object
- /*
- {
- mode: the interaction mode,
- during resizing or translating or extrusion:
- box: box we're currently manipulating,
- axis: axis of plane we're manipulating: 0, 1 or 2
- side: side of plane we're manipulating: -1 or 1
- point: the point at which the interaction started
- during translation we also have:
- startMax: the starting max along the interaction axis
- startMin: the starting min along the interaction axis
- during drawing
- box: box we're currently drawing
- point: the point at which we started drawing
- axis: the axis of the plane which we're drawing on
- side: the side of the plane which we're drawin on
- }
- */
- this.interactionState = null;
- ///////////////////////////////////
- // load programs
- wgl.createProgramsFromFiles({
- backgroundProgram: {
- vertexShader: 'shaders/background.vert',
- fragmentShader: 'shaders/background.frag'
- },
- boxProgram: {
- vertexShader: 'shaders/box.vert',
- fragmentShader: 'shaders/box.frag'
- },
- boxWireframeProgram: {
- vertexShader: 'shaders/boxwireframe.vert',
- fragmentShader: 'shaders/boxwireframe.frag'
- },
- gridProgram: {
- vertexShader: 'shaders/grid.vert',
- fragmentShader: 'shaders/grid.frag'
- },
- pointProgram: {
- vertexShader: 'shaders/point.vert',
- fragmentShader: 'shaders/point.frag'
- }
- }, (function (programs) {
- for (var programName in programs) {
- this[programName] = programs[programName];
- }
- onLoaded();
- }).bind(this));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement