Beispiel #1
0
 public override void SetToMaterial(object container, Material material)
 {
     LogTool.AssertNotNull(material);
     if (material == null || !this.Inited)
     {
         return;
     }
     this.SetToGPUBuffer();
     material.SetBuffer(this.shaderName, this.Data);
     material.SetInt(this.shaderName + "Count", this.Size);
 }
Beispiel #2
0
        protected override void OnEnable()
        {
            float E  = this.mpmParameter.E;
            float nu = this.mpmParameter.nu;

            this.mpmParameter.mu.Value     = GetMU(E, nu);
            this.mpmParameter.lambda.Value = GetLambda(E, nu);


            LogTool.AssertNotNull(this.cs);

            this.dispather = new ComputeShaderDispatcher(this.cs);
            //init append buffer
            this.dispather.AddParameter("InitParticle", this.bufferParameter);
            this.dispather.AddParameter("InitParticle", this.soaBuffer);

            this.dispather.AddParameter("P2G", this.parameter);
            this.dispather.AddParameter("P2G", this.bufferParameter);
            this.dispather.AddParameter("P2G", this.mpmParameter);
            this.dispather.AddParameter("P2G", this.soaBuffer);

            this.dispather.AddParameter("UpdateGrid", this.parameter);
            this.dispather.AddParameter("UpdateGrid", this.bufferParameter);
            this.dispather.AddParameter("UpdateGrid", this.mpmParameter);
            this.dispather.AddParameter("UpdateGrid", this.soaBuffer);

            this.dispather.AddParameter("G2P", this.parameter);
            this.dispather.AddParameter("G2P", this.bufferParameter);
            this.dispather.AddParameter("G2P", this.mpmParameter);
            this.dispather.AddParameter("G2P", this.soaBuffer);

            this.dispather.AddParameter("InitGrid", this.mpmParameter);


            this.dispather.AddParameter("AddParticles", this.mpmParameter);
            this.dispather.AddParameter("AddParticles", this.bufferParameter);
            this.dispather.AddParameter("AddParticles", this.soaBuffer);

            this.dispather.AddParameter("CalGridDensity", this.parameter);
            this.dispather.AddParameter("CalGridDensity", this.bufferParameter);
            this.dispather.AddParameter("CalGridDensity", this.mpmParameter);
            this.dispather.AddParameter("CalGridDensity", this.soaBuffer);

            this.dispather.AddParameter("UpdateParticleVolume", this.parameter);
            this.dispather.AddParameter("UpdateParticleVolume", this.bufferParameter);
            this.dispather.AddParameter("UpdateParticleVolume", this.mpmParameter);
            this.dispather.AddParameter("UpdateParticleVolume", this.soaBuffer);


            this.ResizeBuffer(this.parameter.numberOfParticles.Value);

            // this.Emit(512);
        }
Beispiel #3
0
        public virtual void SetToMaterial(object container, Material material)
        {
            LogTool.AssertNotNull(container);
            LogTool.AssertNotNull(material);
            var t     = this.Value.FieldType;
            var value = this.Value.GetValue(container);

            if (TypeSetterMatMap.ContainsKey(t) == false)
            {
                LogTool.Log(t.ToString() + " Handler not found");
                return;
            }

            TypeSetterMatMap[t].Invoke(value, this.shaderName, material, this.mustNotNull);
        }
Beispiel #4
0
        public virtual void SetToGPU(object container, ComputeShader cs, string kernel = null)
        {
            LogTool.AssertNotNull(container);
            LogTool.AssertNotNull(cs);
            var t     = this.Value.FieldType;
            var value = this.Value.GetValue(container);

            if (TypeSetterMap.ContainsKey(t) == false)
            {
                LogTool.Log(t.ToString() + " Handler not found");
                return;
            }

            TypeSetterMap[t].Invoke(value, this.shaderName, cs, kernel, this.mustNotNull);
        }
Beispiel #5
0
        public override void SetToGPU(object container, ComputeShader cs, string kernel = null)
        {
            LogTool.AssertNotNull(cs);
            LogTool.AssertNotNull(kernel);
            // LogTool.AssertIsTrue(this.Size > 0);
            // if (cs == null || this.Size == 0) { LogTool.Log(this.displayName + " is not set to GPU", LogLevel.Warning); return; }
            if (cs == null || !this.Inited)
            {
                return;
            }

            LogTool.AssertNotNull(this.shaderName);

            this.SetToGPUBuffer();
            var id = cs.FindKernel(kernel);

            cs.SetInt(this.shaderName + "Count", this.Size);
            cs.SetBuffer(id, this.shaderName, this.Data);
        }
Beispiel #6
0
        static public void UpdateGradientTexture(Texture2D tex, List <Gradient> from, int size = 128)
        {
            var w = size;
            var h = from.Count;

            LogTool.AssertNotNull(tex);
            LogTool.AssertIsTrue(w == tex.width);
            LogTool.AssertIsTrue(h == tex.height);

            foreach (var y in Enumerable.Range(0, h))
            {
                foreach (var x in Enumerable.Range(0, w))
                {
                    var key   = x * 1f / (w - 1);
                    var color = from[y].Evaluate(key);
                    tex.SetPixel(x, y, color);
                }
            }

            tex.Apply();
        }
Beispiel #7
0
        protected void DoReduction(Texture source, Dictionary <int, Texture> reductionBuffer)
        {
            LogTool.AssertNotNull(source);
            LogTool.AssertNotNull(reductionBuffer);

            Graphics.CopyTexture(source, this.reductionBuffer[math.max(source.width, source.height)]);

            var w = source.width;
            var h = source.height;

            for (; w > 0 || h > 0; w = w >> 1, h = h >> 1)
            {
                var k = this.computeShader.FindKernel(ReductionKernelName);
                var p = math.max(w, h);

                if (p > 1)
                {
                    var from = p;
                    var to   = p >> 1;
                    var tw   = math.max(1, w >> 1);
                    var th   = math.max(1, h >> 1);

                    // Debug.Log("from " + from + " with " + reductionBuffer[from].width + "x" + reductionBuffer[from].height);
                    // Debug.Log("to " + to + " with " + reductionBuffer[to].width + "x" + reductionBuffer[to].height);
                    // Debug.Log("Dispatch " + tw + "x" + th);

                    this.computeShader.SetTexture(k, "_From", reductionBuffer[from]);
                    this.computeShader.SetTexture(k, "_To", reductionBuffer[to]);
                    this.computeShader.Dispatch(k, tw, th, 1);
                }
                else
                {
                    LogTool.AssertIsTrue(p == 1);
                    k = this.computeShader.FindKernel(ResultKernelName);
                    this.computeShader.SetTexture(k, "_From", reductionBuffer[p]);
                    this.computeShader.SetBuffer(k, this.resultBuffer.ShaderName, this.resultBuffer);
                    this.computeShader.Dispatch(k, 1, 1, 1);
                }
            }
        }
Beispiel #8
0
        protected static void MergeVertexTo(INewGraph graph, IVertex from, IVertex to)
        {
            LogTool.AssertIsFalse(from.Equals(to));
            var fromNeighbors = graph.GetNeighborVertices(from as Common.IVertex).ToList();

            foreach (var n in fromNeighbors)
            {
                var e = graph.GetEdge(from as Common.IVertex, n);
                LogTool.AssertNotNull(e);
                graph.Remove(e);

                if (n.Equals(to))
                {
                    continue;
                }
                graph.AddEdge(n, to as Common.IVertex);
            }
            graph.Remove(from as Common.IVertex);

            foreach (var n in fromNeighbors)
            {
                UpdateEdgeCost(graph, n as IVertex);
            }
        }
Beispiel #9
0
 public override void Init(params object[] parameters)
 {
     base.Init();
     LogTool.AssertNotNull(this.mesh);
 }
Beispiel #10
0
        public override IEdge AddEdge(IVertex v1, IVertex v2, bool isDirectional = false)
        {
            if (this.GetEdge(v1, v2) != default)
            {
                return(this.GetEdge(v1, v2));
            }
            //Half edge is always directional
            isDirectional = true;
            var v12Edge = base.AddEdge(v1, v2, isDirectional) as IHalfEdge;
            var v21Edge = base.AddEdge(v2, v1, isDirectional) as IHalfEdge;
            var from    = v1 as IHalfEdgeVertex;
            var to      = v2 as IHalfEdgeVertex;

            if (from.Outgoing == null)
            {
                from.Outgoing = NoneHalfEdgeObject.None;
            }
            if (to.Outgoing == null)
            {
                to.Outgoing = NoneHalfEdgeObject.None;
            }

            if (v12Edge.Face == null)
            {
                v12Edge.Face = NoneHalfEdgeObject.None;
            }
            if (v12Edge.Next == null)
            {
                v12Edge.Next = v21Edge;
            }
            if (v12Edge.Opposite == null)
            {
                v12Edge.Opposite = v21Edge;
            }
            if (v12Edge.Previous == null)
            {
                v12Edge.Previous = v21Edge;
            }

            if (v21Edge.Face == null)
            {
                v21Edge.Face = NoneHalfEdgeObject.None;
            }
            if (v21Edge.Next == null)
            {
                v21Edge.Next = v12Edge;
            }
            if (v21Edge.Opposite == null)
            {
                v21Edge.Opposite = v12Edge;
            }
            if (v21Edge.Previous == null)
            {
                v21Edge.Previous = v12Edge;
            }


            if (from.Outgoing != NoneHalfEdgeObject.None)
            {
                LogTool.AssertIsTrue(from.Outgoing != null);
                LogTool.AssertIsTrue(v21Edge.Vertex == to);
                LogTool.AssertIsTrue(v21Edge.OtherVertex == from);
                var prev = from.Outgoing.Previous;
                var next = from.Outgoing.Previous.Next;

                prev.Next        = v12Edge;
                v12Edge.Previous = prev;

                next.Previous = v21Edge;
                v21Edge.Next  = next;
            }
            if (to.Outgoing != NoneHalfEdgeObject.None)
            {
                LogTool.AssertIsTrue(to.Outgoing != null);
                LogTool.AssertIsTrue(v12Edge.Vertex == from);
                LogTool.AssertIsTrue(v12Edge.OtherVertex == to);

                var prev = to.Outgoing.Previous;
                var next = to.Outgoing.Previous.Next;

                prev.Next        = v21Edge;
                v21Edge.Previous = prev;

                next.Previous = v12Edge;
                v12Edge.Next  = next;
            }

            from.Outgoing = v12Edge;
            to.Outgoing   = v21Edge;

            this.CheckFace(v12Edge);
            this.CheckFace(v21Edge);

            LogTool.AssertIsTrue(v12Edge.Opposite == v21Edge);
            LogTool.AssertIsTrue(v21Edge.Opposite == v12Edge);
            LogTool.AssertNotNull(v12Edge.Face);
            LogTool.AssertNotNull(v21Edge.Face);
            return(v12Edge);
        }