Exploring Three.js: Implementing Multiple Textures for Points

I'm currently working on a Three.js project where I want to incorporate multiple points with different textures. However, I've encountered an issue with changing textures in my fragment shader. Despite setting up the scene so that points should switch between texture 0 (a cat) and texture 1 (a dog), all points are displaying the same texture:

/**
* Creating a scene object with a specific background color
**/

function getScene() {
  var scene = new THREE.Scene();
  scene.background = new THREE.Color(0xaaaaaa);
  return scene;
}

/**
* Setting up the camera for the scene. Camera parameters:
*   [0] field of view: determines the visible portion of the scene (in degrees)
*   [1] aspect ratio: defines the scene's aspect ratio in width/height
*   [2] near clipping plane: objects nearer than this are culled from the scene
*   [3] far clipping plane: objects farther than this are removed from the scene
**/

function getCamera() {
  var aspectRatio = window.innerWidth / window.innerHeight;
  var camera = new THREE.PerspectiveCamera(75, aspectRatio, 0.1, 10000);
  camera.position.set(0, 1, -6000);
  return camera;
}

/**
* Configuring the renderer for the scene
**/

function getRenderer() {
  // Create a WebGL canvas renderer
  var renderer = new THREE.WebGLRenderer({antialias: true});
  // Support for retina displays
  renderer.setPixelRatio(window.devicePixelRatio);
  // Set the canvas size
  renderer.setSize(window.innerWidth, window.innerHeight);
  // Append the canvas to the DOM
  document.body.appendChild(renderer.domElement);
  return renderer;
}
... (continued code snippets)
html, body { width: 100%; height: 100%; background: #000; }
body { margin: 0; overflow: hidden; }
canvas { width: 100%; height: 100%; }
<html>
<body>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.min.js'></script>
  <script src='https://rawgit.com/YaleDHLab/pix-plot/master/assets/js/trackball-controls.js'></script>

    <script type='x-shader/x-vertex' id='vertex-shader'>
    /**
    * Explanation of the vertex shader...
    ***/
    
    ... (vertex shader code here)

    </script>

    <script type='x-shader/x-fragment' id='fragment-shader'>
    /**
    * Explanation of the fragment shader...
    ***/

    ... (fragment shader code here)

    </script>
</body>
</html>

I'm seeking input from the community to help me figure out what might be causing this issue. Any suggestions or insights would be greatly appreciated!

Answer №1

If you find yourself in a similar situation as I did, facing the same issue, I'd like to share that the example above was just a simplified version of a larger scene. In the other scene, I mistakenly passed an attribute called texture and initialized it as textureIndex inside the vertex shader. This led to passing the textureIndex to the fragment shader as a varying, where it always ended up being 0. The lesson learned here is that when trying to read from an attribute not properly passed to the vertex shader, the value defaults to zero. It would have been better if this triggered an error instead.

In the initial example mentioned, I had accidentally commented out the varying declaration in the fragment shader. This issue has now been resolved:

 // Code snippets for generating the scene, camera, renderer, controls, points, and rendering
...
 // HTML script tags for Three.js library and trackball controls
</body>
</html>

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 are the functioning principles of older ajax file uploading frameworks (pre-html5)?

Traditional HTML includes an input element with a file type option. Tools are available that enable asynchronous file uploads with progress tracking. From what I gather, this is achieved by splitting the file into chunks and sending multiple requests wit ...

PHP and JavaScript are two powerful programming languages that are

While I understand that PHP and JavaScript operate in different locations, I am curious to know if there is a way to incorporate some PHP code into my JavaScript file. I need to create unique URLs for linking to profiles and news posts, such as /#/news/IDH ...

How to implement an external font in AngularJs

I am developing a multilingual website with AngularJS and need to load a font using a .woff file. However, I only want to load the font when it corresponds to the specific language being used on the site. function init(lang){ if(lang == 'eng') ...

Creating a server that is exclusive to the application in Node.js/npm or altering the response body of outgoing requests

As I work on developing a node app, the need for a server that can be used internally by the app has become apparent. In my attempts to create a server without listening to a port, I have hit a roadblock where further actions seem impossible: let http = ...

Guide on incorporating an SVG file according to the user's role within an Angular application

How can I dynamically import an SVG file based on the user's role, whether they are an admin or a regular user? Currently, my code in layout.ts looks like this: <div class="layout-body h-100 "> <ng-container [ngTemplateOutlet]=&q ...

Encountered an error with symbol '@' while utilizing ES6 decorators

I have recently set up a React project and now I'm attempting to integrate MobX into it. This requires using decorators such as: @observable However, when I try to implement this, I encounter the following error: https://github.com/mobxjs/mobx Mod ...

In an MVC 4 application, when using JQuery to replace HTML and then calling the .show() function, the

I have a null div element: <div id="reportBody"></div> with basic styling: #reportBody { height: 100%; } The reportBody div is located within a hidden modal that is triggered by a button click. I am using jQuery and AJAX to call a contro ...

Having trouble with npm global installation? Encountering the error message "Error: EACCES: permission denied

As the administrator of my MacBook, I am facing an issue while trying to run a npm command in my Django project. It is refusing to run due to missing permissions. (venv) jonas@Air-von-Jonas salaryx % npm install -g sass npm ERR! code EACCES npm ERR! syscal ...

Is there a way to sort data by year and month in mongodb?

I'm trying to filter data by year in MongoDB based on a specific year and month. For example, if I pass in the year 2022, I only want to see data from that year. However, when I try using the $gte and $lte tags, it returns empty results. Can someone g ...

Using Vue.js to loop through and display a set of images from the assets directory using v-for

I have a collection of 100 images from 1.png to 100.png stored in my assets folder. I am looking to dynamically display them using v-for without having to manually specify each individual image URL. <div v-for="n in 100"> <img :src="`../asset ...

Error exporting a function in Node.js

As someone who is new to writing node.js modules, I have been working on a module in the following way, a.js var fs = require("fs") ; var util = require("util") ; var mime = require("mime") ; module.exports = { getDataUri: function (image, callback ) ...

Terminate AJAX requests and form submissions, store them, modify them, and dispatch them at a later time

I am in need of requesting additional credentials from a user upon clicking specific buttons or submitting certain forms. The approach I am currently taking involves: Intercepting the submit event, stopping it, and saving a copy Prompting the user for c ...

Error: The function (0, _context_stateContext__WEBPACK_IMPORTED_MODULE_4__.useStateContext) is not of type TypeError

I have been working on setting up a useContext() for an online store to share data across all components and manage product quantities. However, I've hit a snag while trying to import the context(). Everything was running smoothly until I added this p ...

Retrieve information from a PHP file using AJAX when the output is just a single line

I never thought I would find myself in this situation, but here I am, stuck. I just need a single result from this PHP file, so is using an array really necessary? Despite my efforts to console.log(result) multiple times, all I get back is "null". What c ...

What are some creative ways to customize and animate the cursor or caret within an input field?

Just to clarify, I am working with React and Material-UI. I have a task at hand where I need to modify the appearance of the caret within an input element by adding an animation to it. I have managed to change its color using caret-color and set a default ...

What is the process for duplicating by value?

I am utilizing a DataTables plugin and have it defined like this: var oTable = $('#table1').dataTable({ 'aaData': data, 'aoColumns': columns, 'bScrollInfinite': true, 'bScrollCollapse&ap ...

Tips for altering the checked status of a radio input in a React component

I'm working with input type radio buttons const AnswerOptions = (props) => ( <> <input type="radio" id="something" ...

Implementing jquery to show information in a dropdown menu

I would like to populate a select box with data from an AJAX response. Below is the code from my view file: <select class="form-control" name="vendor" id="vendor_list" required style="width: 159px;"> <option value="">Vendor 1</option> & ...

Designing a Unique Shader with Three.js

Please be aware that a lot of the code has changed as per edit 3 below. I came across a blog post by Brandon Jones (link here) that I really liked. I wanted to convert his code to Three.js, but I'm encountering some difficulties. You can access his c ...

Developing a dynamic modal using Angular and embedding Google Maps within an iframe

I'm currently working on implementing a modal in my Angular application that, when opened, displays Google Maps within an iframe. The problem I'm facing is that the iframe isn't loading and I'm receiving this error in the browser conso ...