Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "_Shader/IBLshader" {
- //Properties can be used as if they are public data members, but they are actually special methods called accessors
- Properties {
- _DiffuseColor ("Diffuse Color", 2D) = "white" {}
- _SpecularColor ("Specular Color", Color) = (1,1,1,1)
- _Shininess ("Shininess", range(1,20)) = 1
- _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
- _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
- _Glossiness ("Glossiness", Float ) = 0.5
- _AmbientCubemap ("Ambient Cubemap", Cube) = "_Skybox" {}
- _AmbientCubemapIntensity ("Ambient Cubemap Intensity", Float ) = 1
- _AmbientCubemapContrast ("Ambient Cubemap Contrast", Float ) = 1
- }
- // mapping of the picture
- SubShader {
- Tags { "Queue"="Transparent" }
- LOD 200
- Pass {
- SetTexture[_Cube] { combine texture }
- }
- }
- SubShader {
- Pass {
- Tags { "LightMode" = "ForwardBase" }
- CGPROGRAM
- //Preprocessor compile directive
- #pragma vertex vert
- #pragma fragment frag
- //Forward rendering base pass (main directional light, lightmaps, SH).
- #define UNITY_PASS_FORWARDBASE
- //Commonly used global variables and helper functions.
- #include "UnityCG.cginc"
- //Lighting & shadowing functionality, e.g. surface shaders use this file internally.
- #include "AutoLight.cginc"
- //Standard surface shader lighting models; automatically included when you’re writing surface shaders.
- #include "Lighting.cginc"
- //tells the compiler needs to generate a permutation of the shader for each of those defines and will produce two shader variants Off and On
- #pragma multi_compile_fwdbase_fullshadows
- //Do not compile shader for given renderers. By default shaders are compiled for all renderers.
- #pragma exclude_renderers xbox360 ps3 flash
- //Which shader target to compile to
- #pragma target 3.0
- /// Keywords used to create light mapping in unity - a negative of #ifdef LIGHTMAP_OFF. #ifndef checks whether the given token has been #defined earlier in the file or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement. #ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.
- //Using pragma multi compile to compile more than one shader
- #ifndef LIGHTMAP_OFF
- //Floating point sampler for 2D Texture
- sampler2D unity_Lightmap;
- float4 unity_LightmapST;
- #ifndef DIRLIGHTMAP_OFF
- sampler2D unity_LightmapInd;
- #endif
- #endif
- //Uniforms for accessing shader properties declared in properties block
- uniform sampler2D _DiffuseColor;
- uniform float4 _DiffuseColor_ST;
- uniform sampler2D _SpecularColor;
- uniform float4 _SpecularColor_ST;
- uniform float _Shininess;
- uniform sampler2D _MainTex;
- uniform sampler2D _Cube;
- uniform float4 _Color;
- uniform samplerCUBE _AmbientCubemap;
- uniform float _AmbientCubemapIntensity;
- uniform float _Glossiness;
- uniform sampler2D _NormalMap;
- uniform float4 _NormalMap_ST;
- uniform float _AmbientCubemapContrast;
- //vertex input data must be passed in a struct
- struct vertexInput {
- float4 vertex : POSITION; //a vertex position
- float3 normal : NORMAL; //the vertex normal
- float3 tangent: TANGENT; //the tangent vector (used for normal mapping)
- float4 uv0 : TEXCOORD0; // the first UV coordinate
- float4 uv1 : TEXCOORD1; // the second UV coordinate
- };
- //vertex output data
- struct vertexOutput {
- float4 pos : SV_POSITION; // when using UV’s the output often is written as SV_
- float4 uv0 : TEXCOORD0; // the first UV coordinate
- float4 worldPos: TEXCOORD1;
- float3 tangentWorld : TEXCOORD2;
- float3 normalWorld : TEXCOORD3;
- float3 binormalWorld : TEXCOORD4;
- LIGHTING_COORDS(5,6) // LIGHTING_COORDS needs two TEXCOORDS in the vertex shader
- //multi compile pragma just tells Unity to compile multiple times with mutually exclusive identifiers
- #ifndef LIGHTMAP_OFF
- float2 uvLM : TEXCOORD7;
- #endif
- };
- //vertex output function
- vertexOutput vert(vertexInput input) {
- vertexOutput output;
- output.uv0 = input.uv0;
- //transformations
- float4x4 modelMatrix = _Object2World; //Current model matrix
- float4x4 viewMatrix = UNITY_MATRIX_V; //Current view matrix
- float4x4 projectionMatrix = UNITY_MATRIX_P; //Current projection matrix
- //Find the world position
- output.worldPos = mul(_Object2World,input.vertex);
- //Find the normals in world coordinates
- float4x4 modelMatrixInverse = _World2Object; //Inverse of current world matrix
- output.normalWorld = normalize(mul(float4(input.normal,0),modelMatrixInverse).xyz);
- output.tangentWorld = normalize(mul(float4(input.tangent,0),modelMatrix).xyz);
- output.binormalWorld = normalize(cross(output.tangentWorld,output.normalWorld));
- output.pos = mul(projectionMatrix,mul(viewMatrix,mul(modelMatrix,input.vertex)));
- output.uv0 = input.uv0;
- // Keywords used to create light mapping in unity - a negative of #ifdef LIGHTMAP_OFF
- #ifndef LIGHTMAP_OFF
- output.uvLM = input.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
- //specifies the end of a conditional directive
- #endif
- // part of attenuation in the fragment program
- TRANSFER_VERTEX_TO_FRAGMENT(output)
- return output;
- }
- //fragment function
- fixed4 frag(vertexOutput input) : COLOR {
- float3x3 tangentTransform = float3x3( input.tangentWorld, input.binormalWorld, input.normalWorld);
- float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - input.worldPos.xyz);
- float2 node_3 = input.uv0;
- float3 normalLocal = UnpackNormal(tex2D(_NormalMap,TRANSFORM_TEX(node_3.rg, _NormalMap))).rgb;
- float3 normalDirection = normalize( mul( normalLocal, tangentTransform ) );
- // Keywords used to create light mapping in unity - a negative of #ifdef LIGHTMAP_OFF. #ifndef checks whether the given token has been #defined earlier in the file or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement. #ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.
- #ifndef LIGHTMAP_OFF
- float4 lmtex = tex2D(unity_Lightmap,input.uvLM);
- #ifndef DIRLIGHTMAP_OFF
- //Unity built-in function DecodeLightmap to decode information from data stored in lightmap textures and built-in UNITY_DIRBASIS macro defining basis vectors for Directional lightmaps
- float3 lightmap = DecodeLightmap(lmtex);
- //An index into the lightmap array
- float3 scalePerBasisVector = DecodeLightmap(tex2D(unity_LightmapInd,i.uvLM));
- UNITY_DIRBASIS
- // Saturate is a function that clamps it's input between 0 and 1.
- half3 normalInRnmBasis = saturate (mul (unity_DirBasis, normalLocal));
- lightmap *= dot (normalInRnmBasis, scalePerBasisVector);
- #else
- float3 lightmap = DecodeLightmap(tex2D(unity_Lightmap,i.uvLM));
- #endif
- #endif
- #ifndef LIGHTMAP_OFF
- #ifdef DIRLIGHTMAP_OFF
- float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
- #else
- //built-in UNITY_DIRBASIS macro defining basis vectors for Directional lightmaps
- float3 lightDirection = normalize (scalePerBasisVector.x * unity_DirBasis[0] + scalePerBasisVector.y * unity_DirBasis[1] + scalePerBasisVector.z * unity_DirBasis[2]);
- lightDirection = mul(lightDirection,tangentTransform); // Tangent to world
- #endif
- #else
- float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
- #endif
- float3 halfDirection = normalize(viewDirection+lightDirection);
- ////// Lighting:
- //The attenuation of light traditionally matches how light behaves in the physical world; it fades out quickly at first, but has a tiny effect for a very long range.
- float attenuation = LIGHT_ATTENUATION(input)*2;
- float3 attenColor = attenuation * _LightColor0.xyz;
- /////// Diffuse:
- //N*L to determine which faces are front-facing and just reduce shadows this way
- float NdotL = dot( normalDirection, lightDirection );
- #ifndef LIGHTMAP_OFF
- float3 diffuse = lightmap;
- #else
- //Current ambient color
- float3 diffuse = max( 0.0, NdotL) * attenColor + UNITY_LIGHTMODEL_AMBIENT.xyz*2;
- #endif
- ///////// Gloss:
- float gloss = exp2(_Glossiness*10.0+1.0);
- ////// Specular:
- NdotL = max(0.0, NdotL);
- //vertex program uses the TRANSFORM_TEX macro from UnityCG.cginc to make sure texture scale&offset is applied correctly, and fragment program just samples the texture and multiplies by the color property.
- float3 specularMask = tex2D(_SpecularColor,TRANSFORM_TEX(node_3.rg, _SpecularColor)).rgb;
- float3 specular = (floor(attenuation) * _LightColor0.xyz) * pow(max(0,dot(halfDirection,normalDirection)),gloss) * specularMask;
- float node_22 = 0.5;
- //tex-Cube performs a texture lookup in a given CUBE sampler and, in some cases, a shadow comparison. In this case it combines all the shaders,
- float3 finalColor = ( diffuse + ((((texCUBE(_AmbientCubemap,normalDirection).rgb-node_22)*_AmbientCubemapContrast)+node_22)*_AmbientCubemapIntensity) ) * (_Color.rgb*tex2D(_DiffuseColor,TRANSFORM_TEX(node_3.rg, _DiffuseColor)).rgb) + specular;
- /// Final Color:
- return fixed4(finalColor,1);
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement