• 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 /
avatar image
0
Question by miketucker · Oct 12, 2011 at 12:13 PM · shaderdiffuse

Inverted Diffuse Shader

Hi, I'm trying to achieve essentially the inverse of a diffuse shader. Lit areas are black, and unlit are white.

I attempted to modify the existing Mobile Diffuse shader with the hope of inverting a color value, but at 4,360 lines of code, i could not find the correct spot.

Also worth noting it will be used for iOS (OpenGL ES 2.0)

alt text

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by miketucker · Oct 12, 2011 at 01:29 PM

This is per-pixel so it's not very efficient. it also requires a directional light. but it works.

It's modified from http://en.wikibooks.org/wiki/GLSL_Programming/Unity/Diffuse_Reflection

The only change is on the definition of color at the end of the main() function which is the inverse of each color value.

 Shader "RH/InvertDiffuse" {
    Properties {
       _Color ("Color", Color) = (1,1,1,1) 
    }
    SubShader {
       Pass {      
          Tags { "LightMode" = "ForwardBase" } // make sure that all uniforms are correctly set
  
          GLSLPROGRAM
  
          uniform vec4 _Color; // shader property specified by users
  
          // The following built-in uniforms are also defined in "UnityCG.glslinc",
          // i.e. one could also #include "UnityCG.glslinc" (except _LightColor0)
          uniform mat4 _Object2World; // model matrix
          uniform mat4 _World2Object; // inverse model matrix
          uniform vec4 _WorldSpaceLightPos0; // direction to or position of light source
          uniform vec4 _LightColor0; // color of light source (from "Lighting.cginc")
  
          varying vec4 color; // the diffuse lighting computed in the vertex shader
  
          #ifdef VERTEX
  
          void main()
          {                                
             mat4 modelMatrix = _Object2World;
             mat4 modelMatrixInverse = _World2Object; // unity_Scale.w is unnecessary because we normalize vectors
  
             vec3 normalDirection = normalize(vec3(vec4(gl_Normal, 0.0) * modelMatrixInverse));
             vec3 lightDirection = normalize(vec3(_WorldSpaceLightPos0));
  
             vec3 diffuseReflection = vec3(_LightColor0) * vec3(_Color)
                * max(0.0, dot(normalDirection, lightDirection));
  
             color = vec4(1.0-diffuseReflection.x,1.0-diffuseReflection.y,1.0-diffuseReflection.z, 1.0);
             gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
          }
  
          #endif
  
          #ifdef FRAGMENT
  
          void main()
          {
             gl_FragColor = color;
          }
  
          #endif
  
          ENDGLSL
       }
    } 
    // The definition of a fallback shader should be commented out during development:
    // Fallback "Diffuse"
 }


Comment
Add comment · Share
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
0

Answer by dandeentremont · Oct 10, 2013 at 12:37 AM

I believe you would get this effect by multiplying the normal's x y and z values by -1, no?

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Transparent w/ Full Shadowcasting in Unity 5 1 Answer

Separate Alpha and Color Textures for CutOut Shader 1 Answer

Making shader's first pass light sensitive (2D) 0 Answers

Gameobject with Diffuse Shader visible in darkness 1 Answer

Transparent Diffuse issues 1 Answer


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