ThreeJS半球

时间:2018-07-25 00:28:14

标签: javascript three.js

这很可能是一个非常简单的问题,因此提前致歉。

我正在尝试在ThreeJS中制作3D半球。

我可以使用Circle或CircleBuffer几何体在外部创建一个半球(以防万一,如果我使用了错误的单词,一个球体,切成两半!),但是由于缺少更好的单词,这使得平坦的侧面为“空”

我试图用圆圈填充它,但是没有运气。

我确定这只是添加一些内容以告知几何图形在其中添加另一个面的一种情况,但我不知道如何做!

3 个答案:

答案 0 :(得分:1)

//Here's some code to modify a sphere into a hemisphere
var sphereGeom = new THREE.SphereGeometry(1,16,16);
for(var i=0;i<sphereGeom.vertices.length;i++){
  var v = sphereGeom.vertices[i];
  if(v.y<0)
      v.y=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.SphereGeometry(5,16,16);
for(var i=0;i<sphereGeom.vertices.length;i++){
  var v = sphereGeom.vertices[i];
  if(v.y<0) v.y=0;
}
sphereGeom.verticesNeedUpdate = true;
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://threejs.org/build/three.min.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>

答案 1 :(得分:1)

您可以

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

答案 2 :(得分:1)

我的5个kopeikas。半球+圆:

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://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>