import { WasmSceneGraph , WasmSceneNode } from "../client/wasm-scene-graph" ; import * as THREE from './node_modules/three/src/Three' ; export function benchMatrix ( ) { function buildWasmSceneGraph ( scene , childrenCount , depth ) { let count = 0 ; function addChildren ( node , d ) { if ( d > 0 ) { for ( let i = 0 ; i < childrenCount ; i ++ ) { const child = scene . createNewNode ( ) ; count ++ ; node . add ( child ) ; addChildren ( child , d - 1 ) ; } } } addChildren ( scene . root , depth ) ; } function buildTHREEScene ( scene , childrenCount , depth ) { let count = 0 ; function addChildren ( node , d ) { if ( d > 0 ) { for ( let i = 0 ; i < childrenCount ; i ++ ) { const child = new THREE . Object3D ( ) ; child . matrixAutoUpdate = false ; count ++ ; node . add ( child ) ; addChildren ( child , d - 1 ) ; } } } addChildren ( scene , depth ) ; console . log ( `${ count } has add to scene (object 3d)` ) ; } function output ( result ) { let sum = 0 ; result . forEach ( re => { sum += re ; } ) ; console . log ( "avg:" + sum / result . length ) ; } console . log ( '===========' ) ; const wasmSceneGraph = new WasmSceneGraph ( ) ; buildWasmSceneGraph ( wasmSceneGraph , 6 , 6 ) ; console . log ( "wasm graph" ) ; let wasmresult2 = [ ] ; for ( let i = 0 ; i < 50 ; i ++ ) { let t = performance . now ( ) ; wasmSceneGraph . perf_matrix ( ) ; t = performance . now ( ) - t ; wasmresult2 . push ( t ) ; } wasmresult2 = wasmresult2 . slice ( 3 ) ; output ( wasmresult2 ) ; console . log ( '===========' ) ; const scenethree = new THREE . Scene ( ) ; scenethree . matrixAutoUpdate = false ; buildTHREEScene ( scenethree , 6 , 6 ) ; console . log ( "three" ) ; let threeresult = [ ] ; for ( let i = 0 ; i < 50 ; i ++ ) { let t = performance . now ( ) ; scenethree . updateWorldMatrix ( true , true ) ; t = performance . now ( ) - t ; threeresult . push ( t ) ; } threeresult = threeresult . slice ( 3 ) ; output ( threeresult ) ; }