进入第二章的书本了接下来就是单独的介绍各个具体属性了第一个是场景
2016-11-13 11:41
309 查看
场景的属性比较有意思了具体都在注释里面
<!DOCTYPE html>
<html>
<head>
<title>Threejs</title>
<script type="text/javascript" src="Js/three.js"></script>
<script type="text/javascript" src="Js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="Js/stats.js"></script>
<script type="text/javascript" src="Js/dat.gui.js"></script>
<style>
body {margin: 0;overflow: hidden;}
</style>
</head>
<body>
<div id="Stats-output">
</div>
<!-- 上面是监视器,下面是输出 -->
<div id="WebGL-output">
</div>
<script type="text/javascript" src="Js/index.js"></script>
</body>
</html>
js的文件
/**
* Created by 12204 on 2016/11/12.
*/
function init() {
var stats = initStats();//初始化2监视器
//创建一个场景
var scene = new THREE.Scene();
// 创建一个相机
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// 创建一个渲染器
var renderer = new THREE.WebGLRenderer();
renderer.setClearColorHex();
renderer.setClearColor(new THREE.Color(0xEEEEEE));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;//告诉渲染器我需要影子
// 把坐标轴显示出来
var axes = new THREE.AxisHelper(20);
scene.add(axes);
// 创建一个平面
var planeGeometry = new THREE.PlaneGeometry(60, 40, 1, 1);
var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});//新的材质对光源有反应
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;//我接受影子照在我身上
//旋转平面的位置因为默认是与x轴平行的
plane.rotation.x = 1.5 * Math.PI;//-1.5到到0.5其实是一样的反向的问题
//所以也可以这么写
//plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 0;
plane.position.y = 0;
plane.position.z = 0;
// 添加到场景中去
scene.add(plane);
// 相机放在哪里其实就是从哪个位置看
camera.position.x = -30;
camera.position
4000
.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position);
// 添加光照效果但是没有材质的话没啥区别
var spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(-60, 60, -10);
spotLight.castShadow = true;
scene.add(spotLight);
// 获取上下问创建一个渲染的位置
document.getElementById("WebGL-output").appendChild(renderer.domElement);
//渲染场景
renderer.render(scene, camera);
var step = 0;
var controls = new function () {
this.rotationSpeed = 0.02;
this.numberOfObjects = scene.children.length;//获取当前scene对象中的所有对象的总数
this.removeCube = function () {
var allChildren = scene.children;//scene的children属性可以统计当前scene上面的所有对象
var lastObject = allChildren[allChildren.length - 1];
if (lastObject instanceof THREE.Mesh) {
scene.remove(lastObject);//上面的if-else判断是为了检测当前对象的属性防止把光源和相机移除了
this.numberOfObjects = scene.children.length;
}
};
this.addCube = function () {
var cubeSize = Math.ceil((Math.random() * 3));
var cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
var cubeMaterial = new THREE.MeshLambertMaterial({color: Math.random() * 0xffffff});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
cube.name = "cube-" + scene.children.length;//创建对象时指定的独一无二的名字
//可以通过getChildByName来获取这个对象
cube.position.x = -30 + Math.round((Math.random() * planeGeometry.parameters.width));//随机生成方块位置,颜色
cube.position.y = Math.round((Math.random() * 5));
cube.position.z = -20 + Math.round((Math.random() * planeGeometry.parameters.height));
// 增加一个方块
scene.add(cube);
this.numberOfObjects = scene.children.length;
};
this.outputObjects = function () {
console.log(scene.children);
}
};
var gui = new dat.GUI();
gui.add(controls, 'rotationSpeed', 0, 0.5);
gui.add(controls, 'addCube');
gui.add(controls, 'removeCube');
gui.add(controls, 'outputObjects');//在控制台输出所有的对象还有属性
gui.add(controls, 'numberOfObjects').listen();
render();
function render() {
stats.update();
// rotate the cubes around its axes
scene.traverse(function (e) {
if (e instanceof THREE.Mesh && e != plane) {
e.rotation.x += controls.rotationSpeed;
e.rotation.y += controls.rotationSpeed;
e.rotation.z += controls.rotationSpeed;
}
});
/*以上的代码也可以这么写
scene.traverse(function(e)
{
if(e instanceof THREE.Mesh && e!=plane)
{
e.rotation.x += controls.rotationSpeed;
e.rotation.y += controls.rotationSpeed;
e.rotation.z += controls.rotationSpeed;
}
})
*/
//循环渲染动画
requestAnimationFrame(render);
renderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// 监视器位置
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
<!DOCTYPE html>
<html>
<head>
<title>Threejs</title>
<script type="text/javascript" src="Js/three.js"></script>
<script type="text/javascript" src="Js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="Js/stats.js"></script>
<script type="text/javascript" src="Js/dat.gui.js"></script>
<style>
body {margin: 0;overflow: hidden;}
</style>
</head>
<body>
<div id="Stats-output">
</div>
<!-- 上面是监视器,下面是输出 -->
<div id="WebGL-output">
</div>
<script type="text/javascript" src="Js/index.js"></script>
</body>
</html>
js的文件
/**
* Created by 12204 on 2016/11/12.
*/
function init() {
var stats = initStats();//初始化2监视器
//创建一个场景
var scene = new THREE.Scene();
// 创建一个相机
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// 创建一个渲染器
var renderer = new THREE.WebGLRenderer();
renderer.setClearColorHex();
renderer.setClearColor(new THREE.Color(0xEEEEEE));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;//告诉渲染器我需要影子
// 把坐标轴显示出来
var axes = new THREE.AxisHelper(20);
scene.add(axes);
// 创建一个平面
var planeGeometry = new THREE.PlaneGeometry(60, 40, 1, 1);
var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});//新的材质对光源有反应
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;//我接受影子照在我身上
//旋转平面的位置因为默认是与x轴平行的
plane.rotation.x = 1.5 * Math.PI;//-1.5到到0.5其实是一样的反向的问题
//所以也可以这么写
//plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 0;
plane.position.y = 0;
plane.position.z = 0;
// 添加到场景中去
scene.add(plane);
// 相机放在哪里其实就是从哪个位置看
camera.position.x = -30;
camera.position
4000
.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position);
// 添加光照效果但是没有材质的话没啥区别
var spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(-60, 60, -10);
spotLight.castShadow = true;
scene.add(spotLight);
// 获取上下问创建一个渲染的位置
document.getElementById("WebGL-output").appendChild(renderer.domElement);
//渲染场景
renderer.render(scene, camera);
var step = 0;
var controls = new function () {
this.rotationSpeed = 0.02;
this.numberOfObjects = scene.children.length;//获取当前scene对象中的所有对象的总数
this.removeCube = function () {
var allChildren = scene.children;//scene的children属性可以统计当前scene上面的所有对象
var lastObject = allChildren[allChildren.length - 1];
if (lastObject instanceof THREE.Mesh) {
scene.remove(lastObject);//上面的if-else判断是为了检测当前对象的属性防止把光源和相机移除了
this.numberOfObjects = scene.children.length;
}
};
this.addCube = function () {
var cubeSize = Math.ceil((Math.random() * 3));
var cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
var cubeMaterial = new THREE.MeshLambertMaterial({color: Math.random() * 0xffffff});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
cube.name = "cube-" + scene.children.length;//创建对象时指定的独一无二的名字
//可以通过getChildByName来获取这个对象
cube.position.x = -30 + Math.round((Math.random() * planeGeometry.parameters.width));//随机生成方块位置,颜色
cube.position.y = Math.round((Math.random() * 5));
cube.position.z = -20 + Math.round((Math.random() * planeGeometry.parameters.height));
// 增加一个方块
scene.add(cube);
this.numberOfObjects = scene.children.length;
};
this.outputObjects = function () {
console.log(scene.children);
}
};
var gui = new dat.GUI();
gui.add(controls, 'rotationSpeed', 0, 0.5);
gui.add(controls, 'addCube');
gui.add(controls, 'removeCube');
gui.add(controls, 'outputObjects');//在控制台输出所有的对象还有属性
gui.add(controls, 'numberOfObjects').listen();
render();
function render() {
stats.update();
// rotate the cubes around its axes
scene.traverse(function (e) {
if (e instanceof THREE.Mesh && e != plane) {
e.rotation.x += controls.rotationSpeed;
e.rotation.y += controls.rotationSpeed;
e.rotation.z += controls.rotationSpeed;
}
});
/*以上的代码也可以这么写
scene.traverse(function(e)
{
if(e instanceof THREE.Mesh && e!=plane)
{
e.rotation.x += controls.rotationSpeed;
e.rotation.y += controls.rotationSpeed;
e.rotation.z += controls.rotationSpeed;
}
})
*/
//循环渲染动画
requestAnimationFrame(render);
renderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// 监视器位置
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
相关文章推荐
- textarea的disabled与readonly属性具体用法介绍
- JSP第二篇【内置对象的介绍、4种属性范围、应用场景】
- iphone开发基础控件基础属性总结介绍<链接党>-----内附具体博客链接
- JSP第二篇【内置对象的介绍、4种属性范围、应用场景】
- Android中项目的AndroidManifest.xml文件的作用及改文件中相关属性的具体介绍
- JSP第二篇【内置对象的介绍、4种属性范围、应用场景】
- 熟悉了整个网站制作的流程,那么接下来的第一个步骤就是给自己挑选一个适合的域名
- JSP第二篇【内置对象的介绍、4种属性范围、应用场景】
- 第二章 编写第一个JAVA程序
- 上传文件用到的属性和方法介绍
- 介绍MIDP应用程序的属性
- core jave2:第一章java介绍;第二章java编辑环境
- linux进入系统直接就是x界面,怎么才能进入命令行模式?
- 接下来就是看IRP了中断请求包
- css之BackGround属性介绍
- 介绍MIDP应用程序的属性
- 进入%WINDIR%/FONTS目录,选中所有文件,右键点选属性,看看结果是什么?
- 最近有些朋友问我我们在写的室外自然场景引擎用了些什么技术具体在那些
- 《Web Service 编程 --用C#.NET 开发网络服务》北京希望出版社 我的学习笔记(第二章)(也就是书上抄了一写东西而已)
- vc.net 游戏开发 第二章 创建3D场景