threejs 小例子,链接文档

该博客是一份详尽的three.js教程,涵盖了从创建几何、变换、矩阵到灯光、相机、纹理、着色器编程以及交互和动画的全面内容。通过实例讲解,包括建造楼梯、饮酒鸟等,帮助读者逐步掌握three.js的使用技巧。
摘要由CSDN通过智能技术生成

开始

第2课:点,矢量和网格

在three.js中创建几何

var material = new THREE.MeshBasicMaterial( {
    color: 0xff0000, side: THREE.DoubleSide } );

var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( 5, 10, 0 ) );  // vertex 0
geometry.vertices.push( new THREE.Vector3( 5, 5, 0 ) );   // vertex 1
geometry.vertices.push( new THREE.Vector3( 10, 5, 0 ) );  // vertex 2

geometry.faces.push( new THREE.Face3( 0, 1, 2 ) );  // make a triangle

var mesh = new THREE.Mesh( geometry, material );  // create an object

scene.add( mesh ); // add object to the scene to make it visible

建造楼梯

var geoBlock = new THREE.CubeGeometry( width, height, thickness );
var blockMesh = new THREE.Mesh( geoBlock, material );
// set the X,Y,Z position
stepMesh.position.x = xPosition;
stepMesh.position.y = yPosition;
stepMesh.position.z = zPosition;

饮酒鸟

// values 32, 16 are longitude and latitude tessellations
var sphere = new THREE.Mesh(
    new THREE.SphereGeometry( radius, 32, 16 ), sphereMaterial );

var cylinder = new THREE.Mesh(
    new THREE.CylinderGeometry( radiusTop, radiusBottom, height, 32 ), cylMaterial );

第3课:颜色和材料

设置颜色

var sphereMaterial = new THREE.MeshLambertMaterial( );
// three separate floats to set RGB
sphereMaterial.color.r = 1.0;    
sphereMaterial.color.g = 0.0;
sphereMaterial.color.b = 0.0;
// or use the setRGB method
sphereMaterial.color.setRGB( 0.972, 0.749, 0.141 );
// or use a hex value, 0x00 to 0xFF (0-255), for each channel
sphereMaterial.color.setHex( 0x1280FF );

// or initialize your material with the color
var cylMaterial = new THREE.MeshLambertMaterial( {
    color: 0xF4F100 } );

顶点属性

// how to set the three vertex colors for face #0
var color1 = new THREE.Color( 0xF08000 ); // orange
var color2 = new THREE.Color( 0x808000 ); // olive
var color3 = new THREE.Color( 0x0982FF ); // bright blue
geometry.faces[0].vertexColors = [ color1, color2, color3 ];

漫射材料

material = new THREE.MeshBasicMaterial( {
    color: 0x80fc66, shading: THREE.FlatShading } );
material.color.setRGB( red, green, blue );
var newRed = material.color.r * 0.7;
material.ambient.setRGB( ... );

Ka,Kd和HSL

var color = new Color();
// hue, saturation, and lightness, all 0-1
color.setHSL( 0.117, 0.937, 0.557 ); // orange

透明度和Three.js

var movingBoxMaterial = new THREE.MeshLambertMaterial(
    {
    color: 0xE53319, opacity: 0.7, transparent: true } );

第4课:变换

翻译

sphere.position.x = xPosition;
sphere.position.y = yPosition;
sphere.position.z = zPosition;

回转

cube.rotation.x = xRotationInRadians; // e.g. -70 * Math.PI/180
cube.rotation.y = yRotationInRadians;
cube.rotation.z = zRotationInRadians;

刚体转换与缩放

cube.scale.x = xSize;
cube.scale.y = ySize;
cube.scale.z = zSize;

缩放旋转平移

// This is always the order of application:
cube.scale.set( xSize, ySize, zSize );
cube.rotation.set( xRotationInRadians, yRotationInRadians, zRotationInRadians );
cube.position.set( xPosition, yPosition, zPosition );

Object3D

var block = new THREE.Mesh(
    new THREE.CubeGeometry( 100, 4, 4 ), clockHandMaterial );
block.position
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值