• 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: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

avatar image
0
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
Add comment · Show 2
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 awplays49 · Dec 20, 2015 at 07:47 PM 0
Share

Where do i find the cubemap that reflection probes generate? How can I access that for a custom shader?

avatar image awplays49 · Dec 20, 2015 at 09:52 PM -1
Share

Still stuck :/

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

36 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

Related Questions

Help with grabpass shader for 2D water effect. 2 Answers

Removing the mirror effect in Reflection probes 0 Answers

How to write a surface shader for realtime reflection? (CG) 0 Answers

HD Render Pipeline Refraction / Distortions 1 Answer

Realtime Light-probe ? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges