我正在挖掘Three.js并正在尝试使用vrml,现在使用collada文件。
导入collada文件有效。它适用于webGL和画布作为后备:我的3D模型旋转,显示模型......在webGL中,我甚至可以拥有阴影,颠簸等奇妙的效果。
我确实实现了加载另一个纹理文件,将其分配给webGL渲染器......但在这种特殊情况下,画布渲染器完全失败:帧从60fps下降到2fps,纹理在多边形上“滑动”。
我想我想念一些东西以便将纹理“修复”到模型中,或者在导入纹理时可能会错过一些参数吗?再次它在没有改变纹理的情况下工作正常......但我真的需要这样做:p
这是一个有效的预览: http://dokmixer.com/three-tests/
这是魔法失败的部分:
//model loading
loader = new THREE.ColladaLoader();
loader.load('models/collada/7cm.005.dae',function colladaReady( collada ){
player = collada.scene;
skin = collada.skins [ 0 ];
player.scale.x = player.scale.y = player.scale.z = 0.10;
if (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1) {
var newskinTexture = new THREE.ImageUtils.loadTexture( 'models/collada/dokMixer.png' );
themodel = collada.scene.children[0];
themodel.material = new THREE.MeshBasicMaterial( { map: newskinTexture, overdraw: true} );
}
else {
var newskinTexture = new THREE.ImageUtils.loadTexture( 'models/collada/dokMixer.png' );
var bumpTexture = new THREE.ImageUtils.loadTexture( 'models/collada/noise.png' );
bumpTexture.anisotropy = 2;
player_material = collada.scene.children[0].material;
themodel = collada.scene.children[0];
themodel.material = new THREE.MeshPhongMaterial( { map: newskinTexture, bumpMap: bumpTexture, bumpScale: 0.05} );
}
//utile pour avoir les ombres
daemesh = player.children[0];
daemesh.castShadow = true;
daemesh.receiveShadow = true;
scene.add( player );
});
要研究的部分是第一个if语句(因为我在使用Safari测试画布,然后扩展到其他支持canvas的设备)。分配新材质时,渲染器完全失败。您可以通过使用Safari访问该页面来测试效果。
注意:如果可以提供任何相关信息,我正在开发OSX。
任何帮助表示赞赏:)
编辑:我想我在这里设置一个新材料,而不是只替换源图像文件?答案 0 :(得分:1)
好的,终于找到了解决方法!
我交换纹理的方式对于画布是错误的,我认为这是一个错误。 我通过psychok7找到了这个答案的线索。
好吧,而不是加载和那么改变纹理......我改变了ColladaLoader.js文件中的代码。好吧,几乎一样。因为我使用" load"我在加载函数中添加了几次imageReload,并在调用"解析"功能。然后,在解析函数中,我做了一些更改,因为给出的代码在他们的链接上是错误的。 对于那些感兴趣的人,一个变化列表,以及我的最终代码。可悲的是,我无法实现同样的" functionnalities(发送一个纹理数组,执行regExp的东西,并一次替换几个纹理),这部分对我来说是错误的,因为我只需要改变一个纹理文件......
所以,长话短说,这里是我称之为collada
的代码 loader = new THREE.ColladaLoader();
newtextures = ['dokMixer.png'];
loader.load('models/collada/7cm.005.dae', newtextures,function colladaReady( collada ){
player = collada.scene;
skin = collada.skins [ 0 ];
player.scale.x = player.scale.y = player.scale.z = 0.10;
//i'll add code here later for extra bump mapping on webgl versions
//usefull for shadows on webgl version
daemesh = player.children[0];
daemesh.castShadow = true;
daemesh.receiveShadow = true;
scene.add( player );
});
这里是colladaLoader中的代码。它是函数load和parse的替代。
function load ( url, imageReplace, readyCallback, progressCallback ) {
var length = 0;
if ( document.implementation && document.implementation.createDocument ) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if( request.readyState == 4 ) {
if( request.status == 0 || request.status == 200 ) {
if ( request.responseXML ) {
readyCallbackFunc = readyCallback;
parse( request.responseXML, imageReplace, undefined, url );
} else if ( request.responseText ) {
readyCallbackFunc = readyCallback;
var xmlParser = new DOMParser();
var responseXML = xmlParser.parseFromString( request.responseText, "application/xml" );
parse( responseXML, imageReplace, undefined, url );
} else {
console.error( "ColladaLoader: Empty or non-existing file (" + url + ")" );
}
}
} else if ( request.readyState == 3 ) {
if ( progressCallback ) {
if ( length == 0 ) {
length = request.getResponseHeader( "Content-Length" );
}
progressCallback( { total: length, loaded: request.responseText.length } );
}
}
}
request.open( "GET", url, true );
request.send( null );
} else {
alert( "Don't know how to parse XML!" );
}
}
function parse( doc, imageReplace, callBack, url ) {
COLLADA = doc;
callBack = callBack || readyCallbackFunc;
if ( url !== undefined ) {
var parts = url.split( '/' );
parts.pop();
baseUrl = ( parts.length < 1 ? '.' : parts.join( '/' ) ) + '/';
}
parseAsset();
setUpConversion();
images = parseLib( "library_images image", _Image, "image" );
for(var i in imageReplace) {
var iR = imageReplace[i];
for(var i in images) {
var image = images[i];
//added line, but no multiple textures !
image.init_from = iR;
//RegExp and patt.test not working
var patt=new RegExp('[a-zA-Z0-9\-\_]*\/'+iR.name,'g');
//if(image.id==iR.id)
//
if(patt.test(image.init_from))
image.init_from = iR.new_image;
}//for
}
materials = parseLib( "library_materials material", Material, "material" );
effects = parseLib( "library_effects effect", Effect, "effect" );
geometries = parseLib( "library_geometries geometry", Geometry, "geometry" );
cameras = parseLib( "library_cameras camera", Camera, "camera" );
lights = parseLib( "library_lights light", Light, "light" );
controllers = parseLib( "library_controllers controller", Controller, "controller" );
animations = parseLib( "library_animations animation", Animation, "animation" );
visualScenes = parseLib( "library_visual_scenes visual_scene", VisualScene, "visual_scene" );
// materials = parseLib( "//dae:library_materials/dae:material", Material, "material" );
// effects = parseLib( "//dae:library_effects/dae:effect", Effect, "effect" );
// geometries = parseLib( "//dae:library_geometries/dae:geometry", Geometry, "geometry" );
// cameras = parseLib( ".//dae:library_cameras/dae:camera", Camera, "camera" );
// controllers = parseLib( "//dae:library_controllers/dae:controller", Controller, "controller" );
// animations = parseLib( "//dae:library_animations/dae:animation", Animation, "animation" );
// visualScenes = parseLib( ".//dae:library_visual_scenes/dae:visual_scene", VisualScene, "visual_scene" );
morphs = [];
skins = [];
daeScene = parseScene();
scene = new THREE.Object3D();
for ( var i = 0; i < daeScene.nodes.length; i ++ ) {
scene.add( createSceneGraph( daeScene.nodes[ i ] ) );
}
// unit conversion
scene.position.multiplyScalar(colladaUnit);
scene.scale.multiplyScalar(colladaUnit);
createAnimations();
var result = {
scene: scene,
morphs: morphs,
skins: skins,
animations: animData,
dae: {
images: images,
materials: materials,
cameras: cameras,
effects: effects,
geometries: geometries,
controllers: controllers,
animations: animations,
visualScenes: visualScenes,
scene: daeScene
}
};
if ( callBack ) {
callBack( result );
}
return result;
}
希望这有助于某人!玩得开心 ! :)