1
\$\begingroup\$

I am trying to recreate Drilian's lightning bolt in unity and I have faced a problem in the "Adding Some Glow" part. I am missing glow effect (and/ or "rounded beams").

Here is what I am doing:

I create 3 quads for each segment as it is stated in the original post and here and then I render the whole lightning bolt with a shader as it did drilian(he said this in the comment section:

I changed from rendering each segment of lightning as its own separate rounded-off segment (as explained above) to rendering an entire bolt as a single strip

).

Here is the gl code:

 private void OnRenderObject()
 {
        GL.PushMatrix();
        GL.LoadOrtho();

        _material.SetPass(0);

        for (var i = 0; i < _segments.Count; i++)
        {
            GL.Begin(GL.QUADS);
            var seg = _segments[i];
            var normal = seg.Normal() * seg.width / 8;

            GL.Vertex(seg.p0 / 8 + normal);
            GL.TexCoord(new Vector3(0, 0));

            GL.Vertex(seg.p0 / 8);
            GL.TexCoord(new Vector3(0.5f, 0));

            GL.Vertex(seg.p1 / 8);
            GL.TexCoord(new Vector3(0.5f, 0));

            GL.Vertex(seg.p1 / 8 + normal);
            GL.TexCoord(new Vector3(1f, 0));
            //GL.End();

            //GL.Begin(GL.QUADS);
            GL.Vertex(seg.p0 / 8);
            GL.TexCoord(new Vector3(0, 1));

            GL.Vertex(seg.p0 / 8 - normal);
            GL.TexCoord(new Vector3(0.5f, 1));

            GL.Vertex(seg.p1 / 8 - normal);
            GL.TexCoord(new Vector3(0.5f, 1));

            GL.Vertex(seg.p1 / 8);
            GL.TexCoord(new Vector3(1f, 1));
            GL.End();
        }

        GL.Begin(GL.TRIANGLE_STRIP);
        for (var i = 0; i < _segments.Count; i++)
        {
            var seg = _segments[i];
            var normal = seg.Normal() * 0.0001f;
            GL.Vertex(seg.p0 / 8 - normal);
            GL.Vertex(seg.p0 / 8 + normal);
            GL.Vertex(seg.p1 / 8 - normal);
            GL.Vertex(seg.p1 / 8 + normal);
        }
        GL.End();

        GL.PopMatrix();
  }

Shader:

Shader "Custom/Lightning 1" {
    Properties {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Color ("Ambient", Color) = (1, 1, 1, 1)
    }
    SubShader {
        Tags { "Queue"="Transparent" }
        ZWrite Off
        //Blend SrcAlpha OneMinusSrcAlpha
        Blend One One
        //BlendOp Max

        Pass {
            CGPROGRAM
            #pragma target 2.0
            #pragma exclude_renderers ps3 xbox360 flash
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            uniform sampler2D _MainTex;
            uniform float4 _MainTex_ST;
            uniform fixed4 _Color;

            struct vertexInput {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };

            struct fragmentInput {
                float4 pos : SV_POSITION;
                half2 uv : TEXCOORD0;
            };

            fragmentInput vert(vertexInput i) {
                fragmentInput o;

                o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
                o.uv = TRANSFORM_TEX(i.vertex, _MainTex);

                return o;
            }

            fixed4 frag(fragmentInput i) : COLOR {

                fixed4 light = tex2D(_MainTex, i.uv);
                light.x += 0.5;
                return light * _Color;
            }


            ENDCG
        }
    } 
    FallBack "Diffuse"
}

Glow texture(actually I am using transparent one, but it is not visible here): enter image description here

Result: result

Wanted result: enter link description here http://drilian.com/wp-content/uploads/2009/02/lightningtest-463x270.jpg

And I don't quietly understand what he's talking about here:

In the case of the glow texture, it was, at the time,just a gradient based on distance from the center (so it looked like a circle). The trick was, the segments were rendered as triples of quads. The middle quad was the line (which stretched the center of the texture out). That is, from left to right, the U coordinates on the vertices went 0, 0.5, 0.5, 1…repeating the middle is what makes it a beam. The outer two quads exist to display the rest of the texture to make it appear rounded. The texture coordinates were basically:

(0, 0)……..(0.5, 0)……..(0.5, 0)……..(1, 0)

(0, 1)……..(0.5, 1)……..(0.5, 1)……..(1, 1)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.