1 Star 0 Fork 11

zheng_zn/three-weixin-demo_2

forked from three.js/three-weixin-demo 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
misc_exporter_obj.js 6.68 KB
一键复制 编辑 原始数据 按行查看 历史
zhangjin 提交于 2024-04-02 15:17 . .
import * as THREE from 'three';
const {
document,
window,
HTMLCanvasElement,
requestAnimationFrame,
cancelAnimationFrame,
core,
Event,
Event0
} = THREE .DHTML
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { OrbitControls0 } from 'three/examples/jsm/controls/OrbitControls0.js';
import { OBJExporter } from 'three/examples/jsm/exporters/OBJExporter.js';
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';
var requestId
Page({
onUnload() {
cancelAnimationFrame(requestId, this.canvas)
this.worker && this.worker.terminate()
if(this.canvas) this.canvas = null
setTimeout(() => {
if (this.renderer instanceof THREE.WebGLRenderer) {
this.renderer.dispose()
this.renderer.forceContextLoss()
this.renderer.context = null
this.renderer.domElement = null
this.renderer = null
}
}, 10)
},
webgl_touch(e){
const web_e = (window.platform=="devtools"?Event:Event0).fix(e)
this.canvas.dispatchEvent(web_e)
},
onLoad() {
document.createElementAsync("canvas", "webgl2",this).then(canvas => {
this.canvas = canvas
this.body_load(canvas).then()
})
},
async body_load(canvas3d) {
let camera, scene, renderer;
const params = {
addTriangle: addTriangle,
addCube: addCube,
addCylinder: addCylinder,
addMultiple: addMultiple,
addTransformed: addTransformed,
addPoints: addPoints,
exportToObj: exportToObj
};
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 0, 400 );
scene = new THREE.Scene();
const ambientLight = new THREE.AmbientLight( 0xffffff );
scene.add( ambientLight );
const directionalLight = new THREE.DirectionalLight( 0xffffff, 2.5 );
directionalLight.position.set( 0, 1, 1 );
scene.add( directionalLight );
const gui = new GUI();
let h = gui.addFolder( 'Geometry Selection' );
h.add( params, 'addTriangle' ).name( 'Triangle' );
h.add( params, 'addCube' ).name( 'Cube' );
h.add( params, 'addCylinder' ).name( 'Cylinder' );
h.add( params, 'addMultiple' ).name( 'Multiple objects' );
h.add( params, 'addTransformed' ).name( 'Transformed objects' );
h.add( params, 'addPoints' ).name( 'Point Cloud' );
h = gui.addFolder( 'Export' );
h.add( params, 'exportToObj' ).name( 'Export OBJ' );
gui.open();
addGeometry( 1 );
window.addEventListener( 'resize', onWindowResize );
const controls = new (window.platform=="devtools"?OrbitControls:OrbitControls0)( camera, renderer.domElement );
controls.enablePan = false;
}
function exportToObj() {
const exporter = new OBJExporter();
const result = exporter.parse( scene );
saveString( result, 'object.obj' );
}
function addGeometry( type ) {
for ( let i = 0; i < scene.children.length; i ++ ) {
const child = scene.children[ i ];
if ( child.isMesh || child.isPoints ) {
child.geometry.dispose();
scene.remove( child );
i --;
}
}
if ( type === 1 ) {
const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
const geometry = generateTriangleGeometry();
scene.add( new THREE.Mesh( geometry, material ) );
} else if ( type === 2 ) {
const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
const geometry = new THREE.BoxGeometry( 100, 100, 100 );
scene.add( new THREE.Mesh( geometry, material ) );
} else if ( type === 3 ) {
const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
const geometry = new THREE.CylinderGeometry( 50, 50, 100, 30, 1 );
scene.add( new THREE.Mesh( geometry, material ) );
} else if ( type === 4 || type === 5 ) {
const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
const geometry = generateTriangleGeometry();
const mesh = new THREE.Mesh( geometry, material );
mesh.position.x = - 200;
scene.add( mesh );
const geometry2 = new THREE.BoxGeometry( 100, 100, 100 );
const mesh2 = new THREE.Mesh( geometry2, material );
scene.add( mesh2 );
const geometry3 = new THREE.CylinderGeometry( 50, 50, 100, 30, 1 );
const mesh3 = new THREE.Mesh( geometry3, material );
mesh3.position.x = 200;
scene.add( mesh3 );
if ( type === 5 ) {
mesh.rotation.y = Math.PI / 4.0;
mesh2.rotation.y = Math.PI / 4.0;
mesh3.rotation.y = Math.PI / 4.0;
}
} else if ( type === 6 ) {
const points = [ 0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 100, 0 ];
const colors = [ 0.5, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0.5, 0 ];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( points, 3 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
const material = new THREE.PointsMaterial( { size: 10, vertexColors: true } );
const pointCloud = new THREE.Points( geometry, material );
pointCloud.name = 'point cloud';
scene.add( pointCloud );
}
}
function addTriangle() {
addGeometry( 1 );
}
function addCube() {
addGeometry( 2 );
}
function addCylinder() {
addGeometry( 3 );
}
function addMultiple() {
addGeometry( 4 );
}
function addTransformed() {
addGeometry( 5 );
}
function addPoints() {
addGeometry( 6 );
}
const link = document.createElement( 'a' );
link.style.display = 'none';
document.body.appendChild( link );
function save( blob, filename ) {
link.href = URL.createObjectURL( blob );
link.download = filename;
link.click();
}
function saveString( text, filename ) {
save( new Blob( [ text ], { type: 'text/plain' } ), filename );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestId = requestAnimationFrame( animate );
renderer.render( scene, camera );
}
function generateTriangleGeometry() {
const geometry = new THREE.BufferGeometry();
const vertices = [];
vertices.push( - 50, - 50, 0 );
vertices.push( 50, - 50, 0 );
vertices.push( 50, 50, 0 );
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry.computeVertexNormals();
return geometry;
}
}
})
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/zheng_zn/three-weixin-demo_2.git
[email protected]:zheng_zn/three-weixin-demo_2.git
zheng_zn
three-weixin-demo_2
three-weixin-demo_2
master

搜索帮助