Challenges arise when working with texture size in Three.js

Less talk, more code =)


var objects = [];

var camera, scene, renderer;

document.addEventListener('mousedown', onDocumentMouseDown, false);

init();
render();

function onDocumentMouseDown(event) {
event.preventDefault();
var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
projector.unprojectVector(vector, camera);
var ray = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize());
var intersects = ray.intersectObjects(objects);
if (intersects.length > 0) {
console.log(intersects[0].object);
}
}

function init() {

container = document.getElementById('container');
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 1, 1100);
camera.position.z = 50;
scene.add(camera);


var particle = new THREE.Particle(new THREE.ParticleBasicMaterial({ map: THREE.ImageUtils.loadTexture("img/satellite.png") }));
objects.push(particle);
//particle.scale.x = particle.scale.y = 0.25
scene.add(particle);

projector = new THREE.Projector();
renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
}

function render() {
camera.lookAt(scene.position);
renderer.render(scene, camera);
}

So now we have a clickable particle with a texture. But there are some issues I need help with:

  1. Why is the clickable area of the particle so small? It only works when clicking in the middle.

  2. Also, why is the particle so large even though the texture file is 16x16? How can I make it smaller without affecting the clickable area?

Answer №1

Although this question may seem outdated, I encountered the same issue recently and noticed that it had gone unanswered. After trying different approaches, I finally found a solution.

The workaround involves creating two particles - one to draw a simple geometry using ParticleCanvasMaterial, and another to display an image on top of it.

To implement this solution effectively, you can use ParticleCanvasMaterial for tracking intersections and have the second particle serve as a dummy object solely responsible for displaying the image in the 3D scene.

Here's a snippet of the code:

var programFill = function (context) {
                context.beginPath();
                context.rect(-0.5, -0.38, 1, 1);
                //context.fill(); 
            }

            var p = new THREE.ParticleCanvasMaterial({ program: programFill, transparent: true }); 
            var particle = new THREE.Particle(p);
            particle.scale.set(23, 23);
            particle.position.set(200, 300, 200);

            var imgTexture = THREE.ImageUtils.loadTexture('images/image.png');
            var p2 = new THREE.ParticleBasicMaterial({
                map: imgTexture
                , size: 1
            });

            var imgParticle = new THREE.Particle(p2);
            imgParticle.scale.x = 0.5;
            imgParticle.scale.y = 0.5; 
            imgParticle.position.set(200, 300, 200);

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

What is the best way to create a JavaScript button that can be clicked to increase a variable by 1?

I am curious to know how you can create a button that appears on an empty HTML canvas and, when clicked, adds +1 to a variable. I am working on this JavaScript code within an HTML text file which will later be saved as an HTML file. Please ensure that th ...

What is the best way to handle various sections with changing structures within a complex form using react-hook-form?

I am working on a complex form that has sections A, B, and C, each of which can be in shape A1 or A2, B1 or B2, C1, or C2. Users are required to fill out settings based on whether the section is set to "advanced" or "basic". I want users to submit the enti ...

Press the button within the table as its name undergoes periodic changes

I am using Python along with Selenium to automate the process of selecting and reserving a room on a website. The site displays a table containing available rooms, and my goal is to locate a specific room and click on the corresponding button within the ta ...

Retrieve information in JSON format from a document

I'm trying to extract data from a JSON file without knowing the exact location of the data. Here is an example JSON: var names= [ { "category":"category1" , "name1":"david", "name2":"jhon", "name3":"peter" }, { "category":"catego ...

Dealing with an empty req.body in an Express.js POST request

Having trouble solving this issue with a simple search. Can anyone provide clearer guidance? In the client-side code, I attempted to attach an object using xhr.send(obj). Currently, I'm trying to append to the formData object, but the result remains ...

Using a prop array as v-model in a Vue JS CheckBoxGroup implementation

Struggling to create a reusable CheckBoxGroup component with a prop array as v-model. I checked out the vuejs guide at https://v2.vuejs.org/v2/guide/forms.html#Checkbox which uses the v-model array in the data of the same component. However, this approach ...

Is there a simple method in JavaScript to combine, structure, and join numerous multi-dimensional arrays in a specific manner (from right to left)?

Looking for a simple solution to merge, flatten, and concatenate multiple multi-dimensional arrays in JavaScript in a specific manner (from right to left) # Example [['.class1', '.class2'], ['.class3', ['.class4', & ...

Using Three.js to create a repeating texture within a single canvas rendering

I am using a threejs library for React to render a sphere, "import * as THREE from 'three';". The sphere and its texture render successfully. I have successfully loaded an image that wraps around the sphere entirely. My question is: How can I ...

Showing a collection of 30 Instagram photos with a specific tag from a particular user ID

My Instagram API call is set up to retrieve images with the CrookedSpaces tag. Once the images come back, I filter the data to only show images from a specific user identified by their userID. Here is the code snippet: $(function() { $.ajax({ type: "G ...

Hexagon Pie Chart Presentation

I am interested in creating a Pie Chart within a Hexagon shape. There are likely multiple ways to achieve this. In the image below, you can see my Hexagon and two potential ideas: The Hexagon itself with 6 vertices and 4 faces The desired final look wi ...

What is the best way to showcase varying colors in HTML content pulled from a database?

I have a MySQL database with 15 or more rows that I want to retrieve and display in alternating colors. For the first row: <tr><td height="30" bgcolor="#F5F5F5">....</td></tr> For the second row: <td height="30" align="center" ...

Unlimited digest loop in Angular.js caused by nested ng-repeat and filter

In my AngularJS project, I have developed a custom filter that categorizes elements by type and performs a search for multiple search terms across all attributes of devices. angular.module('abc').filter('searchFor', function(){ return ...

Applying a condition to filter elements using AngularJS

I have created an array in my controller with the following data: var people = [ { name:"Alice", number:'808574629632', email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail ...

Dynamic Rendering of Object Arrays in Table Columns using JavaScript

In the process of developing an appointment slot selection grid, I have successfully grouped all appointments by dates. However, I am facing challenges in displaying this array of Objects as a clickable grid with columns. The current output can be viewed h ...

When the input tag is set to null, the array that holds e.target.files will be devoid of any content

Hey there, I'm currently in the process of uploading a folder. However, I encountered an issue when trying to upload the same folder again. It seems that the data was empty when I attempted to set it to null. const uploadFolders = []; let uploadFolder ...

Enhancing Accessibility for the jQuery Countdown Plugin

Seeking to enhance the accessibility of my website's jQuery countdown, I am striving to adhere to WAI-ARIA guidelines. The specified requirements are as follows: Ensure the area is live so it updates dynamically with the countdown display. Avoid re ...

I am experiencing difficulties with decoding the JSON in Appengine ProtoRPC

For some reason, I'm having trouble getting the protoRPC API to work on my app-engine. Here is the request I am making: $.ajax({ url: '/guestRPC.get_tags', type: 'POST', contentType: 'application/json', ...

What is the best way to create a sleek typewriter effect transition by hovering between tabs?

When hovering over a tab, the content in the main content area's child div should be displayed from a sibling div that holds the content. Initially, the details div style is set to hide the contents but upon hover, it is displayed. The described func ...

How to resolve the following error message: "MongoNotConnectedError: Client must be connected before executing operations."

Hello everyone, I am encountering an issue while running the following code: File: productinit.js const product = require('../model/product'); const mongoose= require('mongoose'); const connectDB = async () => { await mongoose.c ...

Refresh Jira Gadget AJAX while Viewing Configuration Screen

Having trouble finding a solution to this specific issue and I'm really hoping for a resolution. I am currently working on developing a Jira gadget where I have a configuration screen with two fields. The first one is a quickfind project picker that ...