1617a3babSopenharmony_ci/* 2617a3babSopenharmony_ciThe MIT License (MIT) 3617a3babSopenharmony_ci 4617a3babSopenharmony_ciCopyright (c) 2022 Google LLC 5617a3babSopenharmony_ciCopyright (c) 2022 Sascha Willems 6617a3babSopenharmony_ci 7617a3babSopenharmony_ciPermission is hereby granted, free of charge, to any person obtaining a copy 8617a3babSopenharmony_ciof this software and associated documentation files (the "Software"), to deal 9617a3babSopenharmony_ciin the Software without restriction, including without limitation the rights 10617a3babSopenharmony_cito use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11617a3babSopenharmony_cicopies of the Software, and to permit persons to whom the Software is 12617a3babSopenharmony_cifurnished to do so, subject to the following conditions: 13617a3babSopenharmony_ci 14617a3babSopenharmony_ciThe above copyright notice and this permission notice shall be included in all 15617a3babSopenharmony_cicopies or substantial portions of the Software. 16617a3babSopenharmony_ci 17617a3babSopenharmony_ciTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18617a3babSopenharmony_ciIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19617a3babSopenharmony_ciFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20617a3babSopenharmony_ciAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21617a3babSopenharmony_ciLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22617a3babSopenharmony_ciOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23617a3babSopenharmony_ciSOFTWARE. 24617a3babSopenharmony_ci*/ 25617a3babSopenharmony_ci 26617a3babSopenharmony_cistruct VSInput 27617a3babSopenharmony_ci{ 28617a3babSopenharmony_ci[[vk::location(0)]] float3 Pos : POSITION0; 29617a3babSopenharmony_ci[[vk::location(1)]] float3 Normal : NORMAL0; 30617a3babSopenharmony_ci[[vk::location(2)]] float2 UV : TEXCOORD0; 31617a3babSopenharmony_ci[[vk::location(3)]] float3 Color : COLOR0; 32617a3babSopenharmony_ci 33617a3babSopenharmony_ci// Instanced attributes 34617a3babSopenharmony_ci[[vk::location(4)]] float3 instancePos : POSITION1; 35617a3babSopenharmony_ci[[vk::location(5)]] float3 instanceRot : TEXCOORD1; 36617a3babSopenharmony_ci[[vk::location(6)]] float instanceScale : TEXCOORD2; 37617a3babSopenharmony_ci[[vk::location(7)]] int instanceTexIndex : TEXCOORD3; 38617a3babSopenharmony_ci}; 39617a3babSopenharmony_ci 40617a3babSopenharmony_cistruct UBO 41617a3babSopenharmony_ci{ 42617a3babSopenharmony_ci float4x4 projection; 43617a3babSopenharmony_ci float4x4 modelview; 44617a3babSopenharmony_ci float4 lightPos; 45617a3babSopenharmony_ci float locSpeed; 46617a3babSopenharmony_ci float globSpeed; 47617a3babSopenharmony_ci}; 48617a3babSopenharmony_ci 49617a3babSopenharmony_cicbuffer ubo : register(b0) { UBO ubo; } 50617a3babSopenharmony_ci 51617a3babSopenharmony_cistruct VSOutput 52617a3babSopenharmony_ci{ 53617a3babSopenharmony_ci float4 Pos : SV_POSITION; 54617a3babSopenharmony_ci[[vk::location(0)]] float3 Normal : NORMAL0; 55617a3babSopenharmony_ci[[vk::location(1)]] float3 Color : COLOR0; 56617a3babSopenharmony_ci[[vk::location(2)]] float3 UV : TEXCOORD0; 57617a3babSopenharmony_ci[[vk::location(3)]] float3 ViewVec : TEXCOORD1; 58617a3babSopenharmony_ci[[vk::location(4)]] float3 LightVec : TEXCOORD2; 59617a3babSopenharmony_ci}; 60617a3babSopenharmony_ci 61617a3babSopenharmony_ciVSOutput main(VSInput input) 62617a3babSopenharmony_ci{ 63617a3babSopenharmony_ci VSOutput output = (VSOutput)0; 64617a3babSopenharmony_ci output.Color = input.Color; 65617a3babSopenharmony_ci output.UV = float3(input.UV, input.instanceTexIndex); 66617a3babSopenharmony_ci 67617a3babSopenharmony_ci // rotate around x 68617a3babSopenharmony_ci float s = sin(input.instanceRot.x + ubo.locSpeed); 69617a3babSopenharmony_ci float c = cos(input.instanceRot.x + ubo.locSpeed); 70617a3babSopenharmony_ci 71617a3babSopenharmony_ci float3x3 mx = { c, -s, 0.0, 72617a3babSopenharmony_ci s, c, 0.0, 73617a3babSopenharmony_ci 0.0, 0.0, 1.0 }; 74617a3babSopenharmony_ci 75617a3babSopenharmony_ci // rotate around y 76617a3babSopenharmony_ci s = sin(input.instanceRot.y + ubo.locSpeed); 77617a3babSopenharmony_ci c = cos(input.instanceRot.y + ubo.locSpeed); 78617a3babSopenharmony_ci 79617a3babSopenharmony_ci float3x3 my = { c, 0.0, -s, 80617a3babSopenharmony_ci 0.0, 1.0, 0.0, 81617a3babSopenharmony_ci s, 0.0, c }; 82617a3babSopenharmony_ci 83617a3babSopenharmony_ci // rot around z 84617a3babSopenharmony_ci s = sin(input.instanceRot.z + ubo.locSpeed); 85617a3babSopenharmony_ci c = cos(input.instanceRot.z + ubo.locSpeed); 86617a3babSopenharmony_ci 87617a3babSopenharmony_ci float3x3 mz = { 1.0, 0.0, 0.0, 88617a3babSopenharmony_ci 0.0, c, -s, 89617a3babSopenharmony_ci 0.0, s, c }; 90617a3babSopenharmony_ci 91617a3babSopenharmony_ci float3x3 rotMat = mul(mz, mul(my, mx)); 92617a3babSopenharmony_ci 93617a3babSopenharmony_ci float4x4 gRotMat; 94617a3babSopenharmony_ci s = sin(input.instanceRot.y + ubo.globSpeed); 95617a3babSopenharmony_ci c = cos(input.instanceRot.y + ubo.globSpeed); 96617a3babSopenharmony_ci gRotMat[0] = float4(c, 0.0, -s, 0.0); 97617a3babSopenharmony_ci gRotMat[1] = float4(0.0, 1.0, 0.0, 0.0); 98617a3babSopenharmony_ci gRotMat[2] = float4(s, 0.0, c, 0.0); 99617a3babSopenharmony_ci gRotMat[3] = float4(0.0, 0.0, 0.0, 1.0); 100617a3babSopenharmony_ci 101617a3babSopenharmony_ci float4 locPos = float4(mul(rotMat, input.Pos.xyz), 1.0); 102617a3babSopenharmony_ci float4 pos = float4((locPos.xyz * input.instanceScale) + input.instancePos, 1.0); 103617a3babSopenharmony_ci 104617a3babSopenharmony_ci output.Pos = mul(ubo.projection, mul(ubo.modelview, mul(gRotMat, pos))); 105617a3babSopenharmony_ci output.Normal = mul((float3x3)mul(ubo.modelview, gRotMat), mul(rotMat, input.Normal)); 106617a3babSopenharmony_ci 107617a3babSopenharmony_ci pos = mul(ubo.modelview, float4(input.Pos.xyz + input.instancePos, 1.0)); 108617a3babSopenharmony_ci float3 lPos = mul((float3x3)ubo.modelview, ubo.lightPos.xyz); 109617a3babSopenharmony_ci output.LightVec = lPos - pos.xyz; 110617a3babSopenharmony_ci output.ViewVec = -pos.xyz; 111617a3babSopenharmony_ci return output; 112617a3babSopenharmony_ci} 113