 		data = JSON.stringify data, null, "\t"
 	fs.writeFileSync file, data
 
+sceneFiles = []
+
+
+# Converter functions
 convertColor = (hexColor) ->
 	color =	for i in [1..7] by 2
 		Math.round((parseInt hexColor.substr(i,2), 16)/2.55)/100
+
+convertMaterials = (materials) ->
+	for material in materials
+		newmat =
+			uniforms: {}
+		newmat.name = material.MaterialName
+		
+		for key, value of material when /Color$/.test key
+			match = key.match(/^(.*)Color$/)
+			newkey = match[1]
+			newmat.uniforms['material'+newkey] = convertColor(value)
+		
+		if material.Shininess?
+			newmat.uniforms.materialSpecularPower = material.Shininess
+		
+		# TODO: Should be done in a better way
+		if material.TextureEntries?	
+			convertTextures(newmat, material.TextureEntries)
+			newmat.shaderRef = 'shaders/texturedLit.shader'
+		else 
+			newmat.shaderRef = 'shaders/simpleLit.shader'
+		
+		outputFile "materials/#{newmat.name}.mat.json", newmat
 
 convertTextures = (material, textures) ->
 	material.textures = []

		result[1] = Math.atan2(-matrix[2], matrix[0])
		result[0] = Math.atan2(-matrix[7], matrix[4])
		result[2] = Math.asin(matrix[1])
		
	return result


convertChildren = (children, parent, entities, compression) ->
	for child in children

	
	data = fs.readFileSync file
	oldObject = JSON.parse(data)	
	
	convertMaterials oldObject.Materials

	entities = []
	compression =
		compressed: oldObject.UseCompression
		compressedVertsRange: oldObject.CompressedVertsRange
		compressedColorsRange: oldObject.CompressedColorsRange
		compressedUnitVectorRange: oldObject.CompressedUnitVectorRange
	
	oldObject.Scene.Name = objectName
	
	convertChildren [oldObject.Scene], null, entities, compression
	
	for entity in entities
		outputFile "entities/#{entity.name}.ent.json", entity
