//2017.3.23
// 1.
function Mesh(geometry, material) {

Object3D.call(this);

this.type = 'Mesh';

this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
this.material = material !== undefined ? material : new MeshBasicMaterial({ color: Math.random() * 0xffffff });

this.drawMode = TrianglesDrawMode;

this.updateMorphTargets();

}
//如果mesh不传material, 那么材质就用基本材质代替, 颜色随机。上面是源码。


var mesh = new THREE.Mesh(geometries[i]);


// 2. 移除物体时出错
for (var i = 0; i < group.children.length; i++) {


group.remove(group.getObjectByName(modelName[i]));
}
//改法
for (var i = 0; i < modelName.length; i++) {


group.remove(group.getObjectByName(modelName[i]));
}
// 移除物体的时候,因为group.children的长度发生变化,所以移除一个,后面的那个往前填充,而移除的时后面的,所以会移除不干净。

// 3.在group组中改变某一个小mesh的材质的时候,需要new一个材质出来,不能直接改变它的map,否则的话,其他的也都变化了。
group.getObjectByName('xiegen').material = new THREE.MeshPhongMaterial({
map: textureLoader.load($(this).find('img').attr('src'))
})

// 4.导出的ctm模型材质属性会根据一定的规则来转换到json属性,然后threejs内部再根据json创建一个基于这些属性的材质。属性名有对比。
// 关于材质的一些属性:对应于ctm.js里面的材质。
case 'DbgColor':
case 'DbgIndex':
case 'opticalDensity':
case 'illumination':
break;
case 'DbgName':
json.name = value;
break;
case 'blending':
json.blending = BlendingMode[ value ];
break;
case 'colorAmbient':
case 'mapAmbient':
console.warn( 'THREE.Loader.createMaterial:', name, 'is no longer supported.' );
break;
case 'colorDiffuse':
json.color = color.fromArray( value ).getHex();
break;
case 'colorSpecular':
json.specular = color.fromArray( value ).getHex();
break;
case 'colorEmissive':
json.emissive = color.fromArray( value ).getHex();
break;
case 'specularCoef':
json.shininess = value;
break;
case 'shading':
if ( value.toLowerCase() === 'basic' ) json.type = 'MeshBasicMaterial';
if ( value.toLowerCase() === 'phong' ) json.type = 'MeshPhongMaterial';
if ( value.toLowerCase() === 'standard' ) json.type = 'MeshStandardMaterial';
break;
case 'mapDiffuse':
json.map = loadTexture( value, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy );
break;
case 'mapDiffuseRepeat':
case 'mapDiffuseOffset':
case 'mapDiffuseWrap':
case 'mapDiffuseAnisotropy':
break;
case 'mapEmissive':
json.emissiveMap = loadTexture( value, m.mapEmissiveRepeat, m.mapEmissiveOffset, m.mapEmissiveWrap, m.mapEmissiveAnisotropy );
break;
case 'mapEmissiveRepeat':
case 'mapEmissiveOffset':
case 'mapEmissiveWrap':
case 'mapEmissiveAnisotropy':
break;
case 'mapLight':
json.lightMap = loadTexture( value, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy );
break;
case 'mapLightRepeat':
case 'mapLightOffset':
case 'mapLightWrap':
case 'mapLightAnisotropy':
break;
case 'mapAO':
json.aoMap = loadTexture( value, m.mapAORepeat, m.mapAOOffset, m.mapAOWrap, m.mapAOAnisotropy );
break;
case 'mapAORepeat':
case 'mapAOOffset':
case 'mapAOWrap':
case 'mapAOAnisotropy':
break;
case 'mapBump':
json.bumpMap = loadTexture( value, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy );
break;
case 'mapBumpScale':
json.bumpScale = value;
break;
case 'mapBumpRepeat':
case 'mapBumpOffset':
case 'mapBumpWrap':
case 'mapBumpAnisotropy':
break;
case 'mapNormal':
json.normalMap = loadTexture( value, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy );
break;
case 'mapNormalFactor':
json.normalScale = [ value, value ];
break;
case 'mapNormalRepeat':
case 'mapNormalOffset':
case 'mapNormalWrap':
case 'mapNormalAnisotropy':
break;
case 'mapSpecular':
json.specularMap = loadTexture( value, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy );
break;
case 'mapSpecularRepeat':
case 'mapSpecularOffset':
case 'mapSpecularWrap':
case 'mapSpecularAnisotropy':
break;
case 'mapMetalness':
json.metalnessMap = loadTexture( value, m.mapMetalnessRepeat, m.mapMetalnessOffset, m.mapMetalnessWrap, m.mapMetalnessAnisotropy );
break;
case 'mapMetalnessRepeat':
case 'mapMetalnessOffset':
case 'mapMetalnessWrap':
case 'mapMetalnessAnisotropy':
break;
case 'mapRoughness':
json.roughnessMap = loadTexture( value, m.mapRoughnessRepeat, m.mapRoughnessOffset, m.mapRoughnessWrap, m.mapRoughnessAnisotropy );
break;
case 'mapRoughnessRepeat':
case 'mapRoughnessOffset':
case 'mapRoughnessWrap':
case 'mapRoughnessAnisotropy':
break;
case 'mapAlpha':
json.alphaMap = loadTexture( value, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy );
break;
case 'mapAlphaRepeat':
case 'mapAlphaOffset':
case 'mapAlphaWrap':
case 'mapAlphaAnisotropy':
break;
case 'flipSided':
json.side = BackSide;
break;
case 'doubleSided':
json.side = DoubleSide;
break;
case 'transparency':
console.warn( 'THREE.Loader.createMaterial: transparency has been renamed to opacity' );
json.opacity = value;
break;
case 'depthTest':
case 'depthWrite':
case 'colorWrite':
case 'opacity':
case 'reflectivity':
case 'transparent':
case 'visible':
case 'wireframe':
json[ name ] = value;
break;
case 'vertexColors':
if ( value === true ) json.vertexColors = VertexColors;
if ( value === 'face' ) json.vertexColors = FaceColors;
break;
default:
console.error( 'THREE.Loader.createMaterial: Unsupported', name, value );
break;

}

}


//再根据json创建一个新材质
if ( json.uuid !== undefined ) material.uuid = json.uuid;
if ( json.name !== undefined ) material.name = json.name;
if ( json.color !== undefined ) material.color.setHex( json.color );
if ( json.roughness !== undefined ) material.roughness = json.roughness;
if ( json.metalness !== undefined ) material.metalness = json.metalness;
if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
if ( json.specular !== undefined ) material.specular.setHex( json.specular );
if ( json.shininess !== undefined ) material.shininess = json.shininess;
if ( json.clearCoat !== undefined ) material.clearCoat = json.clearCoat;
if ( json.clearCoatRoughness !== undefined ) material.clearCoatRoughness = json.clearCoatRoughness;
if ( json.uniforms !== undefined ) material.uniforms = json.uniforms;
if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
if ( json.fog !== undefined ) material.fog = json.fog;
if ( json.shading !== undefined ) material.shading = json.shading;
if ( json.blending !== undefined ) material.blending = json.blending;
if ( json.side !== undefined ) material.side = json.side;
if ( json.opacity !== undefined ) material.opacity = json.opacity;
if ( json.transparent !== undefined ) material.transparent = json.transparent;
if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
if ( json.skinning !== undefined ) material.skinning = json.skinning;
if ( json.morphTargets !== undefined ) material.morphTargets = json.morphTargets;

// for PointsMaterial

if ( json.size !== undefined ) material.size = json.size;
if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;

// maps

if ( json.map !== undefined ) material.map = getTexture( json.map );

if ( json.alphaMap !== undefined ) {

material.alphaMap = getTexture( json.alphaMap );
material.transparent = true;

}

if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;

if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
if ( json.normalScale !== undefined ) {

var normalScale = json.normalScale;

if ( Array.isArray( normalScale ) === false ) {

// Blender exporter used to export a scalar. See #7459

normalScale = [ normalScale, normalScale ];

}

material.normalScale = new Vector2().fromArray( normalScale );

}

if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;

if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );

if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;

if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );

if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );

if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;

if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;

if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;

if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );

// MultiMaterial

if ( json.materials !== undefined ) {

for ( var i = 0, l = json.materials.length; i < l; i ++ ) {

material.materials.push( this.parse( json.materials[ i ] ) );

}

}

return material;

6.roughtness值越小越光滑,而且shiness光点越小,越集中。metalness越大越偏于黑色,金属性。

posted on 2017-03-23 18:03  feixiangsnail  阅读(262)  评论(0编辑  收藏  举报