Skip to content

Commit

Permalink
Restores indigo.syntax.shaders.* for conversion to ultraviolet types
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 18, 2024
1 parent a5bb494 commit ee0d05f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
22 changes: 20 additions & 2 deletions indigo/indigo/src/main/scala/indigo/IndigoShader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,26 @@ trait IndigoShader extends GameLauncher[IndigoShaderModel, IndigoShaderModel, Un
/** The uniform blocks (data) you want to pass to your shader. Example:
*
* ```scala
* final case class CustomData(color: RGBA, customTime: Seconds) extends FragmentEnvReference derives ToUniformBlock
* def uniformBlocks: Batch[UniformBlock] = Batch(CustomData(RGBA.Magenta, 0.seconds))
* import indigo.*
* import indigo.syntax.shaders.*
* import ultraviolet.syntax.*
*
* final case class CustomData(color: vec4, customTime: Float) extends FragmentEnvReference derives ToUniformBlock
* def uniformBlocks: Batch[UniformBlock] = Batch(CustomData(RGBA.Magenta.toUVVec4, 0.seconds.toFloat))
* ```
*
* As long as the field types in your case class are ultraviolet types, you can pass them to your shader, see
* Ultraviolet docs for more info.
*
* Many standard Indigo types are supported for the data fields, but you will need a separate case class for the
* Shader side of the data contract definition, i.e. This is valid too:
*
* ```scala
* // For use with Indigo's shader setup. Note: derives ToUniformBlock, but doesn't need to extend FragmentEnvReference
* final case class CustomDataIndigo(color: RGBA, customTime: Seconds) derives ToUniformBlock
*
* // For use with Ultraviolet's UBO definitions. Note extends FragmentEnvReference, but doesn't derive ToUniformBlock
* final case class CustomDataUV(color: vec4, customTime: Float) extends FragmentEnvReference
* ```
*/
def uniformBlocks: Batch[UniformBlock]
Expand Down
53 changes: 53 additions & 0 deletions indigo/indigo/src/main/scala/indigo/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,59 @@ object syntax:
export SignalFunction.multiply
end animations

object shaders:

extension (c: RGBA)
def toUVVec4: ultraviolet.syntax.vec4 =
ultraviolet.syntax.vec4(c.r.toFloat, c.g.toFloat, c.b.toFloat, c.a.toFloat)
extension (c: RGB)
def toUVVec3: ultraviolet.syntax.vec3 =
ultraviolet.syntax.vec3(c.r.toFloat, c.g.toFloat, c.b.toFloat)
extension (p: Point)
def toUVVec2: ultraviolet.syntax.vec2 =
ultraviolet.syntax.vec2(p.x.toFloat, p.y.toFloat)
extension (s: Size)
def toUVVec2: ultraviolet.syntax.vec2 =
ultraviolet.syntax.vec2(s.width.toFloat, s.height.toFloat)
extension (v: Vector2)
def toUVVec2: ultraviolet.syntax.vec2 =
ultraviolet.syntax.vec2(v.x.toFloat, v.y.toFloat)
extension (v: Vector3)
def toUVVec3: ultraviolet.syntax.vec3 =
ultraviolet.syntax.vec3(v.x.toFloat, v.y.toFloat, v.z.toFloat)
extension (v: Vector4)
def toUVVec4: ultraviolet.syntax.vec4 =
ultraviolet.syntax.vec4(v.x.toFloat, v.y.toFloat, v.z.toFloat, v.w.toFloat)
extension (r: Rectangle)
def toUVVec4: ultraviolet.syntax.vec4 =
ultraviolet.syntax.vec4(r.x.toFloat, r.y.toFloat, r.width.toFloat, r.height.toFloat)
extension (m: Matrix4)
def toUVMat4: ultraviolet.syntax.mat4 =
ultraviolet.syntax.mat4(m.toArray.map(_.toFloat))
extension (d: Depth) def toUVFloat: Float = d.toFloat
extension (m: Millis) def toUVFloat: Float = m.toFloat
extension (r: Radians) def toUVFloat: Float = r.toFloat
extension (s: Seconds)
@targetName("ext_Seconds_toUVFloat")
def toUVFloat: Float = s.toFloat
extension (d: Double)
@targetName("ext_Double_toUVFloat")
def toUVFloat: Float = d.toFloat
extension (i: Int)
@targetName("ext_Int_toUVFloat")
def toUVFloat: Float = i.toFloat
extension (l: Long)
@targetName("ext_Long_toUVFloat")
def toUVFloat: Float = l.toFloat
extension (a: Array[Float])
def toUVArray: ultraviolet.syntax.array[Singleton & Int, Float] =
ultraviolet.syntax.array(a)
extension (a: scalajs.js.Array[Float])
def toUVArray: ultraviolet.syntax.array[Singleton & Int, Float] =
ultraviolet.syntax.array(a.toArray)

end shaders

end syntax

object mutable:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import indigo.*
import indigo.syntax.shaders.*
import ultraviolet.syntax.*

import scala.scalajs.js.annotation._
Expand All @@ -24,7 +25,7 @@ object ShaderGame extends IndigoShader:
final case class CustomData(CUSTOM_COLOR: vec4) extends FragmentEnvReference derives ToUniformBlock
object CustomData:
val reference =
CustomData(vec4(1.0f, 0.0f, 1.0f, 1.0f))
CustomData(RGBA.Magenta.toUVVec4)

object ShaderWithData:

Expand Down

0 comments on commit ee0d05f

Please sign in to comment.