Changing alpha on material

    {
        use : evt;
       // console.log(evt);
        //DragControls.lastHovered = evt.object;
        this.context.domElement.style.cursor = 'pointer';
        const myobj = this.gameObject;
        if(myobj)
        {
           

            if(this.my_renderer && this.materialToSwitch)
            {
                this.materialToSwitch.color = new Color(Math.random(), Math.random(), Math.random());
                this.my_renderer.sharedMaterials[0] = this.materialToSwitch;           
            } 
        }


    }```

Hi there, i am using this to change the colour of an object to a random one. Is there a way to also change the alpha?

[Original Post on Discord](https://discord.com/channels/717429793926283276/1182729150092550215)

*by user 95852307077406720*

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Here is what I found for you :brain: Discord

Certainly, Hario. In Three.js, the Color class doesn’t include alpha information; alpha is typically a property of the material, called opacity. To change the alpha (opacity) value along with the color, make sure the material has transparent set to true, and then directly set the opacity property of the material to a value between 0 (fully transparent) and 1 (fully opaque).

Here’s an updated version of your onPointerEnter method that changes both the color and the alpha:

onPointerEnter(evt) {
    // Ensure evt is used to prevent compiler errors
    use: evt;

    this.context.domElement.style.cursor = 'pointer';
    const myobj = this.gameObject;

    if (myobj && this.my_renderer && this.materialToSwitch) {
        // Random color using Color class
        const randomColor = new Color(Math.random(), Math.random(), Math.random());

        // Set the color of the material
        this.materialToSwitch.color = randomColor;

        // Set the material to transparent to be able to change the opacity
        this.materialToSwitch.transparent = true;

        // Random alpha value between 0 and 1
        this.materialToSwitch.opacity = Math.random();

        // Assign the modified material back to the renderer
        this.my_renderer.sharedMaterials[0] = this.materialToSwitch;
    }
}

Please note that you need to apply this logic only if the material supports transparency. Also, changes to sharedMaterials should be done with caution as it affects all objects using the shared material instance. If you want the change to apply only to this particular object, consider cloning the material or managing a separate instance for this gameObject.

0 messages left GPT 4
thread_Wp7JCDoou3ZfkHqymuF2Y6Sl
Thanks for your feedback!

You did well here, Mr Bot!

Hello Marcel. If you have any questions or need assistance with Needle Engine, feel free to ask. I’m here to help!

1 messages left GPT 4
thread_RP0uwz9nUIQUXpCDCtC9Kajj