// This runs everything, and is set so that when the event is triggered, it cascades the wealth of commands it needs to public static GameObject GenerateTexturePrefab() { // Grab the Room Texture folder // Grab the CameraLocation folder // // Get the textures out of the texture folder and make materials out of them // Get the camera locations and generate projectors // Get the room mesh from wherever it is stored // Instantiate the room mesh and save it to the scene // Instantiate the projectors and save them to the scene // Make the projectors children of the mesh // Make the mesh and all its children a prefab - save this prefab // Save the scene and asset database // Alpha clip adjust and SAVE the textures foreach (string rawTexFile in Directory.GetFiles(Constants.Folders.RoomTextureFolderPath)) { Texture2D rawRoomTex = LoadTexture.Load(rawTexFile); AlphaClipTexture.AlphaClip(rawRoomTex); } // Create an empty GameObject to house all of the Texture Projectors GameObject RoomTextureObject = new GameObject(); RoomTextureObject.name = Constants.Names.TexturePrefabName; LayerSwitcher.SetLayer(RoomTextureObject, Constants.Names.LayerName); // Load up all alpha clipped textures to create materials foreach (string clippedTexFile in Directory.GetFiles(Constants.Folders.ClippedRoomTextureFolderPath)) { string clippedRoomTexName = Path.GetFileNameWithoutExtension(clippedTexFile); // Generate the projector and its GameObject container // Attach the container onto the overall RoomTextureObject as a child GameObject ProjectorChild = new GameObject(); ProjectorChild.transform.parent = RoomTextureObject.transform; Projector textureProjector = TextureProjector.GenerateProjector( ProjectorChild, // GameObject to house the projector Constants.Folders.CameraLocationFolderPath + FileNameTranslator.ClippedTextureToCameraLocation(clippedRoomTexName), // Camera Location file path clippedTexFile // Clipped texture file path ); ProjectorChild.name = textureProjector.name; } #if UNITY_EDITOR // Create a prefab of all the projectors GameObject prefab = PrefabUtility.CreatePrefab(Constants.Folders.PrefabFolderPath + RoomTextureObject.name, RoomTextureObject); SaveTexturePrefab.Save(); AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); // ERROR TESTING - is this the correct order? #else GameObject prefab = null; #endif return(prefab); }
public static Material GenerateRoomMaterial(Texture2D tex) { Material mat = new Material(Shader.Find(Constants.Shaders.RoomTexture)); mat.SetTexture("_ProjectionTexture", tex); mat.name = FileNameTranslator.ClippedTextureToMaterial(tex.name); return(mat); }
public static Texture2D AlphaClip(Texture2D tex) { // Safeguard against bad input bool debug = Constants.DebugStrings.DebugFlag; if (tex == null) { if (debug) { Debug.Log("AlphaClip() encountered a null texture."); } throw new System.Exception(Constants.ErrorStrings.NullTexture); } // Run method if (debug) { // Display startup of method Debug.Log("Starting AlphaClip() method."); // Display texture info if (tex != null) { Debug.Log("AlphaClip() received texture\n" + "\tName: " + tex.name + "\n" + "\tWidth: " + tex.width + "\n" + "\tHeight: " + tex.height + "\n"); } } // Texture format options limited because of Unity method Texture2D clippedTex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false, false); // Prevent texture from tiling in projector clippedTex.wrapMode = TextureWrapMode.Clamp; // Adjust the pixels Color[] pixels = tex.GetPixels(); // Make horizontal edge pixels transparent for (int i = 0; i < clippedTex.width; i++) { // Overwrite the bottom row of pixels int bottomRowPixel = i; pixels[bottomRowPixel] = new Color( pixels[bottomRowPixel].r, // Same red pixels[bottomRowPixel].g, // Same green pixels[bottomRowPixel].b, // Same blue 0 // Make pixel transparent ); // Overwrite the top row of pixels int topRowPixel = clippedTex.width * (clippedTex.height - 1) + i; pixels[topRowPixel] = new Color( pixels[topRowPixel].r, // Same red pixels[topRowPixel].g, // Same green pixels[topRowPixel].b, // Same blue 0 // Make pixel transparent ); } // Make vertical edge pixels transparent for (int j = 0; j < clippedTex.height; j++) { //Debug.Log("j = " + j + "; index = " + (j * tex.width + tex.width - 1).ToString()); // Overwrite the left column of pixels int leftColumnPixel = j * clippedTex.width; pixels[leftColumnPixel] = new Color( pixels[leftColumnPixel].r, // Same red pixels[leftColumnPixel].g, // Same green pixels[leftColumnPixel].b, // Same blue 0 // Make pixel transparent ); // Overwrite the right column of pixels int rightColumnPixel = j * clippedTex.width + clippedTex.width - 1; pixels[rightColumnPixel] = new Color( pixels[rightColumnPixel].r, // Same red pixels[rightColumnPixel].g, // Same green pixels[rightColumnPixel].b, // Same blue 0 // Make pixel transparent ); } // Set adjusted colors for new texture clippedTex.SetPixels(pixels); // Adjust name of texture to desired name clippedTex.name = FileNameTranslator.TextureToClippedTexture(tex.name); // ERROR TESTING REMOVE //clippedTex.name = tex.name; // Save the texture SaveTexture.Save(clippedTex, Constants.Suffixes.ImageSuffixTypes.PNG, Constants.Folders.ClippedRoomTextureFolderPath); if (debug) { Debug.Log("AlphaClip() has saved the clipped texture to " + Constants.Folders.ClippedRoomTextureFolderPath + "."); } // Reload the Unity Asset database #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); #endif if (debug) { Debug.Log("AlphaClip() has refreshed the Asset database to show the new clipped texture."); } return(clippedTex); }
// This assumes that all information found on // https://developer.microsoft.com/en-us/windows/holographic/locatable_camera // is correct. public static Projector GenerateProjector(string CameraLocationFilePath, string TextureFilePath) { Projector newProjector = null; CameraLocation camLoc = CameraLocation.Load(CameraLocationFilePath); if (camLoc.HasLocationData) { newProjector = new Projector(); // Set the main aspects of the projector int horizontalFOV = CameraLocation.GetHorizontalFOV(Constants.Camera.CameraResolution()); // in degrees newProjector.aspectRatio = 16.0f / 9.0f; newProjector.fieldOfView = horizontalFOV / newProjector.aspectRatio; newProjector.orthographic = false; newProjector.nearClipPlane = camLoc.NearClipPlane; newProjector.farClipPlane = camLoc.FarClipPlane; // Set the position, rotation, and scale of the projector // This information was found on a forum post at http://answers.unity3d.com/questions/402280/how-to-decompose-a-trs-matrix.html newProjector.transform.position = camLoc.WorldToCameraTransform.GetColumn(3); newProjector.transform.rotation = Quaternion.LookRotation( camLoc.WorldToCameraTransform.GetColumn(2), camLoc.WorldToCameraTransform.GetColumn(1) ); newProjector.transform.localScale = new Vector3( camLoc.WorldToCameraTransform.GetColumn(0).magnitude, camLoc.WorldToCameraTransform.GetColumn(1).magnitude, camLoc.WorldToCameraTransform.GetColumn(2).magnitude ); // Generate and set the material Texture2D tex = LoadTexture.Load(TextureFilePath); //Material mat = MaterialMaker.GenerateRoomMaterial(tex, RoomTexture.Projector_MaterialAutoName + camLoc.name); Material mat = MaterialMaker.GenerateRoomMaterial(tex); #if UNITY_EDITOR // ERROR TESTING - DOESN'T EVEN APPLY TO FINAL BUILD SaveMaterial.Save(mat); #endif // AssetDatabase.CreateAsset(mat, RoomTexture.MaterialFolderPath + mat.name); // AssetDatabase.Refresh(); newProjector.material = mat; // Set the projector's name //newProjector.name = RoomTexture.Projector_AutoName + camLoc.name; newProjector.name = FileNameTranslator.ClippedTextureToProjector(tex.name); // Set it to ignore all layers except for the one it needs to project onto int layerID = LayerMask.NameToLayer(Constants.Names.LayerName); int ignoreLayer = 1 << layerID; ignoreLayer = ~ignoreLayer; newProjector.ignoreLayers = ignoreLayer; // ERROR TESTING REMOVE //AssetDatabase.CreateAsset(newProjector, Constants.Folders.ProjectorFolderPath + FileNameTranslator.ClippedTextureToProjector(tex.name)); //AssetDatabase.SaveAssets(); } return(newProjector); }
public static void GetShaderArrays(out Texture2DArray texArray, out Matrix4x4[] worldToCameraMatrixArray, out Matrix4x4[] projectionMatrixArray) { string[] clippedTexFiles = Directory.GetFiles(Constants.Folders.ClippedRoomTextureFolderPath); string[] cameraLocFiles = Directory.GetFiles(Constants.Folders.CameraLocationFolderPath); Resolution camRes = Constants.Camera.CameraResolution(); Resolution downgradedRes = GetDowngradedResolution(camRes); int numLegitimateTextureFiles = 0; foreach (string texFile in clippedTexFiles) { string fileExtension = Path.GetExtension(texFile); if (!fileExtension.Equals(Constants.Suffixes.FileSuffix_PNG) && !fileExtension.Equals(Constants.Suffixes.FileSuffix_JPG)) { continue; } ++numLegitimateTextureFiles; } if (clippedTexFiles.Length > 0) { texArray = new Texture2DArray(downgradedRes.width, downgradedRes.height, numLegitimateTextureFiles, Constants.Camera.Format, false); } else { texArray = null; } worldToCameraMatrixArray = new Matrix4x4[cameraLocFiles.Length]; projectionMatrixArray = new Matrix4x4[cameraLocFiles.Length]; int arrayIndex = 0; foreach (string texFile in clippedTexFiles) { string fileExtension = Path.GetExtension(texFile); if (!fileExtension.Equals(Constants.Suffixes.FileSuffix_PNG) && !fileExtension.Equals(Constants.Suffixes.FileSuffix_JPG)) { continue; } // ERROR TESTING REMOVE // Adjust the texture filepath to ready it for loading by Resources.Load, which requires a relative path with NO file extension //string correctFilepath = Constants.Folders.ClippedRoomTextureFolderPath_Load + Path.GetFileNameWithoutExtension(texFile); Texture2D tex = LoadTexture.Load(texFile); Texture2D downgradedTex = DowngradeTexture(tex, downgradedRes); // copy texture into texture array texArray.SetPixels32(downgradedTex.GetPixels32(), arrayIndex); // ERROR TESTING REMOVE //Graphics.CopyTexture(downgradedTex, 0, 0, texArray, arrayIndex, 0); CameraLocation camLoc = CameraLocation.Load(Constants.Folders.CameraLocationFolderPath + FileNameTranslator.ClippedTextureToCameraLocation(Path.GetFileNameWithoutExtension(texFile)) + Constants.Suffixes.FileSuffix_CameraLocation); if (camLoc != null) { worldToCameraMatrixArray[arrayIndex] = camLoc.WorldToCameraTransform; projectionMatrixArray[arrayIndex] = camLoc.ProjectionTransform; } ++arrayIndex; } }