代码拉取完成,页面将自动刷新
同步操作将从 three.js/three-weixin-demo 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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;
}
}
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。