Alex Cormack-Widdop
Alex Cormack-Widdop

Reputation: 23

ThreeJS Hemisphere

This is quite possibly a very simple question, so apologies in advance.

I’m trying to make a 3D hemisphere in ThreeJS.

I can create a hemisphere (in case I’m using the wrong word, a sphere, cut in half!) outer using the Circle or CircleBuffer geometry, but this leaves the flat side ‘empty’ for lack of a better word.

I have tried to fill this with a circle, but no luck.

I'm sure its just a case of adding something to tell the geometry to add another face there but I have no idea how!

Upvotes: 1

Views: 2125

Answers (3)

manthrax
manthrax

Reputation: 5016

//Here's some code to modify a sphere into a hemisphere
var sphereGeom = new THREE.SphereBufferGeometry(1,16,16);
let verts = sphereGeom.attributes.position.array
for(var i=0;i<verts.length;i+=3){
  if(verts[i+1]<0)
      verts[i+1]=0;
}
sphereGeom.computeFaceNormals();
sphereGeom.computeVertexNormals();
//sphereGeom.verticesNeedUpdate = true;
//Working snippet is below...

var renderer = new THREE.WebGLRenderer();
var w = 300;
var h = 200;
    renderer.setSize( w,h );
    document.body.appendChild( renderer.domElement );
    
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(
        45,     // Field of view
        w/h,    // 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 sphereGeom = new THREE.SphereBufferGeometry(5,16,16);
let verts = sphereGeom.attributes.position.array
for(var i=0;i<verts.length;i+=3){
  if(verts[i+1]<0)
      verts[i+1]=0;
}
sphereGeom.computeFaceNormals();
sphereGeom.computeVertexNormals();

var material = new THREE.MeshLambertMaterial( { color: 0x808080 } );
var mesh = new THREE.Mesh( sphereGeom, 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/[email protected]/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>

Upvotes: 0

prisoner849
prisoner849

Reputation: 17586

My 5 kopeikas. Hemisphere + circle:

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

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

var 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));

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

scene.add(hemiSphere);

render();

function render() {
  requestAnimationFrame(render);
  renderer.render(scene, camera);
}
body {
  overflow: hidden;
  margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>

Upvotes: 1

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

You can do

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

Upvotes: 1

Related Questions