- Home /
Can Unity bake lighting/mapping from a projector?
I would like to use a projector to "map" several objects at once and have them keep that mapping when they are no longer in the projector.
Example:
When an image is "projected" into a camera, it gets "baked" onto the film. The image stays baked on the film even though it is no longer being exposed to that image through the camera lens (the projector).
Answer by Jacktang · Mar 02, 2012 at 07:59 AM
This can actually be done with unity pro, and a bit of shader knowhow. What you do is you write a custom vertex shader that unwraps your mesh according to its UV structure and transforms it into screen space. Then you perform your regular texture projection in the same shader. Assign that shader to a material, assign that material to a projector, put that projector onto a layer, and cull the main camera from rendering that layer, then create a second camera that renders only that projector layer and tell it to render its output to a RenderTexture. You will then have a render texture with your projector sprite projected onto its UVs.
Should be relatively straight forward for anyone who knows their vertex / fragment shaders. Here is a basic one that I came up with. I would wager a guess that this method could be easily translated to light cookies as well.
Shader "Custom/UnwrapShader" {
Properties {
_ProjectedTexture ("Projected Texture", 2D) = "" { TexGen ObjectLinear }
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Cull Off
AlphaTest Greater 0
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert_main
#pragma fragment frag_main
#include "UnityCG.cginc"
sampler2D _ProjectedTexture;
float4x4 _Projector;
struct Output_Struct {
float4 position : SV_POSITION;
float4 texcoord : TEXCOORD0;
};
Output_Struct vert_main (appdata_tan v) {
Output_Struct OUT;
OUT.position.xy = v.texcoord.xy * 2.0 - 1.0;
OUT.position.z = 0;
OUT.position.w = v.vertex.w;
OUT.texcoord = mul(_Projector, v.vertex);
return OUT;
}
float4 frag_main (Output_Struct OUT) : COLOR {
half4 tex = tex2Dproj(_ProjectedTexture, UNITY_PROJ_COORD(OUT.texcoord)) * half4(1,0,0,1);
return tex;
}
ENDCG
}
}
}
Hi @Jacktang, I've tried the above shader with my projector and all the steps you mentioned in your answer. I can see the unwrapped UV islands on my screen (in scene view, and also in game view if I don't remove my projection layer from the culling mask field). However, my second camera does not show this (neither adds to the target RenderTexture). Am I missing something here?
Got this to work finally! Had to duplicate my mesh into the second layer for the second camera to see as well. It seems you can't see projections unless you see the mesh as well. Just putting the projector into another layer did not work until you put a mesh in the same layer as well.
Answer by Dreamora · Jul 10, 2011 at 11:18 PM
No, thats not possible.
Projectors only exist at realtime.
Lightmap baking is restricted to static lights and as far as I know does currently not even support light cookies
Your answer
Follow this Question
Related Questions
Crash on Bake Selected 0 Answers
Unity lightning problem (wood) 0 Answers
Assigning UV Map to model at runtime 0 Answers
Different baking results from identical lamps 0 Answers
Material Behaviour 0 Answers