static IntPtr OnNewTextureEvaluator(int serialNumber) { IntPtr rc = IntPtr.Zero; try { RenderTexture texture = FromSerialNumber(serialNumber) as RenderTexture; if (texture != null) { TextureEvaluator eval = texture.CreateEvaluator(); if (eval != null) { rc = eval.NonConstPointer(); } } } catch { rc = IntPtr.Zero; } return(rc); }
TextureInfo CombineBaseColorAndAlphaTexture(Rhino.Render.RenderTexture baseColorTexture, Rhino.Render.RenderTexture alphaTexture, bool baseColorDiffuseAlphaForTransparency, Color4f baseColor, bool baseColorLinear) { bool hasBaseColorTexture = baseColorTexture != null; int baseColorWidth, baseColorHeight, baseColorDepth; baseColorWidth = baseColorHeight = baseColorDepth = 0; if (hasBaseColorTexture) { baseColorTexture.PixelSize(out baseColorWidth, out baseColorHeight, out baseColorDepth); } alphaTexture.PixelSize(out int alphaWidth, out int alphaHeight, out int alphaDepth); int width = Math.Max(baseColorWidth, alphaWidth); int height = Math.Max(baseColorHeight, alphaHeight); if (width <= 0) { width = 1024; } if (height <= 0) { height = 1024; } TextureEvaluator baseColorTextureEvaluator = baseColorTexture.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal); TextureEvaluator alphaTextureEvaluator = alphaTexture.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal); Bitmap bitmap = new Bitmap(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { double x = (double)i / ((double)(width - 1)); double y = (double)j / ((double)(height - 1)); y = 1.0 - y; Point3d uvw = new Point3d(x, y, 0.0); Color4f baseColorOut = baseColor; if (hasBaseColorTexture) { baseColorOut = baseColorTextureEvaluator.GetColor(uvw, Vector3d.Zero, Vector3d.Zero); if (baseColorLinear) { baseColorOut = Color4f.ApplyGamma(baseColorOut, workflow.PreProcessGamma); } } if (!baseColorDiffuseAlphaForTransparency) { baseColorOut = new Color4f(baseColorOut.R, baseColorOut.G, baseColorOut.B, 1.0f); } Color4f alphaColor = alphaTextureEvaluator.GetColor(uvw, Vector3d.Zero, Vector3d.Zero); float alpha = baseColor.A * alphaColor.L; Color4f colorFinal = new Color4f(baseColorOut.R, baseColorOut.G, baseColorOut.B, alpha); bitmap.SetPixel(i, j, colorFinal.AsSystemColor()); } } bitmap.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "out.png")); return(GetTextureInfoFromBitmap(bitmap)); }
/// <summary> /// Get material bitmap from texture evaluator /// </summary> /// <param name="shader"></param> /// <param name="rm"></param> /// <param name="renderTexture"></param> /// <param name="textureType"></param> internal static void MaterialBitmapFromEvaluator(ref CyclesShader shader, RenderMaterial rm, RenderTexture renderTexture, RenderMaterial.StandardChildSlots textureType) { if (renderTexture == null) return; var rId = renderTexture.RenderHashWithoutLocalMapping; var rhinotfm = renderTexture.LocalMappingTransform; var projectionMode = renderTexture.GetProjectionMode(); var envProjectionMode = renderTexture.GetInternalEnvironmentMappingMode(); using (var textureEvaluator = renderTexture.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.DisableLocalMapping)) { SimulatedTexture st = textureEvaluator == null ? renderTexture.SimulatedTexture(RenderTexture.TextureGeneration.Disallow) : null; using ( var actualEvaluator = textureEvaluator ?? RenderTexture.NewBitmapTexture(st).CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal)) { InternalMaterialBitmapFromEvaluator(shader, renderTexture, textureType, rhinotfm, rId, actualEvaluator, projectionMode, envProjectionMode); } } }