public unsafe bool UpdateSimpleMaterialBGFX(RendererBGFXInstance *sys, ref SimpleMaterial mat, ref SimpleMaterialBGFX matBGFX, bool srgbColors) { // if constants changed, need to update packed value matBGFX.constAlbedo_Opacity = srgbColors ? new float4(Color.LinearToSRGB(mat.constAlbedo), mat.constOpacity) : new float4(mat.constAlbedo, mat.constOpacity); // if texture entity OR load state changed need to update texture handles // content of texture change should transparently update texture referenced by handle bool stillLoading = false; if (InitTexture(ref matBGFX.texAlbedoOpacity, mat.texAlbedoOpacity, sys->m_whiteTexture)) { stillLoading = true; } // if twoSided or hasalpha changed, need to update state matBGFX.state = (ulong)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA | bgfx.StateFlags.DepthTestLess); if (!mat.twoSided && !mat.billboarded) { matBGFX.state |= (ulong)bgfx.StateFlags.CullCw; } if (mat.transparent) { matBGFX.state |= RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha); } else { matBGFX.state |= (ulong)bgfx.StateFlags.WriteZ; } matBGFX.mainTextureScaleTranslate = new float4(mat.scale, mat.offset); matBGFX.billboarded = new float4(mat.billboarded ? 1 : 0, 0, 0, 0); return(!stillLoading); }
public unsafe bool UpdateLitMaterialBGFX(RendererBGFXInstance *sys, ref LitMaterial mat, ref LitMaterialBGFX matBGFX, bool srgbColors) { bool stillLoading = false; if (InitTexture(ref matBGFX.texAlbedoOpacity, mat.texAlbedoOpacity, sys->m_whiteTexture)) { stillLoading = true; } if (InitTexture(ref matBGFX.texNormal, mat.texNormal, sys->m_upTexture)) { stillLoading = true; } if (InitTexture(ref matBGFX.texMetal, mat.texMetal, sys->m_whiteTexture)) { stillLoading = true; } if (InitTexture(ref matBGFX.texEmissive, mat.texEmissive, sys->m_whiteTexture)) { stillLoading = true; } InitShader(ref matBGFX.shaderProgram, mat.shader, sys->m_litShader.m_prog); matBGFX.constAlbedo_Opacity = srgbColors ? new float4(Color.LinearToSRGB(mat.constAlbedo), mat.constOpacity) : new float4(mat.constAlbedo, mat.constOpacity); matBGFX.constMetal_Smoothness_Billboarded = new float4(mat.constMetal, mat.constSmoothness, mat.billboarded ? 1 : 0, 0); matBGFX.constEmissive_normalMapZScale = srgbColors ? new float4(Color.LinearToSRGB(mat.constEmissive), mat.normalMapZScale) : new float4(mat.constEmissive, mat.normalMapZScale); matBGFX.mainTextureScaleTranslate = new float4(mat.scale, mat.offset); matBGFX.smoothness = new float4(0.0f); matBGFX.smoothness.x = (!mat.transparent && mat.smoothnessAlbedoAlpha) ? 1 : 0; matBGFX.smoothness.y = (!mat.transparent && !mat.smoothnessAlbedoAlpha) ? 1 : 0; matBGFX.smoothness.z = !mat.transparent ? 1 : 0; // if twoSided, need to update state matBGFX.state = (ulong)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA | bgfx.StateFlags.DepthTestLess); if (!mat.twoSided && !mat.billboarded) { matBGFX.state |= (ulong)bgfx.StateFlags.CullCcw; } if (mat.transparent) { matBGFX.state |= RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha); } else { matBGFX.state |= (ulong)bgfx.StateFlags.WriteZ; } return(!stillLoading); }
public ulong state; // includes blending and culling! internal unsafe bool Update(EntityManager em, RendererBGFXInstance *sys, ref BitmapFontMaterial mat) { constClipRect = mat.ConstClipRect; constMaskSoftness = mat.ConstMaskSoftness; // if texture entity OR load state changed need to update texture handles // content of texture change should transparently update texture referenced by handle bool stillLoading = UpdateTextMaterialsSystem.InitTexture(em, ref texAtlas, mat.AtlasTexture, sys->m_whiteTexture); // text is always two-sided and transparent state = (ulong)(bgfx.StateFlags.WriteRgb | bgfx.StateFlags.WriteA | bgfx.StateFlags.DepthTestLess); state |= RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendSrcAlpha, bgfx.StateFlags.BlendInvSrcAlpha); return(!stillLoading); }
public static unsafe void EncodeLinePreTransformed(RendererBGFXInstance *sys, bgfx.Encoder *encoder, ushort viewId, SimpleVertex *vertices, int n) { bgfx.TransientIndexBuffer tib; bgfx.TransientVertexBuffer tvb; int ni = (n / 4) * 6; if (!bgfx.alloc_transient_buffers(&tvb, &sys->m_simpleVertexBufferDecl, (uint)n, &tib, (uint)ni)) { throw new InvalidOperationException("Out of transient bgfx memory!"); } UnsafeUtility.MemCpy((SimpleVertex *)tvb.data, vertices, sizeof(SimpleVertex) * n); ushort *indices = (ushort *)tib.data; for (int i = 0; i < n; i += 4) { indices[0] = (ushort)i; indices[1] = (ushort)(i + 1); indices[2] = (ushort)(i + 2); indices[3] = (ushort)(i + 2); indices[4] = (ushort)(i + 3); indices[5] = (ushort)i; indices += 6; } bgfx.encoder_set_transient_index_buffer(encoder, &tib, 0, (uint)ni); bgfx.encoder_set_transient_vertex_buffer(encoder, 0, &tvb, 0, (uint)n, sys->m_simpleVertexBufferDeclHandle); // material uniforms setup ulong state = (ulong)(bgfx.StateFlags.DepthTestLess | bgfx.StateFlags.WriteRgb) | RendererBGFXStatic.MakeBGFXBlend(bgfx.StateFlags.BlendOne, bgfx.StateFlags.BlendInvSrcAlpha); bgfx.encoder_set_state(encoder, state, 0); bgfx.encoder_submit(encoder, viewId, sys->m_lineShader.m_prog, 0, (byte)bgfx.DiscardFlags.All); }