Tree::Tree() : Mesh(), bFire(false), bLeaves(false), counter(0.0), bFog(false), render_mode(3), fx_m_World(0), fx_m_WorldViewProj(0), fx_m_L_ViewProj(0), fx_tex_shadow_map(0), fx_lights(0), fx_num_lights(0), fx_pEye(0), fx_bShadowed(0), fx_bUsePCSS(0), srv_heightmap(0), srv_sand(0) { isShadowCaster(true); isShadowReceiver(true); }
void Material::setShader(Shader* shader) { bool is_alpha = isAlphaCutout(); bool is_receiver = isShadowReceiver(); if (m_shader) { Shader* shader = m_shader; m_shader = nullptr; removeDependency(*shader); m_resource_manager.get(ResourceManager::SHADER)->unload(*shader); } m_shader = shader; if (m_shader) { addDependency(*m_shader); if (m_shader->isReady()) { m_shader_mask = 0; enableShadowReceiving(is_receiver); enableAlphaCutout(is_alpha); for (int i = 0; i < m_shader->getTextureSlotCount(); ++i) { if (m_shader->getTextureSlot(i).m_define_idx >= 0 && m_textures[i]) { m_shader_mask |= m_shader->getDefineMask( m_shader->getTextureSlot(i).m_define_idx); } } m_shader_instance = &m_shader->getInstance(m_shader_mask); } } }
void Tree::draw(const Camera& camera, const vector<Light*>& lights) { ID3DX11EffectScalarVariable* fx_time = getFX()->GetVariableByName("border")->AsScalar(); fx_time->SetFloat(counter); if (bLeaves) _leaves.draw(camera, lights); // Material Mesh::setMaterial(getFX()); context()->IASetInputLayout(getInputLayout()); // World Matrix XMMATRIX mWorld = XMLoadFloat4x4(&(mtxWorld())); fx_m_World->SetMatrix((float*)&mWorld); fx_m_WorldViewProj->SetMatrix((float*)&(mWorld * camera.getViewMatrix() * camera.getProjectionMatrix())); fx_pEye->SetFloatVector((float*)&(camera.getPosVector3())); // Light // mView, shadow_map, light params for (unsigned int i=0; i<lights.size(); ++i) { fx_m_L_ViewProj = getFX()->GetVariableByName("m_L_ViewProj")->GetElement(i)->AsMatrix(); fx_lights = getFX()->GetVariableByName("lights")->GetElement(i); fx_tex_shadow_map = getFX()->GetVariableByName("tex_shadow_map")->GetElement(i)->AsShaderResource(); fx_m_L_ViewProj->SetMatrix( (float*)&(lights[i]->getViewProjMatrix()) ); fx_tex_shadow_map->SetResource( lights[i]->getShadowMapSRV()) ; fx_lights->SetRawValue( (void*)&(lights[i]->getLightStructure()), 0, sizeof(Light_Params) ); } ID3DX11EffectScalarVariable* fx_shadow_size = getFX()->GetVariableByName("shadow_size")->AsScalar(); fx_shadow_size->SetInt(lights[0]->getShadowMapSize()); fx_num_lights->SetInt(lights.size()); fx_bShadowed->SetBool(isShadowReceiver()); fx_bUsePCSS->SetBool(PCSS()); ID3DX11EffectScalarVariable* fx_bLit = getFX()->GetVariableByName("bLit")->AsScalar(); fx_bLit->SetBool(isLit()); if (render_mode == Tree::NonTexturedNonLitWireframe) Mesh::draw(0, getFX(), "Tree_NonTexturedNonLitWireframe"); else if (render_mode == Tree::NonTexturedFlatShaded) Mesh::draw(0, getFX(), "Tree_NonTexturedFlatShaded"); else if (render_mode == Tree::NonTexturedSmoothShaded) Mesh::draw(0, getFX(), "Tree_NonTexturedSmoothShaded"); else if (render_mode == Tree::TexturedSmoothShaded) Mesh::draw(0, getFX(), "Tree"); else { ID3DX11EffectShaderResourceVariable* fx_tex_heightmap = getFX()->GetVariableByName("tex_heightmap")->AsShaderResource(); fx_tex_heightmap->SetResource(srv_heightmap); Mesh::draw(0, getFX(), "Tree_DisplacementMapping"); } for (unsigned int i=0; i<lights.size(); ++i) { fx_tex_shadow_map = getFX()->GetVariableByName("tex_shadow_map")->GetElement(i)->AsShaderResource(); fx_tex_shadow_map->SetResource(0); } ID3DX11EffectTechnique* tech = getFX()->GetTechniqueByName("Tree"); tech->GetPassByIndex(0)->Apply(0, context()); }
bool Material::save(JsonSerializer& serializer) { serializer.beginObject(); serializer.serialize("shader", m_shader ? m_shader->getPath().c_str() : ""); for (int i = 0; i < m_texture_count; ++i) { char path[MAX_PATH_LENGTH]; int flags = 0; int atlas_size = -1; if (m_textures[i]) { flags = m_textures[i]->getFlags(); path[0] = '/'; Lumix::copyString(path + 1, MAX_PATH_LENGTH - 1, m_textures[i]->getPath().c_str()); atlas_size = m_textures[i]->getAtlasSize(); } else { path[0] = '\0'; } serializer.beginObject("texture"); serializer.serialize("source", path); if (atlas_size > 0) serializer.serialize("atlas_size", atlas_size); if (flags & BGFX_TEXTURE_U_CLAMP) serializer.serialize("u_clamp", true); if (flags & BGFX_TEXTURE_V_CLAMP) serializer.serialize("v_clamp", true); if (flags & BGFX_TEXTURE_W_CLAMP) serializer.serialize("w_clamp", true); if (flags & BGFX_TEXTURE_MIN_POINT) serializer.serialize("min_filter", "point"); if (flags & BGFX_TEXTURE_MIN_ANISOTROPIC) serializer.serialize("min_filter", "anisotropic"); if (flags & BGFX_TEXTURE_MAG_POINT) serializer.serialize("mag_filter", "point"); if (flags & BGFX_TEXTURE_MAG_ANISOTROPIC) serializer.serialize("mag_filter", "anisotropic"); if (m_textures[i] && m_textures[i]->getData()) serializer.serialize("keep_data", true); serializer.endObject(); } serializer.beginArray("uniforms"); for (int i = 0; i < m_uniforms.size(); ++i) { serializer.beginObject(); serializer.serialize("name", m_uniforms[i].m_name); switch (m_uniforms[i].m_type) { case Uniform::FLOAT: serializer.serialize("float_value", m_uniforms[i].m_float); break; case Uniform::TIME: serializer.serialize("time", m_uniforms[i].m_float); break; case Uniform::INT: serializer.serialize("int_value", m_uniforms[i].m_int); break; case Uniform::MATRIX: serializer.beginArray("matrix_value"); for (int j = 0; j < 16; ++j) { serializer.serializeArrayItem(m_uniforms[i].m_matrix[j]); } serializer.endArray(); break; default: ASSERT(false); break; } serializer.endObject(); } serializer.endArray(); serializer.serialize("backface_culling", isBackfaceCulling()); serializer.serialize("alpha_cutout", isAlphaCutout()); serializer.serialize("shadow_receiver", isShadowReceiver()); serializer.serialize("shininess", m_shininess); serializer.beginArray("specular"); serializer.serializeArrayItem(m_specular.x); serializer.serializeArrayItem(m_specular.y); serializer.serializeArrayItem(m_specular.z); serializer.endArray(); serializer.serialize("z_test", isZTest()); serializer.endObject(); return true; }