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

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 remove specular reflections? 0 Answers

How do I get the fragment position in world coordinates? 1 Answer

How to write a shader like this? [Screenshots] 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