Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 plehmann1991 · Jun 10, 2020 at 07:12 AM · editortextureshaders

Textures set by Custom Shader only appear on Editor restart

For my procedural generated terrain I created a Terrain Generator, which generates Terrain Chunks on request. These Chunks use a custom Material powered by a custom Shader. The Custom Shader enables the use of colors and/or textures on different height levels, that can also blend into another on the edge between the height zones.

The shader works for now, but strangely enough when generating a new Terrain chunk, the terrain textures are not loaded. Only if I restart the Unity Editor, the textures are loaded and displayed as they are supposed to.

I honestly have no idea where this problem comes from. Following the code of the shader and two screenshots of the problem. Keep in mind that these are placeholder textures and the shader is work in progress.

 Shader "Custom/TerrainShader"
 {
     Properties
     {
         _Color ("Base Color", Color) = (1,1,1,1)
         _MainTex ("Texture Area 1 / Base", 2D) = "white" {}
         _HeightArea1("Border Height 1 to 2", Range(-100,100)) = 0.2
         _BlendArea1("Blend Area 1 to 2", Range(0,100)) = 0.5
         _Color2 ("Color2", Color) = (1,1,1,1)
         _MainTex2 ("Texture Area 2", 2D) = "white" {}
         _HeightArea2("Border Height 2 to 3", Range(-100,100)) = 30
         _BlendArea2("Blend Area 2 to 3", Range(0,100)) = 1
         _Color3 ("Color3", Color) = (1,1,1,1)
         _MainTex3 ("Texture Area 3", 2D) = "white" {}
         _BumpMap ("Bumpmap", 2D) = "bump" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 200
 
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         const static float epsilon = 1E-4;
 
         sampler2D _MainTex;
         sampler2D _MainTex2;
         sampler2D _MainTex3;
 
         struct Input {
             float3 worldPos;
             float3 worldNormal;
             float2 uv_MainTex;
             float2 uv_MainTex2;
             float2 uv_MainTex3;
             float2 uv_BumpMap;
         };
 
         float inverseLerp(float a, float b, float value) {
             return saturate((value-a)/(b-a));
         }
 
        
         half _Glossiness;
         half _Metallic;
         fixed4 _Color;
         fixed4 _Color2;
         fixed4 _Color3;
         half _BlendArea1;
         half _BlendArea2;
         half _HeightArea1;
         half _HeightArea2;
         sampler2D _BumpMap;
 
 
         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
         // #pragma instancing_options assumeuniformscaling
         UNITY_INSTANCING_BUFFER_START(Props)
             // put more per-instance properties here
         UNITY_INSTANCING_BUFFER_END(Props)
        
         void surf (Input IN, inout SurfaceOutputStandard o)
         {
         
             float3 blendAxes = abs(IN.worldNormal);
             blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;
 
             // first area, default
 
             fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
             o.Albedo = c.rgb;
             
             // border 1
             if (IN.worldPos.y > _HeightArea1-_BlendArea1 && IN.worldPos.y < _HeightArea1+_BlendArea1/2){
                float heightPercent = inverseLerp(_HeightArea1-_BlendArea1/2,_HeightArea1+_BlendArea1/2, IN.worldPos.y);
                float drawStrength = inverseLerp(-_BlendArea1/2, _BlendArea1/2, heightPercent);
                o.Albedo = lerp(tex2D (_MainTex, IN.uv_MainTex) * _Color , tex2D (_MainTex2, IN.uv_MainTex2) * _Color2, heightPercent);
             }
 
             // second area
             if (IN.worldPos.y > _HeightArea1+_BlendArea1/2 && IN.worldPos.y < _HeightArea2-_BlendArea2/2){
                fixed4 c = tex2D (_MainTex2, IN.uv_MainTex2) * _Color2;
                o.Albedo = c.rgb;
                o.Alpha = c.a;
             }
 
             // second border
             if (IN.worldPos.y > _HeightArea2-_BlendArea2 && IN.worldPos.y < _HeightArea2+_BlendArea2/2){
                float heightPercent = inverseLerp(_HeightArea2-_BlendArea2/2,_HeightArea2+_BlendArea2/2, IN.worldPos.y);
                o.Albedo = lerp(tex2D (_MainTex2, IN.uv_MainTex2) * _Color2 , tex2D (_MainTex3, IN.uv_MainTex3) * _Color3, heightPercent);
             }
 
             // third area
             if (IN.worldPos.y > _HeightArea2+_BlendArea2/2){
                fixed4 c = tex2D (_MainTex3, IN.uv_MainTex3) * _Color3;
                o.Albedo = c.rgb;
                o.Alpha = c.a;
             }
 
             // Metallic and smoothness come from slider variables
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
         }
         ENDCG
     }
     FallBack "Diffuse"
 }



When generating Terrain/Applying the Material&Shader alt text

When restarting Unity alt text

bildschirmfoto-2020-06-10-um-090645.jpg (84.5 kB)
bildschirmfoto-2020-06-10-um-090537.jpg (185.9 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

282 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 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

Problem with a transparent texture in an object that already has a material 0 Answers

Shader/Lighting Problem! Terrain texture looks awful. Please Help 1 Answer

HDRP, terrain and layer count, 0 Answers

Farther away object turns green 0 Answers

Calculating normals in 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