/* // TODO Storing of cubemaps used in soft blending switch Cubemap _curCubemap; Cubemap _targetCubemap; Cubemap _tmpCubemap; */ void Start() { // Verify and abort if no Material on this object supports Cubemaps VerifyShaderCubemapSupport(); // Store reference to nodes, abort if none present GetNodes(); // If required we will first assign the nearest node's cubemap and set it as current if(realtimeSwitching || startupSwap) { _currentNode = FindNearestNode(); StartCoroutine(SetCubemap(_currentNode)); } // Start processing cubemaps via co-routine if we are supposed to do real-time switching if(realtimeSwitching) { isAlive = true; StartCoroutine("WatchEnviroment"); } }
IEnumerator WatchEnviroment() { // If we are supposed to switch... while(isAlive) { _nearestNode = FindNearestNode(); // Nearest Node differs from current node if(_nearestNode != _currentNode) { // Make nearest node our new current _currentNode = _nearestNode; // Assign Cubemap for our Node yield return StartCoroutine(SetCubemap(_currentNode)); } yield return null; } yield return null; }
IEnumerator MakeSnapshot(Cubemap c, CubemapFace face, CubemapNode node) { // We should only read the screen buffer after rendering is complete yield return new WaitForEndOfFrame(); int width = Screen.width; int height = Screen.height; //Create the blank texture container Texture2D snapshot = new Texture2D(width, height, textureFormat, useMipMaps); snapshot.wrapMode = TextureWrapMode.Clamp; // Rectangle Area from the Camera Rect copyRect = new Rect((camera.pixelWidth * 0.5f) - (snapshot.width * 0.5f), (camera.pixelHeight * 0.5f) - (snapshot.height * 0.5f), snapshot.width, snapshot.height); //Read the current render into the texture container, snapshot snapshot.ReadPixels(copyRect, 0, 0, false); yield return null; snapshot.Apply(); // Resize our Texture snapshot = Scale(snapshot, nodeResolution, nodeResolution); // Write mirrored pixels from our Texture to Cubemap Color cubemapColor; for (int y = 0; y<nodeResolution; y++) { for (int x = 0; x<nodeResolution; x++) { cubemapColor = snapshot.GetPixel(nodeResolution + x, (nodeResolution-1) - y); c.SetPixel(face, x, y, cubemapColor); } } c.Apply(); // Optional PNG generation. Double-check it with overriding Node setting if(makePNG && node.allowGeneratePNG) { // Mirror the snapshot image for our PNG in order to be identical with the cubemap faces snapshot.SetPixels32( MirrorColor32( c.GetPixels(face) ) ); snapshot.Apply(); // Convert to PNG file byte[] bytes = snapshot.EncodeToPNG(); // Save the file string path = Application.dataPath + "/" + pathCubemapPNG + "/" + sceneName + "_" + node.name + "_" + face.ToString() + ".png"; //System.IO.File.WriteAllBytes(path, bytes); // deprecated because not available on Webplayer System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs); bw.Write(bytes); bw.Close(); fs.Close(); // Fix compression state string finalImagePath = "Assets/" + pathCubemapPNG + "/" + sceneName + "_" + node.name + "_" + face.ToString() + ".png"; if (finalImagePath.Contains("//")) finalImagePath = finalImagePath.Replace("//", "/"); AssetDatabase.Refresh(); // refresh necessary before we can use the textureimporter TextureImporter textureImporter = AssetImporter.GetAtPath(finalImagePath) as TextureImporter; if (textureImporter != null) { textureImporter.textureFormat = TextureImporterFormat.RGB24; AssetDatabase.ImportAsset(finalImagePath); } } // Delete our screenshot texture as clean up DestroyImmediate(snapshot); yield return null; }
void SetNodeResolution(CubemapNode node) { nodeResolution = (node.overrideResolution) ? node.resolution : resolution; }
// Switch Cubemap based on Node parameter IEnumerator SetCubemap(CubemapNode targetNode) { Cubemap c = targetNode.cubemap; // Don't proceed if there is no cubemap if(c == null) Debug.LogError("Failed to assign Cubemap to \" " + gameObject.name + " \" because Node " + targetNode.name + " seems to have not stored a corresponding Cubemap in cubemap variable."); else { // Instant switching SetCubemapToMaterials(c); Debug.Log("Assigned Cubemap " + c.name + " successfully to \"" + gameObject.name + "\" Shader!"); /* // TODO Soft-blend cubemap over time if(softBlending) { _targetCubemap = c; // Get the base reflection color to go back to after fade Color colorOriginal = renderer.sharedMaterial.GetColor("_ReflectColor"); // Counter float elapsedTime = 0f; float startTime = Time.time; bool isCounting = false; isCounting = true; while(isCounting) { elapsedTime = Time.time - startTime; if(elapsedTime < fadeTime) { // TODO Fade Debug.Log(elapsedTime + " - still fading..."); } // Stop Timer and assign final texture else { SetCubemapToMaterials(c); isCounting = false; } yield return null; } //Debug.Log("Timer expired, outside Loop!"); // After Fade set target to current _curCubemap = _targetCubemap; } // Instant switching else { SetCubemapToMaterials(c); Debug.Log("Assigned Cubemap " + c.name + " successfully to \"" + gameObject.name + "\" Shader!"); } */ } yield return null; }