ShaderTerrain

From SimsWiki
Revision as of 10:18, 14 April 2007 by Niol (Talk | contribs)

Jump to: navigation, search

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