例如,在 Flash (Actionscript) 中,我可以摆脱每行着色器代码 (AGAL) 中的双引号和加号,只需使用 CDATA 就可以像这样左对齐代码:
// VERTEX SHADER CODE
var vertexShader:String = <![CDATA[
// varying registers: v1, v2, v3
// temp reg: vt0
m44 vt0, va0, vc0 // transform vertices in world space with model matrix --> vt0
sub v3, vc16, vt0 // interpolate direction to light --> v3
m44 op, vt0, vc8 // append the tranformed vertices to view-projection matrix in order to transform to clip space and output position
m44 v1, va1, vc4 // transform normals with transposed & inverted modelmatrix and interpolate --> v1
mov v2, va2 // interpolate uvs --> v2
]]>;
在three.js(Javascript)中是否可以以类似的方式摆脱每行的两个引号和逗号?
var fspass1 = [
"varying vec2 vUv;",
"uniform float time;",
"void main() {",
"float r = vUv.x;",
"if( vUv.y < 0.5 ) r = 0.0;",
"float g = vUv.y;",
"if( vUv.x < 0.5 ) g = 0.0;",
"gl_FragColor = vec4( r, g, time, 1.0 );",
"}"
].join("\n")
我尝试了以下方法,但它不起作用:
var fspass1 = "<![CDATA[
varying vec2 vUv;
uniform float time;
void main() {
float r = vUv.x;
if( vUv.y < 0.5 ) r = 0.0;
float g = vUv.y;
if( vUv.x < 0.5 ) g = 0.0;
gl_FragColor = vec4( r, g, time, 1.0 );
}
]]>";
难道我做错了什么?有解决办法吗?如今,为了被框架理解,仍然必须在每行代码中使用愚蠢的双引号、逗号等,这确实令人分心、耗时且令人尴尬。它应该比这更聪明,程序应该让我们的生活更轻松,而不是更困难!