"Exploring the world of ThreeJS Hemis

Just a quick question - I'm attempting to create a 3D hemisphere in ThreeJS.

I've managed to make a hemisphere using the Circle or CircleBuffer geometry, but there's an issue with the flat side being empty.

I've tried adding a circle to fill the gap, but it hasn't worked out so far.

I believe I just need to instruct the geometry to add another face there, but I'm not sure how to do it!

Answer №1

Creating a 5 kopeikas model: Hemisphere + Circle

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.show()
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 5, 8);
const renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const controls = new THREE.OrbitControls(camera, renderer.domElement);

const light = new THREE.DirectionalLight(0xffffff, 0.5);
light.position.setScalar(10);
scene.add(light);
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
scene.add(new THREE.AxesHelper(5));

const radius = 3;
const radialSegments = 32;
const material = new THREE.MeshStandardMaterial({
  color: "blue"
});
const hemiSphereGeom = new THREE.SphereBufferGeometry(radius, radialSegments, Math.round(radialSegments / 4), 0, Math.PI * 2, 0, Math.PI * 0.5);
const hemiSphere = new THREE.Mesh(hemiSphereGeom, material);
const capGeom = new THREE.CircleBufferGeometry(radius, radialSegments);
capGeom.rotateX(Math.PI * 0.5);
const cap = new THREE.Mesh(capGeom, material);
hemiSphere.add(cap);

scene.add(hemiSphere);

render();

function render() {
  requestAnimationFrame(render);
  renderer.render(scene, camera);
}

Answer №2

Here's how you can achieve it:

const sphere = new THREE.SphereGeometry(1, 32, 32, 0, 2*Math.PI, 0, Math.PI/2);

Answer №3

//Snippet to transform a sphere into a hemisphere
var sphereGeometry = new THREE.SphereBufferGeometry(1,16,16);
let vertices = sphereGeometry.attributes.position.array
for(var index=0;index<vertices.length;index+=3){
  if(vertices[index+1]<0)
      vertices[index+1]=0;
}
sphereGeometry.computeFaceNormals();
sphereGeometry.computeVertexNormals();
//sphereGeometry.verticesNeedUpdate = true;
//See the working code snippet below...

var renderer = new THREE.WebGLRenderer();
var width = 300;
var height = 200;
    renderer.setSize(width, height);
    document.body.appendChild(renderer.domElement);
    
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(
        45,     // Field of view
        width/height,    // Aspect ratio
        0.1,        // Near
        10000       // Far
    );
    camera.position.set(15, 10, 15);
    camera.lookAt(scene.position);
controls = new THREE.OrbitControls(camera, renderer.domElement);

var light = new THREE.PointLight(0xFFFF00);
light.position.set(20, 20, 20);
scene.add(light);
var light1 = new THREE.AmbientLight(0x808080);
light1.position.set(20, 20, 20);
scene.add(light1);
    var light2 = new THREE.PointLight(0x00FFFF);
light2.position.set(-20, 20, -20);
scene.add(light2);
var light3 = new THREE.PointLight(0xFF00FF);
light3.position.set(-20, -20, -20);
scene.add(light3);
var sphereGeometry = new THREE.SphereBufferGeometry(5,16,16);
let vertices = sphereGeometry.attributes.position.array
for(var index=0;index<vertices.length;index+=3){
  if(vertices[index+1]<0)
      vertices[index+1]=0;
}
sphereGeometry.computeFaceNormals();
sphereGeometry.computeVertexNormals();

var material = new THREE.MeshLambertMaterial({ color: 0x808080 });
var mesh = new THREE.Mesh(sphereGeometry, material);
scene.add(mesh);
renderer.setClearColor(0xdddddd, 1);

(function animate() {
    requestAnimationFrame(animate);
    controls.update();
    renderer.render(scene, camera);
})();
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f08498829595b0c0dec9c4dec0">[email protected]</a>/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99edf1ebfcfcd9a9b7a0adb7a9">[email protected]</a>/examples/js/controls/OrbitControls.js"></script>

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

What are the steps to develop a React iOS app that can be used offline?

Exploring the possibility of creating an offline React app (not React-Native) for iOS deployment. I'm working on a learning project and prefer to avoid dealing with React-Native. Wanting to leverage my web skills to develop an iOS app using only React ...

import all mongoose models in the express app.js

Every time I create a model in my express app's app.js, I find myself having to include the model by writing out the specific path. require('./models/Users') require('./models/Projects') Is there a shortcut or more streamlined wa ...

What is the best way to include a property with a name in quotes to an object after it has already been declared?

Is there a way to include a property with a quoted name to an object after its declaration? Here's a simplified scenario: var Obj = {} Instead of: Obj.dog = "Woof!"; I want to achieve: Obj."dog" = "Woof!"; Similar to: var Obj = { "dog" : " ...

Embedding the view in a partial with Handlebars.js: A comprehensive guide

It might seem a bit perplexing, but allow me to explain the issue. In my NodeJs web application, I am utilizing express-handlebars. To collect user input through forms, let's consider having two distinct forms in two different views: 1) Login and 2) ...

Argument-based recursive method

Why is the script only printing 'Hello' and not 'Good bye' as well, even though both are passed as arguments on the function call? What could be causing this issue? Note: The script used to work before. It stopped working after adding ...

Engage in Step 2: Receive a response from the server via AJAX request

I've been utilizing AJAX in conjunction with the Play 2 framework to send requests and execute actions on the server side. Learn how to make an AJAX request with a common button in Play 2.x Troubleshooting Jquery and Play Framework 2 JavaScript rout ...

Leveraging traditional code methods within AngularJs

With a multitude of older javascript functions for sign up/sign in operations through parse.com, I am considering integrating AngularJS for improved routing and other advantages. Is it feasible to establish an angular stack and encapsulate these functions ...

Reduce XSLTProcessor output by 50%

I have a persistent problem (from my perspective, at least). Every time I use XSLTProcessor.transformToFragment, the output is consistently halved compared to the input. For example, when I receive 200 entries in an XML file as a response from a webservi ...

The media print is not displaying correctly when attempting to print with JavaScript

I am attempting to print a div using the following javascript code: var divToPrint = document.getElementById(curid); var newTab = window.open('', 'Print-Window'); newTab.document.open(); newTab.document.write('<h ...

Display a hidden div on hover using JQUERY

How can I make a hover popup appear when submitting a form, and have it disappear only when the mouse is out of both the popup div and the submit button? Currently, the hover popup shows up but disappears when entering the popup. Can someone assist me in r ...

Adding JQuery elements to the map pane

I recently decided to upgrade one of my projects to use the Google Maps API v3 instead of v2. In v2, I used the following code to append a div to the map pane: $("#marker_popup").appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE)); Is there a straightforward ...

A comprehensive guide on personalizing Bootstrap 4 tooltips to suit your specific needs

I would like to customize the tooltip in Bootstrap 4 based on the screenshot provided below: https://i.stack.imgur.com/wg4Wu.jpg <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta chars ...

Integrate birthday validation using the validate.js library in a React application

After reading the article linked here, I incorporated it into my app. My next step is to include a date validation to ensure that the person is over 18 years old. I am currently struggling with understanding where and how to incorporate the parse and for ...

The AJAX Modal Popup Extender is not being displayed due to an error with the undefined Sys

My ASP.Net WebForms project, developed using C# in Visual Studio 2013, involves the utilization of an AJAX Popup Extender. I have integrated AjaxControlToolKit and AjaxControlToolkitStaticResources through NuGet. However, recently the popup form has ceased ...

`Arc canvas countdown timer`

My countdown timer created using canvas is functioning well, but I am encountering some issues: I would like to align the circle representing seconds with the circles for minutes, hours, and days similar to this demo. Please ensure running the code snipp ...

How can I delete the Location Box from Google Maps that appears in the top left corner?

My goal is to display all warehouse locations from Google Maps on my website by utilizing Google Map's iframe: <iframe src="http://maps.google.com/maps?q=118+Lamington+Rd.+–+Bateman+Student+Center,+Branchburg,+NJ+08876&amp;output=embed" fram ...

Learn how to toggle multiple class elements with jQuery, and how to hide the ones that have

This is an example of my HTML code: <h4 class="list-group-item-heading"> @Model.Customer.FirstName @Model.Customer.LastName wrote: <span class="right"><span class="icon-file padding-right link"></span></span> </h4& ...

Is it possible to execute in a specific context using npm?

I am seeking to execute npm scripts that are executable by VuePress. For instance, I have VuePress installed and would like to run the command vuepress eject. Although I can access vuepress in my scripts, there is no specific script for eject: "scr ...

When utilizing jQuery, I implemented this code but it does not display anything. I am unsure of the error within the code

When using jQuery, I implemented the following code snippet but it doesn't display anything. What could be causing this issue? // $.ajax({ // URL:"https://dog.ceo/api/breeds/image/random", // method:"GET", // ...

"Is there a way to determine the height of an inline element, such as a span element,

var mySpan=document.getElementById("mySpan"); alert(mySpan.height); <span id="mySpan" style="line-height:200px;">hello world</span> However, the result I receive is undefined. This is because Javascript can only retrieve the he ...