Updated export for Unity

This commit is contained in:
RodZill4 2020-02-19 23:24:34 +01:00
parent 4d73ff1a2b
commit b3d5972557
6 changed files with 322 additions and 19 deletions

View File

@ -223,12 +223,15 @@ func _serialize(data: Dictionary) -> Dictionary:
print("cannot save "+name)
return data
func _serialize_data(data: Dictionary) -> Dictionary:
return data
func serialize() -> Dictionary:
var rv = { name=name, type=get_type(), parameters={}, node_position={ x=position.x, y=position.y } }
for p in get_parameter_defs():
if parameters.has(p.name):
rv.parameters[p.name] = MMType.serialize_value(parameters[p.name])
else:
elif p.has("default"):
rv.parameters[p.name] = p.default
if seed_locked:
rv.seed_value = seed_value
@ -236,6 +239,7 @@ func serialize() -> Dictionary:
rv.type = model
else:
rv = _serialize(rv)
rv = _serialize_data(rv)
return rv
func _deserialize(_data : Dictionary) -> void:

View File

@ -2,6 +2,8 @@ tool
extends MMGenShader
class_name MMGenMaterial
var export_paths = {}
var material : SpatialMaterial
var generated_textures = {}
@ -200,9 +202,20 @@ func get_export_profiles() -> Array:
func get_export_extension(profile : String) -> String:
return shader_model.exports[profile].export_extension
func get_export_path(profile : String) -> String:
if export_paths.has(profile):
return export_paths[profile]
return ""
func subst_string(s : String, export_context : Dictionary) -> String:
for k in export_context.keys():
s = s.replace(k, export_context[k])
var modified : bool = true
while modified:
modified = false
for k in export_context.keys():
var new_s = s.replace(k, export_context[k])
if new_s != s:
s = new_s
modified = true
while (true):
var search_string = "$(expr:"
var position = s.find(search_string)
@ -233,8 +246,9 @@ func create_file_from_template(template : String, file_name : String, export_con
if in_file.open(OS.get_executable_path().get_base_dir()+"/generators/"+template, File.READ) != OK:
print("Cannot find template file "+template)
return false
if out_file.open(file_name, File.WRITE):
print("Cannot write file '"+file_name+"'")
Directory.new().remove(file_name)
if out_file.open(file_name, File.WRITE) != OK:
print("Cannot write file '"+file_name+"' ("+str(out_file.get_error())+")")
return false
var skip_state : Array = [ false ]
while ! in_file.eof_reached():
@ -256,6 +270,7 @@ func create_file_from_template(template : String, file_name : String, export_con
return true
func export_material(prefix, profile) -> void:
export_paths[profile] = prefix
var export_context : Dictionary = {
"$(path_prefix)":prefix,
"$(file_prefix)":prefix.get_file()
@ -277,27 +292,49 @@ func export_material(prefix, profile) -> void:
export_context["$(param:"+p.name+".a)"] = str(value.a)
_:
print(p.type+" not supported in material")
if shader_model.exports[profile].has("uids"):
for i in range(shader_model.exports[profile].uids):
var uid : String
var r = []
for k in range(16):
r.append(randi() & 255)
r[6] = (r[6] & 0x0f) | 0x40
r[8] = (r[8] & 0x3f) | 0x80
for k in range(16):
uid += '%02x' % r[k]
export_context["$(uid:"+str(i)+")"] = uid
for f in shader_model.exports[profile].files:
if f.has("conditions"):
var condition = subst_string(f.conditions, export_context)
var expr = Expression.new()
var error = expr.parse(condition, [])
if error != OK:
print("Error in expression: "+expr.get_error_text())
continue
if !expr.execute():
continue
match f.type:
"texture":
var file_name = subst_string(f.file_name, export_context)
if f.has("conditions"):
var condition = subst_string(f.conditions, export_context)
var expr = Expression.new()
var error = expr.parse(condition, [])
if error != OK:
print("Error in expression: "+expr.get_error_text())
continue
if !expr.execute():
continue
var result = render(f.output, get_image_size())
while result is GDScriptFunctionState:
result = yield(result, "completed")
result.save_to_file(file_name)
result.release()
"template":
var file_name = f.file_name.replace("$(path_prefix)", prefix)
create_file_from_template(f.template, file_name, export_context)
var file_export_context = export_context.duplicate()
if f.has("file_params"):
for p in f.file_params.keys():
file_export_context["$(file_param:"+p+")"] = f.file_params[p]
var file_name = subst_string(f.file_name, export_context)
create_file_from_template(f.template, file_name, file_export_context)
func _serialize(data: Dictionary) -> Dictionary:
func _serialize_data(data: Dictionary) -> Dictionary:
data = ._serialize_data(data)
data.export_paths = export_paths
return data
func _deserialize(data : Dictionary) -> void:
._deserialize(data)
if data.has("export_paths"):
export_paths = data.export_paths.duplicate()

View File

@ -116,7 +116,7 @@
},
{
"desc":"7: unity normal",
"rgb": "$normal_tex($uv)*vec3(1.0, 1.0, -1.0)+vec3(0.0, 0.0, 1.0)",
"rgb": "$normal_tex($uv)*vec3(-1.0, 1.0, -1.0)+vec3(1.0, 0.0, 1.0)",
"type": "rgb"
},
{
@ -184,6 +184,7 @@
},
"Unity": {
"export_extension":"mat",
"uids":6,
"files": [
{
"type":"texture",
@ -191,35 +192,106 @@
"output":0,
"conditions":"$(connected:albedo_tex)"
},
{
"type":"template",
"file_name":"$(path_prefix)_albedo.png.meta",
"template":"unity.png.meta.tmpl",
"file_params": {
"uid":"$(uid:0)",
"srgb":"1",
"normal":"0"
},
"conditions":"$(connected:albedo_tex)"
},
{
"type":"texture",
"file_name":"$(path_prefix)_metal_smoothness.png",
"output":6,
"conditions":"$(connected:roughness_tex) or $(connected:metallic_tex)"
},
{
"type":"template",
"file_name":"$(path_prefix)_metal_smoothness.png.meta",
"template":"unity.png.meta.tmpl",
"file_params": {
"uid":"$(uid:1)",
"srgb":"1",
"normal":"0"
},
"conditions":"$(connected:roughness_tex) or $(connected:metallic_tex)"
},
{
"type":"texture",
"file_name":"$(path_prefix)_normal.png",
"output":7,
"conditions":"$(connected:normal_tex)"
},
{
"type":"template",
"file_name":"$(path_prefix)_normal.png.meta",
"template":"unity.png.meta.tmpl",
"file_params": {
"uid":"$(uid:2)",
"srgb":"0",
"normal":"1"
},
"conditions":"$(connected:normal_tex)"
},
{
"type":"texture",
"file_name":"$(path_prefix)_height.png",
"output":8,
"conditions":"$(connected:depth_tex)"
},
{
"type":"template",
"file_name":"$(path_prefix)_height.png.meta",
"template":"unity.png.meta.tmpl",
"file_params": {
"uid":"$(uid:3)",
"srgb":"1",
"normal":"0"
},
"conditions":"$(connected:depth_tex)"
},
{
"type":"texture",
"file_name":"$(path_prefix)_occlusion.png",
"output":9,
"conditions":"$(connected:ao_tex)"
},
{
"type":"template",
"file_name":"$(path_prefix)_occlusion.png.meta",
"template":"unity.png.meta.tmpl",
"file_params": {
"uid":"$(uid:4)",
"srgb":"1",
"normal":"0"
},
"conditions":"$(connected:ao_tex)"
},
{
"type":"texture",
"file_name":"$(path_prefix)_emission.png",
"output":2,
"conditions":"$(connected:emission_tex)"
},
{
"type":"template",
"file_name":"$(path_prefix)_emission.png.meta",
"template":"unity.png.meta.tmpl",
"file_params": {
"uid":"$(uid:5)",
"srgb":"1",
"normal":"0"
},
"conditions":"$(connected:emission_tex)"
},
{
"type":"template",
"file_name":"$(path_prefix).mat",
"template":"unity.mat.tmpl"
}
]
},

View File

@ -0,0 +1,97 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: test
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:$(expr:" _METALLICGLOSSMAP" if $(connected:roughness_tex) or $(connected:metallic_tex) else "")$(expr:" _NORMALMAP" if $(connected:normal_tex) else "")$(expr:" _PARALLAXMAP" if $(connected:depth_tex) else "")
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
$if $(connected:normal_tex)
m_Texture: {fileID: 2800000, guid: $(uid:2), type: 3}
$else
m_Texture: {fileID: 0}
$fi
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
$if $(connected:albedo_tex)
m_Texture: {fileID: 2800000, guid: $(uid:0), type: 3}
$else
m_Texture: {fileID: 0}
$fi
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
$if $(connected:roughness_tex) or $(connected:metallic_tex)
m_Texture: {fileID: 2800000, guid: $(uid:1), type: 3}
$else
m_Texture: {fileID: 0}
$fi
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
$if $(connected:ao_tex)
m_Texture: {fileID: 2800000, guid: $(uid:4), type: 3}
$else
m_Texture: {fileID: 0}
$fi
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
$if $(connected:depth_tex)
m_Texture: {fileID: 2800000, guid: $(uid:3), type: 3}
$else
m_Texture: {fileID: 0}
$fi
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

View File

@ -0,0 +1,91 @@
fileFormatVersion: 2
guid: $(file_param:uid)
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 10
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: $(file_param:srgb)
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -100
wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: $(file_param:normal)
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@ -237,13 +237,15 @@ func _on_ExportMaterial_id_pressed(id) -> void:
if material_node == null:
return
var profile = material_node.get_export_profiles()[id]
print("Exporting for "+profile)
var dialog : FileDialog = FileDialog.new()
add_child(dialog)
dialog.rect_min_size = Vector2(500, 500)
dialog.access = FileDialog.ACCESS_FILESYSTEM
dialog.mode = FileDialog.MODE_SAVE_FILE
dialog.add_filter("*."+material_node.get_export_extension(profile)+";"+profile+" Material")
var export_path = material_node.get_export_path(profile)
if export_path != "":
dialog.current_path = export_path
dialog.connect("file_selected", self, "export_material", [ profile ])
dialog.popup_centered()