Difference between revisions of "ShaderPool"

From SimsWiki
Jump to: navigation, search
(GunMod_GMRLS21-Chocolate_Pi_RLS-2.2_RC1)
(EP6-BV)
 
Line 1: Line 1:
 
<PRE>
 
<PRE>
  
GunMod_GMRLS21-Chocolate_Pi_RLS-2.2_RC1
+
EP6-BV
 
0xCD7FE87A
 
0xCD7FE87A
 
0x1C0532FA
 
0x1C0532FA
 
0x6852819C
 
0x6852819C
 
0xFF76EA99
 
0xFF76EA99
 +
# poolwater
  
#outdoordaytime-envcube      #pretty good
+
##############################################################
#marble-blue  could make a good refraction tile
+
 
# Pool Water
 
# Pool Water
 +
#
  
 
seti poolWaterLayer 5
 
seti poolWaterLayer 5
setf wmTransparency  1
+
 
 +
 
 +
##############################################################
 +
# Simple water material
 +
#
 +
# This material modulates the vertices by two sin waves --
 +
# one in X and one in Y. It then produces reflection and
 +
# refraction direction texture coordinates suitable for a
 +
# cubic environment map.
 +
 
 +
 
 +
#beginshader PoolWaterMaterialTwoWave
 +
#description Pool water with X/Y waves
 +
 
 +
#BeginStdAttrBlock
 +
 
 +
 
 +
setf wmTransparency  0.5
 +
#attrdescription Water transparency
 +
 
 
setf wmXSpeed        3    # min:0 max:100
 
setf wmXSpeed        3    # min:0 max:100
setf wmXRepeat      5     # min:0 max:100
+
#attrdescription Lookup texture for diffuse intensity.
setf wmXWaveHeight  0.02   # min:0 max:100
+
setf wmXRepeat      1     # min:0 max:100
 +
#attrdescription Lookup texture for diffuse intensity.
 +
setf wmXWaveHeight  0.1   # min:0 max:100
 +
#attrdescription Lookup texture for diffuse intensity.
 +
 
 
setf wmYSpeed        5    # min:0 max:100
 
setf wmYSpeed        5    # min:0 max:100
setf wmYRepeat      5   # min:0 max:100
+
#attrdescription Lookup texture for diffuse intensity.
setf wmYWaveHeight  0.01   # min:0 max:100
+
setf wmYRepeat      1.8   # min:0 max:100
 +
#attrdescription Lookup texture for diffuse intensity.
 +
setf wmYWaveHeight  0.2   # min:0 max:100
 +
#attrdescription Lookup texture for diffuse intensity.
  
set wmReflectionTexture water_spec
+
set wmReflectionTexture reflectionoutdoorwater-envcube
setb wmRefractionEnabled true
+
#attrdescription Environment cube map texture for reflection
set wmRefractionTexture water_spec
+
 
 +
setb wmRefractionEnabled false
 +
#attrdescription Whether refraction is enabled
 +
 
 +
set wmRefractionTexture reflectionoutdoorwater-envcube
 +
#attrdescription Environment cube map texture for refraction
 +
 
 +
 
 +
#EndStdAttrBlock
  
 
define PoolWaterSurfaceMaterial()
 
define PoolWaterSurfaceMaterial()
   material
+
   material    
 
       shader -layer $poolWaterLayer
 
       shader -layer $poolWaterLayer
 
         validateRenderShaderContext -vertexFormat position 0 required
 
         validateRenderShaderContext -vertexFormat position 0 required
 +
 
         create DetermineHardwareSupport()
 
         create DetermineHardwareSupport()
          # create RegularWavesHLSL()
+
   
create GunModWaves()
+
        if ($useSWVertexShaderPath or $useFixedFunctionPath)
       end
+
            # The vertex shader to ripple the water surface is deemed too expensive for SWVS.
 +
            # Note that because on some low-end cards shaders are turned off, DetermineHardwareSupport()
 +
            # will set $useFixedFunctionPath to true and $useSWVertexShaderPath to false; however,
 +
            # since the device is in swvp mode, the RegularWavesHLSL shader would validate anyway, which
 +
            # we don't want. Therefore here we do the simplified water surface if using either SWVS or FFP.
 +
            create SimplifiedWaterSurface()
 +
        else
 +
            if ($useHWShader2Path)
 +
              create HighEndWaterSurface()              
 +
            else
 +
              create RegularWavesHLSL()
 +
            endif
 +
        endif
 +
       
 +
       end #end shader
 +
     
 
       # basic fallback.
 
       # basic fallback.
 
       shader -layer $poolWaterLayer
 
       shader -layer $poolWaterLayer
         create SimplifiedWaterSurface()
+
         create SimplifiedWaterSurface()        
       end
+
       end #end shader
   end
+
     
 +
   end #end material
 
enddef
 
enddef
  
 
define SimplifiedWaterSurface()
 
define SimplifiedWaterSurface()
   pass -fixedFunction
+
   pass -fixedFunction    
 
       alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
 
       alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
 
       depthTest true -enableDepthWrite false
 
       depthTest true -enableDepthWrite false
       colorScalar (0.2,0.3,1.0) 0.4
+
       colorScalar (0.2,0.3,1.0) 0.4          
 
       stage
 
       stage
 
         textureBlend select(colorScalar) select(colorScalar)
 
         textureBlend select(colorScalar) select(colorScalar)
Line 52: Line 104:
  
 
define RegularWavesHLSL()
 
define RegularWavesHLSL()
 
+
 
   #DRIVERBUG
 
   #DRIVERBUG
   #-clipAlways
+
   # The -clipAlways flag is needed to work around what appears to be a driver bug.
pass -modifiedEachFrameHint
+
  # On NVIDIA GF2 class HW, the presence of the cube map texture in the pass below
 +
  # leads to a large performance drop whenever the pass is rendered with clipping disabled.
 +
  # Rendering this pass with clipping enabled avoids the performance drop. In addition,
 +
  # substituting a 2D texture for the cube map texture, or rendering this pass in HW
 +
  # using fixed function vertex processing also avoids the performance drop (but would obviously
 +
  # not have the desired visual result).
 +
  # The cause of this is unknown. This was observed on a GF4GO, driver version 42.58.
 +
pass -clipAlways -modifiedEachFrameHint
 +
#fillmode wireframe
 
alphaBlend srcFactor(one) add dstFactor(one)
 
alphaBlend srcFactor(one) add dstFactor(one)
       seti textureLights (numLightsOfType(environmentCube))
+
       seti textureLights (numLightsOfType(environmentCube))      
 +
 
 
shaderProgram -target vertexProgram -method compile -version 1_1
 
shaderProgram -target vertexProgram -method compile -version 1_1
 +
 
bindConstants 0 -bindingID geomToClip -constantCount 4
 
bindConstants 0 -bindingID geomToClip -constantCount 4
 
bindConstants 4 -bindingID geomToCamera -constantCount 3
 
bindConstants 4 -bindingID geomToCamera -constantCount 3
bindConstants 7 -bindingID frameInfo
+
 
 +
bindConstants 7 -bindingID frameInfo  
 +
 
 
bindConstants 11 -bindingID immediateData -data ($wmXRepeat, 0, $wmXWaveHeight, $wmXSpeed)
 
bindConstants 11 -bindingID immediateData -data ($wmXRepeat, 0, $wmXWaveHeight, $wmXSpeed)
 
bindConstants 12 -bindingID immediateData -data (0, $wmYRepeat, $wmYWaveHeight, $wmYSpeed)
 
bindConstants 12 -bindingID immediateData -data (0, $wmYRepeat, $wmYWaveHeight, $wmYSpeed)
 +
 +
# pre-evaluate these
 
setf xscale (-$wmXRepeat * $wmXWaveHeight)
 
setf xscale (-$wmXRepeat * $wmXWaveHeight)
 
setf yscale (-$wmYRepeat * $wmYWaveHeight)
 
setf yscale (-$wmYRepeat * $wmYWaveHeight)
Line 69: Line 135:
 
bindConstants 14 -bindingID allStandardLightData -constantCount 4 -constantType float
 
bindConstants 14 -bindingID allStandardLightData -constantCount 4 -constantType float
 
bindConstants 18 -bindingID cameraToGlobal -constantCount 3
 
bindConstants 18 -bindingID cameraToGlobal -constantCount 3
         if (tsIsDay)
+
       
             bindConstants 25 -bindingID immediateData -data (0.75, 0.75, 0.75, 1)
+
         if (tsIsDay)        
 +
             bindConstants 25 -bindingID immediateData -data (0.75, 0.75, 0.75, 1)        
 
         else
 
         else
             bindConstants 25 -bindingID immediateData -data (0.2, 0.2, 0.2, 1)
+
             bindConstants 25 -bindingID immediateData -data (0.2, 0.2, 0.2, 1)        
 
         endif
 
         endif
 +
 
shaderSource
 
shaderSource
 +
 
float4 frameInfo : register(c7);
 
float4 frameInfo : register(c7);
 
float4 waveDataX : register(c11);
 
float4 waveDataX : register(c11);
Line 82: Line 151:
 
float4x3 cameraSpaceMatrix : register(c4);
 
float4x3 cameraSpaceMatrix : register(c4);
 
float4x3 cameraToGlobalMatrix : register(c18);
 
float4x3 cameraToGlobalMatrix : register(c18);
        float4 nightColor: register(c25);
+
 +
            float4 nightColor: register(c25);
 +
           
 
float4 lightDirection : register(c14);
 
float4 lightDirection : register(c14);
 
float4 lightColor : register(c15);
 
float4 lightColor : register(c15);
 
const static float4 refractionWeights={1,1,2,0};
 
const static float4 refractionWeights={1,1,2,0};
 +
 
const static float4 layerBlue={0.3, 0.7, 1.0, 1};
 
const static float4 layerBlue={0.3, 0.7, 1.0, 1};
 +
 
struct InputVertex
 
struct InputVertex
 
{
 
{
Line 92: Line 165:
 
float3 normal : NORMAL0;
 
float3 normal : NORMAL0;
 
};
 
};
 +
 
struct OutputVertex
 
struct OutputVertex
 
{
 
{
Line 98: Line 172:
 
float4 specularColor: COLOR1;
 
float4 specularColor: COLOR1;
 
float3 reflection : TEXCOORD0;
 
float3 reflection : TEXCOORD0;
 +
 
};
 
};
 +
           
 +
 +
 
OutputVertex VertexMain( InputVertex inputVertex)
 
OutputVertex VertexMain( InputVertex inputVertex)
 
{
 
{
 +
// Do Y-direction waves
 +
// r0 = (x, y, z, t)
 +
 
OutputVertex outputVertex;
 
OutputVertex outputVertex;
 +
 
float4 posAndTime;
 
float4 posAndTime;
 
posAndTime.xyz = inputVertex.position;
 
posAndTime.xyz = inputVertex.position;
 
posAndTime.w = frameInfo.w;
 
posAndTime.w = frameInfo.w;
 +
 
float temp = dot(posAndTime, waveDataX);
 
float temp = dot(posAndTime, waveDataX);
 +
 +
// z = h * sin(...)
 +
 
float z;
 
float z;
 +
// scale temp to fit -pi +pi range
 +
//temp = temp *  (1 / (2 * 3.14159)) + 0.5;
 +
 
float3 waveNormal;
 
float3 waveNormal;
 +
 
z = sin(temp) * waveDataX.z + inputVertex.position.z;
 
z = sin(temp) * waveDataX.z + inputVertex.position.z;
 +
 
waveNormal.x = cos(temp) * waveDataHelper.x + inputVertex.normal.x;
 
waveNormal.x = cos(temp) * waveDataHelper.x + inputVertex.normal.x;
 +
 
temp = dot(posAndTime, waveDataY);
 
temp = dot(posAndTime, waveDataY);
 +
//temp = temp *  (1 / (2 * 3.14159)) + 0.5;
 +
 
z +=  sin(temp) * waveDataY.z;
 
z +=  sin(temp) * waveDataY.z;
 +
 
waveNormal.y = cos(temp) * waveDataHelper.y + inputVertex.normal.y;
 
waveNormal.y = cos(temp) * waveDataHelper.y + inputVertex.normal.y;
 +
 
waveNormal.z = inputVertex.normal.z;
 
waveNormal.z = inputVertex.normal.z;
 +
 
waveNormal = normalize(waveNormal);
 
waveNormal = normalize(waveNormal);
 +
 
posAndTime.w = 1.0;
 
posAndTime.w = 1.0;
 
posAndTime.z = z;
 
posAndTime.z = z;
 +
 
outputVertex.clipPosition = mul( posAndTime, clipSpaceMatrix);
 
outputVertex.clipPosition = mul( posAndTime, clipSpaceMatrix);
 +
 
float3 cameraSpaceNormal = normalize(mul(waveNormal, cameraSpaceMatrix));
 
float3 cameraSpaceNormal = normalize(mul(waveNormal, cameraSpaceMatrix));
 +
 
float3 cameraSpacePosition = mul( posAndTime, cameraSpaceMatrix);
 
float3 cameraSpacePosition = mul( posAndTime, cameraSpaceMatrix);
 +
 
float3 viewVector = normalize(-cameraSpacePosition);
 
float3 viewVector = normalize(-cameraSpacePosition);
 +
 
float3 R = reflect(viewVector, cameraSpaceNormal);
 
float3 R = reflect(viewVector, cameraSpaceNormal);
 +
 
outputVertex.reflection = mul( -R, cameraToGlobalMatrix);
 
outputVertex.reflection = mul( -R, cameraToGlobalMatrix);
 +
 +
 
float fresnel = dot(viewVector , cameraSpaceNormal);
 
float fresnel = dot(viewVector , cameraSpaceNormal);
 +
 
float rdotl = saturate(dot(R, lightDirection));
 
float rdotl = saturate(dot(R, lightDirection));
 +
 
float I = pow(rdotl+0.1, 15); // fudge factor to punch up the highlights.
 
float I = pow(rdotl+0.1, 15); // fudge factor to punch up the highlights.
               nightColor = nightColor * 2;
+
outputVertex.diffuseColor = ((1.0 + fresnel) * saturate(nightColor) * 0.5) * layerBlue;
+
               nightColor = nightColor * 2;                          
 +
                         
 +
outputVertex.diffuseColor = ((1.0 - fresnel) * saturate(nightColor) * 0.5) * layerBlue;
 +
 
outputVertex.specularColor = I;
 
outputVertex.specularColor = I;
 
return(outputVertex);
 
return(outputVertex);
 +
 
}
 
}
endShaderSource
+
 
end
+
 
 +
endShaderSource
 +
end # shaderProgram
 +
 
stage
 
stage
 
texture $wmReflectionTexture
 
texture $wmReflectionTexture
Line 138: Line 253:
 
textureBlend multiply(texture diffuse) select(diffuse)
 
textureBlend multiply(texture diffuse) select(diffuse)
 
end
 
end
 +
     
 +
     
 
addSpecular true
 
addSpecular true
end
+
 +
end # end pass
 +
 
 +
 
 
enddef
 
enddef
  
define GunModWaves()
 
  
   pass
+
define HighEndWaterSurface()
 +
    
 +
pass -clipAlways -modifiedEachFrameHint
 
#fillmode wireframe
 
#fillmode wireframe
alphaBlend srcFactor(one) add dstFactor(one)
+
alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
+
       seti textureLights (numLightsOfType(environmentCube)
       shaderProgram -target vertexProgram -method compile -version 1_1
+
       depthTest true -enableDepthWrite false          
     
+
        bindConstants 0 -bindingID geomToClip -constantCount 4
+
        bindConstants 4 -bindingID geomToCamera -constantCount 3
+
     
+
        bindConstants 7 -bindingID frameInfo
+
     
+
        bindConstants 11 -bindingID immediateData -data ($wmXRepeat, 0, $wmXWaveHeight, $wmXSpeed)
+
        bindConstants 12 -bindingID immediateData -data (0, $wmYRepeat, $wmYWaveHeight, $wmYSpeed)
+
        
+
        # pre-evaluate these
+
         setf xscale (-$wmXRepeat * $wmXWaveHeight)
+
        setf yscale (-$wmYRepeat * $wmYWaveHeight)
+
        bindConstants 13 -bindingID immediateData -data ($xscale,$yscale, 1,0)
+
        bindConstants 14 -bindingID allStandardLightData -constantCount 4 -constantType float
+
        bindConstants 18 -bindingID cameraToGlobal -constantCount 3
+
 
    
 
    
        shaderSource
+
shaderProgram -target vertexProgram -method compile -version 1_1
     
+
 
            float4 frameInfo : register(c7);
+
bindConstants 0 -bindingID geomToClip -constantCount 4
            float4 waveDataX : register(c11);
+
bindConstants 4 -bindingID geomToCamera -constantCount 3
            float4 waveDataY : register(c12);
+
 
            float4 waveDataHelper : register(c13);
+
bindConstants 7 -bindingID frameInfo  
            float4x4 clipSpaceMatrix : register(c0);
+
 
            float4x3 cameraSpaceMatrix : register(c4);
+
bindConstants 11 -bindingID immediateData -data ($wmXRepeat, 0, $wmXWaveHeight, $wmXSpeed)
            float4x3 cameraToGlobalMatrix : register(c18);
+
bindConstants 12 -bindingID immediateData -data (0, $wmYRepeat, $wmYWaveHeight, $wmYSpeed)
           
+
 
            float4 lightDirection : register(c14);
+
# pre-evaluate these
            float4 lightColor : register(c15);
+
setf xscale (-$wmXRepeat * $wmXWaveHeight)
            const static float4 refractionWeights={1,1,2,0};
+
setf yscale (-$wmYRepeat * $wmYWaveHeight)
           
+
bindConstants 13 -bindingID immediateData -data ($xscale,$yscale, 1,0)
            struct InputVertex
+
bindConstants 14 -bindingID allStandardLightData -constantCount 4 -constantType float
            {
+
bindConstants 18 -bindingID cameraToGlobal -constantCount 3
              float4 position: POSITION0;
+
bindConstants 21 -bindingID cameraToGeom -constantCount 3
              float3 normal : NORMAL0;
+
              float2 texc : TEXCOORD0;
+
            };
+
           
+
            struct OutputVertex
+
            {
+
              float4 clipPosition : POSITION;
+
              float3 normal : TEXCOORD0;
+
              float3 viewVector : TEXCOORD1;
+
 
          
 
          
            };
+
        if (tsIsDay)       
              
+
             bindConstants 25 -bindingID immediateData -data (0.75, 0.75, 0.75, 1)        
            static float2 waveCenter1 = {0.0, 0.0};
+
        else
            static float2 waveCenter2 = {-0.6, 0.2};
+
             bindConstants 25 -bindingID immediateData -data (0.2, 0.2, 0.2, 1)        
           
+
        endif
            static float2 timeScale = {2,1};
+
            static float2 waveHeight = {0.04, 0.02};
+
            static float2 distScale = {12, 8};
+
           
+
           
+
           
+
            float4 ComputeWavePositionTwoWay(float2 pos1, float2 pos2)
+
            {
+
              
+
  float2 vec;
+
              float4 dist;
+
             
+
              vec = waveCenter1 - pos1;
+
              dist.x = dot(vec, vec);
+
             
+
              vec = waveCenter2 - pos1;
+
              dist.y = dot(vec,vec);
+
             
+
  vec = waveCenter2 - pos2;
+
              dist.z = dot(vec, vec);
+
             
+
              vec = waveCenter2 - pos2;
+
              dist.w = dot(vec,vec);
+
 
+
  dist *= distScale.xyxy;
+
 
+
  float4 sinResult = sin(dist + frameInfo.w * timeScale.xyxy);
+
  
  //sinResult = 1.0- abs(sinResult);
+
shaderSource
+
  return(sinResult);
+
+
            }
+
           
+
            float2 ComputeWavePosition(float2 pos1)
+
            {
+
           
+
  float2 vec;
+
              float2 dist;
+
 
+
  vec = waveCenter1 - pos1;
+
              dist.x = dot(vec, vec);
+
             
+
              vec = waveCenter2 - pos1;
+
              dist.y = dot(vec,vec);
+
               
+
  dist *= distScale.xy;
+
 
+
  float2 sinResult = sin(dist + frameInfo.w * timeScale);
+
  
  //sinResult = 1.0- abs(sinResult);
+
float4 frameInfo : register(c7);
 +
float4 waveDataX : register(c11);
 +
float4 waveDataY : register(c12);
 +
float4 waveDataHelper : register(c13);
 +
float4x4 clipSpaceMatrix : register(c0);
 +
float4x3 cameraSpaceMatrix : register(c4);
 +
float4x3 cameraToGlobalMatrix : register(c18);
 +
float4x3 cameraToGeomMatrix : register(c21);
 
 
  return(sinResult);
 
 
 
             }
+
             float4 nightColor: register(c25);
 
              
 
              
            OutputVertex VertexMain( InputVertex inputVertex)
+
float4 lightDirection : register(c14);
            {
+
float4 lightColor : register(c15);
              // Do Y-direction waves
+
float4 lightSpecular : register(c16);
              // r0 = (x, y, z, t)
+
const static float4 refractionWeights={1,1,2,0};
             
+
              OutputVertex outputVertex;
+
+
float4 pos = inputVertex.position;
+
 
 
float2 sample1, sample2, sample3;
+
const static float4 layerBlue={0.3, 0.7, 1.0, 1};
 
 
sample1 = inputVertex.position;
+
struct InputVertex
sample2 = inputVertex.position;
+
{
sample3 = inputVertex.position;
+
float3 position: POSITION0;
sample2.x += 0.1;
+
float3 normal : NORMAL0;
sample3.y += 0.1;
+
};
+
float4 heights1=ComputeWavePositionTwoWay(sample1, sample2);
+
float2 heights2=ComputeWavePosition(sample3);
+
+
+
float3 pos1, pos2;
+
pos1 = pos;
+
pos2 = pos;
+
+
  pos.z += (heights1.x* waveHeight.x);// + heights1.y *  waveHeight.y);
+
  pos1.z += (heights1.z* waveHeight.x);// + heights1.w *  waveHeight.y);
+
  pos2.z += (heights2.x* waveHeight.x);// + heights2.y *  waveHeight.y);
+
 
 
pos1.x +=0.1;
+
struct OutputVertex
pos2.y +=0.1;
+
{
float3 vec1, vec2;
+
float4 clipPosition : POSITION;
vec1 = normalize(pos1 - pos);
+
float4 sPos        : TEXCOORD0;
vec2 = normalize(pos2 - pos);
+
float2 Wave0        : TEXCOORD1;
+
              float2 Wave1        : TEXCOORD2;
float3 normal = cross(vec1, vec2);
+
              float2 Wave2        : TEXCOORD3;
+
               float2 Wave3        : TEXCOORD4;
normal = mul(normal, (float3x3)cameraSpaceMatrix);
+
               float3 Eye          : TEXCOORD5;
 
+
               float4 specular    : COLOR0;    
float3 viewVector = mul(pos, cameraSpaceMatrix);
+
};
+
outputVertex.viewVector = normalize(-viewVector);
+
+
               outputVertex.clipPosition = mul(pos, clipSpaceMatrix);
+
               outputVertex.normal = normal;
+
               return(outputVertex);
+
             
+
        }
+
 
              
 
              
        endShaderSource
+
     
+
      end # end shaderProgram
+
OutputVertex VertexMain( InputVertex inputVertex)
       
+
{
      shaderProgram -target pixelProgram -method assemble
+
// Do Y-direction waves
        bindConstants 0 -bindingID allStandardLightData -constantCount 4 -constantType float
+
// r0 = (x, y, z, t)
 +
 +
OutputVertex outputVertex;
 +
 +
float4 posAndTime;
 +
posAndTime.xyz = inputVertex.position;
 +
posAndTime.w = frameInfo.w;
 +
 +
float temp = dot(posAndTime, waveDataX);
 +
float z = sin(temp) * waveDataX.z + inputVertex.position.z;
 +
temp = dot(posAndTime, waveDataY);
 +
posAndTime.z =  z + sin(temp) * waveDataY.z;
 +
posAndTime.w = 1.0f;
 +
 +
outputVertex.clipPosition = mul( posAndTime, clipSpaceMatrix);
 +
outputVertex.sPos = float4(0.5f*(outputVertex.clipPosition.ww - outputVertex.clipPosition.xy), outputVertex.clipPosition.ww);
 +
 +
float3 waveNormal = normalize(inputVertex.normal + float3(cos(temp)*waveDataHelper.xy, 0));
 +
        float3 cameraSpaceNormal = normalize(mul(waveNormal, cameraSpaceMatrix));
 +
float3 cameraSpacePosition = mul( posAndTime, cameraSpaceMatrix);
 +
float3 viewVector = normalize(-cameraSpacePosition);
 +
 +
float3 halfVector = normalize(viewVector + lightDirection);
 +
outputVertex.specular = lightSpecular*pow(dot(cameraSpaceNormal, halfVector), 50)*nightColor*lightColor*0.5;
 +
 +
 +
float2 fTranslation=float2(fmod(frameInfo.w, 100)*0.005, fmod(frameInfo.w, 100)*0.005);
 +
              float2 vTexCoords = posAndTime.xy*0.05;
 +
               
 +
              // Output bump layers texture coordinates   
 +
              float fSinTranslation=sin(fTranslation*100)*0.005;
 +
              float2 vTranslation0=fTranslation+fSinTranslation;
 +
              float2 vTranslation1=fTranslation-fSinTranslation;
 +
              float2 vTranslation2=fTranslation;
 +
               
 +
              // Scale texture coordinates to get mix of low/high frequency details
 +
              outputVertex.Wave0.xy = vTexCoords.xy+fTranslation*2.0;
 +
              outputVertex.Wave1.xy = vTexCoords.xy*2.0+fTranslation*4.0;
 +
              outputVertex.Wave2.xy = vTexCoords.xy*7.0+fTranslation*2.0;
 +
              outputVertex.Wave3.xy = vTexCoords.xy*12.0+fTranslation;               
 +
                                   
 +
            // compute binormal
 +
            float3 binormal = normalize(cross(waveNormal, float3(1.0f, 0.0f, 0.0f)));            
 +
            float3 tangent = normalize(cross(binormal, waveNormal));
 +
           
 +
            // tangent space matrix
 +
            float3x3 objToTangentSpace = float3x3(tangent, binormal, waveNormal);
 +
           
 +
            float3 viewVectorInObjSpace = mul(viewVector, (float3x3)cameraToGeomMatrix);
 +
           
 +
            outputVertex.Eye.xyz = mul(objToTangentSpace, viewVectorInObjSpace);
 +
 +
 +
return(outputVertex);
 +
}
  
        shaderSource
+
 
            ps_2_0
+
endShaderSource  
            def c10,20,4,0,0
+
end # shaderProgram
            def c11, 0,0.5,1,2
+
            dcl t0
+
            dcl t1 ; v
+
dcl_cube s0
+
     
+
            nrm r0, t0      ; n
+
           
+
            add r1, r0, r0 ; 2N
+
           
+
            dp3 r1.w, r0, t1 ; N.V
+
mad r2.xyz, r1, r1.w, -t1    ; 2N * (N.V) -V
+
+
texld r5, r2, s0
+
+
dp3 r3.x, r2, c0  ; R.L
+
pow r4, r3.x, c10.x
+
+
mov_sat r1.x, r1.w
+
+
add r6.x, c11.z, -r1.x      ; 1 - Fresnel
+
+
pow r3.y, r6.x, c10.y  ; N.V ^ 4
+
 
 
mul r7, r5, r3.y
+
shaderProgram -target pixelProgram -method compile -version 2_0             
mad r4, r4, r3.y, r7
+
        shaderSource                           
mul r4, r4, c11.y
+
            sampler reflect; 
+
            sampler bump;         
mov oC0, r4
+
            struct cInputPixel
 +
            {             
 +
              float4 sPos        : TEXCOORD0;             
 +
              float2 Wave0        : TEXCOORD1;
 +
              float2 Wave1        : TEXCOORD2;
 +
              float2 Wave2        : TEXCOORD3;
 +
              float2 Wave3        : TEXCOORD4;
 +
              float3 Eye          : TEXCOORD5;
 +
              float4 specular    : COLOR0;
 +
            };
 
              
 
              
 +
            float Fresnel(float NdotL, float fresnelBias, float fresnelPow, float facing)
 +
            {             
 +
              return max(fresnelBias + (1.0f - fresnelBias)*pow(facing, fresnelPow), 0.0);
 +
            }
 +
           
 +
            float4 PixelMain(cInputPixel pi) : COLOR
 +
            {       
 +
              float3 vEye = normalize(pi.Eye);
 +
 +
              // Get bump layers
 +
              float3 vBumpTexA = tex2D(bump, pi.Wave0.xy).xyz;
 +
              float3 vBumpTexB = tex2D(bump, pi.Wave1.xy).xyz;
 +
              float3 vBumpTexC = tex2D(bump, pi.Wave2.xy).xyz;
 +
              float3 vBumpTexD = tex2D(bump, pi.Wave3.xy).xyz;
 +
               
 +
              // Average bump layers
 +
              float3 vBumpTex=normalize(2.0 * (vBumpTexA.xyz + vBumpTexB.xyz + vBumpTexC.xyz + vBumpTexD.xyz)-4.0);
 +
               
 +
              // Apply individual bump scale for refraction and reflection             
 +
              float3 vReflBump = vBumpTex.xyz * float3(0.1, 0.1, 1.0); 
 +
             
 +
              float4 vReflection = tex2Dproj(reflect, pi.sPos + float4(vReflBump.xy, 0.0f, 0.0f));//*0.001 + float4(2.0*vBumpTexD-1.0f.xxx, 1);
 +
             
 +
              // Compute Fresnel term
 +
              float NdotL = max(dot(vEye, vReflBump), 0);
 +
              float facing = (1.0 - NdotL);
 +
              float fresnel = Fresnel(NdotL, 0.2, 5.0, facing);
 +
                             
 +
              vReflection.a = fresnel;             
 +
              return saturate(vReflection + float4(0.0, 0.25, 0.5, 0.0) + pi.specular);
 +
            }
 
         endShaderSource
 
         endShaderSource
       end
+
       end                                                    
 
        
 
        
       sampler 0
+
       sampler reflect
texture $wmReflectionTexture
+
        texture "PoolReflection"
textureAddressing clamp clamp clamp
+
        textureAddressing clamp clamp
       end
+
       end
  end # pass
+
     
 +
      sampler bump
 +
        texture "poolShape-body-bump"
 +
        textureAddressing tile tile
 +
      end 
 +
           
 +
end # end pass
 
    
 
    
 
enddef
 
enddef
 +
 +
 +
#endshader PoolWaterSurfaceMaterial
 +
 +
 +
 +
# NOTE:
 +
# refraction in vector terms:
 +
#  v  = incoming unit vector
 +
#  n  = surface normal
 +
#  v' = refracted vector
 +
#  a  = refraction ratio, ni / nr =~ 0.75 for air->water
 +
#
 +
# v' = v - sn
 +
# where s = sqrt(1 - a^2 (1 - (v.n)^2))  - v.n.
  
  
  
 +
##########################################################
 
# Caustics
 
# Caustics
 +
#
  
 
define TiledTextureAnimShaderProgram(tilesX tilesY speed)
 
define TiledTextureAnimShaderProgram(tilesX tilesY speed)
 
   shaderProgram -target vertexProgram -method assemble
 
   shaderProgram -target vertexProgram -method assemble
 +
 
 
       bindConstants 0 -bindingID frameInfo            # for time in .w
 
       bindConstants 0 -bindingID frameInfo            # for time in .w
 
       bindConstants 1 -data      (&tilesX, &tilesY, &speed, 1)        # tx, ty, speed
 
       bindConstants 1 -data      (&tilesX, &tilesY, &speed, 1)        # tx, ty, speed
       bindConstants 2 -data      ((1/&tilesX), (1/&tilesY), (&tilesY - 1), 1) # 1/tx 1/ty ty-1
+
       bindConstants 2 -data      ((1/&tilesX), (1/&tilesY), (&tilesY - 1), 1) # 1/tx 1/ty ty-1    
 
       if (tsIsDay)
 
       if (tsIsDay)
 
         bindConstants 3 -bindingID immediateData -data (0.75, 0.75, 0.75, 1)
 
         bindConstants 3 -bindingID immediateData -data (0.75, 0.75, 0.75, 1)
Line 366: Line 494:
 
         bindConstants 3 -bindingID immediateData -data (0.2, 0.2, 0.2, 1)
 
         bindConstants 3 -bindingID immediateData -data (0.2, 0.2, 0.2, 1)
 
       endif
 
       endif
 +
     
 
       shaderSource
 
       shaderSource
 
         vs_1_1
 
         vs_1_1
 
         dcl_position v0
 
         dcl_position v0
 
         dcl_texcoord v1
 
         dcl_texcoord v1
 +
 
         mov oPos, v0  # clip space quad, no transforms needed.
 
         mov oPos, v0  # clip space quad, no transforms needed.
 +
     
 +
        # tiled texture animation
 
         mov r0, c0
 
         mov r0, c0
 
         mul r1, c1.z, r0.w
 
         mul r1, c1.z, r0.w
 
         frc r5.y, r1.y
 
         frc r5.y, r1.y
 +
       
 
         mul r1, c1.y, r5.y    # f -> [0, ty)
 
         mul r1, c1.y, r5.y    # f -> [0, ty)
 
         frc r5.y, r1
 
         frc r5.y, r1
 
         sub r3.y, r1, r5.y    # v' = floor(f)
 
         sub r3.y, r1, r5.y    # v' = floor(f)
 +
       
 
         mul r1, c1.x, r5.y    # f -> [0, tx)
 
         mul r1, c1.x, r5.y    # f -> [0, tx)
 
         frc r5.y, r1
 
         frc r5.y, r1
 
         sub r3.x, r1, r5.y    # u' = floor(f)
 
         sub r3.x, r1, r5.y    # u' = floor(f)
 +
 
         add r8.xy, v1.xy,  r3.xy
 
         add r8.xy, v1.xy,  r3.xy
 
         mul oT0.xy, r8.xy, c2.xy
 
         mul oT0.xy, r8.xy, c2.xy
 +
       
 
         mov oD0, c3 # also spit out diffuse color with modcolor information
 
         mov oD0, c3 # also spit out diffuse color with modcolor information
 +
       
 
       endShaderSource
 
       endShaderSource
   end
+
 
 +
   end
 
enddef
 
enddef
  
define CausticsGeneratorMaterial()
+
define CausticsGeneratorMaterial()  
   material
+
   material    
 
       # This material updates a render target with the current frame
 
       # This material updates a render target with the current frame
 
       # of the tile animation. This is the only way to handle a repeated
 
       # of the tile animation. This is the only way to handle a repeated
 
       # animating texture (not to mention using it for projective texturing)
 
       # animating texture (not to mention using it for projective texturing)
 
       # in the absence of clip maps.
 
       # in the absence of clip maps.
 
+
     
 
       create DetermineHardwareSupport()
 
       create DetermineHardwareSupport()
 +
     
 
       if ($causticsEnabled and $useFixedFunctionPath = false and $useSWVertexShaderPath = false)
 
       if ($causticsEnabled and $useFixedFunctionPath = false and $useSWVertexShaderPath = false)
         shader -layer +9999
+
     
             pass
+
         shader -layer +9999  
 +
 
 +
             pass            
 
               renderClipSpaceRect
 
               renderClipSpaceRect
 +
             
 
               renderTarget causticsTile -fixed (64, 64) -allocateDepthBuffer false -undo
 
               renderTarget causticsTile -fixed (64, 64) -allocateDepthBuffer false -undo
 +
 
               create TiledTextureAnimShaderProgram(8 4 1)
 
               create TiledTextureAnimShaderProgram(8 4 1)
 +
             
 
               alphaBlend srcFactor(one) add dstFactor(zero)
 
               alphaBlend srcFactor(one) add dstFactor(zero)
 
               alphaTest false 0
 
               alphaTest false 0
 
               alphaTestFunction acceptIfGreater
 
               alphaTestFunction acceptIfGreater
 +
 
               depthTest false -enableDepthWrite false
 
               depthTest false -enableDepthWrite false
 
               depthTestFunction accept
 
               depthTestFunction accept
               # 7/24/2004 Fix bug with kRenderTypeNormal default stencil state and nv40.
+
 
               # It reads random stencil values even this target has no depth stencil target.
+
               # 7/24/2004 Fix bug with kRenderTypeNormal default stencil state and nv40.
 +
               # It reads random stencil values even this target has no depth stencil target.              
 
               # This stencil call will break pixo, but pixo does not show caustics.
 
               # This stencil call will break pixo, but pixo does not show caustics.
 
               stencil false
 
               stencil false
               fillmode $stdMatFillMode
+
 
               shaderProgram -target pixelProgram -method compile -version 1_1
+
               fillmode $stdMatFillMode                            
                   shaderSource
+
             
 +
               shaderProgram -target pixelProgram -method compile -version 1_1              
 +
                   shaderSource                          
 
                     sampler caustics;
 
                     sampler caustics;
 
                     struct cInputPixel
 
                     struct cInputPixel
Line 420: Line 568:
 
                     };
 
                     };
 
                     float4 PixelMain(cInputPixel pi) : COLOR
 
                     float4 PixelMain(cInputPixel pi) : COLOR
                     {
+
                     {        
                         float4 texColor = tex2D(caustics, pi.tc0);
+
                         float4 texColor = tex2D(caustics, pi.tc0);                                              
                         return texColor*pi.color;
+
                         return texColor*pi.color;                      
 
                     }
 
                     }
 
                   endShaderSource
 
                   endShaderSource
               end
+
               end                                                    
 +
             
 
               sampler 0
 
               sampler 0
 
                   texture causticsTiled
 
                   texture causticsTiled
                   textureAddressing tile tile
+
                   textureAddressing tile tile                
 
               end
 
               end
             end
+
                             
 +
             end  
 
         end
 
         end
 
       else
 
       else
 
         shader
 
         shader
 +
       
 
         end
 
         end
 
       endif
 
       endif
 
   end
 
   end
 
enddef
 
enddef
 
+
 
setf causticsStrength 0.8
 
setf causticsStrength 0.8
 
setf causticsBaseStrength 0.5
 
setf causticsBaseStrength 0.5
 +
  
 
###################################################
 
###################################################
 
# Materials
 
# Materials
 
#
 
#
 +
  
 
# Pool surface materials
 
# Pool surface materials
 
materialDefinition "poolWater-0"
 
materialDefinition "poolWater-0"
 
   setDefinition PoolWaterSurfaceMaterial
 
   setDefinition PoolWaterSurfaceMaterial
   addParam stdMatDiffCoef (0, 0, 1)
+
   addParam stdMatDiffCoef (0, 0, 1)  
 
   addParam wmRefractionEnabled true
 
   addParam wmRefractionEnabled true
 
end
 
end
  
 
materialDefinition "poolWater-1"
 
materialDefinition "poolWater-1"
   setDefinition PoolWaterSurfaceMaterial
+
   setDefinition PoolWaterSurfaceMaterial  
 
   addParam stdMatLightingEnabled false
 
   addParam stdMatLightingEnabled false
 
   addParam stdMatLayer 0
 
   addParam stdMatLayer 0
   addParam stdMatDiffCoef (1, 1, 1)
+
 
   addParam wmReflectionTexture water_spec
+
   addParam stdMatDiffCoef (1, 1, 1)  
   addParam wmTransparency 1
+
 
 +
   addParam wmReflectionTexture swimming_pool-envcube
 +
 
 +
   addParam wmTransparency 0.4
 +
 
 
   addParam wmXSpeed        3
 
   addParam wmXSpeed        3
 
   addParam wmXRepeat      5
 
   addParam wmXRepeat      5
   addParam wmXWaveHeight  0.01
+
   addParam wmXWaveHeight  0.01  
 +
 
 
   addParam wmYSpeed        3
 
   addParam wmYSpeed        3
   addParam wmYRepeat      5
+
   addParam wmYRepeat      6
   addParam wmYWaveHeight  0.02
+
   addParam wmYWaveHeight  0.01
 
end
 
end
  
Line 472: Line 630:
 
   addParam stdMatLightingEnabled false
 
   addParam stdMatLightingEnabled false
 
   addParam stdMatLayer 0
 
   addParam stdMatLayer 0
   addParam stdMatDiffCoef (0, 0, 1)
+
 
   addParam wmTransparency 1
+
   addParam stdMatDiffCoef (0, 0, 1)  
 +
 
 +
   addParam wmTransparency 0.5
 +
 
 
   addParam wmXSpeed        3
 
   addParam wmXSpeed        3
   addParam wmXRepeat      5
+
   addParam wmXRepeat      12
   addParam wmXWaveHeight  0.016
+
   addParam wmXWaveHeight  0 #0.02
 +
 
 
   addParam wmYSpeed        3
 
   addParam wmYSpeed        3
   addParam wmYRepeat      5
+
   addParam wmYRepeat      12
   addParam wmYWaveHeight  0.011
+
   addParam wmYWaveHeight  0 # 0.02
 
end
 
end
  
Line 493: Line 655:
 
# is too constrained. We need a layer less than 7, or we'll
 
# is too constrained. We need a layer less than 7, or we'll
 
# render over transparent areas of Sims (e.g. hair) when
 
# render over transparent areas of Sims (e.g. hair) when
# they're in the pool at a shallow view angle.
+
# they're in the pool at a shallow view angle.  
 
setc poolLayerColour (0, 0.5, 1.0, 1.0)
 
setc poolLayerColour (0, 0.5, 1.0, 1.0)
 
define PoolDepthLayerMaterial()
 
define PoolDepthLayerMaterial()
   material
+
   material    
       create DetermineHardwareSupport()
+
       create DetermineHardwareSupport()  
 
       if ($useSWVertexShaderPath or $useFixedFunctionPath)
 
       if ($useSWVertexShaderPath or $useFixedFunctionPath)
 
         shader
 
         shader
Line 505: Line 667:
 
             validateRenderShaderContext -vertexFormat position 0 required
 
             validateRenderShaderContext -vertexFormat position 0 required
 
             validateRenderShaderContext -vertexFormat texcoord 0 required
 
             validateRenderShaderContext -vertexFormat texcoord 0 required
 
+
           
 
             pass
 
             pass
 
               alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
 
               alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
 
      depthTest true -enableDepthWrite false
 
      depthTest true -enableDepthWrite false
                shaderProgram -target vertexProgram -method compile -version 1_1
+
              #fillmode wireframe
 +
              shaderProgram -target vertexProgram -method compile -version 1_1
 
                   bindConstants 0 -bindingID geomToClip -constantCount 4
 
                   bindConstants 0 -bindingID geomToClip -constantCount 4
 
                   bindConstants 4 -bindingID immediateData -data ($poolLayerColour)
 
                   bindConstants 4 -bindingID immediateData -data ($poolLayerColour)
 +
                 
 
                   shaderSource
 
                   shaderSource
 
                     float4x4 clipSpaceMatrix      : register(c0);
 
                     float4x4 clipSpaceMatrix      : register(c0);
 
                     float4  poolLayer            : register(c4);
 
                     float4  poolLayer            : register(c4);
 +
                   
 
                     struct cVertexIn
 
                     struct cVertexIn
 
                     {
 
                     {
                         float3 mPosition : POSITION0;
+
                         float3 mPosition : POSITION0;  
                         float2 alpha    : TEXCOORD0;
+
                         float2 alpha    : TEXCOORD0;                
 
                     };
 
                     };
 +
                   
 
                     struct cVertexOut
 
                     struct cVertexOut
 
                     {
 
                     {
                         float4 mClipPosition : POSITION;
+
                         float4 mClipPosition : POSITION;                    
 
                         float4 mColor        : COLOR0;
 
                         float4 mColor        : COLOR0;
 
                     };
 
                     };
 +
                   
 
                     cVertexOut VertexMain(cVertexIn vertexIn)
 
                     cVertexOut VertexMain(cVertexIn vertexIn)
                     {
+
                     {                
                         cVertexOut result;
+
                         cVertexOut result;                                                                      
                         result.mClipPosition = mul(float4(vertexIn.mPosition, 1), clipSpaceMatrix);
+
                         result.mClipPosition = mul(float4(vertexIn.mPosition, 1), clipSpaceMatrix);                    
                         result.mColor = float4(poolLayer.r, poolLayer.g, poolLayer.b, poolLayer.a * vertexIn.alpha.x);
+
                         result.mColor = float4(poolLayer.r, poolLayer.g, poolLayer.b, poolLayer.a * vertexIn.alpha.x);  
                         return result;
+
                         return result;        
 
                     }
 
                     }
 +
                   
 
                   endShaderSource
 
                   endShaderSource
               end
+
               end                      
 +
             
 
               shaderProgram -target pixelProgram -method compile -version 1_1
 
               shaderProgram -target pixelProgram -method compile -version 1_1
                   shaderSource
+
                   shaderSource                
 
                     float4 PixelMain(float4 color : COLOR) : COLOR
 
                     float4 PixelMain(float4 color : COLOR) : COLOR
                     {
+
                     {                                            
 
                         return color;
 
                         return color;
 
                     }
 
                     }
 
                   endShaderSource
 
                   endShaderSource
               end
+
               end                      
             end
+
             end        
 
         end
 
         end
 
       endif
 
       endif
Line 550: Line 719:
 
materialDefinition "poolWaterLayer-0"
 
materialDefinition "poolWaterLayer-0"
 
   # this layer does nothing but create the caustics tile.
 
   # this layer does nothing but create the caustics tile.
   setDefinition CausticsGeneratorMaterial
+
   setDefinition CausticsGeneratorMaterial  
end
+
end  
  
 
materialDefinition "poolWaterLayer-1"
 
materialDefinition "poolWaterLayer-1"
   setDefinition PoolDepthLayerMaterial
+
   setDefinition PoolDepthLayerMaterial  
   addParam poolLayerColour (0, 0.5, 1.0, 1)
+
   addParam poolLayerColour (0, 0.5, 1.0, 0.2)
 
end
 
end
materialDefinition "poolWaterLayer-2"
+
materialDefinition "poolWaterLayer-2"  
   setDefinition PoolDepthLayerMaterial
+
   setDefinition PoolDepthLayerMaterial
   addParam poolLayerColour (0, 0.5, 1.0, 1)
+
   addParam poolLayerColour (0, 0.5, 1.0, 0.2)  
 
end
 
end
 
materialDefinition "poolWaterLayer-3"
 
materialDefinition "poolWaterLayer-3"
 
   setDefinition PoolDepthLayerMaterial
 
   setDefinition PoolDepthLayerMaterial
   addParam poolLayerColour (0, 0.5, 1.0, 1)
+
   addParam poolLayerColour (0, 0.5, 1.0, 0.5)  
 
end
 
end
  
 
materialDefinition mtl_0
 
materialDefinition mtl_0
 
   setDefinition CausticsGeneratorMaterial
 
   setDefinition CausticsGeneratorMaterial
 +
end
 +
 +
materialDefinition "lightingwallpoolsimple_reflective"
 +
  setDefinition StandardMaterial 
 +
  addParam  stdMatDiffCoef (0.8, 0.8, 0.8) 
 +
  addParam  stdMatAlphaMultiplier 1.0
 +
  addParam  stdMatAlphaBlendMode none
 +
  addParam  stdMatSpecCoef 0.091,0.091,0.091
 +
  addParam  stdMatBaseTextureEnabled true
 +
  addParam  stdMatBaseTextureName lightingwallpoolsimple-surface
 +
  addParam  stdMatEnvCubeLockedToCamera 0
 +
  addParam  stdMatEnvCubeMode reflection
 +
  addParam  stdMatEnvCubeTextureName reflectionkitchenhighcontrast-envcube
 +
  addParam  forceIgnoreShapeBlend true
 
end
 
end
  
 
</PRE>
 
</PRE>

Latest revision as of 02:55, 7 September 2007


EP6-BV
0xCD7FE87A
0x1C0532FA
0x6852819C
0xFF76EA99
# poolwater

##############################################################
# Pool Water
#

seti poolWaterLayer 5


##############################################################
# Simple water material
#
# This material modulates the vertices by two sin waves --
# one in X and one in Y. It then produces reflection and
# refraction direction texture coordinates suitable for a
# cubic environment map.


#beginshader PoolWaterMaterialTwoWave
#description Pool water with X/Y waves

#BeginStdAttrBlock


setf wmTransparency  0.5
#attrdescription Water transparency

setf wmXSpeed        3     # min:0 max:100
#attrdescription Lookup texture for diffuse intensity.
setf wmXRepeat       1     # min:0 max:100
#attrdescription Lookup texture for diffuse intensity.
setf wmXWaveHeight   0.1   # min:0 max:100
#attrdescription Lookup texture for diffuse intensity.

setf wmYSpeed        5     # min:0 max:100
#attrdescription Lookup texture for diffuse intensity.
setf wmYRepeat       1.8   # min:0 max:100
#attrdescription Lookup texture for diffuse intensity.
setf wmYWaveHeight   0.2   # min:0 max:100
#attrdescription Lookup texture for diffuse intensity.

set wmReflectionTexture reflectionoutdoorwater-envcube
#attrdescription Environment cube map texture for reflection

setb wmRefractionEnabled false
#attrdescription Whether refraction is enabled

set wmRefractionTexture reflectionoutdoorwater-envcube
#attrdescription Environment cube map texture for refraction


#EndStdAttrBlock

define PoolWaterSurfaceMaterial()
   material      
      shader -layer $poolWaterLayer
         validateRenderShaderContext -vertexFormat position 0 required
 
         create DetermineHardwareSupport()
    
         if ($useSWVertexShaderPath or $useFixedFunctionPath)
            # The vertex shader to ripple the water surface is deemed too expensive for SWVS.
            # Note that because on some low-end cards shaders are turned off, DetermineHardwareSupport()
            # will set $useFixedFunctionPath to true and $useSWVertexShaderPath to false; however, 
            # since the device is in swvp mode, the RegularWavesHLSL shader would validate anyway, which
            # we don't want. Therefore here we do the simplified water surface if using either SWVS or FFP.
            create SimplifiedWaterSurface()
         else
            if ($useHWShader2Path)
               create HighEndWaterSurface()               
            else
               create RegularWavesHLSL()
            endif
         endif
         
      end #end shader
      
      # basic fallback.
      shader -layer $poolWaterLayer
         create SimplifiedWaterSurface()          
      end #end shader
      
   end  #end material
enddef

define SimplifiedWaterSurface()
   pass -fixedFunction      
      alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
      depthTest true -enableDepthWrite false
      colorScalar (0.2,0.3,1.0) 0.4            
      stage
         textureBlend select(colorScalar) select(colorScalar)
      end
   end
enddef


define RegularWavesHLSL()
	
   #DRIVERBUG
   # The -clipAlways flag is needed to work around what appears to be a driver bug.
   # On NVIDIA GF2 class HW, the presence of the cube map texture in the pass below
   # leads to a large performance drop whenever the pass is rendered with clipping disabled.
   # Rendering this pass with clipping enabled avoids the performance drop. In addition,
   # substituting a 2D texture for the cube map texture, or rendering this pass in HW
   # using fixed function vertex processing also avoids the performance drop (but would obviously
   # not have the desired visual result).
   # The cause of this is unknown. This was observed on a GF4GO, driver version 42.58.
	pass -clipAlways -modifiedEachFrameHint
		#fillmode wireframe
		alphaBlend srcFactor(one) add dstFactor(one)
      seti textureLights (numLightsOfType(environmentCube))        
   
		shaderProgram -target vertexProgram -method compile -version 1_1

			bindConstants 0 -bindingID geomToClip -constantCount 4
			bindConstants 4 -bindingID geomToCamera -constantCount 3

			bindConstants 7 -bindingID frameInfo 

			bindConstants 11 -bindingID immediateData -data ($wmXRepeat, 0, $wmXWaveHeight, $wmXSpeed)
			bindConstants 12 -bindingID immediateData -data (0, $wmYRepeat, $wmYWaveHeight, $wmYSpeed)

			# pre-evaluate these
			setf xscale (-$wmXRepeat * $wmXWaveHeight)
			setf yscale (-$wmYRepeat * $wmYWaveHeight)
			bindConstants 13 -bindingID immediateData -data ($xscale,$yscale, 1,0)
			bindConstants 14 -bindingID allStandardLightData -constantCount 4 -constantType float
			bindConstants 18 -bindingID cameraToGlobal -constantCount 3
         
         if (tsIsDay)         
            bindConstants 25 -bindingID immediateData -data (0.75, 0.75, 0.75, 1)         
         else
            bindConstants 25 -bindingID immediateData -data (0.2, 0.2, 0.2, 1)         
         endif

			shaderSource

				float4 frameInfo : register(c7);
				float4 waveDataX : register(c11);
				float4 waveDataY : register(c12);
				float4 waveDataHelper : register(c13);
				float4x4 clipSpaceMatrix : register(c0);
				float4x3 cameraSpaceMatrix : register(c4);
				float4x3 cameraToGlobalMatrix : register(c18);
				
            float4 nightColor: register(c25);
            
				float4 lightDirection : register(c14);
				float4 lightColor : register(c15);
				const static float4 refractionWeights={1,1,2,0};
				
				const static float4 layerBlue={0.3, 0.7, 1.0, 1};
				
				struct InputVertex
				{
					float3 position: POSITION0;
					float3 normal : NORMAL0;
				};
				
				struct OutputVertex
				{
					float4 clipPosition : POSITION;
					float4 diffuseColor: COLOR0;
					float4 specularColor: COLOR1;
					float3 reflection : TEXCOORD0;
					
				};
            
		
		
				OutputVertex VertexMain( InputVertex inputVertex)
				{
					// Do	Y-direction	waves
					// r0	= (x, y, z,	t)
					
					OutputVertex outputVertex;
					
					float4 posAndTime;
					posAndTime.xyz = inputVertex.position;
					posAndTime.w = frameInfo.w;
					
					float temp = dot(posAndTime, waveDataX);
								
					// z = h * sin(...)

					float z;
					// scale temp to fit -pi +pi range
					//temp = temp *  (1 / (2 * 3.14159)) + 0.5;
					
					float3 waveNormal;
					
					z = sin(temp) * waveDataX.z + inputVertex.position.z;
					
					waveNormal.x = cos(temp) * waveDataHelper.x + inputVertex.normal.x;
					
					temp = dot(posAndTime, waveDataY);
					//temp = temp *  (1 / (2 * 3.14159)) + 0.5;
					
					z +=  sin(temp) * waveDataY.z;
					
					waveNormal.y = cos(temp) * waveDataHelper.y + inputVertex.normal.y;
					
					waveNormal.z = inputVertex.normal.z;
					
					waveNormal = normalize(waveNormal);
					
					posAndTime.w = 1.0;
					posAndTime.z = z;
					
					outputVertex.clipPosition = mul( posAndTime, clipSpaceMatrix);
					
					float3 cameraSpaceNormal = normalize(mul(waveNormal, cameraSpaceMatrix));

					float3 cameraSpacePosition = mul( posAndTime, cameraSpaceMatrix);
					
					float3 viewVector = normalize(-cameraSpacePosition);
					
					float3 R = reflect(viewVector, cameraSpaceNormal);
					
					outputVertex.reflection = mul( -R, cameraToGlobalMatrix);
					
							
					float fresnel = dot(viewVector , cameraSpaceNormal);
							
					float rdotl = saturate(dot(R, lightDirection));
					
					float I = pow(rdotl+0.1, 15); // fudge factor to punch up the highlights.
					
               nightColor = nightColor * 2;                           
                           
					outputVertex.diffuseColor = ((1.0 - fresnel) * saturate(nightColor) * 0.5) * layerBlue;
					
					outputVertex.specularColor = I;
					return(outputVertex);
					
				}


			endShaderSource  
		end # shaderProgram
		
		stage
			texture $wmReflectionTexture
			textureAddressing clamp clamp clamp
			textureBlend multiply(texture diffuse) select(diffuse)
		end
      
      
		addSpecular true
					
	end # end pass


enddef


define HighEndWaterSurface()
   
	pass -clipAlways -modifiedEachFrameHint
		#fillmode wireframe
		alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
      seti textureLights (numLightsOfType(environmentCube))  
      depthTest true -enableDepthWrite false          
   
		shaderProgram -target vertexProgram -method compile -version 1_1

			bindConstants 0 -bindingID geomToClip -constantCount 4
			bindConstants 4 -bindingID geomToCamera -constantCount 3

			bindConstants 7 -bindingID frameInfo 

			bindConstants 11 -bindingID immediateData -data ($wmXRepeat, 0, $wmXWaveHeight, $wmXSpeed)
			bindConstants 12 -bindingID immediateData -data (0, $wmYRepeat, $wmYWaveHeight, $wmYSpeed)

			# pre-evaluate these
			setf xscale (-$wmXRepeat * $wmXWaveHeight)
			setf yscale (-$wmYRepeat * $wmYWaveHeight)
			bindConstants 13 -bindingID immediateData -data ($xscale,$yscale, 1,0)
			bindConstants 14 -bindingID allStandardLightData -constantCount 4 -constantType float
			bindConstants 18 -bindingID cameraToGlobal -constantCount 3
			bindConstants 21 -bindingID cameraToGeom -constantCount 3
         
         if (tsIsDay)         
            bindConstants 25 -bindingID immediateData -data (0.75, 0.75, 0.75, 1)         
         else
            bindConstants 25 -bindingID immediateData -data (0.2, 0.2, 0.2, 1)         
         endif

			shaderSource

				float4 frameInfo : register(c7);
				float4 waveDataX : register(c11);
				float4 waveDataY : register(c12);
				float4 waveDataHelper : register(c13);
				float4x4 clipSpaceMatrix : register(c0);
				float4x3 cameraSpaceMatrix : register(c4);
				float4x3 cameraToGlobalMatrix : register(c18);
				float4x3 cameraToGeomMatrix : register(c21);
				
				
            float4 nightColor: register(c25);
            
				float4 lightDirection : register(c14);
				float4 lightColor : register(c15);
				float4 lightSpecular : register(c16);
				const static float4 refractionWeights={1,1,2,0};
				
				const static float4 layerBlue={0.3, 0.7, 1.0, 1};
				
				struct InputVertex
				{
					float3 position: POSITION0;
					float3 normal : NORMAL0;
				};
				
				struct OutputVertex
				{
					float4 clipPosition : POSITION;					
					float4 sPos         : TEXCOORD0;					
					float2 Wave0        : TEXCOORD1;
               float2 Wave1        : TEXCOORD2;
               float2 Wave2        : TEXCOORD3;
               float2 Wave3        : TEXCOORD4;
               float3 Eye          : TEXCOORD5;
               float4 specular     : COLOR0;     
				};
            
		
		
				OutputVertex VertexMain( InputVertex inputVertex)
				{
					// Do	Y-direction	waves
					// r0	= (x, y, z,	t)
					
					OutputVertex outputVertex;
					
					float4 posAndTime;
					posAndTime.xyz = inputVertex.position;
					posAndTime.w = frameInfo.w;
					
					float temp = dot(posAndTime, waveDataX);					
					float z = sin(temp) * waveDataX.z + inputVertex.position.z;															
					temp = dot(posAndTime, waveDataY);										
					posAndTime.z =  z + sin(temp) * waveDataY.z;									
					posAndTime.w = 1.0f;					 
										
					outputVertex.clipPosition = mul( posAndTime, clipSpaceMatrix);
					outputVertex.sPos = float4(0.5f*(outputVertex.clipPosition.ww - outputVertex.clipPosition.xy), outputVertex.clipPosition.ww);					
					
					float3 waveNormal = normalize(inputVertex.normal + float3(cos(temp)*waveDataHelper.xy, 0));															
		         float3 cameraSpaceNormal = normalize(mul(waveNormal, cameraSpaceMatrix));					
					float3 cameraSpacePosition = mul( posAndTime, cameraSpaceMatrix);					
					float3 viewVector = normalize(-cameraSpacePosition);	
					
					float3 halfVector = normalize(viewVector + lightDirection);
					outputVertex.specular = lightSpecular*pow(dot(cameraSpaceNormal, halfVector), 50)*nightColor*lightColor*0.5;									
					
					
					float2 fTranslation=float2(fmod(frameInfo.w, 100)*0.005, fmod(frameInfo.w, 100)*0.005);
               float2 vTexCoords = posAndTime.xy*0.05;
                
               // Output bump layers texture coordinates    
               float fSinTranslation=sin(fTranslation*100)*0.005;
               float2 vTranslation0=fTranslation+fSinTranslation;
               float2 vTranslation1=fTranslation-fSinTranslation;
               float2 vTranslation2=fTranslation;
                
               // Scale texture coordinates to get mix of low/high frequency details
               outputVertex.Wave0.xy = vTexCoords.xy+fTranslation*2.0;
               outputVertex.Wave1.xy = vTexCoords.xy*2.0+fTranslation*4.0;
               outputVertex.Wave2.xy = vTexCoords.xy*7.0+fTranslation*2.0;
               outputVertex.Wave3.xy = vTexCoords.xy*12.0+fTranslation;                															
	                  	                  
	            // compute binormal
	            float3 binormal = normalize(cross(waveNormal, float3(1.0f, 0.0f, 0.0f)));	            
	            float3 tangent = normalize(cross(binormal, waveNormal));
	            
	            // tangent space matrix
	            float3x3 objToTangentSpace = float3x3(tangent, binormal, waveNormal);
	            
	            float3 viewVectorInObjSpace = mul(viewVector, (float3x3)cameraToGeomMatrix);
	            
	            outputVertex.Eye.xyz = mul(objToTangentSpace, viewVectorInObjSpace);
												
					
					return(outputVertex);					
				}


			endShaderSource  
		end # shaderProgram
		
		shaderProgram -target pixelProgram -method compile -version 2_0              
         shaderSource                            
            sampler reflect;  
            sampler bump;          
            struct cInputPixel
            {               
               float4 sPos         : TEXCOORD0;               
               float2 Wave0        : TEXCOORD1;
               float2 Wave1        : TEXCOORD2;
               float2 Wave2        : TEXCOORD3;
               float2 Wave3        : TEXCOORD4;
               float3 Eye          : TEXCOORD5;
               float4 specular     : COLOR0;
            };
            
            float Fresnel(float NdotL, float fresnelBias, float fresnelPow, float facing)
            {               
               return max(fresnelBias + (1.0f - fresnelBias)*pow(facing, fresnelPow), 0.0);
            }
            
            float4 PixelMain(cInputPixel pi) : COLOR
            {         
               float3 vEye = normalize(pi.Eye);

               // Get bump layers
               float3 vBumpTexA = tex2D(bump, pi.Wave0.xy).xyz;
               float3 vBumpTexB = tex2D(bump, pi.Wave1.xy).xyz;
               float3 vBumpTexC = tex2D(bump, pi.Wave2.xy).xyz;
               float3 vBumpTexD = tex2D(bump, pi.Wave3.xy).xyz;
                 
               // Average bump layers
               float3 vBumpTex=normalize(2.0 * (vBumpTexA.xyz + vBumpTexB.xyz + vBumpTexC.xyz + vBumpTexD.xyz)-4.0);
                 
               // Apply individual bump scale for refraction and reflection               
               float3 vReflBump = vBumpTex.xyz * float3(0.1, 0.1, 1.0);  
               
               float4 vReflection = tex2Dproj(reflect, pi.sPos + float4(vReflBump.xy, 0.0f, 0.0f));//*0.001 + float4(2.0*vBumpTexD-1.0f.xxx, 1);
               
               // Compute Fresnel term
               float NdotL = max(dot(vEye, vReflBump), 0);
               float facing = (1.0 - NdotL);
               float fresnel = Fresnel(NdotL, 0.2, 5.0, facing);
                              
               vReflection.a = fresnel;               
               return saturate(vReflection + float4(0.0, 0.25, 0.5, 0.0) + pi.specular);
            }
         endShaderSource
      end                                                     
      
      sampler reflect
         texture "PoolReflection"
         textureAddressing clamp clamp
      end		
      
      sampler bump
         texture "poolShape-body-bump"
         textureAddressing tile tile
      end   
            
	end # end pass
   
enddef


#endshader PoolWaterSurfaceMaterial



# NOTE: 
# refraction in vector terms:
#   v  = incoming unit vector
#   n  = surface normal
#   v' = refracted vector
#   a  = refraction ratio, ni / nr =~ 0.75 for air->water
#
# v' = v - sn
# where s = sqrt(1 - a^2 (1 - (v.n)^2))  - v.n.



##########################################################
# Caustics
#

define TiledTextureAnimShaderProgram(tilesX tilesY speed)
   shaderProgram -target vertexProgram -method assemble
   
      bindConstants 0 -bindingID frameInfo            # for time in .w
      bindConstants 1 -data      (&tilesX, &tilesY, &speed, 1)         # tx, ty, speed
      bindConstants 2 -data      ((1/&tilesX), (1/&tilesY), (&tilesY - 1), 1) # 1/tx 1/ty ty-1      
      if (tsIsDay)
         bindConstants 3 -bindingID immediateData -data (0.75, 0.75, 0.75, 1)
      else
         bindConstants 3 -bindingID immediateData -data (0.2, 0.2, 0.2, 1)
      endif
      
      shaderSource
         vs_1_1
         dcl_position v0
         dcl_texcoord v1

         mov oPos, v0  # clip space quad, no transforms needed.
      
         # tiled texture animation
         mov r0, c0
         mul r1, c1.z, r0.w
         frc r5.y, r1.y
         
         mul r1, c1.y, r5.y    # f -> [0, ty)
         frc r5.y, r1
         sub r3.y, r1, r5.y    # v' = floor(f)
         
         mul r1, c1.x, r5.y    # f -> [0, tx)
         frc r5.y, r1
         sub r3.x, r1, r5.y    # u' = floor(f)

         add r8.xy, v1.xy,  r3.xy
         mul oT0.xy, r8.xy, c2.xy
         
         mov oD0, c3 # also spit out diffuse color with modcolor information
         
      endShaderSource
   
   end		
enddef

define CausticsGeneratorMaterial()   
   material      
      # This material updates a render target with the current frame
      # of the tile animation. This is the only way to handle a repeated
      # animating texture (not to mention using it for projective texturing)
      # in the absence of clip maps.
      
      create DetermineHardwareSupport()
      
      if ($causticsEnabled and $useFixedFunctionPath = false and $useSWVertexShaderPath = false)
      
         shader -layer +9999 

            pass              
               renderClipSpaceRect
               
               renderTarget causticsTile -fixed (64, 64) -allocateDepthBuffer false -undo

               create TiledTextureAnimShaderProgram(8 4 1)
               
               alphaBlend srcFactor(one) add dstFactor(zero)
               alphaTest false 0
               alphaTestFunction acceptIfGreater

               depthTest false -enableDepthWrite false
               depthTestFunction accept

               # 7/24/2004 Fix bug with kRenderTypeNormal default stencil state and nv40.  
               # It reads random stencil values even this target has no depth stencil target.               
               # This stencil call will break pixo, but pixo does not show caustics.
               stencil false

               fillmode $stdMatFillMode                             
               
               shaderProgram -target pixelProgram -method compile -version 1_1               
                  shaderSource                            
                     sampler caustics;
                     struct cInputPixel
                     {
                        float4 color : COLOR;
                        float2 tc0 : TEXCOORD0;
                     };
                     float4 PixelMain(cInputPixel pi) : COLOR
                     {         
                        float4 texColor = tex2D(caustics, pi.tc0);                                                
                        return texColor*pi.color;                        
                     }
                  endShaderSource
               end                                                     
               
               sampler 0
                  texture causticsTiled
                  textureAddressing tile tile                  
               end
                              
            end   
         end
      else
         shader
         
         end
      endif
   end
enddef
 
setf causticsStrength 0.8
setf causticsBaseStrength 0.5


###################################################
# Materials
#


# Pool surface materials
materialDefinition "poolWater-0"
   setDefinition PoolWaterSurfaceMaterial
   addParam stdMatDiffCoef (0, 0, 1)   
   addParam wmRefractionEnabled true
end

materialDefinition "poolWater-1"
   setDefinition PoolWaterSurfaceMaterial   
   addParam stdMatLightingEnabled false
   addParam stdMatLayer 0
   
   addParam stdMatDiffCoef (1, 1, 1)   

   addParam wmReflectionTexture swimming_pool-envcube

   addParam wmTransparency 0.4

   addParam wmXSpeed        3
   addParam wmXRepeat       5
   addParam wmXWaveHeight   0.01 
   
   addParam wmYSpeed        3
   addParam wmYRepeat       6
   addParam wmYWaveHeight   0.01 
end

materialDefinition "poolWater-2"
   setDefinition PoolWaterSurfaceMaterial
   addParam stdMatLightingEnabled false
   addParam stdMatLayer 0
   
   addParam stdMatDiffCoef (0, 0, 1)   

   addParam wmTransparency 0.5

   addParam wmXSpeed        3
   addParam wmXRepeat       12
   addParam wmXWaveHeight   0 #0.02 
   
   addParam wmYSpeed        3
   addParam wmYRepeat       12 
   addParam wmYWaveHeight   0 # 0.02 
end

materialDefinition "poolWater-4"
   setDefinition WaterAnimatingTextures
   addParam waterSpeed 0.3
end


# Pool depth layer materials

# this is here soley because the layering on standard material
# is too constrained. We need a layer less than 7, or we'll
# render over transparent areas of Sims (e.g. hair) when
# they're in the pool at a shallow view angle. 
setc poolLayerColour (0, 0.5, 1.0, 1.0)
define PoolDepthLayerMaterial()
   material     
      create DetermineHardwareSupport() 
      if ($useSWVertexShaderPath or $useFixedFunctionPath)
         shader
         end
      else
         shader -layer $poolWaterLayer
            validateRenderShaderContext -vertexFormat position 0 required
            validateRenderShaderContext -vertexFormat texcoord 0 required
            
            pass
               alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
			      depthTest true -enableDepthWrite false
               #fillmode wireframe
               shaderProgram -target vertexProgram -method compile -version 1_1
                  bindConstants 0 -bindingID geomToClip -constantCount 4
                  bindConstants 4 -bindingID immediateData -data ($poolLayerColour)
                  
                  shaderSource
                     float4x4 clipSpaceMatrix      : register(c0);
                     float4   poolLayer            : register(c4);
                     
                     struct cVertexIn
                     {
                        float3 mPosition : POSITION0;   
                        float2 alpha     : TEXCOORD0;                  
                     };
                     
                     struct cVertexOut
                     {
                        float4 mClipPosition : POSITION;                     
                        float4 mColor        : COLOR0;
                     };
                     
                     cVertexOut VertexMain(cVertexIn vertexIn)
                     {                 
                        cVertexOut result;                                                                        
                        result.mClipPosition = mul(float4(vertexIn.mPosition, 1), clipSpaceMatrix);                     
                        result.mColor = float4(poolLayer.r, poolLayer.g, poolLayer.b, poolLayer.a * vertexIn.alpha.x); 
                        return result;         
                     }
                     
                  endShaderSource
               end                       
               
               shaderProgram -target pixelProgram -method compile -version 1_1
                  shaderSource                  
                     float4 PixelMain(float4 color : COLOR) : COLOR
                     {                                             
                        return color;
                     }
                  endShaderSource
               end                        
            end         
         end
      endif
   end
enddef

materialDefinition "poolWaterLayer-0"
   # this layer does nothing but create the caustics tile.
   setDefinition CausticsGeneratorMaterial   
end 

materialDefinition "poolWaterLayer-1"
   setDefinition PoolDepthLayerMaterial   
   addParam poolLayerColour (0, 0.5, 1.0, 0.2)
end
materialDefinition "poolWaterLayer-2" 
   setDefinition PoolDepthLayerMaterial  
   addParam poolLayerColour (0, 0.5, 1.0, 0.2)    
end
materialDefinition "poolWaterLayer-3"
   setDefinition PoolDepthLayerMaterial
   addParam poolLayerColour (0, 0.5, 1.0, 0.5)   
end

materialDefinition mtl_0
   setDefinition CausticsGeneratorMaterial
end

materialDefinition "lightingwallpoolsimple_reflective"
   setDefinition StandardMaterial   
   addParam   stdMatDiffCoef (0.8, 0.8, 0.8)   
   addParam   stdMatAlphaMultiplier 1.0
   addParam   stdMatAlphaBlendMode none
   addParam   stdMatSpecCoef 0.091,0.091,0.091
   addParam   stdMatBaseTextureEnabled true
   addParam   stdMatBaseTextureName lightingwallpoolsimple-surface
   addParam   stdMatEnvCubeLockedToCamera 0
   addParam   stdMatEnvCubeMode reflection
   addParam   stdMatEnvCubeTextureName reflectionkitchenhighcontrast-envcube
   addParam   forceIgnoreShapeBlend true
end

Personal tools
Namespaces

Variants
Actions
Navigation
game select
Toolbox