2016-09-18 40 views
2

我想在浏览器中使用three.js加载3dsmax .obj和.mtl文件。three.js obj和mtl文件呈现黑色阴影

我使用本教程和代码..

obj的模型加载失败,但它看起来暗,而不是事情应该是这样。

enter image description here

我应该怎么办所以在对象无黑点。

我应该如何添加四面八方的灯光?

这里的脚本 -

<script> 

    var lesson6 = { 
    scene: null, 
    camera: null, 
     renderer: null, 
    container: null, 
    controls: null, 
    clock: null, 
    stats: null, 

    init: function() { // Initialization 

// create main scene 
this.scene = new THREE.Scene(); 
this.scene.fog = new THREE.FogExp2(0xcce0ff, 0.0003); 

var SCREEN_WIDTH = window.innerWidth, 
    SCREEN_HEIGHT = window.innerHeight; 

// prepare camera 
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH/SCREEN_HEIGHT, NEAR = 1, FAR = 2000; 
this.camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR); 
this.scene.add(this.camera); 
this.camera.position.set(0, 100, 300); 
this.camera.lookAt(new THREE.Vector3(0,0,0)); 

// prepare renderer 
this.renderer = new THREE.WebGLRenderer({ antialias:true }); 
this.renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); 
this.renderer.setClearColor(this.scene.fog.color); 
this.renderer.shadowMapEnabled = true; 
this.renderer.shadowMapSoft = true; 

// prepare container 
this.container = document.createElement('div'); 
document.body.appendChild(this.container); 
this.container.appendChild(this.renderer.domElement); 

// events 
THREEx.WindowResize(this.renderer, this.camera); 

// prepare controls (OrbitControls) 
this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement); 
this.controls.target = new THREE.Vector3(0, 0, 0); 
this.controls.maxDistance = 2000; 

// prepare clock 
this.clock = new THREE.Clock(); 

// prepare stats 
this.stats = new Stats(); 
this.stats.domElement.style.position = 'absolute'; 
this.stats.domElement.style.left = '50px'; 
this.stats.domElement.style.bottom = '50px'; 
this.stats.domElement.style.zIndex = 1; 
this.container.appendChild(this.stats.domElement); 

// add spot light 
    var spLight = new THREE.SpotLight(0xffffff, 1.75, 2000, Math.PI/3); 

spLight.castShadow = true; 

spLight.position.set(-200, 500, -150); 

this.scene.add(spLight); 



// add simple ground 
var ground = new THREE.Mesh(new THREE.PlaneGeometry(200, 200, 10, 10), new  THREE.MeshLambertMaterial({color:0x999999})); 
ground.receiveShadow = true; 
ground.position.set(0, 0, 0); 
    ground.rotation.x = -Math.PI/2; 
this.scene.add(ground); 

    // load a model 
    this.loadModel(); 
    }, 
    loadModel: function() { 
// prepare loader and load the model 
    var oLoader = new THREE.OBJMTLLoader(); 
    oLoader.load('1.obj', '1.mtl', function(object) { 

object.position.x = 80; 
object.position.y = 0; 
object.position.z = 30; 
object.scale.set(10,10,10); 
    lesson6.scene.add(object); 
    }); 
    } 
}; 

// Animate the scene 
function animate() { 
    requestAnimationFrame(animate); 
render(); 
update(); 
} 

// Update controls and stats 
function update() { 
lesson6.controls.update(lesson6.clock.getDelta()); 
lesson6.stats.update(); 
} 

// Render the scene 
function render() { 
if (lesson6.renderer) { 
lesson6.renderer.render(lesson6.scene, lesson6.camera); 
} 
} 

// Initialize lesson on page load 
function initializeLesson() { 
lesson6.init(); 
animate(); 
} 

if (window.addEventListener) 
window.addEventListener('load', initializeLesson, false); 
else if (window.attachEvent) 
    window.attachEvent('onload', initializeLesson); 
else window.onload = initializeLesson; 

</script> 

Default obj and mtl file preview- [![enter image description here][2]][2] 
+0

任何人都请..? – user2897282

回答

2

参见:three.js load obj/mtl renders black

OBJMTLLoader以来一直R74删除。您应该使用MTLLoader和OBJLoader。

var mtlLoader = new THREE.MTLLoader(); 
mtlLoader.setPath('obj/'); 
mtlLoader.load('1.mtl', function(materials){ 
    materials.preload(); 
    //OBJ LOADER 
    var objLoader = new THREE.OBJLoader(); 
    objLoader.setMaterials(materials); 
    objLoader.setPath('obj/'); 
    objLoader.load('1.obj', function(object){ 
     objModel = object; 
     scene.add(objModel); 
    }); 
}); 

也可以尝试在场景中添加环境光检查材料是否已正确加载。

var ambientLight = new THREE.AmbientLight('#fff'); 
scene.add(ambientLight); 
+0

我添加了OBJLoader并添加了这段代码,但没有加载对象。我对此很新颖。我只想加载要在浏览器中看到的对象。有没有一些好的浏览器,我可以添加我的.obj和.mtl文件,并使用适当的照明进行加载? – user2897282

+0

任何控制台错误? –

+0

TypeError:mtlLoader.setPath不是函数 – user2897282