Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Zitat
Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.
Was soll ich für eine Textur übergeben die zu beleuchten ist?
Zitat
Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.
Danke. Nur was wenn ich ein Objekt habe was mehrer Texturen hat?
Zitat
Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.
Also kurz gesagt wie weiß der Shader die Entfernung zur Lichtquelle.
Hallo. Nun ich habe es jetzt hinbekommen auf einem Würfel mit Textur ein per Pixel Spotlight zu benutzen
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
struct Light { float3 dir; // world space direction float3 pos; // world space position float4 ambient; float4 diffuse; float4 specular; float spotInnerCone; // spot light inner cone (theta) angle float spotOuterCone; // spot light outer cone (phi) angle float radius; // applies to point and spot lights only }; struct Material { float4 ambient; float4 diffuse; float4 emissive; float4 specular; float shininess; }; //----------------------------------------------------------------------------- // Globals. //----------------------------------------------------------------------------- float4x4 worldMatrix; float4x4 worldInverseTransposeMatrix; float4x4 worldViewProjectionMatrix; float3 cameraPos; float4 globalAmbient; Light light; Material material; //----------------------------------------------------------------------------- // Textures. //----------------------------------------------------------------------------- texture colorMapTexture; sampler2D colorMap = sampler_state { Texture = <colorMapTexture>; MagFilter = Linear; MinFilter = Anisotropic; MipFilter = Linear; MaxAnisotropy = 16; }; //----------------------------------------------------------------------------- // Vertex Shaders. //----------------------------------------------------------------------------- struct VS_INPUT { float3 position : POSITION; float2 texCoord : TEXCOORD0; float3 normal : NORMAL; }; struct VS_OUTPUT_DIR { float4 position : POSITION; float2 texCoord : TEXCOORD0; float3 halfVector : TEXCOORD1; float3 lightDir : TEXCOORD2; float3 normal : TEXCOORD3; float4 diffuse : COLOR0; float4 specular : COLOR1; }; struct VS_OUTPUT_POINT { float4 position : POSITION; float2 texCoord : TEXCOORD0; float3 viewDir : TEXCOORD1; float3 lightDir : TEXCOORD2; float3 normal : TEXCOORD3; float4 diffuse : COLOR0; float4 specular : COLOR1; }; struct VS_OUTPUT_SPOT { float4 position : POSITION; float2 texCoord : TEXCOORD0; float3 viewDir : TEXCOORD1; float3 lightDir : TEXCOORD2; float3 spotDir : TEXCOORD3; float3 normal : TEXCOORD4; float4 diffuse : COLOR0; float4 specular : COLOR1; }; VS_OUTPUT_DIR VS_DirLighting(VS_INPUT IN) { VS_OUTPUT_DIR OUT; float3 worldPos = mul(float4(IN.position, 1.0f), worldMatrix).xyz; float3 viewDir = cameraPos - worldPos; OUT.position = mul(float4(IN.position, 1.0f), worldViewProjectionMatrix); OUT.texCoord = IN.texCoord; OUT.lightDir = -light.dir; OUT.halfVector = normalize(normalize(OUT.lightDir) + normalize(viewDir)); OUT.normal = mul(IN.normal, (float3x3)worldInverseTransposeMatrix); OUT.diffuse = material.diffuse * light.diffuse; OUT.specular = material.specular * light.specular; return OUT; } VS_OUTPUT_POINT VS_PointLighting(VS_INPUT IN) { VS_OUTPUT_POINT OUT; float3 worldPos = mul(float4(IN.position, 1.0f), worldMatrix).xyz; OUT.position = mul(float4(IN.position, 1.0f), worldViewProjectionMatrix); OUT.texCoord = IN.texCoord; OUT.viewDir = cameraPos - worldPos; OUT.lightDir = (light.pos - worldPos) / light.radius; OUT.normal = mul(IN.normal, (float3x3)worldInverseTransposeMatrix); OUT.diffuse = material.diffuse * light.diffuse; OUT.specular = material.specular * light.specular; return OUT; } VS_OUTPUT_SPOT VS_SpotLighting(VS_INPUT IN) { VS_OUTPUT_SPOT OUT; float3 worldPos = mul(float4(IN.position, 1.0f), worldMatrix).xyz; OUT.position = mul(float4(IN.position, 1.0f), worldViewProjectionMatrix); OUT.texCoord = IN.texCoord; OUT.viewDir = cameraPos - worldPos; OUT.lightDir = (light.pos - worldPos) / light.radius; OUT.spotDir = light.dir; OUT.normal = mul(IN.normal, (float3x3)worldInverseTransposeMatrix); OUT.diffuse = material.diffuse * light.diffuse; OUT.specular = material.specular * light.specular; return OUT; } //----------------------------------------------------------------------------- // Pixel Shaders. //----------------------------------------------------------------------------- float4 PS_DirLighting(VS_OUTPUT_DIR IN) : COLOR { float3 n = normalize(IN.normal); float3 h = normalize(IN.halfVector); float3 l = normalize(IN.lightDir); float nDotL = saturate(dot(n, l)); float nDotH = saturate(dot(n, h)); float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, material.shininess); float4 color = (material.ambient * (globalAmbient + light.ambient)) + (IN.diffuse * nDotL) + (IN.specular * power); return color * tex2D(colorMap, IN.texCoord); } float4 PS_PointLighting(VS_OUTPUT_POINT IN) : COLOR { float atten = saturate(1.0f - dot(IN.lightDir, IN.lightDir)); float3 n = normalize(IN.normal); float3 l = normalize(IN.lightDir); float3 v = normalize(IN.viewDir); float3 h = normalize(l + v); float nDotL = saturate(dot(n, l)); float nDotH = saturate(dot(n, h)); float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, material.shininess); float4 color = (material.ambient * (globalAmbient + (atten * light.ambient))) + (IN.diffuse * nDotL * atten) + (IN.specular * power * atten); return color * tex2D(colorMap, IN.texCoord); } float4 PS_SpotLighting(VS_OUTPUT_SPOT IN) : COLOR { float atten = saturate(1.0f - dot(IN.lightDir, IN.lightDir)); float3 l = normalize(IN.lightDir); float2 cosAngles = cos(float2(light.spotOuterCone, light.spotInnerCone) * 0.5f); float spotDot = dot(-l, normalize(IN.spotDir)); float spotEffect = smoothstep(cosAngles[0], cosAngles[1], spotDot); atten *= spotEffect; float3 n = normalize(IN.normal); float3 v = normalize(IN.viewDir); float3 h = normalize(l + v); float nDotL = saturate(dot(n, l)); float nDotH = saturate(dot(n, h)); float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, material.shininess); float4 color = (material.ambient * (globalAmbient + (atten * light.ambient))) + (IN.diffuse * nDotL * atten) + (IN.specular * power * atten); return color * tex2D(colorMap, IN.texCoord); } //----------------------------------------------------------------------------- // Techniques. //----------------------------------------------------------------------------- technique PerPixelDirectionalLighting { pass { VertexShader = compile vs_2_0 VS_DirLighting(); PixelShader = compile ps_2_0 PS_DirLighting(); } } technique PerPixelPointLighting { pass { VertexShader = compile vs_2_0 VS_PointLighting(); PixelShader = compile ps_2_0 PS_PointLighting(); } } technique PerPixelSpotLighting { pass { VertexShader = compile vs_2_0 VS_SpotLighting(); PixelShader = compile ps_2_0 PS_SpotLighting(); } } |
Zitat
Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.
Ja, das stimmt, allerdings ist da ein Unterschied wenn ich mehrere Objekte habe?
Werbeanzeige