我从 android SDK 附带的 API 演示中复制了三角形渲染器,并用我自己的 raymarcher 替换了着色器。但是,我遇到了有史以来最令人麻木的故障,它似乎只会影响红色。
这是我的着色器:
private final String fs =
" precision mediump float;\n"+
"uniform float t;\n"+
"varying vec2 vtex;\n"+
"mat3 genRotMat(float a0,float x,float y,float z){\n"+
" float a=a0*3.1415926535897932384626433832795/180.0;\n"+
" return mat3(\n"+
" 1.0+(1.0-cos(a))*(x*x-1.0),\n"+
" -z*sin(a)+(1.0-cos(a))*x*y,\n"+
" y*sin(a)+(1.0-cos(a))*x*z,\n"+
" z*sin(a)+(1.0-cos(a))*x*y,\n"+
" 1.0+(1.0-cos(a))*(y*y-1.0),\n"+
" -x*sin(a)+(1.0-cos(a))*y*z,\n"+
" -y*sin(a)+(1.0-cos(a))*x*z,\n"+
" x*sin(a)+(1.0-cos(a))*y*z,\n"+
" 1.0+(1.0-cos(a))*(z*z-1.0)\n"+
" );\n"+
"}\n"+
"float cubeDist(vec3 p){\n"+
" float t0 = 0.5;\n"+
" float t1 = 0.6;\n"+
" float max = max(abs(p.x),max(abs(p.y),abs(p.z)));\n"+
" if(max > t1) return 0.0;\n"+
" else if(max > t0) return (t1-max)/(t1-t0);\n"+
" else return 1.0;\n"+
"};\n"+
"void main() {\n"+
" float spread = 1.0;\n"+
" float val=0.0;\n"+
" float delta="+delta+";\n"+
" float cameraz="+cameraz+";\n"+
" float nearz="+nearz+";\n"+
" float farz="+farz+";\n"+
" float r=0.0,g=0.0,b=0.0;"+
" vec3 ray=vec3(0.0,0.0,0.0);\n"+
" mat3 rot=genRotMat(sin(t/3.13)*360.0,1.0,0.0,0.0);\n"+
" rot=rot*genRotMat(sin(t/3.64)*360.0,0.0,1.0,0.0);\n"+
" rot=rot*genRotMat(sin(t/3.24)*360.0,0.0,0.0,1.0);\n"+
" ray.z=nearz;\n"+
" ray.xy+=vtex.xy*spread*(nearz-cameraz);\n"+
" for(int i=0;i<"+iterations+";i++){\n"+
" vec3 temp;\n"+
" ray.xy+=vtex.xy*spread*delta;\n"+
" temp=ray*rot;\n"+
" val+=cubeDist(temp);\n"+
" r+=abs(temp.x*delta*delta*val);\n"+
" g+=abs(temp.y*delta*delta*val);\n"+
" b+=abs(temp.z*delta*delta*val);\n"+
" ray.z+=delta;\n"+
" }\n"+
" gl_FragColor=vec4(r,g,b,1.0);\n"+
"}\n";
这是故障的图像:http: //imgur.com/2zcps.png
整个文件在这里: http: //pastebin.com/JF7rnSdE