This question was
closed Dec 22, 2015 at 06:54 AM by
Le-Pampelmuse for the following reason:
Duplicate by same User: http://answers.unity3d.com/questions/1111514/broken-specular-shader-code-cg.html
Question by
awplays49 · Dec 20, 2015 at 07:46 PM ·
shaderreflectioncustomshader writingreflections
How to use reflection probes/cubemaps to make a reflection shader
Shader "Example/Specular" {
Properties {
_Color ("Color", Color) = (1.0,1.0,1.0,1.0)
_Atten ("Atten", float) = 1
_SpecColor ("Specular Color", Color) = (1.0,1.0,1.0,1.0)
_Shininess ("Shininess", float) = 10
_Reflection ("Reflection", float) = 0.5
_Cube ("Cubemap", Cube) = "" {}
}
SubShader {
Tags
{
"LightMode" = "ForwardBase"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
float _Atten;
float4 _SpecColor;
float _Shininess;
float _Reflection;
float4 _LightColor0;
samplerCUBE _Cube;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct VertexOutput {
float4 pos : POSITION;
float4 col : COLOR;
float3 view : TEXCOORD0;
float3 norm : TEXCOORD1;
};
VertexOutput vert (VertexInput input) {
VertexOutput output;
// Matrix variables
float4x4 worldMatrix = _Object2World;
float4x4 localMatrix = _World2Object;
// Positions
float3 cameraPosition = mul (localMatrix, _WorldSpaceCameraPos);
// Directions
float3 normalDirection = normalize (mul (localMatrix, float4 (input.normal, 0.0)).xyz);
float3 lightDirection = normalize (_WorldSpaceLightPos0.xyz);
float3 viewDirection = normalize (cameraPosition - input.vertex);
// Diffuse
float3 diffuseLightColor = _LightColor0.rgb * max (0.0, dot (normalDirection, lightDirection));
float3 diffuseColor = _Color.rgb * diffuseLightColor * _Atten;
// Specular
float3 lightToView = normalize(lightDirection + viewDirection);
float3 specularLightColor = pow (max (0, dot (normalDirection, lightToView)), _Shininess) * pow (max (0, dot (reflect (-lightDirection, normalDirection), viewDirection)), _Shininess);
float3 specularColor = _SpecColor.rgb * specularLightColor * _Atten;
// Combination of all colors
float3 colorFinal = diffuseColor + specularColor + UNITY_LIGHTMODEL_AMBIENT;
// Apply variables calculated above
output.col = float4 (colorFinal, 1.0);
output.pos = mul (UNITY_MATRIX_MVP, input.vertex);
output.view = viewDirection;
output.norm = normalDirection;
return output;
}
float4 frag (VertexOutput output) : COLOR {
float3 reflection = reflect (output.view, normalize (output.norm)) * _Reflection;
return texCUBE (_Cube, reflection) * output.col;
}
ENDCG
}
}
}
I have this code and I think itll work (someone tell me otherwise) but I dont know how to make the reflection probes bake cubemaps. How do other people make realtime reflection with custom shaders?
Comment
Where do i find the cubemap that reflection probes generate? How can I access that for a custom shader?