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 /
avatar image
0
Question by Odessa_Int · Jan 27, 2018 at 12:06 AM · shaderplayershadersviewviewport

Unity Shader, Get the player POV

I have a shader that enables me to change the texture around the player.

It gives the following effect:

alt text

I would like to change this so that the area in which the non-white texture is viable is the players POV

This means it would need both the world position, to get the location the change stars from in the scene, and the player POV to get the area in which the texture has changed.

Here is the shader:

 Shader "Custom/Proximity" {
      Properties {
          _MainTex ("Textre (RGB)", 2D) = "white" {} // Regular object texture 
           _WhiteTex ("White (RGB)", 2D) = "white" {}
          _PlayerPosition ("Player Position", vector) = (0,0,0,0) // The location of the player - will be set by script
            _VisibleDistance ("Visibility Distance", float) = 10.0 // How close does the player have to be to make object visible
            _OutlineWidth ("Outline Width", float) = 3.0 // Used to add an outline
            _OutlineColour ("Outline Colour", color) = (1.0,1.0,0.0,1.0) // Colour of the outline
           
      }
      SubShader {
          Tags { "RenderType"="Transparent" "Queue"="Transparent"}
          Pass {
          Blend SrcAlpha OneMinusSrcAlpha
          LOD 200
      
          CGPROGRAM
          #pragma vertex vert
          #pragma fragment frag
  
          // Access the shaderlab properties
          uniform sampler2D _MainTex;
          uniform sampler2D _WhiteTex;
          uniform float4 _PlayerPosition;
          uniform float _VisibleDistance;
          uniform float _OutlineWidth;
          uniform fixed4 _OutlineColour;
          
          // Input to vertex shader
          struct vertexInput {
              float4 vertex : POSITION;
              float4 texcoord : TEXCOORD0;
           };
          // Input to fragment shader
           struct vertexOutput {
              float4 pos : SV_POSITION;
              float4 position_in_world_space : TEXCOORD0;
              float4 tex : TEXCOORD1;
           };
           
           // VERTEX SHADER
           vertexOutput vert(vertexInput input) 
           {
              vertexOutput output; 
              output.pos =  UnityObjectToClipPos(input.vertex);
              output.position_in_world_space = mul(unity_ObjectToWorld, input.vertex);
              output.tex = input.texcoord;
              return output;
           }
   
           // FRAGMENT SHADER
          float4 frag(vertexOutput input) : COLOR 
          {
              // Calculate distance to player position
              float dist = distance(input.position_in_world_space, _PlayerPosition);
              
   
               // Return appropriate colour
              if (dist < _VisibleDistance) {
                 return tex2D(_MainTex, float4(input.tex)); // Visible
              }
              else if (dist < _VisibleDistance + _OutlineWidth) {
                  return _OutlineColour; // Edge of visible range
              }
              else {
                  float4 tex = tex2D(_WhiteTex, float4(input.tex)); // Outside visible range
                  tex.a = 1;
                  
                  return tex;
 
                 
                 
              }
           }
  
          ENDCG
          }
      } 
      //FallBack "Diffuse"
  }

I can not for the life of me figure out how to do this.

Any help would be greatly appreciated!

capture.png (164.2 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.

Follow this Question

Answers Answers and Comments

182 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 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 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 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 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 avatar image avatar image

Related Questions

Creating a 3D portal effect using stencil buffer with a deferred shading setup? 1 Answer

LWRP Shader Graph material fades away with distance. 0 Answers

How do I cell shade terrain? 0 Answers

Making clamped texture render parts outside of the uvs transparent 1 Answer

Material.SetFloat not working 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