/*
Author: James Vella
Website: http://www.jamesvella.net/
Name-US: JV_PhysicalToCorona
Version: 1.1.0
Software: 3D Studio Max 2021.3
Engine: Corona 7.0
Language: Maxscript
Description-US: Convert Autodesk Physical Materials to Corona Physical Materials
*/

macroscript PhysicalToCorona
	category:"Vella" 
	internalcategory:"Automate"
	tooltip:"Automate PhysicalToCorona"
	buttonText:"PhysicalMtl To CoronaMtl"
	Icon:#("GameTools",2)

(
	undo on

	-- Function to recursively apply Gamma 1.0 to Bitmap
	fn applyGammaCorrection i =
	(
		-- Check if the input is a bitmap
		if isKindOf i BitmapTexture do
		(
			-- Apply Gamma 1.0 to the bitmap
			i.bitmap = openBitmap i.bitmap.filename gamma:1.0
		)

		-- Check if the input has sub-properties
		if i.numsubs > 0 do
		(
			-- Traverse the sub-properties
			for j = 1 to i.numsubs do
			(
				-- Recursively apply gamma correction to sub-properties
				applyGammaCorrection i[j]
			)
		)

		-- Return the input unchanged
		i
	)

	-- Function to replace Physical Mtl with Corona Mtl
	fn convertToCoronaMtl i =
	(
		-- Standard Material Settings for Physical Corona Mtl
		newMat = CoronaPhysicalMtl()
		newMat.name = i.name
		
		-- Replace Diffuse Node
		try
		(
			if (i.base_color_map != undefined) then
			(
				-- Copy bitmap from Physical Mtl
				newMat.baseTexmap = i.base_color_map
			)
			else 
			-- Replace Diffuse Color Swatch if no Diffuse Node found 
			(
				newMat.basecolor = i.base_color
			)
		)
		catch()

		-- Replace Roughness texture
		try
		(
			if (i.roughness_map != undefined) then
			(
				-- Copy bitmap from Physical Mtl
				newMat.baseRoughnessTexmap = i.roughness_map
				-- Apply Gamma 1.0 from applyGammaCorrection function
				applyGammaCorrection i.roughness_map
			)
		)
		catch()
		
		-- Replace Opacity texture
		try
		(
			if (i.cutout_map != undefined) then
			(
				-- Copy bitmap from Physical Mtl
				newMat.opacityTexmap = i.cutout_map
				-- Apply Gamma 1.0 from applyGammaCorrection function
				applyGammaCorrection i.cutout_map
			)
		)
		catch()

		-- Replace Normal texture
		try
		(
			if (i.bump_map != undefined) then
			(
				-- Create Corona Normal node
				newmat.baseBumpTexmap = CoronaNormal ()
				-- Copy bitmap from Physical Mtl
				newmat.baseBumpTexmap.normalMap = i.bump_map
				-- Apply Gamma 1.0 from applyGammaCorrection function
				applyGammaCorrection i.bump_map
			)
		)
		catch()
		
		-- Replace Metal texture
		try
		(
			if (i.metalness_map != undefined) then
			(
				-- Change Physical Material to Metal type
				newMat.metalnessMode = 1
				-- Copy bitmap from Physical Mtl
				newMat.metalnessTexmap = i.metalness_map
				-- Apply Gamma 1.0 from applyGammaCorrection function
				applyGammaCorrection i.metalness_map
			)
		)
		catch()
		
		-- Convert Physical Mtl to Corona Mtl
		i = newMat
	)

	--
	-- Execute Functions
	--

	-- Search through all scene materials
	for i in sceneMaterials do
	(
		try
		(
			-- Convert Physical Mtl with Corona Mtl
			if classof i == PhysicalMaterial do 
			(
				-- Run Function convertToCoronaMtl to convert materials
				p = convertToCoronaMtl i

				if i.name == p.name do 
				(
					replaceInstances i p
				)
			)
		)
		catch()
		
		-- Replace Children Materials in Multi-Sub Materials
		try
		(
			if classof i == Multimaterial do    
			(
				for n in i do
				(
					if classof n == PhysicalMaterial do 
					(
						-- Run Function convertToCoronaMtl to convert materials
						p = convertToCoronaMtl n
					
						if n.name == p.name do 
						(
							replaceInstances n p
						)
					)   
				) 
			)
		)
		catch()
	)

	-- Refresh asset tracker
	ATSOps.Refresh()
)