Utilizing Shadertoy's iResolution variable within a Three.js code snippet

Currently, I am in the process of converting a mesmerizing shader effect from Shadertoy into a local Three.js project. Despite my efforts, I have been struggling to get it to render properly. If you'd like to give it a shot yourself, you can access the full code snippet here.

The main issue seems to stem from how I am handling the iResolution variable. In Shadertoy, this built-in global variable typically holds the pixel dimensions of the window. Let's take a look at how iResolution is utilized in the original Shadertoy:

vec2 uv = fragCoord.xy / iResolution.y;
vec2 ak = abs(fragCoord.xy / iResolution.xy - 0.5);

For the conversion process into a local Three.js script, I've attempted two different approaches when dealing with iResolution:

1) Initially, I tried loading the window dimensions as a Vector2 and passing them into the shader as the uniform vec2 uResolution:

vec2 uv = gl_FragCoord.xy / uResolution.y;
vec2 ak = abs(gl_FragCoord.xy / uResolution.xy - 0.5);

This method closely mirrors the structure of the original Shadertoy, but unfortunately, no rendering takes place.

2) The second approach, borrowed from this helpful Stack Overflow answer, involves transforming the uv coordinates to absolute xy values:

vec2 uvCustom = -1.0 + 2.0 * vUv;
vec2 ak = abs(gl_FragCoord.xy / uvCustom.xy - 0.5);

To be honest, I'm not entirely clear on its workings, and there might be an error in my use of the uvCustom variable in the second line.

Ultimately, despite my best efforts, nothing appears on the screen other than a CameraHelper object from Three.js. The rest remains pitch black, and both the JavaScript and WebGL consoles show no signs of error. Thank you for your attention and assistance!

Answer №1

Instead of performing the division, you can simply render a full screen quad using only UV coordinates if you are working with a PlaneBufferGeometry:

vec2 uv = gl_FragCoord.xy / uResolution.y;
vec2 vUv = varyingUV;

uv == vUv; //kind of

Your vertex shader could be something like this:

varying vec2 varyingUV;

void main(){
  varyingUV = uv;
  gl_Position = vec4( position.xy , 0. , 1.);
}

If you create a

new THREE.PlaneGeometry(2,2,1,1);
, it will render as a full screen quad.

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

Chromium extensions: Tips for executing a content_script once all document listeners have been activated

Is there a method to execute a chromium extension content_script after window.load and following all other window.load listeners have been activated? I am currently analyzing a website's javascript in an attempt to enhance its functionality. However, ...

Angular's Innovative 3D-esque Carousel Rotation

My goal is to design a pseudo-3d carousel with 5 items, like the example below (and have them rotate around): https://i.sstatic.net/FtcSe.png I came across this fantastic stackblitz as a base, and I've been tinkering with it for hours attempting to ...

What is the best way to validate if fields are blank before sending a message using the button?

<template> <div> <div class="form-group"> <label for="name">First Name</label> <input type="text" class="form-control" v-model="firstName" placeholder="Ente ...

Having trouble getting array push to work in Internet Explorer 5 with JavaScript?

Every time I attempt to populate the select options into the optStored array using array push, the array remains empty. The code works fine in Chrome, Firefox, and Safari but not in Internet Explorer. Is there a workaround for this issue or is it something ...

Exploring the intricacies of using jquery text() with HTML Entities

I am having difficulty grasping the intricacies of the jquery text() function when used with HTML Entities. It appears that the text() function converts special HTML Entities back to regular characters. I am particularly uncertain about the behavior of thi ...

Display dynamically created JavaScript variable in Rails code

I have been working on incorporating links into a google map feature on my website. Each link is supposed to connect to a different venue within the site. To achieve this, I have been using JavaScript and following tutorials provided by Google API. The va ...

Calculating the number of rows in a dynamic jQuery table

I have a table structured like this: <div class="row"> <input type="button" id="btnAddGatePass" value="Add Gate Pass Requester" /> <div class="table-responsive"> <table id="gatePass" class="table table-striped table-ho ...

Adding a model to a canvas in Three.js: A step-by-step guide

Can somebody please assist me? I'm attempting to incorporate my model into a canvas that I've inserted into the HTML document. However, the code below does not seem to be functioning properly: function main() { const container = ...

Round up all the hyperlinks within a paragraph and organize them neatly into a list

Let me present a scenario: <p class="links">Lorem <a href="#">diam</a> nonummy nibh <a href="#">Lorem</a></p> Following that, I have a lineup: <ul class="list"> </ul> How do I achieve this using jQuery? ...

Feathers.js - Incorporating Static Content

Recently, I've been exploring the capabilities of feathers.js for a new project and have been quite impressed with its potential. Intrigued by what it offers, I decided to embark on creating a basic content management system as a way to further my lea ...

Encountering a 404 error when attempting to use the jQuery .load() function with parameters that include periods ('.')

I am attempting to invoke a controller function using the .load() method, but encountering an error which might be caused by the encoding of '.'. The call I am making is as follows: $("#main-content").load( url + "/" + encodeURIComponent(text ...

Rotating the icon in Bootstrap Accordion upon opening

I am trying to customize a Bootstrap 4 accordion by conditional rotating the icon to point up when it is open and back down when closed. I managed to achieve this using CSS, but now I need to implement it conditionally based on active states rather than ev ...

Everything seems to be functioning properly on the local server, but once the media files or players (mp3 and mp4) are uploaded, the background fails to work entirely

function playMusic() { var songs = [ "pump.mp3", "ybwm.mp3", "bb.mp3", ]; var randomIndex = Math.floor(Math.random() * songs.length); var selectedSong = songs[randomIndex]; var audio = new Audio(selecte ...

What are some solutions for ensuring that the dots on a slideshow function automatically?

My attempt to showcase an automated slideshow seems to be partially successful. The images are changing as intended, but the dots below aren't functioning properly. Although clicking on a dot triggers the desired action, I am aiming for all dots to tu ...

The MDBDataTable features header sections at both the top and bottom, but the filters UI seems to be

Currently, I am working with MDBDataTable and encountering an issue with the double heading that appears both on top and bottom of the table. I am unsure how to remove it. The code snippet in question is as follows: There is a function that retrieves and ...

Issues with jQuery autocomplete when using special characters (Norwegian)

On my website in Norway, I am facing an issue with jQuery's autocomplete function. When users type in the Norwegian characters æ, ø, and å, the autocomplete feature suggests words with these characters within them but not ones that start with these ...

guide on launching react with pure javascript

Is it feasible to operate react "straight out of the box" using only JavaScript? In essence, I am seeking a way to utilize react by simply utilizing notepad to create the page (without needing to install and configure node etc.). More specifically - 1) ...

Is there a way I can interact with an HTML control marked as runat="server" using JavaScript

Can someone assist with the code below? <telerik:GridTemplateColumn DataField="JOB_CODE" <EditItemTemplate> <input type="text" ID="JOB_CODETextBox" runat="server" value='<%# Eval("JOB_CODE") %>' readonly="readonly" onclic ...

Best practices for using parent and child methods in Vue applications

I'm exploring the most effective approach to creating a modal component that incorporates hide and show methods accessible from both the parent and the component itself. One option is to store the status on the child. Utilize ref on the child compo ...

Check for weaknesses in your project using NPM audit

While trying to install the npm package to retrieve the node module file, I encountered a total of 184 vulnerabilities (153 low, 1 moderate, 30 high) after running npm install. I am considering running npm audit fix to address these vulnerabilities, but ...