源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[JavaScript] three.js实现围绕某物体旋转

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2017-1-25 09:28:32 | 显示全部楼层 |阅读模式
本篇文章主要介绍了three.js实现围绕某物体旋转的示例代码。具有很好的参考价值,下面跟着小编一起来看下吧

话不多说,请看代码:

可以拖动右上角观察变化

<!DOCTYPE html>
<html lang="en" style="width: 100%; height:100%;">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="http://cdn.bootcss.com/three.js/r83/three.min.js"></script>
 <script src="http://cooklife.cn/html/node_modules/dat.gui/build/dat.gui.min.js"></script>
</head>
<body onload="threeExcute()" style="width: 100%; height:100%;">
 <div id="box"></div>
</body>
 <!-- Three.js的核心五步就是:
 1.设置three.js渲染器
 2.设置摄像机camera
 3.设置场景scene
 4.设置光源light
 5.设置物体object 
 -->
 <script>
 // 1.设置three.js渲染器
 var renderer;
 function initThree(){
 width = document.getElementById("box").clientWidth;
 height = document.getElementById("box").clientHeight;
 renderer = new THREE.WebGLRenderer({
 antialias:true
 });/*生成渲染器对象(属性:抗锯齿效果为设置有效)*/
 renderer.setSize(width,height);
 document.getElementById("box").appendChild(renderer.domElement);
 /*设置canvas背景色(clearColor)和背景色透明度(clearAlpha) */
 renderer.setClearColor(0xFFFF00,1.0);
 }

 // 2.设置摄像机camera
 var camera;
 function initCamera(){
 camera = new THREE.PerspectiveCamera(45,width/height,1,10000);
 camera.position.x = 1000;
 camera.position.y = 1000;
 camera.position.z = 1000;
 camera.up.x = 0;
 camera.up.y = 0;
 camera.up.z = 100;
 camera.lookAt({x:0,y:0,z:0}); //设置视野的中心坐标 
 }

 // 3.设置场景
 var scene;
 function initScene(){
 scene = new THREE.Scene();
 }

 // 4.设置光源light
 var light;
 function initLight(){
 light = new THREE.DirectionalLight(0xFF00FF, 1.0, 0); //平行光
 light.position.set(100,100, 200); //设置光源位置
 scene.add(light); //将官员添加到场景
 }

 //5.设置物体 
 var sphereMesh;
 var cubeMesh;
 var cubeMesh2;
 var cubeMesh3;
 var cubeMesh4;
 var cubeMesh5;
 var cubeMesh6;
 function initObject(){
 cubeMesh = new THREE.Mesh(new THREE.BoxGeometry(80,80,80),new THREE.MeshLambertMaterial({color:0xff0000})/*
 设置球体的材质*/);
 cubeMesh2 = new THREE.Mesh(new THREE.BoxGeometry(80,80,80),new THREE.MeshLambertMaterial({color:0xff0000})/*
 设置球体的材质*/);
 cubeMesh3 = new THREE.Mesh(new THREE.BoxGeometry(80,80,80),new THREE.MeshLambertMaterial({color:0xff0000})/*
 设置球体的材质*/);
 sphereMesh = new THREE.Mesh(new THREE.SphereGeometry(200,200,200),new THREE.MeshLambertMaterial({color:0xff00FF})/*设置球体的材质*/); //材质设定 
 sphereMesh.position.set(0,0,0); /*设置物体位置*/ 
 cubeMesh2.position.set(400,0,0); 
 cubeMesh.position.set(390,150,0); 
 cubeMesh3.position.set(380,100,0); 
 /*
 * 旋转要点。。。
 */
 var pivotPoint = new THREE.Object3D();
 pivotPoint.add(cubeMesh);
 pivotPoint.add(cubeMesh2);
 pivotPoint.add(cubeMesh3);
 sphereMesh.add(pivotPoint);
 scene.add(sphereMesh); 
 sphereMesh.name = 'cube' 
 } 

 control = new function () {
  this.rotationSpeedX = 0.001;
  this.rotationSpeedY = 0.001;
  this.rotationSpeedZ = 0.001;
 };

 function addController(){
 var gui = new dat.GUI();
 gui.add(control, 'rotationSpeedX', -0.2, 0.2);
  gui.add(control, 'rotationSpeedY', -0.2, 0.2);
  gui.add(control, 'rotationSpeedZ', -0.2, 0.2);
 }

 function render(){
 renderer.render(scene, camera);
  scene.getObjectByName('cube').rotation.x += control.rotationSpeedX;
  scene.getObjectByName('cube').rotation.y += control.rotationSpeedY;
  scene.getObjectByName('cube').rotation.z += control.rotationSpeedZ;

  requestAnimationFrame(render);
 } 
 function threeExcute(){ 
  initThree(); 
  initCamera(); 
  initScene(); 
  initLight(); 
  initObject(); 
  renderer.clear();
  addController(); 
  render(); 
 } 
 </script>
 <style type="text/css">
 div#box{
  border: none;
  cursor: move;
  width: 100%;
  height: 100%;
  background-color: #EEEEEE;
  }
 </style>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

回复

使用道具 举报

5

主题

2万

回帖

183

积分

注册会员

Rank: 2

积分
183
发表于 2022-9-15 07:19:14 | 显示全部楼层
撒房产税陈飞飞
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

59

积分

注册会员

Rank: 2

积分
59
发表于 2023-2-22 02:22:34 | 显示全部楼层
sdsadsadsadf
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

499

积分

中级会员

Rank: 3Rank: 3

积分
499
发表于 2023-7-3 19:28:03 | 显示全部楼层
额UI废物iuhfujewfiewnnfen
回复 支持 反对

使用道具 举报

16

主题

2万

回帖

174

积分

注册会员

Rank: 2

积分
174
发表于 2023-9-10 07:45:18 | 显示全部楼层
而非为吾问无为谓娃娃
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

67

积分

注册会员

Rank: 2

积分
67
发表于 2023-10-19 09:08:42 | 显示全部楼层
儿童服务绯闻绯闻绯闻
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-5-1 11:09:04 | 显示全部楼层
下载来瞧瞧
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-7-4 07:41:36 | 显示全部楼层
儿童服务绯闻绯闻绯闻
回复 支持 反对

使用道具 举报

3

主题

2万

回帖

172

积分

注册会员

Rank: 2

积分
172
发表于 2024-9-11 19:24:15 | 显示全部楼层
下载来瞧瞧
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-9-16 15:09:17 | 显示全部楼层
终于找到了,我擦
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-7 07:39 , Processed in 0.065691 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表