使用 rgl 0.93.935 R 包进行 WebGL 渲染 [英] WebGL rendering with rgl 0.93.935 R package

查看:23
本文介绍了使用 rgl 0.93.935 R 包进行 WebGL 渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 R 代码生成一个 HTML 文件并在浏览器中打开它:

The following R code generates an HTML file and opens it in the browser:

library(rgl)
M <- rbind(
  c(0,0,0),
  c(-1,4,0),
  c(4,9,0),
  c(6,3,0)
  )
  quads3d(M,col='red')
browseURL(paste("file://", writeWebGL(dir=file.path(tempdir(), "webGL"), 
          width=500), sep=""))

渲染是 3D 空间中的交互式平面多面体.

The rendering is an interactive planar polyhedron in the 3D space.

使用最新版本的 rgl 包 (0.93.935),HTML 呈现对于 Windows 用户不起作用(以及作为 iOS 用户,我认为)使用默认配置浏览器.使用旧版本 0.93.928,它可以工作.

With the latest version of the rgl package (0.93.935), the HTML rendering does not work for Windows users (as well as iOS users, I think) with default configuration browser. With the older version 0.93.928, it works.

我已经发布了 rgl 0.93.928 的 html 输出rgl 0.93.935 的 html 输出.

I have posted the html output of rgl 0.93.928 and the html output of rgl 0.93.935.

我已将此问题报告给 Duncan Murdoch(rgl 的作者),他为我提供了以下 Firefox 解决方案:键入并运行 "about:config"在地址栏中,并将参数webgl.prefer-native-glwebgl.force-enabled改为true.然后 HTML 渲染工作.

I have reported this issue to Duncan Murdoch (author of rgl) and he has given me the following solution for Firefox: type and run "about: config" in the address bar, and turn the parameters webgl.prefer-native-gl and webgl.force-enabled to true. Then the HTML rendering works.

我的问题:

  • 如何使用谷歌浏览器?

  • How to do with Google Chrome?

是否可以更改 HTML 代码中的某些内容,以便 HTML 呈现使用默认配置?(至于 0.93.928 版本).

Is it possible to change something in the HTML code in order that the HTML rendering works with the default configuration? (as for the 0.93.928 version).

推荐答案

解决问题与解决问题一样简单.

As difficult as the getting to the problem was as easy is the solution to it.

从最新版本的 rgl 开始,我发现问题出在 html 输出的片段着色器中:

As of latest version of rgl I could acquire the problem resides inside the fragment shader of the html output:

varying vec4 vCol; // carries alpha
varying vec4 vPosition;
varying vec3 vNormal;

vec3 eye = normalize(-vPosition.xyz);
const vec3 emission = vec3(0., 0., 0.);
const vec3 ambient1 = vec3(0., 0., 0.);
const vec3 specular1 = vec3(1., 1., 1.);// light*material
const float shininess1 = 50.;
vec4 colDiff1 = vec4(vCol.rgb * vec3(1., 1., 1.), vCol.a);
const vec3 lightDir1 = vec3(0., 0., 1.);
vec3 halfVec1 = normalize(lightDir1 + eye);

void main(void) {
    vec4 lighteffect = vec4(emission, 0.);
    vec3 n = normalize(vNormal);
    n = -faceforward(n, n, eye);
    vec3 col1 = ambient1;
    float nDotL1 = dot(n, lightDir1);
    col1 = col1 + max(nDotL1, 0.) * colDiff1.rgb;
    col1 = col1 + pow(max(dot(halfVec1, n), 0.), shininess1) * specular1;
    lighteffect = lighteffect + vec4(col1, colDiff1.a);
    gl_FragColor = lighteffect;
}

它定义了 main 函数之外的变量,该函数跳过了赋值,因此无法编译并出现除以零失败的情况.解决方案是将主函数的开头直接移动到定义块上方的变化值之后:

it defines variables outside on the main function which skips value assignments and thus fails to compile with division-by-zero failures. The solution is to move the start of the main function above the definition block directly after the varying values:

varying vec4 vCol; // carries alpha
varying vec4 vPosition;
varying vec3 vNormal;

void main(void) {
    vec3 eye = normalize(-vPosition.xyz);
    const vec3 emission = vec3(0., 0., 0.);
    const vec3 ambient1 = vec3(0., 0., 0.);
    const vec3 specular1 = vec3(1., 1., 1.);// light*material
    const float shininess1 = 50.;
    vec4 colDiff1 = vec4(vCol.rgb * vec3(1., 1., 1.), vCol.a);
    const vec3 lightDir1 = vec3(0., 0., 1.);
    vec3 halfVec1 = normalize(lightDir1 + eye);

    vec4 lighteffect = vec4(emission, 0.);
    vec3 n = normalize(vNormal);
    n = -faceforward(n, n, eye);
    vec3 col1 = ambient1;
    float nDotL1 = dot(n, lightDir1);
    col1 = col1 + max(nDotL1, 0.) * colDiff1.rgb;
    col1 = col1 + pow(max(dot(halfVec1, n), 0.), shininess1) * specular1;
    lighteffect = lighteffect + vec4(col1, colDiff1.a);
    gl_FragColor = lighteffect;
}

这将根据需要显示对象.

This will show the object as desired.

如果您说您不精通 html 和 webgl,您可能想再次联系 Duncan Murdoch 并向他发送此帖子的链接.

You may want to contact Duncan Murdoch again and send him a link to this post if as you said you're not versed in html and webgl.

这篇关于使用 rgl 0.93.935 R 包进行 WebGL 渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆