我是ThreeJS新手,並且有一個簡單的問題。我有以下代碼可以正常工作,但我無法將顏色添加到我的.obj。簡而言之,我在Solidworks 2012中設計了一款遊戲控制器,然後將該CAD文件作爲.stl導出。然後我使用MeshLab將.stl作爲.obj導出。現在我在ThreeJS中使用.obj並且它可以工作,但是我不能爲我的生活添加顏色到.obj。這裏是代碼添加Colour to .obj在ThreeJS
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - loaders - vtk loader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>
<body>
<div id="info">
<a href="" target="_blank">three.js</a> -
vtk format loader test -
model from <a href="" target="_blank">The GeorgiaTech Lagre Geometric Model Archive</a>,
</div>
<script src="three.min.js"></script>
<script src="TrackballControls.js"></script>
<script src="OBJLoader.js"></script>
<script src="BinaryLoader.js"></script>
<script src="Detector.js"></script>
<script src="stats.min.js"></script>
<script>
if (! Detector.webgl) Detector.addGetWebGLMessage();
var container, stats;
var camera, controls, scene, renderer;
var cross;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.01, 1e10);
camera.position.z = 200;
camera.position.y = 200;
controls = new THREE.TrackballControls(camera);
controls.rotateSpeed = 5.0;
controls.zoomSpeed = 5;
controls.panSpeed = 2;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
scene = new THREE.Scene();
scene.add(camera);
// light
var dirLight = new THREE.DirectionalLight(0xffffff);
dirLight.position.set(20, 20, 100).normalize();
camera.add(dirLight);
camera.add(dirLight.target);
// texture
var manager = new THREE.LoadingManager();
manager.onProgress = function (item, loaded, total) {
console.log(item, loaded, total);
};
var texture = new THREE.Texture();
var loader = new THREE.ImageLoader(manager);
loader.load('bigthumbnail.jpg', function (image) {
texture.image = image;
texture.needsUpdate = true;
});
var loader = new THREE.OBJLoader()
loader.load('Gamepad.obj', function (object) {
object.position.y = 0;
scene.add(object);
});
// renderer
renderer = new THREE.WebGLRenderer({ antialias: false });
renderer.setSize(window.innerWidth, window.innerHeight);
container = document.createElement('div');
document.body.appendChild(container);
container.appendChild(renderer.domElement);
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild(stats.domElement);
//
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
controls.handleResize();
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
stats.update();
}
</script>
</body>
</html>
我已經通過threejs.org網站倒了,看了大多數的例子。所有使用複雜顏色的示例都使用.bin文件或.js文件。所以我下載了Python 2.7.6,安裝它並運行convert_obj_three.py。這生成了一個.js文件,但我不確定它的格式是否正確。不幸的是,convert_obj_three.py給我的輸出太大而無法發佈。我的第二個問題是哪種文件格式最適合複雜着色,如鉻藍色? .bin,.js或可以使用.obj?如果使用.js是最好的方法,那麼我該如何可靠地將.obj文件轉換爲.js?順便說一句,我嘗試使用由convert_obj_three.py創建的.js,但網頁一直是空白的。看來我無法使用THREE.JSONLoader()加載.js。
在此先感謝。
您是否需要創建材質並將其添加到對象中? – 2pha
@ 2pha:沒有加載函數爲你做。 – Shiva
它加載紋理,但我不認爲它適用於對象材質 – 2pha