Browse Source

Better bump scale fix

master
Palash Bansal 1 year ago
parent
commit
2a26d9504f
No account linked to committer's email address

+ 2
- 2
package-lock.json View File

{ {
"name": "threepipe", "name": "threepipe",
"version": "0.0.43",
"version": "0.0.44",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "threepipe", "name": "threepipe",
"version": "0.0.43",
"version": "0.0.44",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"@types/three": "https://github.com/repalash/three-ts-types/releases/download/v0.158.1002/package.tgz", "@types/three": "https://github.com/repalash/three-ts-types/releases/download/v0.158.1002/package.tgz",

+ 1
- 1
package.json View File

{ {
"name": "threepipe", "name": "threepipe",
"version": "0.0.43",
"version": "0.0.44",
"description": "A modern 3D viewer framework built on top of three.js, written in TypeScript, designed to make creating high-quality, modular, and extensible 3D experiences on the web simple and enjoyable.", "description": "A modern 3D viewer framework built on top of three.js, written in TypeScript, designed to make creating high-quality, modular, and extensible 3D experiences on the web simple and enjoyable.",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.mjs", "module": "dist/index.mjs",

+ 2
- 1
plugins/3d-tiles-renderer/package-lock.json View File

} }
}, },
"../../node_modules/three": { "../../node_modules/three": {
"version": "0.157.1007",
"version": "0.158.1001",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@puppeteer/browsers": "^1.4.1", "@puppeteer/browsers": "^1.4.1",
"@rollup/plugin-terser": "^0.4.0", "@rollup/plugin-terser": "^0.4.0",
"chalk": "^5.2.0", "chalk": "^5.2.0",
"concurrently": "^8.0.1", "concurrently": "^8.0.1",
"dpdm": "^3.14.0",
"eslint": "^8.37.0", "eslint": "^8.37.0",
"eslint-config-mdcs": "^5.0.0", "eslint-config-mdcs": "^5.0.0",
"eslint-plugin-compat": "^4.1.2", "eslint-plugin-compat": "^4.1.2",

+ 16
- 15
src/assetmanager/MaterialManager.ts View File

import {ColorManagement, Event, EventDispatcher, EventListener2, Material, Texture} from 'three'
import {ColorManagement, Event, EventDispatcher, EventListener2, Material, ShaderChunk, Texture} from 'three'
import { import {
IMaterial, IMaterial,
iMaterialCommons, iMaterialCommons,
import {MaterialExtension} from '../materials' import {MaterialExtension} from '../materials'
import {generateUUID, isInScene} from '../three' import {generateUUID, isInScene} from '../three'
import {IMaterialEventMap} from '../core/IMaterial' import {IMaterialEventMap} from '../core/IMaterial'
import {shaderReplaceString} from '../utils'


/** /**
* Material Manager * Material Manager


constructor() { constructor() {
super() super()
legacyBumpScaleFixSetup()
} }


/** /**
const lastColorManagementEnabled = ColorManagement.enabled const lastColorManagementEnabled = ColorManagement.enabled
if (legacyColors) ColorManagement.enabled = false if (legacyColors) ColorManagement.enabled = false


// bump map scale fix
// https://github.com/repalash/three.js/commit/7b13bb515866f6a002928bd28d0a793cafeaeb1a
const legacyBumpScale = (oldMaterial as any)?.metadata && (oldMaterial as any)?.metadata.version <= 4.6
if (legacyBumpScale && (oldMaterial as any)?.bumpScale !== undefined && (oldMaterial as any)?.bumpMap) {
// if (Math.abs((oldMaterial as any).bumpScale) > 0.01) {
// (oldMaterial as any).bumpScale *= 430 // test model - http://asset-samples.threepipe.org/tests/bumpmap_normalize_migrate.glb
// console.warn('MaterialManager: Old format material loaded, bump map scaled by 430, it might be incorrect.', (oldMaterial as any).bumpScale)
// } else {
console.warn('MaterialManager: Old format material loaded, bump map might be incorrect.', oldMaterial, (oldMaterial as any).bumpScale)
// }
}



const material = template.generator(template.params || {}) const material = template.generator(template.params || {})
if (oldMaterial && material) material.setValues(oldMaterial, true) if (oldMaterial && material) material.setValues(oldMaterial, true)




} }


function legacyBumpScaleFixSetup() {
const a = `
vec3 vSigmaX = normalize( dFdx( surf_pos.xyz ) );
vec3 vSigmaY = normalize( dFdy( surf_pos.xyz ) );
`
ShaderChunk.bumpmap_pars_fragment = shaderReplaceString(ShaderChunk.bumpmap_pars_fragment, a, `
#ifdef BUMP_MAP_SCALE_LEGACY
${a.replace(/normalize/g, '')}
#else
${a}
#endif
`)
}

+ 12
- 2
src/assetmanager/gltf/GLTFMaterialExtrasExtension.ts View File

* Also {@link Export} * Also {@link Export}
* @param loadConfigResources * @param loadConfigResources
*/ */
static Import = (loadConfigResources: (res: any)=>any)=> (_: GLTFParser): GLTFLoaderPlugin=>({
static Import = (loadConfigResources: (res: any)=>any)=> (parser: GLTFParser): GLTFLoaderPlugin=>({
name: '__' + GLTFMaterialExtrasExtension.WebGiMaterialExtrasExtension, // __ is prefix so that the extension is added to userdata, and we can process later in afterRoot name: '__' + GLTFMaterialExtrasExtension.WebGiMaterialExtrasExtension, // __ is prefix so that the extension is added to userdata, and we can process later in afterRoot
afterRoot: async(result: GLTF) => { afterRoot: async(result: GLTF) => {
const scenes = result.scenes || (result.scene ? [result.scene] : []) const scenes = result.scenes || (result.scene ? [result.scene] : [])


s.traverse((obj: any)=>{ s.traverse((obj: any)=>{
const o = obj?.material const o = obj?.material
if (!o?.isMaterial) return
if (!o?.isMaterial) return // todo array materials
const ext = o.userData?.gltfExtensions?.[GLTFMaterialExtrasExtension.WebGiMaterialExtrasExtension] const ext = o.userData?.gltfExtensions?.[GLTFMaterialExtrasExtension.WebGiMaterialExtrasExtension]
if (!ext) return if (!ext) return




delete o.userData.gltfExtensions[GLTFMaterialExtrasExtension.WebGiMaterialExtrasExtension] delete o.userData.gltfExtensions[GLTFMaterialExtrasExtension.WebGiMaterialExtrasExtension]


// legacy bump map scale fix, test model - test model - http://asset-samples.threepipe.org/tests/bumpmap_normalize_migrate.glb
const assetVersion = parser.json?.asset?.version ? parseFloat(parser.json?.asset?.version) : null
// https://github.com/repalash/three.js/commit/7b13bb515866f6a002928bd28d0a793cafeaeb1a
if ((o.userData.legacyBumpScale || assetVersion && assetVersion <= 2.0) && (o as any)?.bumpScale !== undefined && o?.bumpMap && o.defines) {
console.warn('MaterialManager: Old format material loaded, bump map might be incorrect.', o, (o as any).bumpScale)
o.defines.BUMP_MAP_SCALE_LEGACY = '1'
o.userData.legacyBumpScale = true
o.needsUpdate = true
}

}) })


// todo: check for resources that are not used and dispose them? see todo in ThreeViewer.fromJSON // todo: check for resources that are not used and dispose them? see todo in ThreeViewer.fromJSON

+ 0
- 25
src/assetmanager/import/GLTFLoader2.ts View File

if (res.cameras) res.cameras.forEach(c => !c.parent && scene.add(c)) if (res.cameras) res.cameras.forEach(c => !c.parent && scene.add(c))
if (res.asset) scene.userData.gltfAsset = res.asset // todo: put back in gltf in GLTFExporter2 if (res.asset) scene.userData.gltfAsset = res.asset // todo: put back in gltf in GLTFExporter2


// todo
if (res.asset?.version) {
const version = parseFloat(res.asset.version)
if (version <= 2.0) {
// bump map scale fix
// https://github.com/repalash/three.js/commit/7b13bb515866f6a002928bd28d0a793cafeaeb1a
// const bounds = new Box3B().expandByObject(scene)
// const size = bounds.getSize(new Vector3()).length() / 2
const fixBump = (material: any) => {
if (!material || !material.bumpMap || material.bumpScale === undefined) return
// if (Math.abs(material.bumpScale) > 0.01) {
// material.bumpScale *= 430 / Math.max(size, 0.1) // test model - http://asset-samples.threepipe.org/tests/bumpmap_normalize_migrate.glb
// console.warn(`GLTFLoader2: Old format material loaded, bump map scaled by ${430 / Math.max(size, 0.1)}, it might be incorrect.`, material, material.bumpScale)
// } else {
console.warn('GLTFLoader2: Old format material loaded, bump map might be incorrect.', material, material.bumpScale)
// }
}
scene?.traverse((obj: any)=>{
if (!obj?.material) return
const mats = Array.isArray(obj.material) ? obj.material : [obj.material]
mats.forEach(fixBump)
})
}
}

return scene return scene
} }



+ 10
- 0
src/core/material/iMaterialCommons.ts View File



if (userData) copyMaterialUserData(this.userData, userData) if (userData) copyMaterialUserData(this.userData, userData)


// bump map scale fix todo: move this to Material.fromJSON
// https://github.com/repalash/three.js/commit/7b13bb515866f6a002928bd28d0a793cafeaeb1a
const legacyBumpScale = (parameters as any)?.metadata && (parameters as any)?.metadata.version <= 4.6
if ((legacyBumpScale || this.userData.legacyBumpScale) && (this as any)?.bumpScale !== undefined && this?.bumpMap && this.defines) {
console.warn('MaterialManager: Old format material loaded, bump map might be incorrect.', parameters, (parameters as any).bumpScale)
this.defines.BUMP_MAP_SCALE_LEGACY = '1'
this.userData.legacyBumpScale = true
this.needsUpdate = true
}

if (legacyColors) ColorManagement.enabled = lastColorManagementEnabled if (legacyColors) ColorManagement.enabled = lastColorManagementEnabled


this.setDirty?.() this.setDirty?.()

+ 6
- 0
src/plugins/material/shaders/CustomBumpMapPlugin.glsl View File

#ifndef USE_BUMPMAP #ifndef USE_BUMPMAP
vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, float faceDirection ) { vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, float faceDirection ) {


#ifdef BUMP_MAP_SCALE_LEGACY
vec3 vSigmaX = ( dFdx( surf_pos.xyz ) );
vec3 vSigmaY = ( dFdy( surf_pos.xyz ) );
#else
// normalize is done to ensure that the bump map looks the same regardless of the texture's scale // normalize is done to ensure that the bump map looks the same regardless of the texture's scale
vec3 vSigmaX = normalize( dFdx( surf_pos.xyz ) ); vec3 vSigmaX = normalize( dFdx( surf_pos.xyz ) );
vec3 vSigmaY = normalize( dFdy( surf_pos.xyz ) ); vec3 vSigmaY = normalize( dFdy( surf_pos.xyz ) );
#endif

vec3 vN = surf_norm; // normalized vec3 vN = surf_norm; // normalized


vec3 R1 = cross( vSigmaY, vN ); vec3 R1 = cross( vSigmaY, vN );

+ 5
- 0
src/plugins/material/shaders/NoiseBumpMaterialPlugin.pars.glsl View File



vec3 perturbNormalArb_nb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, float faceDirection ) { vec3 perturbNormalArb_nb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, float faceDirection ) {


#ifdef BUMP_MAP_SCALE_LEGACY
vec3 vSigmaX = ( dFdx( surf_pos.xyz ) );
vec3 vSigmaY = ( dFdy( surf_pos.xyz ) );
#else
// normalize is done to ensure that the bump map looks the same regardless of the texture's scale // normalize is done to ensure that the bump map looks the same regardless of the texture's scale
vec3 vSigmaX = normalize( dFdx( surf_pos.xyz ) ); vec3 vSigmaX = normalize( dFdx( surf_pos.xyz ) );
vec3 vSigmaY = normalize( dFdy( surf_pos.xyz ) ); vec3 vSigmaY = normalize( dFdy( surf_pos.xyz ) );
#endif
vec3 vN = surf_norm; // normalized vec3 vN = surf_norm; // normalized


vec3 R1 = cross( vSigmaY, vN ); vec3 R1 = cross( vSigmaY, vN );

+ 1
- 1
src/viewer/version.ts View File

export const VERSION = '0.0.43'
export const VERSION = '0.0.44'

Loading…
Cancel
Save