void Init() { if (origPositionsList == null) { origPositionsList = new List <Vector3>(); } if (queuedColliders == null) { queuedColliders = new List <BoxCollider>(); } if (queuedOrigPositionsList == null) { queuedOrigPositionsList = new List <Vector3>(); } if (allColliders == null) { allColliders = new List <BoxCollider>(); } if (batchSizeDict == null) { batchSizeDict = new Dictionary <Guid, int>(); } if (serialQueue == null) { serialQueue = new List <Guid>(); } colliderShaderParams = new ShaderParams() { ltwMatrix = Matrix4x4.zero, wtlMatrix = Matrix4x4.zero, viw = Vector3.zero }; }
public SDFShadowMaterial(Blending blending) { Blending = blending; shaderParams = new ShaderParams(); shaderParamsArray = new[] { Renderer.GlobalShaderParams, shaderParams }; dilateKey = shaderParams.GetParamKey <float>("dilate"); softnessKey = shaderParams.GetParamKey <float>("softness"); colorKey = shaderParams.GetParamKey <Vector4>("color"); }
public void Render(int width, int height) { if (Params == null) { Params = new ShaderParams(); Shader.GetUniformLocations(Params); } if (LastQuad == 0) // Bail out return; Resources.Texture lastTexture = null; var lastAlpha = false; var lastSRGB = false; var projectionMatrix = Matrix4.CreateOrthographicOffCenter(0.0f, width, height, 0.0f, -1.0f, 1.0f); for (var i = 0; i < LastQuad; i++) { var quad = Quads[i]; if (lastTexture != quad.Texture || lastAlpha != quad.AlphaBlend || lastSRGB != quad.SRGB) { if (lastTexture != null) { Buffer.EndInline(Backend); Backend.BeginInstance(lastSRGB ? ShaderSRGB.Handle : Shader.Handle, new int[] { lastTexture.Handle }, new int[] { Backend.DefaultSamplerNoFiltering }, lastAlpha ? RenderStateAlphaBlend : RenderStateNoAlphaBlend); Backend.BindShaderVariable(Params.HandleDiffuseTexture, 0); Backend.BindShaderVariable(Params.HandleModelViewProjection, ref projectionMatrix); Backend.DrawMesh(Buffer.MeshHandle); Backend.EndInstance(); } Buffer.Begin(); lastSRGB = quad.SRGB; lastTexture = quad.Texture; lastAlpha = quad.AlphaBlend; } Buffer.AddQuadInverseUV(quad.Position, quad.Size, quad.UvPositon, quad.UvSize, quad.Color); } Buffer.EndInline(Backend); // Render final batch Backend.BeginInstance(lastSRGB ? ShaderSRGB.Handle : Shader.Handle, new int[] { lastTexture.Handle }, new int[] { Backend.DefaultSamplerNoFiltering }, lastAlpha ? RenderStateAlphaBlend : RenderStateNoAlphaBlend); Backend.BindShaderVariable(Params.HandleDiffuseTexture, 0); Backend.BindShaderVariable(Params.HandleModelViewProjection, ref projectionMatrix); Backend.DrawMesh(Buffer.MeshHandle); Backend.EndInstance(); LastQuad = 0; }
public void SetShaderParameter(string name, ShaderParamType type, object value) { ShaderParam param = new ShaderParam(); param.Name = name; param.DataValue = value; param.Type = type; if (!ShaderParams.ContainsKey(name)) { ShaderParams.Add(name, param); } else { ShaderParams[name] = param; } }
public void Reload(Material material) { ShaderArchive = ""; ShaderModel = ""; ShaderParams.Clear(); ShaderOptions.Clear(); Samplers.Clear(); TextureMaps.Clear(); UpdateRenderState(); if (material.ShaderAssign != null) { ShaderArchive = material.ShaderAssign.ShaderArchiveName; ShaderModel = material.ShaderAssign.ShadingModelName; } foreach (var param in material.ShaderParams) { ShaderParams.Add(param.Key, param.Value); } foreach (var option in material.ShaderAssign.ShaderOptions) { ShaderOptions.Add(option.Key, option.Value); } // if (ShaderParams.ContainsKey("gsys_i_color_ratio0")) // ShaderParams["gsys_i_color_ratio0"].DataValue = 0.1f; for (int i = 0; i < material.TextureRefs.Count; i++) { string name = material.TextureRefs[i].Name; Sampler sampler = material.Samplers[i]; var texSampler = material.Samplers[i].TexSampler; string samplerName = sampler.Name; string fragSampler = ""; //Force frag shader sampler to be used if (material.ShaderAssign.SamplerAssigns.ContainsValue(samplerName)) { material.ShaderAssign.SamplerAssigns.TryGetKey(samplerName, out fragSampler); } Samplers.Add(fragSampler); this.TextureMaps.Add(new TextureMap() { Name = name, Sampler = samplerName, MagFilter = GXConverter.ConvertMagFilter(texSampler.MagFilter), MinFilter = GXConverter.ConvertMinFilter( texSampler.MipFilter, texSampler.MinFilter), Type = GetTextureType(fragSampler), WrapU = GXConverter.ConvertWrapMode(texSampler.ClampX), WrapV = GXConverter.ConvertWrapMode(texSampler.ClampY), LODBias = texSampler.LodBias, MaxLOD = texSampler.MaxLod, MinLOD = texSampler.MinLod, }); } }
private IEnumerator GPUUpdatePositions() { coroutineTimer.Stop(); coroutineTimer.Reset(); coroutineTimer.Start(); colliderShaderParams.vpc = -gameState.PlayerVelocityVector / (float)gameState.SpeedOfLight; colliderShaderParams.playerOffset = gameState.playerTransform.position; colliderShaderParams.speed = (float)(gameState.PlayerVelocity / gameState.SpeedOfLight); colliderShaderParams.spdOfLight = (float)gameState.SpeedOfLight; ShaderParams[] spa = new ShaderParams[1]; spa[0] = colliderShaderParams; //Put verts in R/W buffer and dispatch: if (paramsBuffer == null) { paramsBuffer = new ComputeBuffer(1, System.Runtime.InteropServices.Marshal.SizeOf(colliderShaderParams)); } paramsBuffer.SetData(spa); if (posBuffer == null) { posBuffer = new ComputeBuffer(origPositionsBufferLength, System.Runtime.InteropServices.Marshal.SizeOf(new Vector3())); } else if (posBuffer.count < origPositionsBufferLength) { posBuffer.Dispose(); posBuffer = new ComputeBuffer(origPositionsBufferLength, System.Runtime.InteropServices.Marshal.SizeOf(new Vector3())); } if (trnsfrmdPositions == null || (trnsfrmdPositions.Length < queuedOrigPositions.Length)) { trnsfrmdPositions = new Vector3[queuedOrigPositions.Length]; } //Read data for frame at last possible moment: if (dispatchedShader) { posBuffer.GetData(trnsfrmdPositions); dispatchedShader = false; } posBuffer.SetData(queuedOrigPositions); int kernel = colliderShader.FindKernel("CSMain"); colliderShader.SetBuffer(kernel, "glblPrms", paramsBuffer); colliderShader.SetBuffer(kernel, "verts", posBuffer); //Dispatch doesn't block, but it might take multiple frames to return: colliderShader.Dispatch(kernel, origPositionsBufferLength, 1, 1); dispatchedShader = true; //Update the old result while waiting: float nanInfTest; for (int i = 0; i < queuedColliders.Count; i++) { nanInfTest = Vector3.Dot(trnsfrmdPositions[i], trnsfrmdPositions[i]); if (!float.IsInfinity(nanInfTest) && !float.IsNaN(nanInfTest)) { if ((!takePriority) && (coroutineTimer.ElapsedMilliseconds > 16)) { coroutineTimer.Stop(); coroutineTimer.Reset(); yield return(null); coroutineTimer.Start(); } queuedColliders[i].center = queuedColliders[i].transform.InverseTransformPoint(trnsfrmdPositions[i]); } } finishedCoroutine = true; coroutineTimer.Stop(); coroutineTimer.Reset(); }
private IEnumerator GPUUpdatePositions() { coroutineTimer.Stop(); coroutineTimer.Reset(); coroutineTimer.Start(); //Debug.Log("Updating mesh collider."); //Freeze the physics if the global state is frozen. if (!isStatic) { if (gameState.MovementFrozen) { if (!myRO.wasFrozen) { //Read the state of the rigidbody and shut it off, once. myRO.wasFrozen = true; myRO.wasKinematic = myRB.isKinematic; myRB.isKinematic = true; } myRO.collideTimeStart += gameState.DeltaTimeWorld; } else if (myRO.wasFrozen) { //Restore the state of the rigidbody, once. myRO.wasFrozen = false; myRO.isKinematic = myRO.wasKinematic; } } //Set remaining global parameters: ShaderParams colliderShaderParams = new ShaderParams(); colliderShaderParams.ltwMatrix = transform.localToWorldMatrix; colliderShaderParams.wtlMatrix = transform.worldToLocalMatrix; if (isStatic) { colliderShaderParams.viw = Vector3.zero; } else { colliderShaderParams.viw = myRO.viw / (float)gameState.SpeedOfLight; } colliderShaderParams.vpc = -gameState.PlayerVelocityVector / (float)gameState.SpeedOfLight; colliderShaderParams.playerOffset = gameState.playerTransform.position; colliderShaderParams.speed = (float)(gameState.PlayerVelocity / gameState.SpeedOfLight); colliderShaderParams.spdOfLight = (float)gameState.SpeedOfLight; //Center of mass in local coordinates should be invariant, // but transforming the collider verts will change it, // so we save it and restore it at the end: Vector3 initCOM = Vector3.zero; if (!isStatic) { initCOM = myRB.centerOfMass; } ShaderParams[] spa = new ShaderParams[1]; spa[0] = colliderShaderParams; //Put verts in R/W buffer and dispatch: if (paramsBuffer == null) { paramsBuffer = new ComputeBuffer(1, System.Runtime.InteropServices.Marshal.SizeOf(colliderShaderParams)); } paramsBuffer.SetData(spa); if (posBuffer == null) { posBuffer = new ComputeBuffer(origPositionsBufferLength, System.Runtime.InteropServices.Marshal.SizeOf(new Vector3())); } else if (posBuffer.count != origPositionsBufferLength) { posBuffer.Dispose(); posBuffer = new ComputeBuffer(origPositionsBufferLength, System.Runtime.InteropServices.Marshal.SizeOf(new Vector3())); } //Read data for frame at last possible moment: if (dispatchedShader) { posBuffer.GetData(trnsfrmdPositions); dispatchedShader = false; } posBuffer.SetData(origPositions); int kernel = colliderShader.FindKernel("CSMain"); colliderShader.SetBuffer(kernel, "glblPrms", paramsBuffer); colliderShader.SetBuffer(kernel, "verts", posBuffer); //Dispatch doesn't block, but it might take multiple frames to return: colliderShader.Dispatch(kernel, origPositionsBufferLength, 1, 1); dispatchedShader = true; //Update the old result while waiting: float nanInfTest; for (int i = 0; i < totalBoxCount; i++) { nanInfTest = Vector3.Dot(trnsfrmdPositions[i], trnsfrmdPositions[i]); if (!float.IsInfinity(nanInfTest) && !float.IsNaN(nanInfTest)) { if (coroutineTimer.ElapsedMilliseconds > 2) { coroutineTimer.Stop(); coroutineTimer.Reset(); yield return(new WaitForFixedUpdate()); coroutineTimer.Start(); } change[i].center = trnsfrmdPositions[i]; } } if (!isStatic) { //Cache actual world center of mass, and then reset local (rest frame) center of mass: myRB.ResetCenterOfMass(); myRO.opticalWorldCenterOfMass = myRB.worldCenterOfMass; myRB.centerOfMass = initCOM; } //Debug.Log("Finished updating mesh collider."); finishedCoroutine = true; coroutineTimer.Stop(); coroutineTimer.Reset(); }
public void Render(Camera camera) { if (Params == null) { Params = new ShaderParams(); Shader.GetUniformLocations(Params); } Matrix4 viewMatrix; camera.GetViewMatrix(out viewMatrix); Matrix4 projectionMatrix; camera.GetProjectionMatrix(out projectionMatrix); var modelViewProjectionMatrix = viewMatrix * projectionMatrix; Batch.End(); Backend.BeginInstance(Shader.Handle, new int[0], null, RenderStateId); Backend.BindShaderVariable(Params.HandleModelViewProjection, ref modelViewProjectionMatrix); Backend.BindShaderVariable(Params.HandleColor, ref Color); Backend.DrawMesh(Batch.MeshHandle); Backend.EndInstance(); TriangleIndex = 0; Batch.Begin(); }