Преглед изворни кода

Updated max exporter to use new JSON model format.

(also fixed vertex colors to use proper per-face-vertex colors)
alteredq пре 14 година
родитељ
комит
a069bbf966
1 измењених фајлова са 164 додато и 201 уклоњено
  1. 164 201
      utils/exporters/max/ThreeJSExporter.ms

+ 164 - 201
utils/exporters/max/ThreeJSExporter.ms

@@ -118,16 +118,6 @@ rollout ThreeJSExporter "ThreeJSExporter"
 	-------------------------------------------------------------------------------------
 	-- Dump colors
 
-	function SmallColor c = 
-	(
-
-		if c == 255 then 
-			return "1"
-		else 
-			return formattedPrint ( c / 255 ) format:".4f"
-
-	)
-	
 	function DumpColors src useColors = 
 	(
 
@@ -143,11 +133,15 @@ rollout ThreeJSExporter "ThreeJSExporter"
 
 				col = src[i]
 				
-				r = SmallColor col.r
-				g = SmallColor col.g
-				b = SmallColor col.b
+				r = col.r as Integer
+				g = col.g as Integer
+				b = col.b as Integer
+				
+				hexNum = ( bit.shift r 16 ) + ( bit.shift g 8 ) + b
 				
-				Format "%,%,%" r g b to:ostream
+				hexColor = formattedPrint hexNum format:"#x"
+				
+				Format "%" hexColor to:ostream
 				
 				if i < num then Format "," to:ostream
 
@@ -215,7 +209,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 	function DumpUvs src = 
 	(
 
-		Format "'uvs': [" to:ostream
+		Format "'uvs': [[" to:ostream
 
 		num = src.count
 
@@ -246,177 +240,172 @@ rollout ThreeJSExporter "ThreeJSExporter"
 
 		)
 
-		Format "],\n\n" to:ostream
+		Format "]],\n\n" to:ostream
 
 	)
 
 	-------------------------------------------------------------------------------------
-	-- Dump face type
+	-- Dump faces
 
-	function DumpFaceType label content = 
+	function DumpFaces src useColors = 
 	(
-		Format "'%': [" label to:ostream
 
-		num = content.count
+		Format "'faces': [" to:ostream
+		
+		num = src.count
 
-		if num > 0 then 
+		if num > 0 then
 		(
 
 			for i = 1 to num do
 			(
 
-				zface = content[i]
+				zface = src[i]
 
 				fv  = zface[1]
 				fuv = zface[2]
 				m   = zface[3] - 1
-
-				needsFlip = zface[4]
-
-				hasUVs = (classof fuv == Point3)
-
-				va = (fv.x - 1) as Integer
-				vb = (fv.y - 1) as Integer
-				vc = (fv.z - 1) as Integer
-
-				if smoothNormal.checked then 
-				(
-
-					-- normals have the same indices as vertices
-
-					na = va
-					nb = vb
-					nc = vc
-
-				)
-				else 
+				fc  = zface[4]
+				
+				needsFlip = zface[5]
+				
+				isTriangle = true
+				hasMaterial = true
+				hasFaceUvs = false
+				hasFaceVertexUvs = ((classof fuv == Point3) and exportUv.checked)
+				hasFaceNormals = false
+				hasFaceVertexNormals = (exportNormal.checked)
+				hasFaceColors = false
+				hasFaceVertexColors = ((classof fc == Point3) and useColors)
+
+				faceType = 0
+				faceType = bit.set faceType 1 (not isTriangle)
+				faceType = bit.set faceType 2 hasMaterial
+				faceType = bit.set faceType 3 hasFaceUvs
+				faceType = bit.set faceType 4 hasFaceVertexUvs
+				faceType = bit.set faceType 5 hasFaceNormals
+				faceType = bit.set faceType 6 hasFaceVertexNormals
+				faceType = bit.set faceType 7 hasFaceColors
+				faceType = bit.set faceType 8 hasFaceVertexColors
+				
+				if i > 1 then
 				(
-					-- normals have the same indices as face
-
-					na = i - 1
-					nb = na
-					nc = na
-
+					Format "," faceType to:ostream
 				)
+				
+				Format "%" faceType to:ostream
 
-				if hasUVs then
+				if isTriangle then
 				(
-				
-					ua = (fuv.x - 1) as Integer
-					ub = (fuv.y - 1) as Integer
-					uc = (fuv.z - 1) as Integer
 
-				)
+					va = (fv.x - 1) as Integer
+					vb = (fv.y - 1) as Integer
+					vc = (fv.z - 1) as Integer
+					
+					if flipFace.checked or needsFlip then 
+					(
 
-				if flipFace.checked or needsFlip then 
-				(
-					tmp = vb
-					vb = vc
-					vc = tmp
+						tmp = vb
+						vb = vc
+						vc = tmp
 
-					tmp = nb
-					nb = nc
-					nc = tmp
+					)
+					
+					
+					Format ",%,%,%" va vb vc to:ostream
 
-					if hasUVs then
+				
+					if hasMaterial then 
 					(
 
-						tmp = ub
-						ub = uc
-						uc = tmp
+						Format ",%" m to:ostream
 
 					)
+					
+					if hasFaceVertexUvs then
+					(
 
-				)
+						ua = (fuv.x - 1) as Integer
+						ub = (fuv.y - 1) as Integer
+						uc = (fuv.z - 1) as Integer
+						
+						if flipFace.checked or needsFlip then 
+						(
 
-				if label == "triangles" then
-				(
-					Format triFormat va vb vc m to:ostream
-				)
-				else if label == "trianglesUvs" then
-				(
-					Format triUVFormat va vb vc m ua ub uc to:ostream
-				)
-				else if label == "trianglesNormals" then
-				(
-					Format triNFormat va vb vc m na nb nc to:ostream
-				)
-				else if label == "trianglesNormalsUvs" then
-				(
-					Format triUVNFormat va vb vc m na nb nc ua ub uc to:ostream
-				)
+							tmp = ub
+							ub = uc
+							uc = tmp
 
-				if i < num then Format "," to:ostream
+						)
+						
+						Format ",%,%,%" ua ub uc to:ostream
 
-			)
+					)
+				
+					if hasFaceVertexNormals then
+					(
 
-		)
+						if smoothNormal.checked then 
+						(
 
-		Format "],\n\n" to:ostream
+							-- normals have the same indices as vertices
 
-	)
+							na = va
+							nb = vb
+							nc = vc
 
-	-------------------------------------------------------------------------------------
-	-- Dump faces
+						)
+						else 
+						(
+							-- normals have the same indices as face
 
-	function DumpFaces src = 
-	(
+							na = i - 1
+							nb = na
+							nc = na
 
-		hasUVs = true
+						)
+						
+						if flipFace.checked or needsFlip then 
+						(
 
-		triangles = #()
-		trianglesUvs = #()
-		trianglesNormals = #()
-		trianglesNormalsUvs = #()
+							tmp = nb
+							nb = nc
+							nc = tmp
 
-		quads = #()
-		quadsUvs = #()
-		quadsNormals = #()
-		quadsNormalsUvs = #()
+						)
+						
+						Format ",%,%,%" na nb nc to:ostream
 
-		num = src.count
+					)
 
-		if num > 0 then
-		(
+				
+					if hasFaceVertexColors then
+					(
 
-			for i = 1 to num do
-			(
+						ca = (fc.x - 1) as Integer
+						cb = (fc.y - 1) as Integer
+						cc = (fc.z - 1) as Integer
+						
+						if flipFace.checked or needsFlip then 
+						(
 
-				zface = src[i]
-				fuv = zface[2]
+							tmp = cb
+							cb = cc
+							cc = tmp
 
-				hasUVs = (classof fuv == Point3)
+						)
+						
+						Format ",%,%,%" ca cb cc to:ostream
 
-				if hasUVs and exportUv.checked and exportNormal.checked then
-				(
-					append trianglesNormalsUvs zface
-				)
-				else if exportNormal.checked then
-				(
-					append trianglesNormals zface
-				)
-				else if hasUVs and exportUv.checked then
-				(
-					append trianglesUvs zface
-				)
-				else
-				(
-					append triangles zface
-				)
+					)
 
+				)
+				
 			)
 
 		)
 
-		DumpFaceType "triangles" triangles
-		DumpFaceType "trianglesUvs" trianglesUvs
-		DumpFaceType "trianglesNormals" trianglesNormals
-		DumpFaceType "trianglesNormalsUvs" trianglesNormalsUvs
-
-		DumpFaceType "quads" quads
-		DumpFaceType "quadsUvs" quadsUvs
-		DumpFaceType "quadsNormals" quadsNormals
-		DumpFaceType "quadsNormalsUvs" quadsNormalsUvs
+		Format "]\n\n" to:ostream
 
 	)
 
@@ -435,7 +424,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 
 		Format "'%'  : [%, %, %],\n" label fr fg fb to:ostream
 
-	)
+	)	
 
 	-------------------------------------------------------------------------------------
 	-- Dump map
@@ -462,7 +451,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 	-------------------------------------------------------------------------------------
 	-- Export materials
 
-	function ExportMaterials zmaterials useVertexColors = 
+	function ExportMaterials zmaterials zcolors = 
 	(
 
 		Format "'materials': [\n" to:ostream
@@ -482,6 +471,8 @@ rollout ThreeJSExporter "ThreeJSExporter"
 			if classof mat != BooleanClass then
 			(
 
+				useVertexColors = zcolors[i]
+				
 				Format "'DbgName'  : '%',\n" mat.name to:ostream
 				
 				-- colors
@@ -547,80 +538,27 @@ rollout ThreeJSExporter "ThreeJSExporter"
 	-------------------------------------------------------------------------------------
 	-- Extract vertex colors from mesh
 
-	function BlendValues x y =
-	(
-
-		return x + ( y - x )/2
-
-	)
-	
-	function HandleColor firstColor i whereto startOffset c = 
-	(
-
-		if firstColor[ i ] then 
-		(
-			whereto[ startOffset + i ] = c
-			firstColor[ i ] = false
-		)
-		else
-		(
-			whereto[ startOffset + i ] = BlendValues whereto[ startOffset + i ] c
-		)
-
-	)
-	
 	function ExtractColors obj whereto =
 	(
 
-		-- white no-op fallback color for all vertices
-		
-		nVertices = obj.numVerts
-		
-		startOffset = whereto.count
-		
-		firstColor = #()
-		
-		for i = 1 to nVertices do
-		(
-
-			append whereto (color 255 255 255)
-			append firstColor true
-
-		)
-
 		nColors = GetNumCPVVerts obj
 		
 		if nColors > 0 then
 		(
 
-			nFaces = obj.numFaces
-			
-			for i = 1 to nFaces do 
+			for i = 1 to nColors do
 			(
 
-				f  = GetFace obj i
-				cf = GetVCFace obj i
-				
-				c1 = GetVertColor obj cf[1]
-				c2 = GetVertColor obj cf[2]
-				c3 = GetVertColor obj cf[3]
-				
-				-- set vertices to vertex colors
-				-- ( there is no guaranteed 1-1 correspondence
-				--   between vertices and vertex colors in max
-				--   so this may mess up things, but in Three
-				--   there is 1-1 correspondence, so we need this )
-				
-				HandleColor firstColor f.x whereto startOffset c1
-				HandleColor firstColor f.y whereto startOffset c2
-				HandleColor firstColor f.z whereto startOffset c3
+				c = GetVertColor obj i
+				append whereto c
 
 			)
 
-		)		
+		)
 
 	)
 
+
 	-------------------------------------------------------------------------------------
 	-- Extract normals from mesh
 
@@ -694,7 +632,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 	-------------------------------------------------------------------------------------
 	-- Extract faces from mesh
 
-	function ExtractFaces objMesh objMaterial whereto allMaterials needsFlip offsetVert offsetUv =
+	function ExtractFaces objMesh objMaterial whereto allMaterials needsFlip hasVColors offsetVert offsetUv offsetColor =
 	(
 		n = objMesh.numFaces
 		hasUVs = objMesh.numTVerts > 0
@@ -796,10 +734,28 @@ rollout ThreeJSExporter "ThreeJSExporter"
 				fuv = false
 
 			)
+			
+			if hasVColors then
+			(
+
+				fc = GetVCFace objMesh i
+				
+				fc.x += offsetColor
+				fc.y += offsetColor
+				fc.z += offsetColor
+
+			)
+			else
+			(
+
+				fc = false
+
+			)
 
 			append zface fv 
 			append zface fuv
 			append zface fm
+			append zface fc
 			append zface needsFlip
 
 			append whereto zface
@@ -811,7 +767,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 	-------------------------------------------------------------------------------------
 	-- Extract materials from eventual multi-material
 	
-	function ExtractMaterials objMesh objMaterial whereto zname =
+	function ExtractMaterials objMesh objMaterial whereto wheretoColors zname hasVColors =
 	(
 		
 		materialClass = classof objMaterial
@@ -823,6 +779,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 			(
 
 				append whereto objMaterial
+				append wheretoColors hasVColors
 
 			)
 
@@ -847,6 +804,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 					(
 
 						append whereto subMaterial
+						append wheretoColors hasVColors
 
 					)
 
@@ -861,6 +819,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 			-- unknown or undefined material
 			
 			append whereto false
+			append wheretoColors hasVColors
 
 		)
 
@@ -945,7 +904,9 @@ rollout ThreeJSExporter "ThreeJSExporter"
 		
 		mergedUvs = #()
 		mergedFaces = #()
+		
 		mergedMaterials = #()
+		mergedMaterialsColors = #()
 		
 		sceneHasVColors = false
 		
@@ -969,8 +930,9 @@ rollout ThreeJSExporter "ThreeJSExporter"
 
 				vertexOffset = mergedVertices.count
 				uvOffset = mergedUvs.count
+				colorOffset = mergedColors.count
 
-				ExtractMaterials meshObj meshMaterial mergedMaterials meshName
+				ExtractMaterials meshObj meshMaterial mergedMaterials mergedMaterialsColors meshName hasVColors
 
 				ExtractVertices meshObj mergedVertices
 				ExtractNormals meshObj mergedNormals needsFlip
@@ -978,7 +940,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 				
 				ExtractUvs meshObj mergedUvs
 
-				ExtractFaces meshObj meshMaterial mergedFaces mergedMaterials needsFlip vertexOffset uvOffset
+				ExtractFaces meshObj meshMaterial mergedFaces mergedMaterials needsFlip hasVColors vertexOffset uvOffset colorOffset
 
 			)
 
@@ -997,7 +959,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 		if sceneHasVColors and exportColor.checked then
 		(
 
-			totalColors = totalVertices
+			totalColors = mergedColors.count
 			useColors = true
 
 		)
@@ -1039,10 +1001,11 @@ rollout ThreeJSExporter "ThreeJSExporter"
 		-- Dump model
 
 		Format "\n\nvar model = {\n\n" to:ostream
+		Format "'version' :2,\n\n" to:ostream
 
 		-- Dump all materials in the scene
 
-		ExportMaterials mergedMaterials useColors
+		ExportMaterials mergedMaterials mergedMaterialsColors
 
 		-- Dump merged data from all selected geometries
 
@@ -1050,7 +1013,7 @@ rollout ThreeJSExporter "ThreeJSExporter"
 		DumpNormals mergedNormals
 		DumpColors mergedColors useColors
 		DumpUvs mergedUvs
-		DumpFaces mergedFaces
+		DumpFaces mergedFaces useColors
 
 		-- Dump footer