Attempt to place an image with transparency onto a plane using three.js

I am a beginner with three.js and I'm trying to display an image (PNG image with transparency) on a plane, but my code isn't working...

Here is the code I've written:

var texture = new THREE.TextureLoader().load( 'images/lampedepoche.png' );
    
var geometry = new THREE.PlaneGeometry(300, 80);
var material = new THREE.MeshPhongMaterial({map: texture, color: 0xFFFFFF});
material.emissive.set(0x333333);
material.shininess = 60;
    
var ldp = new THREE.Mesh(geometry, material);
scene.add(ldp);

This is the image I want to use:

https://i.sstatic.net/B70i6.png

Below is the entire code I have written:

window.onload = init();


function init(){
    // Initializing the renderer
    renderer = new THREE.WebGLRenderer();

    renderer.setSize( window.innerWidth, window.innerHeight );
    document.getElementById('container').appendChild(renderer.domElement);
    
    // Initializing the scene
    scene = new THREE.Scene();
    
    // Placing the camera in the scene
    camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 100000 );
    camera.position.set(0, 0, 1000);
    scene.add(camera);
    
    // Creating a plane
me = new THREE.Mesh(new THREE.PlaneGeometry(900,550), new THREE.MeshPhongMaterial({color: 0xffffff}));
scene.add( me );
    
    me.position.x = 130;
    me.position.y = 10;
    me.rotation.y = -20;
    
    var texture = new THREE.TextureLoader().load( 'images/lampedepoche.png' );
    
    var geometry = new THREE.PlaneGeometry(300, 80);
    var material = new THREE.MeshPhongMaterial({map: texture, color: 0xFFFFFF});  
    material.transparent = true
    var ldp = new THREE.Mesh(geometry, material);
    scene.add(ldp);
    
    ldp.position.x = -410;
    document.getElementById("positionlx").value = 0;
    
    // Creating a cube 
    cube = new THREE.Mesh( new THREE.CubeGeometry( 100, 100, 100 ), new THREE.MeshPhongMaterial({color:0x00ffff}) );
    scene.add( cube );
    
    
    cube.position.y = 0;
    cube.position.x = 0;
    cube.position.z = 0;
    cube.rotation.y = 0;
    
    scene.add( new THREE.AmbientLight( 0x212223) );
      
    light = new THREE.SpotLight(0xffffff, 1);
    light.position.set(-320,0,0);
    light.angle = Math.PI/5;
    light.shadowCameraVisible = true;
    scene.add(light);
    
    renderer.shadowMap.enabled = true;
    renderer.shadowMapDarkness = 1;
    
     light.castShadow = true;
     light.intensity = 0.8;
     cube.castShadow = true;
     cube.receiveShadow = true;
     me.receiveShadow = true;
    
     lightHelper = new THREE.SpotLightHelper( light );
     scene.add(lightHelper);
    
    renderer.render( scene, camera );
}

I may have made some mistakes since I am still learning JavaScript.

I would greatly appreciate any help. Thank you!

Answer №1

Did you experiment with setting material.transparent to true?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modifying the HTML <select> element with JavaScript

[resolved] I'm encountering an issue with the code below that is supposed to dynamically change the options in a second drop-down menu based on the selection made in the first menu. I've tried to troubleshoot the problem but haven't been suc ...

Display issue with React TypeScript select field

I am working with a useState hook that contains an array of strings representing currency symbols such as "USD", "EUR", etc. const [symbols, setSymbols] = useState<string[]>() My goal is to display these currency symbols in a select field. Currently ...

Show the time in hours and minutes (00:00) while rounding off seconds to the nearest minute

I need the time to always display with leading zeros when less than 10. For example, if a task took 3 hours, 7 minutes, and 33 seconds, it should be shown as 03:08. Currently, I have the buttons disabled after they are clicked to prevent restarting the ti ...

Mongodb: Search for IDs within a nested array field

My MongoDB data structure includes user profiles with friend request information. Here's an example: { _id: "someId", profile: { username: "oliv", friendRequests: [ { fromUserId: "anId", accepted: false, created: " ...

Numerous Axios GET calls made by various components

I am facing an issue where I have two different components rendering on the main screen. Both of them make multiple axios.get requests to fetch data. However, upon initial page load, only the last component successfully retrieves the data while the first c ...

Getting the document ID from a faunaDb query: a step-by-step guide

One array that I am dealing with consists of the following: [ Ref(Collection("twitch_users"), "280881231730573837") ] My goal is to extract the string of numbers from this array and utilize it in another function within my codebase. Ho ...

Retrieve the socket within a callback using socket.io

Currently, I am incorporating socket.io into my node.js project. When setting up an event handler like the example below: io = require('socket.io')(http); io.on('connect',function(socket){ socket.on('some event',function( ...

Steps for invoking a Java function within a JSP document

In my project, I need to ensure that the username entered by a user is unique and does not match any other usernames already registered. This validation process occurs in newuser.jsp, where a function called searchForUsername in SemanticSearch.java is exec ...

Use JavaScript to automatically fill input field with data

Is there a way to use the script below in order to populate an input field value, rather than just populating a paragraph? Any suggestions on how to achieve this? $(function check(){ $(document).ready(function() { $('#forma').s ...

Updating CodeIgniter content dynamically by clicking on an anchor link without reloading the page

I am looking to dynamically update my webpage content without the need for a page refresh. View <a href="<?php echo site_url('site2/getBranchDetails/'.$row_bank->branch_id.''); ?>"><?php echo $row_bank->bank; ?&g ...

My shader is set up to analyze all vertex coordinates using a data texture, but for some reason, the texture appears to be devoid of any information

I have constructed a BufferGeometry using an array. How can I manipulate individual vertices in the vertexShader? Everything is functioning correctly so far. Now, I intend to transmit the vertex coordinates not only as an attribute but also as a data tex ...

Using SVG Mask to enhance shape Fill

I am having trouble achieving the desired effect of darkening the fill of objects based on a specified gradient. Instead, when the mask is applied over the fill, it actually lightens it. I suspect that this issue arises from the color blending method being ...

Once you address a block using jQuery

$(function(){ $(window).scroll(function () { var scrollVal = $(this).scrollTop(); var adscrtop =$(".header").offset().top // 在 RWD 767以下不作動 if(window.screen.width>767){ if(sc ...

Is it possible to detect a swipe event without relying on third-party libraries

Is there a way to detect a swipe instead of a click using only pure jQuery, without relying on jQuery Mobile or external libraries? I've been examining the TouchSwipe source code, but it contains a lot of unnecessary code for what I really need - sim ...

Using jQuery to insert PHP code into a <div> element

In our capstone project, I'm looking to implement a dropdown calendar feature. I initially attempted to use ajax within a dropdown Bootstrap 4, but encountered issues with collapsing. As an alternative, I am considering utilizing the include method. A ...

The AJAX request is now being "canceled" since the website is up and running

After successfully running AJAX requests on my new version, which was in a sub directory of my site (www.staging.easyuniv.com), I moved the site to the main directory to make it live (www.easyzag.com). Although everything seems to be functioning properly, ...

a gentle breeze gathers a multitude of entities rather than items

When utilizing a restful server with node.js and sending a collection of entities wrapped in one entity object (Lookups), everything seems to be functioning properly. However, the issue arises when breeze views the entities in the collection as plain objec ...

The componentDidMount function is not initializing the state

I am trying to access the references from the render function and then set them to the state. Below is my code snippet: class App extends Component { constructor(props) { super(); this.arr = this.generateTimelineArray(); ...

What are the steps to create a distorted effect on HTML using webGL?

Recently, I was experimenting with an interesting example from this link: . In my local environment, I tweaked the Three.js renderer to become an overlay by adjusting the z-index to 999 and setting {alpha: true} to make it transparent. Then, underneath th ...

What are some strategies for exporting methods without resorting to the use of import * as ...?

Imagine having a file structured like this: // HelperFunctions.ts export const dateFormat = 'MM/DD/YYYY'; export const isEmpty = (input: string | null | undefined): boolean => { if (input == null) { return true; } if (!in ...