Fantasy-Map-Generator/libs/objexporter.min.js
Efruz Yıldırır 3d8aa7c3ca
3D Scene system upgrade. (#956)
* 3d view system upgrade.

* version fix

* Versioning fixed.

* Subdivision Added

* Subdivision added. Removed toggle wireframe as an option.
Reverted to previous rendering method.

* Update obj export because new threejs version.

* Clean up of unrequired code.

* Multiple fixes to 3D view upgrade PR.

* Remove unused code.(for3DRender)
2023-08-05 22:54:13 +04:00

25 lines
No EOL
3.6 KiB
JavaScript

(function(){class OBJExporter{parse(object){let output='';let indexVertex=0;let indexVertexUvs=0;let indexNormals=0;const vertex=new THREE.Vector3();const color=new THREE.Color();const normal=new THREE.Vector3();const uv=new THREE.Vector2();const face=[];function parseMesh(mesh){let nbVertex=0;let nbNormals=0;let nbVertexUvs=0;const geometry=mesh.geometry;const normalMatrixWorld=new THREE.Matrix3();if(geometry.isBufferGeometry!==!0){throw new Error('THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.')}
const vertices=geometry.getAttribute('position');const normals=geometry.getAttribute('normal');const uvs=geometry.getAttribute('uv');const indices=geometry.getIndex();output+='o '+mesh.name+'\n';if(mesh.material&&mesh.material.name){output+='usemtl '+mesh.material.name+'\n'}
if(vertices!==undefined){for(let i=0,l=vertices.count;i<l;i ++,nbVertex ++){vertex.fromBufferAttribute(vertices,i);vertex.applyMatrix4(mesh.matrixWorld);output+='v '+vertex.x+' '+vertex.y+' '+vertex.z+'\n'}}
if(uvs!==undefined){for(let i=0,l=uvs.count;i<l;i ++,nbVertexUvs ++){uv.fromBufferAttribute(uvs,i);output+='vt '+uv.x+' '+uv.y+'\n'}}
if(normals!==undefined){normalMatrixWorld.getNormalMatrix(mesh.matrixWorld);for(let i=0,l=normals.count;i<l;i ++,nbNormals ++){normal.fromBufferAttribute(normals,i);normal.applyMatrix3(normalMatrixWorld).normalize();output+='vn '+normal.x+' '+normal.y+' '+normal.z+'\n'}}
if(indices!==null){for(let i=0,l=indices.count;i<l;i+=3){for(let m=0;m<3;m ++){const j=indices.getX(i+m)+1;face[m]=indexVertex+j+(normals||uvs?'/'+(uvs?indexVertexUvs+j:'')+(normals?'/'+(indexNormals+j):''):'')}
output+='f '+face.join(' ')+'\n'}}else{for(let i=0,l=vertices.count;i<l;i+=3){for(let m=0;m<3;m ++){const j=i+m+1;face[m]=indexVertex+j+(normals||uvs?'/'+(uvs?indexVertexUvs+j:'')+(normals?'/'+(indexNormals+j):''):'')}
output+='f '+face.join(' ')+'\n'}}
indexVertex+=nbVertex;indexVertexUvs+=nbVertexUvs;indexNormals+=nbNormals}
function parseLine(line){let nbVertex=0;const geometry=line.geometry;const type=line.type;if(geometry.isBufferGeometry!==!0){throw new Error('THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.')}
const vertices=geometry.getAttribute('position');output+='o '+line.name+'\n';if(vertices!==undefined){for(let i=0,l=vertices.count;i<l;i ++,nbVertex ++){vertex.fromBufferAttribute(vertices,i);vertex.applyMatrix4(line.matrixWorld);output+='v '+vertex.x+' '+vertex.y+' '+vertex.z+'\n'}}
if(type==='Line'){output+='l ';for(let j=1,l=vertices.count;j<=l;j ++){output+=indexVertex+j+' '}
output+='\n'}
if(type==='LineSegments'){for(let j=1,k=j+1,l=vertices.count;j<l;j+=2,k=j+1){output+='l '+(indexVertex+j)+' '+(indexVertex+k)+'\n'}}
indexVertex+=nbVertex}
function parsePoints(points){let nbVertex=0;const geometry=points.geometry;if(geometry.isBufferGeometry!==!0){throw new Error('THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.')}
const vertices=geometry.getAttribute('position');const colors=geometry.getAttribute('color');output+='o '+points.name+'\n';if(vertices!==undefined){for(let i=0,l=vertices.count;i<l;i ++,nbVertex ++){vertex.fromBufferAttribute(vertices,i);vertex.applyMatrix4(points.matrixWorld);output+='v '+vertex.x+' '+vertex.y+' '+vertex.z;if(colors!==undefined){color.fromBufferAttribute(colors,i).convertLinearToSRGB();output+=' '+color.r+' '+color.g+' '+color.b}
output+='\n'}
output+='p ';for(let j=1,l=vertices.count;j<=l;j ++){output+=indexVertex+j+' '}
output+='\n'}
indexVertex+=nbVertex}
object.traverse(function(child){if(child.isMesh===!0){parseMesh(child)}
if(child.isLine===!0){parseLine(child)}
if(child.isPoints===!0){parsePoints(child)}});return output}}
THREE.OBJExporter=OBJExporter})()