从Unity到Three.js(动态创建mesh)

本文介绍了如何在JavaScript中使用THREE.js库,通过顶点数据和索引创建Mesh对象,实现点云转Mesh和磨碎效果,并演示了如何手动创建模型和动画渲染的过程。

js var let const基础

手动创建模型mesh功能测试,此功能跑通就可以实现很多功能了,如点云转mesh,磨碎效果等等。

import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

//只使用顶点数据创建mesh
//创建顶点数据
//猜测是直接在顶点数据中定义好三角形索引顺序。
const vertices = new Float32Array([
    0.0, 0.0, 1.0,
    1.0, 0.0, 1.0,
    1.0, 1.0, 1.0,

    0.0, 1.0, 1.0,
    0.0, 0.0, 1.0,
    1.0, 1.0, 1.0
]);

// itemSize = 3 因为每个顶点都是一个三元组。
// geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
// const material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
const mesh = createMesh(vertices, 0x0000FF, testCallback);// new THREE.Mesh( geometry, material );
scene.add(mesh);
mesh.position.x -= 10;

//使用顶点+索引+法向量创建mesh
const vertices2 = new Float32Array([
    0.0, 0.0, 1.0,
    1.0, 0.0, 1.0,
    1.0, 1.0, 1.0,
    0.0, 1.0, 1.0,
]);
const verticesIndex = new Uint16Array([
    0, 1, 2,
    3, 0, 2,
]);

const normals = new Float32Array([
    0, 0, 1, //顶点1法向量
    0, 0, 1, //顶点2法向量
    0, 0, 1, //顶点3法向量
    0, 0, 1, //顶点4法向量
]);
const mesh2 = createMeshSetIndex(vertices2, 0x00FF00, verticesIndex, normals);
mesh2.position.x
scene.add(mesh2);
mesh2.position.x += 10;

function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}

animate();
camera.position.z = 50;

function testCallback(msg) {
    console.log(msg);
    console.log("测试方法作为参数使用,对应c#委托事件功能");
}

//====================================================创建mesh方法封装==========================================
// 函数定义,参数不需要指定类型,返回值也不需要定义
//传入的顶点数组中定义好顶点索引
function createMesh(verticesArg, colorArg, callback) {
    let geometryTemp = new THREE.BufferGeometry();
    geometryTemp.setAttribute('position', new THREE.BufferAttribute(verticesArg, 3));
    const material = new THREE.MeshBasicMaterial({ color: colorArg });
    const mesh = new THREE.Mesh(geometryTemp, material);
    if (callback != null) {
        callback("mesh创建完毕");
    }
    return mesh;
}
//依据顶点、顶点索引、法向量创建mesh
function createMeshSetIndex(verticesArg, colorArg, indexs, normals) {
    let geometryTemp = new THREE.BufferGeometry();
    let ba = new THREE.BufferAttribute(verticesArg, 3);
    //配置顶点
    geometryTemp.setAttribute('position', ba);
    //配置法向量
    geometryTemp.attributes.normal = new THREE.BufferAttribute(normals, 3);
    //配置顶点索引
    geometryTemp.index = new THREE.BufferAttribute(indexs, 1);
    const material = new THREE.MeshBasicMaterial({ color: colorArg });
    const mesh = new THREE.Mesh(geometryTemp, material);
    return mesh;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值