private void Update(EvaluationContext context) { var worldToClipSpace = Matrix.Multiply(context.WorldToCamera, context.CameraToClipSpace); //Matrix worldToView = context.WorldToCamera * context.CameraProjection; //var worldToClipSpace = context.WorldToCamera //var viewToWorld = Matrix.Invert(worldToClipSpace); var brightness = Brightness.GetValue(context); var color = Color.GetValue(context); var randomizeColor = RandomizeColor.GetValue(context); var size = Size.GetValue(context); var randomizeSize = RandomizeSize.GetValue(context); var stretch = Stretch.GetValue(context); var distanceFromLight = DistanceFromLight.GetValue(context); var spread = Spread.GetValue(context); var randomizeSpread = RandomizeSpread.GetValue(context); var positionFactor = PositionFactor.GetValue(context); var randomizePosition = RandomizePosition.GetValue(context); var mixPointLightColor = MixPointLightColor.GetValue(context); var referencedLightIndex = LightIndex.GetValue(context); var innerFxZone = InnerFxZone.GetValue(context); var edgeFxZone = EdgeFxZone.GetValue(context); var zoneFxScale = FxZoneScale.GetValue(context); var zoneFxBrightness = FxZoneBrightness.GetValue(context); var matteBoxZone = MattBoxZone.GetValue(context); var rand = new Random(RandomSeed.GetValue(context)); var fxZoneMode = (ZoneFxModes)FxZoneMode.GetValue(context); var rotation = Rotation.GetValue(context); var rotationSpread = RotationSpread.GetValue(context); var rotateTowards = (Categories)RotateTowards.GetValue(context); int startLightIndex = 0; int endLightIndex = context.PointLights.Count; _tempList.Clear(); if (brightness > 0.00001f) { if (referencedLightIndex >= 0) { startLightIndex = referencedLightIndex; endLightIndex = referencedLightIndex + 1; } var aspectRatio = (float)context.RequestedResolution.Width / (float)context.RequestedResolution.Height; for (int lightIndex = startLightIndex; lightIndex < endLightIndex; lightIndex++) { var pointLight = context.PointLights.GetPointLight(lightIndex); var lightPosDx = pointLight.Position.ToSharpDxVector4(1); var posInViewDx = SharpDX.Vector4.Transform(lightPosDx, worldToClipSpace); posInViewDx /= posInViewDx.W; // Ignore light sources behind var hideFactor = posInViewDx.Z < 0 ? 0 : 1; posInViewDx /= posInViewDx.W; var lightPosInView2D = new Vector2(posInViewDx.X, posInViewDx.Y); var count = SpriteCount.GetValue(context).Clamp(0, 1000); if (count != _sprites.NumElements) { _sprites = new StructuredList <Sprite>(count); } // Render Planes for (var i = 0; i < count; ++i) { var f = count <= 1 ? 0 : ((float)i / (count - 1) - 0.5f); var positionOnLine = (float)((-distanceFromLight + f * spread * 2 + randomizeSpread * (rand.NextDouble() - 0.5) + 1)); Vector2 objectScreenPos = lightPosInView2D * positionOnLine * positionFactor + (new Vector2(1, 1) - positionFactor) * lightPosInView2D; objectScreenPos += new Vector2((float)(randomizePosition.X * (rand.NextDouble() - 0.5)), (float)(randomizePosition.Y * (rand.NextDouble() - 0.5))); var sizeWithRandom = size * (float)(1.0 + randomizeSize * (rand.NextDouble() - 0.5)) / 0.2f; var colorWithLight = new Vector4((color.X + randomizeColor.X * (float)(rand.NextDouble() - 0.5) * 4) * MathUtils.Lerp(1f, pointLight.Color.X, mixPointLightColor), (color.Y + randomizeColor.Y * (float)(rand.NextDouble() - 0.5) * 4) * MathUtils.Lerp(1f, pointLight.Color.Y, mixPointLightColor), (color.Z + randomizeColor.Z * (float)(rand.NextDouble() - 0.5) * 4) * MathUtils.Lerp(1f, pointLight.Color.Z, mixPointLightColor), color.W * (1 - randomizeColor.W * (float)(rand.NextDouble() * 2))); var spriteColor = Vector4.Clamp(colorWithLight, Vector4.Zero, new Vector4(100, 100, 100, 1)); var triggerPosition = fxZoneMode == ZoneFxModes.Lights ? lightPosInView2D : objectScreenPos; var d = GetDistanceToEdge(triggerPosition); var cInnerZone = MathUtils.SmootherStep(innerFxZone.Y, innerFxZone.X, 1 - d); var cEdgeZone = MathUtils.SmootherStep(edgeFxZone.X, edgeFxZone.Y, 1 - d); var cMatteBox = MathUtils.SmootherStep(matteBoxZone.Y, matteBoxZone.X, 1 - d); var totalTriggerAmount = (cInnerZone + cEdgeZone) * cMatteBox; sizeWithRandom *= (1 + zoneFxScale * totalTriggerAmount).Clamp(0, 100); var brightnessEffect = (zoneFxBrightness * totalTriggerAmount).Clamp(0, 100); spriteColor.X += brightnessEffect; spriteColor.Y += brightnessEffect; spriteColor.Z += brightnessEffect; spriteColor.W = ((spriteColor.W + brightnessEffect)).Clamp(0, 1); spriteColor.W *= cMatteBox * pointLight.Color.W; // This might actually be a good idea. Maybe we should do this later.. // Fade with incoming alpha from FlatShaders and Materials //color.W *= materialAlpha; float spriteRotation = rotation; switch (rotateTowards) { case Categories.Object: break; case Categories.Light: spriteRotation -= (float)(Math.Atan2((objectScreenPos.X - lightPosInView2D.X) * aspectRatio, objectScreenPos.Y - lightPosInView2D.Y) + MathF.PI) * (180 / MathF.PI); break; case Categories.ScreenCenter: spriteRotation -= (float)(Math.Atan2(objectScreenPos.X, objectScreenPos.Y) + MathF.PI) * 180f / MathF.PI; break; default: throw new ArgumentOutOfRangeException(); } // // Transforom UV to pick correct texture cell // if (TextureCellsRows == 0) // TextureCellsRows = 1; // // if (TextureCellsColumns == 0) // TextureCellsColumns = 1; // int row = (int)(Math.Floor(i / TextureCellsColumns) % TextureCellsRows); // int column = (int)(i % TextureCellsRows); // // var translationUV = new Vector3(1 / TextureCellsColumns * column, 1 / TextureCellsRows * row, 0); // var rotationUV = new Quaternion(); // var scaleUV = new Vector3(1 / TextureCellsColumns, 1 / TextureCellsRows, 0); // var pivotUV = new Vector3(0, 0, 0); // // var transformUV = Matrix.Transformation(pivotUV, new Quaternion(), scaleUV, pivotUV, rotationUV, translationUV); // var prevTransformUV = context.TextureMatrix; // context.TextureMatrix = transformUV * prevTransformUV; spriteColor.W *= brightness; _tempList.Add(new Sprite { PosInClipSpace = objectScreenPos, Size = sizeWithRandom * stretch * hideFactor, Color = spriteColor, RotationDeg = spriteRotation + f * rotationSpread * 180, UvMin = Vector2.Zero, UvMax = Vector2.One, }); } } } // Copy to structured array if (_tempList.Count != _sprites.NumElements) { _sprites = new StructuredList <Sprite>(_tempList.Count); } for (var spriteIndex = 0; spriteIndex < _tempList.Count; spriteIndex++) { _sprites.TypedElements[spriteIndex] = _tempList[spriteIndex]; } OutBuffer.Value = _sprites; }
private void Update(EvaluationContext context) { try { var resourceManager = ResourceManager.Instance(); var scale = Scale.GetValue(context); var stretch = Stretch.GetValue(context); var stretchDX = new SharpDX.Vector3(stretch.X, stretch.Y, stretch.Z); var pivot = Pivot.GetValue(context); var pivotDX = new SharpDX.Vector3(pivot.X, pivot.Y, pivot.Z); var rotation = Rotation.GetValue(context); var cubeRotationMatrix = Matrix.RotationYawPitchRoll(MathUtil.DegreesToRadians(rotation.Y), MathUtil.DegreesToRadians(rotation.X), MathUtil.DegreesToRadians(rotation.Z)); var center = Center.GetValue(context); // var offset = new SharpDX.Vector3(stretch.X * scale * (pivot.X - 0.5f), // stretch.Y * scale * (pivot.Y - 0.5f), // stretch.Z * scale * (pivot.Z - 0.5f)); var offset = -SharpDX.Vector3.One * 0.5f; var center2 = new SharpDX.Vector3(center.X, center.Y, center.Z); var segments = Segments.GetValue(context); _xSegments = segments.X.Clamp(1, 10000) + 1; _ySegments = segments.Y.Clamp(1, 10000) + 1; _zSegments = segments.Z.Clamp(1, 10000) + 1; var faceCount = (_ySegments - 1) * (_xSegments - 1) * 2 * 2 // front / back + (_ySegments - 1) * (_zSegments - 1) * 2 * 2 // top / bottom + (_xSegments - 1) * (_zSegments - 1) * 2 * 2; // left / right var verticesCount = (_ySegments * _xSegments + _ySegments * _zSegments + _xSegments * _zSegments) * 2; // Create buffers if (_vertexBufferData.Length != verticesCount) { _vertexBufferData = new PbrVertex[verticesCount]; } if (_indexBufferData.Length != faceCount) { _indexBufferData = new SharpDX.Int3[faceCount]; } int sideFaceIndex = 0; int sideVertexIndex = 0; for (var sideIndex = 0; sideIndex < _sides.Length; sideIndex++) { var side = _sides[sideIndex]; var sideRotationMatrix = Matrix.RotationYawPitchRoll(side.SideRotation.Y, side.SideRotation.X, side.SideRotation.Z); var rotationMatrix = Matrix.Multiply(sideRotationMatrix, cubeRotationMatrix); var columnCount = GetSegmentCountForAxis(side.ColumnAxis); var rowCount = GetSegmentCountForAxis(side.RowAxis); var columnStretch = GetComponentForAxis(side.ColumnAxis, stretch); var rowStretch = GetComponentForAxis(side.RowAxis, stretch); var depthStretch = GetComponentForAxis(side.DepthAxis, stretch); double columnStep = 1.0 / (columnCount - 1); double rowStep = 1.0 / (rowCount - 1); float depthScale = 1f; var normal = SharpDX.Vector3.TransformNormal(SharpDX.Vector3.ForwardLH, rotationMatrix); var tangent = SharpDX.Vector3.TransformNormal(SharpDX.Vector3.Right, rotationMatrix); var binormal = SharpDX.Vector3.TransformNormal(SharpDX.Vector3.Up, rotationMatrix); // Initialize for (int columnIndex = 0; columnIndex < columnCount; ++columnIndex) { var columnFragment = (float)(columnIndex * columnStep); var u0 = columnIndex / ((float)columnCount - 1); for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex) { var rowFragment = (float)(rowIndex * rowStep); var vertexIndex = rowIndex + columnIndex * rowCount + sideVertexIndex; var faceIndex = 2 * (rowIndex + columnIndex * (rowCount - 1)) + sideFaceIndex; var p = new SharpDX.Vector3(columnFragment, rowFragment, depthScale); var v0 = (rowIndex) / ((float)rowCount - 1); var uv0 = new SharpDX.Vector2(u0, v0); var position = (SharpDX.Vector3.TransformNormal(p + offset, sideRotationMatrix) + pivotDX) * stretchDX * scale; position = SharpDX.Vector3.TransformNormal(position, cubeRotationMatrix); _vertexBufferData[vertexIndex + 0] = new PbrVertex { Position = position + center2, Normal = normal, Tangent = tangent, Bitangent = binormal, Texcoord = uv0, Selection = 1, }; if (columnIndex >= columnCount - 1 || rowIndex >= rowCount - 1) { continue; } _indexBufferData[faceIndex + 0] = new SharpDX.Int3(vertexIndex, vertexIndex + rowCount, vertexIndex + 1); _indexBufferData[faceIndex + 1] = new SharpDX.Int3(vertexIndex + rowCount, vertexIndex + rowCount + 1, vertexIndex + 1); } } sideVertexIndex += columnCount * rowCount; sideFaceIndex += (columnCount - 1) * (rowCount - 1) * 2; } // Write Data _vertexBufferWithViews.Buffer = _vertexBuffer; resourceManager.SetupStructuredBuffer(_vertexBufferData, PbrVertex.Stride * verticesCount, PbrVertex.Stride, ref _vertexBuffer); resourceManager.CreateStructuredBufferSrv(_vertexBuffer, ref _vertexBufferWithViews.Srv); resourceManager.CreateStructuredBufferUav(_vertexBuffer, UnorderedAccessViewBufferFlags.None, ref _vertexBufferWithViews.Uav); _indexBufferWithViews.Buffer = _indexBuffer; const int stride = 3 * 4; resourceManager.SetupStructuredBuffer(_indexBufferData, stride * faceCount, stride, ref _indexBuffer); resourceManager.CreateStructuredBufferSrv(_indexBuffer, ref _indexBufferWithViews.Srv); resourceManager.CreateStructuredBufferUav(_indexBuffer, UnorderedAccessViewBufferFlags.None, ref _indexBufferWithViews.Uav); _data.VertexBuffer = _vertexBufferWithViews; _data.IndicesBuffer = _indexBufferWithViews; Data.Value = _data; Data.DirtyFlag.Clear(); } catch (Exception e) { Log.Error("Failed to create cube mesh:" + e.Message); } }
private void Update(EvaluationContext context) { try { var resourceManager = ResourceManager.Instance(); var scale = Scale.GetValue(context); var stretch = Stretch.GetValue(context); var pivot = Pivot.GetValue(context); var rotation = Rotation.GetValue(context); float yaw = MathUtil.DegreesToRadians(rotation.Y); float pitch = MathUtil.DegreesToRadians(rotation.X); float roll = MathUtil.DegreesToRadians(rotation.Z); var rotationMatrix = Matrix.RotationYawPitchRoll(yaw, pitch, roll); //var offset = var center = Center.GetValue(context); //var centerRotated = SharpDX.Vector3.Transform(new SharpDX.Vector3(center.X, center.Y, center.Z), rotationMatrix); var offset = new SharpDX.Vector3(stretch.X * scale * (pivot.X - 0.5f), stretch.Y * scale * (pivot.Y - 0.5f), 0); var center2 = new SharpDX.Vector3(center.X, center.Y, center.Z); var segments = Segments.GetValue(context); var columns = segments.Width.Clamp(1, 10000) + 1; var rows = segments.Height.Clamp(1, 10000) + 1; var faceCount = (columns - 1) * (rows - 1) * 2; var verticesCount = columns * rows; // Create buffers if (_vertexBufferData.Length != verticesCount) { _vertexBufferData = new PbrVertex[verticesCount]; } if (_indexBufferData.Length != faceCount) { _indexBufferData = new SharpDX.Int3[faceCount]; } double columnStep = (scale * stretch.X) / (columns - 1); double rowStep = (scale * stretch.Y) / (rows - 1); // var normal = SharpDX.Vector3.ForwardRH; // var tangent = SharpDX.Vector3.Right; // var binormal = SharpDX.Vector3.Up; var normal = SharpDX.Vector3.TransformNormal(SharpDX.Vector3.ForwardLH, rotationMatrix); var tangent = SharpDX.Vector3.TransformNormal(SharpDX.Vector3.Right, rotationMatrix); var binormal = SharpDX.Vector3.TransformNormal(SharpDX.Vector3.Up, rotationMatrix); // Initialize for (int columnIndex = 0; columnIndex < columns; ++columnIndex) { var columnFragment = (float)(columnIndex * columnStep); var u0 = columnIndex / ((float)columns - 1); //var v1 = (columnIndex + 1) / (float)columns; for (int rowIndex = 0; rowIndex < rows; ++rowIndex) { var rowFragment = (float)(rowIndex * rowStep); //var rowFragment = ((float)rowIndex / rows - pivot.Y) * stretch.Y; var vertexIndex = rowIndex + columnIndex * rows; var faceIndex = 2 * (rowIndex + columnIndex * (rows - 1)); var p = new SharpDX.Vector3(columnFragment, rowFragment, 0); var v0 = (rowIndex) / ((float)rows - 1); var uv0 = new SharpDX.Vector2(u0, v0); _vertexBufferData[vertexIndex + 0] = new PbrVertex { Position = SharpDX.Vector3.TransformNormal(p + offset, rotationMatrix) + center2, Normal = normal, Tangent = tangent, Bitangent = binormal, Texcoord = uv0, Selection = 1, }; if (columnIndex >= columns - 1 || rowIndex >= rows - 1) { continue; } _indexBufferData[faceIndex + 0] = new SharpDX.Int3(vertexIndex, vertexIndex + rows, vertexIndex + 1); _indexBufferData[faceIndex + 1] = new SharpDX.Int3(vertexIndex + rows, vertexIndex + rows + 1, vertexIndex + 1); } } // Write Data _vertexBufferWithViews.Buffer = _vertexBuffer; resourceManager.SetupStructuredBuffer(_vertexBufferData, PbrVertex.Stride * verticesCount, PbrVertex.Stride, ref _vertexBuffer); resourceManager.CreateStructuredBufferSrv(_vertexBuffer, ref _vertexBufferWithViews.Srv); resourceManager.CreateStructuredBufferUav(_vertexBuffer, UnorderedAccessViewBufferFlags.None, ref _vertexBufferWithViews.Uav); _indexBufferWithViews.Buffer = _indexBuffer; const int stride = 3 * 4; resourceManager.SetupStructuredBuffer(_indexBufferData, stride * faceCount, stride, ref _indexBuffer); resourceManager.CreateStructuredBufferSrv(_indexBuffer, ref _indexBufferWithViews.Srv); resourceManager.CreateStructuredBufferUav(_indexBuffer, UnorderedAccessViewBufferFlags.None, ref _indexBufferWithViews.Uav); _data.VertexBuffer = _vertexBufferWithViews; _data.IndicesBuffer = _indexBufferWithViews; Data.Value = _data; Data.DirtyFlag.Clear(); } catch (Exception e) { Log.Error("Failed to create torus mesh:" + e.Message); } }