英文:
How can I fix a blank output in aframe when converting a plane to a sphere using a custom shader with glsl script in three.js?
问题
我无法理解为什么aframe自定义着色器在顶点着色器中将平面转换为球体时输出空白。在three.js中,这个glsl脚本可以输出一个球体而没有任何问题,但在aframe中不起作用。感谢任何帮助。
链接:https://jsfiddle.net/AshVRAR/gya5x83e/34/
AFRAME.registerShader("customShader", {
schema: {
color: { type: "color", is: "uniform", default: "red" },
opacity: { type: "number", is: "uniform", default: 1.0 },
},
vertexShader: `
precision mediump float;
void main() {
float PI = 3.14159265359;
// 将position.xy从[-1,1]转换为[0,1]
vec2 uv = position.xy * 0.5 + 0.5;
// 将uv转换为球坐标
float theta = 2.0 * PI * uv.x; // 方位角,[0, 2PI]
float phi = PI * uv.y; // 极角,[0, PI]
// 将球坐标转换为笛卡尔坐标
vec3 newPosition = vec3(0.0);
newPosition.x = sin(phi) * cos(theta);
newPosition.y = cos(phi);
newPosition.z = sin(phi) * sin(theta);
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0 );
/* gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0 ) */;
}
`,
};
英文:
I can't figure out why the aframe custom shader outputs a blank in this plane to sphere conversion in vertex shader. The glsl script outputs an sphere without any issue in three.js but it doesn't work in aframe.
Appreciate any help.
https://jsfiddle.net/AshVRAR/gya5x83e/34/
AFRAME.registerShader("customShader", {
schema: {
color: { type: "color", is: "uniform", default: "red" },
opacity: { type: "number", is: "uniform", default: 1.0 },
},
vertexShader: `
precision mediump float;
void main() {
float PI = 3.14159265359;
// convert position.xy from [-1,1] to [0,1]
vec2 uv = position.xy * 0.5 + 0.5;
// convert uv to spherical coordinates
float theta = 2.0 * PI * uv.x; // azimuthal angle, [0, 2PI]
float phi = PI * uv.y; // polar angle, [0, PI]
// convert spherical coordinates to cartesian coordinates
vec3 newPosition = vec3(0.0);
newPosition.x = sin(phi) * cos(theta);
newPosition.y = cos(phi);
newPosition.z = sin(phi) * sin(theta);
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0 );
/* gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0 ) */;
}
`,
答案1
得分: 0
以下是您要翻译的代码部分:
Four vertices (base `a-plane`) are a bit too little for the algorithm in the vertex shader - hence it changes the position of four points.
If you add up some more segments, its working as intended:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-html -->
<script src="https://aframe.io/releases/1.4.1/aframe.min.js"></script>
<script>
AFRAME.registerShader("customShader", {
schema: {
color: {type: "color", is: "uniform", default: "red"},
opacity: {type: "number", is: "uniform", default: 1.0},
},
vertexShader: `
void main() {
float PI = 3.14159265359;
// convert uv to spherical coordinates
float theta = 2.0 * PI * uv.x; // azimuthal angle, [0, 2PI]
float phi = PI * uv.y; // polar angle, [0, PI]
// convert spherical coordinates to cartesian coordinates
vec3 newPosition = position;
newPosition.x = sin(phi) * cos(theta);
newPosition.y = cos(phi);
newPosition.z = sin(phi) * sin(theta);
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0 );
}
`,
fragmentShader: `
precision mediump float;
uniform vec3 color;
uniform float opacity;
void main() {
gl_FragColor = vec4(color, 1.0);
}
`,
});
</script>
<a-scene background="color: black">
<a-camera position="0 0 3"></a-camera>
<a-plane
segments-height="15"
segments-width="15"
position="0 0 -2"
material="shader: customShader; color: green" />
<a-sky color="#ECECEC"></a-sky>
</a-scene>
<!-- end snippet -->
如果您需要进一步的帮助,请随时告诉我。
英文:
Four vertices (base a-plane
) are a bit too little for the algorithm in the vertex shader - hence it changes the position of four points.
If you add up some more segments, its working as intended:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-html -->
<script src="https://aframe.io/releases/1.4.1/aframe.min.js"></script>
<script>
AFRAME.registerShader("customShader", {
schema: {
color: {type: "color", is: "uniform", default: "red"},
opacity: {type: "number", is: "uniform", default: 1.0},
},
vertexShader: `
void main() {
float PI = 3.14159265359;
// convert uv to spherical coordinates
float theta = 2.0 * PI * uv.x; // azimuthal angle, [0, 2PI]
float phi = PI * uv.y; // polar angle, [0, PI]
// convert spherical coordinates to cartesian coordinates
vec3 newPosition = position;
newPosition.x = sin(phi) * cos(theta);
newPosition.y = cos(phi);
newPosition.z = sin(phi) * sin(theta);
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0 );
}
`,
fragmentShader: `
precision mediump float;
uniform vec3 color;
uniform float opacity;
void main() {
gl_FragColor = vec4(color, 1.0);
}
`,
});
</script>
<a-scene background="color: black">
<a-camera position="0 0 3"></a-camera>
<a-plane
segments-height="15"
segments-width="15"
position="0 0 -2"
material="shader: customShader; color: green" />
<a-sky color="#ECECEC"></a-sky>
</a-scene>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论