public static Texture2D merge( ProceduralTexture[] colorLayers ) { if(colorLayers.Length<=0) return null; ProceduralTexture tex = colorLayers[0]; Color[] pix = new Color[tex.width * tex.height]; for(int k = 0; k < colorLayers.Length; k++){ tex = colorLayers[k]; Color[] add = tex.pixColors(); for (int y = 0; y < tex.height; y++) { for (int x = 0; x < tex.width; x++) { int i = y * tex.width + x; float fromRatio = Mathf.Clamp( add[i].a, 0.0f, 1.0f ); float toRatio = 1.0f-fromRatio; // Debug.Log("from:"+fromRatio+" to:"+toRatio); // pix[i] = new Color(color1[i].r*toRatio+color2[i].r*fromRatio, color1[i].g*toRatio+color2[i].g*fromRatio, color1[i].b*toRatio+color2[i].b*fromRatio); pix[i] = pix[i]*toRatio + add[i]*fromRatio; } } } Texture2D mergedTex = new Texture2D(tex.pixWidth, tex.pixHeight); mergedTex.SetPixels(pix); mergedTex.Apply(); return mergedTex; }
public static Task <TextureBrush> LoadMapChunkImageBrush( Vector2Ushort chunkStartPosition, uint checksum, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(null); } if (TexturesCache.TryGetValue(checksum, out var result)) { return(result); } if (Api.Client.CurrentGame.ConnectionState != ConnectionState.Connected) { // not connected - don't load the map return(null); } var tempTilesList = Api.Shared.GetTempList <Tile>(); tempTilesList.AddRange(ClientWorldService.GetWorldChunk(chunkStartPosition)); result = LoadAsync(); TexturesCache.Add(checksum, result); return(result); async Task <TextureBrush> LoadAsync() { activeTasksCount++; // create new image brush task var proceduralTexture = new ProceduralTexture( "WorldMapChunk checksum=" + checksum, // ReSharper disable once AccessToDisposedClosure request => GenerateChunkProceduralTexture(tempTilesList, chunkStartPosition, request), isTransparent: false, isUseCache: false, useThrottling: false); var brush = ClientUIService.GetTextureBrush(proceduralTexture, Stretch.Fill); //brush = ClientUIService.GetTextureBrush(new TextureResource("TestWhiteRect", isTransparent: false), Stretch.None); try { await brush.WaitLoaded(); } finally { activeTasksCount--; } return(brush); } }
public void Add(string name, ProceduralTexture texture) { if (!_textures.ContainsKey(name)) { texture.content.name = name; _textures.Add(name, texture); } }
static int GetProceduralOutputType(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 1); ProceduralTexture obj = LuaScriptMgr.GetNetObject <ProceduralTexture>(L, 1); ProceduralOutputType o = obj.GetProceduralOutputType(); LuaScriptMgr.PushEnum(L, o); return(1); }
static int GetGeneratedTexture(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 2); ProceduralMaterial obj = LuaScriptMgr.GetNetObject<ProceduralMaterial>(L, 1); string arg0 = LuaScriptMgr.GetLuaString(L, 2); ProceduralTexture o = obj.GetGeneratedTexture(arg0); LuaScriptMgr.Push(L, o); return 1; }
public void Add(string name, string shaderName, ProceduralTexture texture) { if (!_materials.ContainsKey(name)) { var mat = new Material(Shader.Find(shaderName)); mat.name = name; mat.mainTexture = texture.content; _materials.Add(mat.name, mat); } }
private float[,] GetHeightsFromRenderTexture(ProceduralTexture proceduralTexture) { float[,] heightData = new float[proceduralTexture.width, proceduralTexture.height]; // render procedual texture through a temp camera - this will allow for more real-time changes Texture2D tempTexture2D = new Texture2D(proceduralTexture.width, proceduralTexture.height, TextureFormat.ARGB32, false, false); RenderTexture tempRenderTexture = RenderTexture.GetTemporary(proceduralTexture.width, proceduralTexture.height, 24, RenderTextureFormat.ARGB32); GameObject tempCameraGO = new GameObject("RenderTerrainHeightCamera", typeof(Camera)); Camera tempCamera = tempCameraGO.GetComponent <Camera>(); tempCamera.enabled = false; tempCamera.targetTexture = tempRenderTexture; tempCamera.orthographic = true; tempCamera.orthographicSize = 0.5f; tempCamera.nearClipPlane = -1f; tempCamera.farClipPlane = 1f; tempCamera.aspect = 1f; GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad); Material quadMat = new Material(Shader.Find("Unlit/Texture")); quadMat.mainTexture = proceduralTexture; Renderer quadRenderer = quad.GetComponent <Renderer>(); quadRenderer.material = quadMat; quad.transform.parent = tempCameraGO.transform; quad.transform.localPosition = Vector3.zero; tempCamera.transform.position = new Vector3(1000f, 1000f, 1000f); tempCamera.Render(); RenderTexture.active = tempRenderTexture; tempTexture2D.ReadPixels(new Rect(0, 0, proceduralTexture.width, proceduralTexture.height), 0, 0); tempTexture2D.Apply(); Color32[] textureData = tempTexture2D.GetPixels32(); quadMat.mainTexture = tempTexture2D; for (int y = 0; y < tempTexture2D.height; y++) { for (int x = 0; x < tempTexture2D.width; x++) { heightData [y, x] = (float)(textureData[(y * x) + x].r) / 256.0f; } } // clean up temp camera render RenderTexture.active = null; RenderTexture.ReleaseTemporary(tempRenderTexture); DestroyImmediate(tempCameraGO); DestroyImmediate(quadMat); return(heightData); }
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); }
int CalculateTextureSizeBytes(Texture tTexture) { int tWidth = tTexture.width; int tHeight = tTexture.height; if (tTexture is Texture2D) { Texture2D tTex2D = tTexture as Texture2D; int bitsPerPixel = GetBitsPerPixel(tTex2D.format); int mipMapCount = tTex2D.mipmapCount; int mipLevel = 1; int tSize = 0; while (mipLevel <= mipMapCount) { tSize += tWidth * tHeight * bitsPerPixel / 8; tWidth = tWidth / 2; tHeight = tHeight / 2; mipLevel++; } return(tSize); } if (tTexture is ProceduralTexture) { ProceduralTexture tProceduralTexture = tTexture as ProceduralTexture; int bitsPerPixel = GetBitsPerPixel(tProceduralTexture.format); int mipMapCount = (int)tProceduralTexture.anisoLevel; int mipLevel = 1; int tSize = 0; while (mipLevel <= mipMapCount) { tSize += tWidth * tHeight * bitsPerPixel / 8; tWidth = tWidth / 2; tHeight = tHeight / 2; mipLevel++; } return(tSize); } if (tTexture is Cubemap) { Cubemap tCubemap = tTexture as Cubemap; int bitsPerPixel = GetBitsPerPixel(tCubemap.format); return(tWidth * tHeight * 6 * bitsPerPixel / 8); } return(0); }
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); }
public void AddToCollection(string name, ProceduralTexture texture) { if (_collections.ContainsKey(name)) { texture.content.name = name + "_" + (_collections[name].Count + 1).ToString(); _collections[name].Add(texture); } else { var list = new List<ProceduralTexture>(); texture.content.name = name + "_1"; list.Add(texture); _collections.Add(name, list); } }
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); }
static int _CreateProceduralTexture(IntPtr L) { int count = LuaDLL.lua_gettop(L); if (count == 0) { ProceduralTexture obj = new ProceduralTexture(); LuaScriptMgr.Push(L, obj); return(1); } else { LuaDLL.luaL_error(L, "invalid arguments to method: ProceduralTexture.New"); } return(0); }
public void AddToCollection(string collectionName, string shaderName, ProceduralTexture texture) { var mat = new Material(Shader.Find(shaderName)); mat.mainTexture = texture.content; if (_collections.ContainsKey(collectionName)) { mat.name = collectionName + "_" + (_collections[collectionName].Count + 1).ToString(); _collections[collectionName].Add(mat); } else { mat.name = collectionName + "_1"; var list = new List<Material>(); list.Add(mat); _collections.Add(collectionName, list); } }
protected override void OnDisable() { base.OnDisable(); if (!EditorApplication.isPlaying && !InternalEditorUtility.ignoreInspectorChanges && this.m_MightHaveModified) { this.m_MightHaveModified = false; string[] array = new string[base.targets.GetLength(0)]; int num = 0; UnityEngine.Object[] targets = base.targets; for (int i = 0; i < targets.Length; i++) { ProceduralTexture proceduralTexture = (ProceduralTexture)targets[i]; string assetPath = AssetDatabase.GetAssetPath(proceduralTexture); SubstanceImporter substanceImporter = AssetImporter.GetAtPath(assetPath) as SubstanceImporter; if (substanceImporter) { substanceImporter.OnTextureInformationsChanged(proceduralTexture); } assetPath = AssetDatabase.GetAssetPath(proceduralTexture.GetProceduralMaterial()); bool flag = false; for (int j = 0; j < num; j++) { if (array[j] == assetPath) { flag = true; break; } } if (!flag) { array[num++] = assetPath; } } for (int k = 0; k < num; k++) { SubstanceImporter substanceImporter2 = AssetImporter.GetAtPath(array[k]) as SubstanceImporter; if (substanceImporter2 && EditorUtility.IsDirty(substanceImporter2.GetInstanceID())) { AssetDatabase.ImportAsset(array[k], ImportAssetOptions.ForceUncompressedImport); } } } }
public static ITextureAtlasResource CreateProceduralTexture( [NotNull] string name, [NotNull] TextureAtlasResource sourceTextureAtlas) { var proceduralTextureResource = new ProceduralTexture( name, isTransparent: true, isUseCache: true, generateTextureCallback: request => Instance.GenerateProceduralTexture(name, sourceTextureAtlas, request), dependsOn: new[] { sourceTextureAtlas }); var columnsCount = AtlasSize.ColumnsCount; var rowsCount = AtlasSize.RowsCount; return(new TextureAtlasResource( proceduralTextureResource, columnsCount, rowsCount)); }
protected ProtoObjectDoor() { this.lazyHorizontalDoorBlueprintTexture = new Lazy <ProceduralTexture>( () => { var textureResources = new[] { this.TextureBaseHorizontal, (ITextureResource)this.AtlasTextureHorizontal.Chunk( (byte)(this.AtlasTextureHorizontal.AtlasSize.ColumnsCount - 1), 0) }; var proceduralTexture = new ProceduralTexture( "Composed blueprint " + this.Id, generateTextureCallback: request => ClientComposeHorizontalDoor(request, textureResources), isTransparent: true, isUseCache: true, dependsOn: textureResources); return(proceduralTexture); }); this.lazyVerticalDoorBlueprintTexture = new Lazy <ProceduralTexture>( () => { var atlas = this.AtlasTextureVertical; var columnChunk = (byte)(atlas.AtlasSize.ColumnsCount - 1); return(ClientProceduralTextureHelper.CreateComposedTexture( "Composed vertical door blueprint " + this.Id, isTransparent: true, isUseCache: true, textureResources: new ITextureResource[] { atlas.Chunk(columnChunk, 0), // door base atlas.Chunk((byte)(columnChunk - 1), 0), // door front part atlas.Chunk((byte)(columnChunk - 2), 0) // door back part (opened) })); }); }
private IEnumerator CreateTerrain() { 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; } TerrainData terrainHeightData = new TerrainData(); terrainHeightData.heightmapResolution = heightTexture.width; terrainHeightData.SetHeights(0, 0, GetHeightsFromTexture(heightTexture)); //terrainHeightData.SetHeights(0, 0, GetHeightsFromRenderTexture(heightTexture)); terrainHeightData.size = m_TerrainSize; GameObject newTerrainGameObject = Terrain.CreateTerrainGameObject(terrainHeightData); newTerrainGameObject.transform.position = new Vector3(-m_TerrainSize.x * 0.5f, -m_TerrainSize.y * 0.5f, -m_TerrainSize.z * 0.5f); Terrain terrain = newTerrainGameObject.GetComponent <Terrain>(); terrain.materialType = Terrain.MaterialType.BuiltInStandard; terrain.Flush(); }
static int get_format(IntPtr L) { object o = LuaScriptMgr.GetLuaObject(L, 1); if (o == null) { LuaTypes types = LuaDLL.lua_type(L, 1); if (types == LuaTypes.LUA_TTABLE) { LuaDLL.luaL_error(L, "unknown member name format"); } else { LuaDLL.luaL_error(L, "attempt to index format on a nil value"); } } ProceduralTexture obj = (ProceduralTexture)o; LuaScriptMgr.PushEnum(L, obj.format); return(1); }
public static Texture2D merge(ProceduralTexture[] colorLayers) { if (colorLayers.Length <= 0) { return(null); } ProceduralTexture tex = colorLayers[0]; Color[] pix = new Color[tex.width * tex.height]; for (int k = 0; k < colorLayers.Length; k++) { tex = colorLayers[k]; Color[] add = tex.pixColors(); for (int y = 0; y < tex.height; y++) { for (int x = 0; x < tex.width; x++) { int i = y * tex.width + x; float fromRatio = Mathf.Clamp(add[i].a, 0.0f, 1.0f); float toRatio = 1.0f - fromRatio; // Debug.Log("from:"+fromRatio+" to:"+toRatio); // pix[i] = new Color(color1[i].r*toRatio+color2[i].r*fromRatio, color1[i].g*toRatio+color2[i].g*fromRatio, color1[i].b*toRatio+color2[i].b*fromRatio); pix[i] = pix[i] * toRatio + add[i] * fromRatio; } } } Texture2D mergedTex = new Texture2D(tex.pixWidth, tex.pixHeight); mergedTex.SetPixels(pix); mergedTex.Apply(); return(mergedTex); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); if (GUI.changed) { this.m_MightHaveModified = true; } UnityEngine.Object[] targets = base.targets; for (int i = 0; i < targets.Length; i++) { ProceduralTexture proceduralTexture = (ProceduralTexture)targets[i]; if (proceduralTexture) { ProceduralMaterial proceduralMaterial = proceduralTexture.GetProceduralMaterial(); if (proceduralMaterial && proceduralMaterial.isProcessing) { base.Repaint(); SceneView.RepaintAll(); GameView.RepaintAll(); break; } } } }
public static ITextureResource GetIcon(IProtoEntity protoEntity) { if (protoEntity is null) { return(null); } var originalIcon = GetOriginalIcon(protoEntity); if (IconsCache.TryGetValue(originalIcon, out var weakReference) && weakReference.TryGetTarget(out var proceduralTexture)) { return(proceduralTexture); } proceduralTexture = new ProceduralTexture( "Crate icon " + protoEntity.ShortId, proceduralTextureRequest => GenerateIcon(proceduralTextureRequest, originalIcon), isTransparent: true, isUseCache: true, dependsOn: new[] { originalIcon }); IconsCache[originalIcon] = new WeakReference <ProceduralTexture>(proceduralTexture); return(proceduralTexture); }
// 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 CreateWindowTextures() { var color = Color.white; var th = 22; ProceduralTexture tex; int h; // 1st texture tex = new ProceduralTexture(512, 1024); tex.SetBackgroundColor(new Color32(25, 0, 143, 255)); AddBorders(ref tex, color, th); h = (tex.content.height * 3) >> 2; tex.lines.Add(new TextureLine(0, h, tex.content.width, h, color, th)); tex.lines.Add(new TextureLine(tex.content.width >> 1, 0, tex.content.width >> 1, h, color, th << 1)); tex.Draw(); TextureManager.Instance.AddToCollection("tex_window", tex); // 1st texture // 2nd texture tex = new ProceduralTexture(512, 1024); tex.SetBackgroundColor(new Color32(25, 0, 143, 255)); AddBorders(ref tex, color, th); h = (tex.content.height * 3) >> 2; tex.lines.Add(new TextureLine(0, h, tex.content.width, h, color, th)); tex.lines.Add(new TextureLine(tex.content.width >> 1, 0, tex.content.width >> 1, h, color, th << 1)); tex.lines.Add(new TextureLine(0, h >> 1, tex.content.width, h >> 1, color, th)); tex.Draw(); TextureManager.Instance.AddToCollection("tex_window", tex); // 2nd texture // 3rd texture tex = new ProceduralTexture(512, 1024); tex.SetBackgroundColor(new Color32(25, 0, 143, 255)); AddBorders(ref tex, color, th); AddBalancedLines(ref tex, 2, color, th); tex.lines.Add(new TextureLine(tex.content.width >> 1, 0, tex.content.width >> 1, tex.content.height, color, th << 1)); tex.Draw(); TextureManager.Instance.AddToCollection("tex_window", tex); // 3rd texture // 4th texture tex = new ProceduralTexture(512, 1024); tex.SetBackgroundColor(new Color32(25, 0, 143, 255)); AddBorders(ref tex, color, th); AddBalancedLines(ref tex, 3, color, th); tex.lines.Add(new TextureLine(tex.content.width >> 1, 0, tex.content.width >> 1, (tex.content.height * 3) >> 2, color, th << 1)); tex.Draw(); TextureManager.Instance.AddToCollection("tex_window", tex); // 4th texture // 5th texture tex = new ProceduralTexture(512, 1024); tex.SetBackgroundColor(new Color32(25, 0, 143, 255)); AddBorders(ref tex, color, th); AddBalancedLines(ref tex, 3, color, th); tex.lines.Add(new TextureLine(tex.content.width >> 1, 0, tex.content.width >> 1, tex.content.height, color, th << 1)); tex.Draw(); TextureManager.Instance.AddToCollection("tex_window", tex); // 5th texture }
private void AddBorders(ref ProceduralTexture tex, Color color, int thickness) { tex.lines.Add(new TextureLine(thickness >> 1, 0, thickness >> 1, tex.content.height, color, thickness)); tex.lines.Add(new TextureLine(0, thickness >> 1, tex.content.width, thickness >> 1, color, thickness)); tex.lines.Add(new TextureLine(tex.content.width - (thickness >> 1), 0, tex.content.width - (thickness >> 1), tex.content.height, color, thickness)); tex.lines.Add(new TextureLine(0, tex.content.height - (thickness >> 1), tex.content.width, tex.content.height - (thickness >> 1), color, thickness)); }
private void CreateBalconyRailTexture() { var tex = new ProceduralTexture(); tex.content = new Texture2D(1024, 512); tex.Clear(); var ratio = 2f; var outBorderSize = 0.02f; var inBorderSize = 0.018f; var spaceBetweenBorders = 0.06f; int vOutBorderWidth = Mathf.FloorToInt(tex.content.width * outBorderSize); int topOutBorderWidth = Mathf.FloorToInt(tex.content.height * outBorderSize * 2 * ratio); int vInBorderWidth = Mathf.FloorToInt(tex.content.width * inBorderSize); int hInBorderWidth = Mathf.FloorToInt(tex.content.height * inBorderSize * ratio); int vInBorderOffset = Mathf.FloorToInt(tex.content.width * spaceBetweenBorders) + vOutBorderWidth; int botInBorderOffset = Mathf.FloorToInt(tex.content.height * spaceBetweenBorders * ratio); var halfWidth = tex.content.width >> 1; int topOutBorderY = tex.content.height - (topOutBorderWidth >> 1); int botInBorderY = botInBorderOffset + (hInBorderWidth >> 1); int topInBorderY = tex.content.height - botInBorderY - topOutBorderWidth; int leftOutBorderX = (vOutBorderWidth >> 1) - 1; int rightOutBorderX = tex.content.width - leftOutBorderX; int leftInBorderX = vInBorderOffset + (vInBorderWidth >> 1); int rightInBorderX = tex.content.width - leftInBorderX; // top out border tex.lines.Add(new TextureLine(0, topOutBorderY, tex.content.width, topOutBorderY, Color.black, topOutBorderWidth)); // bot in border tex.lines.Add(new TextureLine(0, botInBorderY, tex.content.width, botInBorderY, Color.black, hInBorderWidth)); // top in border tex.lines.Add(new TextureLine(0, topInBorderY, tex.content.width, topInBorderY, Color.black, hInBorderWidth)); // left out border tex.lines.Add(new TextureLine(leftOutBorderX, 0, leftOutBorderX, tex.content.height, Color.black, vOutBorderWidth)); // right out border tex.lines.Add(new TextureLine(rightOutBorderX, 0, rightOutBorderX, tex.content.height, Color.black, vOutBorderWidth)); // left in border tex.lines.Add(new TextureLine(leftInBorderX, 0, leftInBorderX, tex.content.width, Color.black, vInBorderWidth)); // right in border tex.lines.Add(new TextureLine(rightInBorderX, 0, rightInBorderX, tex.content.width, Color.black, vInBorderWidth)); // middle border tex.lines.Add(new TextureLine(halfWidth, 0, halfWidth, tex.content.height, Color.black, vInBorderWidth)); // left inner box tex.lines.Add(new TextureLine(leftInBorderX, botInBorderY + 2, halfWidth + 1, topInBorderY, Color.black, vInBorderWidth)); tex.lines.Add(new TextureLine(leftInBorderX, topInBorderY - 1, halfWidth + 1, botInBorderY, Color.black, vInBorderWidth)); // right inner box tex.lines.Add(new TextureLine(halfWidth + 1, botInBorderY, rightInBorderX, topInBorderY, Color.black, vInBorderWidth)); tex.lines.Add(new TextureLine(halfWidth + 1, topInBorderY, rightInBorderX, botInBorderY, Color.black, vInBorderWidth)); tex.Draw(); TextureManager.Instance.Add("tex_balcony", tex); }
internal static extern bool IsSubstanceParented(ProceduralTexture texture, ProceduralMaterial material);
internal extern void OnTextureInformationsChanged(ProceduralTexture texture);
private void ConfigPortsFromMaterial( bool invalidateConnections = false, Texture[] newTextures = null ) { SetAdditonalTitleText( ( m_proceduralMaterial != null ) ? string.Format( Constants.PropertyValueLabel, m_proceduralMaterial.name ) : "Value( <None> )" ): Texture[] textures = newTextures != null ? newTextures : ( ( m_proceduralMaterial != null ) ? m_proceduralMaterial.GetGeneratedTextures() : null ): if( textures != null ) { m_firstOutputConnected = -1: string nameToRemove = m_proceduralMaterial.name + "_": m_textureTypes = new ProceduralOutputType[ textures.Length ]: for( int i = 0: i < textures.Length: i++ ) { ProceduralTexture procTex = textures[ i ] as ProceduralTexture: m_textureTypes[ i ] = procTex.GetProceduralOutputType(): WirePortDataType portType = ( m_autoNormal && m_textureTypes[ i ] == ProceduralOutputType.Normal ) ? WirePortDataType.FLOAT3 : WirePortDataType.COLOR: string newName = textures[ i ].name.Replace( nameToRemove, string.Empty ): char firstLetter = Char.ToUpper( newName[ 0 ] ): newName = firstLetter.ToString() + newName.Substring( 1 ): if( i < m_outputPorts.Count ) { m_outputPorts[ i ].ChangeProperties( newName, portType, false ): if( invalidateConnections ) { m_outputPorts[ i ].FullDeleteConnections(): } } else { AddOutputPort( portType, newName ): } } if( textures.Length < m_outputPorts.Count ) { int itemsToRemove = m_outputPorts.Count - textures.Length: for( int i = 0: i < itemsToRemove: i++ ) { int idx = m_outputPorts.Count - 1: if( m_outputPorts[ idx ].IsConnected ) { m_outputPorts[ idx ].ForceClearConnection(): } RemoveOutputPort( idx ): } } } else { int itemsToRemove = m_outputPorts.Count - 1: m_outputPorts[ 0 ].ChangeProperties( Constants.EmptyPortValue, WirePortDataType.COLOR, false ): m_outputPorts[ 0 ].ForceClearConnection(): for( int i = 0: i < itemsToRemove: i++ ) { int idx = m_outputPorts.Count - 1: if( m_outputPorts[ idx ].IsConnected ) { m_outputPorts[ idx ].ForceClearConnection(): } RemoveOutputPort( idx ): } } m_sizeIsDirty = true: m_isDirty = true: }
private void AddBalancedLines(ref ProceduralTexture tex, int count, Color color, int thickness) { int interval = tex.content.height / (count + 1); int h = 0; for (int i = 0; i < count; ++i) { h+= interval; tex.lines.Add(new TextureLine(0, h, tex.content.width, h, color, thickness)); } }
private void ConfigPortsFromMaterial(bool invalidateConnections = false, Texture[] newTextures = null) { //PreviewSizeX = ( m_proceduralMaterial != null ) ? UIUtils.ObjectField.CalcSize( new GUIContent( m_proceduralMaterial.name ) ).x + 15 : 110; m_insideSize.x = TexturePreviewSizeX + 5; SetAdditonalTitleText((m_proceduralMaterial != null) ? string.Format(Constants.PropertyValueLabel, m_proceduralMaterial.name) : string.Empty); Texture[] textures = newTextures != null ? newTextures : ((m_proceduralMaterial != null) ? m_proceduralMaterial.GetGeneratedTextures() : null); if (textures != null) { string nameToRemove = m_proceduralMaterial.name + "_"; m_textureTypes = new ProceduralOutputType[textures.Length]; for (int i = 0; i < textures.Length; i++) { ProceduralTexture procTex = textures[i] as ProceduralTexture; m_textureTypes[i] = procTex.GetProceduralOutputType(); WirePortDataType portType = (m_autoNormal && m_textureTypes[i] == ProceduralOutputType.Normal) ? WirePortDataType.FLOAT3 : WirePortDataType.COLOR; string newName = textures[i].name.Replace(nameToRemove, string.Empty); char firstLetter = Char.ToUpper(newName[0]); newName = firstLetter.ToString() + newName.Substring(1); if (i < m_outputPorts.Count) { m_outputPorts[i].ChangeProperties(newName, portType, false); if (invalidateConnections) { m_outputPorts[i].FullDeleteConnections(); } } else { AddOutputPort(portType, newName); } } if (textures.Length < m_outputPorts.Count) { int itemsToRemove = m_outputPorts.Count - textures.Length; for (int i = 0; i < itemsToRemove; i++) { int idx = m_outputPorts.Count - 1; if (m_outputPorts[idx].IsConnected) { m_outputPorts[idx].ForceClearConnection(); } RemoveOutputPort(idx); } } } else { int itemsToRemove = m_outputPorts.Count - 1; m_outputPorts[0].ChangeProperties(Constants.EmptyPortValue, WirePortDataType.COLOR, false); m_outputPorts[0].ForceClearConnection(); for (int i = 0; i < itemsToRemove; i++) { int idx = m_outputPorts.Count - 1; if (m_outputPorts[idx].IsConnected) { m_outputPorts[idx].ForceClearConnection(); } RemoveOutputPort(idx); } } m_sizeIsDirty = true; m_isDirty = true; Event.current.Use(); }
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; }
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); }
public void Set(string name, ProceduralTexture texture) { if (_textures.ContainsKey(name)) _textures[name] = texture; }