【问题标题】:Aframe - PNG with transparency in front of entityAframe - 在实体前面具有透明度的 PNG
【发布时间】:2017-10-10 21:07:00
【问题描述】:

也许这不是 Aframe 特有的,所以如果这是一个一般性的 html 问题,我们深表歉意,但是如果您有一个透明的 PNG 并将其放在另一个图像或不透明度小于 1.0 的任何对象前面,则透明PNG的一部分掩盖了它背后的东西。有没有办法在不将 PNG 定位在其他实体后面的情况下解决这个问题?

这是一个 png 的示例,它在图元面前表现得如何。它之所以有效,是因为基元都是完全不透明的: https://codepen.io/iBrews/pen/dWNymp

<script src="https://aframe.io/releases/0.5.0/aframe.min.js"></script>

<a-assets>
<img id="pngImage" crossorigin="anonymous" 
src="http://ekladata.com/hXTGfWnZm170W274zDRObDlqOlc.png">
</a-assets>

<a-scene>
<a-image position = "0 1.5 -1" width="1" height="1" src="#pngImage"></a-image>

<a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
<a-box position="-1 0.5 -3" rotation="0 45 0" width="1" height="1" depth="1" color="#4CC3D9"></a-box>
<a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
<a-sky color="#ECECEC"></a-sky>
</a-scene>

这是我的问题的一个例子。具有透明度的 png 会掩盖其后面的所有图像,无论它们是否具有透明度,以及不透明度小于 1.0 的任何图元 https://codepen.io/iBrews/pen/ZKLpqp

<script src="https://aframe.io/releases/0.5.0/aframe.min.js"></script>

<a-assets>
<img id="transpImage" crossorigin="anonymous" src="http://ekladata.com/hXTGfWnZm170W274zDRObDlqOlc.png">
<img id="flatImage" crossorigin="anonymous" src="https://upload.wikimedia.org/wikipedia/commons/e/e9/Felis_silvestris_silvestris_small_gradual_decrease_of_quality.png">
</a-assets>

<a-scene>
<a-image position = "0 1.6 -1" width="1" height="1" src="#transpImage"></a-image>
<a-image position = "1 1.8 -1.5" width="1" height="1" src="#transpImage"></a-image>
<a-image position = "-.7 2 -1.5" width="1" height="1" src="#flatImage"></a-image>

<a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
<a-box position="-1 0.5 -3" opacity= ".5" rotation="0 45 0" width="1" height="1" depth="1" color="#4CC3D9"></a-box>
<a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
<a-sky color="#ECECEC"></a-sky>
</a-scene>

【问题讨论】:

  • 你的代码在哪里?网络上的透明 PNG 按您的预期工作;下面的任何东西都会通过透明区域显示出来。如果您在 canvas 中渲染透明 PNG,则完全不同——但您没有提供任何有用的信息,因此无法提供上下文帮助。
  • 抱歉——添加了代码和示例。
  • 您的代码看起来应该可以工作,也许将transparent="true" 添加到&lt;a-image /&gt;? (请注意,A-Frame 库使用此问题中的 HTML 标记来呈现 WebGL 上下文,因此它不是其他 DOM 元素之上的标准 &lt;image&gt;
  • 没用。查看我在底部添加的新 codepen 示例——完美地说明了这个问题。

标签: image transparency aframe


【解决方案1】:

您可以将材质的 alphaTest 设置为 0.5。在 A-Frame master 上(运送到 0.6.0),你可以这样做:

&lt;a-image material="alphaTest: 0.5"&gt; 或者&lt;a-image alpha-test="0.5"&gt;&lt;/a-image&gt;

在 A-Frame 0.5.0 上,您可以手动完成:

<script>
  AFRAME.registerComponent('alpha-test', {
    dependencies: ['material'],
    init: function () {
      this.el.getObject3D('mesh').material.alphaTest = 0.5;
    }
  });
</script>

&lt;a-image src="#transpImage" alpha-test&gt;&lt;/a-image&gt;

笔:https://codepen.io/mozvr/pen/jmyVRG

【讨论】:

  • 这非常有帮助!在 PNG 透明背景上完美运行,但在渐变上效果不佳。有没有办法让它也适用于渐变?
猜你喜欢
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2010-12-22
  • 1970-01-01
  • 2014-09-30
  • 2016-08-23
  • 1970-01-01
相关资源
最近更新 更多