Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Dec 22, 2015 at 06:52 AM by Le-Pampelmuse for the following reason:

Duplicate by same user: http://answers.unity3d.com/questions/1111514/broken-specular-shader-code-cg.html

avatar image
-1
Question by awplays49 · Dec 21, 2015 at 10:49 PM · scripting problemshaderreflectionmirrorcg

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.

alt text

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:

alt text

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.

render.png (70.9 kB)
probe-settings.png (23.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Le-Pampelmuse · Dec 22, 2015 at 06:52 AM 1
Share

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.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges