mirror of
https://github.com/sotrh/learn-wgpu.git
synced 2024-11-08 07:10:33 +00:00
480 lines
144 KiB
HTML
480 lines
144 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en-US">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>Working with Lights | Learn Wgpu</title>
|
|
<meta name="description" content="">
|
|
<meta name="generator" content="VuePress 1.4.0">
|
|
|
|
<meta property="article:modified_time" content="2021-02-21T17:26:16.000Z"><meta property="og:site_name" content="Learn Wgpu"><meta property="og:title" content="Working with Lights"><meta property="og:type" content="website"><meta property="og:url" content="/intermediate/tutorial10-lighting/"><meta name="twitter:title" content="Working with Lights"><meta name="twitter:url" content="/intermediate/tutorial10-lighting/"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:label1" content="Written by"><meta name="twitter:data2" content="Benjamin R Hansen"><meta name="twitter:creator" content="https://twitter.com/sotrh760">
|
|
<link rel="preload" href="/learn-wgpu/assets/css/0.styles.7acc6ef6.css" as="style"><link rel="preload" href="/learn-wgpu/assets/js/app.61124e6f.js" as="script"><link rel="preload" href="/learn-wgpu/assets/js/2.da3fd46f.js" as="script"><link rel="preload" href="/learn-wgpu/assets/js/3.2a393c66.js" as="script"><link rel="preload" href="/learn-wgpu/assets/js/22.2132e362.js" as="script"><link rel="prefetch" href="/learn-wgpu/assets/js/10.de8cadfb.js"><link rel="prefetch" href="/learn-wgpu/assets/js/11.a00c3d10.js"><link rel="prefetch" href="/learn-wgpu/assets/js/12.26aabcb1.js"><link rel="prefetch" href="/learn-wgpu/assets/js/13.78eedf26.js"><link rel="prefetch" href="/learn-wgpu/assets/js/14.f852c057.js"><link rel="prefetch" href="/learn-wgpu/assets/js/15.0fda50ec.js"><link rel="prefetch" href="/learn-wgpu/assets/js/16.f1c9d31a.js"><link rel="prefetch" href="/learn-wgpu/assets/js/17.8d0c6c00.js"><link rel="prefetch" href="/learn-wgpu/assets/js/18.d3691f26.js"><link rel="prefetch" href="/learn-wgpu/assets/js/19.e9350ae9.js"><link rel="prefetch" href="/learn-wgpu/assets/js/20.70e640cc.js"><link rel="prefetch" href="/learn-wgpu/assets/js/21.19d2890c.js"><link rel="prefetch" href="/learn-wgpu/assets/js/23.1a88f225.js"><link rel="prefetch" href="/learn-wgpu/assets/js/24.6da05868.js"><link rel="prefetch" href="/learn-wgpu/assets/js/25.2c8435d6.js"><link rel="prefetch" href="/learn-wgpu/assets/js/26.5b0dad74.js"><link rel="prefetch" href="/learn-wgpu/assets/js/27.04f788d9.js"><link rel="prefetch" href="/learn-wgpu/assets/js/28.a3ac06e2.js"><link rel="prefetch" href="/learn-wgpu/assets/js/29.281013fa.js"><link rel="prefetch" href="/learn-wgpu/assets/js/30.dc887be9.js"><link rel="prefetch" href="/learn-wgpu/assets/js/4.65144eb4.js"><link rel="prefetch" href="/learn-wgpu/assets/js/5.1cb681ac.js"><link rel="prefetch" href="/learn-wgpu/assets/js/6.e779eca3.js"><link rel="prefetch" href="/learn-wgpu/assets/js/7.88810c81.js"><link rel="prefetch" href="/learn-wgpu/assets/js/8.d5139107.js"><link rel="prefetch" href="/learn-wgpu/assets/js/9.ecb6e79c.js">
|
|
<link rel="stylesheet" href="/learn-wgpu/assets/css/0.styles.7acc6ef6.css">
|
|
</head>
|
|
<body>
|
|
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="inner"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/learn-wgpu/" class="home-link router-link-active"><!----> <span class="site-name">Learn Wgpu</span></a> <div class="links"><!----> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div></div></header> <div class="sidebar-mask"></div> <div class="docs-layout"><aside class="sidebar"><!----> <ul class="sidebar-links"><li><a href="/learn-wgpu/" class="sidebar-link">Introduction</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Beginner</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/learn-wgpu/beginner/tutorial1-window/" class="sidebar-link">Dependencies and the window</a></li><li><a href="/learn-wgpu/beginner/tutorial2-swapchain/" class="sidebar-link">The Swapchain</a></li><li><a href="/learn-wgpu/beginner/tutorial3-pipeline/" class="sidebar-link">The Pipeline</a></li><li><a href="/learn-wgpu/beginner/tutorial4-buffer/" class="sidebar-link">Buffers and Indices</a></li><li><a href="/learn-wgpu/beginner/tutorial5-textures/" class="sidebar-link">Textures and bind groups</a></li><li><a href="/learn-wgpu/beginner/tutorial6-uniforms/" class="sidebar-link">Uniform buffers and a 3d camera</a></li><li><a href="/learn-wgpu/beginner/tutorial7-instancing/" class="sidebar-link">Instancing</a></li><li><a href="/learn-wgpu/beginner/tutorial8-depth/" class="sidebar-link">The Depth Buffer</a></li><li><a href="/learn-wgpu/beginner/tutorial9-models/" class="sidebar-link">Model Loading</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Intermediate</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/learn-wgpu/intermediate/tutorial10-lighting/" class="active sidebar-link">Working with Lights</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/learn-wgpu/intermediate/tutorial10-lighting/#ray-path-tracing" class="sidebar-link">Ray/Path Tracing</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/intermediate/tutorial10-lighting/#the-blinn-phong-model" class="sidebar-link">The Blinn-Phong Model</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/intermediate/tutorial10-lighting/#seeing-the-light" class="sidebar-link">Seeing the light</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/intermediate/tutorial10-lighting/#ambient-lighting" class="sidebar-link">Ambient Lighting</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/intermediate/tutorial10-lighting/#diffuse-lighting" class="sidebar-link">Diffuse Lighting</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/intermediate/tutorial10-lighting/#the-normal-matrix" class="sidebar-link">The normal matrix</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/intermediate/tutorial10-lighting/#specular-lighting" class="sidebar-link">Specular Lighting</a></li><li class="sidebar-sub-header"><a href="/learn-wgpu/intermediate/tutorial10-lighting/#the-half-direction" class="sidebar-link">The half direction</a></li></ul></li><li><a href="/learn-wgpu/intermediate/tutorial11-normals/" class="sidebar-link">Normal Mapping</a></li><li><a href="/learn-wgpu/intermediate/tutorial12-camera/" class="sidebar-link">A Better Camera</a></li><li><a href="/learn-wgpu/intermediate/tutorial13-threading/" class="sidebar-link">Multi-threading with Wgpu and Rayon</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Showcase</span> <span class="arrow right"></span></p> <!----></section></li><li><a href="/learn-wgpu/news/" class="sidebar-link">News</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="working-with-lights"><a href="#working-with-lights" class="header-anchor">#</a> Working with Lights</h1> <p>While we can tell that our scene is 3d because of our camera, it still feels very flat. That's because our model stays the same color regardless of how it's oriented. If we want to change that we need to add lighting to our scene.</p> <p>In the real world, a light source emits photons which bounce around until they enter into our eyes. The color we see is the light's original color minus whatever energy it lost while it was bouncing around.</p> <p>In the computer graphics world, modeling individual photons would be hilariously computationally expensive. A single 100 Watt light bulb emits about 3.27 x 10^20 photons <em>per second</em>. Just imagine that for the sun! To get around this, we're gonna use math to cheat.</p> <p>Let's discuss a few options.</p> <h2 id="ray-path-tracing"><a href="#ray-path-tracing" class="header-anchor">#</a> Ray/Path Tracing</h2> <p>This is an <em>advanced</em> topic, and we won't be covering it in depth here. It's the closest model to the way light really works so I felt I had to mention it. Check out the <a href="/learn-wgpu/todo/">ray tracing tutorial</a> if you want to learn more.</p> <h2 id="the-blinn-phong-model"><a href="#the-blinn-phong-model" class="header-anchor">#</a> The Blinn-Phong Model</h2> <p>Ray/path tracing is often too computationally expensive for most realtime applications (though that is starting to change), so a more efficient, if less accurate method based on the <a href="https://en.wikipedia.org/wiki/Phong_shading" target="_blank" rel="noopener noreferrer">Phong reflection model<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> is often used. It splits up the lighting calculation into three (3) parts: ambient lighting, diffuse lighting, and specular lighting. We're going to be learning the <a href="https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_reflection_model" target="_blank" rel="noopener noreferrer">Blinn-Phong model<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>, which cheats a bit at the specular calculation to speed things up.</p> <p>Before we can get into that though, we need to add a light to our scene.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token comment">// main.rs</span>
|
|
<span class="token attribute attr-name">#[repr(C)]</span>
|
|
<span class="token attribute attr-name">#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]</span>
|
|
<span class="token keyword">struct</span> <span class="token type-definition class-name">Light</span> <span class="token punctuation">{</span>
|
|
position<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token keyword">f32</span><span class="token punctuation">;</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
<span class="token comment">// Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here</span>
|
|
_padding<span class="token punctuation">:</span> <span class="token keyword">u32</span><span class="token punctuation">,</span>
|
|
color<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token keyword">f32</span><span class="token punctuation">;</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><p>Our <code>Light</code> represents a colored point in space. We're just going to use pure white light, but it's good to allow different colors of light.</p> <p>We're going to create another buffer to store our light in.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">let</span> light <span class="token operator">=</span> <span class="token class-name">Light</span> <span class="token punctuation">{</span>
|
|
position<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">into</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
_padding<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
|
|
color<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">into</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token comment">// We'll want to update our lights position, so we use COPY_DST</span>
|
|
<span class="token keyword">let</span> light_buffer <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_buffer_init</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span>util<span class="token punctuation">::</span></span><span class="token class-name">BufferInitDescriptor</span> <span class="token punctuation">{</span>
|
|
label<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token string">"Light VB"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
contents<span class="token punctuation">:</span> <span class="token namespace">bytemuck<span class="token punctuation">::</span></span><span class="token function">cast_slice</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token punctuation">[</span>light<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
usage<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BufferUsage</span><span class="token punctuation">::</span><span class="token constant">UNIFORM</span> <span class="token operator">|</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BufferUsage</span><span class="token punctuation">::</span><span class="token constant">COPY_DST</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>Don't forget to add the <code>light</code> and <code>light_buffer</code> to <code>State</code>. After that we need to create a bind group layout and bind group for our light.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">let</span> light_bind_group_layout <span class="token operator">=</span>
|
|
device<span class="token punctuation">.</span><span class="token function">create_bind_group_layout</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroupLayoutDescriptor</span> <span class="token punctuation">{</span>
|
|
entries<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroupLayoutEntry</span> <span class="token punctuation">{</span>
|
|
binding<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
|
|
visibility<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">ShaderStage</span><span class="token punctuation">::</span><span class="token constant">VERTEX</span> <span class="token operator">|</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">ShaderStage</span><span class="token punctuation">::</span><span class="token constant">FRAGMENT</span><span class="token punctuation">,</span>
|
|
ty<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindingType</span><span class="token punctuation">::</span><span class="token class-name">Buffer</span> <span class="token punctuation">{</span>
|
|
ty<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BufferBindingType</span><span class="token punctuation">::</span><span class="token class-name">Uniform</span><span class="token punctuation">,</span>
|
|
has_dynamic_offset<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
|
|
min_binding_size<span class="token punctuation">:</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
count<span class="token punctuation">:</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
label<span class="token punctuation">:</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">let</span> light_bind_group <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_bind_group</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroupDescriptor</span> <span class="token punctuation">{</span>
|
|
layout<span class="token punctuation">:</span> <span class="token operator">&</span>light_bind_group_layout<span class="token punctuation">,</span>
|
|
bindings<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">Binding</span> <span class="token punctuation">{</span>
|
|
binding<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
|
|
resource<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindingResource</span><span class="token punctuation">::</span><span class="token class-name">Buffer</span> <span class="token punctuation">{</span>
|
|
buffer<span class="token punctuation">:</span> <span class="token operator">&</span>light_buffer<span class="token punctuation">,</span>
|
|
range<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">..</span><span class="token namespace">std<span class="token punctuation">::</span>mem<span class="token punctuation">::</span></span><span class="token function">size_of_val</span><span class="token punctuation">(</span><span class="token operator">&</span>light<span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BufferAddress</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
label<span class="token punctuation">:</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>Add those to <code>State</code>, and also update the <code>render_pipeline_layout</code>.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">let</span> render_pipeline_layout <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_pipeline_layout</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">PipelineLayoutDescriptor</span> <span class="token punctuation">{</span>
|
|
bind_group_layouts<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token punctuation">[</span>
|
|
<span class="token operator">&</span>texture_bind_group_layout<span class="token punctuation">,</span>
|
|
<span class="token operator">&</span>uniform_bind_group_layout<span class="token punctuation">,</span>
|
|
<span class="token operator">&</span>light_bind_group_layout<span class="token punctuation">,</span>
|
|
<span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>Let's also update the lights position in the <code>update()</code> method, so we can see what our objects look like from different angles.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token comment">// Update the light</span>
|
|
<span class="token keyword">let</span> old_position<span class="token punctuation">:</span> <span class="token namespace">cgmath<span class="token punctuation">::</span></span><span class="token class-name">Vector3</span><span class="token operator"><</span>_<span class="token operator">></span> <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>light<span class="token punctuation">.</span>position<span class="token punctuation">.</span><span class="token function">into</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span>light<span class="token punctuation">.</span>position <span class="token operator">=</span>
|
|
<span class="token namespace">cgmath<span class="token punctuation">::</span></span><span class="token class-name">Quaternion</span><span class="token punctuation">::</span><span class="token function">from_axis_angle</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">into</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token namespace">cgmath<span class="token punctuation">::</span></span><span class="token class-name">Deg</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
|
|
<span class="token operator">*</span> old_position<span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span>queue<span class="token punctuation">.</span><span class="token function">write_buffer</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token keyword">self</span><span class="token punctuation">.</span>light_buffer<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token namespace">bytemuck<span class="token punctuation">::</span></span><span class="token function">cast_slice</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token punctuation">[</span><span class="token keyword">self</span><span class="token punctuation">.</span>light<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>This will have the light rotate around the origin one degree every frame.</p> <h2 id="seeing-the-light"><a href="#seeing-the-light" class="header-anchor">#</a> Seeing the light</h2> <p>For debugging purposes, it would be nice if we could see where the light is to make sure that the scene looks correct. We could adapt our existing render pipeline to draw the light, but it will likely get in the way. Instead we are going to extract our render pipeline creation code into a new function called <code>create_render_pipeline()</code>.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">create_render_pipeline</span><span class="token punctuation">(</span>
|
|
device<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">Device</span><span class="token punctuation">,</span>
|
|
layout<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">PipelineLayout</span><span class="token punctuation">,</span>
|
|
color_format<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureFormat</span><span class="token punctuation">,</span>
|
|
depth_format<span class="token punctuation">:</span> <span class="token class-name">Option</span><span class="token operator"><</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">TextureFormat</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
vertex_layouts<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">VertexBufferLayout</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
vs_src<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">ShaderModuleDescriptor</span><span class="token punctuation">,</span>
|
|
fs_src<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">ShaderModuleDescriptor</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">-></span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">RenderPipeline</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">let</span> vs_module <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_shader_module</span><span class="token punctuation">(</span><span class="token operator">&</span>vs_src<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">let</span> fs_module <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_shader_module</span><span class="token punctuation">(</span><span class="token operator">&</span>fs_src<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
|
|
device<span class="token punctuation">.</span><span class="token function">create_render_pipeline</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">RenderPipelineDescriptor</span> <span class="token punctuation">{</span>
|
|
label<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token string">"Render Pipeline"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
layout<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token operator">&</span>layout<span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
vertex<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">VertexState</span> <span class="token punctuation">{</span>
|
|
module<span class="token punctuation">:</span> <span class="token operator">&</span>vs_module<span class="token punctuation">,</span>
|
|
entry_point<span class="token punctuation">:</span> <span class="token string">"main"</span><span class="token punctuation">,</span>
|
|
buffers<span class="token punctuation">:</span> vertex_layouts<span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
fragment<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">FragmentState</span> <span class="token punctuation">{</span>
|
|
module<span class="token punctuation">:</span> <span class="token operator">&</span>fs_module<span class="token punctuation">,</span>
|
|
entry_point<span class="token punctuation">:</span> <span class="token string">"main"</span><span class="token punctuation">,</span>
|
|
targets<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">ColorTargetState</span> <span class="token punctuation">{</span>
|
|
format<span class="token punctuation">:</span> color_format<span class="token punctuation">,</span>
|
|
alpha_blend<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BlendState</span><span class="token punctuation">::</span><span class="token constant">REPLACE</span><span class="token punctuation">,</span>
|
|
color_blend<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BlendState</span><span class="token punctuation">::</span><span class="token constant">REPLACE</span><span class="token punctuation">,</span>
|
|
write_mask<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">ColorWrite</span><span class="token punctuation">::</span><span class="token constant">ALL</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
primitive<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">PrimitiveState</span> <span class="token punctuation">{</span>
|
|
topology<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">PrimitiveTopology</span><span class="token punctuation">::</span><span class="token class-name">TriangleList</span><span class="token punctuation">,</span>
|
|
strip_index_format<span class="token punctuation">:</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
|
|
front_face<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">FrontFace</span><span class="token punctuation">::</span><span class="token class-name">Ccw</span><span class="token punctuation">,</span>
|
|
cull_mode<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">CullMode</span><span class="token punctuation">::</span><span class="token class-name">Back</span><span class="token punctuation">,</span>
|
|
<span class="token comment">// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE</span>
|
|
polygon_mode<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">PolygonMode</span><span class="token punctuation">::</span><span class="token class-name">Fill</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
depth_stencil<span class="token punctuation">:</span> depth_format<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token closure-params"><span class="token closure-punctuation punctuation">|</span>format<span class="token closure-punctuation punctuation">|</span></span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">DepthStencilState</span> <span class="token punctuation">{</span>
|
|
format<span class="token punctuation">,</span>
|
|
depth_write_enabled<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
|
|
depth_compare<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">CompareFunction</span><span class="token punctuation">::</span><span class="token class-name">Less</span><span class="token punctuation">,</span>
|
|
stencil<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">StencilState</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
bias<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">DepthBiasState</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token comment">// Setting this to true requires Features::DEPTH_CLAMPING</span>
|
|
clamp_depth<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
multisample<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">MultisampleState</span> <span class="token punctuation">{</span>
|
|
count<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
|
|
mask<span class="token punctuation">:</span> <span class="token operator">!</span><span class="token number">0</span><span class="token punctuation">,</span>
|
|
alpha_to_coverage_enabled<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><p>We also need to change <code>State::new()</code> to use this function.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">let</span> render_pipeline <span class="token operator">=</span> <span class="token function">create_render_pipeline</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span>device<span class="token punctuation">,</span>
|
|
<span class="token operator">&</span>render_pipeline_layout<span class="token punctuation">,</span>
|
|
sc_desc<span class="token punctuation">.</span>format<span class="token punctuation">,</span>
|
|
<span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token namespace">texture<span class="token punctuation">::</span></span><span class="token class-name">Texture</span><span class="token punctuation">::</span><span class="token constant">DEPTH_FORMAT</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token operator">&</span><span class="token punctuation">[</span><span class="token namespace">model<span class="token punctuation">::</span></span><span class="token class-name">ModelVertex</span><span class="token punctuation">::</span><span class="token function">desc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">InstanceRaw</span><span class="token punctuation">::</span><span class="token function">desc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
<span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token macro property">include_spirv!</span><span class="token punctuation">(</span><span class="token string">"shader.vert.spv"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token macro property">include_spirv!</span><span class="token punctuation">(</span><span class="token string">"shader.frag.spv"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>We're going to need to modify <code>model::DrawModel</code> to use our <code>light_bind_group</code>.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">pub</span> <span class="token keyword">trait</span> <span class="token class-name">DrawModel</span><span class="token operator"><</span><span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span> <span class="token lifetime-annotation symbol">'b</span><span class="token operator">></span>
|
|
<span class="token keyword">where</span>
|
|
<span class="token lifetime-annotation symbol">'b</span><span class="token punctuation">:</span> <span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">{</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_mesh</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
mesh<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Mesh</span><span class="token punctuation">,</span>
|
|
material<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Material</span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_mesh_instanced</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
mesh<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Mesh</span><span class="token punctuation">,</span>
|
|
material<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Material</span><span class="token punctuation">,</span>
|
|
instances<span class="token punctuation">:</span> <span class="token class-name">Range</span><span class="token operator"><</span><span class="token keyword">u32</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_model</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
model<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Model</span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_model_instanced</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
model<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Model</span><span class="token punctuation">,</span>
|
|
instances<span class="token punctuation">:</span> <span class="token class-name">Range</span><span class="token operator"><</span><span class="token keyword">u32</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">impl</span><span class="token operator"><</span><span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span> <span class="token lifetime-annotation symbol">'b</span><span class="token operator">></span> <span class="token class-name">DrawModel</span><span class="token operator"><</span><span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span> <span class="token lifetime-annotation symbol">'b</span><span class="token operator">></span> <span class="token keyword">for</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">RenderPass</span><span class="token operator"><</span><span class="token lifetime-annotation symbol">'a</span><span class="token operator">></span>
|
|
<span class="token keyword">where</span>
|
|
<span class="token lifetime-annotation symbol">'b</span><span class="token punctuation">:</span> <span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">{</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_mesh</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
mesh<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Mesh</span><span class="token punctuation">,</span>
|
|
material<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Material</span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">draw_mesh_instanced</span><span class="token punctuation">(</span>mesh<span class="token punctuation">,</span> material<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">..</span><span class="token number">1</span><span class="token punctuation">,</span> uniforms<span class="token punctuation">,</span> light<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_mesh_instanced</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
mesh<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Mesh</span><span class="token punctuation">,</span>
|
|
material<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Material</span><span class="token punctuation">,</span>
|
|
instances<span class="token punctuation">:</span> <span class="token class-name">Range</span><span class="token operator"><</span><span class="token keyword">u32</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_vertex_buffer</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">&</span>mesh<span class="token punctuation">.</span>vertex_buffer<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_index_buffer</span><span class="token punctuation">(</span><span class="token operator">&</span>mesh<span class="token punctuation">.</span>index_buffer<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_bind_group</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">&</span>material<span class="token punctuation">.</span>bind_group<span class="token punctuation">,</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_bind_group</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">&</span>uniforms<span class="token punctuation">,</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_bind_group</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">&</span>light<span class="token punctuation">,</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">draw_indexed</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">..</span>mesh<span class="token punctuation">.</span>num_elements<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> instances<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_model</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
model<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Model</span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">draw_model_instanced</span><span class="token punctuation">(</span>model<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">..</span><span class="token number">1</span><span class="token punctuation">,</span> uniforms<span class="token punctuation">,</span> light<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_model_instanced</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
model<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Model</span><span class="token punctuation">,</span>
|
|
instances<span class="token punctuation">:</span> <span class="token class-name">Range</span><span class="token operator"><</span><span class="token keyword">u32</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">for</span> mesh <span class="token keyword">in</span> <span class="token operator">&</span>model<span class="token punctuation">.</span>meshes <span class="token punctuation">{</span>
|
|
<span class="token keyword">let</span> material <span class="token operator">=</span> <span class="token operator">&</span>model<span class="token punctuation">.</span>materials<span class="token punctuation">[</span>mesh<span class="token punctuation">.</span>material<span class="token punctuation">]</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">draw_mesh_instanced</span><span class="token punctuation">(</span>mesh<span class="token punctuation">,</span> material<span class="token punctuation">,</span> instances<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> uniforms<span class="token punctuation">,</span> light<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><p>With that done we can create another render pipeline for our light.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">let</span> light_render_pipeline <span class="token operator">=</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">let</span> layout <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_pipeline_layout</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">PipelineLayoutDescriptor</span> <span class="token punctuation">{</span>
|
|
label<span class="token punctuation">:</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token string">"Light Pipeline Layout"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
bind_group_layouts<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token operator">&</span>uniform_bind_group_layout<span class="token punctuation">,</span> <span class="token operator">&</span>light_bind_group_layout<span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
push_constant_ranges<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token function">create_render_pipeline</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span>device<span class="token punctuation">,</span>
|
|
<span class="token operator">&</span>layout<span class="token punctuation">,</span>
|
|
sc_desc<span class="token punctuation">.</span>format<span class="token punctuation">,</span>
|
|
<span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token namespace">texture<span class="token punctuation">::</span></span><span class="token class-name">Texture</span><span class="token punctuation">::</span><span class="token constant">DEPTH_FORMAT</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token operator">&</span><span class="token punctuation">[</span><span class="token namespace">model<span class="token punctuation">::</span></span><span class="token class-name">ModelVertex</span><span class="token punctuation">::</span><span class="token function">desc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
<span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token macro property">include_spirv!</span><span class="token punctuation">(</span><span class="token string">"light.vert.spv"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token macro property">include_spirv!</span><span class="token punctuation">(</span><span class="token string">"light.frag.spv"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>I chose to create a seperate layout for the <code>light_render_pipeline</code>, as it doesn't need all the resources that the regular <code>render_pipeline</code> needs (main just the textures).</p> <p>With that in place we need to write the actual shaders.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token comment">// light.vert</span>
|
|
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">version</span> <span class="token expression"><span class="token number">450</span></span></span>
|
|
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token keyword">vec3</span> a_position<span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">out</span> <span class="token keyword">vec3</span> v_color<span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>set<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> binding<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">uniform</span> Uniforms <span class="token punctuation">{</span>
|
|
<span class="token keyword">mat4</span> u_view_proj<span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>set<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> binding<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">uniform</span> Light <span class="token punctuation">{</span>
|
|
<span class="token keyword">vec3</span> u_position<span class="token punctuation">;</span>
|
|
<span class="token keyword">vec3</span> u_color<span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token comment">// Let's keep our light smaller than our other objects</span>
|
|
<span class="token keyword">float</span> scale <span class="token operator">=</span> <span class="token number">0.25</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">vec3</span> v_position <span class="token operator">=</span> a_position <span class="token operator">*</span> scale <span class="token operator">+</span> u_position<span class="token punctuation">;</span>
|
|
gl_Position <span class="token operator">=</span> u_view_proj <span class="token operator">*</span> <span class="token keyword">vec4</span><span class="token punctuation">(</span>v_position<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
|
|
v_color <span class="token operator">=</span> u_color<span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token comment">// light.frag</span>
|
|
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">version</span> <span class="token expression"><span class="token number">450</span></span></span>
|
|
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token keyword">vec3</span> v_color<span class="token punctuation">;</span>
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">out</span> <span class="token keyword">vec4</span> f_color<span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
f_color <span class="token operator">=</span> <span class="token keyword">vec4</span><span class="token punctuation">(</span>v_color<span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><p>Now we could manually implement the draw code for the light in <code>render()</code>, but to keep with the pattern we developed, let's create a new trait called <code>DrawLight</code>.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">pub</span> <span class="token keyword">trait</span> <span class="token class-name">DrawLight</span><span class="token operator"><</span><span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span> <span class="token lifetime-annotation symbol">'b</span><span class="token operator">></span>
|
|
<span class="token keyword">where</span>
|
|
<span class="token lifetime-annotation symbol">'b</span><span class="token punctuation">:</span> <span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">{</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_light_mesh</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
mesh<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Mesh</span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_light_mesh_instanced</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
mesh<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Mesh</span><span class="token punctuation">,</span>
|
|
instances<span class="token punctuation">:</span> <span class="token class-name">Range</span><span class="token operator"><</span><span class="token keyword">u32</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token keyword">where</span>
|
|
<span class="token lifetime-annotation symbol">'b</span><span class="token punctuation">:</span> <span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_light_model</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
model<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Model</span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_light_model_instanced</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
model<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Model</span><span class="token punctuation">,</span>
|
|
instances<span class="token punctuation">:</span> <span class="token class-name">Range</span><span class="token operator"><</span><span class="token keyword">u32</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">impl</span><span class="token operator"><</span><span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span> <span class="token lifetime-annotation symbol">'b</span><span class="token operator">></span> <span class="token class-name">DrawLight</span><span class="token operator"><</span><span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span> <span class="token lifetime-annotation symbol">'b</span><span class="token operator">></span> <span class="token keyword">for</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">RenderPass</span><span class="token operator"><</span><span class="token lifetime-annotation symbol">'a</span><span class="token operator">></span>
|
|
<span class="token keyword">where</span>
|
|
<span class="token lifetime-annotation symbol">'b</span><span class="token punctuation">:</span> <span class="token lifetime-annotation symbol">'a</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">{</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_light_mesh</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
mesh<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Mesh</span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">draw_light_mesh_instanced</span><span class="token punctuation">(</span>mesh<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">..</span><span class="token number">1</span><span class="token punctuation">,</span> uniforms<span class="token punctuation">,</span> light<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_light_mesh_instanced</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
mesh<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Mesh</span><span class="token punctuation">,</span>
|
|
instances<span class="token punctuation">:</span> <span class="token class-name">Range</span><span class="token operator"><</span><span class="token keyword">u32</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_vertex_buffer</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> mesh<span class="token punctuation">.</span>vertex_buffer<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token punctuation">..</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_index_buffer</span><span class="token punctuation">(</span>mesh<span class="token punctuation">.</span>index_buffer<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token punctuation">..</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">IndexFormat</span><span class="token punctuation">::</span><span class="token class-name">Uint32</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_bind_group</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> uniforms<span class="token punctuation">,</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_bind_group</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> light<span class="token punctuation">,</span> <span class="token operator">&</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">draw_indexed</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">..</span>mesh<span class="token punctuation">.</span>num_elements<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> instances<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_light_model</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
model<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Model</span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">draw_light_model_instanced</span><span class="token punctuation">(</span>model<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">..</span><span class="token number">1</span><span class="token punctuation">,</span> uniforms<span class="token punctuation">,</span> light<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">draw_light_model_instanced</span><span class="token punctuation">(</span>
|
|
<span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span>
|
|
model<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token class-name">Model</span><span class="token punctuation">,</span>
|
|
instances<span class="token punctuation">:</span> <span class="token class-name">Range</span><span class="token operator"><</span><span class="token keyword">u32</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
uniforms<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
light<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token lifetime-annotation symbol">'b</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroup</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">for</span> mesh <span class="token keyword">in</span> <span class="token operator">&</span>model<span class="token punctuation">.</span>meshes <span class="token punctuation">{</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">draw_light_mesh_instanced</span><span class="token punctuation">(</span>mesh<span class="token punctuation">,</span> instances<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> uniforms<span class="token punctuation">,</span> light<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><p>With all that we'll end up with something like this.</p> <p><img src="/learn-wgpu/assets/img/light-in-scene.7c329d72.png" alt="./light-in-scene.png"></p> <h2 id="ambient-lighting"><a href="#ambient-lighting" class="header-anchor">#</a> Ambient Lighting</h2> <p>Light has a tendency to bounce around before entering our eyes. That's why you can see in areas that are in shadow. Actually modeling this interaction is computationally expensive, so we cheat. We define an ambient lighting value that stands in for the light bouncing of other parts of the scene to light our objects.</p> <p>The ambient part is based on the light color as well as the object color. We've already added our <code>light_bind_group</code>, so we just need to use it in our shader. In <code>shader.frag</code>, add the following below the texture uniforms.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">layout</span><span class="token punctuation">(</span>set <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">,</span> binding <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">uniform</span> Light <span class="token punctuation">{</span>
|
|
<span class="token keyword">vec3</span> light_position<span class="token punctuation">;</span>
|
|
<span class="token keyword">vec3</span> light_color<span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>Then we need to update our main shader code to calculate and use the ambient color value.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">vec4</span> object_color <span class="token operator">=</span> <span class="token function">texture</span><span class="token punctuation">(</span><span class="token keyword">sampler2D</span><span class="token punctuation">(</span>t_diffuse<span class="token punctuation">,</span> s_diffuse<span class="token punctuation">)</span><span class="token punctuation">,</span> v_tex_coords<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token comment">// We don't need (or want) much ambient light, so 0.1 is fine</span>
|
|
<span class="token keyword">float</span> ambient_strength <span class="token operator">=</span> <span class="token number">0.1</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">vec3</span> ambient_color <span class="token operator">=</span> light_color <span class="token operator">*</span> ambient_strength<span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">vec3</span> result <span class="token operator">=</span> ambient_color <span class="token operator">*</span> object_color<span class="token punctuation">.</span>xyz<span class="token punctuation">;</span>
|
|
|
|
<span class="token comment">// Since lights don't typically (afaik) cast transparency, so we use</span>
|
|
<span class="token comment">// the alpha here at the end.</span>
|
|
f_color <span class="token operator">=</span> <span class="token keyword">vec4</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> object_color<span class="token punctuation">.</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><p>With that we should get something like the this.</p> <p><img src="/learn-wgpu/assets/img/ambient_lighting.8bece8d1.png" alt="./ambient_lighting.png"></p> <h2 id="diffuse-lighting"><a href="#diffuse-lighting" class="header-anchor">#</a> Diffuse Lighting</h2> <p>Remember the normal vectors that were included with our model? We're finally going to use them. Normals represent the direction a surface is facing. By comparing the normal of a fragment with a vector pointing to a light source, we get a value of how light/dark that fragment should be. We compare the vector be using the dot product to get the cosine of the angle between them.</p> <p><img src="/learn-wgpu/assets/img/normal_diagram.dfa577f7.png" alt="./normal_diagram.png"></p> <p>If the dot product of the normal and light vector is 1.0, that means that the current fragment is directly inline with the light source and will receive the lights full intensity. A value of 0.0 or lower means that the surface is perpendicular or facing away from the light, and therefore will be dark.</p> <p>We're going to need to pull in the normal vector into our <code>shader.vert</code>.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token keyword">vec3</span> a_position<span class="token punctuation">;</span>
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token keyword">vec2</span> a_tex_coords<span class="token punctuation">;</span>
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token keyword">vec3</span> a_normal<span class="token punctuation">;</span> <span class="token comment">// NEW!</span>
|
|
</code></pre></div><p>We're also going to want to pass that value, as well as the vertex's position to the fragment shader.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">out</span> <span class="token keyword">vec3</span> v_normal<span class="token punctuation">;</span>
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">out</span> <span class="token keyword">vec3</span> v_position<span class="token punctuation">;</span>
|
|
</code></pre></div><p>For now let's just pass the normal directly as is. This is wrong, but we'll fix it later.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
v_tex_coords <span class="token operator">=</span> a_tex_coords<span class="token punctuation">;</span> v_normal <span class="token operator">=</span> a_normal<span class="token punctuation">;</span> <span class="token comment">// NEW!</span>
|
|
<span class="token keyword">vec4</span> model_space <span class="token operator">=</span> model_matrix <span class="token operator">*</span> <span class="token keyword">vec4</span><span class="token punctuation">(</span>a_position<span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// NEW!</span>
|
|
v_position <span class="token operator">=</span> model_space<span class="token punctuation">.</span>xyz<span class="token punctuation">;</span> <span class="token comment">// NEW!</span>
|
|
gl_Position <span class="token operator">=</span> u_view_proj <span class="token operator">*</span> model_space<span class="token punctuation">;</span> <span class="token comment">// UPDATED!</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><p>Now in <code>shader.frag</code> we'll take in the vertex's normal and position.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token keyword">vec2</span> v_tex_coords<span class="token punctuation">;</span>
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token keyword">vec3</span> v_normal<span class="token punctuation">;</span> <span class="token comment">// NEW!</span>
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>location<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token keyword">vec3</span> v_position<span class="token punctuation">;</span> <span class="token comment">// NEW!</span>
|
|
</code></pre></div><p>With that we can do the actual calculation. Below the <code>ambient_color</code> calculation, but above <code>result</code>, add the following.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">vec3</span> normal <span class="token operator">=</span> <span class="token function">normalize</span><span class="token punctuation">(</span>v_normal<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">vec3</span> light_dir <span class="token operator">=</span> <span class="token function">normalize</span><span class="token punctuation">(</span>light_position <span class="token operator">-</span> v_position<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">float</span> diffuse_strength <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span><span class="token function">dot</span><span class="token punctuation">(</span>normal<span class="token punctuation">,</span> light_dir<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">vec3</span> diffuse_color <span class="token operator">=</span> light_color <span class="token operator">*</span> diffuse_strength<span class="token punctuation">;</span>
|
|
</code></pre></div><p>Now we can include the <code>diffuse_color</code> in the <code>result</code>.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">vec3</span> result <span class="token operator">=</span> <span class="token punctuation">(</span>ambient_color <span class="token operator">+</span> diffuse_color<span class="token punctuation">)</span> <span class="token operator">*</span> object_color<span class="token punctuation">.</span>xyz<span class="token punctuation">;</span>
|
|
</code></pre></div><p>With that we get something like this.</p> <p><img src="/learn-wgpu/assets/img/ambient_diffuse_wrong.dab00d9a.png" alt="./ambient_diffuse_wrong.png"></p> <h2 id="the-normal-matrix"><a href="#the-normal-matrix" class="header-anchor">#</a> The normal matrix</h2> <p>Remember when I said passing the vertex normal directly to the fragment shader was wrong? Let's explore that by removing all the cubes from the scene except one that will be rotated 180 degrees on the y-axis.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">const</span> <span class="token constant">NUM_INSTANCES_PER_ROW</span><span class="token punctuation">:</span> <span class="token keyword">u32</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token comment">// In the loop we create the instances in</span>
|
|
<span class="token keyword">let</span> rotation <span class="token operator">=</span> <span class="token namespace">cgmath<span class="token punctuation">::</span></span><span class="token class-name">Quaternion</span><span class="token punctuation">::</span><span class="token function">from_axis_angle</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">into</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token namespace">cgmath<span class="token punctuation">::</span></span><span class="token class-name">Deg</span><span class="token punctuation">(</span><span class="token number">180.0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>We'll also remove the <code>ambient_color</code> from our lighting <code>result</code>.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">vec3</span> result <span class="token operator">=</span> <span class="token punctuation">(</span>diffuse_color<span class="token punctuation">)</span> <span class="token operator">*</span> object_color<span class="token punctuation">.</span>xyz<span class="token punctuation">;</span>
|
|
</code></pre></div><p>That should give us something that looks like this.</p> <p><img src="/learn-wgpu/assets/img/diffuse_wrong.ce856a2c.png" alt="./diffuse_wrong.png"></p> <p>This is clearly wrong as the light is illuminating the wrong side of the cube. This is because we aren't rotating our normals with our object, so no matter what direction the object faces, the normals will always face the same way.</p> <p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAABhWlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV/TSkUqClYQcchQnSyIiuimVShChVIrtOpgcukXNGlIUlwcBdeCgx+LVQcXZ10dXAVB8APEydFJ0UVK/F9SaBHjwXE/3t173L0DhHqZqWZgDFA1y0jFY2ImuyoGXxFAL/oRxYzETH0umUzAc3zdw8fXuyjP8j735+hWciYDfCLxLNMNi3iDeGrT0jnvE4dZUVKIz4lHDbog8SPXZZffOBccFnhm2Ein5onDxGKhjeU2ZkVDJZ4kji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AAAECACAAAEAECAAAAIEAAACBABAgAAACBAAAAECACBAAAAgQAAABAgAgAABABAgAAACBAAAAgQAQIAAAAgQAAABAgAgQAAAIEAAAAQIAIAAAQAQIAAAECAAAAIEAECAAAAIEAAAAQIAAAECACBAAACe8f8Iph1tvtY2iAAAAABJRU5ErkJggg==" alt="./normal_not_rotated.png"></p> <p>We need to use the model matrix to transform the normals to be in the right direction. We only want the rotation data though. A normal represents a direction, and should be a unit vector throughout the calculation. We can get our normals into the right direction using what is called a normal matrix. We can calculate the normal matrix with the following.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token comment">// shader.vert</span>
|
|
<span class="token keyword">mat4</span> model_matrix <span class="token operator">=</span> model_matrix<span class="token punctuation">;</span>
|
|
<span class="token keyword">mat3</span> normal_matrix <span class="token operator">=</span> <span class="token keyword">mat3</span><span class="token punctuation">(</span><span class="token function">transpose</span><span class="token punctuation">(</span><span class="token function">inverse</span><span class="token punctuation">(</span>model_matrix<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
v_normal <span class="token operator">=</span> normal_matrix <span class="token operator">*</span> a_normal<span class="token punctuation">;</span>
|
|
</code></pre></div><p>This takes the <code>model_matrix</code> from our <code>instance_buffer</code>, inverts it, transposes it and then pulls out the top left 3x3 to just get the rotation data. This is all necessary because because normals are technically not vectors, there bivectors. The explanation is beyond me, but I do know that it means we have to treat them differently.</p> <ul><li>Note: I'm currently doing things in <a href="https://gamedev.stackexchange.com/questions/65783/what-are-world-space-and-eye-space-in-game-development" target="_blank" rel="noopener noreferrer">world space<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>. Doing things in view-space also known as eye-space, is more standard as objects can have lighting issues when they are further away from the origin. If we wanted to use view-space, we would use something along the lines of <code>mat3(transpose(inverse(view_matrix * model_matrix)))</code>. Currently we are combining the view matrix and projection matrix before we draw, so we'd have to pass those in separately. We'd also have to transform our light's position using something like <code>view_matrix * model_matrix * light_position</code> to keep the calculation from getting messed up when the camera moves.</li> <li>Another Note: I'm calculating the <code>normal_matrix</code> in the vertex shader currently. This is rather expensive, so it is often suggested that you compute the <code>normal_matrix</code> on the CPU and pass it in with the other uniforms.</li></ul> <p>With that change our lighting now looks correct.</p> <p><img src="/learn-wgpu/assets/img/diffuse_right.e3a6b793.png" alt="./diffuse_right.png"></p> <p>Bringing back our other objects, and adding the ambient lighting gives us this.</p> <p><img src="/learn-wgpu/assets/img/ambient_diffuse_lighting.98735034.png" alt="./ambient_diffuse_lighting.png">;</p> <h2 id="specular-lighting"><a href="#specular-lighting" class="header-anchor">#</a> Specular Lighting</h2> <p>Specular lighting describes the highlights that appear on objects when viewed from certain angles. If you've ever looked at a car, it's the super bright parts. Basically, some of the light can reflect of the surface like a mirror. The location of the hightlight shifts depending on what angle you view it at.</p> <p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV/TSkUqgnYQcchQnSyIVnHUKhShQqgVWnUwufQLmjQkKS6OgmvBwY/FqoOLs64OroIg+AHi5Oik6CIl/q8ptIjx4Lgf7+497t4BQr3MNCswDmi6baYScTGTXRWDrwigHwKmEJOZZcxJUhKe4+sePr7eRXmW97k/R6+asxjgE4lnmWHaxBvE05u2wXmfOMyKskp8Tjxm0gWJH7muuPzGudBkgWeGzXRqnjhMLBY6WOlgVjQ14hhxRNV0yhcyLquctzhr5Spr3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3iV8mto1VS9RNtPDBeTxX9RKvd8SwXAQFCHxEFst+ReAAt4l1YsLSgB+bVQZ5PPBDFmIGA2UhrsxHiATADAbORds9GVge/LMQDzECAaJuJMOsAmIGAmQjxAAgIYPqBnHiAgABEhHgABARExBP22yceICAAEWHWARAQILyDPfEA+DTesPDcxHNg0n8ZSdLu/wT3Nt/up3rkOZVn0yocj/MchO255TgQwhGNOhqR7qfy50JIcBibsIhHVGpvCLqf6iEe/I2CgADtjMgYniuAgLBmg/ZGZIzUna28/K2iRexEBxE5tYWd64fDQTsAAgK0azZCOIAjYhNWCPDuDoC/VQICAAABYc0GAH+j4cQ+kDC8QHmnB0A4CAh4wQJAK9iEBQAgIAAAAgIAICAAAAICAAABAQAQEAAAAQEAEBAAAAEBAICAAAAICMNszhAAAAIZSURBVACAgAAACAgAgIAAAEBAAAAEBABAQAAABAQAQEAAACAgAAACAgAgIAAAAgIAICAAABAQAAABAQAQEAAAAQEAEBAAAAgIAICAAAAICACAgAAACAgAAAQEAEBAAAAEBABAQAAAICAAAAICACAgAAACAgAgIAAAEBAAAAEBABAQAAABAQAQEAAACAgAgIAAAAgIAICAAAAICAAABAQAQEAAAAQEAEBAAAAEBAAAAgIAICAAAAICACAgAAACAgAAAQEAEBAAAAEBABAQAAABAQCAgAAACAgAgIAAAAgIAICA8BQAAAgIAICAAAAICACAgAAAQEAAAAQEAEBAAAAEBABAQAAAICAAAAICACAgAAACAgAgIAAAEBAAAAEBABAQAAABAQAQEAAACAgAgIAAAAgIAICAAAAICAAABAQAQEAAAAQEAEBAAAAEBAAAAgIAICAAAAICACAgAAACAgAAAQEAEBAAAAEBABAQAAAICACAgAAACAgAgIAAAAgIAAAEBABAQAAABAQAQEAAAAQEAAACAgAgIAAAAgIAICAAAAICAAABAQAQEAAAAQEAEBAAAAEBAICAAAAICACAgAAACAgAgIAAAEBAAAAEBABAQAAABAQAQEAAACAgAAACAgAgIAAAAgIAICAAABAQAAABAQAQEAAAAQEAgIAAAAgIAICAAAAICACAgAAAQEAAAAQEAEBAAAAW8v8BQR2HRzqeBAEAAAAASUVORK5CYII=" alt="./specular_diagram.png"></p> <p>Because this is relative to the view angle, we are going to need to pass in the camera's position both into the fragment shader and into the vertex shader.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token comment">// shader.frag</span>
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>set<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> binding<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">uniform</span> Uniforms <span class="token punctuation">{</span>
|
|
<span class="token keyword">vec3</span> u_view_position<span class="token punctuation">;</span>
|
|
<span class="token keyword">mat4</span> u_view_proj<span class="token punctuation">;</span> <span class="token comment">// unused</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">;</span>
|
|
</code></pre></div><div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token comment">// shader.vert & light.vert</span>
|
|
<span class="token keyword">layout</span><span class="token punctuation">(</span>set<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> binding<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">uniform</span> Uniforms <span class="token punctuation">{</span>
|
|
<span class="token keyword">vec3</span> u_view_position<span class="token punctuation">;</span> <span class="token comment">// unused</span>
|
|
<span class="token keyword">mat4</span> u_view_proj<span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>We're going to need to update the <code>Uniforms</code> struct as well.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token comment">// main.rs</span>
|
|
<span class="token attribute attr-name">#[repr(C)]</span>
|
|
<span class="token attribute attr-name">#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]</span>
|
|
<span class="token keyword">struct</span> <span class="token type-definition class-name">Uniforms</span> <span class="token punctuation">{</span>
|
|
view_position<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token keyword">f32</span><span class="token punctuation">;</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
view_proj<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token keyword">f32</span><span class="token punctuation">;</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">impl</span> <span class="token class-name">Uniforms</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-></span> <span class="token keyword">Self</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">Self</span> <span class="token punctuation">{</span>
|
|
view_position<span class="token punctuation">:</span> <span class="token class-name">Zero</span><span class="token punctuation">::</span><span class="token function">zero</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
view_proj<span class="token punctuation">:</span> <span class="token namespace">cgmath<span class="token punctuation">::</span></span><span class="token class-name">Matrix4</span><span class="token punctuation">::</span><span class="token function">identity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">fn</span> <span class="token function-definition function">update_view_proj</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> camera<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token class-name">Camera</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token comment">// We don't specifically need homogeneous coordinates since we're just using</span>
|
|
<span class="token comment">// a vec3 in the shader. We're using Point3 for the camera.eye, and this is</span>
|
|
<span class="token comment">// the easiest way to convert to Vector4. We're using Vector4 because of</span>
|
|
<span class="token comment">// the uniforms 16 byte spacing requirement</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span>view_position <span class="token operator">=</span> camera<span class="token punctuation">.</span>eye<span class="token punctuation">.</span><span class="token function">to_homogeneous</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">self</span><span class="token punctuation">.</span>view_proj <span class="token operator">=</span> <span class="token constant">OPENGL_TO_WGPU_MATRIX</span> <span class="token operator">*</span> camera<span class="token punctuation">.</span><span class="token function">build_view_projection_matrix</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre></div><p>Since we want to use our uniforms in the fragment shader now, we need to change it's visibility.</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token comment">// main.rs</span>
|
|
<span class="token keyword">let</span> uniform_bind_group_layout <span class="token operator">=</span> device<span class="token punctuation">.</span><span class="token function">create_bind_group_layout</span><span class="token punctuation">(</span><span class="token operator">&</span><span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroupLayoutDescriptor</span> <span class="token punctuation">{</span>
|
|
bindings<span class="token punctuation">:</span> <span class="token operator">&</span><span class="token punctuation">[</span>
|
|
<span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindGroupLayoutBinding</span> <span class="token punctuation">{</span>
|
|
binding<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
|
|
visibility<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">ShaderStage</span><span class="token punctuation">::</span><span class="token constant">VERTEX</span> <span class="token operator">|</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">ShaderStage</span><span class="token punctuation">::</span><span class="token constant">FRAGMENT</span><span class="token punctuation">,</span> <span class="token comment">// Updated!</span>
|
|
ty<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BindingType</span><span class="token punctuation">::</span><span class="token class-name">Buffer</span> <span class="token punctuation">{</span>
|
|
ty<span class="token punctuation">:</span> <span class="token namespace">wgpu<span class="token punctuation">::</span></span><span class="token class-name">BufferBindingType</span><span class="token punctuation">::</span><span class="token class-name">Uniform</span><span class="token punctuation">,</span>
|
|
has_dynamic_offset<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token comment">// ...</span>
|
|
<span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|
label<span class="token punctuation">:</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>We're going to get the direction from the fragment's position to the camera, and use that with the normal to calculate the <code>reflect_dir</code>.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">vec3</span> view_dir <span class="token operator">=</span> <span class="token function">normalize</span><span class="token punctuation">(</span>u_view_position <span class="token operator">-</span> v_position<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">vec3</span> reflect_dir <span class="token operator">=</span> <span class="token function">reflect</span><span class="token punctuation">(</span><span class="token operator">-</span>light_dir<span class="token punctuation">,</span> normal<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>Then we use the dot product to calculate the <code>specular_strength</code> and use that to compute the <code>specular_color</code>.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">float</span> specular_strength <span class="token operator">=</span> <span class="token function">pow</span><span class="token punctuation">(</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token function">dot</span><span class="token punctuation">(</span>view_dir<span class="token punctuation">,</span> reflect_dir<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">vec3</span> specular_color <span class="token operator">=</span> specular_strength <span class="token operator">*</span> light_color<span class="token punctuation">;</span>
|
|
</code></pre></div><p>Finally we add that to the result.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">vec3</span> result <span class="token operator">=</span> <span class="token punctuation">(</span>ambient_color <span class="token operator">+</span> diffuse_color <span class="token operator">+</span> specular_color<span class="token punctuation">)</span> <span class="token operator">*</span> object_color<span class="token punctuation">.</span>xyz<span class="token punctuation">;</span>
|
|
</code></pre></div><p>With that you should have something like this.</p> <p><img src="/learn-wgpu/assets/img/ambient_diffuse_specular_lighting.76386148.png" alt="./ambient_diffuse_specular_lighting.png"></p> <p>If we just look at the <code>specular_color</code> on it's own we get this.</p> <p><img src="/learn-wgpu/assets/img/specular_lighting.b3869eb1.png" alt="./specular_lighting.png"></p> <h2 id="the-half-direction"><a href="#the-half-direction" class="header-anchor">#</a> The half direction</h2> <p>Up to this point we've actually only implemented the Phong part of Blinn-Phong. The Phong reflection model works well, but it can break down under <a href="https://learnopengl.com/Advanced-Lighting/Advanced-Lighting" target="_blank" rel="noopener noreferrer">certain circumstances<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>. The Blinn part of Blinn-Phong comes from the realization that if you add the <code>view_dir</code>, and <code>light_dir</code> together, normalize the result and use the dot product of that and the <code>normal</code>, you get roughly the same results without the issues that using <code>reflect_dir</code> had.</p> <div class="language-glsl extra-class"><pre class="language-glsl"><code><span class="token keyword">vec3</span> view_dir <span class="token operator">=</span> <span class="token function">normalize</span><span class="token punctuation">(</span>u_view_position <span class="token operator">-</span> v_position<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
<span class="token keyword">vec3</span> half_dir <span class="token operator">=</span> <span class="token function">normalize</span><span class="token punctuation">(</span>view_dir <span class="token operator">+</span> light_dir<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
|
|
<span class="token keyword">float</span> specular_strength <span class="token operator">=</span> <span class="token function">pow</span><span class="token punctuation">(</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token function">dot</span><span class="token punctuation">(</span>normal<span class="token punctuation">,</span> half_dir<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
|
</code></pre></div><p>It's hard to tell the difference, but here's the results.</p> <p><img src="/learn-wgpu/assets/img/half_dir.bbcc23b8.png" alt="./half_dir.png"></p> <div class="auto-github-link"><a href="https://github.com/sotrh/learn-wgpu/tree/master/code/intermediate/tutorial10-lighting/" target="_blank" rel="noopener noreferrer">Check out the code!</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2/21/2021, 5:26:16 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
|
|
←
|
|
<a href="/learn-wgpu/beginner/tutorial9-models/" class="prev">
|
|
Model Loading
|
|
</a></span> <span class="next"><a href="/learn-wgpu/intermediate/tutorial11-normals/">
|
|
Normal Mapping
|
|
</a>
|
|
→
|
|
</span></p></div> </main></div></div><div class="global-ui"><!----></div></div>
|
|
<script src="/learn-wgpu/assets/js/app.61124e6f.js" defer></script><script src="/learn-wgpu/assets/js/2.da3fd46f.js" defer></script><script src="/learn-wgpu/assets/js/3.2a393c66.js" defer></script><script src="/learn-wgpu/assets/js/22.2132e362.js" defer></script>
|
|
</body>
|
|
</html>
|