protected void CreatePaintTextureData() { paintTextureData = new TextureSpaceData(this.gameObject.name, resolution); }
// Protected Class Methods. // --------------------------------------------------------------- public void SimulateSpray() { // Detect whether the spray button is pressed. if ( (GetComponent <StickyGrabbable>() != null && GetComponent <StickyGrabbable>().isGrabbed&& ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Trigger)) || (GetComponent <VivePoseTracker>() != null && GetComponent <VivePoseTracker>().enabled&& ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Trigger)) || (Input.GetKeyDown(KeyCode.Space) && GameObject.FindObjectOfType <ARRender>() == null) ) { Ray ray = new Ray(spraySourceObj.transform.position, spraySourceObj.transform.forward); ViveInput.TriggerHapticPulse(HandRole.RightHand, 500); #if UNITY_EDITOR // DebugUtil.DrawVisRay(ray, 1000.0f, Color.white); #endif //float triggerValue = ViveInput.GetTriggerValue(HandRole.RightHand, false); //float mappedPressAmount = 1; // float sprayAngle = PaintUtil.ComputeSprayAngle(mappedPressAmount, ParameterManager.instance.CurrentSprayAngle, ParameterManager.instance.minSprayAngleRatio); // float sprayDist = PaintUtil.ComputeSprayDistance(mappedPressAmount, ParameterManager.instance.sprayDistMappingCurve, ParameterManager.instance.CurrentMaxSprayDist); float sprayAngle = ParameterManager.instance.CurrentSprayAngle; float sprayDist = 200; //float sprayAlpha = ComputeSprayAlpha(mappedPressAmount, ParameterManager.instance.sprayAlphaMappingCurve); // int layer = 1 << PaintComponentDefine.PaintObjectLayer; RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, sprayDist, hitLayerMask) && hitInfo.transform.tag == PaintComponentDefine.PaintMeshTag) { sprayDist = Vector3.Distance(ray.origin, hitInfo.point); pbsPaintObjController = hitInfo.collider.gameObject.GetComponent <PBSPaintableObject>(); if (pbsPaintObjController == null) { Debug.LogWarning("hitInfo.collider not has PBSPaintableObject : " + hitInfo.collider.name); } else { TextureSpaceData paintTextureData = pbsPaintObjController.PaintTextureData; pbsPaintObjController.InvokeOnPaintingEvent(); if (useGBufferRT) { GBufferGenerator posTexGen = gBufferGen; sprayMaterial.SetTexture("_PosMap", posTexGen.PositionMap); sprayMaterial.SetTexture("_NormalMap", posTexGen.NormalMap); } else { sprayMaterial.SetTexture("_PosMap", pbsPaintObjController.PositionMap); sprayMaterial.SetTexture("_NormalMap", pbsPaintObjController.NormalMap); } Vector3 hitPos = hitInfo.point; sprayMaterial.SetVector("_SprayCenter", new Vector4(hitPos.x, hitPos.y, hitPos.z, 1.0f)); // Set spray color. sprayMaterial.SetColor("_SprayColor", ParameterManager.instance.CurrentPaintColor); // Set spray source. //Vector3 spraySrc = spraySourceObj.transform.position; Vector3 spraySrc = hitInfo.point - ray.direction * hitPointToCamera; sprayMaterial.SetVector("_SpraySource", new Vector4(spraySrc.x, spraySrc.y, spraySrc.z, 1.0f)); // Compute spray cutoff. float ratio = ParameterManager.instance.sprayAngleHalfRatioCurve.Evaluate(sprayAngle); sprayMaterial.SetFloat("_SprayInnerAngle", Mathf.Cos(Mathf.Deg2Rad * sprayAngle * ratio)); sprayMaterial.SetFloat("_SprayOuterAngle", Mathf.Cos(Mathf.Deg2Rad * sprayAngle)); // Set spray alpha. sprayMaterial.SetFloat("_SprayAlpha", 1f); Matrix4x4 worldMat = hitInfo.collider.gameObject.GetComponent <Renderer>().localToWorldMatrix; Matrix4x4 normalMat = worldMat.inverse.transpose; sprayMaterial.SetMatrix("_WorldMatrix", worldMat); sprayMaterial.SetMatrix("_NormalMatrix", normalMat); // Update depth map. float distance = Vector3.Distance(spraySourceObj.transform.position, spraySrc); UpdateDepthCam(sprayAngle, distance); sprayMaterial.SetTexture("_DepthMap", depthRT); // Build light matrix. Matrix4x4 lightViewProjMat = depthCam.projectionMatrix * depthCam.worldToCameraMatrix; sprayMaterial.SetMatrix("_LightViewProjMatrix", lightViewProjMat); sprayMaterial.SetMatrix("_LightViewMatrix", depthCam.worldToCameraMatrix); // Set brush pattern mask. sprayMaterial.SetTexture("_CookieMap", defaultPattern); RenderTexture rt = RenderTexture.GetTemporary(paintTextureData.AlbedoTexture.width, paintTextureData.AlbedoTexture.height, 0, paintTextureData.AlbedoTexture.format); Graphics.Blit(paintTextureData.AlbedoTexture, rt); Graphics.Blit(rt, paintTextureData.AlbedoTexture, sprayMaterial); RenderTexture.ReleaseTemporary(rt); // Update color history. EventManager.TriggerUpdateColorHistoryEvent(ParameterManager.instance.CurrentPaintColor); EnableShotEffect(spraySourceObj.transform.position, hitInfo.point); } } else { hitInfo.point = ray.origin + sprayDist * ray.direction; if (ScoreManager.instance != null) { ScoreManager.instance.ShotEvent(false); } } EnableShotEffect(spraySourceObj.transform.position, hitInfo.point); particle.Play(); RandomColorSelect(); } }