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?

huangapple go评论53阅读模式
英文:

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 -->

&lt;script src=&quot;https://aframe.io/releases/1.4.1/aframe.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
  AFRAME.registerShader(&quot;customShader&quot;, {
    schema: {
      color: {type: &quot;color&quot;, is: &quot;uniform&quot;, default: &quot;red&quot;},
      opacity: {type: &quot;number&quot;, is: &quot;uniform&quot;, 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);
              }
        `,
  });
&lt;/script&gt;

  &lt;a-scene background=&quot;color: black&quot;&gt;
    &lt;a-camera position=&quot;0 0 3&quot;&gt;&lt;/a-camera&gt;
    &lt;a-plane 
           segments-height=&quot;15&quot; 
           segments-width=&quot;15&quot; 
           position=&quot;0 0 -2&quot; 
           material=&quot;shader: customShader; color: green&quot; /&gt;
    &lt;a-sky color=&quot;#ECECEC&quot;&gt;&lt;/a-sky&gt;
  &lt;/a-scene&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月26日 08:01:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定