Compare commits
22 Commits
6887e65a70
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b48e4e92fa | |||
| 73b58eb3e0 | |||
| 3020ea8a08 | |||
| 1d3b18becb | |||
| 4fdfb2e6ea | |||
| b894cd4254 | |||
| 22be9bbe00 | |||
| e031523ed1 | |||
| 9cde1d98e2 | |||
| 745476a7a0 | |||
| a6d6277070 | |||
| 81064b497f | |||
| 344080e14d | |||
| 2a88f7d446 | |||
| 20b42d7a32 | |||
| a5e0d37e74 | |||
| 14223ddc35 | |||
| d045560ef5 | |||
| 2c5547320d | |||
| 23eefd9408 | |||
| 331efe5030 | |||
| 7bf7b0501c |
@@ -20,15 +20,19 @@ class Colin
|
||||
{
|
||||
private var pos = Vector2()
|
||||
private var angle = 0.0f
|
||||
private var offsAngle = Vector2()
|
||||
private var fov = 60.0f
|
||||
private var cam: PerspectiveCamera
|
||||
private var nutted = false
|
||||
private var colinMode = false
|
||||
private var backPressed = false
|
||||
|
||||
private var texture: Texture = assetManager.get("colin.png")
|
||||
private val nut: Sound = assetManager.get("nut.wav")
|
||||
|
||||
init
|
||||
{
|
||||
cam = PerspectiveCamera(60.0f,
|
||||
cam = PerspectiveCamera(fov,
|
||||
Gdx.graphics.width.toFloat(),
|
||||
Gdx.graphics.height.toFloat())
|
||||
cam.near = 0.1f
|
||||
@@ -38,12 +42,16 @@ class Colin
|
||||
private fun updateCamera()
|
||||
{
|
||||
cam.position.set(Vector3(pos.x, 1.0f, pos.y))
|
||||
val forward = Vector3(0.0f, 0.0f, -1.0f)
|
||||
val up = Vector3(0.0f, 1.0f, 0.0f)
|
||||
cam.direction.set(forward.rotateRad(up, angle))
|
||||
cam.direction.set(Util.forward.rotateRad(Util.right, offsAngle.y).rotateRad(Util.up, offsAngle.x + angle))
|
||||
cam.update()
|
||||
}
|
||||
|
||||
fun resize(width: Int, height: Int)
|
||||
{
|
||||
cam.viewportWidth = width.toFloat()
|
||||
cam.viewportHeight = height.toFloat()
|
||||
}
|
||||
|
||||
fun update(deltaTime: Float)
|
||||
{
|
||||
val stick = Controllers.getCurrent()?.let { pad ->
|
||||
@@ -70,6 +78,31 @@ class Colin
|
||||
if (!stick.isZero)
|
||||
pos -= forward * stick.y * speed * deltaTime
|
||||
|
||||
if (Gdx.input.isKeyJustPressed(Input.Keys.C))
|
||||
colinMode = !colinMode
|
||||
|
||||
Controllers.getCurrent()?.let { pad ->
|
||||
val dst = Vector2(
|
||||
pad.getAxis(pad.mapping.axisRightX),
|
||||
pad.getAxis(pad.mapping.axisRightY))
|
||||
.radialDeadzone(0.1f, 1.0f) * MathUtils.PI * -0.5f
|
||||
offsAngle = offsAngle.lerp(dst, 16.0f * deltaTime)
|
||||
|
||||
val targetFov = MathUtils.lerp(60.0f, 20.0f, pad.getAxis(5)) //fixme: where is mapping for rt??
|
||||
cam.fieldOfView = MathUtils.lerp(cam.fieldOfView, targetFov, 20.0f * deltaTime)
|
||||
|
||||
val right = Vector2(MathUtils.cos(angle), -MathUtils.sin(angle))
|
||||
if (pad.getButton(pad.mapping.buttonL1))
|
||||
pos -= right * speed * deltaTime
|
||||
if (pad.getButton(pad.mapping.buttonR1))
|
||||
pos += right * speed * deltaTime
|
||||
|
||||
val back = pad.getButton(pad.mapping.buttonBack)
|
||||
if (!backPressed && back)
|
||||
colinMode = !colinMode
|
||||
backPressed = back
|
||||
}
|
||||
|
||||
if (Controllers.getCurrent()?.let { pad -> pad.getButton(pad.mapping.buttonA) } == true || Gdx.input.isKeyJustPressed(Input.Keys.N))
|
||||
{
|
||||
if (!nutted)
|
||||
@@ -89,15 +122,16 @@ class Colin
|
||||
|
||||
fun draw(spriteBatch: SpriteBatch)
|
||||
{
|
||||
val drawPos = Vector2(pos.x * 100.0f, -pos.y * 100.0f)
|
||||
//spriteBatch.draw(texture, drawPos.x - 32.0f, drawPos.y - 32.0f, 64.0f, 64.0f)
|
||||
val region = TextureRegion(texture)
|
||||
spriteBatch.draw(region,
|
||||
drawPos.x - 32.0f, drawPos.y - 32.0f,
|
||||
32.0f, 32.0f,
|
||||
64.0f, 64.0f,
|
||||
1.0f, 1.0f, angle * MathUtils.radiansToDegrees)
|
||||
|
||||
if (colinMode)
|
||||
{
|
||||
val drawPos = Vector2(pos.x * 100.0f, -pos.y * 100.0f)
|
||||
val region = TextureRegion(texture)
|
||||
spriteBatch.draw(region,
|
||||
drawPos.x - 32.0f, drawPos.y - 32.0f,
|
||||
32.0f, 32.0f,
|
||||
64.0f, 64.0f,
|
||||
1.0f, 1.0f, angle * MathUtils.radiansToDegrees)
|
||||
}
|
||||
}
|
||||
|
||||
val camera get() = cam
|
||||
|
||||
120
src/main/kotlin/CustomDefaultShader.kt
Normal file
@@ -0,0 +1,120 @@
|
||||
package gay.pizza.CavesOfJolk
|
||||
|
||||
import com.badlogic.gdx.graphics.g3d.Attributes
|
||||
import com.badlogic.gdx.graphics.g3d.Renderable
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.shaders.BaseShader
|
||||
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader
|
||||
|
||||
class CustomDefaultShader(renderable: Renderable, config: Config):
|
||||
DefaultShader(renderable, config, createPrefix(renderable, config))
|
||||
{
|
||||
companion object
|
||||
{
|
||||
fun createPrefix(renderable: Renderable, config: Config): String
|
||||
{
|
||||
var prefix = DefaultShader.createPrefix(renderable, config)
|
||||
|
||||
val attribs = Attributes()
|
||||
renderable.environment.let { attribs.set(it) }
|
||||
renderable.material.let { attribs.set(it) }
|
||||
|
||||
if (attribs.has(CustomFloatAttribute.BumpHeight) && attribs.has(TextureAttribute.Bump))
|
||||
{
|
||||
prefix += "#define " + CustomFloatAttribute.BumpHeightAlias + "Flag\n"
|
||||
prefix += "#define " + TextureAttribute.BumpAlias + "Flag\n"
|
||||
prefix += "#define " + TextureAttribute.BumpAlias + "Coord texCoord0\n"
|
||||
}
|
||||
|
||||
if (attribs.has(ColorAttribute.Fog))
|
||||
{
|
||||
prefix += "#define fog${
|
||||
when (attribs.get<CustomIntAttribute>(CustomIntAttribute.FogMode)?.value
|
||||
?: CustomIntAttribute.FogModes.Distance.toInt()) {
|
||||
CustomIntAttribute.FogModes.Distance.toInt() -> "Distance"
|
||||
CustomIntAttribute.FogModes.Depth.toInt() -> "Depth"
|
||||
else -> throw IndexOutOfBoundsException()
|
||||
}
|
||||
}Flag\n"
|
||||
prefix += "#define fog${
|
||||
when (attribs.get<CustomIntAttribute>(CustomIntAttribute.FogType)?.value
|
||||
?: CustomIntAttribute.FogTypes.Original.toInt()) {
|
||||
CustomIntAttribute.FogTypes.Original.toInt() -> "Original"
|
||||
CustomIntAttribute.FogTypes.Linear.toInt() -> "Linear"
|
||||
CustomIntAttribute.FogTypes.Smooth.toInt() -> "Smooth"
|
||||
CustomIntAttribute.FogTypes.InvSquare.toInt() -> "InvSquare"
|
||||
CustomIntAttribute.FogTypes.Exp.toInt() -> "Exp"
|
||||
CustomIntAttribute.FogTypes.Exp2.toInt() -> "Exp2"
|
||||
else -> throw IndexOutOfBoundsException()
|
||||
}
|
||||
}Flag\n"
|
||||
}
|
||||
|
||||
return prefix
|
||||
}
|
||||
|
||||
object Inputs
|
||||
{
|
||||
val fogNear = Uniform("u_fogNear")
|
||||
val fogFar = Uniform("u_fogFar")
|
||||
val fogDensity = Uniform("u_fogDensity")
|
||||
|
||||
val bumpHeight = Uniform("u_bumpHeight")
|
||||
|
||||
val bumpTexture = Uniform("u_bumpTexture")
|
||||
val bumpUVTransform = Uniform("u_bumpUVTransform")
|
||||
}
|
||||
|
||||
object Setters
|
||||
{
|
||||
val bumpHeight = object: LocalSetter()
|
||||
{
|
||||
override fun set(shader: BaseShader, inputId: Int, renderable: Renderable, combAttribs: Attributes)
|
||||
{
|
||||
val attr = combAttribs.get(CustomFloatAttribute.BumpHeight) as FloatAttribute
|
||||
shader.set(inputId, attr.value)
|
||||
}
|
||||
}
|
||||
|
||||
val bumpTexture = object: LocalSetter()
|
||||
{
|
||||
override fun set(shader: BaseShader, inputId: Int, renderable: Renderable, combAttribs: Attributes)
|
||||
{
|
||||
val texAttr = combAttribs.get(TextureAttribute.Bump) as TextureAttribute
|
||||
val unit = shader.context.textureBinder.bind(texAttr.textureDescription)
|
||||
shader.set(inputId, unit)
|
||||
}
|
||||
}
|
||||
var bumpUVTransform = object: LocalSetter()
|
||||
{
|
||||
override fun set(shader: BaseShader, inputId: Int, renderable: Renderable, combAttribs: Attributes)
|
||||
{
|
||||
val ta = combAttribs.get(TextureAttribute.Normal) as TextureAttribute
|
||||
shader.set(inputId, ta.offsetU, ta.offsetV, ta.scaleU, ta.scaleV)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val u_fogNear = register(Inputs.fogNear)
|
||||
private val u_fogFar = register(Inputs.fogFar)
|
||||
private val u_fogDensity = register(Inputs.fogDensity)
|
||||
|
||||
private val u_bumpHeight = register(Inputs.bumpHeight, Setters.bumpHeight)
|
||||
private val u_bumpTexture = register(Inputs.bumpTexture, Setters.bumpTexture)
|
||||
private val u_bumpUVTransform = register(Inputs.bumpUVTransform, Setters.bumpUVTransform)
|
||||
|
||||
override fun bindLights(renderable: Renderable?, attributes: Attributes?)
|
||||
{
|
||||
if (attributes == null)
|
||||
return
|
||||
|
||||
attributes.get<CustomFloatAttribute>(CustomFloatAttribute.FogNear)?.let { set(u_fogNear, it.value) }
|
||||
attributes.get<CustomFloatAttribute>(CustomFloatAttribute.FogFar)?.let { set(u_fogFar, it.value) }
|
||||
attributes.get<CustomFloatAttribute>(CustomFloatAttribute.FogDensity)?.let { set(u_fogDensity, it.value) }
|
||||
|
||||
super.bindLights(renderable, attributes)
|
||||
}
|
||||
}
|
||||
40
src/main/kotlin/CustomDefaultShaderProvider.kt
Normal file
@@ -0,0 +1,40 @@
|
||||
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.attributes.TextureAttribute
|
||||
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)
|
||||
|
||||
val bumpMask = CustomFloatAttribute.BumpHeight or TextureAttribute.Bump
|
||||
if (renderableMask and bumpMask == bumpMask)
|
||||
return CustomDefaultShader(renderable, config)
|
||||
|
||||
return super.createShader(renderable)
|
||||
}
|
||||
}
|
||||
25
src/main/kotlin/CustomFloatAttribute.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package gay.pizza.CavesOfJolk
|
||||
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute
|
||||
|
||||
class CustomFloatAttribute private constructor(type: Long, value: Float): FloatAttribute(type, value)
|
||||
{
|
||||
companion object
|
||||
{
|
||||
const val FogNearAlias = "fogNear"
|
||||
val FogNear = register(FogNearAlias)
|
||||
fun createFogNear(value: Float) = CustomFloatAttribute(FogNear, value)
|
||||
|
||||
const val FogFarAlias = "fogFar"
|
||||
val FogFar = register(FogFarAlias)
|
||||
fun createFogFar(value: Float) = CustomFloatAttribute(FogFar, value)
|
||||
|
||||
const val FogDensityAlias = "fogDensity"
|
||||
val FogDensity = register(FogDensityAlias)
|
||||
fun createFogDensity(value: Float) = CustomFloatAttribute(FogDensity, value)
|
||||
|
||||
const val BumpHeightAlias = "bumpHeight"
|
||||
val BumpHeight = register(BumpHeightAlias)
|
||||
fun createBumpHeight(value: Float) = CustomFloatAttribute(BumpHeight, value)
|
||||
}
|
||||
}
|
||||
37
src/main/kotlin/CustomIntAttribute.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
package gay.pizza.CavesOfJolk
|
||||
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.IntAttribute
|
||||
|
||||
class CustomIntAttribute private constructor(type: Long, value: Int): IntAttribute(type, value)
|
||||
{
|
||||
enum class FogModes(private val value: Int)
|
||||
{
|
||||
Distance(0),
|
||||
Depth(1);
|
||||
|
||||
fun toInt() = value
|
||||
}
|
||||
|
||||
enum class FogTypes(private val value: Int)
|
||||
{
|
||||
Original(0),
|
||||
Linear(1),
|
||||
Smooth(2),
|
||||
InvSquare(3),
|
||||
Exp(4),
|
||||
Exp2(5);
|
||||
|
||||
fun toInt() = value
|
||||
}
|
||||
|
||||
companion object
|
||||
{
|
||||
const val FogModeAlias = "fogMode"
|
||||
val FogMode = register(FogModeAlias)
|
||||
fun createFogMode(value: FogModes) = CustomIntAttribute(FogMode, value.toInt())
|
||||
|
||||
const val FogTypeAlias = "fogType"
|
||||
val FogType = register(FogTypeAlias)
|
||||
fun createFogType(value: FogTypes) = CustomIntAttribute(FogType, value.toInt())
|
||||
}
|
||||
}
|
||||
@@ -2,30 +2,30 @@ package gay.pizza.CavesOfJolk
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.audio.Sound
|
||||
import com.badlogic.gdx.graphics.*
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import com.badlogic.gdx.graphics.g3d.*
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.IntAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.*
|
||||
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight
|
||||
import com.badlogic.gdx.graphics.g3d.environment.PointLight
|
||||
import com.badlogic.gdx.graphics.g3d.model.data.*
|
||||
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader
|
||||
import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider
|
||||
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder
|
||||
import com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor
|
||||
import com.badlogic.gdx.graphics.g3d.utils.TextureProvider.AssetTextureProvider
|
||||
import com.badlogic.gdx.math.*
|
||||
import com.badlogic.gdx.utils.Array
|
||||
import com.badlogic.gdx.utils.ScreenUtils
|
||||
import gay.pizza.CavesOfJolk.Resources.Companion.assetManager
|
||||
import ktx.math.times
|
||||
|
||||
class Game: ApplicationAdapter()
|
||||
{
|
||||
private lateinit var texJolk: Texture
|
||||
private lateinit var fntComic: BitmapFont
|
||||
private lateinit var nut: Sound
|
||||
private lateinit var cube: Model
|
||||
private lateinit var floor: Model
|
||||
private lateinit var toybox: Model
|
||||
|
||||
private lateinit var spriteBatch: SpriteBatch
|
||||
private lateinit var modelBatch: ModelBatch
|
||||
@@ -33,40 +33,85 @@ class Game: ApplicationAdapter()
|
||||
|
||||
private lateinit var colin: Colin
|
||||
private var jolkRot = 0.0f
|
||||
private var lightTheta = 0.0f
|
||||
private val rand = RandomXS128(69 + 420 + 1919 + 916 + 42 + 1)
|
||||
|
||||
private lateinit var cubeInstance: ModelInstance
|
||||
private lateinit var floorInstance: ModelInstance
|
||||
private lateinit var suzanneInstance: ModelInstance
|
||||
private lateinit var knux: ModelInstance
|
||||
private lateinit var toyboxInstance: ModelInstance
|
||||
|
||||
private fun makeCube(texture: Texture): Model
|
||||
private fun makeCube(size: Float, material: Material): Model
|
||||
{
|
||||
val modelBuilder = ModelBuilder()
|
||||
val size = 2.0f
|
||||
//val material = Material(ColorAttribute.createDiffuse(Color.WHITE))
|
||||
val material = Material(TextureAttribute(TextureAttribute.Diffuse,
|
||||
TextureDescriptor(texture,
|
||||
Texture.TextureFilter.Linear,
|
||||
Texture.TextureFilter.Linear,
|
||||
Texture.TextureWrap.ClampToEdge,
|
||||
Texture.TextureWrap.ClampToEdge)))
|
||||
val attribs = VertexAttributes.Usage.Position or VertexAttributes.Usage.TextureCoordinates or VertexAttributes.Usage.Normal
|
||||
return modelBuilder.createBox(size, size, size, material, attribs.toLong())
|
||||
ModelBuilder().apply {
|
||||
val attribs =
|
||||
VertexAttributes.Usage.Position or
|
||||
VertexAttributes.Usage.TextureCoordinates or
|
||||
VertexAttributes.Usage.Normal
|
||||
return createBox(size, size, size, material, attribs.toLong())
|
||||
}
|
||||
}
|
||||
|
||||
private fun makeFloor(texture: Texture): Model
|
||||
private fun makeFloor(size: Float): Model
|
||||
{
|
||||
val modelBuilder = ModelBuilder()
|
||||
//val material = Material(ColorAttribute.createDiffuse(XnaColor.BlanchedAlmond))
|
||||
val material = Material(TextureAttribute.createDiffuse(texture))
|
||||
val attribs = VertexAttributes.Usage.Position or VertexAttributes.Usage.TextureCoordinates or VertexAttributes.Usage.Normal
|
||||
val size = 10.0f
|
||||
return modelBuilder.createRect(
|
||||
0.0f, 0.0f, 0.0f,
|
||||
size, 0.0f, 0.0f,
|
||||
size, 0.0f, -size,
|
||||
0.0f, 0.0f, -size,
|
||||
0.0f, 1.0f, 0.0f,
|
||||
material, attribs.toLong())
|
||||
val texs = size / 4.0f
|
||||
|
||||
val vertex = { pos: Vector3, tex: Vector2 ->
|
||||
val normal = Util.up
|
||||
val tangent = Util.right
|
||||
val bitangent = Util.forward
|
||||
floatArrayOf(
|
||||
pos.x, pos.y, pos.z,
|
||||
normal.x, normal.y, normal.z, tangent.x, tangent.y, tangent.z, bitangent.x, bitangent.y, bitangent.z,
|
||||
tex.x, tex.y)
|
||||
}
|
||||
|
||||
val modelTexture = { modelTextureUsage: Int, textureFilename: String -> ModelTexture().apply {
|
||||
usage = modelTextureUsage
|
||||
fileName = textureFilename
|
||||
}}
|
||||
|
||||
return Model(ModelData().apply {
|
||||
addMesh(ModelMesh().apply {
|
||||
id = "floormodel"
|
||||
attributes = arrayOf(VertexAttribute.Position(), VertexAttribute.Normal(), VertexAttribute.Tangent(), VertexAttribute.Binormal(), VertexAttribute.TexCoords(0))
|
||||
vertices =
|
||||
vertex(Vector3(0.0f, 0.0f, 0.0f), Vector2(0.0f, texs)) +
|
||||
vertex(Vector3(size, 0.0f, 0.0f), Vector2(texs, texs)) +
|
||||
vertex(Vector3(0.0f, 0.0f, -size), Vector2(0.0f, 0.0f)) +
|
||||
vertex(Vector3(size, 0.0f, -size), Vector2(texs, 0.0f))
|
||||
parts = arrayOf(ModelMeshPart().apply {
|
||||
id = "floormesh"
|
||||
primitiveType = GL20.GL_TRIANGLES
|
||||
indices = shortArrayOf(
|
||||
0, 1, 2,
|
||||
3, 2, 1)
|
||||
})
|
||||
})
|
||||
materials.add(ModelMaterial().apply {
|
||||
id = "floormat"
|
||||
diffuse = XnaColor.White
|
||||
specular = XnaColor.BlanchedAlmond.mix(XnaColor.Black, 0.12f)
|
||||
shininess = 65.0f
|
||||
textures = Array()
|
||||
textures.add(
|
||||
modelTexture(ModelTexture.USAGE_DIFFUSE, "cobblestone.png"),
|
||||
modelTexture(ModelTexture.USAGE_NORMAL, "cobblestone_normal.png"),
|
||||
modelTexture(ModelTexture.USAGE_BUMP, "cobblestone_bump.png"),
|
||||
modelTexture(ModelTexture.USAGE_SPECULAR, "cobblestone_specular.png"))
|
||||
})
|
||||
nodes.add(ModelNode().apply {
|
||||
id = "floornode"
|
||||
scale = Util.one
|
||||
parts = arrayOf(ModelNodePart().apply {
|
||||
meshPartId = "floormesh"
|
||||
materialId = "floormat"
|
||||
})
|
||||
})
|
||||
}, AssetTextureProvider(assetManager)).apply {
|
||||
materials[0].set(CustomFloatAttribute.createBumpHeight(0.0075f))
|
||||
}
|
||||
}
|
||||
|
||||
override fun create()
|
||||
@@ -74,41 +119,83 @@ class Game: ApplicationAdapter()
|
||||
Resources.instance.loadAssets()
|
||||
assetManager.finishLoading()
|
||||
|
||||
texJolk = assetManager.get("jolkmeup.jpg")
|
||||
fntComic = assetManager.get("Comic Sans MS.ttf")
|
||||
|
||||
|
||||
cube = makeCube(texJolk)
|
||||
floor = makeFloor(assetManager.get("cobblestone.png"))
|
||||
cube = makeCube(2.0f, Material(
|
||||
ColorAttribute.createDiffuse(XnaColor.White),
|
||||
TextureAttribute(TextureAttribute.Diffuse, TextureDescriptor(assetManager.get("jolkmeup.jpg"))),
|
||||
ColorAttribute.createSpecular(XnaColor.Gray),
|
||||
FloatAttribute.createShininess(20.0f)))
|
||||
toybox = assetManager.get("toybox.g3db", Model::class.java).apply { materials[0].set(
|
||||
TextureAttribute(TextureAttribute.Diffuse, TextureDescriptor(assetManager.get("toybox_albedo.png"))),
|
||||
TextureAttribute(TextureAttribute.Normal, TextureDescriptor(assetManager.get("toybox_normal.png"))),
|
||||
TextureAttribute(TextureAttribute.Bump, TextureDescriptor(assetManager.get("toybox_displace.png"))),
|
||||
CustomFloatAttribute.createBumpHeight(0.04f),
|
||||
FloatAttribute.createShininess(35.0f)) }
|
||||
val joeMany = 56.0f
|
||||
floor = makeFloor(joeMany)
|
||||
|
||||
spriteBatch = SpriteBatch()
|
||||
|
||||
env = Environment()
|
||||
env.set(IntAttribute.createCullFace(GL20.GL_BACK))
|
||||
env.set(ColorAttribute.createAmbientLight(XnaColor.DarkSlateGray.lighten(-0.6)))
|
||||
env.set(ColorAttribute.createFog(XnaColor.CornflowerBlue))
|
||||
env.add(DirectionalLight().set(XnaColor.White, Vector3(1.0f, -1.0f, -1.0f).nor()))
|
||||
env.set(
|
||||
IntAttribute.createCullFace(GL20.GL_BACK),
|
||||
ColorAttribute.createAmbientLight(XnaColor.DarkSlateGray.lighten(-0.666)),
|
||||
ColorAttribute.createFog(XnaColor.CornflowerBlue))
|
||||
env.set(
|
||||
CustomIntAttribute.createFogMode(CustomIntAttribute.FogModes.Depth),
|
||||
CustomIntAttribute.createFogType(CustomIntAttribute.FogTypes.Smooth),
|
||||
CustomFloatAttribute.createFogNear(1.25f),
|
||||
CustomFloatAttribute.createFogFar(24.5f))
|
||||
/*
|
||||
env.set(
|
||||
CustomIntAttribute.createFogMode(CustomIntAttribute.FogModes.Distance),
|
||||
CustomIntAttribute.createFogType(CustomIntAttribute.FogTypes.Exp2),
|
||||
CustomFloatAttribute.createFogDensity(0.1f))
|
||||
*/
|
||||
env.add(DirectionalLight().set(
|
||||
XnaColor.BlanchedAlmond.lighten(-0.02).mix(XnaColor.LightSlateGray, 0.5f).mix(XnaColor.White, 0.125f),
|
||||
Vector3(1.0f, -1.0f, -1.0f).nor()))
|
||||
env.add(PointLight().set(
|
||||
XnaColor.Green.mix(XnaColor.Gray, 0.5f),
|
||||
Vector3(3.0f, 0.33f, -5.0f), 2.0f))
|
||||
env.add(PointLight().set(
|
||||
XnaColor.Red.mix(XnaColor.Gray, 0.5f),
|
||||
Vector3(5.5f, 0.33f, -6.0f), 2.0f))
|
||||
env.add(PointLight().set(
|
||||
XnaColor.Blue.mix(XnaColor.Gray, 0.5f),
|
||||
Vector3(4.0f, 0.33f, -7.0f), 2.0f))
|
||||
|
||||
val shaderConfig = DefaultShader.Config(
|
||||
Gdx.files.internal("lit.vert.glsl").readString(),
|
||||
Gdx.files.internal("lit.frag.glsl").readString())
|
||||
val shaderConfig = DefaultShader.Config()
|
||||
shaderConfig.numDirectionalLights = 1
|
||||
shaderConfig.numPointLights = 0
|
||||
shaderConfig.numPointLights = 3
|
||||
shaderConfig.numBones = 0
|
||||
modelBatch = ModelBatch(DefaultShaderProvider(shaderConfig))
|
||||
modelBatch = ModelBatch(CustomDefaultShaderProvider(shaderConfig))
|
||||
|
||||
colin = Colin()
|
||||
|
||||
cubeInstance = ModelInstance(cube)
|
||||
floorInstance = ModelInstance(floor)
|
||||
suzanneInstance = ModelInstance(assetManager.get("suzanne.g3db", Model::class.java))
|
||||
|
||||
suzanneInstance.transform = Matrix4().translate(3.0f, 1.0f, -3.5f)
|
||||
knux = ModelInstance(assetManager.get("knux.g3db", Model::class.java))
|
||||
toyboxInstance = ModelInstance(toybox)
|
||||
}
|
||||
|
||||
override fun resize(width: Int, height: Int)
|
||||
{
|
||||
colin.resize(width, height)
|
||||
spriteBatch.projectionMatrix.setToOrtho2D(0.0f, 0.0f,
|
||||
Gdx.graphics.getWidth().toFloat(),
|
||||
Gdx.graphics.getHeight().toFloat());
|
||||
}
|
||||
|
||||
private fun update(deltaTime: Float)
|
||||
{
|
||||
colin.update(deltaTime)
|
||||
|
||||
lightTheta += deltaTime
|
||||
jolkRot += 15.0f * deltaTime
|
||||
}
|
||||
|
||||
@@ -117,9 +204,29 @@ class Game: ApplicationAdapter()
|
||||
val deltaTime = Gdx.graphics.deltaTime
|
||||
update(deltaTime)
|
||||
|
||||
val fogColour = XnaColor.CornflowerBlue.lighten(Math.sin(0.056 * jolkRot.toDouble()) * 0.15 - 0.15)
|
||||
ScreenUtils.clear(fogColour, true)
|
||||
env.set(ColorAttribute.createFog(fogColour))
|
||||
ScreenUtils.clear(XnaColor.CornflowerBlue, true)
|
||||
|
||||
env.get(PointLightsAttribute.Type)?.let { it as PointLightsAttribute
|
||||
var thetaa = lightTheta * 6.0f * 0.12f
|
||||
var thetab = lightTheta * 6.0f * 0.011f
|
||||
var thetac = lightTheta * 6.0f * 0.056f
|
||||
|
||||
for (light in it.lights)
|
||||
{
|
||||
val x = 4.0f + 6.0f * MathUtils.cos(thetaa)
|
||||
val z = -6.0f + 3.0f * MathUtils.sin(thetaa * 2.0f)
|
||||
val y = 0.33f + 0.33f * MathUtils.sin(thetab)
|
||||
val i = 3.1f + 1.53f * MathUtils.cos(thetac)
|
||||
|
||||
val spacing = 0.5f * MathUtils.PI * (2.0f / 3.0f)
|
||||
thetaa += spacing
|
||||
thetab += spacing * 0.98f
|
||||
thetac += spacing * 0.5566f
|
||||
|
||||
light.setPosition(x, y, z)
|
||||
light.setIntensity(i)
|
||||
}
|
||||
}
|
||||
|
||||
val jolkPos = Vector3(0.0f, 1.0f + MathUtils.sin(jolkRot * 0.25f) * 0.25f, -4.0f)
|
||||
val world = Matrix4()
|
||||
@@ -128,11 +235,21 @@ class Game: ApplicationAdapter()
|
||||
.rotate(0.0f, 1.0f, 0.0f, jolkRot)
|
||||
.scale(0.25f, 0.25f, 0.25f)
|
||||
cubeInstance.transform = world
|
||||
knux.transform.rotate(Quaternion().slerp(rand.nextQuaternion(), deltaTime))
|
||||
knux.transform.translate(knux.transform * Util.forward * deltaTime)
|
||||
|
||||
toyboxInstance.transform = Matrix4(
|
||||
Vector3(6.0f, 0.667f, -3.5f),
|
||||
Quaternion().setEulerAnglesRad(lightTheta * 0.5f, 0.0f, 0.0f),
|
||||
Util.one * 0.25f
|
||||
)
|
||||
|
||||
modelBatch.begin(colin.camera)
|
||||
modelBatch.render(floorInstance, env)
|
||||
modelBatch.render(cubeInstance, env)
|
||||
modelBatch.render(suzanneInstance, env)
|
||||
modelBatch.render(knux, env)
|
||||
modelBatch.render(toyboxInstance, env)
|
||||
modelBatch.end()
|
||||
|
||||
spriteBatch.begin()
|
||||
|
||||
@@ -14,6 +14,6 @@ fun main()
|
||||
//conf.useVsync(false)
|
||||
//conf.setForegroundFPS(300)
|
||||
conf.setWindowedMode(1280, 720)
|
||||
conf.setResizable(false)
|
||||
conf.setResizable(true)
|
||||
Lwjgl3Application(Game(), conf)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gay.pizza.CavesOfJolk
|
||||
|
||||
import com.badlogic.gdx.assets.AssetManager
|
||||
import com.badlogic.gdx.assets.loaders.TextureLoader
|
||||
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver
|
||||
import com.badlogic.gdx.audio.Sound
|
||||
import com.badlogic.gdx.graphics.Texture
|
||||
@@ -30,14 +31,37 @@ class Resources private constructor()
|
||||
assetManager.setLoader(BitmapFont::class.java, ".ttf", FreetypeFontLoader(resolver))
|
||||
}
|
||||
|
||||
val linearMipped = TextureLoader.TextureParameter().apply {
|
||||
minFilter = Texture.TextureFilter.MipMapLinearLinear
|
||||
magFilter = Texture.TextureFilter.Linear
|
||||
wrapU = Texture.TextureWrap.Repeat
|
||||
wrapV = Texture.TextureWrap.Repeat
|
||||
genMipMaps = true
|
||||
}
|
||||
|
||||
val linearClamp = TextureLoader.TextureParameter().apply {
|
||||
minFilter = Texture.TextureFilter.Linear
|
||||
magFilter = Texture.TextureFilter.Linear
|
||||
wrapU = Texture.TextureWrap.ClampToEdge
|
||||
wrapV = Texture.TextureWrap.ClampToEdge
|
||||
}
|
||||
|
||||
fun loadAssets()
|
||||
{
|
||||
assetManager.load("colin.png", Texture::class.java)
|
||||
assetManager.load("jolkmeup.jpg", Texture::class.java)
|
||||
assetManager.load("cobblestone.png", Texture::class.java)
|
||||
assetManager.load("jolkmeup.jpg", Texture::class.java, linearClamp)
|
||||
assetManager.loadFont("Comic Sans MS.ttf", 20)
|
||||
assetManager.load("suzanne.g3db", Model::class.java)
|
||||
assetManager.load("nut.wav", Sound::class.java)
|
||||
assetManager.load("cobblestone.png", Texture::class.java, linearMipped)
|
||||
assetManager.load("cobblestone_normal.png", Texture::class.java, linearMipped)
|
||||
assetManager.load("cobblestone_specular.png", Texture::class.java, linearMipped)
|
||||
assetManager.load("knux.g3db", Model::class.java)
|
||||
assetManager.load("toybox.g3db", Model::class.java)
|
||||
assetManager.load("cobblestone_bump.png", Texture::class.java, linearMipped)
|
||||
assetManager.load("toybox_albedo.png", Texture::class.java, linearMipped)
|
||||
assetManager.load("toybox_normal.png", Texture::class.java, linearMipped)
|
||||
assetManager.load("toybox_displace.png", Texture::class.java, linearMipped)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package gay.pizza.CavesOfJolk
|
||||
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.math.Vector2
|
||||
import com.badlogic.gdx.graphics.g3d.Attribute
|
||||
import com.badlogic.gdx.graphics.g3d.Attributes
|
||||
import com.badlogic.gdx.math.*
|
||||
import ktx.math.div
|
||||
import ktx.math.times
|
||||
import ktx.math.unaryMinus
|
||||
import org.hsluv.HUSLColorConverter
|
||||
import kotlin.math.pow
|
||||
import kotlin.random.Random
|
||||
|
||||
fun Float.axisDeadzone(min: Float, max: Float): Float
|
||||
{
|
||||
@@ -40,6 +44,12 @@ class Util
|
||||
((abgr8888 and 0xFF00u) shr 8).toFloat() / 255.0f,
|
||||
((abgr8888 and 0xFF0000u) shr 16).toFloat() / 255.0f,
|
||||
((abgr8888 and 0xFF000000u) shr 24).toFloat() / 255.0f)
|
||||
|
||||
val zero get() = Vector3.Zero
|
||||
val one get() = Vector3(1.0f, 1.0f, 1.0f)
|
||||
val forward get() = -Vector3.Z
|
||||
val right get() = Vector3.X
|
||||
val up get() = Vector3.Y
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,3 +61,29 @@ fun Color.lighten(ld: Double): Color
|
||||
val rgb = HUSLColorConverter.hsluvToRgb(hsl)
|
||||
return Color(rgb[0].toFloat(), rgb[1].toFloat(), rgb[2].toFloat(), a)
|
||||
}
|
||||
|
||||
fun Color.mix(rhs: Color, x: Float) = Color(
|
||||
MathUtils.lerp(this.r, rhs.r, x),
|
||||
MathUtils.lerp(this.g, rhs.g, x),
|
||||
MathUtils.lerp(this.b, rhs.b, x),
|
||||
MathUtils.lerp(this.a, rhs.a, x))
|
||||
|
||||
//FIXME: find some way to get rid of these warnings
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "UNCHECKED_CAST")
|
||||
fun <T: Attribute> Attributes.get(type: Long) = get(type) as? T
|
||||
|
||||
fun Random.nextFloat(min: Float, max: Float) = min + nextFloat() * (max - min)
|
||||
|
||||
fun RandomXS128.nextQuaternion(): Quaternion
|
||||
{
|
||||
var x: Float
|
||||
var y: Float
|
||||
var z: Float
|
||||
do { x = nextFloat(-1.0f, 1.0f); y = nextFloat(-1.0f, 1.0f); z = x * x + y * y } while (z > 1.0f)
|
||||
var u: Float
|
||||
var v: Float
|
||||
var w: Float
|
||||
do { u = nextFloat(-1.0f, 1.0f); v = nextFloat(-1.0f, 1.0f); w = u * u + v * v } while (w > 1.0f)
|
||||
val s = kotlin.math.sqrt((1.0f - z) / w)
|
||||
return Quaternion(x, y, s * u, s * v)
|
||||
}
|
||||
|
||||
BIN
src/main/resources/cobblestone_bump.png
Normal file
|
After Width: | Height: | Size: 197 KiB |
BIN
src/main/resources/cobblestone_normal.png
Normal file
|
After Width: | Height: | Size: 265 KiB |
BIN
src/main/resources/cobblestone_specular.png
Normal file
|
After Width: | Height: | Size: 235 KiB |
BIN
src/main/resources/knux.g3db
Normal file
@@ -13,17 +13,29 @@ precision mediump float;
|
||||
varying vec3 v_normal;
|
||||
#endif // normalFlag
|
||||
|
||||
#if (defined(tangentFlag) && defined(binormalFlag)) || (defined(bumpHeightFlag) && defined(bumpTextureFlag))
|
||||
varying mat3 v_tbn;
|
||||
#endif // tangentFlag && bitangentFlag
|
||||
|
||||
#ifdef colorFlag
|
||||
varying vec4 v_color;
|
||||
#endif // colorFlag
|
||||
|
||||
#if defined(diffuseTextureFlag) || defined(specularTextureFlag) || defined(emissiveTextureFlag)
|
||||
#if defined(diffuseTextureFlag) || defined(normalTextureFlag) || defined(specularTextureFlag) || defined(emissiveTextureFlag)
|
||||
#define textureFlag
|
||||
#endif
|
||||
#ifdef diffuseTextureFlag
|
||||
varying MEDP vec2 v_diffuseUV;
|
||||
uniform sampler2D u_diffuseTexture;
|
||||
#endif // diffuseTextureFlag
|
||||
#ifdef bumpTextureFlag
|
||||
varying MEDP vec2 v_bumpUV;
|
||||
uniform sampler2D u_bumpTexture;
|
||||
#endif // bumpTextureFlag
|
||||
#ifdef normalTextureFlag
|
||||
varying MEDP vec2 v_normalUV;
|
||||
uniform sampler2D u_normalTexture;
|
||||
#endif // normalTextureFlag
|
||||
#ifdef specularTextureFlag
|
||||
varying MEDP vec2 v_specularUV;
|
||||
uniform sampler2D u_specularTexture;
|
||||
@@ -52,6 +64,10 @@ uniform float u_shininess;
|
||||
const float u_shininess = 20.0;
|
||||
#endif // shininessFlag
|
||||
|
||||
#ifdef bumpHeightFlag
|
||||
uniform float u_bumpHeight;
|
||||
#endif // bumpHeightFlag
|
||||
|
||||
#if numDirectionalLights > 0
|
||||
struct DirectionalLight
|
||||
{
|
||||
@@ -81,14 +97,21 @@ varying vec3 v_ambient;
|
||||
|
||||
#endif // lightingFlag
|
||||
|
||||
#if defined(specularFlag) || defined(fogFlag)
|
||||
#if (numPointLights > 0) || defined(specularFlag) || defined(fogFlag)
|
||||
varying vec4 v_worldPosition;
|
||||
uniform vec4 u_cameraPosition;
|
||||
#endif // lightingFlag || fogFlag
|
||||
#endif // numPointLights > 0 || lightingFlag || fogFlag
|
||||
|
||||
#ifdef fogFlag
|
||||
uniform vec4 u_fogColor;
|
||||
varying float v_fog;
|
||||
|
||||
#if defined(fogLinearFlag) || defined(fogSmoothFlag) || defined(fogInvSquareFlag)
|
||||
uniform float u_fogNear;
|
||||
uniform float u_fogFar;
|
||||
#elif defined(fogExpFlag) || defined(fogExp2Flag)
|
||||
uniform float u_fogDensity;
|
||||
#endif
|
||||
|
||||
#endif // fogFlag
|
||||
|
||||
uniform mat4 u_projViewTrans;
|
||||
@@ -99,14 +122,30 @@ vec3 linearDecode(vec3 v) { return vec3(pow(v.r, 1.0 / 2.2), pow(v.g, 1.0 / 2.2)
|
||||
|
||||
void main()
|
||||
{
|
||||
#if defined(specularFlag) || (defined(bumpHeightFlag) && defined(bumpTextureFlag))
|
||||
vec3 eyeVec = normalize(u_cameraPosition.xyz - v_worldPosition.xyz);
|
||||
#endif // specularFlag || (bumpHeightFlag && bumpTextureFlag)
|
||||
|
||||
#if defined(bumpHeightFlag) && defined(bumpTextureFlag)
|
||||
float bumpHeight = (1.0 - texture2D(u_bumpTexture, v_bumpUV).r) * u_bumpHeight;
|
||||
vec3 tbnEyeVec = normalize(mat3(
|
||||
-v_tbn[0][0], v_tbn[1][0], v_tbn[2][0],
|
||||
-v_tbn[0][1], v_tbn[1][1], v_tbn[2][1],
|
||||
-v_tbn[0][2], v_tbn[1][2], v_tbn[2][2]
|
||||
) * -eyeVec);
|
||||
vec2 uvOffset = -tbnEyeVec.xy * bumpHeight;
|
||||
#else // !(bumpHeightFlag && bumpTextureFlag)
|
||||
vec2 uvOffset = vec2(0.0);
|
||||
#endif // bumpHeightFlag && bumpTextureFlag
|
||||
|
||||
#if defined(diffuseTextureFlag) && defined(diffuseColorFlag) && defined(colorFlag)
|
||||
vec4 diffuse = texture2D(u_diffuseTexture, v_diffuseUV) * u_diffuseColor * v_color;
|
||||
vec4 diffuse = texture2D(u_diffuseTexture, v_diffuseUV + uvOffset) * u_diffuseColor * v_color;
|
||||
#elif defined(diffuseTextureFlag) && defined(diffuseColorFlag)
|
||||
vec4 diffuse = texture2D(u_diffuseTexture, v_diffuseUV) * u_diffuseColor;
|
||||
vec4 diffuse = texture2D(u_diffuseTexture, v_diffuseUV + uvOffset) * u_diffuseColor;
|
||||
#elif defined(diffuseTextureFlag) && defined(colorFlag)
|
||||
vec4 diffuse = texture2D(u_diffuseTexture, v_diffuseUV) * v_color;
|
||||
vec4 diffuse = texture2D(u_diffuseTexture, v_diffuseUV + uvOffset) * v_color;
|
||||
#elif defined(diffuseTextureFlag)
|
||||
vec4 diffuse = texture2D(u_diffuseTexture, v_diffuseUV);
|
||||
vec4 diffuse = texture2D(u_diffuseTexture, v_diffuseUV + uvOffset);
|
||||
#elif defined(diffuseColorFlag) && defined(colorFlag)
|
||||
vec4 diffuse = u_diffuseColor * v_color;
|
||||
#elif defined(diffuseColorFlag)
|
||||
@@ -120,7 +159,10 @@ void main()
|
||||
|
||||
#ifdef lightingFlag
|
||||
|
||||
#ifdef normalFlag
|
||||
#if defined(normalFlag) && defined(tangentFlag) && defined(binormalFlag) && defined(normalTextureFlag)
|
||||
vec3 normal = vec3(2.0 * texture2D(u_normalTexture, v_normalUV + uvOffset).rgb - 1.0);
|
||||
normal = normalize(v_tbn * normal);
|
||||
#elif defined(normalFlag)
|
||||
vec3 normal = normalize(v_normal);
|
||||
#endif // normalFlag
|
||||
|
||||
@@ -131,7 +173,6 @@ void main()
|
||||
#endif // ambientFlag
|
||||
|
||||
#ifdef specularFlag
|
||||
vec3 eyeVec = normalize(u_cameraPosition.xyz - v_worldPosition.xyz);
|
||||
vec3 specAccum = vec3(0.0);
|
||||
#endif // specularFlag
|
||||
|
||||
@@ -141,7 +182,7 @@ void main()
|
||||
vec3 lightVec = -u_dirLights[i].direction;
|
||||
float lambert = dot(normal, lightVec);
|
||||
float phongTerm = max(lambert, 0.0);
|
||||
vec3 value = u_dirLights[i].color * phongTerm;
|
||||
vec3 value = u_dirLights[i].color;
|
||||
value = linearEncode(value);
|
||||
value *= phongTerm;
|
||||
accum += value;
|
||||
@@ -154,10 +195,36 @@ void main()
|
||||
}
|
||||
#endif // normalFlag && numDirectionalLights
|
||||
|
||||
#if defined(normalFlag) && (numPointLights > 0)
|
||||
for (int i = 0; i < numPointLights; ++i)
|
||||
{
|
||||
vec3 lightVec = u_pointLights[i].position - v_worldPosition.xyz;
|
||||
float lightDist2 = dot(lightVec, lightVec);
|
||||
vec3 lightDir = lightVec;
|
||||
lightDir *= inversesqrt(lightDist2);
|
||||
float lambert = dot(normal, lightDir);
|
||||
float phongTerm = max(lambert, 0.0);
|
||||
//float lightDist = sqrt(lightDist2);
|
||||
//float attenuation = clamp(1.0 - lightDist * lightDist / (2.0 * 2.0), 0.0, 1.0);
|
||||
//attenuation *= attenuation;
|
||||
float attenuation = 1.0 / (1.0 + lightDist2);
|
||||
vec3 value = u_pointLights[i].color;
|
||||
value = linearEncode(value);
|
||||
value *= phongTerm * attenuation;
|
||||
accum += value;
|
||||
#ifdef specularFlag
|
||||
vec3 halfDir = normalize(lightDir + eyeVec);
|
||||
float specAngle = max(dot(halfDir, normal), 0.0);
|
||||
float specTerm = pow(specAngle, u_shininess);
|
||||
specAccum += value * specTerm;
|
||||
#endif // specularFlag
|
||||
}
|
||||
#endif
|
||||
|
||||
vec3 fragment;
|
||||
#ifdef specularFlag
|
||||
#ifdef specularTextureFlag
|
||||
vec3 specularColorTex = texture2D(u_specularTexture, v_specularUV).rgb;
|
||||
vec3 specularColorTex = texture2D(u_specularTexture, v_specularUV + uvOffset).rgb;
|
||||
specularColorTex = linearEncode(specularColorTex);
|
||||
specAccum *= specularColorTex;
|
||||
#endif // specularTextureFlag
|
||||
@@ -174,68 +241,43 @@ void main()
|
||||
fragment = diffuse.rgb;
|
||||
#endif // lightingFlag
|
||||
|
||||
#ifdef fogFlag
|
||||
#define fogDistance
|
||||
//#define fogDepth
|
||||
#ifdef fogOriginalFlag
|
||||
vec3 fogVec = u_cameraPosition.xyz - v_worldPosition.xyz;
|
||||
float fogDist = dot(fogVec, fogVec);
|
||||
float fog = min(fogDist * u_cameraPosition.w, 1.0);
|
||||
#elif defined(fogDistanceFlag)
|
||||
float fogDist = length(v_worldPosition.xyz - u_cameraPosition.xyz);
|
||||
#elif defined(fogDepthFlag)
|
||||
float fogDist = gl_FragCoord.z / gl_FragCoord.w;
|
||||
#endif // fogOriginalFlag || fogDistanceFlag || fogDepthFlag
|
||||
|
||||
//#define fogOriginal
|
||||
//#define fogLinear
|
||||
#define fogSmooth
|
||||
//#define fogInvSquare
|
||||
//#define fogExp
|
||||
//#define fogExp2
|
||||
|
||||
#if defined(fogLinear) || defined(fogSmooth) || defined(fogInvSquare)
|
||||
float near = 1.5;
|
||||
float far = 20.5;
|
||||
#elif defined(fogExp) || defined(fogExp2)
|
||||
float density = 0.12;
|
||||
#endif
|
||||
|
||||
#ifdef fogOriginal
|
||||
vec3 fvec = u_cameraPosition.xyz - v_worldPosition.xyz;
|
||||
float flen = dot(fvec, fvec);
|
||||
float fog = min(flen * u_cameraPosition.w, 1.0);
|
||||
#elif defined(fogDistance)
|
||||
float flen = length(v_worldPosition.xyz - u_cameraPosition.xyz);
|
||||
#elif defined(fogDepth)
|
||||
float flen = gl_FragCoord.z / gl_FragCoord.w;
|
||||
#endif // fogOriginal || fogDistance || fogDepth
|
||||
|
||||
#ifdef fogLinear
|
||||
#ifdef fogLinearFlag
|
||||
// fog = saturate(linearstep(near, far, x))
|
||||
float fog = clamp((flen - near) / (far - near), 0.0, 1.0);
|
||||
#elif defined(fogSmooth)
|
||||
float fog = clamp((fogDist - u_fogNear) / (u_fogFar - u_fogNear), 0.0, 1.0);
|
||||
#elif defined(fogSmoothFlag)
|
||||
// fog = smoothstep(saturate(linearstep(near, far, x)))
|
||||
float fog = clamp((flen - near) / (far - near), 0.0, 1.0);
|
||||
float fog = clamp((fogDist - u_fogNear) / (u_fogFar - u_fogNear), 0.0, 1.0);
|
||||
fog = fog * fog * (3.0 - 2.0 * fog);
|
||||
//fog = fog * fog * fog * (fog * (6.0 * fog - 15.0) + 10.0);
|
||||
#elif defined(fogInvSquare)
|
||||
#elif defined(fogInvSquareFlag)
|
||||
// fog = isqstep(saturate(linearstep(near, far, x)))
|
||||
float fog = clamp((flen - near) / (far - near), 0.0, 1.0);
|
||||
float fog = clamp((fogDist - u_fogNear) / (u_fogFar - u_fogNear), 0.0, 1.0);
|
||||
fog = 1.0 - fog;
|
||||
fog = 1.0 - fog * fog;
|
||||
#elif defined(fogExp)
|
||||
float fog = max(1.0 - exp(-density * flen), 0.0);
|
||||
#elif defined(fogExp2)
|
||||
float dz = density * flen;
|
||||
#elif defined(fogExpFlag)
|
||||
// todo: can precompute some stuff in uniforms
|
||||
float fog = max(1.0 - exp(-u_fogDensity * fogDist), 0.0);
|
||||
#elif defined(fogExp2Flag)
|
||||
// todo: can precompute some stuff in uniforms
|
||||
float dz = u_fogDensity * fogDist;
|
||||
float fog = max(1.0 - exp(-dz * dz), 0.0);
|
||||
#endif // fogLinear || fogSmooth || fogInvSquare || fogExp || fogExp2
|
||||
#endif // fogLinearFlag || fogSmoothFlag || fogInvSquareFlag || fogExpFlag || fogExp2Flag
|
||||
|
||||
#ifdef fogFlag
|
||||
vec3 fogColor = u_fogColor.rgb;
|
||||
fogColor = linearEncode(fogColor);
|
||||
fragment = mix(fragment, fogColor, fog);
|
||||
#endif // fogFlag
|
||||
|
||||
gl_FragColor = vec4(linearDecode(fragment), diffuse.a);
|
||||
/*
|
||||
if (gl_FragCoord.x > 1280.0)
|
||||
{
|
||||
if (fog <= 0.0)
|
||||
gl_FragColor = vec4(1.0, 0.0, 1.0, diffuse.a);
|
||||
else if (fog >= 1.0)
|
||||
gl_FragColor = vec4(1.0, 0.0, 0.0, diffuse.a);
|
||||
else
|
||||
gl_FragColor = vec4(fog, fog, 0.0, diffuse.a);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -7,6 +7,12 @@ uniform mat3 u_normalMatrix;
|
||||
varying vec3 v_normal;
|
||||
#endif // normalFlag
|
||||
|
||||
#if (defined(tangentFlag) && defined(binormalFlag)) || (defined(bumpHeightFlag) && defined(bumpTextureFlag))
|
||||
attribute vec3 a_tangent;
|
||||
attribute vec3 a_binormal;
|
||||
varying mat3 v_tbn;
|
||||
#endif // tangentFlag && bitangentFlag
|
||||
|
||||
#ifdef colorFlag
|
||||
varying vec4 v_color;
|
||||
attribute vec4 a_color;
|
||||
@@ -27,17 +33,27 @@ attribute vec2 a_texCoord0;
|
||||
#ifdef diffuseTextureFlag
|
||||
uniform vec4 u_diffuseUVTransform;
|
||||
varying vec2 v_diffuseUV;
|
||||
#endif
|
||||
#endif // diffuseTextureFlag
|
||||
|
||||
#ifdef bumpTextureFlag
|
||||
uniform vec4 u_bumpUVTransform;
|
||||
varying vec2 v_bumpUV;
|
||||
#endif // bumpTextureFlag
|
||||
|
||||
#ifdef normalTextureFlag
|
||||
uniform vec4 u_normalUVTransform;
|
||||
varying vec2 v_normalUV;
|
||||
#endif // normalTextureFlag
|
||||
|
||||
#ifdef emissiveTextureFlag
|
||||
uniform vec4 u_emissiveUVTransform;
|
||||
varying vec2 v_emissiveUV;
|
||||
#endif
|
||||
#endif // emissiveTextureFlag
|
||||
|
||||
#ifdef specularTextureFlag
|
||||
uniform vec4 u_specularUVTransform;
|
||||
varying vec2 v_specularUV;
|
||||
#endif
|
||||
#endif // specularTextureFlag
|
||||
|
||||
uniform mat4 u_worldTrans;
|
||||
|
||||
@@ -74,9 +90,15 @@ varying vec4 v_worldPosition;
|
||||
void main()
|
||||
{
|
||||
#ifdef normalFlag
|
||||
//vec3 normal = u_normalMatrix * a_normal;
|
||||
//vec3 normal = (transpose(inverse(u_normalMatrix)) * a_normal);
|
||||
vec3 normal = normalize(u_normalMatrix * a_normal);
|
||||
v_normal = normal;
|
||||
|
||||
#if defined(tangentFlag) && defined(binormalFlag)
|
||||
vec3 tangent = normalize(u_normalMatrix * a_tangent);
|
||||
vec3 bitangent = normalize(u_normalMatrix * a_binormal);
|
||||
v_tbn = mat3(tangent, bitangent, normal);
|
||||
#endif // tangentFlag && bitangentFlag
|
||||
#endif // normalFlag
|
||||
|
||||
#ifdef colorFlag
|
||||
@@ -87,6 +109,14 @@ void main()
|
||||
v_diffuseUV = u_diffuseUVTransform.xy + a_texCoord0 * u_diffuseUVTransform.zw;
|
||||
#endif // diffuseTextureFlag
|
||||
|
||||
#ifdef bumpTextureFlag
|
||||
v_bumpUV = u_bumpUVTransform.xy + a_texCoord0 * u_bumpUVTransform.zw;
|
||||
#endif // bumpTextureFlag
|
||||
|
||||
#ifdef normalTextureFlag
|
||||
v_normalUV = u_normalUVTransform.xy + a_texCoord0 * u_normalUVTransform.zw;
|
||||
#endif // normalTextureFlag
|
||||
|
||||
#ifdef emissiveTextureFlag
|
||||
v_emissiveUV = u_emissiveUVTransform.xy + a_texCoord0 * u_emissiveUVTransform.zw;
|
||||
#endif // emissiveTextureFlag
|
||||
|
||||
BIN
src/main/resources/suzanne_diffuse.png
Normal file
|
After Width: | Height: | Size: 656 KiB |
BIN
src/main/resources/suzanne_normal.png
Normal file
|
After Width: | Height: | Size: 881 KiB |
BIN
src/main/resources/textures/ktx_aka.png
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
BIN
src/main/resources/textures/ktx_eye2.png
Normal file
|
After Width: | Height: | Size: 475 B |
BIN
src/main/resources/textures/ktx_hada.png
Normal file
|
After Width: | Height: | Size: 127 B |
BIN
src/main/resources/textures/ktx_hoho.png
Normal file
|
After Width: | Height: | Size: 580 B |
BIN
src/main/resources/textures/ktx_kutuzoko0.png
Normal file
|
After Width: | Height: | Size: 4.8 KiB |
BIN
src/main/resources/textures/ktx_midori.png
Normal file
|
After Width: | Height: | Size: 295 B |
BIN
src/main/resources/textures/ktx_mune.png
Normal file
|
After Width: | Height: | Size: 3.2 KiB |
BIN
src/main/resources/textures/ktx_shoose0.png
Normal file
|
After Width: | Height: | Size: 1.2 KiB |
BIN
src/main/resources/textures/ktx_shoose1.png
Normal file
|
After Width: | Height: | Size: 449 B |
BIN
src/main/resources/textures/ktx_shoose2.png
Normal file
|
After Width: | Height: | Size: 433 B |
BIN
src/main/resources/textures/ktx_siro.png
Normal file
|
After Width: | Height: | Size: 124 B |
BIN
src/main/resources/textures/stx_hanasaki.png
Normal file
|
After Width: | Height: | Size: 148 B |
BIN
src/main/resources/toybox.g3db
Normal file
BIN
src/main/resources/toybox_albedo.png
Normal file
|
After Width: | Height: | Size: 1.6 MiB |
BIN
src/main/resources/toybox_displace.png
Normal file
|
After Width: | Height: | Size: 145 KiB |
BIN
src/main/resources/toybox_normal.png
Normal file
|
After Width: | Height: | Size: 231 KiB |