"Latest version of ThreeJS (r160) utilizes ShaderMaterial to render textures

I've been attempting to incorporate a texture into this shader, but it's displaying as black. I suspect there's a small detail I'm overlooking, despite my search yielding no relevant information for ThreeJS version r160. The shader code currently remains default, although I intend to personalize it.

Using a diffuse color yields the desired outcome. You can view the page at mfzcreations.com/HRD

Edit: Upon testing the material on a basic box mesh, the result mirrors what is seen on the field mesh.

Here's the JS code:

let dirtTexture = new THREE.TextureLoader().load("textures/fieldDirtTexture.jpg");
let standard = THREE.ShaderLib['standard'];
    let dirtMaterial = new THREE.ShaderMaterial({
    lights: true,
    uniforms: THREE.UniformsUtils.clone( standard.uniforms ),
    defines: {
        PHYSICAL: true
    },
    vertexShader: document.getElementById( 'dirtVS' ).textContent,
    fragmentShader: document.getElementById( 'dirtFS' ).textContent
});

dirtMaterial.map = dirtTexture;

// dirtMaterial.uniforms.diffuse.value = new THREE.Color( 0x91773d );
dirtMaterial.uniforms.roughness.value = 1.0;
dirtMaterial.uniforms.metalness.value = 0.0;
dirtMaterial.uniforms.ior.value = 0.0;

This is the shader code:

<script id="dirtVS" type="GLSL">
        #define STANDARD

        varying vec3 vViewPosition;

        #ifdef USE_TRANSMISSION

            varying vec3 vWorldPosition;

        #endif

        // Remaining shader code omitted for brevity 
    </script>

<script id="dirtFS" type="GLSL">
        // Shader code continued from above...
        
    </script>

Answer №1

Well, the approach works, but it does feel a bit repetitive. I believe this information could come in handy for others down the line, so let me share the solution. Essentially, you need to specify the map value in two different ways.

let soilTexture = new THREE.TextureLoader().load("textures/soilTexture.jpg");
let standard = THREE.ShaderLib['standard'];
let soilUniforms = THREE.UniformsUtils.clone( standard.uniforms );

// soil shader
let soilMaterial = new THREE.ShaderMaterial({
    lights: true,
    uniforms: soilUniforms,
    defines: {
        PHYSICAL: true
    },
    vertexShader: document.getElementById( 'soilVS' ).textContent,
    fragmentShader: document.getElementById( 'soilFS' ).textContent
});

soilMaterial.uniforms.map.value = soilTexture;
soilMaterial.map = soilTexture;
soilMaterial.uniforms.roughness.value = 1.0;
soilMaterial.uniforms.metalness.value = 0.0;
soilMaterial.uniforms.ior.value = 1.0;

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

Exploring an array to retrieve a specific value

Can someone assist me? I am looking to create a JavaScript function that follows the structure of the code example below. Unfortunately, I do not have enough programming skills to develop a functional solution from scratch. The goal is to input a value, ...

In TypeScript, how does "number" differ from "Number"?

Within the realm of TypeScript, there exist two distinct variations of the "number" type. The first is denoted as lowercase number, whereas the second is represented in uppercase as Number. Attempting to display number results in a compiler error: console. ...

Re-establishing the socket channel connection in a React application after a disconnection

There are several solutions to this issue, but none of them seem to be effective for me. The existing solutions are either outdated or do not meet my needs. I am facing a problem where I have a large amount of data being transferred from the server to the ...

After generating the token, where is the appropriate place to define the authorization header?

I am currently working on implementing a security system based on tokens. The challenge I am facing is determining the appropriate location to set the authorization header after generating it, in order to validate it across all of my different routes. Belo ...

Success callbacks parsed from AJAX requests

When dealing with a Backbone object and making an AJAX call to save it, I often wonder about the different ways the success callback can be handled. Sometimes, I see a generic success: function (data) { console.log(data); Other times, it's more spec ...

Angular JS appears to be failing to properly establish values in the Dropdownlist

I have a project requirement to connect a dropdownlist with MVC and angular JS. Here is my attempt: var app1 = angular.module('Assign', []) app1.controller('SAPExecutive_R4GState', function ($scope, $http, $window) { // alert(UMS ...

Vue.js known as javascript hooks

I've been working on connecting Vue.js with velocity.js. Following a guide, I found an example that doesn't use a named transition. Currently, my transition code looks like this: <transition name="collapse"> https://v2.vuejs.org/ ...

Guide to incorporating a jade file into a node.js application after executing an Ajax request

While working on my node.js application, I encountered an issue with loading a new HTML file using Ajax on a button click. Despite everything else working perfectly, the new HTML file was not being loaded. This is how I am making the ajax call in my main. ...

It's possible for anyone to enhance the appearance of the Download button by adding styles without compromising its functionality

Looking to enhance the style of the Download button as it appears too formal. Seeking assistance in adding some button styles to make it more stylish. The code is correct, just aiming to give the Download button a trendy look with specified styles. ...

Caution: It is important for each child within a list to have a distinct "key" prop when using react-input

I'm currently facing an issue with the following code snippet: {array.map((item) => ( <> <input key={item.id} type="checkbox" /> ...

Organizing data into clusters with the three.js cutting plane

After successfully identifying all intersection points between the object and plane, inspired by this helpful example, I am now faced with the task of connecting these points in a more strategic way. I attempted to connect them based on distance, but unfor ...

Array of class properties in Typescript

Is there a way to iterate through the properties of a class or a new object instance of that class when none of the properties are set? When I use Object.keys(), it returns an empty array because no properties have been initialized. How can I achieve this ...

apply a visible border to the item that is clicked or selected by utilizing css and vue

I have a list of items that I want to display as image cards with an active blue border when clicked. Only one item can be selected at a time. Below is the code snippet: Template Code <div class="container"> <div v-for="(obj ...

What is preventing this jQuery selector from locating my form?

My form setup looks a little something like this: <div id="zxRegisterDiv" style="display: none; border: 1px solid;"> <style type="text/css"> label.zxLabel{ display: inline-block; width: 100px; } ...

What method could I use to verify that my Angular 2+ login function has been invoked successfully?

Can anyone provide guidance on how to effectively verify if a function has been executed within the interaction of a component and a service? I welcome any insights or suggestions that can help me address this challenge! ...

Issue: JavaScript code is not functional when operating on data obtained through an Ajax

I am experiencing an issue with my JavaScript code (abc.js) that is designed to run on a div element with the class of "one". While the script works perfectly fine on elements already present within the div in the HTML code, it fails to execute on data tha ...

Invoking a HTTP request in a web service using the link method

Upon the initial page load, a successful RESTful call is made to retrieve data. However, when clicking on the left navigation links that display this data, another RESTful call needs to be made. I am attempting to achieve this functionality within the link ...

Creating animated effects through Javascript and CSS triggered by user events

As a beginner in javascript and CSS, I am experimenting with creating a simple animation that adjusts the transparency of an image when triggered by an event. However, I am facing an issue where the animation only works every other time the function is cal ...

Encountering a problem when trying to pass a PHP array variable to a JavaScript function

I need some help with passing an array and input value from a form to a JavaScript function. The array, $quotes, contains posts. <form method="post"> <p>Search:<input type="text" name="qSearch" id="qSea ...

Conceal any elements designated by a particular class and reveal them based on their corresponding ID

I have utilized jQuery's hide() function to initially hide all elements of a specific class when the page loads. My goal is to make individual elements visible again based on their unique IDs when a corresponding link is clicked. In total, there are ...