Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Stampyturtle · Nov 08, 2013 at 09:07 PM · shaderlightingsurfacedirectxcustom-shader

How can I pass world position to a custom lighting function for a surface shader?

I'm trying to use world position ton a custom lighting function, passed to the lighting function via a custom surface output struct. However, every time it fails to compile and claims the surface output custom struct is not initialized. However, this is clearly not the case and appears that something else is going on behind the scenes. I've included a much stripped down shader that reproduces the error and have marked exactly where it fails at compiling.

However, note that if you perform any sort of operation (o.posWorld = IN.worldPos * 1.0;) on it it will compile, but then fail to compile if you use o.posWorld anywhere in the lighting function.

 Shader "Custom/TestUnityAnswers" 
 {
     Properties 
     {
         _MainTex ("Diffuse RGB", 2D) = "white" {}
     }
 
     SubShader 
     {
         Tags { "RenderType" = "Opaque" }
         LOD 200
         CGPROGRAM
         // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
         #pragma exclude_renderers gles
         #pragma only_renderers d3d11
         #pragma vertex vert 
         #pragma surface surf Lambert2
         sampler2D _MainTex;
         
         struct Input 
         {
             float2 uv_MainTex : TEXCOORD0;
             float3 worldPos   : TEXCOORD1;
         };
     
         struct SurfaceOutputCustom 
         {
             fixed3 Albedo;
             fixed3 Normal;
             fixed3 Emission;
             half Specular;
             fixed Gloss;
             fixed Alpha;
             fixed3 posWorld;
         };
         
         void vert (inout appdata_full v, out Input o) 
         {
             UNITY_INITIALIZE_OUTPUT(Input,o);
             o.worldPos = mul(_Object2World, v.vertex).xyz;
         }
 
         inline fixed4 LightingLambert2 (SurfaceOutputCustom s, fixed3 lightDir, fixed atten) 
         {
             fixed4 c;
             fixed diff = max (0, dot (s.Normal, lightDir));
             c.rgb = s.Albedo * _LightColor0.rgb * diff * atten * 2;
             c.a = s.Alpha;
             return c;
         }
         
         void surf (Input IN, inout SurfaceOutputCustom o) 
         {
             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
             o.Albedo = tex.rgb; 
             
             // Offending line
             o.posWorld = IN.worldPos;
         }
 
         ENDCG
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PatrickHogenboom · Nov 05, 2019 at 02:56 PM

Calling in from 2019, seems that this use-case works fine these days. This code displays world position from the custom lighting model.

 Shader "Custom/TestUnityAnswers"
 {
     Properties
     {
         _MainTex("Diffuse RGB", 2D) = "white" {}
     }
 
         SubShader
     {
         Tags { "RenderType" = "Opaque" }
         LOD 200
         CGPROGRAM
         #pragma exclude_renderers gles
         #pragma only_renderers d3d11
         #pragma vertex vert 
         #pragma surface surf Lambert2
         sampler2D _MainTex;
 
         struct Input
         {
             float2 uv_MainTex : TEXCOORD0;
             float3 worldPos   : TEXCOORD1;
         };
 
         struct SurfaceOutputCustom
         {
             fixed3 Albedo;
             fixed3 Normal;
             fixed3 Emission;
             half Specular;
             fixed Gloss;
             fixed Alpha;
             fixed3 posWorld;
         };
 
         void vert(inout appdata_full v, out Input o)
         {
             UNITY_INITIALIZE_OUTPUT(Input,o);
             o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
         }
 
         inline fixed4 LightingLambert2(SurfaceOutputCustom s, fixed3 lightDir, fixed atten)
         {
             return half4(s.posWorld, 1);
         }
 
         void surf(Input IN, inout SurfaceOutputCustom o)
         {
             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
             o.Albedo = tex.rgb;
             o.posWorld = IN.worldPos;
         }
 
         ENDCG
     }
 }

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.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Better shader effects on terrain - Bumpmapping or Specular? 2 Answers

Passing variables from Script to a Shader 2 Answers

Assigning vertex normal in Surface program to o.Normal brakes lighting? 0 Answers

How do I incorporate per-vertex lighting data into a surface shader? 0 Answers

Unity5 Any way to have a unlit no depth shader? 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