Duplicate by same user: http://answers.unity3d.com/questions/1111514/broken-specular-shader-code-cg.html
Mirror reflection not working as planned
Hello,
I have a shader I have been working on to make mirror reflections. I am making it from scratch for experience purposes to learn CG. I have a reflection probe and a mirror script and also a reflection probe script.
There are a few things wrong with this picture. First, the world is upside down. Second, the closer I get, the more pixelated the reflection appears.
Shader script:
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) * _Reflection * output.col;
}
ENDCG
}
}
}
Reflection probe settings:
Reflection probe script:
using UnityEngine;
using System.Collections;
public class RenderProbe : MonoBehaviour {
public ReflectionProbe probe;
void Update () {
probe.RenderProbe();
}
}
Mirror script:
using UnityEngine;
using System.Collections;
public class SetMirror : MonoBehaviour {
public ReflectionProbe probe;
void Update () {
GetComponent <MeshRenderer> ().material.SetTexture ("_Cube", probe.texture);
}
}
The mirror shader is attached to the cube I am looking at in the screenshot.
Don't post duplicate questions if your other questions are not answered. If you want to add something to your existing questions, like scripts, or screenshots you can edit them easily or comment them.
PS: How-To and Fix$$anonymous$$yCode questions are not appropriate on UA. For popular questions, like how to make a mirror, there are already many discussions and solutions available.
If you have trouble with something, first google it to see if someone already has an answer somewhere. You already posted 4 questions I can see at the moment with the identical idea "how to make a mirror shader". This harms the forum because:
there are now 4 new open questions in the Default Room
they are all the same
they are not appropriate on UA
You should know how to use this forum already, your questions are not filtered anymore by the moderation queue because you have over 15 reputation.
In case you didn't read the forum guidelines: Please read the FAQ and the User Guide carefully so you don't post every single question you might have here.
It is a good practice to google your question and look through the different results. :)
I hope you understand that I'm not trying to be rude, but want to keep the forum working as intended and organized ;)
Have a nice day.
Follow this Question
Related Questions
How to write a surface shader for realtime reflection? (CG) 0 Answers
Intercept last pass on a surface shader to change RGB of that pixel 0 Answers
Why does my mirror not reflect my water shader? 0 Answers
HD Render Pipeline Refraction / Distortions 1 Answer
Parse error: syntax error, unexpected TVAL_ID, expecting TVAL_STRING 0 Answers