ShaderMirror

From SimsWiki
Revision as of 02:04, 16 August 2007 by Niol (Talk | contribs)

Jump to: navigation, search

version 4

set reflectionRenderTarget "ScreenReflection"

set mvAspect 1


define SimpleMirrorReflection()
   material
      if (viewerRenderType = $kRenderTypeShadow)
         create StandardShaderShadow()
      else
         if (viewerRenderType = $kRenderTypeThumbnail)
            create BlankMirrorMaterial($kRenderTypeThumbnail)
         else            
            if (not $mirrorReflectionsEnabled)
               set currentType (viewerRenderType)
               create BlankMirrorMaterial($currentType)
            else
               create MirrorReflectionMaterial()
            endif            
         endif         
      endif
   end
enddef

define BlankMirrorMaterial(renderType)
      shader
         validateRenderShaderContext -viewerRenderType  &renderType
         
         pass             
            shaderProgram -target vertexProgram -method assemble
               bindConstants 0 -bindingID geomToClip -constantCount 4
               shaderSource
                  vs_1_1
                  dcl_position v0
                  m4x4 oPos, v0, c0
               endShaderSource
            end
            
            colorScalar (0.5,0.6,0.75, 1)
            stage
               textureBlend select(colorScalar) select(colorScalar)
            end            
         end
      end      
enddef

 
define MirrorReflectionMaterial()
      
      shader -layer -9998   # this material must render first
         validateRenderShaderContext -viewerRenderType $kRenderTypeMirror  # reflection render

         pass -renderEachFrame
            renderTarget $reflectionRenderTarget -setViewport viewportFromParentRenderContext

            depthTest true -enableDepthWrite true

            applyStencilStateForOverlappingReflections

            alphaBlend srcFactor(zero) add dstFactor(one)

            cullmode none

            shaderProgram -target vertexProgram -method assemble            
               bindConstants 0 -bindingID geomToClipFromParentView -constantCount 4
               shaderSource
                  vs_1_1
                  dcl_position v0
                  def c5, 1,0,0,1
                  m4x4 r0,   v0,  c0
                  mov oPos.x,  -r0
                  mov oPos.yzw, r0                  
               endShaderSource
            end
         end
      end

      shader -layer 6
         validateRenderShaderContext -viewerRenderType $kRenderTypeNormal
         pass -modifiedEachFrameHint
            shaderProgram -target vertexProgram -method assemble            
               bindConstants 0 -bindingID geomToClip -constantCount 4
               bindConstants 4 -bindingID clipToViewTarget -constantCount 4
               shaderSource
                  vs_1_1
                  dcl_position v0
                  def c8,    -0.5, -0.5, 1, 1
                  def c9,     0.5,  0.5, 0, 0 
                  def c10,    0.25, 0.25, 0, 0
                  m4x4 r0,   v0,  c0
                  mov oPos, r0
                  mul r2, c9, r0.w
                  mad r4, r0, c8, r2
                  rcp r3.x, c4.x                                   
                  rcp r3.y, c5.y
                  mov r3.zw, c9.zw
                  mul r3, r3, c10
                  mad r6, r0, r3, r4
                  mov oT0, r6.xyww
               endShaderSource               
            end
            
            stage
               textureTransformType vector3 homogeneous
               textureAddressing clamp clamp
               texture $reflectionRenderTarget
               textureBlend select(texture) select(texture)
            end
                       
         end
      end
      
      shader -layer 6      
         validateRenderShaderContext -viewerRenderType $kRenderTypeNormal
         pass -modifiedEachFrameHint
            shaderProgram -target vertexProgram -method assemble            
               bindConstants 0 -bindingID geomToClip -constantCount 4
               bindConstants 4 -bindingID clipToViewTarget -constantCount 4
               shaderSource
                  vs_1_1
                  dcl_position v0
                  def c8,    -0.5, -0.5, 1, 1
                  def c9,     0.5,  0.5, 0, 0
                  def c10,    0.25, 0.25, 0, 0
                  m4x4 r0,   v0,  c0
                  mov oPos, r0
                  mul r2, c9, r0.w
                  mad r4, r0, c8, r2
                  rcp r3.x, c4.x                                   
                  rcp r3.y, c5.y
                  mov r3.zw, c9.zw
                  mul r3, r3, c10
                  mad r1, r0, r3, r4
                  rcp r1.w, r1.w
                  mul oT0.xy, r1.w, r1.xy
               endShaderSource               
            end
            
            stage
               textureAddressing clamp clamp
               texture $reflectionRenderTarget
               textureBlend select(texture) select(texture)
            end
            
         end
      end
enddef


define SetSpecifiedZOnMirrorRTT(zValue)
   material
      shader      
         pass         
            renderClipSpaceRect
            renderTarget $reflectionRenderTarget -setViewport fullRenderTargetViewport
            stencil false
            depthTest true -enableDepthWrite true
            depthTestFunction accept
            alphaBlend srcFactor(zero) add dstFactor(one)
            shaderProgram -target vertexProgram -method compile -version 1_1
               bindConstants 0 -bindingID immediateData -data (0,0,&zValue, 0)
               shaderSource
                  float4 zValue : register(c0);
                  void VertexMain(float4 position : POSITION, out float4 oPosition : POSITION)
                  {
                     oPosition = position;
                     oPosition.z = zValue.z;    // set specified z value
                  }
               endShaderSource
            end
         end
      end
   end
enddef

define SetNearZOnMirrorRTT()
   create SetSpecifiedZOnMirrorRTT(0)
enddef

define SetFarZOnMirrorRTT()
   create SetSpecifiedZOnMirrorRTT(1)
enddef

define SetStencilOnCensorRTT()
   material
      shader       
         pass 
            renderClipSpaceRect
            stencil true
            stencilFunction accept -refValue 1
            stencilOperation -onPass writeRef
            depthTest false -enableDepthWrite false
            depthTestFunction accept
            alphaBlend srcFactor(zero) add dstFactor(one)
            shaderProgram -target vertexProgram -method assemble
               shaderSource
                  vs_1_1
                  dcl_position v0
                  mov oPos, v0
               endShaderSource
            end
            
            stage
               textureBlend select(colorScalar) select(colorScalar)
            end
            
         end
      end
   end
enddef

defaultState $kRenderTypeMirror
   stencil true
   stencilFunction acceptIfEqual -refValue 1
   stencilOperation -onPass noWrite
end

defaultState $kRenderTypeNormal
   stencil true
   stencilFunction acceptIfEqual -refValue 0
   stencilOperation -onPass noWrite
end

#---------------------------------------------------------------------------------------------------------------#

define TransparentMirrorReflection()
   material
      if (viewerRenderType = $kRenderTypeShadow)
         create StandardShaderShadow()
      else
         if (viewerRenderType = $kRenderTypeThumbnail)
            create TransparentBlankMirrorMaterial($kRenderTypeThumbnail)
         else            
            if (not $mirrorReflectionsEnabled)
               set currentType (viewerRenderType)
               create TransparentBlankMirrorMaterial($currentType)
            else
               create TransparentMirrorReflectionMaterial()
            endif            
         endif         
      endif
   end
enddef

define TransparentBlankMirrorMaterial(renderType)
      shader
         validateRenderShaderContext -viewerRenderType  &renderType
         
         pass             
            shaderProgram -target vertexProgram -method assemble
               bindConstants 0 -bindingID geomToClip -constantCount 4
               shaderSource
                  vs_1_1
                  dcl_position v0
                  m4x4 oPos, v0, c0
               endShaderSource
            end
            
            colorScalar (0,0,0) 0.5
            stage
               textureBlend select(colorScalar) select(colorScalar)
            end            
         end
      end      
enddef

define TransparentMirrorReflectionMaterial()
      
      shader -layer -9998   # this material must render first
         validateRenderShaderContext -viewerRenderType $kRenderTypeMirror  # reflection render

         pass -renderEachFrame
            renderTarget $reflectionRenderTarget -setViewport viewportFromParentRenderContext

            depthTest true -enableDepthWrite true

            applyStencilStateForOverlappingReflections

            alphaBlend srcFactor(zero) add dstFactor(one)

            cullmode none

            shaderProgram -target vertexProgram -method assemble            
               bindConstants 0 -bindingID geomToClipFromParentView -constantCount 4
               shaderSource
                  vs_1_1
                  dcl_position v0
                  def c5, 1,0,0,1
                  m4x4 r0,   v0,  c0
                  mov oPos.x,  -r0
                  mov oPos.yzw, r0                  
               endShaderSource
            end
         end
      end

      shader -layer 6
         validateRenderShaderContext -viewerRenderType $kRenderTypeNormal
         pass -modifiedEachFrameHint
            shaderProgram -target vertexProgram -method assemble            
               bindConstants 0 -bindingID geomToClip -constantCount 4
               bindConstants 4 -bindingID clipToViewTarget -constantCount 4
               shaderSource
                  vs_1_1
                  dcl_position v0
                  def c8,    -0.5, -0.5, 1, 1
                  def c9,     0.5,  0.5, 0, 0 
                  def c10,    0.25, 0.25, 0, 0
                  m4x4 r0,   v0,  c0
                  mov oPos, r0
                  mul r2, c9, r0.w
                  mad r4, r0, c8, r2
                  rcp r3.x, c4.x                                   
                  rcp r3.y, c5.y
                  mov r3.zw, c9.zw
                  mul r3, r3, c10
                  mad r6, r0, r3, r4
                  mov oT0, r6.xyww
               endShaderSource               
            end

            alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
            depthTest true -enableDepthWrite false
            colorScalar (0,0,0) 0.25

            stage
               textureTransformType vector3 homogeneous
               textureAddressing clamp clamp
               texture $reflectionRenderTarget
               textureBlend select(texture) select(colorScalar)
            end
                       
         end
      end
      
      shader -layer 6      
         validateRenderShaderContext -viewerRenderType $kRenderTypeNormal
         pass -modifiedEachFrameHint
            shaderProgram -target vertexProgram -method assemble            
               bindConstants 0 -bindingID geomToClip -constantCount 4
               bindConstants 4 -bindingID clipToViewTarget -constantCount 4
               shaderSource
                  vs_1_1
                  dcl_position v0
                  def c8,    -0.5, -0.5, 1, 1
                  def c9,     0.5,  0.5, 0, 0
                  def c10,    0.25, 0.25, 0, 0
                  m4x4 r0,   v0,  c0
                  mov oPos, r0
                  mul r2, c9, r0.w
                  mad r4, r0, c8, r2
                  rcp r3.x, c4.x                                   
                  rcp r3.y, c5.y
                  mov r3.zw, c9.zw
                  mul r3, r3, c10
                  mad r1, r0, r3, r4
                  rcp r1.w, r1.w
                  mul oT0.xy, r1.w, r1.xy
               endShaderSource               
            end

            alphaBlend srcFactor(srcAlpha) add dstFactor(invSrcAlpha)
            depthTest true -enableDepthWrite false
            colorScalar (0,0,0) 0.25

            stage
               textureAddressing clamp clamp
               texture $reflectionRenderTarget
               textureBlend select(texture) select(colorScalar)
            end
            
         end
      end
enddef

#---------------------------------------------------------------------------------------------------------------#

materialDefinition transparent_mirror_reflection
   setDefinition TransparentMirrorReflection
end

#---------------------------------------------------------------------------------------------------------------#

materialDefinition simple_mirror_reflection
   setDefinition SimpleMirrorReflection
end

materialDefinition MirrorBackPostReflectionRenderMaterial
   setDefinition MirrorBackClearStencilAndSetDepth
end

# bound to the bounding mesh around the mirror plane that prevents the mirror from being
# undesirably culled
materialDefinition MirrorBoundingBoxMaterial
   setDefinition Null
end

# used to clear the mirror render target to z = 0 wherever the stencil has been set to 1
materialDefinition ZClearToNearPlaneMaterial
   setDefinition SetNearZOnMirrorRTT
end

# used to clear the mirror render target to z = 0 wherever the stencil has been set to 1
materialDefinition ZClearToFarPlaneMaterial
   setDefinition SetFarZOnMirrorRTT
end

# bound to a compositing pass geometry that preps the stencil buffer for rendering into
# the censor render target from the mirror view
materialDefinition CensorInMirrorStencilPrepMaterial
   setDefinition SetStencilOnCensorRTT
end

# End DO NOT MODIFY THIS BLOCK

Personal tools
Namespaces

Variants
Actions
Navigation
game select
Toolbox