- Home /
Why does my opaque texture seem to be transparent?
I'm having a strange issue where I have set all objects to use a standard opaque material (with their own textures) but when two objects are overlapping, they appear to be transparent. That is, the two textures are simultaneously visible and seem to be combined.
This is the material in question (the texture is applied in code):
Below are 2 overlapping objects with the same texture. The darker diamond shape is where there is NO overlap. The 'brighter' areas are where the two objects overlap. I don't know if it matters, but the objects are custom meshes (rather than sprites).
My question is, how can I make the textures fully opaque (such that you would not see the diamond)? I tried changing the shader to various other values, but doing so caused the shadows cast and received by the objects to disappear.
UPDATE 1
Here is the function which constructs the object mesh (note that texture and material are set prior to this call. texture is of type Texture2D ):
override public void Construct() {
mesh.Clear();
float halfHeight = Mathf.Sqrt (0.75f);
Vector3[] Vertices = {
new Vector3( 0f, 0f, 0f),//Z //FRONT
new Vector3( 1f, 0f, 0f),//A
new Vector3( 0.5f, -halfHeight, 0f),//B
new Vector3(-0.5f, -halfHeight, 0f),//C
new Vector3(-1f, 0f, 0f),//D
new Vector3(-0.5f, halfHeight, 0f),//E
new Vector3( 0.5f, halfHeight, 0f),//F
new Vector3( 0f, 0f, 0f),//Z //BACK
new Vector3( 1f, 0f, 0f),//A
new Vector3( 0.5f, -halfHeight, 0f),//B
new Vector3(-0.5f, -halfHeight, 0f),//C
new Vector3(-1f, 0f, 0f),//D
new Vector3(-0.5f, halfHeight, 0f),//E
new Vector3( 0.5f, halfHeight, 0f) //F
};
Vector2 scale = new Vector2 (0.5f, 0.5f);
float unitWidth = scale.x;
float unitHeight = scale.y;// * Mathf.Sqrt (0.75f);
float unitHalfWidth = scale.x * 0.5f;
Vector2 FZ = new Vector2 (offset.x, offset.y);
Vector2 FA = new Vector2 (offset.x + unitWidth, offset.y);
Vector2 FB = new Vector2 (offset.x + unitHalfWidth, offset.y - unitHeight);
Vector2 FC = new Vector2 (offset.x - unitHalfWidth, offset.y - unitHeight);
Vector2 FD = new Vector2 (offset.x - unitWidth, offset.y);
Vector2 FE = new Vector2 (offset.x - unitHalfWidth, offset.y + unitHeight);
Vector2 FF = new Vector2 (offset.x + unitHalfWidth, offset.y + unitHeight);
Vector2[] UV = {
FZ, FA, FB, FC, FD, FE, FF,
FZ, FA, FB, FC, FD, FE, FF
};
int[] Triangles = {
0,1,2,0,2,3,0,3,4,0,4,5,0,5,6,0,6,1,
7,9,8,7,10,9,7,11,10,7,12,11,7,13,12,7,8,13
};
Vector3[] Normals = new Vector3[14];
for (int i=0; i<=14; i+=3) {
Vector3 left = Vertices[Triangles[i+1]] - Vertices[Triangles[i]];
Vector3 right = Vertices[Triangles[i+2]] - Vertices[Triangles[i]];
Normals[i] = Vector3.Cross(left, right);
}
GetComponent<MeshFilter>().mesh = mesh;
mesh.vertices = Vertices;
mesh.uv = UV;
mesh.normals = Normals;
mesh.triangles = Triangles;
MeshFilter filter = GetComponent<MeshFilter>();
filter.mesh = mesh;
mesh.RecalculateBounds ();
mesh.RecalculateNormals ();
mesh.Optimize ();
MeshCollider mc = gameObject.GetComponent<MeshCollider> ();
mc.sharedMesh = mesh;
GetComponent<Renderer>().material = material;
GetComponent<Renderer>().material.SetTexture ("_MainTex", texture);
}
UPDATE 2
I checked and the material.color is set to RGBA(1.0,1.0,1.0,1.0) which makes this even more baffling.
Could lighting be causing this issue?
I believe you would have to use sprites for this to work.
Why would that be? Should an opaque texture on a 3D mesh be transparent?
It gets called right after I set all the vertices/normals/triangles/uv, is there somewhere else I should be doing it? I'll update the question with the initializer.
Answer by $$anonymous$$ · Sep 28, 2015 at 07:52 PM
Opaque allows transparency. You can control it by applying alpha values in a picture editor.
The texture files are already fully opaque, I'm not sure that editing the texture file will help. This was helpful though, as until now I didn't understand that opaque would allow transparency.
Answer by Teadaddy · Sep 28, 2015 at 07:29 PM
do the textures (.png?.tif?) have variable opacity?
I'm not sure what you mean. The textures are png files, but they are fully opaque when I open them in GI$$anonymous$$P.
one other thing to check is on import, it may be that you have use alpha from grayscale as transparency - if you look at the texture in the project view that option is found in the inspector. Just a thought, good luck
Your answer
Follow this Question
Related Questions
Cutout Material is not showing texture transparency? 0 Answers
Blending between arbitrary numbers of textures on the same material 0 Answers
Shader Shadow Effect Issues 0 Answers
CRT/LCD screen shader (for materials) 4 Answers
White dots between two cubes? 0 Answers