44using System . Text ;
55using System . Text . RegularExpressions ;
66using System . Threading . Tasks ;
7- using Unity . Collections ;
87using UnityEditor ;
98using UnityEngine ;
109using static WebRequestAsyncUtility ;
@@ -74,7 +73,7 @@ public static void ImportAssetsFromDisk() {
7473 }
7574 }
7675
77- // ask for axis siwtch behaviour
76+ // ask for axis switch behaviour
7877 if ( EditorUtility . DisplayDialog ( SwitchAxisTitle , SwitchAxisMsg , Switch , NoSwitch ) ) {
7978 SwizzleAxis = true ;
8079 } else {
@@ -165,7 +164,7 @@ public static void DownloadStumpAssets() {
165164
166165 /// <summary>
167166 /// Some scenes require switching the y and z axis in the shader.
168- /// For custom scenes this tracks whether which one should be used.
167+ /// For custom scenes this tracks, which one should be used.
169168 /// </summary>
170169 public static bool SwizzleAxis = false ;
171170
@@ -193,12 +192,6 @@ private static string GetMLPAssetPath(string objName) {
193192 Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
194193 return path ;
195194 }
196-
197- private static string GetWeightsAssetPath ( string objName , int i ) {
198- string path = $ "{ GetBasePath ( objName ) } /MLP/weightsTex{ i } .asset";
199- Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
200- return path ;
201- }
202195
203196 private static string GetFeatureTextureAssetPath ( string objName , int shapeNum , int featureNum ) {
204197 string path = $ "{ GetBasePath ( objName ) } /PNGs/shape{ shapeNum } .pngfeat{ featureNum } .png";
@@ -297,13 +290,12 @@ private static async Task ImportDemoSceneAsync(MNeRFScene scene) {
297290
298291 /// <summary>
299292 /// Set specific import settings on OBJs/PNGs.
300- /// Creates Weight Textures, Materials and Shader from MLP data.
293+ /// Creates Materials and Shader from MLP data.
301294 /// Creates a convenient prefab for the MobileNeRF object.
302295 /// </summary>
303296 private static void ProcessAssets ( string objName ) {
304297 Mlp mlp = GetMlp ( objName ) ;
305298 CreateShader ( objName , mlp ) ;
306- CreateWeightTextures ( objName , mlp ) ;
307299 // PNGs are configured in PNGImportProcessor.cs
308300 ProcessOBJs ( objName , mlp ) ;
309301 CreatePrefab ( objName , mlp ) ;
@@ -499,14 +491,6 @@ private static void ProcessOBJs(string objName, Mlp mlp) {
499491 Material material = AssetDatabase . LoadAssetAtPath < Material > ( materialAssetPath ) ;
500492 material . shader = mobileNeRFShader ;
501493
502- // assign weight textures
503- Texture2D weightsTexZero = AssetDatabase . LoadAssetAtPath < Texture2D > ( GetWeightsAssetPath ( objName , 0 ) ) ;
504- Texture2D weightsTexOne = AssetDatabase . LoadAssetAtPath < Texture2D > ( GetWeightsAssetPath ( objName , 1 ) ) ;
505- Texture2D weightsTexTwo = AssetDatabase . LoadAssetAtPath < Texture2D > ( GetWeightsAssetPath ( objName , 2 ) ) ;
506- material . SetTexture ( "weightsZero" , weightsTexZero ) ;
507- material . SetTexture ( "weightsOne" , weightsTexOne ) ;
508- material . SetTexture ( "weightsTwo" , weightsTexTwo ) ;
509-
510494 // assign feature textures
511495 string feat0AssetPath = GetFeatureTextureAssetPath ( objName , i , 0 ) ;
512496 string feat1AssetPath = GetFeatureTextureAssetPath ( objName , i , 1 ) ;
@@ -528,25 +512,26 @@ private static void ProcessOBJs(string objName, Mlp mlp) {
528512 private static void CreateShader ( string objName , Mlp mlp ) {
529513 int width = mlp . _0Bias . Length ;
530514
531- StringBuilder biasListZero = toBiasList ( mlp . _0Bias ) ;
532- StringBuilder biasListOne = toBiasList ( mlp . _1Bias ) ;
533- StringBuilder biasListTwo = toBiasList ( mlp . _2Bias ) ;
534-
535- int channelsZero = mlp . _0Weights . Length ;
536- int channelsOne = mlp . _0Bias . Length ;
537- int channelsTwo = mlp . _1Bias . Length ;
538- int channelsThree = mlp . _2Bias . Length ;
515+ StringBuilder biasListZero = toConstructorList ( mlp . _0Bias ) ;
516+ StringBuilder biasListOne = toConstructorList ( mlp . _1Bias ) ;
517+ StringBuilder biasListTwo = toConstructorList ( mlp . _2Bias ) ;
539518
540519 string shaderSource = ViewDependenceNetworkShader . Template ;
541520 shaderSource = new Regex ( "OBJECT_NAME" ) . Replace ( shaderSource , $ "{ objName } ") ;
542- shaderSource = new Regex ( "NUM_CHANNELS_ZERO" ) . Replace ( shaderSource , $ "{ channelsZero } ") ;
543- shaderSource = new Regex ( "NUM_CHANNELS_ONE" ) . Replace ( shaderSource , $ "{ channelsOne } ") ;
544- shaderSource = new Regex ( "NUM_CHANNELS_TWO" ) . Replace ( shaderSource , $ "{ channelsTwo } ") ;
545- shaderSource = new Regex ( "NUM_CHANNELS_THREE" ) . Replace ( shaderSource , $ "{ channelsThree } ") ;
546521 shaderSource = new Regex ( "BIAS_LIST_ZERO" ) . Replace ( shaderSource , $ "{ biasListZero } ") ;
547522 shaderSource = new Regex ( "BIAS_LIST_ONE" ) . Replace ( shaderSource , $ "{ biasListOne } ") ;
548523 shaderSource = new Regex ( "BIAS_LIST_TWO" ) . Replace ( shaderSource , $ "{ biasListTwo } ") ;
549524
525+ for ( int i = 0 ; i < mlp . _0Weights . Length ; i ++ ) {
526+ shaderSource = new Regex ( $ "__W0_{ i } __") . Replace ( shaderSource , $ "{ toConstructorList ( mlp . _0Weights [ i ] ) } ") ;
527+ }
528+ for ( int i = 0 ; i < mlp . _1Weights . Length ; i ++ ) {
529+ shaderSource = new Regex ( $ "__W1_{ i } __") . Replace ( shaderSource , $ "{ toConstructorList ( mlp . _1Weights [ i ] ) } ") ;
530+ }
531+ for ( int i = 0 ; i < mlp . _2Weights . Length ; i ++ ) {
532+ shaderSource = new Regex ( $ "__W2_{ i } __") . Replace ( shaderSource , $ "{ toConstructorList ( mlp . _2Weights [ i ] ) } ") ;
533+ }
534+
550535 // hack way to flip axes depending on scene
551536 string axisSwizzle = MNeRFSceneExtensions . ToEnum ( objName ) . GetAxisSwizzleString ( ) ;
552537 shaderSource = new Regex ( "AXIS_SWIZZLE" ) . Replace ( shaderSource , $ "{ axisSwizzle } ") ;
@@ -556,52 +541,12 @@ private static void CreateShader(string objName, Mlp mlp) {
556541 AssetDatabase . Refresh ( ) ;
557542 }
558543
559- private static void CreateWeightTextures ( string objName , Mlp mlp ) {
560- Texture2D weightsTexZero = createFloatTextureFromData ( mlp . _0Weights ) ;
561- Texture2D weightsTexOne = createFloatTextureFromData ( mlp . _1Weights ) ;
562- Texture2D weightsTexTwo = createFloatTextureFromData ( mlp . _2Weights ) ;
563- AssetDatabase . CreateAsset ( weightsTexZero , GetWeightsAssetPath ( objName , 0 ) ) ;
564- AssetDatabase . CreateAsset ( weightsTexOne , GetWeightsAssetPath ( objName , 1 ) ) ;
565- AssetDatabase . CreateAsset ( weightsTexTwo , GetWeightsAssetPath ( objName , 2 ) ) ;
566- AssetDatabase . SaveAssets ( ) ;
567- }
568-
569- /// <summary>
570- /// Creates a float32 texture from an array of floats.
571- /// </summary>
572- private static Texture2D createFloatTextureFromData ( double [ ] [ ] weights ) {
573- int width = weights . Length ;
574- int height = weights [ 0 ] . Length ;
575-
576- Texture2D texture = new Texture2D ( width , height , TextureFormat . RFloat , mipChain : false , linear : true ) ;
577- texture . filterMode = FilterMode . Point ;
578- texture . wrapMode = TextureWrapMode . Clamp ;
579- NativeArray < float > textureData = texture . GetRawTextureData < float > ( ) ;
580- FillTexture ( textureData , weights ) ;
581- texture . Apply ( ) ;
582-
583- return texture ;
584- }
585-
586- private static void FillTexture ( NativeArray < float > textureData , double [ ] [ ] data ) {
587- int width = data . Length ;
588- int height = data [ 0 ] . Length ;
589-
590- for ( int co = 0 ; co < height ; co ++ ) {
591- for ( int ci = 0 ; ci < width ; ci ++ ) {
592- int index = co * width + ci ;
593- double weight = data [ ci ] [ co ] ;
594- textureData [ index ] = ( float ) weight ;
595- }
596- }
597- }
598-
599- private static StringBuilder toBiasList ( double [ ] biases ) {
544+ private static StringBuilder toConstructorList ( double [ ] list ) {
600545 System . Globalization . CultureInfo culture = System . Globalization . CultureInfo . InvariantCulture ;
601- int width = biases . Length ;
546+ int width = list . Length ;
602547 StringBuilder biasList = new StringBuilder ( width * 12 ) ;
603548 for ( int i = 0 ; i < width ; i ++ ) {
604- double bias = biases [ i ] ;
549+ double bias = list [ i ] ;
605550 biasList . Append ( bias . ToString ( "F7" , culture ) ) ;
606551 if ( i + 1 < width ) {
607552 biasList . Append ( ", " ) ;
0 commit comments