44 * @license {@link https://opensource.org/licenses/MIT|MIT License }
55 */
66
7+ var Face = require ( './Face' ) ;
78var GetFastValue = require ( '../../utils/object/GetFastValue' ) ;
9+ var Matrix4 = require ( '../../math/Matrix4' ) ;
10+ var Vector3 = require ( '../../math/Vector3' ) ;
11+ var Vertex = require ( './Vertex' ) ;
12+
13+ var tempPosition = new Vector3 ( ) ;
14+ var tempRotation = new Vector3 ( ) ;
15+ var tempMatrix = new Matrix4 ( ) ;
816
917/**
1018 * Creates a grid of vertices based on the given configuration object and optionally adds it to a Mesh.
@@ -37,19 +45,33 @@ var GetFastValue = require('../../utils/object/GetFastValue');
3745 */
3846var GenerateGridVerts = function ( config )
3947{
40- var texture = GetFastValue ( config , 'texture' ) ;
41- var frame = GetFastValue ( config , 'frame' ) ;
4248 var mesh = GetFastValue ( config , 'mesh' ) ;
49+ var texture = GetFastValue ( config , 'texture' , ( mesh ) ? mesh . texture : null ) ;
50+ var frame = GetFastValue ( config , 'frame' ) ;
4351 var width = GetFastValue ( config , 'width' , 128 ) ;
4452 var height = GetFastValue ( config , 'height' , width ) ;
4553 var widthSegments = GetFastValue ( config , 'widthSegments' , 1 ) ;
4654 var heightSegments = GetFastValue ( config , 'heightSegments' , widthSegments ) ;
4755 var posX = GetFastValue ( config , 'x' , 0 ) ;
4856 var posY = GetFastValue ( config , 'y' , 0 ) ;
49- var colors = GetFastValue ( config , 'colors' , 0xffffff ) ;
50- var alphas = GetFastValue ( config , 'alphas' , 1 ) ;
57+ var posZ = GetFastValue ( config , 'z' , 0 ) ;
58+ var rotateX = GetFastValue ( config , 'rotateX' , 0 ) ;
59+ var rotateY = GetFastValue ( config , 'rotateY' , 0 ) ;
60+ var rotateZ = GetFastValue ( config , 'rotateZ' , 0 ) ;
61+ var zIsUp = GetFastValue ( config , 'zIsUp' , true ) ;
62+ var colors = GetFastValue ( config , 'colors' , [ 0xffffff ] ) ;
63+ var alphas = GetFastValue ( config , 'alphas' , [ 1 ] ) ;
5164 var tile = GetFastValue ( config , 'tile' , false ) ;
5265
66+ var result = {
67+ faces : [ ] ,
68+ verts : [ ]
69+ } ;
70+
71+ tempPosition . set ( posX , posY , posZ ) ;
72+ tempRotation . set ( rotateX , rotateY , rotateZ ) ;
73+ tempMatrix . fromRotationXYTranslation ( tempRotation , tempPosition , zIsUp ) ;
74+
5375 var halfWidth = width / 2 ;
5476 var halfHeight = height / 2 ;
5577
@@ -64,110 +86,117 @@ var GenerateGridVerts = function (config)
6486
6587 var uvs = [ ] ;
6688 var vertices = [ ] ;
67- var indices = [ ] ;
6889
6990 var ix ;
7091 var iy ;
7192
72- var textureFrame = texture . get ( frame ) ;
93+ var frameU0 = 0 ;
94+ var frameU1 = 1 ;
95+ var frameV0 = 0 ;
96+ var frameV1 = 1 ;
7397
74- var frameU0 = textureFrame . u0 ;
75- var frameU1 = textureFrame . u1 ;
98+ if ( texture )
99+ {
100+ var textureFrame = texture . get ( frame ) ;
76101
77- var frameV0 = textureFrame . v0 ;
78- var frameV1 = textureFrame . v1 ;
102+ frameU0 = textureFrame . u0 ;
103+ frameU1 = textureFrame . u1 ;
104+ frameV0 = textureFrame . v0 ;
105+ frameV1 = textureFrame . v1 ;
106+ }
79107
80108 var frameU = frameU1 - frameU0 ;
81109 var frameV = frameV1 - frameV0 ;
82110
83- var tv ;
84- var tu ;
85-
86111 for ( iy = 0 ; iy < gridY1 ; iy ++ )
87112 {
88- var y = posY + ( iy * segmentHeight - halfHeight ) ;
113+ var y = iy * segmentHeight - halfHeight ;
89114
90115 for ( ix = 0 ; ix < gridX1 ; ix ++ )
91116 {
92- var x = posX + ( ix * segmentWidth - halfWidth ) ;
117+ var x = ix * segmentWidth - halfWidth ;
93118
94119 vertices . push ( x , - y ) ;
95120
96- if ( ! tile )
97- {
98- tu = frameU0 + frameU * ( ix / gridX ) ;
99- tv = frameV0 + frameV * ( 1 - ( iy / gridY ) ) ;
121+ var tu = frameU0 + frameU * ( ix / gridX ) ;
122+ var tv = frameV0 + frameV * ( iy / gridY ) ;
100123
101- uvs . push ( tu , tv ) ;
102- }
124+ uvs . push ( tu , tv ) ;
103125 }
104126 }
105127
106- var tiledVertices = [ ] ;
128+ if ( ! Array . isArray ( colors ) )
129+ {
130+ colors = [ colors ] ;
131+ }
132+
133+ if ( ! Array . isArray ( alphas ) )
134+ {
135+ alphas = [ alphas ] ;
136+ }
137+
138+ var alphaIndex = 0 ;
139+ var colorIndex = 0 ;
107140
108141 for ( iy = 0 ; iy < gridY ; iy ++ )
109142 {
110143 for ( ix = 0 ; ix < gridX ; ix ++ )
111144 {
112- var a = ix + gridX1 * iy ;
113- var b = ix + gridX1 * ( iy + 1 ) ;
114- var c = ( ix + 1 ) + gridX1 * ( iy + 1 ) ;
115- var d = ( ix + 1 ) + gridX1 * iy ;
145+ var a = ( ix + gridX1 * iy ) * 2 ;
146+ var b = ( ix + gridX1 * ( iy + 1 ) ) * 2 ;
147+ var c = ( ( ix + 1 ) + gridX1 * ( iy + 1 ) ) * 2 ;
148+ var d = ( ( ix + 1 ) + gridX1 * iy ) * 2 ;
149+
150+ var color = colors [ colorIndex ] ;
151+ var alpha = alphas [ alphaIndex ] ;
152+
153+ var vert1 = new Vertex ( vertices [ a ] , vertices [ a + 1 ] , 0 , uvs [ a ] , uvs [ a + 1 ] , color , alpha ) . transformMat4 ( tempMatrix ) ;
154+ var vert2 = new Vertex ( vertices [ b ] , vertices [ b + 1 ] , 0 , uvs [ b ] , uvs [ b + 1 ] , color , alpha ) . transformMat4 ( tempMatrix ) ;
155+ var vert3 = new Vertex ( vertices [ d ] , vertices [ d + 1 ] , 0 , uvs [ d ] , uvs [ d + 1 ] , color , alpha ) . transformMat4 ( tempMatrix ) ;
156+ var vert4 = new Vertex ( vertices [ b ] , vertices [ b + 1 ] , 0 , uvs [ b ] , uvs [ b + 1 ] , color , alpha ) . transformMat4 ( tempMatrix ) ;
157+ var vert5 = new Vertex ( vertices [ c ] , vertices [ c + 1 ] , 0 , uvs [ c ] , uvs [ c + 1 ] , color , alpha ) . transformMat4 ( tempMatrix ) ;
158+ var vert6 = new Vertex ( vertices [ d ] , vertices [ d + 1 ] , 0 , uvs [ d ] , uvs [ d + 1 ] , color , alpha ) . transformMat4 ( tempMatrix ) ;
159+
160+ if ( tile )
161+ {
162+ vert1 . setUVs ( frameU0 , frameV1 ) ;
163+ vert2 . setUVs ( frameU0 , frameV0 ) ;
164+ vert3 . setUVs ( frameU1 , frameV1 ) ;
165+ vert4 . setUVs ( frameU0 , frameV0 ) ;
166+ vert5 . setUVs ( frameU1 , frameV0 ) ;
167+ vert6 . setUVs ( frameU1 , frameV1 ) ;
168+ }
116169
117- if ( ! tile )
170+ colorIndex ++ ;
171+
172+ if ( colorIndex === colors . length )
118173 {
119- indices . push ( a , b , d ) ;
120- indices . push ( b , c , d ) ;
174+ colorIndex = 0 ;
121175 }
122- else
176+
177+ alphaIndex ++ ;
178+
179+ if ( alphaIndex === alphas . length )
123180 {
124- a *= 2 ;
125- b *= 2 ;
126- c *= 2 ;
127- d *= 2 ;
128-
129- tiledVertices . push (
130- vertices [ a ] , vertices [ a + 1 ] ,
131- vertices [ b ] , vertices [ b + 1 ] ,
132- vertices [ d ] , vertices [ d + 1 ] ,
133-
134- vertices [ b ] , vertices [ b + 1 ] ,
135- vertices [ c ] , vertices [ c + 1 ] ,
136- vertices [ d ] , vertices [ d + 1 ]
137- ) ;
138-
139- uvs . push (
140- frameU0 , frameV1 ,
141- frameU0 , frameV0 ,
142- frameU1 , frameV1 ,
143-
144- frameU0 , frameV0 ,
145- frameU1 , frameV0 ,
146- frameU1 , frameV1
147- ) ;
181+ alphaIndex = 0 ;
148182 }
183+
184+ result . verts . push ( vert1 , vert2 , vert3 , vert4 , vert5 , vert6 ) ;
185+
186+ result . faces . push (
187+ new Face ( vert1 , vert2 , vert3 ) ,
188+ new Face ( vert4 , vert5 , vert6 )
189+ ) ;
149190 }
150191 }
151192
152193 if ( mesh )
153194 {
154- if ( tile )
155- {
156- mesh . addVertices ( tiledVertices , uvs , null , colors , alphas ) ;
157- }
158- else
159- {
160- mesh . addVertices ( vertices , uvs , indices , colors , alphas ) ;
161- }
195+ mesh . faces = mesh . faces . concat ( result . faces ) ;
196+ mesh . vertices = mesh . vertices . concat ( result . verts ) ;
162197 }
163198
164- return {
165- verts : ( tile ) ? tiledVertices : vertices ,
166- indices : indices ,
167- uvs : uvs ,
168- colors : colors ,
169- alphas : alphas
170- } ;
199+ return result ;
171200} ;
172201
173202module . exports = GenerateGridVerts ;
0 commit comments