Difference between revisions of "ShaderTerrain"

From SimsWiki
Jump to: navigation, search
(EP3-OFB)
(EP4-PETS)
Line 1: Line 1:
 
<pre>
 
<pre>
  
EP3-OFB
+
EP4-Pets
 
0xCD7FE87A
 
0xCD7FE87A
 
0x1C0532FA
 
0x1C0532FA
Line 26: Line 26:
 
setf  paintTextureScale                  20
 
setf  paintTextureScale                  20
 
setf  paintDetailTextureScale            4.0
 
setf  paintDetailTextureScale            4.0
 +
setb  hasCutout false
  
 
#==============================================================================
 
#==============================================================================
  
define TerrainCanvasShaderShader()
+
define TerrainCanvasShaderShaderVS_PS()
   shader -layer (($terrainMatLayer - 3) * 8)
+
   pass
       vertexFormatPred position     0 true
+
       fillmode $stdMatFillMode      
       vertexFormatPred normal        0 true
+
       depthTest true -enableDepthWrite true                
       vertexFormatPred texcoord      0 true
+
        
       vertexFormatPred blendindices  0 false
+
       if ($hasCutout)                       
       vertexFormatPred targetindices 0 false
+
        depthTestFunction acceptIfEqual
       viewerRenderTypePred viewerRenderType
+
       else
 
+
        depthTestFunction acceptIfLessOrEqual
       pass -fixedFunction
+
       endif
         create LightingStatesNoStdLights()
+
     
        fillmode $stdMatFillMode
+
       shaderProgram -target vertexProgram -method compile -version 1_1
 
+
         bindConstants 0 -bindingID geomToClip -constantCount 4
        if ($stdMatLightingDebug)
+
bindConstants 4 -bindingID immediateData -data (1/$canvasBaseTextureScale, 1/$canvasDetailTextureScale, 0, 0)
            stage
+
              texture $stdMatBaseTextureName ${stdMatBaseTextureParam}
+
shaderSource
              textureBlend multiply(texture outRegister) select(outRegister)
+
  float4x4 clipSpaceMatrix  : register(c0);
            end  
+
  float4  textureConstants : register(c4);
         else
+
             stage
+
struct InputVertex
              texture "${texture}"
+
{
               textureAddressing tile tile
+
float3 position: POSITION0;
               ffTextureMatrix -scalev ($canvasBaseTextureScale, $canvasBaseTextureScale) -invert
+
float2 texcoord : TEXCOORD0;
               ffTextureCoordsSource 0
+
};
               textureBlend multiply(texture outRegister) select(outRegister)
+
             end  
+
struct OutputVertex
            stage
+
{
              texture "${texture}_detail"
+
float4 clipPosition : POSITION;
              textureAddressing tile tile
+
float2 txBase  : TEXCOORD0;
              ffTextureMatrix -scalev ($canvasDetailTextureScale, $canvasDetailTextureScale) -invert
+
float2 txDetail: TEXCOORD1;
              ffTextureCoordsSource 0
+
};
              textureBlend lerpTextureAlpha(texture outRegister) select(outRegister)
+
            end
+
OutputVertex VertexMain( InputVertex i)
         endif
+
{
       end      
+
  OutputVertex o;
 +
  o.clipPosition = mul(float4(i.position, 1), clipSpaceMatrix);
 +
  o.txBase  = i.texcoord * textureConstants.x;
 +
  o.txDetail = i.texcoord * textureConstants.y;  
 +
  return o;
 +
}
 +
endShaderSource 
 +
      end # end shader program
 +
     
 +
     
 +
      shaderProgram -target pixelProgram -method compile -version 1_1     
 +
         shaderSource                           
 +
             sampler base;
 +
            sampler detail;
 +
            struct cInputPixel
 +
            {              
 +
               float2 tcBase : TEXCOORD0;
 +
               float2 tcDetail : TEXCOORD1;
 +
            };
 +
           
 +
            float4 PixelMain(cInputPixel pi) : COLOR
 +
            {       
 +
              float4 baseColor  = tex2D(base, pi.tcBase);                                               
 +
               float4 detailColor = tex2D(detail, pi.tcDetail);                             
 +
               float4 result = (detailColor.a * detailColor) + ((1 - detailColor.a) * baseColor);             
 +
              return result;                       
 +
             }
 +
        endShaderSource
 +
      end # end shader program     
 +
       
 +
      sampler base
 +
        texture "${texture}"
 +
        textureAddressing tile tile
 +
      end
 +
     
 +
      sampler detail
 +
        texture "${texture}_detail"
 +
         textureAddressing tile tile
 +
       end    
 
   end
 
   end
 
enddef
 
enddef
  
 +
define TerrainCanvasShaderShaderFF()
 +
  pass -fixedFunction       
 +
      fillmode $stdMatFillMode
 +
     
 +
      depthTest true -enableDepthWrite true                 
 +
     
 +
      if ($hasCutout)                       
 +
        depthTestFunction acceptIfEqual
 +
      else
 +
        depthTestFunction acceptIfLessOrEqual
 +
      endif
 +
             
 +
      stage
 +
        texture "${texture}"
 +
        textureAddressing tile tile
 +
        ffTextureMatrix -scalev ($canvasBaseTextureScale, $canvasBaseTextureScale) -invert
 +
        ffTextureCoordsSource 0
 +
        textureBlend select(texture) select(texture)
 +
      end
 +
      stage
 +
        texture "${texture}_detail"
 +
        textureAddressing tile tile
 +
        ffTextureMatrix -scalev ($canvasDetailTextureScale, $canvasDetailTextureScale) -invert
 +
        ffTextureCoordsSource 0
 +
        textureBlend lerpTextureAlpha(texture outRegister) select(outRegister)
 +
      end
 +
  end     
 +
enddef
  
 
define TerrainCanvasShader()
 
define TerrainCanvasShader()
 
   material
 
   material
       if ($stdMatLightingDebug)
+
       shader -layer (($terrainMatLayer - 3) * 8)
         create LightingDebugStandardMaterialOverrides()
+
        vertexFormatPred position      0 true
      endif
+
        vertexFormatPred normal        0 true
      create TerrainCanvasShaderShader()
+
        vertexFormatPred texcoord      0 true
 +
        viewerRenderTypePred viewerRenderType
 +
       
 +
         create DetermineHardwareSupport()
 +
        if (viewerRenderType = $kRenderTypeShadowMap)
 +
        create ShadowMapVS_PS()
 +
      else
 +
            if ($useHWShader1Path or $useHWShader2Path)
 +
        create TerrainCanvasShaderShaderVS_PS()
 +
        else 
 +
              create TerrainCanvasShaderShaderFF()
 +
            endif
 +
        endif
 +
      end
 +
     
 
       shader
 
       shader
 
       end
 
       end
 
   end
 
   end
 
enddef
 
enddef
 
 
  
 
#==============================================================================
 
#==============================================================================
 +
 +
define TerrainPaintShaderVS_PS()
 +
  shader -layer (($terrainMatLayer - 2) * 8)
 +
      vertexFormatPred position      0 true
 +
      vertexFormatPred texcoord      0 true
 +
      pass
 +
        fillmode $stdMatFillMode
 +
        alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
 +
       
 +
        alphaTest true 10
 +
        alphaTestFunction acceptIfGreater
 +
       
 +
        depthTest true -enableDepthWrite false
 +
        if (viewerRenderType = $kRenderTypeImposter)
 +
            depthTestFunction acceptIfLessOrEqual
 +
        else
 +
            depthTestFunction acceptIfEqual
 +
        endif
 +
       
 +
        shaderProgram -target vertexProgram -method compile -version 1_1
 +
            bindConstants 0 -bindingID geomToClip -constantCount 4
 +
  bindConstants 4 -bindingID immediateData -data (1/$canvasBaseTextureScale, 1/$canvasDetailTextureScale, $alphaMapScaleU, $alphaMapScaleV)
 +
 
 +
  shaderSource
 +
      float4x4 clipSpaceMatrix  : register(c0);
 +
      float4  textureConstants : register(c4);
 +
 
 +
  struct InputVertex
 +
  {
 +
  float3 position: POSITION0;
 +
  float2 texcoord : TEXCOORD0;
 +
  };
 +
 
 +
  struct OutputVertex
 +
  {
 +
  float4 clipPosition : POSITION;
 +
  float2 txBase  : TEXCOORD0;
 +
  float2 txDetail: TEXCOORD1;
 +
  float2 txAlpha : TEXCOORD2;
 +
  };
 +
 
 +
  OutputVertex VertexMain( InputVertex i)
 +
  {
 +
      OutputVertex o;
 +
      o.clipPosition = mul(float4(i.position, 1), clipSpaceMatrix);
 +
      o.txBase  = i.texcoord * textureConstants.x;
 +
      o.txDetail = i.texcoord * textureConstants.y;  
 +
      o.txAlpha  = i.texcoord.xy * textureConstants.zw;
 +
      return o;
 +
  }
 +
  endShaderSource 
 +
        end # end shader program
 +
       
 +
       
 +
        shaderProgram -target pixelProgram -method compile -version 1_1     
 +
            shaderSource                           
 +
              sampler base;
 +
              sampler detail;
 +
              sampler alphaMap;
 +
              struct cInputPixel
 +
              {             
 +
                  float2 tcBase : TEXCOORD0;
 +
                  float2 tcDetail : TEXCOORD1;
 +
                  float2 tcAlpha  : TEXCOORD2;
 +
              };
 +
             
 +
              float4 PixelMain(cInputPixel pi) : COLOR
 +
              {       
 +
                  float4 baseColor  = tex2D(base, pi.tcBase);                                               
 +
                  float4 detailColor = tex2D(detail, pi.tcDetail);                             
 +
                  float4 alphaColor  = tex2D(alphaMap, pi.tcAlpha);
 +
                  float4 result = (detailColor.a * detailColor) + ((1 - detailColor.a) * baseColor);             
 +
                  result.a = alphaColor.a;
 +
                  return result;                       
 +
              }
 +
            endShaderSource
 +
        end # end shader program     
 +
           
 +
        sampler base
 +
            texture $paintTexture
 +
            textureAddressing tile tile
 +
        end
 +
       
 +
        sampler detail
 +
            texture "${paintTexture}_detail"
 +
            textureAddressing tile tile
 +
        end
 +
       
 +
        sampler alphaMap
 +
            texture $alphaMap
 +
            textureAddressing clamp clamp
 +
        end     
 +
      end
 +
  end
 +
enddef
  
 
define TerrainPaintShader(addDetail)
 
define TerrainPaintShader(addDetail)
 
   shader -layer (($terrainMatLayer - 2) * 8)
 
   shader -layer (($terrainMatLayer - 2) * 8)
 
       vertexFormatPred position      0 true
 
       vertexFormatPred position      0 true
      vertexFormatPred normal        0 true
 
 
       vertexFormatPred texcoord      0 true
 
       vertexFormatPred texcoord      0 true
      vertexFormatPred blendindices  0 false
 
      vertexFormatPred targetindices 0 false
 
      #viewerRenderTypePred viewerRenderType
 
 
        
 
        
       pass -fixedFunction
+
       pass -fixedFunction        
        create LightingStatesNoStdLights()
+
 
         fillmode $stdMatFillMode
 
         fillmode $stdMatFillMode
  
Line 101: Line 270:
 
         alphaTestFunction acceptIfGreater
 
         alphaTestFunction acceptIfGreater
 
          
 
          
         if ($stdMatLightingDebug)
+
        depthTest true -enableDepthWrite false
 +
        depthTestFunction acceptIfEqual
 +
       
 +
         if (&addDetail)
 
             stage
 
             stage
               texture $stdMatBaseTextureName ${stdMatBaseTextureParam}
+
               texture $paintTexture
               textureBlend multiply(texture outRegister) select(outRegister)
+
              textureAddressing tile tile
 +
              ffTextureMatrix -scalev ($paintTextureScale, $paintTextureScale) -invert
 +
              textureTransformType vector2
 +
              ffTextureCoordsSource 0
 +
              textureBlend select(texture) select(outRegister)
 +
            end
 +
            stage
 +
              texture "${paintTexture}_detail"
 +
              textureAddressing tile tile
 +
              ffTextureMatrix -scalev ($paintDetailTextureScale, $paintDetailTextureScale) -invert
 +
              textureTransformType vector2
 +
              ffTextureCoordsSource 0
 +
               textureBlend lerpTextureAlpha(texture outRegister) select(outRegister)
 
             end  
 
             end  
 
         else
 
         else
             if (&addDetail)
+
             stage
               stage
+
               texture $paintTexture
                  texture $paintTexture
+
              textureAddressing tile tile
                  textureAddressing tile tile
+
              ffTextureMatrix -scalev ($paintTextureScale, $paintTextureScale) -invert
                  ffTextureMatrix -scalev ($paintTextureScale, $paintTextureScale) -invert
+
              textureTransformType vector2
                  textureTransformType vector2
+
               ffTextureCoordsSource 0
                  ffTextureCoordsSource 0
+
               textureBlend select(texture) select(outRegister)
                  textureBlend select(texture) select(outRegister)
+
            end  
               end
+
              stage
+
                  texture "${paintTexture}_detail"
+
                  textureAddressing tile tile
+
                  ffTextureMatrix -scalev ($paintDetailTextureScale, $paintDetailTextureScale) -invert
+
                  textureTransformType vector2
+
                  ffTextureCoordsSource 0
+
                  textureBlend lerpTextureAlpha(texture outRegister) select(outRegister)
+
               end
+
            else
+
              stage
+
                  texture $paintTexture
+
                  textureAddressing tile tile
+
                  ffTextureMatrix -scalev ($paintTextureScale, $paintTextureScale) -invert
+
                  textureTransformType vector2
+
                  ffTextureCoordsSource 0
+
                  textureBlend select(texture) select(outRegister)
+
              end  
+
            endif
+
 
         endif
 
         endif
 
         stage
 
         stage
Line 149: Line 314:
 
define TerrainPaint()
 
define TerrainPaint()
 
   material
 
   material
       if ($stdMatLightingDebug)
+
      create DetermineHardwareSupport()
         create LightingDebugStandardMaterialOverrides()
+
     
      endif
+
       if (viewerRenderType != $kRenderTypeShadowMap)  
      create TerrainPaintShader(true)
+
         if ($useHWShader1Path or $useHWShader2Path)
      create TerrainPaintShader(false)
+
      create TerrainPaintShaderVS_PS()
      shader
+
      else           
      end
+
            create TerrainPaintShader(true)
 +
            create TerrainPaintShader(false)
 +
            shader
 +
            end
 +
        endif     
 +
      endif     
 
   end
 
   end
 
enddef
 
enddef
Line 161: Line 331:
  
 
#==============================================================================
 
#==============================================================================
 +
define TerrainLightingVS_PS()
 +
  pass
 +
      fillmode $stdMatFillMode
 +
      alphaBlend srcFactor(destColor) add dstFactor(zero)           
 +
      depthTest true -enableDepthWrite false           
 +
      depthTestFunction acceptIfEqual
 +
     
 +
      shaderProgram -target vertexProgram -method compile -version 1_1
 +
        bindConstants 0 -bindingID geomToClip -constantCount 4
 +
bindConstants 4 -bindingID immediateData -data (1/64, 0, 0, 0)
 +
 +
shaderSource
 +
  float4x4 clipSpaceMatrix  : register(c0);
 +
  float4  textureConstants : register(c4);  
 +
 +
struct InputVertex
 +
{
 +
float3 position: POSITION0;
 +
float2 texcoord : TEXCOORD0;
 +
};
 +
 +
struct OutputVertex
 +
{
 +
float4 clipPosition : POSITION;
 +
float2 txBase  : TEXCOORD0;
 +
};
 +
 +
OutputVertex VertexMain( InputVertex i)
 +
{
 +
  OutputVertex o;
 +
  o.clipPosition = mul(float4(i.position, 1), clipSpaceMatrix);
 +
  o.txBase  = i.texcoord * textureConstants.xx;  
 +
  return o;
 +
}
 +
endShaderSource 
 +
      end # end shader program
 +
     
 +
     
 +
      shaderProgram -target pixelProgram -method compile -version 1_1
 +
        bindConstants 0 -bindingID immediateData -data ($terrainMatDiffCoef.x, $terrainMatDiffCoef.y, $terrainMatDiffCoef.z, 1)       
 +
       
 +
        shaderSource 
 +
            float4  terrainDiffCoef  : register(c0);
 +
                                   
 +
            sampler base;           
 +
            struct cInputPixel
 +
            {             
 +
              float2 tcBase : TEXCOORD0;             
 +
            };
 +
           
 +
            float4 PixelMain(cInputPixel pi) : COLOR
 +
            {       
 +
              float4 baseColor  = tex2D(base, pi.tcBase);                                                             
 +
              float4 result = 2.0*(baseColor*terrainDiffCoef);             
 +
              result.a = terrainDiffCoef.w;
 +
              return result;                       
 +
            }
 +
        endShaderSource
 +
      end # end shader program     
 +
       
 +
      sampler base
 +
        texture "floorLightMap_0"  # at the moment is deterministically mapped to page 0
 +
        textureAddressing tile tile
 +
      end           
 +
  end 
 +
enddef
 +
 
define TerrainLighting()
 
define TerrainLighting()
 
   material
 
   material
Line 167: Line 404:
 
       shader -layer (($terrainMatLayer - 1) * 8)
 
       shader -layer (($terrainMatLayer - 1) * 8)
 
         vertexFormatPred position      0 true
 
         vertexFormatPred position      0 true
        vertexFormatPred normal        0 true
 
 
         vertexFormatPred texcoord      0 true
 
         vertexFormatPred texcoord      0 true
         vertexFormatPred blendindices  0 false
+
         if (viewerRenderType != $kRenderTypeShadowMap)           
        vertexFormatPred targetindices 0 false
+
            if ($useHWShader2Path or $useHWShader1Path)
 +
              create TerrainLightingVS_PS()
 +
            else
 +
              pass -fixedFunction           
 +
                  fillmode $stdMatFillMode
  
        pass -fixedFunction
+
                  alphaBlend srcFactor(destColor) add dstFactor(zero)
            create LightingStatesNoStdLights()
+
            fillmode $stdMatFillMode
+
  
            alphaBlend srcFactor(destColor) add dstFactor(zero)
+
                  colorScalar $terrainMatDiffCoef 1  # important that alpha be one for ARGB capture.
  
            colorScalar $terrainMatDiffCoef 1  # important that alpha be one for ARGB capture.
+
                  ffDepthOffset 0
 +
                  depthTest true -enableDepthWrite false           
 +
                  depthTestFunction acceptIfEqual
  
            ffDepthOffset 0
+
                  stage
            depthTest true -enableDepthWrite false
+
                    texture "floorLightMap_0"  # at the moment is deterministically mapped to page 0
 +
                    # A page holds 64x64 tiles. A tile is one world unit wide.
 +
                    ffTextureMatrix -scalev (1/64, 1/64)
 +
                    textureTransformType vector2
 +
                    ffTextureCoordsSource 0
 +
                    textureBlend multiplyScale2(texture colorScalar) select(colorScalar)
 +
                  end
 +
              end                   
 +
                         
 +
              pass -fixedFunction
 +
                  create LightingStatesNoStdLights()
 +
                  fillmode $stdMatFillMode
  
            stage
+
                   alphaBlend srcFactor(zero) add dstFactor(one)
              texture "floorLightMap_0"  # at the moment is deterministically mapped to page 0
+
     
              # A page holds 64x64 tiles. A tile is one world unit wide.
+
                  ffDepthOffset 0
              ffTextureMatrix -scalev (1/64, 1/64)
+
                  depthTest true -enableDepthWrite false
              textureTransformType vector2
+
                  alphaTest true 100
              ffTextureCoordsSource 0
+
                  alphaTestFunction acceptIfLess
              textureBlend multiplyScale2(texture colorScalar) select(colorScalar)
+
     
            end
+
                  stage
        end   
+
                    texture "floorLightMap_0"  # at the moment is deterministically mapped to page 0
       
+
                    ffTextureMatrix -scalev (1/64, 1/64)
        if ($lmIntegratedShadows)
+
                    textureTransformType vector2
            pass -fixedFunction
+
                    ffTextureCoordsSource 0
              create LightingStatesNoStdLights()
+
                    textureBlend select(texture) select(texture)
              fillmode $stdMatFillMode
+
                  end  
             
+
              end            
              alphaBlend srcFactor(destColor) add dstFactor(zero)
+
            endif        
             
+
         endif
              depthTest true -enableDepthWrite true
+
              depthTestFunction acceptIfLess
+
              alphaTest true 100
+
              alphaTestFunction acceptIfGreater
+
 
+
              colorScalar (0.61, 0.61, 0.61)  # needs to be set by tsUserEtc
+
 
+
              ffDepthOffset 1
+
 
+
              stage
+
                   texture "floorLightMap_0" 
+
                  ffTextureMatrix -scalev (1/64, 1/64)
+
                  textureTransformType vector2
+
                  ffTextureCoordsSource 0
+
                  textureBlend select(colorScalar) select(texture)
+
              end
+
            end
+
        elseif (not ($useHWShader2Path or $useHWShader1Path)) # must match condition in OverlayShadowMaterialDef
+
            pass -fixedFunction
+
              create LightingStatesNoStdLights()
+
              fillmode $stdMatFillMode
+
 
+
              alphaBlend srcFactor(zero) add dstFactor(one)
+
 
+
              ffDepthOffset 1
+
              depthTest true -enableDepthWrite true
+
              alphaTest true 100
+
              alphaTestFunction acceptIfLess
+
 
+
              stage
+
                  texture "floorLightMap_0"  # at the moment is deterministically mapped to page 0
+
                  ffTextureMatrix -scalev (1/64, 1/64)
+
                  textureTransformType vector2
+
                  ffTextureCoordsSource 0
+
                  textureBlend select(texture) select(texture)
+
              end  
+
            end
+
        endif
+
          
+
 
       end
 
       end
 
   end
 
   end

Revision as of 05:18, 14 April 2007


EP4-Pets
0xCD7FE87A
0x1C0532FA
0x4CE399AB
0xFF1AFA1C
# terrain

#
# Shaders for terrain tiles
#
#

setf  waterbumpMapScale    4.0
setv3 thumbnailMatDiffCoef (0.75, 0.93, 0.75)
seti  terrainMatLayer -2

# multiplier on terrain (grass/paints) both on the lot and
# lot skirts. (See lotSkirt.matShad.)
setc terrainMatDiffCoef (0.9, 0.9, 0.9)

# Specifies the number of tiles over which the paint & bump textures should be applied.
setf  canvasDetailTextureScale            4
setf  canvasBaseTextureScale             20
setf  paintTextureScale                  20
setf  paintDetailTextureScale            4.0
setb  hasCutout false

#==============================================================================

define TerrainCanvasShaderShaderVS_PS()
   pass
      fillmode $stdMatFillMode      
      depthTest true -enableDepthWrite true                  
      
      if ($hasCutout)                         
         depthTestFunction acceptIfEqual
      else
         depthTestFunction acceptIfLessOrEqual
      endif
      
      shaderProgram -target vertexProgram -method compile -version 1_1
         bindConstants 0 -bindingID geomToClip -constantCount 4			
			bindConstants 4 -bindingID immediateData -data (1/$canvasBaseTextureScale, 1/$canvasDetailTextureScale, 0, 0)
			
			shaderSource
			   float4x4 clipSpaceMatrix  : register(c0);
			   float4   textureConstants : register(c4);
			
			struct InputVertex
				{
					float3 position: POSITION0;
					float2 texcoord : TEXCOORD0;
				};
				
				struct OutputVertex
				{
					float4 clipPosition : POSITION;
					float2 txBase  : TEXCOORD0;
					float2 txDetail: TEXCOORD1;
				};
				
				OutputVertex VertexMain( InputVertex i)
				{
				   OutputVertex o;
				   o.clipPosition = mul(float4(i.position, 1), clipSpaceMatrix);
				   o.txBase   = i.texcoord * textureConstants.x;
				   o.txDetail = i.texcoord * textureConstants.y;				   
				   return o;
				}
			endShaderSource  
      end # end shader program
      
      
      shaderProgram -target pixelProgram -method compile -version 1_1      
         shaderSource                            
            sampler base;
            sampler detail;
            struct cInputPixel
            {               
               float2 tcBase : TEXCOORD0;
               float2 tcDetail : TEXCOORD1;
            };
            
            float4 PixelMain(cInputPixel pi) : COLOR
            {         
               float4 baseColor   = tex2D(base, pi.tcBase);                                                
               float4 detailColor = tex2D(detail, pi.tcDetail);                              
               float4 result = (detailColor.a * detailColor) + ((1 - detailColor.a) * baseColor);               
               return result;                        
            }
         endShaderSource
      end # end shader program      
         
      sampler base
         texture "${texture}"
         textureAddressing tile tile
      end
      
      sampler detail
         texture "${texture}_detail"
         textureAddressing tile tile
      end      
   end
enddef

define TerrainCanvasShaderShaderFF()
   pass -fixedFunction         
      fillmode $stdMatFillMode
      
      depthTest true -enableDepthWrite true                  
      
      if ($hasCutout)                         
         depthTestFunction acceptIfEqual
      else
         depthTestFunction acceptIfLessOrEqual
      endif
               
      stage
         texture "${texture}"
         textureAddressing tile tile
         ffTextureMatrix -scalev ($canvasBaseTextureScale, $canvasBaseTextureScale) -invert
         ffTextureCoordsSource 0
         textureBlend select(texture) select(texture)
      end 
      stage
         texture "${texture}_detail"
         textureAddressing tile tile
         ffTextureMatrix -scalev ($canvasDetailTextureScale, $canvasDetailTextureScale) -invert
         ffTextureCoordsSource 0
         textureBlend lerpTextureAlpha(texture outRegister) select(outRegister)
      end 
   end       
enddef

define TerrainCanvasShader()
   material
      shader -layer (($terrainMatLayer - 3) * 8)
         vertexFormatPred position      0 true
         vertexFormatPred normal        0 true
         vertexFormatPred texcoord      0 true
         viewerRenderTypePred viewerRenderType
         
         create DetermineHardwareSupport()
         if (viewerRenderType = $kRenderTypeShadowMap)
	         create ShadowMapVS_PS()
	      else
            if ($useHWShader1Path or $useHWShader2Path)	
		         create TerrainCanvasShaderShaderVS_PS()		
	         else   
               create TerrainCanvasShaderShaderFF()
            endif
         endif
      end
      
      shader
      end
   end
enddef

#==============================================================================

define TerrainPaintShaderVS_PS()
   shader -layer (($terrainMatLayer - 2) * 8)
      vertexFormatPred position      0 true
      vertexFormatPred texcoord      0 true
      pass
         fillmode $stdMatFillMode
         alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
         
         alphaTest true 10
         alphaTestFunction acceptIfGreater
         
         depthTest true -enableDepthWrite false
         if (viewerRenderType = $kRenderTypeImposter)
            depthTestFunction acceptIfLessOrEqual
         else
            depthTestFunction acceptIfEqual
         endif
         
         shaderProgram -target vertexProgram -method compile -version 1_1
            bindConstants 0 -bindingID geomToClip -constantCount 4			
			   bindConstants 4 -bindingID immediateData -data (1/$canvasBaseTextureScale, 1/$canvasDetailTextureScale, $alphaMapScaleU, $alphaMapScaleV)
   			
			   shaderSource
			      float4x4 clipSpaceMatrix  : register(c0);
			      float4   textureConstants : register(c4);
   			
			   struct InputVertex
				   {
					   float3 position: POSITION0;
					   float2 texcoord : TEXCOORD0;
				   };
   				
				   struct OutputVertex
				   {
					   float4 clipPosition : POSITION;
					   float2 txBase  : TEXCOORD0;
					   float2 txDetail: TEXCOORD1;
					   float2 txAlpha : TEXCOORD2;
				   };
   				
				   OutputVertex VertexMain( InputVertex i)
				   {
				      OutputVertex o;
				      o.clipPosition = mul(float4(i.position, 1), clipSpaceMatrix);
				      o.txBase   = i.texcoord * textureConstants.x;
				      o.txDetail = i.texcoord * textureConstants.y;				   
				      o.txAlpha  = i.texcoord.xy * textureConstants.zw;
				      return o;
				   }
			   endShaderSource  
         end # end shader program
         
         
         shaderProgram -target pixelProgram -method compile -version 1_1      
            shaderSource                            
               sampler base;
               sampler detail;
               sampler alphaMap;
               struct cInputPixel
               {               
                  float2 tcBase : TEXCOORD0;
                  float2 tcDetail : TEXCOORD1;
                  float2 tcAlpha  : TEXCOORD2;
               };
               
               float4 PixelMain(cInputPixel pi) : COLOR
               {         
                  float4 baseColor   = tex2D(base, pi.tcBase);                                                
                  float4 detailColor = tex2D(detail, pi.tcDetail);                              
                  float4 alphaColor  = tex2D(alphaMap, pi.tcAlpha);
                  float4 result = (detailColor.a * detailColor) + ((1 - detailColor.a) * baseColor);               
                  result.a = alphaColor.a;
                  return result;                        
               }
            endShaderSource
         end # end shader program      
            
         sampler base
            texture $paintTexture
            textureAddressing tile tile
         end
         
         sampler detail
            texture "${paintTexture}_detail"
            textureAddressing tile tile
         end
         
         sampler alphaMap
            texture $alphaMap
            textureAddressing clamp clamp
         end      
      end
   end
enddef

define TerrainPaintShader(addDetail)
   shader -layer (($terrainMatLayer - 2) * 8)
      vertexFormatPred position      0 true
      vertexFormatPred texcoord      0 true
      
      pass -fixedFunction         
         fillmode $stdMatFillMode

         alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
         
         alphaTest true 10
         alphaTestFunction acceptIfGreater
         
         depthTest true -enableDepthWrite false
         depthTestFunction acceptIfEqual
         
         if (&addDetail)
            stage
               texture $paintTexture
               textureAddressing tile tile
               ffTextureMatrix -scalev ($paintTextureScale, $paintTextureScale) -invert
               textureTransformType vector2
               ffTextureCoordsSource 0
               textureBlend select(texture) select(outRegister)
            end 
            stage
               texture "${paintTexture}_detail"
               textureAddressing tile tile
               ffTextureMatrix -scalev ($paintDetailTextureScale, $paintDetailTextureScale) -invert
               textureTransformType vector2
               ffTextureCoordsSource 0
               textureBlend lerpTextureAlpha(texture outRegister) select(outRegister)
            end 
         else
            stage
               texture $paintTexture
               textureAddressing tile tile
               ffTextureMatrix -scalev ($paintTextureScale, $paintTextureScale) -invert
               textureTransformType vector2
               ffTextureCoordsSource 0
               textureBlend select(texture) select(outRegister)
            end 
         endif
         stage
            texture $alphaMap
            textureAddressing clamp clamp
            ffTextureMatrix -scalev ($alphaMapScaleU, $alphaMapScaleV)
            ffTextureCoordsSource 0
            textureBlend select(outRegister) select(texture)
         end 
      end       
   end
enddef


define TerrainPaint()
   material
      create DetermineHardwareSupport()
      
      if (viewerRenderType != $kRenderTypeShadowMap)   
         if ($useHWShader1Path or $useHWShader2Path)	
		      create TerrainPaintShaderVS_PS()
	      else            
            create TerrainPaintShader(true)
            create TerrainPaintShader(false)
            shader
            end
         endif      
      endif      
   end
enddef


#==============================================================================
define TerrainLightingVS_PS()
   pass
      fillmode $stdMatFillMode
      alphaBlend srcFactor(destColor) add dstFactor(zero)            
      depthTest true -enableDepthWrite false            
      depthTestFunction acceptIfEqual
      
      shaderProgram -target vertexProgram -method compile -version 1_1
         bindConstants 0 -bindingID geomToClip -constantCount 4			
			bindConstants 4 -bindingID immediateData -data (1/64, 0, 0, 0)			
			
			shaderSource
			   float4x4 clipSpaceMatrix  : register(c0);
			   float4   textureConstants : register(c4);			   
			
			struct InputVertex
				{
					float3 position: POSITION0;
					float2 texcoord : TEXCOORD0;
				};
				
				struct OutputVertex
				{
					float4 clipPosition : POSITION;
					float2 txBase  : TEXCOORD0;					
				};
				
				OutputVertex VertexMain( InputVertex i)
				{
				   OutputVertex o;
				   o.clipPosition = mul(float4(i.position, 1), clipSpaceMatrix);
				   o.txBase   = i.texcoord * textureConstants.xx;				   
				   return o;
				}
			endShaderSource  
      end # end shader program
      
      
      shaderProgram -target pixelProgram -method compile -version 1_1
         bindConstants 0 -bindingID immediateData -data ($terrainMatDiffCoef.x, $terrainMatDiffCoef.y, $terrainMatDiffCoef.z, 1)         
         
         shaderSource   
            float4   terrainDiffCoef  : register(c0);
                                     
            sampler base;            
            struct cInputPixel
            {               
               float2 tcBase : TEXCOORD0;               
            };
            
            float4 PixelMain(cInputPixel pi) : COLOR
            {         
               float4 baseColor   = tex2D(base, pi.tcBase);                                                               
               float4 result = 2.0*(baseColor*terrainDiffCoef);               
               result.a = terrainDiffCoef.w;
               return result;                        
            }
         endShaderSource
      end # end shader program      
         
      sampler base
         texture "floorLightMap_0"  # at the moment is deterministically mapped to page 0
         textureAddressing tile tile
      end            
   end   
enddef

define TerrainLighting()
   material
   	create DetermineHardwareSupport()

      shader -layer (($terrainMatLayer - 1) * 8)
         vertexFormatPred position      0 true
         vertexFormatPred texcoord      0 true
         if (viewerRenderType != $kRenderTypeShadowMap)            
            if ($useHWShader2Path or $useHWShader1Path)
               create TerrainLightingVS_PS()
            else
               pass -fixedFunction            
                  fillmode $stdMatFillMode

                  alphaBlend srcFactor(destColor) add dstFactor(zero)

                  colorScalar $terrainMatDiffCoef 1  # important that alpha be one for ARGB capture.

                  ffDepthOffset 0
                  depthTest true -enableDepthWrite false            
                  depthTestFunction acceptIfEqual

                  stage
                     texture "floorLightMap_0"  # at the moment is deterministically mapped to page 0
                     # A page holds 64x64 tiles. A tile is one world unit wide.
                     ffTextureMatrix -scalev (1/64, 1/64)
                     textureTransformType vector2
                     ffTextureCoordsSource 0
                     textureBlend multiplyScale2(texture colorScalar) select(colorScalar)
                  end 
               end                     
                           
               pass -fixedFunction
                  create LightingStatesNoStdLights()
                  fillmode $stdMatFillMode

                  alphaBlend srcFactor(zero) add dstFactor(one)
      
                  ffDepthOffset 0
                  depthTest true -enableDepthWrite false
                  alphaTest true 100
                  alphaTestFunction acceptIfLess
      
                  stage
                     texture "floorLightMap_0"  # at the moment is deterministically mapped to page 0
                     ffTextureMatrix -scalev (1/64, 1/64)
                     textureTransformType vector2
                     ffTextureCoordsSource 0
                     textureBlend select(texture) select(texture)
                  end 
               end             
            endif         
         endif
      end
   end
enddef

#==============================================================================
define TerrainToolThumbnail()
   material
      shader -layer ($terrainMatLayer * 8)
         vertexFormatPred position      0 true
         vertexFormatPred normal        0 true
         vertexFormatPred texcoord      0 true
         vertexFormatPred blendindices  0 false
         vertexFormatPred targetindices 0 false

         pass -fixedFunction
            create LightingStates()
            alphaBlend srcFactor(srcAlpha) add dstFactor(zero)
            colorScalar $thumbnailMatDiffCoef
            fillmode $stdMatFillMode
            
            stage
               texture $paintTexture
               textureAddressing tile tile
               textureBlend multiplyScale2(texture diffuse) select(texture)
            end 
         end       
      end
   end
enddef

define TerrainWaterShader(withBumpMap)
   shader -layer 1
      vertexFormatPred position      0 true
      vertexFormatPred normal        0 true
      vertexFormatPred color         0 true
      vertexFormatPred texcoord      0 true
      vertexFormatPred blendindices  0 false
      vertexFormatPred targetindices 0 false
      viewerRenderTypePred viewerRenderType
         
      setb cubeMapSupport (queryBooleanGraphicsDeviceCap(hasCubeMapSupport))   
      create DetermineHardwareSupport()
      
      pass -fixedFunction         
         # disable lighting, disable normalize normals, enable local viewer
         if ($useSWVertexShaderPath)
            create LightingStatesNoStdLights()
         else
            create LightingStatesNoStdLightsParam(false true)            
         endif         
         alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)                  

         fillmode $stdMatFillMode
         colorScalar (1, 1, 1) -applyTextureLightColor 0 0                           
         
         stage
            if ($cubeMapSupport)
               texture reflectionoutdoorwater-envcube
               textureMIPFilterHint disabled 

               ffTextureCoordsSource fromReflectionVector
               textureTransformType vector3
               ffTextureMatrix -orientCameraToGlobal

               textureAddressing clamp clamp clamp
               textureBlend multiply(texture colorScalar) select(diffuse)
            else
               colorScalar (0.3,0.5,1.0)  -applyTextureLightColor 0 0     
               textureBlend select(colorScalar) select(diffuse)
            endif
         end  
      end   
      
      if (&withBumpMap)         
         pass -fixedFunction
            create LightingStatesNoStdLights()
            alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
            fillmode $stdMatFillMode
            colorScalar (1.0, 1.0, 1.0, 0.3) -applyTextureLightColor 0 0
         
            stage 
               texture "water-1-bump"
               textureAddressing tile tile
               # -atrans waveformType cycles/sec offset (startU, startV)  (endU, endV)
               textureMatrixAnimation -targetType fixedFunction -aorigin (0, 0) -atrans sine 0.04 0 (0, 0) (1, 0)  -ascale sawtooth 1 0 ($waterbumpMapScale, $waterbumpMapScale)
               textureTransformType vector2
               ffTextureCoordsSource 0
               textureBlend select(texture) select(texture)
            end 
   
            stage
               texture "water-2-bump"
               textureAddressing tile tile
               # -atrans waveformType cycles/sec offset (startU, startV)  (endU, endV)
               textureMatrixAnimation -targetType fixedFunction -aorigin (0, 0) -atrans sine 0.04 0 (1, 0) (0, 0)  -ascale sawtooth 1 0 ($waterbumpMapScale, $waterbumpMapScale)
               textureTransformType vector2
               ffTextureCoordsSource 0
               textureBlend multiply(texture outRegister) select(texture)
            end 
            
            stage
               # does this work out on GF2 due to reg combiners?
               textureBlend multiply(outRegister colorScalar) select(colorScalar)
            end 
         end  
      endif
   end
enddef


#==============================================================================
define TerrainWater()
   material
      # only activate the water w/ bump shader for pixel shader capable HW
      if (queryIntegerGraphicsDeviceCap(maxPixelProgramVersionMajor) != 0)         
         create TerrainWaterShader(true)
      endif
      create TerrainWaterShader(false)  # fallback without bumps
   end
enddef

#==============================================================================

materialDefinition terrainLightingMaterial
   setDefinition TerrainLighting
end

materialDefinition waterOnTerrain
   setDefinition TerrainWater
end

#endshader

Personal tools
Namespaces

Variants
Actions
Navigation
game select
Toolbox