|
| 1 | +/* Copyright (c) 2025, Sascha Willems |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +struct VSInput |
| 8 | +{ |
| 9 | + float4 Pos; |
| 10 | + float3 Normal; |
| 11 | + float2 UV; |
| 12 | + float3 Color; |
| 13 | + float3 instancePos; |
| 14 | + float3 instanceRot; |
| 15 | + float instanceScale; |
| 16 | + int instanceTexIndex; |
| 17 | +}; |
| 18 | + |
| 19 | +struct VSOutput |
| 20 | +{ |
| 21 | + float4 Pos : SV_POSITION; |
| 22 | + float3 Normal; |
| 23 | + float3 Color; |
| 24 | + float3 UV; |
| 25 | + float3 ViewVec; |
| 26 | + float3 LightVec; |
| 27 | +}; |
| 28 | + |
| 29 | +struct UBO |
| 30 | +{ |
| 31 | + float4x4 projection; |
| 32 | + float4x4 modelview; |
| 33 | +}; |
| 34 | +ConstantBuffer<UBO> ubo; |
| 35 | + |
| 36 | +Sampler2DArray samplerArray; |
| 37 | + |
| 38 | +[shader("vertex")] |
| 39 | +VSOutput vertexMain(VSInput input) |
| 40 | +{ |
| 41 | + VSOutput output; |
| 42 | + output.Color = input.Color; |
| 43 | + output.UV = float3(input.UV, input.instanceTexIndex); |
| 44 | + |
| 45 | + float4x4 mx, my, mz; |
| 46 | + |
| 47 | + // rotate around x |
| 48 | + float s = sin(input.instanceRot.x); |
| 49 | + float c = cos(input.instanceRot.x); |
| 50 | + |
| 51 | + mx[0] = float4(c, s, 0.0, 0.0); |
| 52 | + mx[1] = float4(-s, c, 0.0, 0.0); |
| 53 | + mx[2] = float4(0.0, 0.0, 1.0, 0.0); |
| 54 | + mx[3] = float4(0.0, 0.0, 0.0, 1.0); |
| 55 | + |
| 56 | + // rotate around y |
| 57 | + s = sin(input.instanceRot.y); |
| 58 | + c = cos(input.instanceRot.y); |
| 59 | + |
| 60 | + my[0] = float4(c, 0.0, s, 0.0); |
| 61 | + my[1] = float4(0.0, 1.0, 0.0, 0.0); |
| 62 | + my[2] = float4(-s, 0.0, c, 0.0); |
| 63 | + my[3] = float4(0.0, 0.0, 0.0, 1.0); |
| 64 | + |
| 65 | + // rot around z |
| 66 | + s = sin(input.instanceRot.z); |
| 67 | + c = cos(input.instanceRot.z); |
| 68 | + |
| 69 | + mz[0] = float4(1.0, 0.0, 0.0, 0.0); |
| 70 | + mz[1] = float4(0.0, c, s, 0.0); |
| 71 | + mz[2] = float4(0.0, -s, c, 0.0); |
| 72 | + mz[3] = float4(0.0, 0.0, 0.0, 1.0); |
| 73 | + |
| 74 | + float4x4 rotMat = mul(mz, mul(my, mx)); |
| 75 | + |
| 76 | + output.Normal = mul((float4x3)rotMat, input.Normal).xyz; |
| 77 | + |
| 78 | + float4 pos = mul(rotMat, float4((input.Pos.xyz * input.instanceScale) + input.instancePos, 1.0)); |
| 79 | + |
| 80 | + output.Pos = mul(ubo.projection, mul(ubo.modelview, pos)); |
| 81 | + |
| 82 | + float4 wPos = mul(ubo.modelview, float4(pos.xyz, 1.0)); |
| 83 | + float4 lPos = float4(0.0, -5.0, 0.0, 1.0); |
| 84 | + output.LightVec = lPos.xyz - pos.xyz; |
| 85 | + output.ViewVec = -pos.xyz; |
| 86 | + return output; |
| 87 | +} |
| 88 | + |
| 89 | +[shader("fragment")] |
| 90 | +float4 fragmentMain(VSOutput input) |
| 91 | +{ |
| 92 | + float4 color = samplerArray.Sample(input.UV); |
| 93 | + |
| 94 | + if (color.a < 0.5) |
| 95 | + { |
| 96 | + clip(-1); |
| 97 | + } |
| 98 | + |
| 99 | + float3 N = normalize(input.Normal); |
| 100 | + float3 L = normalize(input.LightVec); |
| 101 | + float3 ambient = float3(0.65, 0.65, 0.65); |
| 102 | + float3 diffuse = max(dot(N, L), 0.0) * input.Color; |
| 103 | + return float4((ambient + diffuse) * color.rgb, 1.0); |
| 104 | +} |
0 commit comments