1/* 2The MIT License (MIT) 3 4Copyright (c) 2022 Google LLC 5Copyright (c) 2022 Sascha Willems 6 7Permission is hereby granted, free of charge, to any person obtaining a copy 8of this software and associated documentation files (the "Software"), to deal 9in the Software without restriction, including without limitation the rights 10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11copies of the Software, and to permit persons to whom the Software is 12furnished to do so, subject to the following conditions: 13 14The above copyright notice and this permission notice shall be included in all 15copies or substantial portions of the Software. 16 17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23SOFTWARE. 24*/ 25 26struct VSInput 27{ 28[[vk::location(0)]] float3 Pos : POSITION0; 29[[vk::location(1)]] float3 Normal : NORMAL0; 30[[vk::location(2)]] float2 UV : TEXCOORD0; 31[[vk::location(3)]] float3 Color : COLOR0; 32 33// Instanced attributes 34[[vk::location(4)]] float3 instancePos : POSITION1; 35[[vk::location(5)]] float3 instanceRot : TEXCOORD1; 36[[vk::location(6)]] float instanceScale : TEXCOORD2; 37[[vk::location(7)]] int instanceTexIndex : TEXCOORD3; 38}; 39 40struct UBO 41{ 42 float4x4 projection; 43 float4x4 modelview; 44 float4 lightPos; 45 float locSpeed; 46 float globSpeed; 47}; 48 49cbuffer ubo : register(b0) { UBO ubo; } 50 51struct VSOutput 52{ 53 float4 Pos : SV_POSITION; 54[[vk::location(0)]] float3 Normal : NORMAL0; 55[[vk::location(1)]] float3 Color : COLOR0; 56[[vk::location(2)]] float3 UV : TEXCOORD0; 57[[vk::location(3)]] float3 ViewVec : TEXCOORD1; 58[[vk::location(4)]] float3 LightVec : TEXCOORD2; 59}; 60 61VSOutput main(VSInput input) 62{ 63 VSOutput output = (VSOutput)0; 64 output.Color = input.Color; 65 output.UV = float3(input.UV, input.instanceTexIndex); 66 67 // rotate around x 68 float s = sin(input.instanceRot.x + ubo.locSpeed); 69 float c = cos(input.instanceRot.x + ubo.locSpeed); 70 71 float3x3 mx = { c, -s, 0.0, 72 s, c, 0.0, 73 0.0, 0.0, 1.0 }; 74 75 // rotate around y 76 s = sin(input.instanceRot.y + ubo.locSpeed); 77 c = cos(input.instanceRot.y + ubo.locSpeed); 78 79 float3x3 my = { c, 0.0, -s, 80 0.0, 1.0, 0.0, 81 s, 0.0, c }; 82 83 // rot around z 84 s = sin(input.instanceRot.z + ubo.locSpeed); 85 c = cos(input.instanceRot.z + ubo.locSpeed); 86 87 float3x3 mz = { 1.0, 0.0, 0.0, 88 0.0, c, -s, 89 0.0, s, c }; 90 91 float3x3 rotMat = mul(mz, mul(my, mx)); 92 93 float4x4 gRotMat; 94 s = sin(input.instanceRot.y + ubo.globSpeed); 95 c = cos(input.instanceRot.y + ubo.globSpeed); 96 gRotMat[0] = float4(c, 0.0, -s, 0.0); 97 gRotMat[1] = float4(0.0, 1.0, 0.0, 0.0); 98 gRotMat[2] = float4(s, 0.0, c, 0.0); 99 gRotMat[3] = float4(0.0, 0.0, 0.0, 1.0); 100 101 float4 locPos = float4(mul(rotMat, input.Pos.xyz), 1.0); 102 float4 pos = float4((locPos.xyz * input.instanceScale) + input.instancePos, 1.0); 103 104 output.Pos = mul(ubo.projection, mul(ubo.modelview, mul(gRotMat, pos))); 105 output.Normal = mul((float3x3)mul(ubo.modelview, gRotMat), mul(rotMat, input.Normal)); 106 107 pos = mul(ubo.modelview, float4(input.Pos.xyz + input.instancePos, 1.0)); 108 float3 lPos = mul((float3x3)ubo.modelview, ubo.lightPos.xyz); 109 output.LightVec = lPos - pos.xyz; 110 output.ViewVec = -pos.xyz; 111 return output; 112} 113