Advertisement
dnnkeeper

GreedyMaterial

Oct 6th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. Shader "Custom/GreedyMaterial" {
  2.     Properties{
  3.         _Color("Color", Color) = (1,1,1,1)
  4.         _MainTex("Albedo (RGB)", 2D) = "white" {}
  5.     }
  6.     SubShader{
  7.         Tags{ "RenderType" = "Opaque" }
  8.         LOD 200
  9.  
  10.         CGPROGRAM
  11.         // Physically based Standard lighting model, and enable shadows on all light types
  12.         //#pragma surface surf Standard fullforwardshadows
  13.         #pragma surface surf Lambert //fullforwardshadows
  14.  
  15.         #pragma target 2.0
  16.  
  17.         sampler2D _MainTex;
  18.  
  19.         struct Input
  20.         {
  21.             float4 color : COLOR;
  22.             float3 worldNormal;
  23.             float3 worldPos;
  24.             float2 uv_MainTex;
  25.         };
  26.  
  27.         half _Glossiness;
  28.         half _Metallic;
  29.         fixed4 _Color;
  30.  
  31.         void surf(Input i, inout SurfaceOutput o) {
  32.  
  33.             const float gridSize = 16; //How many cells are in a row
  34.             const float margin = 0; //(1 / gridSize) / 16; //Margin for preventing bleeding
  35.             const float cellSize = (1 / gridSize) - (margin * 2); //Size of one cell
  36.                                                                   //float blockId = int(i.color.a * gridSize * gridSize) / gridSize; //Get tile id from alpha channel, and transform it so that fraction is horizontal position [0, 1] and integer part points vertical position [0, gridSize]
  37.                                                                   //float blockId= int(i.uv_MainTex.y*gridSize) +int(i.uv_MainTex.x/cellSize);
  38.             fixed4 c = tex2D(_MainTex, frac(float2(
  39.                 i.worldPos.x * (1 - i.worldNormal.x) + i.worldPos.z * i.worldNormal.x, //Figure out texel location inside a cell.
  40.                 i.worldPos.y * (1 - i.worldNormal.y) + i.worldPos.z * i.worldNormal.y)) * cellSize  //Take in account face normal
  41.                                                                                                     //+ float2(blockId + margin, floor(blockId) / gridSize + margin)); //Choose the actual tile
  42.                 + float2(int(i.uv_MainTex.x / cellSize)*cellSize, int(i.uv_MainTex.y / cellSize)*cellSize));
  43.             //+i.uv_MainTex);
  44.             o.Albedo = c.rgb*_Color;
  45.             o.Alpha = c.a;
  46.         }
  47.         ENDCG
  48.     }
  49.     FallBack "Diffuse"
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement