add more model & shader examples

This commit is contained in:
unkl nik 2023-10-25 21:53:53 +02:00
parent 32413f57ee
commit 35682b2387
11 changed files with 357 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View file

@ -0,0 +1,65 @@
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 outline")
texture := rl.LoadTexture("gopher.png")
shader := rl.LoadShader("", "outline.fs")
cnt := rl.NewVector2(float32(screenWidth/2), float32(screenHeight/2))
outlineSize := []float32{2}
outlineColor := []float32{1, 0, 0, 1}
textureSize := []float32{float32(texture.Width), float32(texture.Height)}
outlineSizeLoc := rl.GetShaderLocation(shader, "outlineSize")
outlineColorLoc := rl.GetShaderLocation(shader, "outlineColor")
textureSizeLoc := rl.GetShaderLocation(shader, "textureSize")
rl.SetShaderValue(shader, outlineSizeLoc, outlineSize, rl.ShaderUniformFloat)
rl.SetShaderValue(shader, outlineColorLoc, outlineColor, rl.ShaderUniformVec4)
rl.SetShaderValue(shader, textureSizeLoc, textureSize, rl.ShaderUniformVec2)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if rl.IsKeyPressed(rl.KeyUp) {
outlineSize[0]++
} else if rl.IsKeyPressed(rl.KeyDown) {
outlineSize[0]--
if outlineSize[0] < 1 {
outlineSize[0] = 1
}
}
rl.SetShaderValue(shader, outlineSizeLoc, outlineSize, rl.ShaderUniformFloat)
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginShaderMode(shader)
rl.DrawTexture(texture, int32(cnt.X)-texture.Width/2, int32(cnt.Y)-texture.Height/2, rl.White)
rl.EndShaderMode()
rl.DrawText("UP/DOWN ARROW KEYS INCREASE OUTLINE THICKNESS", 10, 10, 10, rl.Black)
rl.EndDrawing()
}
rl.UnloadShader(shader)
rl.UnloadTexture(texture)
rl.CloseWindow()
}

View file

@ -0,0 +1,35 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
uniform vec2 textureSize;
uniform float outlineSize;
uniform vec4 outlineColor;
// Output fragment color
out vec4 finalColor;
void main()
{
vec4 texel = texture(texture0, fragTexCoord); // Get texel color
vec2 texelScale = vec2(0.0);
texelScale.x = outlineSize/textureSize.x;
texelScale.y = outlineSize/textureSize.y;
// We sample four corner texels, but only for the alpha channel (this is for the outline)
vec4 corners = vec4(0.0);
corners.x = texture(texture0, fragTexCoord + vec2(texelScale.x, texelScale.y)).a;
corners.y = texture(texture0, fragTexCoord + vec2(texelScale.x, -texelScale.y)).a;
corners.z = texture(texture0, fragTexCoord + vec2(-texelScale.x, texelScale.y)).a;
corners.w = texture(texture0, fragTexCoord + vec2(-texelScale.x, -texelScale.y)).a;
float outline = min(dot(corners, vec4(1.0)), 1.0);
vec4 color = mix(vec4(0.0), outlineColor, outline);
finalColor = mix(color, texel, texel.a);
}