Exemple #1
0
 static void scale(ref SHEncoding sh, float s)
 {
     for (int i = 0; i < 27; ++i)
     {
         sh.c[i] *= s;
     }
 }
 public void copyFrom(SHEncoding src)
 {
     for(int i=0; i<27; ++i) {
         this.c[i] = src.c[i];
     }
     copyToBuffer();
 }
		//LOADERS
		public static bool readASH(ref SHEncoding sh, string ashPath) {
			/*# Generated with Lys by Knald Technologies, LLC - http://knaldtech.com
			# The following triplets are spherical harmonic coefficients for RGB in linear color space.

			l=0:
			m=0: 1.145362 1.249203 2.230852

			l=1:
			m=-1: 0.735545 0.874465 1.770959
			m=0: -0.000499 0.000527 0.005869
			m=1: 0.028654 0.038187 0.089021

			l=2:
			m=-2: -0.000077 0.004732 0.020932
			m=-1: 0.009634 0.014970 0.028454
			m=0: -0.190027 -0.232803 -0.469445
			m=1: -0.016092 -0.022949 -0.044332
			m=2: -0.183113 -0.226407 -0.481815
			*/
			StreamReader stream = new StreamReader(Application.dataPath + "/" + ashPath.Substring(7));
			string str = stream.ReadToEnd();
			str = str.ToLower();
			string[] lines = str.Split(new char[] {'\n','\r'});			
			int L = 0;
			int M = 0;
			bool valid = false;
			for(int i=0; i<lines.Length; ++i) {
				if( lines[i].Length == 0 ) continue;
				string line = lines[i];
				line = line.Trim();
				if( line.StartsWith("l=") ) {
					int a = line.IndexOf('=')+1;
					int b = line.IndexOf(':');
					string sub = line.Substring(a, b-a);
					L = Convert.ToInt32(sub);
				} else if( line.StartsWith("m=") ) {
					int a = line.IndexOf('=')+1;
					int b = line.IndexOf(':');
					string sub = line.Substring(a, b-a);
					M = Convert.ToInt32(sub);
					
					if(L >= 0 && (M <= L || M >= -L)) {
						valid |= true;
						
						sub = line.Substring(b+1).TrimStart();
						string[] triplet = sub.Split(new char[] {' ', '\t'});
						int j = M;
						if( L == 1 ) j = M+2;
						else if( L == 2 ) j = M+6;
						
						//Lys exports SH coefficients in Ramamoorthi's range 0-pi but our shaders need them as 0-1
						sh.c[j*3 + 0] = (float)Convert.ToDouble(triplet[0]) / 3.14159f;
						sh.c[j*3 + 1] = (float)Convert.ToDouble(triplet[1]) / 3.14159f;
						sh.c[j*3 + 2] = (float)Convert.ToDouble(triplet[2]) / 3.14159f;
					}
				}
			}
			return valid;
		}
 public void copyFrom(SHEncoding src)
 {
     for (int i = 0; i < 27; ++i)
     {
         this.c[i] = src.c[i];
     }
     copyToBuffer();
 }
 public static void convolve(ref SHEncoding sh, float conv0, float conv1, float conv2)
 {
     for( int i=0; i<27; ++i ) {
         if(i<3)			sh.c[i] *= conv0;
         else if(i<12)	sh.c[i] *= conv1;
         else 			sh.c[i] *= conv2;
     }
 }
        public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            for (int a = 0; a < importedAssets.Length; ++a)
            {
                string filePath = importedAssets[a];
                string ext      = Path.GetExtension(filePath).ToLowerInvariant();

                //images with the ignore string in their filename will not be imported
                string name = Path.GetFileNameWithoutExtension(filePath).ToLowerInvariant();
                if (name.Contains("_noimport"))
                {
                    continue;
                }

                SHProcessor.DataReader readSH = null;
                ext = ext.ToLowerInvariant();
                if (ext.Equals(".ash"))
                {
                    readSH = readASH;
                }

                if (readSH != null)
                {
                    string ashPath   = filePath;
                    string assetPath = Path.ChangeExtension(filePath, ".asset");

                    SHEncoding SH = new SHEncoding();
                    if (!readSH(ref SH, ashPath))
                    {
                        Debug.LogError("Failed to import spherical harmonics file \"" + ashPath + "\"");
                        continue;
                    }

                    SHEncodingFile SHF = AssetDatabase.LoadAssetAtPath(assetPath, typeof(SHEncodingFile)) as SHEncodingFile;

                    if (SHF)
                    {
                        SHF.SH.copyFrom(SH);
                        AssetDatabase.SaveAssets();
                    }
                    else
                    {
                        SHF = SHEncodingFile.CreateInstance <SHEncodingFile>();
                        if (!SHF)
                        {
                            Debug.LogError("Failed to create spherical harmonics asset \"" + assetPath + "\"");
                            continue;
                        }
                        SHF.SH = SH;
                        AssetDatabase.CreateAsset(SHF, assetPath);
                        AssetDatabase.Refresh();
                        AssetDatabase.ImportAsset(assetPath);
                    }
                }
            }
        }
Exemple #7
0
 public bool equals(SHEncoding other)
 {
     for (int i = 0; i < 27; ++i)
     {
         if (c[i] != other.c[i])
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #8
0
        public static void projectCubeBuffer(ref SHEncoding sh, CubeBuffer cube)
        {
            sh.clearToBlack();
            float totalarea = 0f;
            ulong faceSize  = (ulong)cube.faceSize;

            float[] dc = new float[9];
            Vector3 u  = Vector3.zero;

            for (ulong face = 0; face < 6; ++face)
            {
                for (ulong y = 0; y < faceSize; ++y)
                {
                    for (ulong x = 0; x < faceSize; ++x)
                    {
                        //compute cube direction
                        float areaweight = 1f;
                        mset.Util.invCubeLookup(ref u, ref areaweight, face, x, y, faceSize);
                        float shscale = 4f / 3f;
                        ulong index   = face * faceSize * faceSize + y * faceSize + x;
                        Color rgba    = cube.pixels[index];

                        //project on basis functions, and accumulate
                        dc[0] = project_l0_m0(u);

                        dc[1] = project_l1_mneg1(u);
                        dc[2] = project_l1_m0(u);
                        dc[3] = project_l1_m1(u);

                        dc[4] = project_l2_mneg2(u);
                        dc[5] = project_l2_mneg1(u);
                        dc[6] = project_l2_m0(u);
                        dc[7] = project_l2_m1(u);
                        dc[8] = project_l2_m2(u);
                        for (int i = 0; i < 9; ++i)
                        {
                            sh.c[3 * i + 0] += shscale * areaweight * rgba[0] * dc[i];
                            sh.c[3 * i + 1] += shscale * areaweight * rgba[1] * dc[i];
                            sh.c[3 * i + 2] += shscale * areaweight * rgba[2] * dc[i];
                        }
                        totalarea += areaweight;
                    }
                }
            }

            //complete the integration by dividing by total area
            scale(ref sh, 16f / totalarea);
        }
Exemple #9
0
        private static bool skyToGUI(Texture skyCube, bool skyHDR, mset.SHEncoding skySH, mset.CubemapGUI cubeGUI, string skyName, bool updatePreview)
        {
            bool dirty    = false;
            bool dirtyGUI = false;

            //sky -> cubeGUI
            dirtyGUI       |= cubeGUI.HDR != skyHDR;
            cubeGUI.HDR     = skyHDR;
            cubeGUI.skyName = skyName;

            RenderTexture RT = skyCube as RenderTexture;

            if (cubeGUI.input != skyCube)
            {
                if (RT)
                {
                    cubeGUI.setReference(RT, RT.useMipMap);
                }
                else if (skyCube)
                {
                    string path = AssetDatabase.GetAssetPath(skyCube);
                    cubeGUI.setReference(path, cubeGUI.mipmapped);
                }
                else
                {
                    cubeGUI.clear();
                }
                //dirty = true;
            }

            if (RT && skySH != null)
            {
                if (cubeGUI.SH != null && !skySH.equals(cubeGUI.SH))
                {
                    cubeGUI.SH.copyFrom(skySH);
                    cubeGUI.SH.copyToBuffer();
                    //dirty = true;
                }
            }

            if (dirtyGUI && updatePreview)
            {
                cubeGUI.updatePreview();
            }
            return(dirty);
        }
Exemple #10
0
 public static void convolve(ref SHEncoding sh, float conv0, float conv1, float conv2)
 {
     for (int i = 0; i < 27; ++i)
     {
         if (i < 3)
         {
             sh.c[i] *= conv0;
         }
         else if (i < 12)
         {
             sh.c[i] *= conv1;
         }
         else
         {
             sh.c[i] *= conv2;
         }
     }
 }
Exemple #11
0
		public static void OnPostprocessAllAssets ( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
			for (int a=0; a<importedAssets.Length; ++a) {
				string filePath = importedAssets[a];
				string ext = Path.GetExtension(filePath).ToLowerInvariant();
				
				//images with the ignore string in their filename will not be imported
				string name = Path.GetFileNameWithoutExtension(filePath).ToLowerInvariant();
				if(name.Contains("_noimport")) continue;

				SHProcessor.DataReader readSH = null;
				ext = ext.ToLowerInvariant();
				if( ext.Equals(".ash") ) readSH = readASH;

				if( readSH != null ) {
					string ashPath = filePath;
					string assetPath = Path.ChangeExtension(filePath, ".asset");

					SHEncoding SH = new SHEncoding();
					if( !readSH(ref SH, ashPath) ) {
						Debug.LogError("Failed to import spherical harmonics file \"" + ashPath + "\"");
						continue;
					}

					SHEncodingFile SHF = AssetDatabase.LoadAssetAtPath(assetPath, typeof(SHEncodingFile)) as SHEncodingFile;

					if(SHF) {
						SHF.SH.copyFrom(SH);
						AssetDatabase.SaveAssets();
					} else {
						SHF = SHEncodingFile.CreateInstance<SHEncodingFile>();
						if(!SHF) {
							Debug.LogError("Failed to create spherical harmonics asset \"" + assetPath + "\"");
							continue;
						}
						SHF.SH = SH;
						AssetDatabase.CreateAsset(SHF, assetPath);
						AssetDatabase.Refresh();
						AssetDatabase.ImportAsset(assetPath);
					}
				}
			}
	    }
        public static void projectCubeBuffer(ref SHEncoding sh, CubeBuffer cube)
        {
            sh.clearToBlack();
            float totalarea = 0f;
            ulong faceSize = (ulong)cube.faceSize;
            float[] dc = new float[9];
            Vector3 u = Vector3.zero;

            for(ulong face=0; face<6; ++face)
            for(ulong y=0; y<faceSize; ++y)
            for(ulong x=0; x<faceSize; ++x) {
                //compute cube direction
                float areaweight = 1f;
                mset.Util.invCubeLookup(ref u, ref areaweight, face, x, y, faceSize);
                float shscale = 4f / 3f;
                ulong index = face*faceSize*faceSize + y*faceSize + x;
                Color rgba = cube.pixels[index];

                //project on basis functions, and accumulate
                dc[0] = project_l0_m0(u);

                dc[1] = project_l1_mneg1(u);
                dc[2] = project_l1_m0(u);
                dc[3] = project_l1_m1(u);

                dc[4] = project_l2_mneg2(u);
                dc[5] = project_l2_mneg1(u);
                dc[6] = project_l2_m0(u);
                dc[7] = project_l2_m1(u);
                dc[8] = project_l2_m2(u);
                for(int i=0; i<9; ++i ) {
                    sh.c[3*i + 0] += shscale * areaweight * rgba[0] * dc[i];
                    sh.c[3*i + 1] += shscale * areaweight * rgba[1] * dc[i];
                    sh.c[3*i + 2] += shscale * areaweight * rgba[2] * dc[i];
                }
                totalarea += areaweight;
            }

            //complete the integration by dividing by total area
            scale( ref sh, 16f / totalarea );
        }
Exemple #13
0
        private static bool GUIToSky(ref Texture skyCube, ref bool skyHDR, mset.SHEncoding skySH, mset.CubemapGUI cubeGUI)
        {
            //cubeGUI -> sky
            bool    prevHDR   = cubeGUI.HDR;
            Texture prevInput = cubeGUI.input;

            cubeGUI.drawGUI();

            skyCube = cubeGUI.input;
            skyHDR  = cubeGUI.HDR;

            bool dirty = false;


            //copy spherical harmonics if they've changed
            if (cubeGUI.computeSH)
            {
                if (skySH != null)
                {
                    if (cubeGUI.SH == null)
                    {
                        skySH.clearToBlack();
                        dirty = true;
                    }
                    else if (!skySH.equals(cubeGUI.SH))
                    {
                        skySH.copyFrom(cubeGUI.SH);
                        dirty = true;
                    }
                    skySH.copyToBuffer();
                }
            }


            //return true if the cubeGUI gui changed any parameters
            dirty |= prevHDR != cubeGUI.HDR;
            dirty |= prevInput != cubeGUI.input;
            return(dirty);
        }
 public static void convolve(ref SHEncoding sh)
 {
     convolve(ref sh, 1f, 2f/3f, 0.25f);
 }
 static void scale( ref SHEncoding sh, float s )
 {
     for(int i=0; i<27; ++i) { sh.c[i] *= s; }
 }
        //LOADERS
        public static bool readASH(ref SHEncoding sh, string ashPath)
        {
            /*# Generated with Lys by Knald Technologies, LLC - http://knaldtech.com
             # The following triplets are spherical harmonic coefficients for RGB in linear color space.
             #
             # l=0:
             # m=0: 1.145362 1.249203 2.230852
             #
             # l=1:
             # m=-1: 0.735545 0.874465 1.770959
             # m=0: -0.000499 0.000527 0.005869
             # m=1: 0.028654 0.038187 0.089021
             #
             # l=2:
             # m=-2: -0.000077 0.004732 0.020932
             # m=-1: 0.009634 0.014970 0.028454
             # m=0: -0.190027 -0.232803 -0.469445
             # m=1: -0.016092 -0.022949 -0.044332
             # m=2: -0.183113 -0.226407 -0.481815
             */
            StreamReader stream = new StreamReader(Application.dataPath + "/" + ashPath.Substring(7));
            string       str    = stream.ReadToEnd();

            str = str.ToLower();
            string[] lines = str.Split(new char[] { '\n', '\r' });
            int      L     = 0;
            int      M     = 0;
            bool     valid = false;

            for (int i = 0; i < lines.Length; ++i)
            {
                if (lines[i].Length == 0)
                {
                    continue;
                }
                string line = lines[i];
                line = line.Trim();
                if (line.StartsWith("l="))
                {
                    int    a   = line.IndexOf('=') + 1;
                    int    b   = line.IndexOf(':');
                    string sub = line.Substring(a, b - a);
                    L = Convert.ToInt32(sub);
                }
                else if (line.StartsWith("m="))
                {
                    int    a   = line.IndexOf('=') + 1;
                    int    b   = line.IndexOf(':');
                    string sub = line.Substring(a, b - a);
                    M = Convert.ToInt32(sub);

                    if (L >= 0 && (M <= L || M >= -L))
                    {
                        valid |= true;

                        sub = line.Substring(b + 1).TrimStart();
                        string[] triplet = sub.Split(new char[] { ' ', '\t' });
                        int      j       = M;
                        if (L == 1)
                        {
                            j = M + 2;
                        }
                        else if (L == 2)
                        {
                            j = M + 6;
                        }

                        //Lys exports SH coefficients in Ramamoorthi's range 0-pi but our shaders need them as 0-1
                        sh.c[j * 3 + 0] = (float)Convert.ToDouble(triplet[0]) / 3.14159f;
                        sh.c[j * 3 + 1] = (float)Convert.ToDouble(triplet[1]) / 3.14159f;
                        sh.c[j * 3 + 2] = (float)Convert.ToDouble(triplet[2]) / 3.14159f;
                    }
                }
            }
            return(valid);
        }
Exemple #17
0
		public bool equals(SHEncoding other) {
			for(int i=0; i<27; ++i) {
				if( c[i] != other.c[i] ) return false;
			}
			return true;
		}
 public void projectToSH(ref SHEncoding SH)
 {
     updateBuffers();
     SH.clearToBlack();
     SHUtil.projectCubeBuffer(ref SH, this.buffers[0]);
 }
Exemple #19
0
 public static void convolve(ref SHEncoding sh)
 {
     convolve(ref sh, 1f, 2f / 3f, 0.25f);
 }