Having weird problems in shader,Coding a custom 2d lighting shader
I am learning to code HLSL Shaders and also "trying" to code a lighting shader for my personal project. But having problems while doing. Fragments keep choosing the farther light point for some weird reason, and I can't figure it out.
(I coded it to get the closest distance as its value for light) And here is my code :
struct Light{
float4 position;
float size;
float intensity;
};
//appdata and v2f
StructuredBuffer<Light> _LightData;
fixed4 frag (v2f i) : SV_Target{
float _distance = 0;
fixed4 color = tex2D(_MainTex, i.uv);
for(int a = 0; a < _LightArraySize;a++){
if(_distance>0){
if(_distance > distance(i.worldSpacePos, _LightData[a].position+0.5)/_LightData[a].size){
_distance = distance(i.worldSpacePos, _LightData[a].position+0.5)/_LightData[a].size;
}
}else{
_distance = distance(i.worldSpacePos, _LightData[a].position+0.5)/_LightData[a].size;
}
}
return fixed4(color.x - _distance, color.y - _distance, color.z - _distance, color.w);
}
unthlsl2d.png
(72.4 kB)
Comment