@@ -23,7 +23,7 @@ var Descriptor = require('./descriptor')
23
23
var prepr = require ( 'prepr' )
24
24
25
25
var floatRE = / ^ - ? [ 0 - 9 ] * (?: .[ 0 - 9 ] + ) ? (?: e - ? [ 0 - 9 ] + ) ? $ / i
26
-
26
+ var swizzleRE = / ^ [ x y z w s t p d r g b a ] { 1 , 4 } $ /
27
27
28
28
/**
29
29
* Create GLSL codegen instance
@@ -643,7 +643,7 @@ GLSL.prototype.transforms = {
643
643
var prop = node . children [ 1 ] . data
644
644
645
645
//ab.xyz for example
646
- if ( / ^ [ x y z w s t p d r g b a ] { 1 , 4 } $ / . test ( prop ) ) {
646
+ if ( swizzleRE . test ( prop ) ) {
647
647
return this . unswizzle ( node )
648
648
}
649
649
@@ -811,7 +811,7 @@ GLSL.prototype.transforms = {
811
811
812
812
//something[N] return as is
813
813
return Descriptor ( `${ left } [${ right } ]` , {
814
- type : type ,
814
+ type : type || null ,
815
815
complexity : left . complexity + right . complexity + 1
816
816
} )
817
817
}
@@ -836,7 +836,11 @@ GLSL.prototype.transforms = {
836
836
}
837
837
838
838
var target = left
839
- var isSwizzle = node . children [ 0 ] . type === 'operator' && / ^ [ x y z w s t p d r g b a ] { 1 , 4 } $ / . test ( node . children [ 0 ] . children [ 1 ] . data )
839
+
840
+ // here some targets may be unswizzled already, eg.
841
+ // [a[0], a[1]], a[0][0], etc.
842
+ var isSwizzle = node . children [ 0 ] . type === 'operator' &&
843
+ swizzleRE . test ( node . children [ 0 ] . children [ 1 ] . data )
840
844
841
845
//a *= b.x
842
846
if ( ! isSwizzle && this . types [ right . type ] . length == 1 && this . types [ target . type ] . length == 1 ) {
@@ -848,7 +852,7 @@ GLSL.prototype.transforms = {
848
852
849
853
//FIXME: left can be a structure property set a.prop
850
854
851
- //in cases of setting swizzle - we gotta drop left unswizzle to the right
855
+ //in cases of setting swizzle - we have to place left unswizzle to the right
852
856
if ( isSwizzle ) {
853
857
var positions = this . swizzlePositions ( node . children [ 0 ] . children [ 1 ] . data )
854
858
var len = this . types [ this . process ( node . children [ 0 ] . children [ 0 ] ) . type ] . length
@@ -858,10 +862,15 @@ GLSL.prototype.transforms = {
858
862
ids [ positions [ i ] ] = i
859
863
}
860
864
861
- target = Descriptor ( node . children [ 0 ] . children [ 0 ] . data , {
862
- type : right . type ,
863
- optimize : false
864
- } )
865
+ var targetType = node . children [ 0 ] . children [ 0 ] . type
866
+
867
+ // a.x = ...
868
+ if ( ( targetType === 'ident' || targetType === 'builtin' ) ) {
869
+ target = Descriptor ( node . children [ 0 ] . children [ 0 ] . data , {
870
+ type : right . type ,
871
+ optimize : false
872
+ } )
873
+ }
865
874
866
875
//a.wy *= a.zx →
867
876
//a = [null, 1, null, 0].map(function (idx, i) {
@@ -903,10 +912,17 @@ GLSL.prototype.transforms = {
903
912
}
904
913
//a.x *= b → a[0] *= b
905
914
else {
906
- return Descriptor ( `${ target } [${ positions [ 0 ] } ] ${ operator } ${ right } ` , {
907
- type : right . type ,
908
- optimize : false
909
- } )
915
+ if ( targetType === 'builtin' || targetType === 'ident' ) {
916
+ return Descriptor ( `${ target } [${ positions [ 0 ] } ] ${ operator } ${ right } ` , {
917
+ type : right . type ,
918
+ optimize : false
919
+ } )
920
+ } else {
921
+ return Descriptor ( `${ target } ${ operator } ${ right } ` , {
922
+ type : right . type ,
923
+ optimize : false
924
+ } )
925
+ }
910
926
}
911
927
}
912
928
@@ -1212,11 +1228,13 @@ GLSL.prototype.unswizzle = function (node) {
1212
1228
}
1213
1229
else {
1214
1230
if ( args [ 0 ] == null ) console . warn ( `Cannot unswizzle '${ ident . type } (${ ident } ).${ prop } ': ${ prop } is outside the type range.` )
1231
+
1215
1232
result = Descriptor ( args [ 0 ] || `undefined` , {
1216
1233
type : 'float' ,
1217
1234
complexity : 1
1218
1235
} )
1219
1236
}
1237
+
1220
1238
return result
1221
1239
}
1222
1240
0 commit comments