BRFExporter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * BRFExporter.js
  3. *
  4. * @author realor
  5. */
  6. import { Solid } from '../core/Solid.js'
  7. import { Profile } from '../core/Profile.js'
  8. import { Cord } from '../core/Cord.js'
  9. import { SolidGeometry } from '../core/SolidGeometry.js'
  10. import { ProfileGeometry } from '../core/ProfileGeometry.js'
  11. import { CordGeometry } from '../core/CordGeometry.js'
  12. import { Formula } from '../formula/Formula.js'
  13. import { ObjectUtils } from '../utils/ObjectUtils.js'
  14. import * as THREE from '../lib/three.module.js'
  15. class BRFExporter {
  16. static VERSION = 4
  17. constructor() {
  18. this.options = {}
  19. }
  20. parse(object) {
  21. const dateString = new Date().toISOString()
  22. let model = {
  23. metadata: {
  24. format: '4DAGE FILE (brf)',
  25. version: BRFExporter.VERSION,
  26. creation: dateString,
  27. agent: navigator.userAgent
  28. },
  29. geometries: {},
  30. materials: {},
  31. objects: {},
  32. root: { type: '#object', id: String(object.id) }
  33. }
  34. this.export(object, model)
  35. return JSON.stringify(model)
  36. }
  37. export(object, model) {
  38. const id = String(object.id)
  39. let entry = {
  40. id: id,
  41. type: object.type,
  42. name: object.name,
  43. visible: object.visible,
  44. castShadow: object.castShadow,
  45. receiveShadow: object.receiveShadow
  46. }
  47. model.objects[id] = entry
  48. let position = object.position
  49. if (position.x !== 0 || position.y !== 0 || position.z !== 0) {
  50. entry.position = this.exportVector(position)
  51. }
  52. let rotation = object.rotation
  53. if (rotation.x !== 0 || rotation.y !== 0 || rotation.z !== 0) {
  54. entry.rotation = this.exportEuler(rotation)
  55. }
  56. let scale = object.scale
  57. if (scale.x !== 1.0 || scale.y !== 1.0 || scale.z !== 1.0) {
  58. entry.scale = this.exportVector(scale)
  59. }
  60. if (Object.keys(object.userData).length > 0) {
  61. entry.userData = object.userData
  62. }
  63. if (object instanceof Solid) {
  64. entry.edgesVisible = object.edgesVisible
  65. entry.facesVisible = object.facesVisible
  66. }
  67. let exportGeometry = !(object instanceof THREE.Sprite)
  68. let exportChildren = ObjectUtils.isExportableChildren(object)
  69. if (object.builder) {
  70. entry.builder = this.exportBuilder(object.builder)
  71. exportGeometry = exportGeometry && !object.builder.isGeometryBuilder(object)
  72. exportChildren = exportChildren && !object.builder.isChildrenBuilder(object)
  73. }
  74. let controllers = object.controllers
  75. if (controllers) {
  76. entry.controllers = {}
  77. for (let key in controllers) {
  78. let controller = controllers[key]
  79. entry.controllers[key] = this.exportController(controller)
  80. }
  81. }
  82. let geometry = object.geometry
  83. if (geometry && exportGeometry) {
  84. this.exportGeometry(geometry, model)
  85. entry.geometry = { type: '#geometry', id: String(geometry.id) }
  86. }
  87. let material = object.material
  88. if (material) {
  89. this.exportMaterial(material, model)
  90. entry.material = { type: '#material', id: String(material.id) }
  91. }
  92. const formulas = Formula.getAll(object)
  93. if (formulas) {
  94. entry.formulas = {}
  95. for (let path in formulas) {
  96. let formula = formulas[path]
  97. entry.formulas[formula.path] = formula.expression
  98. }
  99. }
  100. if (exportChildren) {
  101. for (let child of object.children) {
  102. if (ObjectUtils.isExportable(child)) {
  103. this.export(child, model)
  104. if (entry.children === undefined) {
  105. entry.children = []
  106. }
  107. entry.children.push({ type: '#object', id: String(child.id) })
  108. }
  109. }
  110. }
  111. }
  112. exportGeometry(geometry, model) {
  113. const id = String(geometry.id)
  114. if (typeof model.geometries[id] === 'undefined') {
  115. let entry = {
  116. id: id
  117. }
  118. if (geometry instanceof SolidGeometry) {
  119. entry.type = 'SolidGeometry'
  120. entry.vertices = []
  121. for (let vertex of geometry.vertices) {
  122. entry.vertices.push(this.exportVector(vertex))
  123. }
  124. entry.faces = []
  125. entry.isManifold = geometry.isManifold
  126. entry.smoothAngle = geometry.smoothAngle
  127. for (let face of geometry.faces) {
  128. if (face.holes.length === 0) {
  129. entry.faces.push(face.indices)
  130. } else {
  131. let loops = [face.indices]
  132. for (let hole of face.holes) {
  133. loops.push(hole.indices)
  134. }
  135. entry.faces.push(loops)
  136. }
  137. }
  138. } else if (geometry instanceof ProfileGeometry) {
  139. const path = geometry.path
  140. entry.type = 'ProfileGeometry'
  141. entry.isClosed = geometry.isClosed()
  142. const points = path.getPoints()
  143. entry.points = []
  144. for (let point of points) {
  145. entry.points.push(this.exportVector(point))
  146. }
  147. if (path instanceof THREE.Shape) {
  148. entry.holes = []
  149. const holes = path.getPointsHoles()
  150. for (let hole of holes) {
  151. const holePoints = []
  152. entry.holes.push(holePoints)
  153. for (let point of hole) {
  154. holePoints.push(this.exportVector(point))
  155. }
  156. }
  157. }
  158. } else if (geometry instanceof CordGeometry) {
  159. entry.type = 'CordGeometry'
  160. entry.points = []
  161. for (let point of geometry.points) {
  162. entry.points.push({ x: point.x, y: point.y, z: point.z })
  163. }
  164. } else if (geometry instanceof THREE.BufferGeometry) {
  165. entry.type = 'BufferGeometry'
  166. entry.attributes = {}
  167. const attributes = geometry.attributes
  168. for (let name in attributes) {
  169. let attribute = attributes[name]
  170. entry.attributes[name] = {
  171. type: attribute.constructor.name,
  172. arrayType: attribute.array.constructor.name,
  173. itemSize: attribute.itemSize,
  174. normalized: attribute.normalized,
  175. array: this.exportBufferAttributeArray(attribute)
  176. }
  177. }
  178. }
  179. model.geometries[id] = entry
  180. }
  181. }
  182. exportMaterial(material, model) {
  183. const id = String(material.id)
  184. if (typeof model.materials[id] === 'undefined') {
  185. let entry = {
  186. id: id,
  187. type: material.type,
  188. name: material.name,
  189. opacity: material.opacity,
  190. transparent: material.transparent,
  191. side: material.side,
  192. depthTest: material.depthTest,
  193. depthWrite: material.depthWrite,
  194. polygonOffset: material.polygonOffset,
  195. polygonOffsetFactor: material.polygonOffsetFactor,
  196. polygonOffsetUnits: material.polygonOffsetUnits
  197. }
  198. if (material.color) {
  199. entry.color = '#' + material.color.getHexString()
  200. }
  201. if (material instanceof THREE.MeshPhongMaterial) {
  202. entry.specular = '#' + material.specular.getHexString()
  203. entry.emissive = '#' + material.emissive.getHexString()
  204. entry.shininess = material.shininess
  205. entry.reflectivity = material.reflectivity
  206. }
  207. if (material instanceof THREE.SpriteMaterial) {
  208. entry.sizeAttenuation = material.sizeAttenuation
  209. }
  210. if (material.map) {
  211. entry.map = { type: 'Texture', image: material.map.name }
  212. }
  213. model.materials[id] = entry
  214. }
  215. }
  216. exportBuilder(builder) {
  217. const builderEntry = {}
  218. builderEntry.type = builder.constructor.name
  219. this.exportProperties(builder, builderEntry)
  220. return builderEntry
  221. }
  222. exportController(controller) {
  223. const controllerEntry = {}
  224. controllerEntry.type = controller.constructor.name
  225. this.exportProperties(controller, controllerEntry, 'name', 'object')
  226. return controllerEntry
  227. }
  228. exportProperties(element, entry, ...exclude) {
  229. const properties = Object.keys(element)
  230. for (let property of properties) {
  231. if (!property.startsWith('_') && !exclude.includes(property)) {
  232. let value = element[property]
  233. let type = typeof value
  234. if (type === 'number' || type === 'string' || type === 'boolean') {
  235. entry[property] = value
  236. } else if (value instanceof THREE.Vector3 || value instanceof THREE.Vector2) {
  237. entry[property] = this.exportVector(value)
  238. } else if (value instanceof THREE.Euler) {
  239. entry[property] = this.exportEuler(value)
  240. } else if (value instanceof THREE.Color) {
  241. entry[property] = this.exportColor(value)
  242. } else if (value instanceof THREE.Object3D) {
  243. entry[property] = { type: '#object', id: String(value.id) }
  244. }
  245. }
  246. }
  247. return entry
  248. }
  249. exportVector(vector) {
  250. const v = { x: vector.x, y: vector.y }
  251. if (vector instanceof THREE.Vector3) {
  252. v.z = vector.z
  253. }
  254. return v
  255. }
  256. exportEuler(euler) {
  257. return { type: 'Euler', x: euler.x, y: euler.y, z: euler.z }
  258. }
  259. exportColor(color) {
  260. return '#' + color.getHexString()
  261. }
  262. exportBufferAttributeArray(attribute) {
  263. const compress = attribute.name !== 'index' && (this.options.enableBufferGeometryCompression === undefined || this.options.enableBufferGeometryCompression === true)
  264. const array = attribute.array
  265. const precision7 = array instanceof Float32Array
  266. if (compress) {
  267. const compressedArray = []
  268. const itemSize = attribute.itemSize
  269. const map = new Map()
  270. for (let i = 0; i < array.length; i += itemSize) {
  271. let vector = []
  272. for (let k = 0; k < itemSize; k++) {
  273. if (precision7) {
  274. vector.push(parseFloat(array[i + k].toPrecision(7)))
  275. } else {
  276. vector.push(array[i + k])
  277. }
  278. }
  279. let key = vector.join(',')
  280. let itemIndex = map.get(key)
  281. if (itemIndex === undefined) {
  282. itemIndex = i / itemSize
  283. map.set(key, itemIndex)
  284. compressedArray.push(vector)
  285. } else {
  286. compressedArray.push(itemIndex)
  287. }
  288. }
  289. return compressedArray
  290. } else {
  291. if (precision7) {
  292. const directArray = []
  293. for (let i = 0; i < array.length; i++) {
  294. directArray.push(parseFloat(array[i].toPrecision(7)))
  295. }
  296. return directArray
  297. } else {
  298. return Array.from(array)
  299. }
  300. }
  301. }
  302. }
  303. export { BRFExporter }