• 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 KuangZheng · Aug 23, 2013 at 04:35 AM · shadercg

_World2Object and _Object2World problem

I have a plane model,and it's divided to some parts. I write shader for them and they share the same material.But i find that some model cannot get correct _World2Object and _Object2World.

I code this in fragment funtion :

 return float4(_World2Object[0][0],_World2Object[1][1],_World2Object[2][2],1);

and get this

alt text

the red part and white part are used the same material. I move or rotate the plane,the red part can change color,but the white part always white,so I guess the _World2Object and _Object2World not change value.

this is the whole shader code:

 Shader "Custom/EF3 Fighter (2)" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _NormalTex ("normal Texture", 2D) = "white" {}
         _ReflectTexPlane ("reflect texture", 2D) = "white" {}
 
         _SpecularShiness ("Specular Shiness", Float) = 50
         _ReflectRatio("Reflect Ratio", Float) = 0.1
         
         _RimColor ("Rim Color", Color) = (0.29,0.4,0.4,0.0)
           _RimPower ("Rim Power", Range(0.5,16.0)) = 11.0
 
     }
     SubShader {
 //        Tags { "RenderType"="Opaque" }
 
         Pass{
             Tags { "LightMode" = "ForwardBase" }
             CGPROGRAM
             #pragma vertex Vert
             #pragma fragment Frag
             #include "UnityCG.cginc"
                 
             uniform sampler2D _MainTex;
             uniform sampler2D _NormalTex;
             uniform sampler2D _ReflectTexPlane;
             uniform float _SpecularShiness;
             uniform float _ReflectRatio;
             uniform float4 _lightSourcePos;
             uniform float4 _LightColor0; 
             
             uniform float4 _RimColor;
             uniform float _RimPower;
 
             
             struct VS_OUT
             {
             
                 float4 pos:POSITION;
                 float2 tex:TEXCOORD0;
                 float3 light:TEXCOORD1;
 //                float3 view:TEXCOORD2;
                 float3 worldPos:TEXCOORD2;
                 float3 normal:TEXCOORD3;
                 
             };
             
     
           
             VS_OUT Vert(appdata_full v)
             {
                 VS_OUT output;
             
                 output.pos = mul(UNITY_MATRIX_MVP,v.vertex);
                 
                 output.tex = v.texcoord.xy;
                 
                    
 
                 output.worldPos = mul(_Object2World,v.vertex).xyz;
                 //output.light = normalize(_lightSourcePos.xyz-worldPos);
 //                output.light = normalize(float3(0,6000,10000));
                 output.light = normalize(float3(_WorldSpaceLightPos0));
                 
 //                output.light = normalize(float3(0,-1,0));
                 
                 
                     //output.view = normalize(_cameraPos.xyz - worldPos);
                     
 //                    output.view = normalize(_WorldSpaceCameraPos.xyz - worldPos);
 //                    output.view = _WorldSpaceCameraPos.xyz - worldPos;
                     
 //                    output.normal = v.normal;
                 output.normal = normalize(mul((float3x3)_World2Object, v.normal));
                    
                     //output.light = normalize(mul((float3x3)_World2Object, output.light));
                      //output.view= normalize(mul((float3x3)_World2Object, output.view));
                     
 //                    output.view.z = -output.view.z;
                     //output.worldPos = worldPos;
                     
                     //output.refl.xyz = normalize(mul((float3x3)_Object2World,v.normal));
                     
                     
                     
                    // float3 worldLightDir = normalize(( CameraWorldPos - mul(_Object2World,v.vertex)).xyz);
                     //output.refl.w = saturate(dot(normalize(mul((float3x3)_Object2World,v.normal)),output.light));
 
 
                 return output;
             }
                 
     
             half4 Frag(float2 tex:TEXCOORD0,float3 light:TEXCOORD1,float3 worldPos:TEXCOORD2,float3 normal:TEXCOORD3):COLOR
             {
                 float3 view =  normalize( _WorldSpaceCameraPos.xyz - worldPos);
                 float4 baseColor = tex2D(_MainTex,tex);
                 
                  float3 normalTex = 2*tex2D(_NormalTex,tex)-1;
 //                 float3 worldNormal = normalTex;
                  float3 worldNormal = normalize(mul((float3x3)_Object2World,normalTex)).xyz;
 //                 float3 worldNormal = normalize(mul(normalTex,(float3x3)_World2Object)).xyz;
 
                  float2 refl = (reflect(view,normal)).xz * 0.4 + 0.5;
 //                float2 refl = (reflect(view,worldNormal)).xz * 0.4 + 0.5;
                  float4 reflectColor = tex2D(_ReflectTexPlane,refl);
 
                  
     
 
                  float3 diffuseReflection = 4* float3(_LightColor0)* baseColor.xyz * max(0.2, dot(worldNormal, light));
 
 
                 
                 float3 specularReflection = float3(1,1,1);
                 float dotSpecular = dot( reflect(-light, worldNormal),  view);
                 if (dot(worldNormal, light) < 0.0) {
                    // light source on the wrong side?
                 
                    specularReflection = float3(0.0, 0.0, 0.0); 
                       // no specular reflection
                 }else{
                     specularReflection = specularReflection*pow(max(0.0, dotSpecular), _SpecularShiness);
                 }
 
                 
 
                     
                     float3 n = normal;
                     n = mul((float3x3)_Object2World,n).xyz;
                     
                 half rim = 1.0 - saturate(dot (-view, n));
 //                half rim =  saturate(dot (-view, worldNormal));
                 
                 float4 rimco =  _RimColor*pow (rim, _RimPower);
                 
 
                 return float4(_World2Object[0][0],_World2Object[1][1],_World2Object[2][2],1);
             }
         ENDCG    
         }  
     }
     FallBack "Diffuse"
 }
 

Please help me

screen shot 2013-08-23 at 下午12.10.19.png (159.4 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

15 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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

How to determine shader center? 0 Answers

Pass ShaderLab properties to Standard cginc 2 Answers

Mobile Additive surface shader 0 Answers

Shader compiler: internal error compiling shader snippet type=0 platform=0: 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