private glTFLoader.Schema.MaterialNormalTextureInfo AddTextureNormal(Rhino.DocObjects.Texture normalTexture) { Rhino.Render.RenderTexture normalRenderTexture = rhinoMaterial.RenderMaterial.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Bump); int textureIdx = AddNormalTexture(normalTexture, normalRenderTexture); double scale = rhinoMaterial.RenderMaterial.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Bump) / 100.0; return(new glTFLoader.Schema.MaterialNormalTextureInfo() { Index = textureIdx, TexCoord = 0, Scale = (float)scale, }); }
internal CreateTexture2dPreviewEventArgs(RenderTexture texture, System.Drawing.Size size) { m_preview_size = size; m_render_texture = texture; }
private static void InternalMaterialBitmapFromEvaluator(CyclesShader shader, RenderTexture renderTexture, RenderMaterial.StandardChildSlots textureType, Rhino.Geometry.Transform rhinotfm, uint rId, TextureEvaluator actualEvaluator, TextureProjectionMode projectionMode, TextureEnvironmentMappingMode envProjectionMode) { int pheight; int pwidth; try { int u, v, w; renderTexture.PixelSize(out u, out v, out w); pheight = u; pwidth = v; } catch (Exception) { pheight = 1024; pwidth = 1024; } if (pheight == 0 || pwidth == 0) { pheight = 1024; pwidth = 1024; } Transform t = new Transform( rhinotfm.ToFloatArray(true) ); var isFloat = renderTexture.IsHdrCapable(); var isLinear = renderTexture.IsLinear(); if (isFloat) { var img = RetrieveFloatsImg(rId, pwidth, pheight, actualEvaluator, false, false, isLinear); img.ApplyGamma(shader.Gamma); switch (textureType) { case RenderMaterial.StandardChildSlots.Diffuse: shader.DiffuseTexture.IsLinear = isLinear; shader.DiffuseTexture.TexFloat = img.Data; shader.DiffuseTexture.TexByte = null; break; case RenderMaterial.StandardChildSlots.Bump: shader.BumpTexture.IsLinear = isLinear; shader.BumpTexture.TexFloat = img.Data; shader.BumpTexture.TexByte = null; break; case RenderMaterial.StandardChildSlots.Transparency: shader.TransparencyTexture.IsLinear = isLinear; shader.TransparencyTexture.TexFloat = img.Data; shader.TransparencyTexture.TexByte = null; break; case RenderMaterial.StandardChildSlots.Environment: shader.EnvironmentTexture.IsLinear = isLinear; shader.EnvironmentTexture.TexFloat = img.Data; shader.EnvironmentTexture.TexByte = null; break; } } else { var img = RetrieveBytesImg(rId, pwidth, pheight, actualEvaluator, false, false, isLinear); img.ApplyGamma(shader.Gamma); switch (textureType) { case RenderMaterial.StandardChildSlots.Diffuse: shader.DiffuseTexture.IsLinear = isLinear; shader.DiffuseTexture.TexFloat = null; shader.DiffuseTexture.TexByte = img.Data; break; case RenderMaterial.StandardChildSlots.Bump: shader.BumpTexture.IsLinear = isLinear; shader.BumpTexture.TexFloat = null; shader.BumpTexture.TexByte = img.Data; break; case RenderMaterial.StandardChildSlots.Transparency: shader.TransparencyTexture.IsLinear = isLinear; shader.TransparencyTexture.TexFloat = null; shader.TransparencyTexture.TexByte = img.Data; break; case RenderMaterial.StandardChildSlots.Environment: shader.EnvironmentTexture.IsLinear = isLinear; shader.EnvironmentTexture.TexFloat = null; shader.EnvironmentTexture.TexByte = img.Data; break; } } switch (textureType) { case RenderMaterial.StandardChildSlots.Diffuse: shader.DiffuseTexture.TexWidth = pwidth; shader.DiffuseTexture.TexHeight = pheight; shader.DiffuseTexture.ProjectionMode = projectionMode; shader.DiffuseTexture.EnvProjectionMode = envProjectionMode; shader.DiffuseTexture.Transform = t; shader.DiffuseTexture.Name = rId.ToString(CultureInfo.InvariantCulture); break; case RenderMaterial.StandardChildSlots.Bump: shader.BumpTexture.TexWidth = pwidth; shader.BumpTexture.TexHeight = pheight; shader.BumpTexture.ProjectionMode = projectionMode; shader.BumpTexture.EnvProjectionMode = envProjectionMode; shader.BumpTexture.Transform = t; shader.BumpTexture.Name = rId.ToString(CultureInfo.InvariantCulture); break; case RenderMaterial.StandardChildSlots.Transparency: shader.TransparencyTexture.TexWidth = pwidth; shader.TransparencyTexture.TexHeight = pheight; shader.TransparencyTexture.ProjectionMode = projectionMode; shader.TransparencyTexture.EnvProjectionMode = envProjectionMode; shader.TransparencyTexture.Transform = t; shader.TransparencyTexture.Name = rId.ToString(CultureInfo.InvariantCulture); break; case RenderMaterial.StandardChildSlots.Environment: shader.EnvironmentTexture.TexWidth = pwidth; shader.EnvironmentTexture.TexHeight = pheight; // special texture, always set to Environment/Emap shader.EnvironmentTexture.ProjectionMode = TextureProjectionMode.EnvironmentMap; shader.EnvironmentTexture.EnvProjectionMode = TextureEnvironmentMappingMode.EnvironmentMap; shader.EnvironmentTexture.Transform = t; shader.EnvironmentTexture.Name = rId.ToString(CultureInfo.InvariantCulture); break; } }
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); } } }