You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
13 KiB

<html>
<head>
<title>light.rs</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #ed864a;}
.s1 { color: #ebebeb;}
.s2 { color: #ffffff;}
.s3 { color: #ed864a; font-weight: bold;}
.s4 { color: #33ccff; font-weight: bold;}
.ln { color: #999999; font-weight: normal; font-style: normal; }
</style>
</head>
<body bgcolor="#131314">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
light.rs</font>
</center></td></tr></table>
<pre><a name="l1"><span class="ln">1 </span></a><span class="s0">use </span><span class="s2">bytemuck</span><span class="s1">::</span><span class="s2">__core</span><span class="s1">::</span><span class="s2">ops</span><span class="s1">::</span><span class="s2">Range</span><span class="s3">;</span>
<a name="l2"><span class="ln">2 </span></a><span class="s0">use </span><span class="s2">bytemuck</span><span class="s1">::{</span><span class="s2">Zeroable</span><span class="s3">, </span><span class="s2">Pod</span><span class="s1">}</span><span class="s3">;</span>
<a name="l3"><span class="ln">3 </span></a><span class="s0">use </span><span class="s2">cgmath</span><span class="s1">::</span><span class="s2">Point3</span><span class="s3">;</span>
<a name="l4"><span class="ln">4 </span></a><span class="s0">use </span><span class="s2">std</span><span class="s1">::</span><span class="s2">sync</span><span class="s1">::</span><span class="s2">Arc</span><span class="s3">;</span>
<a name="l5"><span class="ln">5 </span></a><span class="s0">use </span><span class="s2">wgpu</span><span class="s1">::</span><span class="s2">TextureView</span><span class="s3">;</span>
<a name="l6"><span class="ln">6 </span></a><span class="s0">use crate</span><span class="s1">::</span><span class="s2">components</span><span class="s1">::{</span><span class="s2">RangeCopy</span><span class="s3">, </span><span class="s2">Position</span><span class="s1">}</span><span class="s3">;</span>
<a name="l7"><span class="ln">7 </span></a><span class="s0">use crate</span><span class="s1">::</span><span class="s2">render</span><span class="s1">::</span><span class="s2">OPENGL_TO_WGPU_MATRIX</span><span class="s3">;</span>
<a name="l8"><span class="ln">8 </span></a>
<a name="l9"><span class="ln">9 </span></a>
<a name="l10"><span class="ln">10 </span></a><span class="s1">#[</span><span class="s2">repr</span><span class="s1">(</span><span class="s2">C</span><span class="s1">)]</span>
<a name="l11"><span class="ln">11 </span></a><span class="s1">#[</span><span class="s2">derive</span><span class="s1">(</span><span class="s2">Clone</span><span class="s3">, </span><span class="s2">Copy</span><span class="s1">)]</span>
<a name="l12"><span class="ln">12 </span></a><span class="s0">pub struct </span><span class="s2">LightRaw </span><span class="s1">{</span>
<a name="l13"><span class="ln">13 </span></a> <span class="s2">proj</span><span class="s1">: [[</span><span class="s2">f32</span><span class="s3">; </span><span class="s4">4</span><span class="s1">]</span><span class="s3">; </span><span class="s4">4</span><span class="s1">]</span><span class="s3">,</span>
<a name="l14"><span class="ln">14 </span></a> <span class="s2">pos</span><span class="s1">: [</span><span class="s2">f32</span><span class="s3">; </span><span class="s4">4</span><span class="s1">]</span><span class="s3">,</span>
<a name="l15"><span class="ln">15 </span></a> <span class="s2">color</span><span class="s1">: [</span><span class="s2">f32</span><span class="s3">; </span><span class="s4">4</span><span class="s1">]</span><span class="s3">,</span>
<a name="l16"><span class="ln">16 </span></a><span class="s1">}</span>
<a name="l17"><span class="ln">17 </span></a>
<a name="l18"><span class="ln">18 </span></a><span class="s0">unsafe impl </span><span class="s2">Pod </span><span class="s0">for </span><span class="s2">LightRaw </span><span class="s1">{}</span>
<a name="l19"><span class="ln">19 </span></a>
<a name="l20"><span class="ln">20 </span></a><span class="s0">unsafe impl </span><span class="s2">Zeroable </span><span class="s0">for </span><span class="s2">LightRaw </span><span class="s1">{}</span>
<a name="l21"><span class="ln">21 </span></a>
<a name="l22"><span class="ln">22 </span></a><span class="s1">#[</span><span class="s2">derive</span><span class="s1">(</span><span class="s2">Clone</span><span class="s3">, </span><span class="s2">Debug</span><span class="s1">)]</span>
<a name="l23"><span class="ln">23 </span></a><span class="s0">pub struct </span><span class="s2">DirectionalLight </span><span class="s1">{</span>
<a name="l24"><span class="ln">24 </span></a> <span class="s0">pub </span><span class="s2">color</span><span class="s1">: </span><span class="s2">wgpu</span><span class="s1">::</span><span class="s2">Color</span><span class="s3">,</span>
<a name="l25"><span class="ln">25 </span></a> <span class="s0">pub </span><span class="s2">fov</span><span class="s1">: </span><span class="s2">f32</span><span class="s3">,</span>
<a name="l26"><span class="ln">26 </span></a> <span class="s0">pub </span><span class="s2">depth</span><span class="s1">: </span><span class="s2">RangeCopy</span><span class="s1">&lt;</span><span class="s2">f32</span><span class="s1">&gt;</span><span class="s3">,</span>
<a name="l27"><span class="ln">27 </span></a> <span class="s0">pub </span><span class="s2">target_view</span><span class="s1">: </span><span class="s2">Arc</span><span class="s1">&lt;</span><span class="s2">TextureView</span><span class="s1">&gt;</span><span class="s3">,</span>
<a name="l28"><span class="ln">28 </span></a><span class="s1">}</span>
<a name="l29"><span class="ln">29 </span></a>
<a name="l30"><span class="ln">30 </span></a><span class="s0">impl </span><span class="s2">DirectionalLight </span><span class="s1">{</span>
<a name="l31"><span class="ln">31 </span></a> <span class="s0">pub fn </span><span class="s2">to_raw</span><span class="s1">(&amp;</span><span class="s0">self</span><span class="s3">, </span><span class="s2">pos</span><span class="s1">: &amp;</span><span class="s2">Position</span><span class="s1">) -&gt; </span><span class="s2">LightRaw </span><span class="s1">{</span>
<a name="l32"><span class="ln">32 </span></a> <span class="s0">use </span><span class="s2">cgmath</span><span class="s1">::{</span><span class="s2">Deg</span><span class="s3">, </span><span class="s2">EuclideanSpace</span><span class="s3">, </span><span class="s2">Matrix4</span><span class="s3">, </span><span class="s2">PerspectiveFov</span><span class="s3">, </span><span class="s2">Point3</span><span class="s3">, </span><span class="s2">Vector3</span><span class="s1">}</span><span class="s3">;</span>
<a name="l33"><span class="ln">33 </span></a>
<a name="l34"><span class="ln">34 </span></a> <span class="s0">let </span><span class="s2">point3d </span><span class="s1">= </span><span class="s2">Point3</span><span class="s1">::</span><span class="s2">new</span><span class="s1">(</span><span class="s2">pos</span><span class="s1">.</span><span class="s2">x</span><span class="s3">, </span><span class="s2">pos</span><span class="s1">.</span><span class="s2">y</span><span class="s3">, </span><span class="s2">pos</span><span class="s1">.</span><span class="s2">z</span><span class="s1">)</span><span class="s3">;</span>
<a name="l35"><span class="ln">35 </span></a> <span class="s0">let </span><span class="s2">point3d_2 </span><span class="s1">= </span><span class="s2">Point3</span><span class="s1">::</span><span class="s2">new</span><span class="s1">(</span><span class="s2">pos</span><span class="s1">.</span><span class="s2">x</span><span class="s3">, </span><span class="s2">pos</span><span class="s1">.</span><span class="s2">y </span><span class="s1">- </span><span class="s4">1</span><span class="s1">.</span><span class="s4">0</span><span class="s3">, </span><span class="s2">pos</span><span class="s1">.</span><span class="s2">z</span><span class="s1">)</span><span class="s3">;</span>
<a name="l36"><span class="ln">36 </span></a> <span class="s0">let </span><span class="s2">mx_view </span><span class="s1">= </span><span class="s2">Matrix4</span><span class="s1">::</span><span class="s2">look_at</span><span class="s1">(</span><span class="s2">point3d</span><span class="s3">, </span><span class="s2">point3d_2</span><span class="s3">, </span><span class="s2">Vector3</span><span class="s1">::</span><span class="s2">unit_z</span><span class="s1">())</span><span class="s3">;</span>
<a name="l37"><span class="ln">37 </span></a>
<a name="l38"><span class="ln">38 </span></a> <span class="s0">let </span><span class="s2">projection </span><span class="s1">= </span><span class="s2">PerspectiveFov </span><span class="s1">{</span>
<a name="l39"><span class="ln">39 </span></a> <span class="s2">fovy</span><span class="s1">: </span><span class="s2">Deg</span><span class="s1">(</span><span class="s0">self</span><span class="s1">.</span><span class="s2">fov</span><span class="s1">).</span><span class="s2">into</span><span class="s1">()</span><span class="s3">,</span>
<a name="l40"><span class="ln">40 </span></a> <span class="s2">aspect</span><span class="s1">: </span><span class="s4">1</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
<a name="l41"><span class="ln">41 </span></a> <span class="s2">near</span><span class="s1">: </span><span class="s0">self</span><span class="s1">.</span><span class="s2">depth</span><span class="s1">.</span><span class="s2">start</span><span class="s3">,</span>
<a name="l42"><span class="ln">42 </span></a> <span class="s2">far</span><span class="s1">: </span><span class="s0">self</span><span class="s1">.</span><span class="s2">depth</span><span class="s1">.</span><span class="s2">end</span><span class="s3">,</span>
<a name="l43"><span class="ln">43 </span></a> <span class="s1">}</span><span class="s3">;</span>
<a name="l44"><span class="ln">44 </span></a> <span class="s0">let </span><span class="s2">mx_correction </span><span class="s1">= </span><span class="s2">OPENGL_TO_WGPU_MATRIX</span><span class="s3">;</span>
<a name="l45"><span class="ln">45 </span></a> <span class="s0">let </span><span class="s2">mx_view_proj </span><span class="s1">=</span>
<a name="l46"><span class="ln">46 </span></a> <span class="s2">mx_correction </span><span class="s1">* </span><span class="s2">cgmath</span><span class="s1">::</span><span class="s2">Matrix4</span><span class="s1">::</span><span class="s2">from</span><span class="s1">(</span><span class="s2">projection</span><span class="s1">.</span><span class="s2">to_perspective</span><span class="s1">()) * </span><span class="s2">mx_view</span><span class="s3">;</span>
<a name="l47"><span class="ln">47 </span></a> <span class="s2">LightRaw </span><span class="s1">{</span>
<a name="l48"><span class="ln">48 </span></a> <span class="s2">proj</span><span class="s1">: *</span><span class="s2">mx_view_proj</span><span class="s1">.</span><span class="s2">as_ref</span><span class="s1">()</span><span class="s3">,</span>
<a name="l49"><span class="ln">49 </span></a> <span class="s2">pos</span><span class="s1">: [</span><span class="s2">pos</span><span class="s1">.</span><span class="s2">x</span><span class="s3">, </span><span class="s2">pos</span><span class="s1">.</span><span class="s2">y</span><span class="s3">, </span><span class="s2">pos</span><span class="s1">.</span><span class="s2">z</span><span class="s3">, </span><span class="s4">1</span><span class="s1">.</span><span class="s4">0</span><span class="s1">]</span><span class="s3">,</span>
<a name="l50"><span class="ln">50 </span></a> <span class="s2">color</span><span class="s1">: [</span>
<a name="l51"><span class="ln">51 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">color</span><span class="s1">.</span><span class="s2">r </span><span class="s0">as </span><span class="s2">f32</span><span class="s3">,</span>
<a name="l52"><span class="ln">52 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">color</span><span class="s1">.</span><span class="s2">g </span><span class="s0">as </span><span class="s2">f32</span><span class="s3">,</span>
<a name="l53"><span class="ln">53 </span></a> <span class="s0">self</span><span class="s1">.</span><span class="s2">color</span><span class="s1">.</span><span class="s2">b </span><span class="s0">as </span><span class="s2">f32</span><span class="s3">,</span>
<a name="l54"><span class="ln">54 </span></a> <span class="s4">1</span><span class="s1">.</span><span class="s4">0</span><span class="s3">,</span>
<a name="l55"><span class="ln">55 </span></a> <span class="s1">]</span><span class="s3">,</span>
<a name="l56"><span class="ln">56 </span></a> <span class="s1">}</span>
<a name="l57"><span class="ln">57 </span></a> <span class="s1">}</span>
<a name="l58"><span class="ln">58 </span></a><span class="s1">}</span></pre>
</body>
</html>