Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Last active March 31, 2022 11:53

Revisions

  1. sugi.cho revised this gist Jul 24, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,8 @@ NormalMapは、接線座標系内の法線方向なので、ノーマルマッ
    worldN.z = dot(IN.tSpace2.xyz, o.Normal);
    o.Normal = worldN;
    ```
    タンジェント空間でのノーマル計算したんやから、tangentToWorldのmatrixとかで、変換すれば良いだけな気がするけど、なざ、こうやってるのかは、まだ理解できてない
    タンジェント空間でのノーマル計算したんやから、tangentToWorldのmatrixとかで、変換すれば良いだけな気がするけど、なざ、こうやってるのかは、まだ理解できてない
    ↑tangentToWorld Matrixは、頂点ごとに異なる!!

    ##UnityCG.cginc
    - `#define UNITY_PI 3.14159265359f`
  2. sugi.cho revised this gist Jul 24, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -8,13 +8,13 @@
    NormalMapは、接線座標系内の法線方向なので、ノーマルマップをオブジェクトに適応する計算は、接線座標系で行う。

    `#include UnityStandardInput.cginc``half3 NormalInTangentSpace(float4 texcoords)`
    ```hlsl
    ```glsl
    //in vert func
    o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
    o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
    o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
    ```
    ```hlsl
    ```glsl
    //in frag func
    fixed3 worldN;
    worldN.x = dot(IN.tSpace0.xyz, o.Normal);
  3. sugi.cho revised this gist Jul 24, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -8,13 +8,13 @@
    NormalMapは、接線座標系内の法線方向なので、ノーマルマップをオブジェクトに適応する計算は、接線座標系で行う。

    `#include UnityStandardInput.cginc``half3 NormalInTangentSpace(float4 texcoords)`
    ```csharp
    ```hlsl
    //in vert func
    o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
    o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
    o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
    ```
    ```csharp
    ```hlsl
    //in frag func
    fixed3 worldN;
    worldN.x = dot(IN.tSpace0.xyz, o.Normal);
  4. sugi.cho revised this gist Jul 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #今日のまとめ
    #今日のまとめ(7/23)
    ##オブジェクト空間からワールド空間での各値を求める。
    - `fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);`
    - `fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);`
  5. sugi.cho revised this gist Jul 24, 2015. No changes.
  6. sugi.cho revised this gist Jul 24, 2015. 1 changed file with 129 additions and 0 deletions.
    129 changes: 129 additions & 0 deletions Uneune.shader
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,129 @@
    Shader "Custom/Uneune" {
    Properties {
    _Color ("Color", Color) = (1,1,1,1)
    _MainTex ("Albedo (RGB)", 2D) = "white" {}
    _Glossiness ("Smoothness", Range(0,1)) = 0.5
    _Metallic ("Metallic", Range(0,1)) = 0.0
    _BumpMap ("bump",2D) = "bump" {}
    _VNF ("vert normal factor", Float) = 1.0
    _MNF ("map normal factor", Float) = 1.0
    }
    CGINCLUDE
    //https://github.com/keijiro/NoiseShader
    #include "/Assets/CGINC/ClassicNoise3D.cginc"
    // Use shader model 3.0 target, to get nicer looking lighting
    #pragma target 3.0

    sampler2D _MainTex;
    sampler2D _BumpMap;
    float _VNF,_MNF;

    struct Input {
    float2 uv_MainTex;
    float2 uv_BumpMap;
    half3 tNormal; //normal in tangent space
    };

    half _Glossiness;
    half _Metallic;
    fixed4 _Color;

    //http://forum.unity3d.com/threads/sobel-operator-height-to-normal-map-on-gpu.33159/
    float3 height2normal_sobel(float3x3 c){
    float3x3 x = float3x3
    (
    1.0, 0.0, -1.0,
    2.0, 0.0, -2.0,
    1.0, 0.0, -1.0
    );

    float3x3 y = float3x3
    (
    1.0, 2.0, 1.0,
    0.0, 0.0, 0.0,
    -1.0,-2.0,-1.0
    );

    x = x * c;
    y = y * c;

    float cx =
    x[0][0] + x[0][2] +
    x[1][0] + x[1][2] +
    x[2][0] + x[2][2];

    float cy =
    y[0][0] + y[0][1] + y[0][2] +
    y[2][0] + y[2][1] + y[2][2];

    float cz =sqrt(1-(cx*cx+cy*cy));

    return float3(cx, cy, cz);
    }

    half height(half3 pos){
    return cnoise(pos) * 0.5;
    }

    half3x3 height3x3(half3 pos, half3 normal, half3 tangent, half d){
    half3 binormal = cross(normal, tangent);
    half dx = d * normalize(tangent);
    half dy = d * normalize(binormal);
    half3x3 h;

    h[0][0] = height(pos - dx - dy);
    h[0][1] = height(pos - dy);
    h[0][2] = height(pos + dx - dy);

    h[1][0] = height(pos - dx);
    h[1][1] = height(pos);
    h[1][2] = height(pos - dx);

    h[2][0] = height(pos - dx + dy);
    h[2][1] = height(pos + dy);
    h[2][2] = height(pos + dx + dy);

    return h;
    }

    void vert (inout appdata_full v, out Input o){
    half3 pos = v.vertex.xyz*1.5 + _Time.y;

    v.vertex.xyz += v.normal * height(pos);

    UNITY_INITIALIZE_OUTPUT(Input,o);
    half3x3 h3x3 = height3x3(pos, v.normal, v.tangent, 0.05);
    o.tNormal = height2normal_sobel(h3x3);
    }

    void surf (Input IN, inout SurfaceOutputStandard o) {
    // Albedo comes from a texture tinted by color
    fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    // Metallic and smoothness come from slider variables
    o.Metallic = _Metallic;
    o.Smoothness = _Glossiness;

    half4 normal = tex2D (_BumpMap, IN.uv_BumpMap);
    normal.xy = normal.wy*2-1;
    normal.xy *= _MNF; //ノーマルマップの強さ
    normal.xy += IN.tNormal.xy*_VNF; //頂点シェーダで計算した、法線変位
    normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));

    o.Normal = normal;
    // o.Normal = UnpackScaleNormal (tex2D (_BumpMap, IN.uv_BumpMap),1.5);

    o.Alpha = c.a;
    }
    ENDCG
    SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    // Physically based Standard lighting model, and enable shadows on all light types
    #pragma surface surf Standard vertex:vert addshadow
    ENDCG
    }
    FallBack "Diffuse"
    }
  7. sugi.cho revised this gist Jul 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -30,4 +30,4 @@ NormalMapは、接線座標系内の法線方向なので、ノーマルマッ
    - `UnpackNormal()`とは何なのか?[UnpackNormal](http://tiri-tomato.hatenadiary.jp/entry/20131222/1387694442)

    ##Normal
    <blockquote class="twitter-tweet" lang="ja"><p lang="ja" dir="ltr">法線をShader内で計算するの、できた気がする <a href="http://t.co/PC0WrFMGRu">pic.twitter.com/PC0WrFMGRu</a></p>&mdash; s.c (@sugi_cho) <a href="https://twitter.com/sugi_cho/status/624477332447735813">2015, 7月 24</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
    ![normal](https://pbs.twimg.com/media/CKqW0z-VAAA1M0w.jpg)
  8. sugi.cho revised this gist Jul 24, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -27,4 +27,7 @@ NormalMapは、接線座標系内の法線方向なので、ノーマルマッ
    ##UnityCG.cginc
    - `#define UNITY_PI 3.14159265359f`
    - `unpackScaleNormal(half4 packedNormal, half bumpScale)`
    - `UnpackNormal()`とは何なのか?[UnpackNormal](http://tiri-tomato.hatenadiary.jp/entry/20131222/1387694442)
    - `UnpackNormal()`とは何なのか?[UnpackNormal](http://tiri-tomato.hatenadiary.jp/entry/20131222/1387694442)

    ##Normal
    <blockquote class="twitter-tweet" lang="ja"><p lang="ja" dir="ltr">法線をShader内で計算するの、できた気がする <a href="http://t.co/PC0WrFMGRu">pic.twitter.com/PC0WrFMGRu</a></p>&mdash; s.c (@sugi_cho) <a href="https://twitter.com/sugi_cho/status/624477332447735813">2015, 7月 24</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
  9. sugi.cho revised this gist Jul 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -27,4 +27,4 @@ NormalMapは、接線座標系内の法線方向なので、ノーマルマッ
    ##UnityCG.cginc
    - `#define UNITY_PI 3.14159265359f`
    - `unpackScaleNormal(half4 packedNormal, half bumpScale)`
    - `unpackNormal()`とは何なのか?[UnpackNormal](http://tiri-tomato.hatenadiary.jp/entry/20131222/1387694442)
    - `UnpackNormal()`とは何なのか?[UnpackNormal](http://tiri-tomato.hatenadiary.jp/entry/20131222/1387694442)
  10. sugi.cho revised this gist Jul 23, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,6 @@ NormalMapは、接線座標系内の法線方向なので、ノーマルマッ
    タンジェント空間でのノーマル計算したんやから、tangentToWorldのmatrixとかで、変換すれば良いだけな気がするけど、なざ、こうやってるのかは、まだ理解できてない

    ##UnityCG.cginc
    - `#define UNITY_PI 3.14159265359f`
    - `#define UNITY_PI 3.14159265359f`
    - `unpackScaleNormal(half4 packedNormal, half bumpScale)`
    - `unpackNormal()`とは何なのか?[UnpackNormal](http://tiri-tomato.hatenadiary.jp/entry/20131222/1387694442)
  11. sugi.cho revised this gist Jul 23, 2015. No changes.
  12. sugi.cho revised this gist Jul 23, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ NormalMapは、接線座標系内の法線方向なので、ノーマルマッ
    worldN.z = dot(IN.tSpace2.xyz, o.Normal);
    o.Normal = worldN;
    ```
    タンジェント空間でのノーマル計算したんやから、tangentToWorldのmatrixとかで、変換すれば良いだけな気がするけど、なざ、こうやってるのかは、まだ理解できてない

    ##UnityCG.cginc
    - `#define UNITY_PI 3.14159265359f`
  13. sugi.cho revised this gist Jul 23, 2015. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,10 @@
    - `fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);`
    - `fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;`

    ##タンジェント空間における法線ベクトルからワールド座標系の法線を求める。
    - NormalMapは、接線座標系内の法線方向なので、ノーマルマップをオブジェクトに適応する計算は、接線座標系で行う。
    - `#include UnityStandardInput.cginc``half3 NormalInTangentSpace(float4 texcoords)`
    ##ノーマルマップを貼る
    NormalMapは、接線座標系内の法線方向なので、ノーマルマップをオブジェクトに適応する計算は、接線座標系で行う。

    `#include UnityStandardInput.cginc``half3 NormalInTangentSpace(float4 texcoords)`
    ```csharp
    //in vert func
    o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
    @@ -20,4 +21,7 @@
    worldN.y = dot(IN.tSpace1.xyz, o.Normal);
    worldN.z = dot(IN.tSpace2.xyz, o.Normal);
    o.Normal = worldN;
    ```
    ```

    ##UnityCG.cginc
    - `#define UNITY_PI 3.14159265359f`
  14. sugi.cho revised this gist Jul 23, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    - `fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);`
    - `fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);`
    - `fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;`

    ##タンジェント空間における法線ベクトルからワールド座標系の法線を求める。
    - NormalMapは、接線座標系内の法線方向なので、ノーマルマップをオブジェクトに適応する計算は、接線座標系で行う。
    - `#include UnityStandardInput.cginc``half3 NormalInTangentSpace(float4 texcoords)`
  15. sugi.cho revised this gist Jul 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    - `fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;`
    ##タンジェント空間における法線ベクトルからワールド座標系の法線を求める。
    - NormalMapは、接線座標系内の法線方向なので、ノーマルマップをオブジェクトに適応する計算は、接線座標系で行う。
    - '#include UnityStandardInput.cginc'→'half3 NormalInTangentSpace(float4 texcoords)'
    - `#include UnityStandardInput.cginc``half3 NormalInTangentSpace(float4 texcoords)`
    ```csharp
    //in vert func
    o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
  16. sugi.cho created this gist Jul 23, 2015.
    22 changes: 22 additions & 0 deletions memo-of-today.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #今日のまとめ
    ##オブジェクト空間からワールド空間での各値を求める。
    - `fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);`
    - `fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);`
    - `fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;`
    ##タンジェント空間における法線ベクトルからワールド座標系の法線を求める。
    - NormalMapは、接線座標系内の法線方向なので、ノーマルマップをオブジェクトに適応する計算は、接線座標系で行う。
    - '#include UnityStandardInput.cginc'→'half3 NormalInTangentSpace(float4 texcoords)'
    ```csharp
    //in vert func
    o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
    o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
    o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
    ```
    ```csharp
    //in frag func
    fixed3 worldN;
    worldN.x = dot(IN.tSpace0.xyz, o.Normal);
    worldN.y = dot(IN.tSpace1.xyz, o.Normal);
    worldN.z = dot(IN.tSpace2.xyz, o.Normal);
    o.Normal = worldN;
    ```