Lightmap texture in objects without static batching

[Solved]

Hello

I have some GameObjects in my scene with static flag checked. Then I bake the scene (lightmap). The lightmap it is a requirement.

Well, these objects have to be placed at a random position , so, these objects can´t be batched before changing their position.

I turn off the static flag in order to avoid static batching for these objects, but when I build on iOS, t**he lighmap texture changes depending on camera distance** (only on the iDevice, not on Editor, not on win/mac player).

However, if i use StaticBatchingUtility.Combine after changing the position of objects, the lightmap texture is ok( it doesn´t change), but then I can´t change the position of the object again.

I tried to make a copy of this objects (Instantiate), change their position, Combine it (static batching), and when I need to move again, I make a copy (again) of the original objects, replace, Combine etc… But Instantiante method and Combine method are expensive on runtime.

Also I tryed to use the Instantiate methods and Combine methods on Start() (one method per object, a lot of objects), but on iPhone3GS the loading time is over 20 seconds per scene (this is a lot of time per scene).

My best option is replace the objects on runtime without Instantiate and Combine them (without static batching on runtime or build time), but then the lightmap texture changes depending on distance if I don´t do static batching to the objects. I´m using the Mobile/Unlit shader.

any suggestions??

How to avoid the change of the lightmap texture without static batching ?

Thanks in advance

I have no idea if this would help, but it came to mind.

In your Asset Importer make sure to check the ‘Generate Lightmap UVs’ flag.

The artist import his custom UVs’ for some objects (because of padding). But with ‘Generate Lightmap UVs’ flag checked the problem is not solved ( I just try it). Thanks!

This problem only happens with OpenGL ES 1.x without static batching, with GLES 2.x it´s ok (with static batching and without static batching). The shader is Mobile/unlit,

OpenGLES 2.0 is very slower than GLES 1.x. ¿Any suggestions?

I have searching around the forums, and I found this post : http://forum.unity3d.com/threads/78337-iOS-Beast-Lightmap-and-shadow-shader-issues.

This is exactly the same problem that I have. There are a bug reported, the case number is : 428331 .

OpenGLES 2 is very slower than gles 1.x with built in shaders (I only use unlit shaders and alpha blending shaders).

Edit: I´m using Unity Pro 3.5 with iOS Pro, but I don´t use static batching and I don´t use gles2

I have found a solution.

The problem:
Making some tests, I noticed that the problem is on the shader.
“matrix [unity_LightmapMatrix]” when running on OpenGLES1 without static batching sometimes goes wrong

The solution:
I have modified the Mobile/Unlit shader in order to use two textures: one for the MainText, and other one for the Lightmap. But the lightmap texture must be null, because I will set up the texture on runTime (with scripting).

So, I don´t use

SetTexture [unity_Lightmap] {
			matrix [unity_LightmapMatrix]
			combine texture
		}

I simple use

		SetTexture [_mLight] { 
			combine texture 
		}

This is the complete shader

// Unlit shader with Lightmap. Use with lightmapSetUp.js script
// - SUPPORTS lightmap
// - no lighting
// - no per-material color

Shader "PixelRatio/Mobile/UnlitCustom" {
Properties {
	_MainTex ("Base (RGB)", 2D) = "white" {}
	_mLight ("mLight", 2D) = "white" { }
}

SubShader {
	Tags { "RenderType"="Opaque" }
	LOD 100
	
	// Non-lightmapped
	Pass {
		Tags { "LightMode" = "Vertex" }
		
		Lighting Off
		
		SetTexture [_MainTex] { combine texture } 
	}
	
	// Lightmapped, encoded as dLDR
	Pass {
		Tags {"LightMode" = "VertexLM"}
		
		Lighting Off
		
		BindChannels {
			Bind "Vertex", vertex
			Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
			Bind "texcoord", texcoord1 // main uses 1st uv
		}
		
		SetTexture [_mLight] { 
			combine texture 
		}
		
		SetTexture [_MainTex] {
			combine texture * previous DOUBLE, texture * primary
		}
		
	}
	
	// Lightmapped, encoded as RGBM
	Pass {
		Tags { "LightMode" = "VertexLMRGBM" }
		
		Lighting Off
		BindChannels {
			Bind "Vertex", vertex
			Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
			Bind "texcoord", texcoord1 // main uses 1st uv
		}
		
		SetTexture [_mLight] {
			combine texture * texture alpha DOUBLE
		}
		SetTexture [_MainTex] {
			combine texture * previous QUAD, texture * primary
		}
	}

Then, I have to setUp the material offset and material tiling for every object that use the shader. This is the script

#pragma strict

function Start () {

	var renderers : Renderer[] = GameObject.FindObjectsOfType(Renderer);
	
	
	for(var i : int = 0; i < renderers.Length; i++){
	
		//x and y refers to the lightmap scale
		//z and w refers to the lightmap offset
		var tilOffs : Vector4 = renderers[i].lightmapTilingOffset;
		var tilling : Vector2 = Vector2(tilOffs.x, tilOffs.y);
		var offset : Vector2 = Vector2(tilOffs.z, tilOffs.w);
		
		var lIndex : int = renderers[i].lightmapIndex;
		if(lIndex >= 0  lIndex < 254) {
			
			var mLightmapText : Texture = LightmapSettings.lightmaps[lIndex].lightmapFar;
		
			var mat : Material[] = renderers[i].materials;
			for(var j: int = 0; j < renderers[i].materials.Length; j++){
	
				mat[j].SetTexture("_mLight",mLightmapText);
				mat[j].SetTextureScale("_mLight",tilling);
				mat[j].SetTextureOffset("_mLight",offset);
			
			}
			
		}
		
	
	}

}

The only problem for this solution is that only runs on runTime, on the Editor we can´t see the lightmap, and we see the objects awfull.

I order to view the objects without awfull aspect, I use a 128x128 white texture to Lightmap (Import settings with texture as lightmap).

When I “play” the game, the scrip changes the texture and I can see the lightmap.