/// 使用するデータのセット public void Start(DemoModel.BasicModel useModel, DemoModel.TexContainer useTexCnr, DemoModel.ShaderContainer useShaderCnr) { this.useModel = useModel; this.useTexCnr = useTexCnr; this.useShaderCnr = useShaderCnr; this.useModel.BindTextures(this.useTexCnr); }
/// Matrial + WorldCount + LightCount から利用するシェーダを検索する public BasicProgram findBasicProgram( ShaderContainer shaderContainer, BasicMaterial material, int worldCount, int lightCount ) { BasicProgram program; if( material.BasicShader[ worldCount, lightCount] == null ){ if( material.ShaderName == "" || material.ShaderName == "basic"){ if( material.ShaderNameObj == null ){ material.ShaderNameObj = new BasicShaderName( worldCount, this.lightCount, material ); }else{ material.ShaderNameObj.SetWorldAndLight( worldCount, this.lightCount ); } program = shaderContainer.Find( material.ShaderNameObj ); material.BasicShader[ worldCount, lightCount] = program; }else{ // 特殊 program = shaderContainer.Find( material.ShaderName ); material.BasicShader[ worldCount, lightCount] = program; // uniform foreach( KeyValuePair< string, int > entry in material.BlindData ){ int id = program.FindUniform( entry.Key ); if( id >= 0 ){ program.SetUniformValue( id, entry.Value / 1000.0f ); } } } }else{ program = material.BasicShader[ worldCount, lightCount ]; } return program; }
/// BasicMaterial に設定された ShaderName から、ShaderProgram を探して描画する public void Draw( GraphicsContext graphics, ShaderContainer shaderContainer, Matrix4 viewProj, Vector4 eye ) { foreach ( var bone in Bones ) { int worldCount = 0; int[] blendBones = bone.BlendBones ; if ( blendBones == null ) { worldCount = 1; matrixBuffer = new Matrix4[ 1 ] ; matrixBuffer[ 0 ] = bone.WorldMatrix; }else{ int blendCount = blendBones.Length ; if ( blendCount > matrixBuffer.Length ) matrixBuffer = new Matrix4[ blendCount ] ; for ( int i = 0 ; i < blendCount ; i ++ ) { matrixBuffer[ i ] = Bones[ blendBones[ i ] ].WorldMatrix * bone.BlendOffsets[ i ] ; } } int[] blendSubset = defaultBlendSubset ; foreach ( var index in bone.DrawParts ) { BasicPart part = Parts[ index ] ; foreach ( var mesh in part.Meshes ) { if ( blendBones != null && blendSubset != mesh.BlendSubset ) { blendSubset = ( mesh.BlendSubset != null ) ? mesh.BlendSubset : defaultBlendSubset ; worldCount = blendSubset.Length ; if ( worldCount > blendBones.Length ) worldCount = blendBones.Length ; } BasicMaterial material = Materials[ mesh.Material ]; if( material != null ){ setPolygonMode( graphics, material ); BasicProgram program = findBasicProgram( shaderContainer, material, worldCount, this.lightCount ); graphics.SetShaderProgram( program ); program.SetViewProj( viewProj ); program.SetMaterial( ref material ); if( material.LightEnable != 0 ){ program.SetLightCount( lightCount ); // program.SetLights( ref this.WorldMatrix, ref eye, ref lights ); program.SetLights( ref matrixBuffer[ 0 ], ref eye, ref lights ); }else{ program.SetLightCount( 0 ); } setLayers( graphics, program, material ); program.SetWorldCount( worldCount ); for( int i = 0; i < worldCount; i++ ){ program.SetMatrixPalette( i, ref matrixBuffer[ blendSubset[ i ] ] ); } program.Update(); } graphics.SetVertexBuffer( 0, mesh.VertexBuffer ) ; graphics.DrawArrays( mesh.Primitives ) ; } } } }
/// BasicMaterial に設定された ShaderName から、ShaderProgram を探して描画する public void Draw( GraphicsContext graphics, ShaderContainer shaderContainer, Matrix4 viewProj, Vector4 eye, float Brightness ) { foreach (var bone in Bones) { int worldCount = 0; // sky00_nami; // bone.Name = "sky00_nami"; int[] blendBones = bone.BlendBones; if (blendBones == null) { worldCount = 1; matrixBuffer = new Matrix4[1]; matrixBuffer[0] = bone.WorldMatrix; } else { int blendCount = blendBones.Length; if (blendCount > matrixBuffer.Length) { matrixBuffer = new Matrix4[blendCount]; } for (int i = 0; i < blendCount; i++) { matrixBuffer[i] = Bones[blendBones[i]].WorldMatrix * bone.BlendOffsets[i]; } } int[] blendSubset = defaultBlendSubset; foreach (var index in bone.DrawParts) { BasicPart part = Parts[index]; foreach (var mesh in part.Meshes) { if (blendBones != null && blendSubset != mesh.BlendSubset) { blendSubset = (mesh.BlendSubset != null) ? mesh.BlendSubset : defaultBlendSubset; worldCount = blendSubset.Length; if (worldCount > blendBones.Length) { worldCount = blendBones.Length; } } BasicMaterial material = Materials[mesh.Material]; if (material != null) { setPolygonMode(graphics, material); BasicProgram program = findBasicProgram(shaderContainer, material, worldCount, this.lightCount); graphics.SetShaderProgram(program); program.SetViewProj(viewProj); program.SetMaterial(ref material); if (material.LightEnable != 0) { program.SetLightCount(lightCount); program.SetLights(ref matrixBuffer[0], ref eye, ref lights); } else { program.SetLightCount(0); } program.SetRateLight(Brightness); setLayers(graphics, program, material); program.SetWorldCount(worldCount); for (int i = 0; i < worldCount; i++) { program.SetMatrixPalette(i, ref matrixBuffer[blendSubset[i]]); } program.Update(); } graphics.SetVertexBuffer(0, mesh.VertexBuffer); graphics.DrawArrays(mesh.Primitives); } } } }
/// 終了 public void End() { useModel = null; useTexCnr = null; useShaderCnr = null; }
/// 破棄 public void Term() { useModel = null; useTexCnr = null; useShaderCnr = null; }
/// コンストラクタ public ModelHandle() { useModel = null; useTexCnr = null; useShaderCnr = null; }