private static void AddTextureToMaterial(FDatasmithFacadeUEPbrMaterial DSMaterial, RhinoTextureInfo TextureInfo, Color DiffuseColor) { if (!TextureInfo.IsSupported()) { return; } Texture RhinoTexture = TextureInfo.RhinoTexture; // Extract texture mapping info double BlendConstant, BlendA0, BlendA1, BlendA2, BlendA3; RhinoTexture.GetAlphaBlendValues(out BlendConstant, out BlendA0, out BlendA1, out BlendA2, out BlendA3); FDatasmithFacadeMaterialsUtils.FWeightedMaterialExpressionParameters WeightedExpressionParameters = new FDatasmithFacadeMaterialsUtils.FWeightedMaterialExpressionParameters((float)BlendConstant); FDatasmithFacadeMaterialsUtils.FUVEditParameters UVParameters = GetUVParameter(RhinoTexture); switch (RhinoTexture.TextureType) { case TextureType.Bitmap: { FDatasmithFacadeMaterialExpression TextureExpression = FDatasmithFacadeMaterialsUtils.CreateTextureExpression(DSMaterial, "Diffuse Map", TextureInfo.Name, UVParameters); WeightedExpressionParameters.SetColorsRGB(DiffuseColor.R, DiffuseColor.G, DiffuseColor.B, DiffuseColor.A); WeightedExpressionParameters.SetExpression(TextureExpression); FDatasmithFacadeMaterialExpression Expression = FDatasmithFacadeMaterialsUtils.CreateWeightedMaterialExpression(DSMaterial, "Diffuse Color", WeightedExpressionParameters); DSMaterial.GetBaseColor().SetExpression(Expression); } break; case TextureType.Bump: { FDatasmithFacadeMaterialExpression TextureExpression = FDatasmithFacadeMaterialsUtils.CreateTextureExpression(DSMaterial, "Bump Map", TextureInfo.Name, UVParameters); WeightedExpressionParameters.SetExpression(TextureExpression); WeightedExpressionParameters.SetTextureMode(FDatasmithFacadeTexture.ETextureMode.Bump); FDatasmithFacadeMaterialExpression Expression = FDatasmithFacadeMaterialsUtils.CreateWeightedMaterialExpression(DSMaterial, "Bump Height", WeightedExpressionParameters); DSMaterial.GetNormal().SetExpression(Expression); } break; case TextureType.Transparency: { FDatasmithFacadeMaterialExpression TextureExpression = FDatasmithFacadeMaterialsUtils.CreateTextureExpression(DSMaterial, "Opacity Map", TextureInfo.Name, UVParameters); Color BlendColor = Color.White; WeightedExpressionParameters.SetColorsRGB(BlendColor.R, BlendColor.G, BlendColor.B, BlendColor.A); WeightedExpressionParameters.SetExpression(TextureExpression); FDatasmithFacadeMaterialExpression Expression = FDatasmithFacadeMaterialsUtils.CreateWeightedMaterialExpression(DSMaterial, "White", WeightedExpressionParameters); DSMaterial.GetOpacity().SetExpression(Expression); if (Math.Abs(BlendConstant) > float.Epsilon) { DSMaterial.SetBlendMode(/*EBlendMode::BLEND_Translucent*/ 2); } } break; } }
/// <summary> /// Returns the UV Parameters for the given Texture. /// This function uses the same operations as in FOpenNurbsTranslatorImpl::TranslateMaterialTable(), improvements should be applied to both functions. /// </summary> /// <param name="RhinoTexture"></param> /// <returns></returns> private static FDatasmithFacadeMaterialsUtils.FUVEditParameters GetUVParameter(Texture RhinoTexture) { // Extract texture mapping info FDatasmithFacadeMaterialsUtils.FUVEditParameters UVParameters = new FDatasmithFacadeMaterialsUtils.FUVEditParameters(); // Use cached texture coordinates(channel 0) UVParameters.SetChannelIndex(0); //// Extract the UV tiling, offset and rotation angle from the UV transform matrix Transform RotationTransform, OrthogonalTransform; Vector3d Translation, Scale; RhinoTexture.UvwTransform.DecomposeAffine(out Translation, out RotationTransform, out OrthogonalTransform, out Scale); double RotX, RotY, RotZ; if (!RotationTransform.GetYawPitchRoll(out RotX, out RotY, out RotZ)) { //This is not a valid rotation make sure the angles are at 0; RotX = RotY = RotZ = 0; } else { RotX = FDatasmithRhinoUtilities.RadianToDegree(RotX); RotY = FDatasmithRhinoUtilities.RadianToDegree(RotY); RotZ = FDatasmithRhinoUtilities.RadianToDegree(RotZ); } UVParameters.SetUVTiling((float)Scale.X, (float)Scale.Y); //If the tiling vector is not zero. if (Math.Abs(Scale.X) > float.Epsilon && Math.Abs(Scale.Y) > float.Epsilon) { float UVOffsetX = (float)(Translation.X / Scale.X); float UVOffsetY = (float)-(Translation.Y / Scale.Y + 0.5f - 0.5f / Scale.Y); UVParameters.SetUVOffset(UVOffsetX, UVOffsetY); // V-coordinate is inverted in Unreal } // Rotation angle is reversed because V-axis points down in Unreal while it points up in OpenNurbs UVParameters.SetRotationAngle((float)-RotX); return(UVParameters); }
public static FDatasmithFacadeMaterialExpressionTexture CreateTextureExpression(FDatasmithFacadeUEPbrMaterial MaterialElement, string ParameterName, string TextureMapPath, FDatasmithFacadeMaterialsUtils.FUVEditParameters UVParameters) { global::System.IntPtr objectPtr = DatasmithFacadeCSharpPINVOKE.FDatasmithFacadeMaterialsUtils_CreateTextureExpression(FDatasmithFacadeUEPbrMaterial.getCPtr(MaterialElement), ParameterName, TextureMapPath, FDatasmithFacadeMaterialsUtils.FUVEditParameters.getCPtr(UVParameters)); if (DatasmithFacadeCSharpPINVOKE.SWIGPendingException.Pending) { throw DatasmithFacadeCSharpPINVOKE.SWIGPendingException.Retrieve(); } if (objectPtr == global::System.IntPtr.Zero) { return(null); } else { return(new FDatasmithFacadeMaterialExpressionTexture(objectPtr, true)); } }