From the hue of mossy green to the fiery shade of ruby red,

I managed to create a simple green circle using THREE.Shape.

Now, I am interested in changing the color of the circle so that it transitions from green in the middle to red at the border.

Although I found an example on this website, I'm struggling to figure out how to adapt it to my own project.

Here's the code snippet responsible for creating the circle:

var arcShape = new THREE.Shape();
arcShape.absarc(100, 100, circleRadius, 0, Math.PI * 2, false);

var geometry = new THREE.ShapeGeometry(arcShape);
var material = new THREE.MeshBasicMaterial({ color: 0x00ff11, overdraw: 0.5, side: THREE.DoubleSide });

var mesh = new THREE.Mesh(geometry, material);
mesh.position = CirclePosition;
mesh.rotation.set(Algorithms.ConvertDegreesToRadians(-90), 0, 0);

Answer №1

One alternative method is to utilize vertex colors for the solution

let redColor = new THREE.Color (0.9, 0.0, 0.0);
let greenColor = new THREE.Color (0.0, 0.9, 0.0);

material = new THREE.MeshBasicMaterial({ vertexColors: THREE.VertexColors});

geometry = new THREE.CircleGeometry(100, 10, 0, 3);

let maxFaces = geometry.faces.length;
for (let face=0; face<maxFaces; face++) {
    geometry.faces[face].vertexColors[0] = redColor;
    geometry.faces[face].vertexColors[1] = redColor;
    geometry.faces[face].vertexColors[2] = greenColor;
}

Answer №2

The reference you mentioned doesn't quite fit the current context. The most straightforward approach would be to utilize an image instead. If you're familiar with utilizing a technique specific to the WebGL renderer, I have created a JSFiddle that showcases a basic example of a GLSL shader: http://jsfiddle.net/5rtyu/

The crucial element lies within the fragment shader:

varying vec2 vUv;
varying vec3 vPosition;
uniform float radius;
void main() {
    vec3 center = vec3(150.0, 150.0, 0.0);
    float greenAmount = max(0.0, min(1.0, distance(vPosition, center) / radius));
    gl_FragColor = vec4(0.0, greenAmount, 1.0 - greenAmount, 1.0);
}

Another viable option would be to execute this functionality entirely in JavaScript using vertex colors, making it compatible with the CanvasRenderer as well; you'd simply need to determine the location of the central vertex.

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

Adjusting the height of content in Angular Material 2 md-tab

I've recently started working with angular material and I'm facing an issue while trying to implement md-tab into my application. The problem is that I can't seem to style my tab content to take up the remaining height of the screen. Could s ...

Having trouble figuring out how to load images into a div based on the current page location?

Looking for a solution to my problem - I have a navigation bar with a fixed div (A) on the far right side. The nav bar remains at the top of the page. There are 6 sections in the body of the page, each being 1200px tall divs. What I want is for a different ...

Each time, vue-router instantiate a fresh Component instance

I have encountered a frustrating issue with vue-router that keeps bothering me. Every time I navigate between routes, a new instance of the component is created and the old instances remain active in the background! My expectation was that when I switch t ...

How can you create a basic slideshow without relying on jQuery to cycle through images?

Imagine you have a div containing 3 images. Is there a way to build a basic slideshow that smoothly transitions between the images, showing each one for 5 seconds before moving on to the next one and eventually looping back to the first image without rely ...

typescript scrolling location

In my Angular UI code, I have a component class that includes the following structure: app.component.html //... <div class="banner"> <p-dialog [(visible)]="displayCOI" styleClass="coiDialog" [contentStyle]=" ...

The chosen selection automatically deactivates the checkbox with a matching value

I am looking for a solution where selecting an option will automatically disable the checkbox with the corresponding value. The existing methods I have come across are static and rely on hardcoded values. <select name="pickOne" id="pickOn ...

Exploring ways to retrieve global variables within a required() file in Node.js

Imagine having 2 files: main.js, and module.js: //main.js const myModule = require('./module'); let A = 'a'; myModule.log(); //module.js module.exports = { log() { console.log(A); } } After trying to call myModule.log, ...

The absence of HTML and CSS is preventing the inclusion of an Ajax image viewer, back button, and

Currently, I am experimenting with jQuery and JavaScript to create an ajax overlay image viewer for a PHP website. After adding this code snippet at the end of the 'gallery page', I can successfully open the viewer and navigate using the next and ...

JavaScript API for Tableau

Could you please clarify the functions described below? newViz = createTableauViz(containerDiv, url, options); function listenForMarkSelection() { newViz.addEventListener(tableau.TableauEventName.MARKS_SELECTION, handleMarksSelection); } funct ...

Guide on parsing a JavaScript file and converting the default export module to JSON with Node.js

What I'm trying to accomplish in my Node.js project is reading a sample.js file with ES Module syntax and extracting the default export from it. sample.js import foo from "foo"; const bar = [ { name: "Homer", }, { n ...

Sequencing the loading of resources in AngularJS one after the other by utilizing promises

While working in a service, my goal is to load a resource using $http, store it in a variable, load a child resource and store that as well. I understand the concept of promises for this task, but I am struggling with how to properly implement it in my cod ...

"Using JavaScript to extract a portion of the URL and perform a redirect

I am attempting to extract a specific part of the URL and then redirect to that particular section. Essentially, what I want is for a script to generate a link that will be opened. This link would appear as . My goal now is to extract the portion of the ...

What are the steps to integrating D3js into a WordPress website?

After installing Wordpress and the d3js plugin, I'm looking for the most effective way to upload data in order to create and display graphs on my website. Should I directly upload csv files? And if so, where should I store them? If I prefer storing ...

Why is $scope.$watch('$destroy') being called in Angular UI Router 1.0 even when not leaving the state?

Take a look at the plunker here. My understanding is that $scope.$watch('$destroy') should be invoked when $scope is on the verge of being destroyed. In the provided example, it seems that upon entering a state, the $scope.$watch('$destroy ...

Employing the `instanceof` operator on instances generated by constructors from complex npm dependencies

Context: In one of my npm modules, I've implemented error handling code with a custom error type called CustomError: function CustomError () { /* ... */ } CustomError.prototype = Object.create(Error.prototype); CustomError.prototype.constructor = Cu ...

Set up authentication within a separate AngularJS module

I am struggling with how to develop a standalone login page for the BlurAdmin template found on GitHub. The main structure of the template is based on index.html, which includes header, footer, sidebar, and loads pages as templates using ui-view. However, ...

What could be causing my completed torrents to sometimes not be saved to my disk?

Currently, I am working on developing a small torrent client using electron and webtorrent. Although everything appears to be functioning correctly initially, there is an issue where sometimes the resulting files from a finished download are not being save ...

The Art of jQuery Data Processing

I'm currently working on extracting data from a form submission. Below is the code I've been using. function handleSubmission() { $("#questionForm").submit(function() { $.post("SubmitQuestion.php", $("#questionForm").serialize()).done(functi ...

Looking for an API to retrieve random quotes and images from a website?

Greetings, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS along with the basics of JS. Level of Expertise: Beginner During my exploration, I stumbled upon this intriguing website - . It stands out a ...

The React-native application initialized using npx create-react-app encountered an error and could not launch

Hello there, A couple of months back, I developed an app using create-react-app. However, when I tried to run it with npm start, I encountered the following error message: react-scripts start It seems like there's an issue with the project depende ...