var vertices =[newTHREE.Vector3(1,3,1),newTHREE.Vector3(1,3,-1),newTHREE.Vector3(1,-1,1),newTHREE.Vector3(1,-1,-1),newTHREE.Vector3(-1,3,-1),newTHREE.Vector3(-1,3,1),newTHREE.Vector3(-1,-1,-1),newTHREE.Vector3(-1,-1,1)];var faces =[newTHREE.Face3(0,2,1),newTHREE.Face3(2,3,1),newTHREE.Face3(4,6,5),newTHREE.Face3(6,7,5),newTHREE.Face3(4,5,1),newTHREE.Face3(5,0,1),newTHREE.Face3(7,6,2),newTHREE.Face3(6,3,2),newTHREE.Face3(5,7,0),newTHREE.Face3(7,2,0),newTHREE.Face3(1,3,4),newTHREE.Face3(3,6,4),];
将坐标写入模型
var geom =newTHREE.Geometry();
geom.vertices = vertices;
geom.faces = faces;
geom.computeFaceNormals();//计算表面法线
新建材质
var materials =[newTHREE.MeshLambertMaterial({opacity:0.6, color:0x44ff44, transparent:true}),newTHREE.MeshBasicMaterial({color:0x000000, wireframe:true})];
将模型加入场景
var mesh=THREE.SceneUtils.createMultiMaterialObject(geom, materials);
mesh.children.forEach(function(e){
e.castShadow =true});// mesh.children[0].translateX(0.5);// mesh.children[0].translateZ(0.5);
scene.add(mesh);
完整demo+gui 复制模型控制
<!DOCTYPE html><html><head><title>Example 02.05 - Custom geometry</title><scripttype="text/javascript"src="../libs/three.js"></script><scripttype="text/javascript"src="../libs/stats.js"></script><scripttype="text/javascript"src="../libs/dat.gui.js"></script><style>body{/* set margin to 0 and overflow to hidden, to go fullscreen */margin: 0;overflow: hidden;}</style></head><body><divid="Stats-output"></div><!-- Div which will hold the Output --><divid="WebGL-output"></div><!-- Javascript code that runs our Three.js examples --><scripttype="text/javascript">// once everything is loaded, we run our Three.js stuff.functioninit(){var stats =initStats();// create a scene, that will hold all our elements such as objects, cameras and lights.var scene =newTHREE.Scene();// create a camera, which defines where we're looking at.var camera =newTHREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight,0.1,1000);// create a render and set the sizevar renderer =newTHREE.WebGLRenderer();
renderer.setClearColor(newTHREE.Color(0xEEEEEE,1.0));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled =true;// create the ground planevar planeGeometry =newTHREE.PlaneGeometry(60,40,1,1);var planeMaterial =newTHREE.MeshLambertMaterial({color:0xffffff});var plane =newTHREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow =true;// rotate and position the plane
plane.rotation.x =-0.5* Math.PI;
plane.position.x =0;
plane.position.y =0;
plane.position.z =0;// add the plane to the scene
scene.add(plane);// position and point the camera to the center of the scene
camera.position.x =-20;
camera.position.y =25;
camera.position.z =20;
camera.lookAt(newTHREE.Vector3(5,0,0));// add subtle ambient lighting// var ambientLight = new THREE.AmbientLight(0x494949);// scene.add(ambientLight);// add spotlight for the shadowsvar spotLight =newTHREE.SpotLight(0xffffff);
spotLight.position.set(-40,60,10);
spotLight.castShadow =true;
scene.add(spotLight);// add the output of the renderer to the html element
document.getElementById("WebGL-output").appendChild(renderer.domElement);// call the render functionvar step =0;var vertices =[newTHREE.Vector3(1,3,1),newTHREE.Vector3(1,3,-1),newTHREE.Vector3(1,-1,1),newTHREE.Vector3(1,-1,-1),newTHREE.Vector3(-1,3,-1),newTHREE.Vector3(-1,3,1),newTHREE.Vector3(-1,-1,-1),newTHREE.Vector3(-1,-1,1)];var faces =[newTHREE.Face3(0,2,1),newTHREE.Face3(2,3,1),newTHREE.Face3(4,6,5),newTHREE.Face3(6,7,5),newTHREE.Face3(4,5,1),newTHREE.Face3(5,0,1),newTHREE.Face3(7,6,2),newTHREE.Face3(6,3,2),newTHREE.Face3(5,7,0),newTHREE.Face3(7,2,0),newTHREE.Face3(1,3,4),newTHREE.Face3(3,6,4),];var geom =newTHREE.Geometry();
geom.vertices = vertices;
geom.faces = faces;
geom.computeFaceNormals();//计算表面法线var materials =[newTHREE.MeshLambertMaterial({opacity:0.6, color:0x44ff44, transparent:true}),newTHREE.MeshBasicMaterial({color:0x000000, wireframe:true})];var mesh =THREE.SceneUtils.createMultiMaterialObject(geom, materials);
mesh.children.forEach(function(e){
e.castShadow =true//显示投影});// mesh.children[0].translateX(0.5);// mesh.children[0].translateZ(0.5);
scene.add(mesh);functionaddControl(x, y, z){var controls =newfunction(){this.x = x;this.y = y;this.z = z;};return controls;}var controlPoints =[];
controlPoints.push(addControl(3,5,3));
controlPoints.push(addControl(3,5,0));
controlPoints.push(addControl(3,0,3));
controlPoints.push(addControl(3,0,0));
controlPoints.push(addControl(0,5,0));
controlPoints.push(addControl(0,5,3));
controlPoints.push(addControl(0,0,0));
controlPoints.push(addControl(0,0,3));var gui =newdat.GUI();
gui.add(newfunction(){this.clone=function(){var clonedGeometry = mesh.children[0].geometry.clone();var materials =[newTHREE.MeshLambertMaterial({opacity:0.6, color:0xff44ff, transparent:true}),newTHREE.MeshBasicMaterial({color:0x000000, wireframe:true})];var mesh2 =THREE.SceneUtils.createMultiMaterialObject(clonedGeometry, materials);
mesh2.children.forEach(function(e){
e.castShadow =true});
mesh2.translateX(5);
mesh2.translateZ(5);
mesh2.name ="clone";
scene.remove(scene.getChildByName("clone"));
scene.add(mesh2);}},'clone');for(var i =0; i <8; i++){
f1 = gui.addFolder('Vertices '+(i +1));
f1.add(controlPoints[i],'x',-10,10);
f1.add(controlPoints[i],'y',-10,10);
f1.add(controlPoints[i],'z',-10,10);}render();functionaddCube(){}functionrender(){
stats.update();var vertices =[];for(var i =0; i <8; i++){
vertices.push(newTHREE.Vector3(controlPoints[i].x, controlPoints[i].y, controlPoints[i].z));}
mesh.children.forEach(function(e){
e.geometry.vertices = vertices;
e.geometry.verticesNeedUpdate =true;
e.geometry.computeFaceNormals();});// render using requestAnimationFramerequestAnimationFrame(render);
renderer.render(scene, camera);}functioninitStats(){var stats =newStats();
stats.setMode(0);// 0: fps, 1: ms// Align top-left
stats.domElement.style.position ='absolute';
stats.domElement.style.left ='0px';
stats.domElement.style.top ='0px';
document.getElementById("Stats-output").appendChild(stats.domElement);return stats;}}
window.onload = init
</script></body></html>