Issue encountered with adjusting the scale of vUv in a custom shader for three.js: as the multiplying factor decreases, the painted result grows larger in size

Currently, I am experimenting with a custom shader in three.js to create an animated and expanding texture. However, I have encountered an issue where multiplying vUv by a certain number results in unexpected behavior. Strangely, when the number is increased, the size of the painted result decreases instead of expanding as intended. For instance, multiplying by 0.1 makes the result appear 10 times larger, while multiplying by 10.0 makes it 10 times smaller.

Below are snippets of my shader code for better clarity:

//vertex shader

varying vec2 vUv;

uniform float uFixAspect;

void main() {
  vUv = uv;

  gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
precision mediump float;
uniform float time;
uniform vec2 resolution;

varying vec2 vUv;

uniform sampler2D uTex;

void main() {

  // Despite wanting the result to be 10 times smaller than the original, it appears 10 times larger
  mat2 newvUv = vUv * mat2(0.1, 0.0, 0.0, 0.1);
  gl_FragColor= texture2D(uTex, newvUv);

}

This snippet showcases my implementation in three.js:

      const loader = new THREE.TextureLoader();
      loader.load(
        "./assets/textures/tex.png",
        tex => {
          const geo = new THREE.PlaneGeometry(2, 2);
          const mat = new THREE.ShaderMaterial({
            uniforms: {
              uTex: {
                value: tex
              },
              time: {
                type: "f",
                value: 0.1
              },
              resolution: {
                type: "vec2",
                value: new THREE.Vector2(512, 512)
              }
            },
            vertexShader: vert,
            fragmentShader: frag,
          });

          const shaderObj = new THREE.Mesh(
            geo,
            mat
          );

          marker.add(shaderObj);
        }
      );

I am unsure if there is an error in my code or if this discrepancy is related to three.js itself. Your insights would be greatly appreciated. Thank you.

Answer №1

When I increase the size of vUv by multiplying it with a specific number, the larger the number, the smaller the resulting pattern becomes.

This is because you are adjusting the texture coordinates for the appearance without altering the actual texture itself.
To achieve the desired effect, you need to "scale down" the texture. This means keeping the texture at the same size while selecting texels from an "up scaled" position.

To do this, use the reciprocal of the scale factor:

float scale = 1.0/0.1; // reciprocal scale
mat2 newvUv = vUv * mat2(scale, 0.0, 0.0, scale);

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

JSX conditional rendering not behaving unexpectedly

Having difficulty with a conditional JSX statement causing an element to not display properly unless the window is resized. Please note that it works fine in development mode but does not show correctly after building. The goal is to show navigation links ...

My PHP script is not functioning correctly with Ajax

I am currently working with HTML5, PHP, and JavaScript. My goal is to implement Ajax in order to display the sizes of a selected product when an option is chosen from #productoSeleccionado. However, I believe that there may be an issue with my code as the ...

Tips for converting an online HTML document into a string within an Angular framework

I'm currently facing an issue while trying to extract data from an online HTML document using Angular. The problem lies in encountering a cors error consistently. Here is the snippet of my code for reading the HTML document: loadParsingData(htmlToP ...

troubles with compatibility between bootstrap.css and IE11

I am currently developing a web application using AngularJS and bootstrap.css. While everything appears fine on Chrome, I am facing some formatting issues on both Firefox and IE11. HEAD <head> <meta charset="utf-8"> <meta http-equi ...

What is the best way to transfer information from a server running Express.js to a client using React.js?

When using Express.js, I have the ability to send an HTML file that includes a React component. app.get('/index', function(req, res) { res.sendFile(path.join(__dirname + '/src/index.html')); }); Suppose I want to send a uniqu ...

Is there a jQuery alternative to XMLHttpRequest's upload functionality?

When working with the HTML5 File API, the upload process is handled through an object called upload within the XMLHttpRequest. I am currently following a tutorial on this topic, you can find it here (or use the Google cache mirror since the original link i ...

Angular: Leveraging real-time data updates to populate an Angular Material Table by subscribing to a dynamic data variable in a service

Seeking guidance on how to set up a subscription to a dynamic variable (searchData - representing search results) for use as a data source in an Angular Material Table. I have a table-datasource.ts file where I want to subscribe to the search results from ...

Switching between languages dynamically with Angular JS using $translateProvider and JSON files

I currently have a collection consisting of 6 different JSON files. en.json es.json fr.json it.json ja.json zh.json An illustration of the data present in each file is as follows (in this instance, considering en.json): { "SomeText": "Test in Englis ...

Effortless AJAX Submission and MySQL Data Refresh

I've hit a roadblock and can't seem to figure things out. I'm getting rid of my current code because it's not working well across different browsers, particularly when it comes to ajax submission. Could someone please provide me with a ...

JS - What is causing my JavaScript src to not work properly?

Here is a snippet of my code: <form name="calculator"> <input type="button" name="latest" value="You are not using the latest version."> <script src="http://www.alvinneo.com/bulbuleatsfood.js"> if(latest-version==="1.0.4.2"){ document.ca ...

Guide on how to navigate to the bottom of a div element using Selenium Webdriver

My current project involves a unique challenge where there is a specific div element on the webpage that acts as a popup dialog when a link is clicked (similar to Facebook reaction dialog boxes). To automate tests for this scenario, I am using Selenium We ...

Button with a Jquery icon design

Hello, I am currently working on implementing an icon button that can collapse and expand text. Although I have successfully implemented the logic for collapsing/expanding, I am facing difficulties in creating the actual icon button. The theme I am require ...

Difficulty in displaying additional search outcomes

I decided to take on the challenge of learning coding by working on a website project that involves creating a simple search site. One feature I implemented is when users search for keywords like "Restaurant" or "Restaurants," they are presented with refi ...

Revamp HTML <font size=1-7> with the use of CSS or JavaScript

I've been developing a prototype application that incorporates a plugin utilizing the deprecated HTML feature. Can I set custom pixel sizes for each font size ranging from 1 to 7? Currently, I'm contemplating using CSS zoom/scale properties, bu ...

Tips for incorporating jQuery UI into Angular 6

No matter how many methods I have tried from StackOverflow, I cannot seem to get jquery-ui to work in my angular 6 component. Here's what I've attempted: I went ahead and ran npm install jquery jquery-ui to install both jquery and jquery-ui. ...

The pie chart is unable to render due to issues with accessing the external JSON file in Ext

I attempted to create a pie-chart using extjs, but unfortunately, the pie-chart is not showing up. Interestingly, when I pass the JSON data through AJAX inline, it works perfectly fine. However, when I try passing it through a file, it doesn't work a ...

Mapbox GL - Incorporate a non-scaling polygon feature during zoom operations

Is there a method to incorporate a polygon that maintains its size regardless of zooming? Currently, when adding a polygon circle shape with a 10-meter radius and zooming out, the polygon shrinks in size as expected. However, is it feasible to include a s ...

alter the appearance of a timepiece using a controller dynamically

Currently, I have a watch set up in my controller for the following watch: var pageFunc = $scope.$watch('page["USER"].number', function(newob,oldob){ console.log("WATCHING page"); if(oldob != newob){ //perform data load } },t ...

What steps can I take to refactor a portion of the component using React hooks?

I am trying to rewrite the life cycle methods in hooks but I am facing some issues. It seems like the component is not behaving as expected. How can I correct this? Can you provide guidance on how to properly rewrite it? useEffect(() => { updateUs ...

How can I deactivate a Material UI button after it has been clicked once?

Looking to make a button disabled after one click in my React project that utilizes the MUI CSS framework. How can I achieve this functionality? <Button variant="contained" onClick={()=>handleAdd(course)} disabled={isDisabled} > ...