You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R包rayshader的render_highquality导出全黑PNG问题求助

Rayshader render_highquality导出全黑PNG问题(M1 Mac环境)

问题描述

按照教程操作,调用render_highquality前的所有步骤均生成预期输出,render_snapshot和save_obj函数运行正常,但render_highquality导出的PNG为全黑图像。已尝试从GitHub卸载并重装rayshader包,运行环境为M1 Mac mini,Mac OS Ventura 13.0.1。

复现代码

library(rayshader)
library(MetBrewer)
library(colorspace)
library(sf)
library(raster)
library(ggplot2)
library(stars)

data <- st_read("data/kontur_population_MX_20220630.gpkg")
ags <- getData("GADM", country = "Mexico", level = 1) %>% 
  st_as_sf() %>%
  filter(NAME_1 == "Aguascalientes") |> 
  st_transform(crs= st_crs(data))

st_ags <- st_intersection(data, ags)

bb <- st_bbox(st_ags)

bottom_left <- st_point(c(bb[["xmin"]], bb[["ymin"]])) |> 
  st_sfc(crs = st_crs(data))

bottom_right <- st_point(c(bb[["xmax"]], bb[["ymin"]])) |> 
  st_sfc(crs = st_crs(data))

width <- st_distance(bottom_left, bottom_right)

top_left <- st_point(c(bb[["xmin"]], bb[["ymax"]])) |> 
  st_sfc(crs = st_crs(data))

height <- st_distance(bottom_left, top_left)

if (width > height) {
  w_ratio <- 1
  h_ratio <- height / width
} else {
  h_ratio <- 1
  w_ratio <- width / height
}

size <- 1000

ags_rast <- st_rasterize(st_ags, 
                     nx = floor(size * w_ratio),
                     ny = floor(size * h_ratio))

mat <- matrix(ags_rast$population, 
          nrow = floor(size * w_ratio),
          ncol = floor(size * h_ratio))


c1 <- met.brewer("OKeeffe2")
swatchplot(c1)

texture <- grDevices::colorRampPalette(c1, bias = 2)(256)
swatchplot(texture)

mat |> 
  height_shade(texture = texture) |> 
  plot_3d(heightmap = mat,
          zscale = 100,
          solid = FALSE,
          shadowdepth = 0)

png_outfile <- 'imgs/ags_model.png'

render_camera(theta = -20, phi = 45, zoom = .8)

render_highquality(
  filename = png_outfile
)

可能的原因及解决方案

1. 光照参数未显式配置

render_highquality默认的光照设置在M1 Mac的图形环境下可能未生效,尝试显式添加光照参数:

render_highquality(
  filename = png_outfile,
  lightdirection = 45,  # 光源方向
  lightintensity = 120, # 光源强度
  ambient_intensity = 0.6, # 环境光强度
  shadow_intensity = 0.3 # 阴影强度
)

2. 输出尺寸不匹配

显式指定输出图像的宽高,确保和之前生成的栅格尺寸一致:

render_highquality(
  filename = png_outfile,
  width = floor(size * w_ratio),
  height = floor(size * h_ratio)
)

3. 高度图矩阵方向错误

检查矩阵的行列顺序,尝试转置矩阵后再生成3D图:

# 修改mat的生成代码
mat <- t(matrix(ags_rast$population, 
          nrow = floor(size * w_ratio),
          ncol = floor(size * h_ratio)))

4. M1 Mac图形环境兼容性问题

  • 确保安装了最新版本的XQuartz(M1 Mac运行部分图形工具需要依赖XQuartz);
  • 在调用plot_3d时添加gl = TRUE参数,启用硬件加速:
mat |> 
  height_shade(texture = texture) |> 
  plot_3d(heightmap = mat,
          zscale = 100,
          solid = FALSE,
          shadowdepth = 0,
          gl = TRUE)

5. 预览后再导出

使用preview = TRUE参数先预览渲染效果,确认显示正常后再导出:

render_highquality(
  filename = png_outfile,
  preview = TRUE
)

内容的提问来源于stack exchange,提问作者Diego Plata

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 21:35:27