Is Three.js capable of creating procedurally-generated bump or specular maps?

Using Three.js, it is common practice to utilize external images for bump maps and specular maps:

var specular = THREE.ImageUtils.loadTexture("path/to/file.png");
var myMaterial = new THREE.MeshPhongMaterial();
myMaterial.specularMap = specular;

However, is there a way to generate bump maps or specular maps procedurally? Perhaps you want to create a textured surface randomly without relying on an external file. Can a function be used to dynamically create these maps instead of using pre-existing images?

Answer №1

Creating a captivating visual effect is easier than you think! Check out this fiddle that demonstrates how:

http://jsfiddle.net/zs1bzkab/

generateNoise = function(opacity,canvas) {
   var
   x, y,
   number,
   opacity = opacity || .2;
     ctx = canvas.getContext('2d');
   for ( x = 0; x < canvas.width; x++ ) {
      for ( y = 0; y < canvas.height; y++ ) {
         number = Math.floor( Math.random() * 60 );

         ctx.fillStyle = "rgba(" + number + "," + number + "," + number + "," + opacity + ")";
         ctx.fillRect(x, y, 1, 1);
      }
   }
}

var mesh;
var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera(50, 500 / 400, 0.1, 1000);
camera.position.z = 10;

var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(500, 400);
document.body.appendChild(renderer.domElement);

var canvas = document.createElement("canvas");

canvas.width = 400;
canvas.height = 400;

generateNoise(.1, canvas);

var texture = new THREE.Texture(canvas);
texture.needsUpdate = true;

var geometry = new THREE.SphereGeometry(3, 50, 50, 0, Math.PI * 2, 0, Math.PI * 2);
var material = new THREE.MeshPhongMaterial({ map: texture, bumpMap: texture });

var light = new THREE.PointLight( new THREE.Color("rgb(255,70,3)"), 2.5);
var light2 = new THREE.PointLight( new THREE.Color("rgb(255,15,255)"), 4);
light.position.set( 0, -100, 800 );
light2.position.set( 50, 50, 900 );
mesh = new THREE.Mesh(geometry, material);

scene.add ( light );
scene.add ( light2 );
scene.add(mesh);

var render = function () {
    requestAnimationFrame(render);
    mesh.rotation.y += 0.01;
    renderer.render(scene, camera);
};

render();

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

Creating a new window using JavaScript with custom settings

Is it possible to create a window that opens when double clicking an icon on the desktop? I want this window to have specific dimensions and no menu bar or scrollbar. This is what my current code looks like: document.onload(openWin()); function openWin( ...

Guide to generating and executing a file in node.js

After installing the newest version of node.js, I verified it in the command prompt by running node-v and npm-v commands. Now, I'm looking to learn how to create and execute files using node.js. Can someone provide guidance on this process? ...

Error occurred due to an invalid element type with the imported React component

Using a component imported from an npm package in two different apps has resulted in unexpected behavior. In one app, the component functions perfectly as expected. However, in the other app, an error is raised: Element type is invalid: expected a string ...

Instructions for incorporating a glTF model into the environment:

I'm having trouble adding a 3D object to my scene. An error occurred: Uncaught TypeError: Class constructor ol cannot be invoked without 'new' at new GLTFLoader The main line causing the error is let loader = new THREE.GLTFLoader(); I&apo ...

The ng-change directive in Angular is failing to trigger for checkboxes

I am facing an issue with my code. The ng-change event of individual checkboxes is not triggered when I click the Select All button, but it works fine when I select them individually. I want the itemChecked method to be called when the Select All button is ...

Chrome version 83 encounters an issue preventing file uploads

I recently updated my Chrome browser to version 83 and encountered some issues with certain forms that utilize an "Ajax upload" component. Despite researching the problems associated with this new version (https://developers.google.com/web/updates/2020/05/ ...

Creating a reusable anonymous self-invoking function

Here is a function that I am working with: (function(e, t) { var n = function() { //code, code, code }; //code, code, code e.fn.unslider = function(t) { //code, code, code }; })(jQuery, false) To execute this function, I have impleme ...

Steps to remove a script upon clicking a button?

On my website, I have integrated a plugin called manychat using a <script>. However, when I click on the Drawer Cart, the manychat symbol overlays over the checkout button, which is not visually appealing. Is it possible to unload this script when ...

The loading GIF in jQuery is malfunctioning when displayed simultaneously on multiple div elements

I am currently working on my Laravel dashboard's blade to showcase various statistics. These stats will be updated whenever the date picker is changed. Below is the code for my date picker: <div class="row"> <div class=&qu ...

Updating elements across multiple arrays within a single document in MongoDB efficiently

I've encountered an issue while trying to update elements of multiple arrays using $push with comma separated values. When I attempt to include both arrays in the update like this, it results in an error. How can I achieve this? var conditions = { so ...

Export a specifically designed object from a module using Python

When working with node.js in JavaScript, you can set module.exports = 13; in a file called module.js, and then import it elsewhere using x = require("module.js");. This will directly assign the value of 13 to variable x. This method is useful when a modul ...

What methods are available for utilizing "open as" with commands in the shell?

After setting up a folder called my-express-server, I am eager to launch it directly from the shell using Visual Studio Code. To achieve this, I came across the command start [name of the file] -a [the specific path of the program I intend to use to open ...

Updating the values of parent components in Vue.js 3 has been discovered to not function properly with composite API

Here is a Vue component I have created using PrimeVue: <template lang="pug"> Dialog(:visible="dShow" :modal="true" :draggable="false" header="My Dialog" :style="{ width: '50vw' }" ...

Issue with Bootsrap 4 Dropdown Menu Displaying Incomplete Links

There seems to be an issue with the Bootstrap 4 dropdown menu not displaying all the links. Please refer to the image below: https://i.sstatic.net/ZS2t2.png The dropdown menu is not showing beyond the red band. I can't seem to figure out what I&apos ...

Similar to the reference of $(this) in jQuery, there is a similar concept in Vue.js

When working with jQuery, the use of $(this) allows for accessing elements without relying on classes or ids. How can we achieve a similar outcome in Vue.js? ...

Creating HTML output using an input checkbox in combination with JavaScript

I am creating a dynamic string to be inserted into a div using JavaScript. The issue I am facing involves the onclick attribute of an input checkbox. I want to pass a unique id value with each click of the checkbox. Below is the code snippet I am currently ...

I am facing an issue where new tags are not being created when I pre-fill values in the tags input field on Bootstrap

When working on the Product Edit Page, I successfully displayed the old data that was selected previously. However, I am facing an issue with adding new tags. The new tags do not appear when I press enter or space after typing in the input field. It is pos ...

Adding roles in ReactJS: A step-by-step guide

I am looking to enhance the admin's roles upon login to enable the creation of new users. Unfortunately, I am uncertain on how to add these roles and make them functional. Below is the code I have: Login.js class Login extends Component { cons ...

Scrolling on a div can be used to create a looping magnification or scaling

Is there a way to create a seamless loop between #box2 and #box using this fiddle? I want them to reveal themselves in a continuous loop with infinite scroll. Each time one box fills the frame, the next one should appear in the distance, repeating indefini ...

What is the best way to combine a collection of strings and HTML elements in a React application?

I am facing a challenge with my list of names. I typically use .join to add commas between each name, but one specific item in the list needs to display an icon of a star next to it based on a certain condition. The issue is that using join turns everyth ...