@@ -131,120 +131,6 @@ BSplineCurve.clampedKnots=function(controlPoints,degree){
131131}
132132}
133133
134- function meshToJson ( mesh ) {
135- function colorToHex ( x ) {
136- var r = Math . round ( x [ 0 ] * 255 ) ;
137- var g = Math . round ( x [ 1 ] * 255 ) ;
138- var b = Math . round ( x [ 2 ] * 255 ) ;
139- r = Math . min ( Math . max ( r , 0 ) , 255 ) ;
140- g = Math . min ( Math . max ( g , 0 ) , 255 ) ;
141- b = Math . min ( Math . max ( b , 0 ) , 255 ) ;
142- return ( r << 16 ) | ( g << 8 ) | b ;
143- }
144- var roundNum = function ( num ) {
145- return Math . round ( num * 1000000 ) / 1000000 ;
146- }
147- var faces = [ ] ;
148- var vertices = [ ] ;
149- var colors = [ ] ;
150- var normals = [ ] ;
151- var texcoords = [ [ ] ] ;
152- var json = {
153- "metadata" :{ "formatVersion" :3.1 } ,
154- "materials" :[ { "DbgColor" :0xffffff , "DbgIndex" :0 ,
155- "DbgName" :"Untitled" , "colorDiffuse" :[ 1 , 1 , 1 ] ,
156- "colorAmbient" :[ 1 , 1 , 1 ] , "colorSpecular" :[ 1 , 1 , 1 ] ,
157- "specularCoef" :5 } ]
158- }
159- json . faces = faces ;
160- json . vertices = vertices ;
161- json . colors = colors ;
162- json . uvs = texcoords ;
163- json . normals = normals ;
164- function pushItemAndMaybeReuse3 ( obj , x , y , z ) {
165- var idx = obj . length ;
166- var index = idx / 3 ;
167- var endIdx = Math . max ( 0 , idx - 48 ) ;
168- for ( var i = idx - 3 ; i >= endIdx ; i -= 3 ) {
169- if ( obj [ i ] == x && obj [ i + 1 ] == y && obj [ i + 2 ] == z ) {
170- return i / 3 ;
171- }
172- }
173- obj . push ( x )
174- obj . push ( y )
175- obj . push ( z )
176- return index ;
177- }
178- function pushItemAndMaybeReuse2 ( obj , x , y ) {
179- var idx = obj . length ;
180- var index = idx / 2 ;
181- var endIdx = Math . max ( 0 , idx - 48 ) ;
182- for ( var i = idx - 2 ; i >= endIdx ; i -= 2 ) {
183- if ( obj [ i ] == x && obj [ i + 1 ] == y ) {
184- return i / 2 ;
185- }
186- }
187- obj . push ( x )
188- obj . push ( y )
189- return index ;
190- }
191- function pushItemAndMaybeReuse1 ( obj , x ) {
192- var idx = obj . length ;
193- var index = idx ;
194- var endIdx = Math . max ( 0 , idx - 48 ) ;
195- for ( var i = idx - 1 ; i >= endIdx ; i -= 1 ) {
196- if ( obj [ i ] == x ) {
197- return i ;
198- }
199- }
200- obj . push ( x )
201- return index ;
202- }
203- mesh . enumPrimitives ( function ( prim ) {
204- if ( prim . length != 3 ) throw new Error ( "lines and points not supported" ) ;
205- var idx = faces . length ;
206- var flags = 0 ;
207- faces . push ( 0 ) ;
208- for ( var j = 0 ; j < 3 ; j ++ ) {
209- faces . push (
210- pushItemAndMaybeReuse3 ( vertices ,
211- roundNum ( prim [ j ] . position [ 0 ] ) ,
212- roundNum ( prim [ j ] . position [ 1 ] ) ,
213- roundNum ( prim [ j ] . position [ 2 ] ) ) )
214- }
215- if ( prim [ 0 ] . uv && prim [ 1 ] . uv && prim [ 2 ] . uv ) {
216- var tc = texcoords [ 0 ] ;
217- for ( var j = 0 ; j < 3 ; j ++ ) {
218- faces . push (
219- pushItemAndMaybeReuse2 ( tc ,
220- roundNum ( prim [ j ] . uv [ 0 ] ) ,
221- roundNum ( prim [ j ] . uv [ 1 ] ) ) )
222- }
223- flags |= 0x08 ;
224- }
225- if ( prim [ 0 ] . normal && prim [ 1 ] . normal && prim [ 2 ] . normal ) {
226- for ( var j = 0 ; j < 3 ; j ++ ) {
227- faces . push (
228- pushItemAndMaybeReuse3 ( normals ,
229- roundNum ( prim [ j ] . normal [ 0 ] ) ,
230- roundNum ( prim [ j ] . normal [ 1 ] ) ,
231- roundNum ( prim [ j ] . normal [ 2 ] ) ) )
232- }
233- flags |= 0x20 ;
234- }
235- if ( prim [ 0 ] . color && prim [ 1 ] . color && prim [ 2 ] . color ) {
236- for ( var j = 0 ; j < 3 ; j ++ ) {
237- faces . push (
238- pushItemAndMaybeReuse1 ( colors ,
239- colorToHex ( prim [ j ] . color ) ) )
240- }
241- flags |= 0x80 ;
242- }
243- faces [ idx ] = flags ;
244- } )
245- return JSON . stringify ( json ) ;
246- }
247-
248134function saveString ( string , type , filename ) {
249135 extension = ".txt"
250136 type = type || "text/plain"
@@ -273,7 +159,7 @@ function updateShape(func){
273159 a . id = "settings-link"
274160 a . innerHTML = "Save this model (JSON)"
275161 a . addEventListener ( "click" , function ( ) {
276- var json = meshToJson ( func ( allsettings ) ) ;
162+ var json = MeshJSON . toJSON ( func ( allsettings ) ) ;
277163 saveString ( json , "application/json" , "model.json" )
278164 } ) ;
279165 div . appendChild ( a )
@@ -341,3 +227,5 @@ window.addEventListener("load",function(){
341227 }
342228 } )
343229} )
230+
231+ document . write ( "<script src='../extras/meshjson.js'></script>" )
0 commit comments