Kaynağa Gözat

Add replaceLights, replaceCameras, replaceMaterials option in processRaw in asset importer.

master
Palash Bansal 2 yıl önce
ebeveyn
işleme
b90d702064
No account linked to committer's email address

+ 42
- 17
src/assetmanager/AssetManager.ts Dosyayı Görüntüle

Cache as threeCache, Cache as threeCache,
Camera, Camera,
EventDispatcher, EventDispatcher,
Light,
LinearFilter, LinearFilter,
LinearMipmapLinearFilter, LinearMipmapLinearFilter,
LoadingManager, LoadingManager,
import {IAsset} from './IAsset' import {IAsset} from './IAsset'
import { import {
AddObjectOptions, AddObjectOptions,
AmbientLight2,
DirectionalLight2,
HemisphereLight2,
ICamera, ICamera,
iCameraCommons, iCameraCommons,
ILight,
iLightCommons,
IMaterial, IMaterial,
iMaterialCommons, iMaterialCommons,
IObject3D, IObject3D,
ISceneEvent, ISceneEvent,
ITexture, ITexture,
PerspectiveCamera2, PerspectiveCamera2,
PointLight2,
RectAreaLight2,
SpotLight2,
upgradeTexture, upgradeTexture,
} from '../core' } from '../core'
import {Importer} from './Importer' import {Importer} from './Importer'
this.importer.addEventListener('processRawStart', (event)=>{ this.importer.addEventListener('processRawStart', (event)=>{
// console.log('preprocess mat', mat) // console.log('preprocess mat', mat)
const res = event.data! const res = event.data!
const options = event.options! as ProcessRawOptions
// if (!res.assetType) { // if (!res.assetType) {
// if (res.isBufferGeometry) { // for eg stl todo // if (res.isBufferGeometry) { // for eg stl todo
// res = new Mesh(res, new MeshStandardMaterial()) // res = new Mesh(res, new MeshStandardMaterial())
// } // }
// } // }
if (res.isObject3D) { if (res.isObject3D) {
// todo replace lights
// if (res.isLight) {
// res = upgradeThreejsLight(res)
// } else {
// const lights: any[] = []
// res.traverse((rr: any)=>{
// if (rr !== res && rr.isLight) lights.push(rr)
// })
// for (const light of lights) {
// upgradeThreejsLight(light)
// }
// res = new Object3DModel(res, options as any)
// }

const cameras: Camera[] = [] const cameras: Camera[] = []
const lights: Light[] = []
res.traverse((obj: any) => { res.traverse((obj: any) => {
if (obj.material) { if (obj.material) {
const materials = Array.isArray(obj.material) ? obj.material : [obj.material] const materials = Array.isArray(obj.material) ? obj.material : [obj.material]
const newMaterials = [] const newMaterials = []
for (const material of materials) { for (const material of materials) {
const mat = this.materials.convertToIMaterial(material) || material
const mat = this.materials.convertToIMaterial(material, {createFromTemplate: options.replaceMaterials !== false}) || material
mat.uuid = material.uuid mat.uuid = material.uuid
mat.userData.uuid = material.uuid mat.userData.uuid = material.uuid
newMaterials.push(mat) newMaterials.push(mat)
else obj.material = newMaterials[0] else obj.material = newMaterials[0]
} }
if (obj.isCamera) cameras.push(obj) if (obj.isCamera) cameras.push(obj)
if (obj.isLight) lights.push(obj)
}) })
for (const camera of cameras) { for (const camera of cameras) {
if ((camera as PerspectiveCamera2).assetType === 'camera') continue
// todo: OrthographicCamera // todo: OrthographicCamera
if (!(camera as PerspectiveCamera).isPerspectiveCamera || !camera.parent) {
if (!(camera as PerspectiveCamera).isPerspectiveCamera || !camera.parent || options.replaceCameras === false) {
iCameraCommons.upgradeCamera.call(camera) iCameraCommons.upgradeCamera.call(camera)
} else { } else {
const newCamera: ICamera = (camera as any).iCamera ?? new PerspectiveCamera2('', this.viewer.canvas)
const newCamera: ICamera = (camera as any).iCamera ??
new PerspectiveCamera2('', this.viewer.canvas)
if (camera === newCamera) continue if (camera === newCamera) continue
camera.parent.children.splice(camera.parent.children.indexOf(camera), 1, newCamera) camera.parent.children.splice(camera.parent.children.indexOf(camera), 1, newCamera)
newCamera.parent = camera.parent as any newCamera.parent = camera.parent as any
// console.log('replacing camera', camera, newCamera) // console.log('replacing camera', camera, newCamera)
} }
} }
for (const light of lights) {
// @ts-expect-error update three-ts-types
if ((light as ILight).assetType === 'light') continue
if (!light.parent || options.replaceLights === false) {
iLightCommons.upgradeLight.call(light)
} else {
const newLight: ILight|undefined = (light as any).iLight ??
(light as any).isDirectionalLight ? new DirectionalLight2() :
(light as any).isPointLight ? new PointLight2() :
(light as any).isSpotLight ? new SpotLight2() :
(light as any).isAmbientLight ? new AmbientLight2() :
(light as any).isHemisphereLight ? new HemisphereLight2() :
(light as any).isRectAreaLight ? new RectAreaLight2() :
undefined
// @ts-expect-error update three-ts-types
if (light === newLight || !newLight) continue
light.parent.children.splice(light.parent.children.indexOf(light), 1, newLight)
newLight.parent = light.parent as any
newLight.copy(light as any)
light.parent = null
;(newLight as any).uuid = light.uuid
newLight.userData.uuid = light.uuid
;(light as any).iLight = newLight
}
}


iObjectCommons.upgradeObject3D.call(res) iObjectCommons.upgradeObject3D.call(res)
} else if (res.isMaterial) { } else if (res.isMaterial) {

+ 17
- 0
src/assetmanager/IAssetImporter.ts Dosyayı Görüntüle

*/ */
generateMipmaps?: boolean|undefined, generateMipmaps?: boolean|undefined,


/**
* If true, the importer will replace any three.js light instances with upgraded lights
* default = true
*/
replaceLights?: boolean, // default = true
/**
* If true, the importer will replace any three.js camera instances with upgraded cameras
* default = true
*/
replaceCameras?: boolean, // default = true
/**
* If true, the importer will replace any three.js material instances with upgraded materials
* default = true
*/
replaceMaterials?: boolean, // default = true


/** /**
* default = true, if true, the importer will automatically import the contents of zip files, if zip importer is registered. * default = true, if true, the importer will automatically import the contents of zip files, if zip importer is registered.
*/ */

Loading…
İptal
Kaydet