Jump to content

Realistic sniper rifle scope in LUA

tipforeveryone

2,161 views

This tutorial will help you to make something like this

 

2b9083ba-500c-4d59-897a-6494bdc8fd7c_zpscqcewlby.jpg

 

realistic%20scope%20concept_zpskg1rblqf.png

 

Step 1: Prepare scope model

 

In 3D application, import your 3d scope model and adjust its pivot point. Make sure the pivot's position looks like image below. This step is extreme important! The position of pivot point is the position of Scope Camera Render Target

 

If you use Blender, press Ctrl + A > Location / Scale / Rotation to reposition scope's pivot

 

Example model file attached at the bottom of this entry

 

1f4feaec-14f6-48b8-bbb6-7a1f83ad8df6_zpssmxszzgq.png

5cb64799-7742-4e96-b9d1-99b51fa00e59_zpsagx4w3if.png

15d57c9e-07fb-426a-929e-517fc87af1df_zpsopjgcizy.png

 

Export this scope model to FBX in Leadwerks

 

Step 2: Build scope's components

 

According to above concept, we have 3 main components: Scope Camera, Scope Model, Scope Camera Render Target. We will create them, one by one using LUA

 

First, import Scope Model to the scene > create a new Script > rename it to "Scope.lua" > apply script to Scope Model.

 

Here is the code

 

Script.scopePhysicalLength = 1
--This is actually the distance from Scope Camera to Scope Camera Render Target
Script.scopeResolution = 512
--Scope render resolution, higer = better look = performance cost, 256 is fine
Script.scopeFOV = 4

function Script:Start()
--Create Scope Camera Render Target (SCRT) first
self.scopeTexture = Texture:Create(self.scopeResolution,self.scopeResolution)
self.scopeMaterial = Material:Create()
self.scopeMaterial:SetShader("Shaders/Model/Diffuse.shader")
self.scopeMaterial:SetColor(1,1,1,1)
self.scopeMaterial:SetSortMode(true)
--"SortMode = true" will eliminate the unwanted black shade in SCRT when render
self.scopeMaterial:SetTexture(self.scopeTexture)

self.scopeRenderTarget = Model:Cylinder(32)
self.scopeRenderTarget:SetParent(self.entity)
self.scopeRenderTarget:SetPosition(0,0,0)
self.scopeRenderTarget:SetRotation(0,0,0)
self.scopeRenderTarget:SetScale(0.065,0,0.065)
--X and Z axis of scale should be adjusted to make sure SCRT is fit inside Scope Model
self.scopeRenderTarget:SetMaterial(self.scopeMaterial)

--Create Scope Camera
self.scopeCamera = Camera:Create()
self.scopeCamera:SetParent(self.entity)
self.scopeCamera:SetPosition(0,self.scopePhysicalLength,0)
self.scopeCamera:SetRotation(-90,0,0)
self.scopeCamera:SetClearColor(1,1,1)
self.scopeCamera:SetFOV(self.scopeFOV)
--this FOV value should be smaller than 20 to make the zoom effect
self.scopeCamera:SetRenderTarget(self.scopeTexture)

--Create Scope Reticle
self.scopeReticle = Model:Cylinder(32)
self.scopeReticle:SetParent(self.entity)
self.scopeReticle:SetPosition(0,-0.001,0) --make sure Reticle stays between main camera and CSRT
self.scopeReticle:SetRotation(0,0,0)
self.scopeReticle:SetScale(0.065,0,0.065)
--This reticle should be the same size as SCRT
local mtl = Material:Load("Materials/reticle.mat")
self.scopeReticle:SetMaterial(mtl)
end

 

Step 3: Check the resuilt

 

It should look like this

 

2662fb3f-00a3-4e5e-832e-8aa553612c64_zps4cloh6oe.png

418f948b-a147-4279-9881-c9cd34f262f6_zpskjfdjium.png

 

Small tutorial: How to create your own reticle texture

 

- Step 1: Use Photoshop or Gimp to create a PNG image of a reticle, it should look like this

 

_2016-09-26_07-56-51_zpsk6jskrx5.png

 

- Step 2: Save this file to Leadwerks Project Assets Folder (ie: Materials/reticle.png) it will be converted to reticle.tex automatically

 

- Step 3: In Leadwerks, right click reticle.tex > Generate Material then config this material like image below

 

Material%20Editor%20-%20reticle.mat_2016-09-26_08-14-53_zpshf8ftf09.png

 

That is how you got your own scope reticle wink.png

 

*Tip: You can attach Scope Model to a Weapon Model and combine with iron sight feature in my previous tutorial



3 Comments


Recommended Comments

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...