// The two texture sets are equal if they are using the same // textures/color properties for each map and have the same // tiling for each of those color properties internal bool IsEqual(object obj, bool fixOutOfBoundsUVs, TextureCombinerNonTextureProperties resultMaterialTextureBlender) { if (!(obj is MaterialPropTexturesSet)) { return(false); } MaterialPropTexturesSet other = (MaterialPropTexturesSet)obj; if (other.ts.Length != ts.Length) { return(false); } else { for (int i = 0; i < ts.Length; i++) { if (ts[i].matTilingRect != other.ts[i].matTilingRect) { return(false); } if (!ts[i].AreTexturesEqual(other.ts[i])) { return(false); } if (!resultMaterialTextureBlender.NonTexturePropertiesAreEqual(matsAndGOs.mats[0].mat, other.matsAndGOs.mats[0].mat)) { return(false); } } //IMPORTANT don't use Vector2 != Vector2 because it is only acurate to about 5 decimal places //this can lead to tiled rectangles that can't accept rectangles. if (fixOutOfBoundsUVs && (obUVoffset.x != other.obUVoffset.x || obUVoffset.y != other.obUVoffset.y)) { return(false); } if (fixOutOfBoundsUVs && (obUVscale.x != other.obUVscale.x || obUVscale.y != other.obUVscale.y)) { return(false); } return(true); } }
/// <summary> /// Джх╬ Atlas /// </summary> /// <returns></returns> public Texture2D DoRenderAtlas(GameObject gameObject, int width, int height, int padding, Rect[] rss, List <MaterialPropTexturesSet> textureSetss, int indexOfTexSetToRenders, ShaderTextureProperty texPropertyname, TextureCombinerNonTextureProperties resultMaterialTextureBlender, bool isNormalMap, bool fixOutOfBoundsUVs, bool considerNonTextureProperties, TextureCombineHandler texCombiner) { textureSets = textureSetss; indexOfTexSetToRender = indexOfTexSetToRenders; _texPropertyName = texPropertyname; _padding = padding; _isNormalMap = isNormalMap; _fixOutOfBoundsUVs = fixOutOfBoundsUVs; _resultMaterialTextureBlender = resultMaterialTextureBlender; rs = rss; Shader s; if (_isNormalMap) { s = Shader.Find("MeshBaker/NormalMapShader"); } else { s = Shader.Find("MeshBaker/AlbedoShader"); } if (s == null) { Debug.LogError("Could not find shader for RenderTexture. Try reimporting mesh baker"); return(null); } mat = new Material(s); _destinationTexture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32); _destinationTexture.filterMode = FilterMode.Point; myCamera = gameObject.GetComponent <Camera>(); myCamera.orthographic = true; myCamera.orthographicSize = height >> 1; myCamera.aspect = ((float)width) / height; myCamera.targetTexture = _destinationTexture; myCamera.clearFlags = CameraClearFlags.Color; Transform camTransform = myCamera.GetComponent <Transform>(); camTransform.localPosition = new Vector3(width / 2.0f, height / 2f, 3); camTransform.localRotation = Quaternion.Euler(0, 180, 180); _doRenderAtlas = true; Debug.Log(string.Format("Begin Camera.Render destTex w={0} h={1} camPos={2} camSize={3} camAspect={4}", width, height, camTransform.localPosition, myCamera.orthographicSize, myCamera.aspect.ToString("f5"))); //This triggers the OnRenderObject callback myCamera.Render(); _doRenderAtlas = false; MeshBakerUtility.Destroy(mat); MeshBakerUtility.Destroy(_destinationTexture); Debug.Log("Finished Camera.Render "); Texture2D tempTex = targTex; targTex = null; return(tempTex); }
private void CopyScaledAndTiledToAtlas(MaterialPropTexturesSet texSet, MaterialPropTexture source, Vector2 obUVoffset, Vector2 obUVscale, Rect rec, ShaderTextureProperty texturePropertyName, TextureCombinerNonTextureProperties resultMatTexBlender, bool yIsFlipped) { Rect r = rec; myCamera.backgroundColor = resultMatTexBlender.GetColorForTemporaryTexture(texSet.matsAndGOs.mats[0].mat, texturePropertyName); //yIsFlipped = true; //if (yIsFlipped) //{ //} r.y = 1f - (r.y + r.height); // DrawTexture uses topLeft 0,0, Texture2D uses bottomLeft 0,0 r.x *= _destinationTexture.width; r.y *= _destinationTexture.height; r.width *= _destinationTexture.width; r.height *= _destinationTexture.height; Rect rPadded = r; rPadded.x -= _padding; rPadded.y -= _padding; rPadded.width += _padding * 2; rPadded.height += _padding * 2; Rect targPr = new Rect(); Rect srcPrTex = texSet.ts[indexOfTexSetToRender].GetEncapsulatingSamplingRect().GetRect(); if (!_fixOutOfBoundsUVs) { Debug.Assert(source.matTilingRect.GetRect() == texSet.ts[indexOfTexSetToRender].GetEncapsulatingSamplingRect().GetRect()); } Texture2D tex = source.GetTexture2D(); /* * if (_considerNonTextureProperties && resultMatTexBlender != null) * { * if (LOG_LEVEL >= MB2_LogLevel.trace) Debug.Log(string.Format("Blending texture {0} mat {1} with non-texture properties using TextureBlender {2}", tex.name, texSet.mats[0].mat, resultMatTexBlender)); * * resultMatTexBlender.OnBeforeTintTexture(texSet.mats[0].mat, texturePropertyName.name); * //combine the tintColor with the texture * tex = combiner._createTextureCopy(tex); * for (int i = 0; i < tex.height; i++) * { * Color[] cs = tex.GetPixels(0, i, tex.width, 1); * for (int j = 0; j < cs.Length; j++) * { * cs[j] = resultMatTexBlender.OnBlendTexturePixel(texturePropertyName.name, cs[j]); * } * tex.SetPixels(0, i, tex.width, 1, cs); * } * tex.Apply(); * } */ //main texture TextureWrapMode oldTexWrapMode = tex.wrapMode; if (srcPrTex.width == 1f && srcPrTex.height == 1f && srcPrTex.x == 0f && srcPrTex.y == 0f) { //fixes bug where there is a dark line at the edge of the texture tex.wrapMode = TextureWrapMode.Clamp; } else { tex.wrapMode = TextureWrapMode.Repeat; } Debug.Log("DrawTexture tex=" + tex.name + " destRect=" + r + " srcRect=" + srcPrTex + " Mat=" + mat); //fill the padding first Rect srcPr = new Rect(); //top margin srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y + 1 - 1f / tex.height; srcPr.width = srcPrTex.width; srcPr.height = 1f / tex.height; targPr.x = r.x; targPr.y = rPadded.y; targPr.width = r.width; targPr.height = _padding; RenderTexture oldRT = RenderTexture.active; RenderTexture.active = _destinationTexture; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //bot margin srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y; srcPr.width = srcPrTex.width; srcPr.height = 1f / tex.height; targPr.x = r.x; targPr.y = r.y + r.height; targPr.width = r.width; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //left margin srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y; srcPr.width = 1f / tex.width; srcPr.height = srcPrTex.height; targPr.x = rPadded.x; targPr.y = r.y; targPr.width = _padding; targPr.height = r.height; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //right margin srcPr.x = srcPrTex.x + 1f - 1f / tex.width; srcPr.y = srcPrTex.y; srcPr.width = 1f / tex.width; srcPr.height = srcPrTex.height; targPr.x = r.x + r.width; targPr.y = r.y; targPr.width = _padding; targPr.height = r.height; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //top left corner srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y + 1 - 1f / tex.height; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = rPadded.x; targPr.y = rPadded.y; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //top right corner srcPr.x = srcPrTex.x + 1f - 1f / tex.width; srcPr.y = srcPrTex.y + 1 - 1f / tex.height; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = r.x + r.width; targPr.y = rPadded.y; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //bot left corner srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = rPadded.x; targPr.y = r.y + r.height; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //bot right corner srcPr.x = srcPrTex.x + 1f - 1f / tex.width; srcPr.y = srcPrTex.y; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = r.x + r.width; targPr.y = r.y + r.height; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //now the texture Graphics.DrawTexture(r, tex, srcPrTex, 0, 0, 0, 0, mat); RenderTexture.active = oldRT; tex.wrapMode = oldTexWrapMode; }
public NonTexturePropertiesBlendProps(TextureCombinerNonTextureProperties textureProperties, TextureBlender resultMats) { resultMaterialTextureBlender = resultMats; _textureProperties = textureProperties; }
public NonTexturePropertiesDontBlendProps(TextureCombinerNonTextureProperties textureProperties) { _textureProperties = textureProperties; }
public bool AllTexturesAreSameForMerge(MaterialPropTexturesSet other, bool considerNonTextureProperties, TextureCombinerNonTextureProperties resultMaterialTextureBlender) { if (other.ts.Length != ts.Length) { return(false); } else { if (!other.allTexturesUseSameMatTiling || !allTexturesUseSameMatTiling) { return(false); } // must use same set of textures int idxOfFirstNoneNull = -1; for (int i = 0; i < ts.Length; i++) { if (!ts[i].AreTexturesEqual(other.ts[i])) { return(false); } if (idxOfFirstNoneNull == -1 && !ts[i].isNull) { idxOfFirstNoneNull = i; } if (considerNonTextureProperties) { if (!resultMaterialTextureBlender.NonTexturePropertiesAreEqual(matsAndGOs.mats[0].mat, other.matsAndGOs.mats[0].mat)) { return(false); } } } if (idxOfFirstNoneNull != -1) { //check that all textures are the same. Have already checked all tiling is same for (int i = 0; i < ts.Length; i++) { if (!ts[i].AreTexturesEqual(other.ts[i])) { return(false); } } } return(true); } }
public TextureCombinerMerging(bool considerNonTextureProps, TextureCombinerNonTextureProperties resultMaterialTexBlender, bool fixObUVs) { _considerNonTextureProperties = considerNonTextureProps; resultMaterialTextureBlender = resultMaterialTexBlender; fixOutOfBoundsUVs = fixObUVs; }