private void ConvertProceduralTexture(Texture sourceTex) { //Debug.Log("Converting " + sourceTex.name); ProceduralTexture sourceTexture = (Texture)sourceTex as ProceduralTexture; Color32[] pixels = sourceTexture.GetPixels32(0, 0, sourceTex.width, sourceTex.height); Texture2D destTex = new Texture2D(sourceTexture.width, sourceTexture.height) { //Copy options from substance texture name = sourceTexture.name, anisoLevel = sourceTexture.anisoLevel, filterMode = sourceTexture.filterMode, mipMapBias = sourceTexture.mipMapBias, wrapMode = sourceTexture.wrapMode }; destTex.SetPixels32(pixels); //Convert normal map to Unity format if (sourceTex.name.Contains("_normal")) { Color targetColor = new Color(); for (int x = 0; x < sourceTex.width; x++) { for (int y = 0; y < sourceTex.height; y++) { //Red is stored in the alpha component targetColor.r = destTex.GetPixel(x, y).a; //Green channel, already inverted in Substance Designer targetColor.g = destTex.GetPixel(x, y).g; //Invert blue channel targetColor.b = 1 - destTex.GetPixel(x, y).b; destTex.SetPixel(x, y, targetColor); } } } destTex.Apply(); string path = targetFolder + "/" + substance.name + "/" + destTex.name + ".png"; File.WriteAllBytes(path, destTex.EncodeToPNG()); //Refresh the database so the NormalMapImporter runs if (sourceTex.name.Contains("_normal")) { AssetDatabase.Refresh(); } //Debug.Log("Written file to: " + path); }
static int GetPixels32(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 5); ProceduralTexture obj = LuaScriptMgr.GetNetObject <ProceduralTexture>(L, 1); int arg0 = (int)LuaScriptMgr.GetNumber(L, 2); int arg1 = (int)LuaScriptMgr.GetNumber(L, 3); int arg2 = (int)LuaScriptMgr.GetNumber(L, 4); int arg3 = (int)LuaScriptMgr.GetNumber(L, 5); Color32[] o = obj.GetPixels32(arg0, arg1, arg2, arg3); LuaScriptMgr.PushArray(L, o); return(1); }
private float[,] GetHeightsFromTexture(ProceduralTexture proceduralTexture) { float[,] heightData = new float[proceduralTexture.width, proceduralTexture.height]; Color32[] textureData = proceduralTexture.GetPixels32(0, 0, proceduralTexture.width, proceduralTexture.height); for (int x = 0; x < proceduralTexture.width; x++) { for (int y = 0; y < proceduralTexture.height; y++) { heightData[x, y] = (float)(textureData[(y * proceduralTexture.width) + x].r) / 256.0f; } } return(heightData); }
// Generate height mesh public void BuildHeightMask(ProceduralTexture texture) { Color32[] pixels = texture.GetPixels32(0, 0, texture.width, texture.height); float obstructionRcp = 1.0f / (c_maxObstruction - c_minObstruction); int iDst = 0; for (int y = 0; y < c_height; y++) { for (int x = 0; x < c_width; x++) { int iColor = (x * texture.width) / c_width + ((y * texture.height) / c_height) * texture.width; float heightFrac = (float)(pixels[iColor].r - c_minObstruction) * obstructionRcp; m_obstructions[iDst] = Mathf.Clamp01(1.0f - heightFrac); iDst++; } } m_hasObstruction = true; }
private void ConvertProceduralTexture(Texture sourceTex) { //Debug.Log("Converting " + sourceTex.name); ProceduralTexture sourceTexture = (Texture)sourceTex as ProceduralTexture; Color32[] pixels = sourceTexture.GetPixels32(0, 0, sourceTex.width, sourceTex.height); Texture2D destTex = new Texture2D(sourceTexture.width, sourceTexture.height) { //Copy options from substance texture name = sourceTexture.name, anisoLevel = sourceTexture.anisoLevel, filterMode = sourceTexture.filterMode, mipMapBias = sourceTexture.mipMapBias, wrapMode = sourceTexture.wrapMode }; destTex.SetPixels32(pixels); //Convert normal map to Unity format if (sourceTex.name.Contains("_normal")) { Color targetColor = new Color(); for (int x = 0; x < sourceTex.width; x++) { for (int y = 0; y < sourceTex.height; y++) { //Red is stored in the alpha component targetColor.r = destTex.GetPixel(x, y).a; //Green channel, already inverted in Substance Designer targetColor.g = destTex.GetPixel(x, y).g; //Full blue channel targetColor.b = 1; //Full alpha targetColor.a = 1; destTex.SetPixel(x, y, targetColor); } } } destTex.Apply(); string targetFolder = AssetDatabase.GetAssetPath(material); //Material root folder targetFolder = targetFolder.Replace(material.name + ".mat", string.Empty); //Create Textures folder if it doesn't exist if (!Directory.Exists(targetFolder + "Textures/")) { Debug.Log("Directory: " + targetFolder + "Textures/" + " doesn't exist, creating..."); Directory.CreateDirectory(targetFolder + "Textures/"); AssetDatabase.Refresh(); } //Append textures folder targetFolder += "Textures/"; //Remove Substance material name from texture name destTex.name = destTex.name.Replace(substance.name, string.Empty); //Compose file path string path = targetFolder + material.name + destTex.name + "_baked.png"; File.WriteAllBytes(path, destTex.EncodeToPNG()); AssetDatabase.Refresh(); //Trigger SWSImporter AssetDatabase.ImportAsset(path, ImportAssetOptions.Default); //Grab a reference to the newly saved files if (sourceTex.name.Contains("_normal")) { normalmap = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)); } else if (sourceTex.name.Contains("_shadermap")) { shadermap = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)); } //Debug.Log("Written file to: " + path); }
private IEnumerator CreateTerrainMesh() { m_ProceduralMaterial.SetProceduralFloat("$randomseed", m_RandomSeed); m_ProceduralMaterial.isReadable = true; m_ProceduralMaterial.RebuildTexturesImmediately(); if (m_ProceduralMaterial.isProcessing) { yield return(null); } yield return(null); ProceduralTexture heightTexture = m_ProceduralMaterial.GetGeneratedTexture("TerrainBuilder_height"); if (heightTexture == null) { Debug.LogErrorFormat("Error reading Procedural Height Texture: {0}", m_ProceduralMaterial.name); yield break; } Color32[] heightData = heightTexture.GetPixels32(0, 0, heightTexture.width, heightTexture.height); MeshFilter meshFilter = this.gameObject.AddComponent <MeshFilter>(); Mesh mesh = new Mesh(); meshFilter.mesh = mesh; int width = heightTexture.width - 1; int height = heightTexture.height - 1; // build mesh m_vertices = new Vector3[(width + 1) * (height + 1)]; Vector2[] uv = new Vector2[m_vertices.Length]; Vector4[] tangents = new Vector4[m_vertices.Length]; Vector4 t = new Vector4(1f, 0f, 0f, -1f); for (int i = 0, z = 0; z <= height; z++) { for (int x = 0; x <= width; x++, i++) { float tx = ((float)x / (float)heightTexture.width) * m_TerrainSize.x; int heightIdx = (z * heightTexture.width) + x; float ty = ((float)(heightData[heightIdx].r) / 255.0f) * m_TerrainSize.y; float tz = ((float)z / (float)heightTexture.height) * m_TerrainSize.z; m_vertices[i] = new Vector3(tx, ty, tz); if (i > 0) { t = Vector3.Normalize(m_vertices[i] - m_vertices[i - 1]); } tangents[i] = new Vector4(t.x, t.y, t.z, -1f); uv[i] = new Vector2((float)x / width, (float)z / height); } } mesh.vertices = m_vertices; mesh.uv = uv; int[] triangles = new int[width * height * 6]; for (int ti = 0, vi = 0, y = 0; y < height; y++, vi++) { for (int x = 0; x < width; x++, ti += 6, vi++) { triangles[ti] = vi; triangles[ti + 3] = triangles[ti + 2] = vi + 1; triangles[ti + 4] = triangles[ti + 1] = vi + width + 1; triangles[ti + 5] = vi + width + 2; } } mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); // color material MeshRenderer meshRenderer = this.gameObject.AddComponent <MeshRenderer>(); meshRenderer.material = m_TerrainMaterial; // center terrain this.transform.position = new Vector3(-m_TerrainSize.x * 0.5f, -m_TerrainSize.y * 0.5f, -m_TerrainSize.z * 0.5f); if (m_WaterSimulation != null) { m_WaterSimulation.BuildHeightMask(heightTexture); } m_heightData = heightData; m_heightDataWidth = heightTexture.width; m_heightDataHeight = heightTexture.height; }