three.js實現(xiàn)圓柱體
更新時間:2018年12月30日 11:10:29 作者:你比誰都明白
這篇文章主要為大家詳細(xì)介紹了three.js實現(xiàn)圓柱體的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了three.js繪制圓柱體的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>圓柱體</title>
<style>
#canvas{
width:1100px;
height:600px;
border:1px solid;
}
</style>
<script type="text/javascript" src="js/three.js"></script>
<script>
// 渲染器
var renderer;
function init_renderer(){
width = document.getElementById("canvas").clientWidth;
height = document.getElementById("canvas").clientHeight;
renderer = new THREE.WebGLRenderer({ //生成渲染對象
antialias : true //去鋸齒
});
renderer.setSize(width,height);//設(shè)置渲染的寬度和高度;
document.getElementById("canvas").appendChild(renderer.domElement);
renderer.setClearColor(0xEEEEEE,1);//設(shè)置渲染的顏色;
}
// 場景
var scene;
function init_scene(){
scene = new THREE.Scene();
}
// 圓柱體
var cylinder;
function init_cylinder(){
var cylinder = new THREE.CylinderGeometry(80,50,300,50,50);
var texture = THREE.ImageUtils.loadTexture("textures/2.jpg",null,function(t)//圖片地址可使用本地,同根目錄下文件夾即可
{
});
var material = new THREE.MeshLambertMaterial({map:texture}); //材料
cube = new THREE.Mesh(cylinder,material);
cube.position.set(0,0,5); //設(shè)置幾何體的位置(x,y,z)
scene.add(cube);
}
// 相機(jī)
var camera;
function init_camera(){
// camera = new THREE.PerspectiveCamera(100,width/height,1,10000); //透視相機(jī)
camera = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 1, 1000) //正投影相機(jī)
// (可視角度,可視范圍的長寬比,相對于深度剪切面的近的距離 必須為正數(shù),相對于深度剪切面的遠(yuǎn)的距離 必須為正數(shù))
camera.position.x =600
camera.position.y = 100;
camera.position.z = 100;
camera.up.x = -2;//設(shè)置相機(jī)的上為「x」軸方向
camera.up.y = 2;//設(shè)置相機(jī)的上為「y」軸方向
camera.up.z = 0;//設(shè)置相機(jī)的上為「z」軸方向
camera.lookAt({x:0,y:0,z:0}); //設(shè)置視野的中心坐標(biāo)
}
// 光源
var light;
function init_light(){
light = new THREE.DirectionalLight(0xffffff,1);//設(shè)置平行光源 (光顏色,光強(qiáng)度)
light.position.set(200,100,50);//設(shè)置光源向量 (x,y,z)
scene.add(light);
}
function ThreeJs_Main(){
init_renderer();//渲染
init_scene();//場景
init_cylinder();//圓柱體
init_camera();//相機(jī)
init_light();//光源
renderer.clear();
animation()
renderer.render(scene,camera);
}
function animation(){
//x,y,z為旋轉(zhuǎn)的軸 后邊數(shù)字為速度
// cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
// cube.rotation.z += 0.01;
renderer.render(scene,camera);
requestAnimationFrame(animation);
}
</script>
</head>
<body onload="ThreeJs_Main()">
<div id="canvas"></div>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于JavaScript實現(xiàn)鼠標(biāo)向下滑動加載div的代碼
這篇文章主要介紹了基于JavaScript實現(xiàn)鼠標(biāo)向下滑動加載div的代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
element-ui 的el-button組件中添加自定義顏色和圖標(biāo)的實現(xiàn)方法
這篇文章主要介紹了element-ui 的el-button組件中添加自定義顏色和圖標(biāo)的實現(xiàn)方法,目前的解決方案是:添加一個自定義全局指令,同時在element-ui源碼中,加入對應(yīng)的組件。需要的朋友跟隨小編一起看看吧2018-10-10

