<shader compiler="xmlshader" name="splatting_scattering_base">
  <shadervars>
    <!-- beta1 = Rayleigh -->
    <!-- .2
    <shadervar name="beta' 1" type="vector3">8.3215e-06, 1.4072e-05, 2.9180e-05</shadervar>
    <shadervar name="beta' 1" type="vector3">3.3286e-05, 5.6288e-05, 1.1672e-04</shadervar>
    <shadervar name="beta' 1" type="vector3">4.1608e-06, 7.0360e-06, 1.4590e-05</shadervar>
    -->

    <?if vars."scatter sun color".w &gt; 0 ?>
      <shadervar name="beta' 1" type="expression">
	  <sexp>
(make-vector 
(* 0.64 (elt4 "scatter sun color") (elt1 "scatter color")) 
(* 0.64 (elt4 "scatter sun color") (elt2 "scatter color")) 
(* 0.64 (elt4 "scatter sun color") (elt3 "scatter color")) 
		0)
	  </sexp>
      </shadervar> 
    <?else?>
    <shadervar name="beta' 1" type="vector3">8.3215e-06, 1.4072e-05, 2.9180e-05</shadervar>
    <?endif?>

    <!-- beta2 = Mie -->
    <!-- .01
    <shadervar name="beta' 2" type="vector3">1.3338e-05, 1.7345e-05, 2.4976e-05</shadervar>
    <shadervar name="beta' 2" type="vector3">0.00026676, 0.00034689, 0.00049952</shadervar>
    <shadervar name="beta' 2" type="vector3">0.00066689, 0.00086723, 0.00124881</shadervar>
    -->

    <?if vars."scatter sun color".w &gt; 0 ?>
      <shadervar name="beta' 2" type="expression">
	  <sexp>
	    (make-vector 
		(/ (* 0.64 (elt4 "scatter sun color") (elt1 "scatter ground glow")) 100)
		(/ (* 0.64 (elt4 "scatter sun color") (elt2 "scatter ground glow")) 100)
		(/ (* 0.64 (elt4 "scatter sun color") (elt3 "scatter ground glow")) 100)
		0)
	  </sexp>
      </shadervar>
    <?else?>
    <shadervar name="beta' 2" type="vector3">1.3338e-05, 1.7345e-05, 2.4976e-05</shadervar>
    <?endif?>

    <!--
    <shadervar name="beta1 + beta2" type="vector3">0.00019683, 0.00030978, 0.00059405</shadervar>
    <shadervar name="beta1 + beta2" type="vector3">0.0017058, 0.0024231, 0.0040585</shadervar>
    <shadervar name="beta1 + beta2" type="vector3">0.0029400, 0.0038177, 0.0055016</shadervar>
    -->
     <?if vars."scatter sun color".w &gt; 0 ?>
      <shadervar name="beta1 + beta2" type="expression">
	  <sexp>
	    (make-vector 
		(/ (- 1.0001 (elt1 "scatter world color")) 100)
		(/ (- 1.0001 (elt2 "scatter world color")) 100)
		(/ (- 1.0001 (elt3 "scatter world color")) 100)
		0)
	  </sexp>
      </shadervar>
    <?else?>
    <shadervar name="beta1 + beta2" type="vector3">0.00019683, 0.00030978, 0.00059405</shadervar>
    <?endif?> 

    <!-- g = 0.8, hg = 1-g^2, 1+g, 2g -->
    <shadervar name="hg" type="vector3">0.36, 1.8, 1.6</shadervar>
    <!-- inscatter = .3 -->

    <?if vars."scatter sun color".w &gt; 0 ?>
      <shadervar name="inscattering term" type="expression">
	  <sexp>
	    (make-vector 
		(elt1 "scatter sun color")
		(elt2 "scatter sun color")
		(elt3 "scatter sun color")
		0)
	  </sexp>
      </shadervar>
    <?else?>
    <shadervar name="inscattering term" type="vector3">0.3, 0.3, 0.3</shadervar>
    <?endif?>
  </shadervars>

  <?if vars."tex normal"?>
    <technique priority="300">
      <pass>
	<buffer source="normal" destination="normal" />
	<buffer source="texture coordinate 0" destination="texture coordinate 0" />
	<vp plugin="glarb" file="/shader/scattering.avp" />
	<texture destination="unit 0" name="tex diffuse" />
	<texture destination="unit 1" name="tex normal" />
	<fp plugin="glarb" file="/shader/scattering_base.afp" />
      </pass>
    </technique>
  <?endif?>

  <technique priority="200">
    <pass>
      <mixmode><copy/></mixmode>
      <buffer source="normal" destination="normal" />
      <vp plugin="glarb" file="/shader/splatting_scattering_base.avp"/>
      <texture destination="unit 0" name="standardtex normalization map" />
      <texture destination="unit 1" name="standardtex normalization map" />
      <texture destination="unit 2" name="tex diffuse" />
      <texture destination="unit 3" name="standardtex normalization map" />
      <fp plugin="glfixed">
        <fixedfp>
	  <layer>
	    <colorsource num="0" source="texture" modifier="color"/>
	    <coloroperation operation="replace"/>
          </layer>
          <layer>
            <colorsource num="0" source="previous layer" modifier="color"/>
            <colorsource num="1" source="texture" modifier="color"/>
            <coloroperation operation="dot3"/>
          </layer>
          <layer>
            <colorsource num="0" source="previous layer" modifier="color"/>
            <colorsource num="1" source="texture" modifier="color"/>
            <coloroperation operation="modulate"/>
          </layer>
          <layer>
            <colorsource num="0" source="previous layer" modifier="color"/>
	    <colorsource num="1" source="primary color" modifier="color"/>
            <coloroperation operation="modulate"/>
          </layer>
        </fixedfp>
      </fp>
    </pass>
    <pass>
      <mixmode><add/></mixmode>
      <vp plugin="glarb" file="/shader/scattering.avp" />
      <texture destination="unit 0" name="tex diffuse" />
      <fp plugin="glfixed">
        <fixedfp>
	  <layer>
	    <colorsource num="0" source="primary color" modifier="color"/>
	    <coloroperation operation="replace"/>
	  </layer>
        </fixedfp>
      </fp>
    </pass>
  </technique>

  <technique priority="100">
    <pass>
      <mixmode><copy/></mixmode>
      <buffer source="normal" destination="normal" />
      <vp plugin="glarb" file="/shader/flat_ppl_diffuse_a.avp"/>
      <texture destination="unit 0" name="standardtex normalization map" />
      <texture destination="unit 1" name="standardtex normalization map" />
      <fp plugin="glfixed">
        <fixedfp>
	  <layer>
	    <colorsource num="0" source="texture" modifier="color"/>
	    <coloroperation operation="replace"/>
          </layer>
          <layer>
            <colorsource num="0" source="previous layer" modifier="color"/>
            <colorsource num="1" source="texture" modifier="color"/>
            <coloroperation operation="dot3"/>
          </layer>
        </fixedfp>
      </fp>
    </pass>
    <pass>
      <mixmode><multiply/></mixmode>
      <vp plugin="glarb" file="/shader/splatting_scattering_base_b.avp"/>
      <texture destination="unit 0" name="tex diffuse" />
      <fp plugin="glfixed">
        <fixedfp>
          <layer>
            <colorsource num="0" source="texture" modifier="color"/>
	    <colorsource num="1" source="primary color" modifier="color"/>
            <coloroperation operation="modulate"/>
          </layer>
        </fixedfp>
      </fp>
    </pass>
    <pass>
      <mixmode><add/></mixmode>
      <vp plugin="glarb" file="/shader/scattering.avp" />
      <texture destination="unit 0" name="tex diffuse" />
      <fp plugin="glfixed">
        <fixedfp>
	  <layer>
	    <colorsource num="0" source="primary color" modifier="color"/>
	    <coloroperation operation="replace"/>
	  </layer>
        </fixedfp>
      </fp>
    </pass>
  </technique>

  <fallbackshader name="terrain_fixed_base" file="terrain_fixed_base.xml" />

</shader>
