Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop breaking "else" without curly braces during compression #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/expected/basic.bundle.js
Original file line number Diff line number Diff line change
@@ -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/);
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/shaders/basic.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion test/result/basic.bundle.js
Original file line number Diff line number Diff line change
@@ -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/);
Expand Down