add more texture & shader examples

This commit is contained in:
unkl nik 2023-10-21 22:23:22 +02:00
parent 0dda56d08d
commit f121c0e386
7 changed files with 415 additions and 0 deletions

View file

@ -0,0 +1,61 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Output fragment color
out vec4 finalColor;
// Custom variables
#define PI 3.14159265358979323846
uniform float uTime = 0.0;
float divisions = 5.0;
float angle = 0.0;
vec2 VectorRotateTime(vec2 v, float speed)
{
float time = uTime*speed;
float localTime = fract(time); // The time domain this works on is 1 sec.
if ((localTime >= 0.0) && (localTime < 0.25)) angle = 0.0;
else if ((localTime >= 0.25) && (localTime < 0.50)) angle = PI/4*sin(2*PI*localTime - PI/2);
else if ((localTime >= 0.50) && (localTime < 0.75)) angle = PI*0.25;
else if ((localTime >= 0.75) && (localTime < 1.00)) angle = PI/4*sin(2*PI*localTime);
// Rotate vector by angle
v -= 0.5;
v = mat2(cos(angle), -sin(angle), sin(angle), cos(angle))*v;
v += 0.5;
return v;
}
float Rectangle(in vec2 st, in float size, in float fill)
{
float roundSize = 0.5 - size/2.0;
float left = step(roundSize, st.x);
float top = step(roundSize, st.y);
float bottom = step(roundSize, 1.0 - st.y);
float right = step(roundSize, 1.0 - st.x);
return (left*bottom*right*top)*fill;
}
void main()
{
vec2 fragPos = fragTexCoord;
fragPos.xy += uTime/9.0;
fragPos *= divisions;
vec2 ipos = floor(fragPos); // Get the integer coords
vec2 fpos = fract(fragPos); // Get the fractional coords
fpos = VectorRotateTime(fpos, 0.2);
float alpha = Rectangle(fpos, 0.216, 1.0);
vec3 color = vec3(0.3, 0.3, 0.3);
finalColor = vec4(color, alpha);
}

View file

@ -0,0 +1,49 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture drawing")
imBlank := rl.GenImageColor(1024, 1024, rl.Blank)
texture := rl.LoadTextureFromImage(imBlank)
rl.UnloadImage(imBlank)
shader := rl.LoadShader("", "cubes_panning.fs")
time := []float32{0}
timeLoc := rl.GetShaderLocation(shader, "uTime")
rl.SetShaderValue(shader, timeLoc, time, rl.ShaderUniformFloat)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
time = nil
time = []float32{float32(rl.GetTime())}
rl.SetShaderValue(shader, timeLoc, time, rl.ShaderUniformFloat)
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginShaderMode(shader)
rl.DrawTexture(texture, 0, 0, rl.White)
rl.EndShaderMode()
rl.DrawText("BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, rl.Maroon)
rl.EndDrawing()
}
rl.UnloadShader(shader) // Unload shader
rl.CloseWindow()
}

View file

@ -0,0 +1,67 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture waves")
texture := rl.LoadTexture("space.png")
shader := rl.LoadShader("", "wave.fs")
secondsLoc := rl.GetShaderLocation(shader, "seconds")
freqXLoc := rl.GetShaderLocation(shader, "freqX")
freqYLoc := rl.GetShaderLocation(shader, "freqY")
ampXLoc := rl.GetShaderLocation(shader, "ampX")
ampYLoc := rl.GetShaderLocation(shader, "ampY")
speedXLoc := rl.GetShaderLocation(shader, "speedX")
speedYLoc := rl.GetShaderLocation(shader, "speedY")
freqX := []float32{25}
freqY := []float32{25}
ampX := []float32{5}
ampY := []float32{5}
speedX := []float32{8}
speedY := []float32{8}
screensize := []float32{float32(rl.GetScreenWidth()), float32(rl.GetScreenHeight())}
rl.SetShaderValue(shader, rl.GetShaderLocation(shader, "size"), screensize, rl.ShaderUniformVec2)
rl.SetShaderValue(shader, freqXLoc, freqX, rl.ShaderUniformFloat)
rl.SetShaderValue(shader, freqYLoc, freqY, rl.ShaderUniformFloat)
rl.SetShaderValue(shader, ampXLoc, ampX, rl.ShaderUniformFloat)
rl.SetShaderValue(shader, ampYLoc, ampY, rl.ShaderUniformFloat)
rl.SetShaderValue(shader, speedXLoc, speedX, rl.ShaderUniformFloat)
rl.SetShaderValue(shader, speedYLoc, speedY, rl.ShaderUniformFloat)
seconds := []float32{0}
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
seconds[0] += rl.GetFrameTime()
rl.SetShaderValue(shader, secondsLoc, seconds, rl.ShaderUniformFloat)
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginShaderMode(shader)
rl.DrawTexture(texture, 0, 0, rl.White)
rl.DrawTexture(texture, texture.Width, 0, rl.White)
rl.EndShaderMode()
rl.EndDrawing()
}
rl.UnloadShader(shader)
rl.UnloadTexture(texture)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View file

@ -0,0 +1,37 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
uniform float seconds;
uniform vec2 size;
uniform float freqX;
uniform float freqY;
uniform float ampX;
uniform float ampY;
uniform float speedX;
uniform float speedY;
void main() {
float pixelWidth = 1.0 / size.x;
float pixelHeight = 1.0 / size.y;
float aspect = pixelHeight / pixelWidth;
float boxLeft = 0.0;
float boxTop = 0.0;
vec2 p = fragTexCoord;
p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (seconds * speedX)) * ampX * pixelWidth;
p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (seconds * speedY)) * ampY * pixelHeight;
finalColor = texture(texture0, p)*colDiffuse*fragColor;
}

View file

@ -0,0 +1,201 @@
package main
import (
"fmt"
"math"
rl "github.com/gen2brain/raylib-go/raylib"
)
var (
texRoad rl.Texture2D
showCurve = true
curveW = float32(50)
curveSegments = 24
curveStartPos, curveStartPosTangent, curveEndPos, curveEndPosTangent rl.Vector2
curveSelectedPoint *rl.Vector2
screenW = int32(800)
screenH = int32(450)
)
func main() {
rl.SetConfigFlags(rl.FlagVsyncHint | rl.FlagMsaa4xHint)
rl.InitWindow(screenW, screenH, "raylib [textures] example - textured curve")
texRoad = rl.LoadTexture("road.png")
rl.SetTextureFilter(texRoad, rl.TextureFilterMode(rl.FilterBilinear))
curveStartPos = rl.NewVector2(80, 100)
curveStartPosTangent = rl.NewVector2(100, 300)
curveEndPos = rl.NewVector2(700, 350)
curveEndPosTangent = rl.NewVector2(600, 100)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
upCurve()
upOptions()
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
drawTexturedCurve()
drawCurve()
rl.DrawText("Drag points to move curve, press SPACE to show/hide base curve", 10, 10, 10, rl.Black)
rl.DrawText("Curve width: "+fmt.Sprintf("%.0f", curveW)+" use UP/DOWN arrows to adjust", 10, 30, 10, rl.Black)
rl.DrawText("Curve segments: "+fmt.Sprint(curveSegments)+" use RIGHT/LEFT arrows to adjust", 10, 50, 10, rl.Black)
rl.EndDrawing()
}
rl.UnloadTexture(texRoad)
rl.CloseWindow()
}
func upCurve() {
if !rl.IsMouseButtonDown(rl.MouseLeftButton) {
curveSelectedPoint = &rl.Vector2{}
}
*curveSelectedPoint = rl.Vector2Add(*curveSelectedPoint, rl.GetMouseDelta())
mouse := rl.GetMousePosition()
if rl.CheckCollisionPointCircle(mouse, curveStartPos, 6) {
curveSelectedPoint = &curveStartPos
} else if rl.CheckCollisionPointCircle(mouse, curveStartPosTangent, 6) {
curveSelectedPoint = &curveStartPosTangent
} else if rl.CheckCollisionPointCircle(mouse, curveEndPos, 6) {
curveSelectedPoint = &curveEndPos
} else if rl.CheckCollisionPointCircle(mouse, curveEndPosTangent, 6) {
curveSelectedPoint = &curveEndPosTangent
}
}
func upOptions() {
if rl.IsKeyPressed(rl.KeySpace) {
showCurve = !showCurve
}
if rl.IsKeyPressed(rl.KeyUp) {
curveW += 2
}
if rl.IsKeyPressed(rl.KeyDown) {
curveW -= 2
}
if curveW < 2 {
curveW = 2
}
if rl.IsKeyPressed(rl.KeyLeft) {
curveSegments -= 2
}
if rl.IsKeyPressed(rl.KeyRight) {
curveSegments += 2
}
if curveSegments < 2 {
curveSegments = 2
}
}
func drawTexturedCurve() {
step := float32(1) / float32(curveSegments)
previous := curveStartPos
previousTangent := rl.Vector2Zero()
previousV := float32(0)
tangentSet := false
current := rl.Vector2Zero()
t := float32(0)
for i := 0; i < curveSegments; i++ {
t = step * float32(i)
a := float32(math.Pow(1-float64(t), 3))
b := 3 * float32(math.Pow(1-float64(t), 2)) * t
c := 3 * (1 - t) * float32(math.Pow(float64(t), 2))
d := float32(math.Pow(float64(t), 3))
current.Y = a*curveStartPos.Y + b*curveStartPosTangent.Y + c*curveEndPosTangent.Y + d*curveEndPos.Y
current.X = a*curveStartPos.X + b*curveStartPosTangent.X + c*curveEndPosTangent.X + d*curveEndPos.X
delta := rl.NewVector2(current.X-previous.X, current.Y-previous.Y)
normal := rl.Vector2Normalize(rl.NewVector2(-delta.Y, delta.X))
v := previousV + rl.Vector2Length(delta)
if !tangentSet {
previousTangent = normal
tangentSet = true
}
prevPosNormal := rl.Vector2Add(previous, rl.Vector2Scale(previousTangent, curveW))
prevNegNormal := rl.Vector2Add(previous, rl.Vector2Scale(previousTangent, -curveW))
currentPosNormal := rl.Vector2Add(current, rl.Vector2Scale(normal, curveW))
currentNegNormal := rl.Vector2Add(current, rl.Vector2Scale(normal, -curveW))
rl.SetTexture(texRoad.ID)
rl.Begin(rl.RL_QUADS)
rl.Color4ub(255, 255, 255, 255)
rl.Normal3f(0, 0, 1)
rl.TexCoord2f(0, previousV)
rl.Vertex2f(prevNegNormal.X, prevNegNormal.Y)
rl.TexCoord2f(1, previousV)
rl.Vertex2f(prevPosNormal.X, prevPosNormal.Y)
rl.TexCoord2f(1, v)
rl.Vertex2f(currentPosNormal.X, currentPosNormal.Y)
rl.TexCoord2f(0, v)
rl.Vertex2f(currentNegNormal.X, currentNegNormal.Y)
rl.End()
previous = current
previousTangent = normal
previousV = v
}
}
func drawCurve() {
if showCurve {
rl.DrawLineBezierCubic(curveStartPos, curveEndPos, curveStartPosTangent, curveEndPosTangent, 2, rl.Blue)
}
rl.DrawLineV(curveStartPos, curveStartPosTangent, rl.SkyBlue)
rl.DrawLineV(curveEndPos, curveEndPosTangent, rl.Purple)
mouse := rl.GetMousePosition()
if rl.CheckCollisionPointCircle(mouse, curveStartPos, 6) {
rl.DrawCircleV(curveStartPos, 7, rl.Yellow)
}
rl.DrawCircleV(curveStartPos, 5, rl.Red)
if rl.CheckCollisionPointCircle(mouse, curveStartPosTangent, 6) {
rl.DrawCircleV(curveStartPosTangent, 7, rl.Yellow)
}
rl.DrawCircleV(curveStartPosTangent, 5, rl.Maroon)
if rl.CheckCollisionPointCircle(mouse, curveEndPos, 6) {
rl.DrawCircleV(curveEndPos, 7, rl.Yellow)
}
rl.DrawCircleV(curveEndPosTangent, 5, rl.Green)
if rl.CheckCollisionPointCircle(mouse, curveEndPosTangent, 6) {
rl.DrawCircleV(curveEndPosTangent, 7, rl.Yellow)
}
rl.DrawCircleV(curveEndPosTangent, 5, rl.DarkGreen)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1,006 B