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 /
avatar image
0
Question by Emerald_Eel_Entertainment · Sep 21, 2017 at 11:23 AM · shaderstexturesmappingplaystation

PS1 Shaders - Reducing Distortion

I downloaded a pack of shaders from dsoft20's Github page. They replicate the behaviour of Playstation graphics, including the hardware limitations of the time:

https://github.com/dsoft20/psx_retroshader

I've been modifying the parameters of the VertexLit shader, the default PS1 shader. I toned down the snapping of vertices to pixels, but I was not able to reduce/stop the stretching of the affine textures as they get closer to the camera.

alt text

How can I reduce/stop this deforming? Here is the code from the VertexLit script:

 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
 Shader "psx/vertexlit" {
     Properties{
         _MainTex("Base (RGB)", 2D) = "white" {}
     }
         SubShader{
             Tags { "RenderType" = "Opaque" }
             LOD 200
 
             Pass {
             Lighting On
                 CGPROGRAM
 
                     #pragma vertex vert
                     #pragma fragment frag
                     #include "UnityCG.cginc"
 
                     struct v2f
                     {
                         fixed4 pos : SV_POSITION;
                         half4 color : COLOR0;
                         half4 colorFog : COLOR1;
                         float2 uv_MainTex : TEXCOORD0;
                         half3 normal : TEXCOORD1;
                     };
 
                     float4 _MainTex_ST;
                     uniform half4 unity_FogStart;
                     uniform half4 unity_FogEnd;
 
                     v2f vert(appdata_full v)
                     {
                         v2f o;
 
                         //Vertex snapping
                         float4 snapToPixel = UnityObjectToClipPos(v.vertex);
                         float4 vertex = snapToPixel;
                         vertex.xyz = snapToPixel.xyz / snapToPixel.w;
                         vertex.x = floor(160 * vertex.x) / 160;
                         vertex.y = floor(120 * vertex.y) / 120;
                         vertex.xyz *= snapToPixel.w;
                         o.pos = vertex;
 
                         //Vertex lighting 
                     //    o.color =  float4(ShadeVertexLights(v.vertex, v.normal), 1.0);
                         o.color = float4(ShadeVertexLightsFull(v.vertex, v.normal, 4, true), 1.0);
                         o.color *= v.color;
 
                         float distance = length(mul(UNITY_MATRIX_MV,v.vertex));
 
                         //Affine Texture Mapping
                         float4 affinePos = vertex; //vertex;                
                         o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
                         o.uv_MainTex *= distance + (vertex.w*(UNITY_LIGHTMODEL_AMBIENT.a * 8)) / distance / 2;
                         o.normal = distance + (vertex.w*(UNITY_LIGHTMODEL_AMBIENT.a * 8)) / distance / 2;
 
                         //Fog
                         float4 fogColor = unity_FogColor;
 
                         float fogDensity = (unity_FogEnd - distance) / (unity_FogEnd - unity_FogStart);
                         o.normal.g = fogDensity;
                         o.normal.b = 1;
 
                         o.colorFog = fogColor;
                         o.colorFog.a = clamp(fogDensity,0,1);
 
                         //Cut out polygons
                         if (distance > unity_FogStart.z + unity_FogColor.a * 255)
                         {
                             o.pos.w = 0;
                         }
 
                         return o;
                     }
 
                     sampler2D _MainTex;
 
                     float4 frag(v2f IN) : COLOR
                     {
                         half4 c = tex2D(_MainTex, IN.uv_MainTex / IN.normal.r)*IN.color;
                         half4 color = c*(IN.colorFog.a);
                         color.rgb += IN.colorFog.rgb*(1 - IN.colorFog.a);
                         return color;
                     }
                 ENDCG
             }
     }
 }


Comment
Add comment · Show 1
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 XenomorphGames · Feb 14, 2020 at 06:00 PM 0
Share

Ok ins$$anonymous$$d of getting rid off the modifier change the distance in the script $$anonymous$$e is set to 5 ins$$anonymous$$d of 2 when stretches the texture to far.

 // Upgrade NOTE: replaced 'mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP,*)' with 'UnityObjectToClipPos(*)'
 
 Shader "psx/trasparent/vertexlit" {
     Properties{
         _$$anonymous$$ainTex("Base (RGB)", 2D) = "white" {}
     }
         SubShader{
         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
         LOD 200
         Blend SrcAlpha One$$anonymous$$inusSrcAlpha
         Pass{
         Lighting On
         CGPROGRA$$anonymous$$
 
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc" 
 
     struct v2f
     {
         fixed4 pos : SV_POSITION;
         half4 color : COLOR0;
         half4 colorFog : COLOR1;
         float2 uv_$$anonymous$$ainTex : TEXCOORD0;
         half3 normal: TEXCOORD1;
     };
 
     float4 _$$anonymous$$ainTex_ST;
     uniform half4 unity_FogStart;
     uniform half4 unity_FogEnd;
 
     v2f vert(appdata_full v)
     {
         v2f o;
 
         //Vertex snapping
         float4 snapToPixel = UnityObjectToClipPos(v.vertex);
         float4 vertex = snapToPixel;
         vertex.xyz = snapToPixel.xyz / snapToPixel.w;
         vertex.x = floor(160 * vertex.x) / 160;
         vertex.y = floor(120 * vertex.y) / 120;
         vertex.xyz *= snapToPixel.w;
         o.pos = vertex;
 
 
         //o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
         //Vertex lighting 
         //o.color = float4(ShadeVertexLights(v.vertex, v.normal), 1.0);
         o.color = float4(ShadeVertexLightsFull(v.vertex, v.normal, 4, true), 1.0);
         o.color *= v.color;
 
         float distance = length(mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$V,v.vertex));
 
         //Affine Texture $$anonymous$$apping
         float4 affinePos = vertex;//vertex;                
         o.uv_$$anonymous$$ainTex = TRANSFOR$$anonymous$$_TEX(v.texcoord, _$$anonymous$$ainTex);
         o.uv_$$anonymous$$ainTex *= distance + (vertex.w*(UNITY_LIGHT$$anonymous$$O$$anonymous$$_A$$anonymous$$BIENT.a * 8)) / distance / 5;
         o.normal = distance + (vertex.w*(UNITY_LIGHT$$anonymous$$O$$anonymous$$_A$$anonymous$$BIENT.a * 8)) / distance / 5;
 
         //Fog
         float4 fogColor = unity_FogColor;
 
         float fogDensity = (unity_FogEnd - distance) / (unity_FogEnd - unity_FogStart);
         o.normal.g = fogDensity;
         o.normal.b = 1;
 
         o.colorFog = fogColor;
         o.colorFog.a = clamp(fogDensity,0,1);
 
         //Cut out polygons
         if (distance > unity_FogStart.z + unity_FogColor.a * 255)
         {
             o.pos.w = 0;
         }
 
 
         return o;
     }
 
     sampler2D _$$anonymous$$ainTex;
 
     float4 frag(v2f IN) : COLOR
     {
         half4 c = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex / IN.normal.r)*IN.color;
         half4 color = c*(IN.colorFog.a);
         color.rgb += IN.colorFog.rgb*(1 - IN.colorFog.a);
         color.a = c.a;
         return color;
     }
         ENDCG
     }
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MatheusCururu · Jul 17, 2019 at 02:49 AM

Hello Friend! Your post is from 2017 but I only saw it now. Your problem can be solved by removing the modifiers. The script will look like this...

 Shader "psx/vertexlit" {
     Properties{
         _MainTex("Base (RGB)", 2D) = "white" {}
     }
         SubShader{
             Tags { "RenderType" = "Opaque" }
             LOD 200
 
             Pass {
             Lighting On
                 CGPROGRAM
 
                     #pragma vertex vert
                     #pragma fragment frag
                     #include "UnityCG.cginc"
 
                     struct v2f
                     {
                         fixed4 pos : SV_POSITION;
                         half4 color : COLOR0;
                         half4 colorFog : COLOR1;
                         float2 uv_MainTex : TEXCOORD0;
                         half3 normal : TEXCOORD1;
                     };
 
                     float4 _MainTex_ST;
                     uniform half4 unity_FogStart;
                     uniform half4 unity_FogEnd;
 
                     v2f vert(appdata_full v)
                     {
                         v2f o;
 
                         //Vertex snapping
                         float4 snapToPixel = UnityObjectToClipPos(v.vertex);
                         float4 vertex = snapToPixel;
                         vertex.xyz = snapToPixel.xyz / snapToPixel.w;
                         vertex.x = floor(160 * vertex.x) / 160;
                         vertex.y = floor(120 * vertex.y) / 120;
                         vertex.xyz *= snapToPixel.w;
                         o.pos = vertex;
 
                         //Vertex lighting 
                     //    o.color =  float4(ShadeVertexLights(v.vertex, v.normal), 1.0);
                         o.color = float4(ShadeVertexLightsFull(v.vertex, v.normal, 4, true), 1.0);
                         o.color *= v.color;
 
                         float distance = length(mul(UNITY_MATRIX_MV,v.vertex));
 
                         //Affine Texture Mapping
                         float4 affinePos = vertex; //vertex;                
                         o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
                         o.normal = UNITY_LIGHTMODEL_AMBIENT.a;
 
                         //Fog
                         float4 fogColor = unity_FogColor;
 
                         float fogDensity = (unity_FogEnd - distance) / (unity_FogEnd - unity_FogStart);
                         o.normal.g = fogDensity;
                         o.normal.b = 1;
 
                         o.colorFog = fogColor;
                         o.colorFog.a = clamp(fogDensity,0,1);
 
                         //Cut out polygons
                         if (distance > unity_FogStart.z + unity_FogColor.a * 255)
                         {
                             o.pos.w = 0;
                         }
 
                         return o;
                     }
 
                     sampler2D _MainTex;
 
                     float4 frag(v2f IN) : COLOR
                     {
                         half4 c = tex2D(_MainTex, IN.uv_MainTex / IN.normal.r)*IN.color;
                         half4 color = c*(IN.colorFog.a);
                         color.rgb += IN.colorFog.rgb*(1 - IN.colorFog.a);
                         return color;
                     }
                 ENDCG
             }
     }
 }

Hope this helps.

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

78 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

Related Questions

Generating Textures in Shaders? 1 Answer

Shader global texture reset on scene load? 0 Answers

Weird shader dark spots when using normal maps 0 Answers

Texture distortion at poles on sphere - projecting texture on the inside of the spehre 0 Answers

How can I access a texture created through C# code (using Texture2D) in a unity shader as a sampler2D? 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