1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | // Copyright (c) 2004-2005 Lutz Latta |
---|
3 | // |
---|
4 | // Permission is hereby granted, free of charge, to any person obtaining |
---|
5 | // a copy of this software and associated documentation files (the "Software"), |
---|
6 | // to deal in the Software without restriction, including without limitation |
---|
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
8 | // and/or sell copies of the Software, and to permit persons to whom the |
---|
9 | // Software is furnished to do so, subject to the following conditions: |
---|
10 | // |
---|
11 | // The above copyright notice and this permission notice shall be included |
---|
12 | // in all copies or substantial portions of the Software. |
---|
13 | // |
---|
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
20 | // DEALINGS IN THE SOFTWARE. |
---|
21 | |
---|
22 | |
---|
23 | // Supporting functions for adapting generic sorting algorithm to distance sorting. |
---|
24 | // |
---|
25 | // Note: Packing of two 16-bit halfs into one float is used. |
---|
26 | // This works only perfect with texture sizes up to 1024. |
---|
27 | // With size 2048 the higher coordinates shift from xxxx.5 to integer values, |
---|
28 | // then the algorithm should be changed to store only integers and always add 0.5. |
---|
29 | |
---|
30 | float2 initSortIndex(float2 _SortIndex : TEXCOORD0) : COLOR |
---|
31 | { |
---|
32 | return float2(0, pack_2half(_SortIndex)); |
---|
33 | } |
---|
34 | |
---|
35 | float2 computeDistance(float2 _TexCoord : TEXCOORD0, |
---|
36 | uniform samplerRECT _SortTexture : register(s0), |
---|
37 | uniform samplerRECT _PositionTexture : register(s1), |
---|
38 | uniform float3 _ViewerPosition) : COLOR |
---|
39 | { |
---|
40 | float sortIndex = texRECT(_SortTexture, _TexCoord.xy).y; |
---|
41 | half2 particleIndex = unpack_2half(sortIndex); |
---|
42 | |
---|
43 | float3 particlePos = (float3)texRECT(_PositionTexture, particleIndex); |
---|
44 | |
---|
45 | float3 delta = _ViewerPosition - particlePos; |
---|
46 | float distanceSqr = dot(delta, delta); |
---|
47 | |
---|
48 | // Prevent unused, far-away particles from destroying comparisons in sorting. |
---|
49 | if (distanceSqr > 1e6 || isnan(distanceSqr)) |
---|
50 | distanceSqr = 1e6; |
---|
51 | |
---|
52 | return float2(distanceSqr, sortIndex); |
---|
53 | } |
---|
54 | |
---|
55 | float4 lookupPosition(float2 _TexCoord : TEXCOORD0, |
---|
56 | uniform samplerRECT _SortTexture : register(s0), |
---|
57 | uniform samplerRECT _PositionTexture : register(s1)) : COLOR |
---|
58 | { |
---|
59 | float sortIndex = texRECT(_SortTexture, _TexCoord.xy).y; |
---|
60 | half2 particleIndex = unpack_2half(sortIndex); |
---|
61 | float4 particlePos = (float4)texRECT(_PositionTexture, particleIndex); |
---|
62 | |
---|
63 | return particlePos; |
---|
64 | } |
---|