52

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

1
  • I think it's good to keep these in mind when using opacity feature. It is material from a course so you may need to watch previous videos but they are short. Commented Mar 28, 2014 at 20:17

3 Answers 3

86

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; 
Sign up to request clarification or add additional context in comments.

7 Comments

Note that you must set material.transparent = true as well as the opacity.
0.5 + 0.5*Math.sin(new Date().getTime() * .0025); might be better ;)
As of R69, I had to use THREE.Object3D.material.opacity to change an object's opacity at runtime.
The comment on the "transparent" attribute should be included in the answer.
Does still work? my "object" doesn't have a materials property it has material instead?
|
13
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; 

1 Comment

how about a bit of an explanation?
9

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.