Three.js - 如何动态更改对象的不透明度?

发布于 2024-12-21 03:55:04 字数 351 浏览 0 评论 0原文

这是我的对象:

var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: THREE.ImageUtils.loadTexture( "image.png" ) } ) );
object.position.set(2, 3, 1.5);

现在我在 init() 中创建了这个对象之后;函数,我可以直接转到对象并更改他的位置,如下所示:

object.position.x = 15;

现在的问题是如何更改纹理的不透明度???

谢谢 :-)

This is my object:

var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: THREE.ImageUtils.loadTexture( "image.png" ) } ) );
object.position.set(2, 3, 1.5);

now after I've created this object in init(); function, I can directly go to the object and change his position,like this:

object.position.x = 15;

Now the question is how can I change the opacity of the texture???

Thanks :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑶笙 2024-12-28 03:55:04

THREE.MeshLambertMaterial 扩展了 THREE.Material 这意味着它继承了 opacity 属性,因此您需要做的就是访问对象上的材质,并更改材质的不透明度:

object.materials[0].opacity = 1 + Math.sin(new Date().getTime() * .0025);//or any other value you like

另请注意,材质的 transparent 属性必须设置为 true。

object.materials[0].transparent = true;

(感谢 Drew 和 Dois 指出了这一点)

更新

该属性现在只是 材料

// enable transparency
object.material.transparent = true;
// set opacity to 50%
object.material.opacity = 0.5; 

THREE.MeshLambertMaterial extends THREE.Material which means it inherits the opacity property, so all you need to do is access the material on your object, and change the opacity of the material:

object.materials[0].opacity = 1 + Math.sin(new Date().getTime() * .0025);//or any other value you like

Also note that the material must have it's transparent property set to true.

object.materials[0].transparent = true;

(Thank you Drew and Dois for pointing this out)

Update

the property is now simply material:

// enable transparency
object.material.transparent = true;
// set opacity to 50%
object.material.opacity = 0.5; 
铁轨上的流浪者 2024-12-28 03:55:04
var map = THREE.ImageUtils.loadTexture( myJSONObject[i].url );
var material = new THREE.MeshLambertMaterial( { map: map, transparent: true } );
var object = new THREE.Mesh( geometry, material );

material.opacity = 0.6;
var map = THREE.ImageUtils.loadTexture( myJSONObject[i].url );
var material = new THREE.MeshLambertMaterial( { map: map, transparent: true } );
var object = new THREE.Mesh( geometry, material );

material.opacity = 0.6;
下壹個目標 2024-12-28 03:55:04

我知道这个问题很老了,但我想根据我使用过的内容给出答案,以防有人需要。对于 Three.js,我通过 Greensock 的 TweenMax/TweenLite 使用补间。这样,我就能够补间任何对象的任何属性,并且运行顺利。请在此处查看该库。我需要补间的属性是:

TweenLite.to(object, duration, properties);

其中持续时间以秒为单位,属性位于对象中。这样做的“陷阱”,特别是在使用 Three.js 时,是确保您了解对象参数的具体情况。例如,对于这个问题,如果您要更改网格的不透明度,您不能

TweenLite.to(mesh, 2, {material.opacity: 0});

这样做,您需要更具体并写下

TweenLite.to(mesh.material, 2, {opacity: 0});

我希望这对某人有帮助。补间真的太棒了!

I know this question is very old but I wanted to give my answer from what I used in case someone needs it. With three.js, I used tweening through Greensock's TweenMax/TweenLite. With that, I was able to tween any property of any object and it ran smoothly. Check out the library here. All I needed to tween the properties was:

TweenLite.to(object, duration, properties);

where duration is in seconds and properties are in an object. The "gotcha" for this, especially while using three.js, is to make sure you get specific with the object parameter. For example, per this question, if you are changing the opacity of a mesh, you cannot do

TweenLite.to(mesh, 2, {material.opacity: 0});

rather, you need to be more specific and write

TweenLite.to(mesh.material, 2, {opacity: 0});

I hope this helps someone. Tweening is really awesome!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文