diff --git a/index.js b/index.js index 86b7883..469baf2 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,13 @@ function compressShader(source) { result.push(line, "\n"); needNewline = false } else { + // Add space after "else" at end of prior line (unless this line starts with curly brace) + if (!line.startsWith('{') + && result.length + && result[result.length - 1].endsWith('else') + ) { + result.push(' '); + } result.push(line .replace(/\s*({|}|=|\*|,|\+|\/|>|<|&|\||\[|\]|\(|\)|\-|!|;)\s*/g, "$1")) needNewline = true; diff --git a/test/expected/basic.bundle.js b/test/expected/basic.bundle.js index bfa7f87..87b0de5 100644 --- a/test/expected/basic.bundle.js +++ b/test/expected/basic.bundle.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - var shaderSource = "#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec3 aVertexPosition;attribute vec3 aVertexNormal;attribute vec2 aTextureCoord;attribute float aFace;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;uniform mat3 uNMatrix;varying vec2 vTextureCoord;varying vec3 vTransformedNormal;varying vec4 vPosition;varying float vFace;void main(void){vPosition=uMVMatrix*vec4(aVertexPosition,1.0);gl_Position=uPMatrix*vPosition;vTextureCoord=aTextureCoord;vTransformedNormal=uNMatrix*aVertexNormal;vFace=aFace;}"; + var shaderSource = "#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec3 aVertexPosition;attribute vec3 aVertexNormal;attribute vec2 aTextureCoord;attribute float aFace;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;uniform mat3 uNMatrix;varying vec2 vTextureCoord;varying vec3 vTransformedNormal;varying vec4 vPosition;varying float vFace;void main(void){vPosition=uMVMatrix*vec4(aVertexPosition,1.0);gl_Position=uPMatrix*vPosition;vTextureCoord=aTextureCoord;vTransformedNormal=uNMatrix*aVertexNormal;vFace=aFace;}float getFace(vec3 direction){vec3 absDirection=abs(direction);float face=-1.0;if(absDirection.x>absDirection.z){if(absDirection.x>absDirection.y)face=direction.x>0.0 ? 0.0 : 3.0;else face=direction.y>0.0 ? 1.0 : 4.0;}else{if(absDirection.z>absDirection.y)face=direction.z>0.0 ? 2.0 : 5.0;else face=direction.y>0.0 ? 1.0 : 4.0;}return face;}"; assert.equal(typeof shaderSource, 'string'); const lines = shaderSource.split(/\n/); diff --git a/test/fixtures/shaders/basic.glsl b/test/fixtures/shaders/basic.glsl index 117b52a..cae66e5 100644 --- a/test/fixtures/shaders/basic.glsl +++ b/test/fixtures/shaders/basic.glsl @@ -25,3 +25,21 @@ void main(void) { vTransformedNormal = uNMatrix * aVertexNormal; vFace = aFace; } + +// Copied from ThreeJS (else without curly braces) +float getFace( vec3 direction ) { + vec3 absDirection = abs( direction ); + float face = - 1.0; + if ( absDirection.x > absDirection.z ) { + if ( absDirection.x > absDirection.y ) + face = direction.x > 0.0 ? 0.0 : 3.0; + else + face = direction.y > 0.0 ? 1.0 : 4.0; + } else { + if ( absDirection.z > absDirection.y ) + face = direction.z > 0.0 ? 2.0 : 5.0; + else + face = direction.y > 0.0 ? 1.0 : 4.0; + } + return face; +} \ No newline at end of file diff --git a/test/result/basic.bundle.js b/test/result/basic.bundle.js index bfa7f87..87b0de5 100644 --- a/test/result/basic.bundle.js +++ b/test/result/basic.bundle.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - var shaderSource = "#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec3 aVertexPosition;attribute vec3 aVertexNormal;attribute vec2 aTextureCoord;attribute float aFace;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;uniform mat3 uNMatrix;varying vec2 vTextureCoord;varying vec3 vTransformedNormal;varying vec4 vPosition;varying float vFace;void main(void){vPosition=uMVMatrix*vec4(aVertexPosition,1.0);gl_Position=uPMatrix*vPosition;vTextureCoord=aTextureCoord;vTransformedNormal=uNMatrix*aVertexNormal;vFace=aFace;}"; + var shaderSource = "#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec3 aVertexPosition;attribute vec3 aVertexNormal;attribute vec2 aTextureCoord;attribute float aFace;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;uniform mat3 uNMatrix;varying vec2 vTextureCoord;varying vec3 vTransformedNormal;varying vec4 vPosition;varying float vFace;void main(void){vPosition=uMVMatrix*vec4(aVertexPosition,1.0);gl_Position=uPMatrix*vPosition;vTextureCoord=aTextureCoord;vTransformedNormal=uNMatrix*aVertexNormal;vFace=aFace;}float getFace(vec3 direction){vec3 absDirection=abs(direction);float face=-1.0;if(absDirection.x>absDirection.z){if(absDirection.x>absDirection.y)face=direction.x>0.0 ? 0.0 : 3.0;else face=direction.y>0.0 ? 1.0 : 4.0;}else{if(absDirection.z>absDirection.y)face=direction.z>0.0 ? 2.0 : 5.0;else face=direction.y>0.0 ? 1.0 : 4.0;}return face;}"; assert.equal(typeof shaderSource, 'string'); const lines = shaderSource.split(/\n/);