Skip to content

Commit 2b66c55

Browse files
committed
Add coating to maya
1 parent 54e2368 commit 2b66c55

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

Maya/Exporter/BabylonExporter.Material.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,61 @@ private void ExportMaterial(MFnDependencyNode materialDependencyNode, BabylonSce
383383
float emissionWeight = materialDependencyNode.findPlug("emission").asFloat();
384384
babylonMaterial.emissive = materialDependencyNode.findPlug("emissionColor").asFloatArray().Multiply(emissionWeight);
385385

386+
387+
var list = new List<string>();
388+
389+
for (int i = 0; i < materialDependencyNode.attributeCount; i++)
390+
{
391+
var attr = materialDependencyNode.attribute((uint)i);
392+
var plug = materialDependencyNode.findPlug(attr);
393+
//string aliasName;
394+
//materialDependencyNode.getPlugsAlias(plug, out aliasName);
395+
System.Diagnostics.Debug.WriteLine(plug.name + i.ToString());
396+
}
397+
398+
// --- Clear Coat ---
399+
float coatWeight = materialDependencyNode.findPlug("coat").asFloat();
400+
MFnDependencyNode intensityCoatTextureDependencyNode = getTextureDependencyNode(materialDependencyNode, "coat");
401+
if (coatWeight > 0.0f || intensityCoatTextureDependencyNode != null)
402+
{
403+
babylonMaterial.clearCoat.isEnabled = true;
404+
babylonMaterial.clearCoat.indexOfRefraction = materialDependencyNode.findPlug("coatIOR").asFloat();
405+
406+
var coatRoughness = materialDependencyNode.findPlug("coatRoughness").asFloat();
407+
MFnDependencyNode roughnessCoatTextureDependencyNode = getTextureDependencyNode(materialDependencyNode, "coatRoughness");
408+
var coatTexture = ExportCoatTexture(intensityCoatTextureDependencyNode, roughnessCoatTextureDependencyNode, babylonScene, name, coatWeight, coatRoughness);
409+
if (coatTexture != null)
410+
{
411+
babylonMaterial.clearCoat.texture = coatTexture;
412+
babylonMaterial.clearCoat.roughness = 1.0f;
413+
babylonMaterial.clearCoat.intensity = 1.0f;
414+
}
415+
else
416+
{
417+
babylonMaterial.clearCoat.intensity = coatWeight;
418+
babylonMaterial.clearCoat.roughness = coatRoughness;
419+
}
420+
421+
float[] coatColor = materialDependencyNode.findPlug("coatColor").asFloatArray();
422+
if (coatColor[0] != 1.0f || coatColor[1] != 1.0f || coatColor[2] != 1.0f)
423+
{
424+
babylonMaterial.clearCoat.isTintEnabled = true;
425+
babylonMaterial.clearCoat.tintColor = coatColor;
426+
}
427+
428+
babylonMaterial.clearCoat.tintTexture = ExportTexture(materialDependencyNode, "coatColor", babylonScene);
429+
if (babylonMaterial.clearCoat.tintTexture != null)
430+
{
431+
babylonMaterial.clearCoat.tintColor = new[] { 1.0f, 1.0f, 1.0f };
432+
babylonMaterial.clearCoat.isTintEnabled = true;
433+
}
434+
435+
// EyeBall deduction...
436+
babylonMaterial.clearCoat.tintThickness = 0.65f;
437+
438+
babylonMaterial.clearCoat.bumpTexture = ExportTexture(materialDependencyNode, "coatNormal", babylonScene);
439+
}
440+
386441
// --- Textures ---
387442

388443
// Base color & alpha

Maya/Exporter/BabylonExporter.Texture.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,84 @@ private BabylonTexture ExportORMTexture(BabylonScene babylonScene, MFnDependency
329329
return babylonTexture;
330330
}
331331

332+
private BabylonTexture ExportCoatTexture(MFnDependencyNode intensityTextureDependencyNode, MFnDependencyNode roughnessTextureDependencyNode, BabylonScene babylonScene, string materialName, float intensity, float roughness)
333+
{
334+
// Prints
335+
if (intensityTextureDependencyNode != null)
336+
{
337+
Print(intensityTextureDependencyNode, logRankTexture, "Print ExportCoatTexture intensityTextureDependencyNode");
338+
}
339+
if (roughnessTextureDependencyNode != null)
340+
{
341+
Print(roughnessTextureDependencyNode, logRankTexture, "Print ExportCoatTexture roughnessTextureDependencyNode");
342+
}
343+
344+
// Use one as a reference for UVs parameters
345+
var textureDependencyNode = intensityTextureDependencyNode != null ? intensityTextureDependencyNode : roughnessTextureDependencyNode;
346+
if (textureDependencyNode == null)
347+
{
348+
return null;
349+
}
350+
351+
var id = textureDependencyNode.uuid().asString();
352+
var babylonTexture = new BabylonTexture(id)
353+
{
354+
name = materialName + "_coat" + ".jpg" // TODO - unsafe name, may conflict with another texture name
355+
};
356+
357+
// Level
358+
babylonTexture.level = 1.0f;
359+
360+
// Alpha
361+
babylonTexture.hasAlpha = false;
362+
babylonTexture.getAlphaFromRGB = false;
363+
364+
// UVs
365+
_exportUV(textureDependencyNode, babylonTexture);
366+
367+
// Is cube
368+
string sourcePath = getSourcePathFromFileTexture(textureDependencyNode);
369+
if (sourcePath == null)
370+
{
371+
return null;
372+
}
373+
if (sourcePath == "")
374+
{
375+
RaiseError("Texture path is missing.", logRankTexture + 1);
376+
return null;
377+
}
378+
_exportIsCube(sourcePath, babylonTexture, false);
379+
380+
381+
// --- Merge base color and opacity maps ---
382+
383+
if (CopyTexturesToOutput)
384+
{
385+
// Load bitmaps
386+
var intensityBitmap = LoadTexture(intensityTextureDependencyNode);
387+
var roughnessBitmap = LoadTexture(roughnessTextureDependencyNode);
388+
389+
// Merge bitmaps
390+
Bitmap[] bitmaps = new Bitmap[] { intensityBitmap, roughnessBitmap, null, null };
391+
int[] defaultValues = new int[] { (int)(intensity * 255), (int)(roughness * 255), 0, 1 };
392+
Bitmap coatBitmap = MergeBitmaps(bitmaps, defaultValues, "Coat intensity and roughness");
393+
394+
// Write bitmap
395+
if (isBabylonExported)
396+
{
397+
RaiseMessage($"Texture | write image '{babylonTexture.name}'", logRankTexture + 1);
398+
SaveBitmap(coatBitmap, babylonScene.OutputPath, babylonTexture.name, System.Drawing.Imaging.ImageFormat.Jpeg);
399+
}
400+
else
401+
{
402+
// Store created bitmap for further use in gltf export
403+
babylonTexture.bitmap = coatBitmap;
404+
}
405+
}
406+
407+
return babylonTexture;
408+
}
409+
332410
private BabylonTexture ExportBaseColorAlphaTexture(MFnDependencyNode baseColorTextureDependencyNode, MFnDependencyNode opacityTextureDependencyNode, BabylonScene babylonScene, string materialName, Color defaultBaseColor, float defaultOpacity = 1.0f)
333411
{
334412
// Prints

SharedProjects/BabylonExport.Entities/BabylonPBRClearCoat.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.Serialization;
2+
23
namespace BabylonExport.Entities
34
{
45
[DataContract]

0 commit comments

Comments
 (0)