Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

unity3d - Build crash with custom shader, Unity.exe access violation

Trying to compile my project with custom shaders results in the Unity IDE crashing during build process.

I've got two custom shaders for terrain that seems to be the root of the problem.

What I've done:

  • Checked error logs, reports access violation in Unity.exe, with stacktrace pointing to BuildPlayerRenderPipeline
  • Changed shader of terrains to Nature->Terrain->Diffuse. This works fine.
  • Run Unity Editor as admin, no change.
  • No error codes on Shader code in Editor

See the attached (most critical) shader below. The second one is very similar.

Shader "Custom/HueTerrainShader"
{
    Properties
    {
        _MainTex("Base (RGB)", 2D) = "white" {}
        _Color("Alpha Color Key", Color) = (0,0,0,1)
        _HeightMin("Height Min", Float) = -1
        _HeightMax("Height Max", Float) = 100
        _HSVRangeMin("HSV Affect Range Min", Range(0, 1)) = 0
        _HSVRangeMax("HSV Affect Range Max", Range(0, 1)) = 1
        _HSVAAdjust("HSVA Adjust", Vector) = (0, 0, 0, 0)
        _Opacity("Opacity", Float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        //LOD 200

        CGPROGRAM
        #pragma surface surf Lambert alpha

        sampler2D _MainTex;
        uniform float _HeightMin;
        uniform float _HeightMax;
        float4 _Color;
        float _HSVRangeMin;
        float _HSVRangeMax;
        float4 _HSVAAdjust;
        float _Opacity;

        struct Input
        {
            float2 uv_MainTex;
            float3 worldPos;
        };

        float3 rgb2hsv(float3 c) {
            float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
            float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
            float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));

            float d = q.x - min(q.w, q.y);
            float e = 1.0e-10;
            return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
        }

        float3 hsv2rgb(float3 c) {
            c = float3(c.x, clamp(c.yz, 0.0, 1.0));
            float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
            float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
            return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
        }

        void surf(Input IN, inout SurfaceOutput o)
        {
            /*
            Hue vs h values: 
            Red: hue = 0, hsv.x = 0, h = 1 --> highest point on map
            Blue: hue = 240, hsv.x = 240/360, h = 0 --> lowest point on map
            Everything in between, go from hue = 240 -> 0, h = 0 -> 1, hsv.x = (1 - h)(*240/360)

            */
            float3 hsv = rgb2hsv(_Color.rgb);
            float affectMult = step(_HSVRangeMin, hsv.r) * step(hsv.r, _HSVRangeMax);
            float h = (_HeightMax - IN.worldPos.y) / (_HeightMax);
            hsv.y += _HSVAAdjust.y;
            hsv.z += _HSVAAdjust.z;
            hsv.x = h*(240.0f / 360.0f);
            float3 rgb = hsv2rgb(hsv);

            o.Albedo = rgb;
            o.Alpha = _Opacity;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
question from:https://stackoverflow.com/questions/65945171/build-crash-with-custom-shader-unity-exe-access-violation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...