protected override void OnPopulateMesh(Mesh m) { if (particleSystemRenderer) { particleSystemRenderer.BakeMesh(m, useTransform); //particleSystemRenderer.BakeTrailsMesh } }
void UpdateMesh() { CheckTrail(); if (m_ParticleSystem) { if (Application.isPlaying) { _renderer.enabled = false; } var cam = canvas.worldCamera ?? Camera.main; bool useTransform = false; Matrix4x4 matrix = default(Matrix4x4); switch (m_ParticleSystem.main.simulationSpace) { case ParticleSystemSimulationSpace.Local: matrix = Matrix4x4.Rotate(m_ParticleSystem.transform.rotation).inverse *Matrix4x4.Scale(m_ParticleSystem.transform.lossyScale).inverse; useTransform = true; break; case ParticleSystemSimulationSpace.World: matrix = m_ParticleSystem.transform.worldToLocalMatrix; break; case ParticleSystemSimulationSpace.Custom: break; } _mesh.Clear(); if (0 < m_ParticleSystem.particleCount) { if (m_IsTrail) { _renderer.BakeTrailsMesh(_mesh, cam, useTransform); } else { _renderer.BakeMesh(_mesh, cam, useTransform); } _mesh.GetVertices(s_Vertices); var count = s_Vertices.Count; for (int i = 0; i < count; i++) { s_Vertices[i] = matrix.MultiplyPoint3x4(s_Vertices[i]); } _mesh.SetVertices(s_Vertices); s_Vertices.Clear(); } canvasRenderer.SetMesh(_mesh); canvasRenderer.SetTexture(mainTexture); } }
protected override bool ProcessNode(CommandBuffer cmd) { if (!base.ProcessNode(cmd) || system == null) { return(false); } UpdateTempRenderTexture(ref tmpRenderTexture); if (!system.isPlaying) { PlayParticleSystem(); } system.Simulate(Time.deltaTime, true, false, true); // TODO: depth buffer support cmd.SetRenderTarget(tmpRenderTexture); cmd.SetViewport(new Rect(0, 0, tmpRenderTexture.width, tmpRenderTexture.height)); cmd.ClearRenderTarget((clearMode & ClearFlag.Depth) != 0, (clearMode & ClearFlag.Color) != 0, new Color(0, 0, 0, 1), 1.0f); var material = renderer.sharedMaterial; int passIndex = -1; // Try to find the best pass index to render the particles: if (passIndex == -1) { passIndex = renderer.sharedMaterial.FindPass("Forward"); } if (passIndex == -1) { passIndex = renderer.sharedMaterial.FindPass("ForwardOnly"); } if (passIndex == -1) { passIndex = 4; // by default the Particle Unlit color pass is the 4th } mesh.Clear(); renderer.BakeMesh(mesh, true); cmd.SetViewMatrix(Matrix4x4.identity); // We can't apply any rotation otherwise the particle billboard won't follow :/ cmd.SetProjectionMatrix(Matrix4x4.Ortho(-1, 1, -1, 1, -1, 1)); cmd.DrawMesh(mesh, Matrix4x4.identity, renderer.sharedMaterial, 0, passIndex); outputTexture = tmpRenderTexture; return(true); }
/// <summary> /// Update meshe. /// </summary> void UpdateMesh() { if (m_ParticleSystem && lastCount != m_ParticleSystem.particleCount) { lastCount = m_ParticleSystem.particleCount; } else { if (dontManage && gameObject.layer != 16) { return; } } try { Profiler.BeginSample("CheckTrail"); CheckTrail(); Profiler.EndSample(); if (m_ParticleSystem && canvas) { // I do not know why, but it worked fine when setting `transform.localPosition.z` to `0.01`. (#34, #39) //{ // Vector3 pos = rectTransform.localPosition; // if (Mathf.Abs (pos.z) < 0.01f) // { // pos.z = 0.01f; // rectTransform.localPosition = pos; // } //} var rootCanvas = canvas.rootCanvas; Profiler.BeginSample("Disable ParticleSystemRenderer"); if (_renderer && Application.isPlaying) { _renderer.enabled = false; } Profiler.EndSample(); Profiler.BeginSample("Make Matrix"); ParticleSystem.MainModule main = m_ParticleSystem.main; if (!initCulling) { initCulling = true; originCullingMode = m_ParticleSystem.main.cullingMode; } if (m_ParticleSystem.main.cullingMode != ParticleSystemCullingMode.AlwaysSimulate) { var mainParticle = m_ParticleSystem.main; mainParticle.cullingMode = ParticleSystemCullingMode.AlwaysSimulate; } //scaleaMatrix = main.scalingMode == ParticleSystemScalingMode.Hierarchy // ? Matrix4x4.Scale (scale * Vector3.one) // : Matrix4x4.Scale (scale * rootCanvas.transform.localScale); /* * fxc * 不做跟随rootCanvas的缩放 */ scaleaMatrix = Matrix4x4.Scale(scale * Vector3.one); Matrix4x4 matrix = default(Matrix4x4); switch (main.simulationSpace) { case ParticleSystemSimulationSpace.Local: matrix = //scaleaMatrix //* Matrix4x4.Rotate (rectTransform.rotation).inverse //* Matrix4x4.Scale (rectTransform.lossyScale).inverse; /* * fxc * 1.添加zScale解决z为0的情况 * 2.逆矩阵应该先旋转再缩放 */ scaleaMatrix * Matrix4x4.Scale(rectTransform.lossyScale + zScale).inverse *Matrix4x4.Rotate(rectTransform.rotation).inverse; break; case ParticleSystemSimulationSpace.World: matrix = scaleaMatrix * rectTransform.worldToLocalMatrix; bool isLocalScaling = main.scalingMode == ParticleSystemScalingMode.Local; Vector3 newPos = rectTransform.position; Vector3 delta = (newPos - _oldPos); _oldPos = newPos; if (!Mathf.Approximately(scale, 0) && 0 < delta.sqrMagnitude) { if (isLocalScaling) { var s = rootCanvas.transform.localScale * scale; delta.x *= 1f - 1f / s.x; delta.y *= 1f - 1f / s.y; delta.z *= 1f - 1f / s.z; } else { delta = delta * (1 - 1 / scale); } int count = m_ParticleSystem.particleCount; if (s_Particles.Length < count) { s_Particles = new ParticleSystem.Particle [s_Particles.Length * 2]; } m_ParticleSystem.GetParticles(s_Particles); for (int i = 0; i < count; i++) { var p = s_Particles [i]; p.position = p.position + delta; s_Particles [i] = p; } m_ParticleSystem.SetParticles(s_Particles, count); } break; case ParticleSystemSimulationSpace.Custom: break; } Profiler.EndSample(); _mesh.Clear(); if (0 < m_ParticleSystem.particleCount) { Profiler.BeginSample("Bake Mesh"); var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas) : canvas.worldCamera ?? Camera.main; if (!cam) { return; } if (m_IsTrail) { _renderer.BakeTrailsMesh(_mesh, cam, true); } else { _renderer.BakeMesh(_mesh, cam, true); } Profiler.EndSample(); // Apply matrix. Profiler.BeginSample("Apply matrix to position"); _mesh.GetVertices(s_Vertices); var count = s_Vertices.Count; for (int i = 0; i < count; i++) { s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]); } _mesh.SetVertices(s_Vertices); s_Vertices.Clear(); Profiler.EndSample(); } // Set mesh to CanvasRenderer. Profiler.BeginSample("Set mesh and texture to CanvasRenderer"); canvasRenderer.SetMesh(_mesh); canvasRenderer.SetTexture(mainTexture); if (originRenderQueue == 0 && _renderer != null && _renderer.sharedMaterial != null) { originRenderQueue = _renderer.sharedMaterial.renderQueue; //#if UNITY_EDITOR // if (Application.isPlaying && _renderer.material != null) // _renderer.material.renderQueue = 3000; //#else _renderer.sharedMaterial.renderQueue = 3000; //#endif } // Copy the value from MaterialPropertyBlock to CanvasRenderer (#41) UpdateAnimatableMaterialProperties(); Profiler.EndSample(); } } catch (System.Exception e) { Debug.LogException(e); } }
/// <summary> /// Update meshe. /// </summary> void UpdateMesh() { try { Profiler.BeginSample("CheckTrail"); CheckTrail(); Profiler.EndSample(); if (m_ParticleSystem && canvas) { // I do not know why, but it worked fine when setting `transform.localPosition.z` to `0.01`. (#34, #39) { Vector3 pos = rectTransform.localPosition; if (Mathf.Abs(pos.z) < 0.01f) { pos.z = 0.01f; rectTransform.localPosition = pos; } } var rootCanvas = canvas.rootCanvas; Profiler.BeginSample("Disable ParticleSystemRenderer"); if (Application.isPlaying) { _renderer.enabled = false; } Profiler.EndSample(); // #69: Editor crashes when mesh is set to null when ParticleSystem.RenderMode=Mesh if (_renderer.renderMode == ParticleSystemRenderMode.Mesh && !_renderer.mesh) { return; } // #61: When ParticleSystem.RenderMode=None, an error occurs if (_renderer.renderMode == ParticleSystemRenderMode.None) { return; } Profiler.BeginSample("Make Matrix"); ParticleSystem.MainModule main = m_ParticleSystem.main; scaleaMatrix = main.scalingMode == ParticleSystemScalingMode.Hierarchy ? Matrix4x4.Scale(scale * Vector3.one) : Matrix4x4.Scale(scale * rootCanvas.transform.localScale); Matrix4x4 matrix = default(Matrix4x4); switch (main.simulationSpace) { case ParticleSystemSimulationSpace.Local: matrix = scaleaMatrix * Matrix4x4.Rotate(rectTransform.rotation).inverse *Matrix4x4.Scale(rectTransform.lossyScale + minimumVec3).inverse; break; case ParticleSystemSimulationSpace.World: matrix = scaleaMatrix * rectTransform.worldToLocalMatrix; bool isLocalScaling = main.scalingMode == ParticleSystemScalingMode.Local; Vector3 newPos = rectTransform.position; Vector3 delta = (newPos - _oldPos); _oldPos = newPos; if (!Mathf.Approximately(scale, 0) && 0 < delta.sqrMagnitude) { if (isLocalScaling) { var s = rootCanvas.transform.localScale * scale; delta.x *= 1f - 1f / s.x; delta.y *= 1f - 1f / s.y; delta.z *= 1f - 1f / s.z; } else { delta = delta * (1 - 1 / scale); } int count = m_ParticleSystem.particleCount; if (s_Particles.Length < count) { s_Particles = new ParticleSystem.Particle[s_Particles.Length * 2]; } m_ParticleSystem.GetParticles(s_Particles); for (int i = 0; i < count; i++) { var p = s_Particles[i]; p.position = p.position + delta; s_Particles[i] = p; } m_ParticleSystem.SetParticles(s_Particles, count); } break; case ParticleSystemSimulationSpace.Custom: break; } Profiler.EndSample(); _mesh.Clear(); if (0 < m_ParticleSystem.particleCount) { Profiler.BeginSample("Bake Mesh"); var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas) : canvas.worldCamera ?? Camera.main; if (!cam) { return; } if (m_IsTrail) { _renderer.BakeTrailsMesh(_mesh, cam, true); } else { _renderer.BakeMesh(_mesh, cam, true); } Profiler.EndSample(); // Apply matrix. Profiler.BeginSample("Apply matrix to position"); if (QualitySettings.activeColorSpace == ColorSpace.Linear) { _mesh.GetColors(s_Colors); var count_c = s_Colors.Count; for (int i = 0; i < count_c; i++) { s_Colors[i] = ((Color)s_Colors[i]).gamma; } _mesh.SetColors(s_Colors); } _mesh.GetVertices(s_Vertices); var count = s_Vertices.Count; for (int i = 0; i < count; i++) { s_Vertices[i] = matrix.MultiplyPoint3x4(s_Vertices[i]); } _mesh.SetVertices(s_Vertices); _mesh.RecalculateBounds(); s_Vertices.Clear(); s_Colors.Clear(); Profiler.EndSample(); } // Set mesh to CanvasRenderer. Profiler.BeginSample("Set mesh and texture to CanvasRenderer"); canvasRenderer.SetMesh(_mesh); canvasRenderer.SetTexture(mainTexture); // Copy the value from MaterialPropertyBlock to CanvasRenderer (#41) UpdateAnimatableMaterialProperties(); Profiler.EndSample(); } } catch (System.Exception e) { Debug.LogException(e); } }
void UpdateMesh() { try { Profiler.BeginSample("CheckTrail"); CheckTrail(); Profiler.EndSample(); if (m_ParticleSystem) { Profiler.BeginSample("Disable ParticleSystemRenderer"); if (Application.isPlaying) { _renderer.enabled = false; } Profiler.EndSample(); Profiler.BeginSample("Make Matrix"); var s = scale; scaleaMatrix = Matrix4x4.Scale(new Vector3(s, s, s)); Matrix4x4 matrix = default(Matrix4x4); switch (m_ParticleSystem.main.simulationSpace) { case ParticleSystemSimulationSpace.Local: matrix = scaleaMatrix * Matrix4x4.Rotate(m_ParticleSystem.transform.rotation).inverse *Matrix4x4.Scale(m_ParticleSystem.transform.lossyScale).inverse; break; case ParticleSystemSimulationSpace.World: matrix = scaleaMatrix * m_ParticleSystem.transform.worldToLocalMatrix; break; case ParticleSystemSimulationSpace.Custom: break; } Profiler.EndSample(); _mesh.Clear(); if (0 < m_ParticleSystem.particleCount) { Profiler.BeginSample("Bake Mesh"); if (m_IsTrail) { _renderer.BakeTrailsMesh(_mesh, true); } else { _renderer.BakeMesh(_mesh, true); } Profiler.EndSample(); // Apply matrix. Profiler.BeginSample("Apply matrix to position"); _mesh.GetVertices(s_Vertices); var count = s_Vertices.Count; for (int i = 0; i < count; i++) { s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]); } _mesh.SetVertices(s_Vertices); s_Vertices.Clear(); Profiler.EndSample(); } // Set mesh to CanvasRenderer. Profiler.BeginSample("Set mesh and texture to CanvasRenderer"); canvasRenderer.SetMesh(_mesh); canvasRenderer.SetTexture(mainTexture); Profiler.EndSample(); } } catch (System.Exception e) { Debug.LogException(e); } }
// Here you can implement the rendering logic. // Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers // https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html // You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { var cmd = CommandBufferPool.Get(profilerSampler.name); renderBounds = StylizedGrassRenderer.Instance.bounds; frustrumPlanes = GeometryUtility.CalculateFrustumPlanes(StylizedGrassRenderer.Instance.renderCam); using (new ProfilingScope(cmd, profilerSampler)) { foreach (KeyValuePair <int, List <GrassBender> > layer in StylizedGrassRenderer.GrassBenders) { foreach (GrassBender b in layer.Value) { if (b.enabled == false) { continue; } props.SetVector("_Params", new Vector4(b.strength, b.heightOffset, b.pushStrength, b.scaleMultiplier)); if (b.benderType == GrassBenderBase.BenderType.Trail) { if (!b.trailRenderer) { continue; } if (!b.trailRenderer.emitting) { continue; } if (!GeometryUtility.TestPlanesAABB(frustrumPlanes, b.trailRenderer.bounds)) { continue; } m_TrailRenderer = b.trailRenderer; m_TrailRenderer.SetPropertyBlock(props); //Trail m_TrailRenderer.emitting = b.gameObject.activeInHierarchy; m_TrailRenderer.generateLightingData = true; m_TrailRenderer.widthMultiplier = b.trailRadius; m_TrailRenderer.time = b.trailLifetime; m_TrailRenderer.minVertexDistance = b.trailAccuracy; m_TrailRenderer.widthCurve = b.widthOverLifetime; m_TrailRenderer.colorGradient = GrassBenderBase.GetGradient(b.strengthOverLifetime); //If disabled, temporarly enable in order to bake mesh trailEnabled = m_TrailRenderer.enabled ? true : false; if (!trailEnabled) { m_TrailRenderer.enabled = true; } if (b.bakedMesh == null) { b.bakedMesh = new Mesh(); } m_TrailRenderer.BakeMesh(b.bakedMesh, renderingData.cameraData.camera, false); cmd.DrawMesh(b.bakedMesh, Matrix4x4.identity, GrassBenderBase.TrailMaterial, 0, 0, props); //Note: Faster, but crashed when trails are disabled (Case 1200430) //cmd.DrawRenderer(m_TrailRenderer, GrassBenderBase.TrailMaterial, 0, 0); if (!trailEnabled) { m_TrailRenderer.enabled = false; } //trailMesh.Clear(); } if (b.benderType == GrassBenderBase.BenderType.ParticleSystem) { if (!b.particleSystem) { continue; } if (!GeometryUtility.TestPlanesAABB(frustrumPlanes, b.particleRenderer.bounds)) { continue; } m_ParticleRenderer = b.particleRenderer; m_ParticleRenderer.SetPropertyBlock(props); var grad = b.particleSystem.colorOverLifetime; grad.enabled = true; grad.color = GrassBenderBase.GetGradient(b.strengthOverLifetime); bool localSpace = b.particleSystem.main.simulationSpace == ParticleSystemSimulationSpace.Local; //Note: DrawRenderes with particle systems appear to be broken. Only renders to scene cam when it redraws. Bake the mesh down and render it instead. //Todo: Create repo project and file bug report. //cmd.DrawRenderer(m_ParticleRenderer, m_Material, 0, 0); if (!b.bakedMesh) { b.bakedMesh = new Mesh(); } m_ParticleRenderer.BakeMesh(b.bakedMesh, renderingData.cameraData.camera); cmd.DrawMesh(b.bakedMesh, localSpace ? m_ParticleRenderer.localToWorldMatrix : Matrix4x4.identity, GrassBenderBase.MeshMaterial, 0, b.alphaBlending ? 1 : 0, props); //Also draw particle trails if (b.hasParticleTrails) { if (!b.particleTrailMesh) { b.particleTrailMesh = new Mesh(); } m_ParticleRenderer.BakeTrailsMesh(b.particleTrailMesh, renderingData.cameraData.camera); cmd.DrawMesh(b.particleTrailMesh, m_ParticleRenderer.localToWorldMatrix, GrassBenderBase.TrailMaterial, 1, 0, props); //cmd.DrawRenderer(m_ParticleRenderer, GrassBenderBase.TrailMaterial, 1, 0); } } if (b.benderType == GrassBenderBase.BenderType.Mesh) { if (!b.meshRenderer) { continue; } if (!GeometryUtility.TestPlanesAABB(frustrumPlanes, b.meshRenderer.bounds)) { continue; } m_MeshRenderer = b.meshRenderer; m_MeshRenderer.SetPropertyBlock(props); cmd.DrawRenderer(m_MeshRenderer, GrassBenderBase.MeshMaterial, 0, b.alphaBlending ? 1 : 0); } } } //Mask edges of bend area, avoids streaking at edges if (enableEdgeMasking) { cmd.SetGlobalTexture("_BendMapInput", BuiltinRenderTextureType.CurrentActive); cmd.Blit(BuiltinRenderTextureType.CurrentActive, bendVectorID, m_MaskMat); cmd.SetGlobalTexture(StylizedGrassRenderer.VECTOR_MAP_PARAM, bendVectorID); } } context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); }
void Update() { Profiler.BeginSample("CheckTrail"); CheckTrail(); Profiler.EndSample(); if (m_ParticleSystem) { Profiler.BeginSample("Disable ParticleSystemRenderer"); if (Application.isPlaying) { _renderer.enabled = false; } Profiler.EndSample(); Profiler.BeginSample("Make Matrix"); var cam = canvas.worldCamera ?? Camera.main; bool useTransform = false; Matrix4x4 matrix = default(Matrix4x4); switch (m_ParticleSystem.main.simulationSpace) { case ParticleSystemSimulationSpace.Local: matrix = Matrix4x4.Rotate(m_ParticleSystem.transform.rotation).inverse *Matrix4x4.Scale(m_ParticleSystem.transform.lossyScale).inverse; useTransform = true; break; case ParticleSystemSimulationSpace.World: matrix = m_ParticleSystem.transform.worldToLocalMatrix; break; case ParticleSystemSimulationSpace.Custom: break; } Profiler.EndSample(); _mesh.Clear(); if (0 < m_ParticleSystem.particleCount) { Profiler.BeginSample("Bake Mesh"); if (m_IsTrail) { _renderer.BakeTrailsMesh(_mesh, cam, useTransform); } else { _renderer.BakeMesh(_mesh, cam, useTransform); } Profiler.EndSample(); // Apply matrix. Profiler.BeginSample("Apply matrix to position"); _mesh.GetVertices(s_Vertices); var count = s_Vertices.Count; for (int i = 0; i < count; i++) { s_Vertices[i] = matrix.MultiplyPoint3x4(s_Vertices[i]); } _mesh.SetVertices(s_Vertices); s_Vertices.Clear(); Profiler.EndSample(); } // Set mesh to CanvasRenderer. Profiler.BeginSample("Set mesh to CanvasRenderer"); canvasRenderer.SetMesh(_mesh); Profiler.EndSample(); } }
/// <summary> /// Update meshe. /// </summary> void UpdateMesh() { try { Profiler.BeginSample("CheckTrail"); CheckTrail(); Profiler.EndSample(); if (m_ParticleSystem && canvas) { // I do not know why, but it worked fine when setting `transform.localPosition.z` to `0.01`. (#34, #39) { Vector3 pos = rectTransform.localPosition; if (Mathf.Abs(pos.z) < 0.01f) { pos.z = 0.01f; rectTransform.localPosition = pos; } } var rootCanvas = canvas.rootCanvas; Profiler.BeginSample("Disable ParticleSystemRenderer"); if (Application.isPlaying) { _renderer.enabled = false; } Profiler.EndSample(); Profiler.BeginSample("Make Matrix"); scaleaMatrix = m_ParticleSystem.main.scalingMode == ParticleSystemScalingMode.Hierarchy ? Matrix4x4.Scale(scale * Vector3.one) : Matrix4x4.Scale(scale * rootCanvas.transform.localScale); Matrix4x4 matrix = default(Matrix4x4); switch (m_ParticleSystem.main.simulationSpace) { case ParticleSystemSimulationSpace.Local: matrix = scaleaMatrix * Matrix4x4.Rotate(rectTransform.rotation).inverse *Matrix4x4.Scale(rectTransform.lossyScale).inverse; break; case ParticleSystemSimulationSpace.World: matrix = scaleaMatrix * rectTransform.worldToLocalMatrix; Vector3 newPos = rectTransform.position; Vector3 delta = (newPos - _worldPos); _worldPos = newPos; if (canvas.renderMode != RenderMode.WorldSpace && !Mathf.Approximately(scale, 0) && 0 < delta.sqrMagnitude) { delta *= (1 - 1 / scale); int count = m_ParticleSystem.particleCount; if (s_Particles.Length < count) { s_Particles = new ParticleSystem.Particle [s_Particles.Length * 2]; } m_ParticleSystem.GetParticles(s_Particles); for (int i = 0; i < count; i++) { var p = s_Particles [i]; p.position = p.position + delta; s_Particles [i] = p; } m_ParticleSystem.SetParticles(s_Particles, count); } break; case ParticleSystemSimulationSpace.Custom: break; } Profiler.EndSample(); _mesh.Clear(); if (0 < m_ParticleSystem.particleCount) { Profiler.BeginSample("Bake Mesh"); var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas) : canvas.worldCamera ?? Camera.main; if (!cam) { return; } if (m_IsTrail) { _renderer.BakeTrailsMesh(_mesh, cam, true); } else { _renderer.BakeMesh(_mesh, cam, true); } Profiler.EndSample(); // Apply matrix. Profiler.BeginSample("Apply matrix to position"); _mesh.GetVertices(s_Vertices); var count = s_Vertices.Count; for (int i = 0; i < count; i++) { s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]); } _mesh.SetVertices(s_Vertices); s_Vertices.Clear(); Profiler.EndSample(); } // Set mesh to CanvasRenderer. Profiler.BeginSample("Set mesh and texture to CanvasRenderer"); canvasRenderer.SetMesh(_mesh); canvasRenderer.SetTexture(mainTexture); // Copy the value from MaterialPropertyBlock to CanvasRenderer (#41) UpdateAnimatableMaterialProperties(); Profiler.EndSample(); } } catch (System.Exception e) { Debug.LogException(e); } }
/// <summary> /// Update meshe. /// </summary> void UpdateMesh() { try { Profiler.BeginSample("CheckTrail"); CheckTrail(); Profiler.EndSample(); if (m_ParticleSystem && canvas) { if (canvas.renderMode != RenderMode.ScreenSpaceOverlay) { Vector3 pos = rectTransform.localPosition; if (Mathf.Abs(pos.z) < 0.01f) { pos.z = 0.01f; rectTransform.localPosition = pos; } } var rootCanvas = canvas.rootCanvas; Profiler.BeginSample("Disable ParticleSystemRenderer"); if (Application.isPlaying) { _renderer.enabled = false; } Profiler.EndSample(); Profiler.BeginSample("Make Matrix"); scaleaMatrix = m_ParticleSystem.main.scalingMode == ParticleSystemScalingMode.Hierarchy ? Matrix4x4.Scale(scale * Vector3.one) : Matrix4x4.Scale(scale * rootCanvas.transform.localScale); Matrix4x4 matrix = default(Matrix4x4); switch (m_ParticleSystem.main.simulationSpace) { case ParticleSystemSimulationSpace.Local: matrix = scaleaMatrix * Matrix4x4.Rotate(m_ParticleSystem.transform.rotation).inverse *Matrix4x4.Scale(m_ParticleSystem.transform.lossyScale).inverse; break; case ParticleSystemSimulationSpace.World: matrix = scaleaMatrix * m_ParticleSystem.transform.worldToLocalMatrix; break; case ParticleSystemSimulationSpace.Custom: break; } Profiler.EndSample(); _mesh.Clear(); if (0 < m_ParticleSystem.particleCount) { Profiler.BeginSample("Bake Mesh"); var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas) : canvas.worldCamera ?? Camera.main; if (!cam) { return; } if (m_IsTrail) { _renderer.BakeTrailsMesh(_mesh, cam, true); } else { _renderer.BakeMesh(_mesh, cam, true); } Profiler.EndSample(); // Apply matrix. Profiler.BeginSample("Apply matrix to position"); _mesh.GetVertices(s_Vertices); var count = s_Vertices.Count; for (int i = 0; i < count; i++) { s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]); } _mesh.SetVertices(s_Vertices); s_Vertices.Clear(); Profiler.EndSample(); } // Set mesh to CanvasRenderer. Profiler.BeginSample("Set mesh and texture to CanvasRenderer"); canvasRenderer.SetMesh(_mesh); canvasRenderer.SetTexture(mainTexture); Profiler.EndSample(); } } catch (System.Exception e) { Debug.LogException(e); } }
/// <summary> /// Update meshe. /// </summary> void UpdateMesh() { try { Profiler.BeginSample("CheckTrail"); CheckTrail(); Profiler.EndSample(); if (m_ParticleSystem && canvas) { var rootCanvas = canvas.rootCanvas; Profiler.BeginSample("Disable ParticleSystemRenderer"); if (Application.isPlaying) { _renderer.enabled = false; } Profiler.EndSample(); Profiler.BeginSample("Make Matrix"); scaleaMatrix = m_ParticleSystem.main.scalingMode == ParticleSystemScalingMode.Hierarchy ? Matrix4x4.Scale(scale * Vector3.one) : Matrix4x4.Scale(scale * rootCanvas.transform.localScale); Matrix4x4 matrix = default(Matrix4x4); switch (m_ParticleSystem.main.simulationSpace) { case ParticleSystemSimulationSpace.Local: matrix = scaleaMatrix * Matrix4x4.Rotate(m_ParticleSystem.transform.rotation).inverse *Matrix4x4.Scale(m_ParticleSystem.transform.lossyScale).inverse; break; case ParticleSystemSimulationSpace.World: matrix = scaleaMatrix * m_ParticleSystem.transform.worldToLocalMatrix; break; case ParticleSystemSimulationSpace.Custom: break; } Profiler.EndSample(); _mesh.Clear(); if (0 < m_ParticleSystem.particleCount) { Profiler.BeginSample("Bake Mesh"); // If current scene is prefab mode, prevent create OverlayCamera. #if UNITY_2018_3_OR_NEWER && UNITY_EDITOR var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage(); if (prefabStage != null && prefabStage.scene != null && prefabStage.scene.isLoaded) { return; } #endif var cam = rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay ? UIParticleOverlayCamera.GetCameraForOvrelay(rootCanvas) : canvas.worldCamera ?? Camera.main; if (!cam) { return; } if (m_IsTrail) { _renderer.BakeTrailsMesh(_mesh, cam, true); } else { _renderer.BakeMesh(_mesh, cam, true); } Profiler.EndSample(); // Apply matrix. Profiler.BeginSample("Apply matrix to position"); _mesh.GetVertices(s_Vertices); var count = s_Vertices.Count; for (int i = 0; i < count; i++) { s_Vertices [i] = matrix.MultiplyPoint3x4(s_Vertices [i]); } _mesh.SetVertices(s_Vertices); s_Vertices.Clear(); Profiler.EndSample(); } // Set mesh to CanvasRenderer. Profiler.BeginSample("Set mesh and texture to CanvasRenderer"); canvasRenderer.SetMesh(_mesh); canvasRenderer.SetTexture(mainTexture); Profiler.EndSample(); } } catch (System.Exception e) { Debug.LogException(e); } }