1
\$\begingroup\$

I am just starting with shader programming and after reading a bit, I tried creating a shader that is simulating the flow of lava on rocks. Got a placeholder rock texture from internet and everything, however the end result is obviously bad. How can I simulate a flow that moves in the cracks between the rocks? My first idea was to somehow get the color of the cracks and change it to red. Then to animate it by just moving the texture with the Panner node.

Right now it just drags the bubbles texture and returns it.

Here is my shader code (generated by Shader Forge) :

 // Shader created with Shader Forge v1.38 
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.38;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,lico:1,lgpr:1,limd:0,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:False,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.2426471,fgcg:0.2337262,fgcb:0.2337262,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,atwp:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:3138,x:33559,y:32494,varname:node_3138,prsc:2|emission-2871-OUT;n:type:ShaderForge.SFN_Tex2d,id:7055,x:32587,y:32402,ptovrint:False,ptlb:Bubbles,ptin:_Bubbles,varname:_Bubbles,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:5735196e5b92b7f4491fd8c4988c9271,ntxv:0,isnm:False|UVIN-2523-UVOUT;n:type:ShaderForge.SFN_Color,id:4099,x:32361,y:33099,ptovrint:False,ptlb:node_4099,ptin:_node_4099,varname:_node_4099,prsc:0,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:0.2279412,c3:0.2279412,c4:0.5;n:type:ShaderForge.SFN_Time,id:156,x:32825,y:33195,varname:node_156,prsc:0;n:type:ShaderForge.SFN_Sin,id:7354,x:32982,y:33195,varname:node_7354,prsc:2|IN-156-T;n:type:ShaderForge.SFN_RemapRange,id:1566,x:33042,y:32932,varname:node_1566,prsc:2,frmn:-1,frmx:1,tomn:0.2,tomx:0.6|IN-7354-OUT;n:type:ShaderForge.SFN_Multiply,id:5363,x:33083,y:32739,varname:node_5363,prsc:2|A-1380-OUT,B-1566-OUT;n:type:ShaderForge.SFN_Panner,id:2523,x:32373,y:32402,varname:vvvvvvvvvvvvvvvvvvvvvv,prsc:2,spu:0,spv:1|UVIN-2113-UVOUT,DIST-3406-OUT;n:type:ShaderForge.SFN_TexCoord,id:2113,x:32111,y:32402,varname:node_2113,prsc:2,uv:0,uaff:False;n:type:ShaderForge.SFN_Time,id:9149,x:31788,y:32700,varname:node_9149,prsc:0;n:type:ShaderForge.SFN_Sin,id:8507,x:31886,y:32962,varname:node_8507,prsc:2|IN-9149-T;n:type:ShaderForge.SFN_RemapRange,id:3406,x:32223,y:32638,varname:node_3406,prsc:2,frmn:-1,frmx:1,tomn:0.1,tomx:0.3|IN-8507-OUT;n:type:ShaderForge.SFN_Tex2d,id:86,x:32361,y:32859,ptovrint:False,ptlb:MainTex,ptin:_MainTex,varname:_MainTex,prsc:0,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:5032107f8d981f2409d4adf0dd38b065,ntxv:0,isnm:False;n:type:ShaderForge.SFN_OneMinus,id:6833,x:32536,y:32876,varname:node_6833,prsc:0|IN-86-RGB;n:type:ShaderForge.SFN_Multiply,id:1380,x:32680,y:33027,varname:node_1380,prsc:2|A-6833-OUT,B-4099-RGB,C-4099-A;n:type:ShaderForge.SFN_Add,id:2871,x:33086,y:32458,varname:node_2871,prsc:2|A-5363-OUT,B-1603-OUT;n:type:ShaderForge.SFN_Multiply,id:1603,x:32821,y:32391,varname:node_1603,prsc:2|A-7055-RGB,B-1630-OUT;n:type:ShaderForge.SFN_ValueProperty,id:1630,x:32619,y:32692,ptovrint:False,ptlb:Alpha,ptin:_Alpha,varname:_Alpha,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:0;proporder:7055-4099-86-1630;pass:END;sub:END;*/

Shader "Shader Forge/lavaFlow" {
    Properties {
        _Bubbles ("Bubbles", 2D) = "white" {}
        _node_4099 ("node_4099", Color) = (1,0.2279412,0.2279412,0.5)
        _MainTex ("MainTex", 2D) = "white" {}
        _Alpha ("Alpha", Float ) = 0
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }


            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define UNITY_PASS_FORWARDBASE
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma only_renderers d3d9 d3d11 glcore gles 
            #pragma target 3.0
            uniform sampler2D _Bubbles; uniform float4 _Bubbles_ST;
            uniform fixed4 _node_4099;
            uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
            uniform float _Alpha;
            struct VertexInput {
                float4 vertex : POSITION;
                float2 texcoord0 : TEXCOORD0;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.uv0 = v.texcoord0;
                o.pos = UnityObjectToClipPos( v.vertex );
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
////// Lighting:
////// Emissive:
                fixed4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
                fixed4 node_156 = _Time;
                fixed4 node_9149 = _Time;
                float2 vvvvvvvvvvvvvvvvvvvvvv = (i.uv0+(sin(node_9149.g)*0.1+0.2)*float2(0,1));
                float4 _Bubbles_var = tex2D(_Bubbles,TRANSFORM_TEX(vvvvvvvvvvvvvvvvvvvvvv, _Bubbles));
                float3 emissive = ((((1.0 - _MainTex_var.rgb)*_node_4099.rgb*_node_4099.a)*(sin(node_156.g)*0.2+0.4))+(_Bubbles_var.rgb*_Alpha));
                float3 finalColor = emissive;
                return fixed4(finalColor,1);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}

P.S. I decided to add the Bubbles texture, so I can at least try to animate the move of the flow.

Edit:

GIF Link to imgur

The colors are broken after the upload, however the gif shows the movement. The circle things are the "bubbles" texture that I animated with the shader. The red rocks behind is the rock texture.

\$\endgroup\$
  • \$\begingroup\$ It's hard to imagine how your shader looks just from looking at your sourcecode. Could you post a screenshot or video and then describe which aspects of it you like and which you dislike? \$\endgroup\$ – Philipp Jun 1 '18 at 8:47
  • \$\begingroup\$ @Philipp I added a gif. Hopefully it will clear things out. I wanted to make the lava flow between the cracks of the rocks. :) \$\endgroup\$ – Kristian Kostov Jun 1 '18 at 9:03
2
\$\begingroup\$

I don't use Shaderforge, so i cannot help in telling you how to set it up. However, i can show you in code and explain what you could do.

You should be able to use another texture exactly the same as the rock texture except its black and white (so obviously not exactly the same...), where you want the bubbles to be is white and black wont show any bubbles. This texture can then be used as a mask in the shader above by doing the following:

return fixed4(finalColor,1) * MaskTexture.r;

If MaskTexture contains the black and white texture it works because in a texture black = 0,0,0, white is 1,1,1 and obivously multiplying the bubbles by 0 = no bubbles, and multiplying it by 1 = bubbles!. You could even use the rocks texture itself by using its alpha channel. This means the area where the bubbles are supposed to show is transparent in the texture itself, and then you do this:

return fixed4(finalColor,1) * RockTexture.a;

This does mean you cannot use the alpha channel for anything else but, this is better optimized because it doesnt require a third texture.

Alternatively you could create 2 meshes, 1 for the rock, and 1 for the lava flow. The upside of this is that you could make the lava seemingly change direction and follow the cracks direction when moving trough the cracks (like real lava would) if the uv of the mesh for the lava is setup correctly. The texture moves along the uv axis, so any change in the direction of the line in the mesh will determine the direction of the flow as long as the uv's don't follow the same line, as displayed below. The left side of the image below shows the uv layout, the right the actual line. As you can see the bubble image could simply move up on the UV layout, and on the mesh it would appear to change direction.

A single line for a Lava flow mesh (example): enter image description here

\$\endgroup\$
  • \$\begingroup\$ It works far better now! Thanks! I created a B&W texture of the rocks and painted white a part of it. Then added that B&W texture to the red glowing lava I had already done with the multiply node and then added that to the normal texture. Looks great now. imgur.com/a/QN98T71 \$\endgroup\$ – Kristian Kostov Jun 1 '18 at 10:26
  • \$\begingroup\$ @KristianKostov result is good but anyway If you need realistic lava you can use flow map \$\endgroup\$ – Seyed Morteza Kamali Jun 1 '18 at 10:44
  • \$\begingroup\$ @SeyedMortezaKamali I will look into that. As you can see I am pretty new to both shaders and Shader Forge :) \$\endgroup\$ – Kristian Kostov Jun 1 '18 at 10:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.