three.js 怎么使用stl文件

 我来答
匿名用户
2015-04-19
展开全部
3D打印是当下和未来10年产品技术主流方向之一,影响深远。

对于电子商务类的3D打印网站,一个主要功能是把商品以3D的方式呈现出来,也就是3D数据可视化技术。

HTML5(WebGL)可以用来构建3D视图、立体动画、人机交互。Three.js是一个主流的JS框架用来支持和简化WebGL编程。

本例是一个简单的Three.js应用,即把一个外部可用于打印的3D标准建模文件(STL文件)载入到浏览器中绘制出来。

支持二进制和文本编码的STL文件,支持自动探测文件格式,字符编码使用UTF-8。

使用起来很简单,导入three.js(version 69), 和stlloader.js, 然后编写如下代码即可:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
var loader = new THREE.STLLoader();
loader.addEventListener('load', function(event) {
var geometry = event.content;
scene.add(new THREE.Mesh(geometry));
});
loader.load( './models/stl/slotted_disk.stl' );
huanglenzhi
2015-04-19 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517199
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
if (!Detector.webgl) Detector.addGetWebGLMessage();

var container, stats;

var camera, cameraTarget, scene, renderer;

init();
animate();

function init() {

container = document.createElement('div');
document.body.appendChild(container);

camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 15);
camera.position.set(3, 0.15, 3);

cameraTarget = new THREE.Vector3(0, -0.25, 0);

scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x72645b, 2, 15);

// Ground
var plane = new THREE.Mesh(
new THREE.PlaneBufferGeometry(40, 40),
new THREE.MeshPhongMaterial({
ambient: 0x999999,
color: 0x999999,
specular: 0x101010
})
);
plane.rotation.x = -Math.PI / 2;
plane.position.y = -0.5;
scene.add(plane);

plane.receiveShadow = true;

// ASCII file
var loader = new THREE.STLLoader();
loader.addEventListener('load', function(event) {

var geometry = event.content;
var material = new THREE.MeshPhongMaterial({
ambient: 0xff5533,
color: 0xff5533,
specular: 0x111111,
shininess: 200
});
var mesh = new THREE.Mesh(geometry, material);

mesh.position.set(0, -0.25, 0.6);
mesh.rotation.set(0, -Math.PI / 2, 0);
mesh.scale.set(0.5, 0.5, 0.5);

mesh.castShadow = true;
mesh.receiveShadow = true;

scene.add(mesh);

});
loader.load('/uploads/141201/stl/ascii/slotted_disk.stl');

// Binary files
var material = new THREE.MeshPhongMaterial({
ambient: 0x555555,
color: 0xAAAAAA,
specular: 0x111111,
shininess: 200
});

var loader = new THREE.STLLoader();
loader.addEventListener('load', function(event) {

var geometry = event.content;
var mesh = new THREE.Mesh(geometry, material);

mesh.position.set(0, -0.37, -0.6);
mesh.rotation.set(-Math.PI / 2, 0, 0);
mesh.scale.set(2, 2, 2);

mesh.castShadow = true;
mesh.receiveShadow = true;

scene.add(mesh);

});
loader.load('/uploads/141201/stl/binary/pr2_head_pan.stl');

var loader = new THREE.STLLoader();
loader.addEventListener('load', function(event) {

var geometry = event.content;
var mesh = new THREE.Mesh(geometry, material);

mesh.position.set(0.136, -0.37, -0.6);
mesh.rotation.set(-Math.PI / 2, 0.3, 0);
mesh.scale.set(2, 2, 2);

mesh.castShadow = true;
mesh.receiveShadow = true;

scene.add(mesh);

});
loader.load('/uploads/141201/stl/binary/pr2_head_tilt.stl');

// Colored binary STL
var loaderColored = new THREE.STLLoader();
loaderColored.addEventListener('load', function(event) {

var geometry = event.content;

var meshMaterial = material;
if (geometry.hasColors) {
meshMaterial = new THREE.MeshPhongMaterial({
opacity: geometry.alpha,
vertexColors: THREE.VertexColors
});
}

var mesh = new THREE.Mesh(geometry, meshMaterial);

mesh.position.set(0.5, 0.2, 0);
mesh.rotation.set(-Math.PI / 2, Math.PI / 2, 0);
mesh.scale.set(0.3, 0.3, 0.3);

mesh.castShadow = true;
mesh.receiveShadow = true;

scene.add(mesh);

});
loaderColored.load('/uploads/141201/stl/binary/colored.stl');

// Lights
scene.add(new THREE.AmbientLight(0x777777));

addShadowedLight(1, 1, 1, 0xffffff, 1.35);
addShadowedLight(0.5, 1, -1, 0xffaa00, 1);

// renderer

renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);

renderer.setClearColor(scene.fog.color, 1);

renderer.gammaInput = true;
renderer.gammaOutput = true;

renderer.shadowMapEnabled = true;
renderer.shadowMapCullFace = THREE.CullFaceBack;

container.appendChild(renderer.domElement);

// stats

stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild(stats.domElement);
window.addEventListener('resize', onWindowResize, false);
}

function addShadowedLight(x, y, z, color, intensity) {
var directionalLight = new THREE.DirectionalLight(color, intensity);
directionalLight.position.set(x, y, z)
scene.add(directionalLight);

directionalLight.castShadow = true;
// directionalLight.shadowCameraVisible = true;

var d = 1;
directionalLight.shadowCameraLeft = -d;
directionalLight.shadowCameraRight = d;
directionalLight.shadowCameraTop = d;
directionalLight.shadowCameraBottom = -d;

directionalLight.shadowCameraNear = 1;
directionalLight.shadowCameraFar = 4;

directionalLight.shadowMapWidth = 1024;
directionalLight.shadowMapHeight = 1024;

directionalLight.shadowBias = -0.005;
directionalLight.shadowDarkness = 0.15;

}

function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}

function animate() {
requestAnimationFrame(animate);
render();
stats.update();
}

function render() {
var timer = Date.now() * 0.0005;
camera.position.x = Math.cos(timer) * 3;
camera.position.z = Math.sin(timer) * 3;
camera.lookAt(cameraTarget);
renderer.render(scene, camera);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式