package gay.pizza.CavesOfJolk

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.g3d.Renderable
import com.badlogic.gdx.graphics.g3d.Shader
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader
import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider

class CustomDefaultShaderProvider(config: DefaultShader.Config): DefaultShaderProvider(config)
{
	init
	{
		if (config.vertexShader == null)
			config.vertexShader = Gdx.files.internal("lit.vert.glsl").readString()
		if (config.fragmentShader == null)
			config.fragmentShader = Gdx.files.internal("lit.frag.glsl").readString()
	}

	override fun createShader(renderable: Renderable): Shader
	{
		val renderableMask = renderable.environment.mask or renderable.material.mask

		val fogMask =
			CustomIntAttribute.FogMode or
			CustomIntAttribute.FogType or
			CustomFloatAttribute.FogNear or
			CustomFloatAttribute.FogFar or
			CustomFloatAttribute.FogDensity
		if ((renderableMask and ColorAttribute.Fog == ColorAttribute.Fog) && (renderableMask and fogMask != 0L))
			return CustomDefaultShader(renderable, config)
		return super.createShader(renderable)
	}
}
